From MAILER-DAEMON Thu Dec 01 14:25:31 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RWCGB-0007Wk-Ph
	for mharc-commit-mailutils@gnu.org; Thu, 01 Dec 2011 14:25:31 -0500
Received: from eggs.gnu.org ([140.186.70.92]:33931)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWCG8-0007WK-Tk
	for commit-mailutils@gnu.org; Thu, 01 Dec 2011 14:25:30 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWCG5-0000bM-GN
	for commit-mailutils@gnu.org; Thu, 01 Dec 2011 14:25:28 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:42852)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWCG5-0000bI-Am
	for commit-mailutils@gnu.org; Thu, 01 Dec 2011 14:25:25 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWCG1-0007fv-7W
	for commit-mailutils@gnu.org; Thu, 01 Dec 2011 19:25:25 +0000
Date: Thu, 01 Dec 2011 19:25:23 +0000
Message-Id: <E1RWCG1-0007fv-7W@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-494-g1ae9bae
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: bd55279a9af52b1e6d88b6d8cb483e17174db106
X-Git-Newrev: 1ae9bae29836a20784c836b630264b9e15e5af13
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 01 Dec 2011 19:25: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=1ae9bae29836a20784c836b630264b9e15e5af13

The branch, master has been updated
       via  1ae9bae29836a20784c836b630264b9e15e5af13 (commit)
       via  7b2c07f9701573f679d292423064c18e074c8cad (commit)
      from  bd55279a9af52b1e6d88b6d8cb483e17174db106 (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 1ae9bae29836a20784c836b630264b9e15e5af13
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 1 21:21:06 2011 +0200

    mail: Use new list functions to simplify vartab-related operations.
    
    * mail/mailvar.c (_mailvar_symbol_to_array): Remove.
    (_mailvar_symbol_to_list): New function.
    (mailvar_make_array): Remove.
    (mailvar_list_copy): New function.
    (mailvar_iterator)<varptr,varcnt,pos>: Remove.
    <varlist,varitr>: New members.
    (mailvar_iterate_next): Use subordinate iterator.
    (mailvar_iterate_first): Likewise.
    (mailvar_print): Use mailvar_make_array.

commit 7b2c07f9701573f679d292423064c18e074c8cad
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 1 15:59:21 2011 +0200

    MH: minor change in folder
    
    * mh/folder.c: Use mu_list_t instead of obstack to keep folder list.

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

Summary of changes:
 mail/mailvar.c |  173 ++++++++++++++++++++++++++++++--------------------------
 mh/folder.c    |  128 +++++++++++++++++++++--------------------
 2 files changed, 159 insertions(+), 142 deletions(-)

diff --git a/mail/mailvar.c b/mail/mailvar.c
index 0413fbd..640388f 100644
--- a/mail/mailvar.c
+++ b/mail/mailvar.c
@@ -343,10 +343,10 @@ mailvar_variable_comp (const void *a, const void *b)
 static int
 mailvar_varptr_comp (const void *a, const void *b)
 {
-  const struct mailvar_variable * const * v1 = a;
-  const struct mailvar_variable * const * v2 = b;
+  const struct mailvar_variable const * v1 = a;
+  const struct mailvar_variable const *v2 = b;
 
-  return strcmp ((*v1)->name, (*v2)->name);
+  return strcmp (v1->name, v2->name);
 }
 
 /* Find mailvar_list entry VAR. If not found and CREATE is not NULL, then
@@ -602,35 +602,34 @@ _mailvar_symbol_count (int set)
     }
 }
 
-void
-_mailvar_symbol_to_array (int set, struct mailvar_variable **vartab)
+static int
+mailvar_mapper (void **itmv, size_t itmc, void *call_data)
+{
+  return MU_LIST_MAP_OK;
+}
+
+int
+_mailvar_symbol_to_list (int set, mu_list_t list)
 {
   struct mailvar_symbol *s;
   for (s = mailvar_tab; s->var.name; s++)
     if (!set || s->var.set)
-      {
-	*vartab = &s->var;
-	vartab++;
-      }
+      mu_list_append (list, &s->var);
+  return 0;
 }
 
-struct mailvar_variable **
-mailvar_make_array (int set, size_t *pcount)
+mu_list_t
+mailvar_list_copy (int set)
 {
-  struct mailvar_variable **vartab;
-  size_t count = 0, symcount;
-
-  symcount = _mailvar_symbol_count (set);
-  mu_list_count (mailvar_list, &count);
-  vartab = xcalloc (symcount + count + 1, sizeof *vartab);
-  mu_list_to_array (mailvar_list, (void**) vartab, count, NULL);
-  _mailvar_symbol_to_array (set, vartab + count);
-  count += symcount;
-  qsort (vartab, count, sizeof *vartab, mailvar_varptr_comp);
-  vartab[count] = NULL;
-  if (pcount)
-    *pcount = count;
-  return vartab;
+  mu_list_t list;
+  
+  if (mailvar_list)
+    mu_list_map (mailvar_list, mailvar_mapper, NULL, 1, &list);
+  else
+    mu_list_create (&list);
+  _mailvar_symbol_to_list (set, list);
+  mu_list_sort (list, mailvar_varptr_comp);
+  return list;
 }
   
 
@@ -638,24 +637,25 @@ struct mailvar_iterator
 {
   const char *prefix;
   int prefixlen;
-  struct mailvar_variable **varptr;
-  size_t varcnt;
-  size_t pos;
+  mu_list_t varlist;
+  mu_iterator_t varitr;
 };
   
 const char *
 mailvar_iterate_next (struct mailvar_iterator *itr)
 {
   struct mailvar_variable *vp;
-  
-  while (itr->pos < itr->varcnt)
+
+  do
     {
-      vp = itr->varptr[itr->pos++];
-  
+      mu_iterator_current (itr->varitr, (void**) &vp);
+      mu_iterator_next (itr->varitr);
+      
       if (strlen (vp->name) >= itr->prefixlen
 	  && strncmp (vp->name, itr->prefix, itr->prefixlen) == 0)
 	return vp->name;
     }
+  while (!mu_iterator_is_done (itr->varitr));
   return NULL;
 }
 
@@ -665,8 +665,9 @@ mailvar_iterate_first (int set, const char *prefix, struct mailvar_iterator **pi
   struct mailvar_iterator *itr = xmalloc (sizeof *itr);
   itr->prefix = prefix;
   itr->prefixlen = strlen (prefix);
-  itr->varptr = mailvar_make_array (set, &itr->varcnt);
-  itr->pos = 0;
+  itr->varlist = mailvar_list_copy (set);
+  mu_list_get_iterator (itr->varlist, &itr->varitr);
+  mu_iterator_first (itr->varitr);
   *pitr = itr;
   return mailvar_iterate_next (itr);
 }
@@ -677,68 +678,80 @@ mailvar_iterate_end (struct mailvar_iterator **pitr)
   if (pitr && *pitr)
     {
       struct mailvar_iterator *itr = *pitr;
-      free (itr->varptr);
+      mu_iterator_destroy (&itr->varitr);
+      mu_list_destroy (&itr->varlist);
       free (itr);
       *pitr = NULL;
     }
 }
 
-void
-mailvar_print (int set)
+struct mailvar_print_closure
 {
-  struct mailvar_variable **vartab;
+  int prettyprint;
   mu_stream_t out;
-  size_t i, count;
-  int width = util_getcols ();
-  int prettyprint = mailvar_get (NULL, "variable-pretty-print",
-				 mailvar_type_boolean, 0) == 0;
-  
-  vartab = mailvar_make_array (set, &count);
-
-  out = open_pager (count);
+  int width;
+};
 
-  for (i = 0; i < count; i++)
+static int
+mailvar_printer (void *item, void *data)
+{
+  struct mailvar_variable *vp = item;
+  struct mailvar_print_closure *clos = data;
+  
+  if (clos->prettyprint)
     {
-      if (prettyprint)
-	{
-	  const struct mailvar_symbol *sym =
-	    find_mailvar_symbol (vartab[i]->name);
+      const struct mailvar_symbol *sym = find_mailvar_symbol (vp->name);
 
-	  if (sym)
-	    {
-	      if (sym->flags & MAILVAR_HIDDEN)
-		continue;
-	      if (sym->flags & MAILVAR_RDONLY)
-		mu_stream_printf (out, "# %s:\n", _("Read-only variable"));
-	      print_descr (out, gettext (sym->descr), 1, 3, width - 1, "# ");
-	    }
-	}
-      switch (vartab[i]->type)
+      if (sym)
 	{
-	case mailvar_type_number:
-	  mu_stream_printf (out, "%s=%d",
-			    vartab[i]->name, vartab[i]->value.number);
-	  break;
+	  if (sym->flags & MAILVAR_HIDDEN)
+	    return 0;
+	  if (sym->flags & MAILVAR_RDONLY)
+	    mu_stream_printf (clos->out, "# %s:\n", _("Read-only variable"));
+	  print_descr (clos->out, gettext (sym->descr), 1, 3,
+		       clos->width - 1, "# ");
+	}
+    }
+  switch (vp->type)
+    {
+    case mailvar_type_number:
+      mu_stream_printf (clos->out, "%s=%d", vp->name, vp->value.number);
+      break;
 	  
-	case mailvar_type_string:
-	  mu_stream_printf (out, "%s=\"%s\"",
-			    vartab[i]->name, vartab[i]->value.string);
-	  break;
+    case mailvar_type_string:
+      mu_stream_printf (clos->out, "%s=\"%s\"", vp->name, vp->value.string);
+      break;
 	  
-	case mailvar_type_boolean:
-	  if (!vartab[i]->value.bool)
-	    mu_stream_printf (out, "no");
-	  mu_stream_printf (out, "%s", vartab[i]->name);
-	  break;
+    case mailvar_type_boolean:
+      if (!vp->value.bool)
+	mu_stream_printf (clos->out, "no");
+      mu_stream_printf (clos->out, "%s", vp->name);
+      break;
 	  
-	case mailvar_type_whatever:
-	  mu_stream_printf (out, "%s %s", vartab[i]->name, _("oops?"));
-	}
-      mu_stream_printf (out, "\n");
+    case mailvar_type_whatever:
+      mu_stream_printf (clos->out, "%s %s", vp->name, _("oops?"));
     }
-  free (vartab);
+  mu_stream_printf (clos->out, "\n");
+  return 0;
+}
 
-  mu_stream_unref (out);
+void
+mailvar_print (int set)
+{
+  mu_list_t varlist;
+  size_t count;
+  struct mailvar_print_closure clos;
+  
+  varlist = mailvar_list_copy (set);
+  mu_list_count (varlist, &count);
+  clos.out = open_pager (count);
+  clos.prettyprint = mailvar_get (NULL, "variable-pretty-print",
+				  mailvar_type_boolean, 0) == 0;
+  clos.width = util_getcols ();
+
+  mu_list_foreach (varlist, mailvar_printer, &clos);
+  mu_list_destroy (&varlist);
+  mu_stream_unref (clos.out);
 }
 
 
diff --git a/mh/folder.c b/mh/folder.c
index b24854f..756015f 100644
--- a/mh/folder.c
+++ b/mh/folder.c
@@ -28,10 +28,6 @@
 
 #include <dirent.h>
 
-#define obstack_chunk_alloc malloc
-#define obstack_chunk_free free
-#include <obstack.h>
-
 static char doc[] = N_("GNU MH folder")"\v"
 N_("Use -help to obtain the list of traditional MH options.");
 static char args_doc[] = N_("[ACTION] [MSG]");
@@ -243,10 +239,7 @@ struct folder_info
   size_t others;           /* Number of non-message files */ 
 };
 
-struct obstack folder_info_stack; /* Memory storage for folder infp */
-struct folder_info *folder_info;  /* Finalized array of information
-				     structures */
-size_t folder_info_count;         /* Number of the entries in the array */
+mu_list_t folder_info_list; /* Memory storage for folder info */
 
 size_t message_count;             /* Total number of messages */
 
@@ -255,9 +248,10 @@ int name_prefix_len;              /* Length of the mu_path_folder_dir */
 void
 install_folder_info (const char *name, struct folder_info *info)
 {
-  info->name = strdup (name) + name_prefix_len;
-  obstack_grow (&folder_info_stack, info, sizeof (*info));
-  folder_info_count++;
+  struct folder_info *new_info = xmalloc (sizeof (*new_info));
+  *new_info = *info;
+  new_info->name = strdup (new_info->name + name_prefix_len);
+  mu_list_append (folder_info_list, new_info);
   message_count += info->message_count;
 }
 
@@ -321,7 +315,7 @@ _scan (const char *name, size_t depth)
       if (fast_mode && depth > 0)
 	{
 	  memset (&info, 0, sizeof (info));
-	  info.name = strdup (name);
+	  info.name = name;
 	  install_folder_info (name, &info);
 	  closedir (dir);
 	  return;
@@ -382,60 +376,69 @@ _scan (const char *name, size_t depth)
   if (depth > 0)
     install_folder_info (name, &info);
 }
-    
-static void
-print_all ()
+
+static int
+_folder_info_printer (void *item, void *data)
 {
-  struct folder_info *info, *end = folder_info + folder_info_count;
+  struct folder_info *info = item;
+  int len = strlen (info->name);
 
-  for (info = folder_info; info < end; info++)
+  if (len < 22)
+    printf ("%22.22s", info->name);
+  else
+    printf ("%s", info->name);
+  
+  if (strcmp (info->name, mh_current_folder ()) == 0)
+    printf ("+");
+  else
+    printf (" ");
+  
+  if (info->message_count)
     {
-      int len = strlen (info->name);
-      if (len < 22)
-	printf ("%22.22s", info->name);
-      else
-	printf ("%s", info->name);
-      
-      if (strcmp (info->name, mh_current_folder ()) == 0)
-	printf ("+");
-      else
-	printf (" ");
-      
-      if (info->message_count)
-	{
-	  printf (ngettext(" has %4lu message  (%4lu-%4lu)",
-			   " has %4lu messages (%4lu-%4lu)",
-			   info->message_count),
-		  (unsigned long) info->message_count,
-		  (unsigned long) info->min,
-		  (unsigned long) info->max);
-	  if (info->cur)
-	    printf ("; cur=%4lu", (unsigned long) info->cur);
-	}
+      printf (ngettext(" has %4lu message  (%4lu-%4lu)",
+		       " has %4lu messages (%4lu-%4lu)",
+		       info->message_count),
+	      (unsigned long) info->message_count,
+	      (unsigned long) info->min,
+	      (unsigned long) info->max);
+      if (info->cur)
+	printf ("; cur=%4lu", (unsigned long) info->cur);
+    }
+  else
+    {
+      printf (_(" has no messages"));
+    }
+  
+  if (info->others)
+    {
+      if (!info->cur)
+	printf (";           ");
       else
-	{
-	  printf (_(" has no messages"));
-	}
-      
-      if (info->others)
-	{
-	  if (!info->cur)
-	    printf (";           ");
-	  else
-	    printf ("; ");
-	  printf (_("(others)"));
-	}
-      printf (".\n");
+	printf ("; ");
+      printf (_("(others)"));
     }
+  printf (".\n");
+  return 0;
+}
+
+static int
+_folder_name_printer (void *item, void *data)
+{
+  struct folder_info *info = item;
+  printf ("%s\n", info->name);
+  return 0;
 }
 
 static void
-print_fast ()
+print_all ()
 {
-  struct folder_info *info, *end = folder_info + folder_info_count;
+  mu_list_foreach (folder_info_list, _folder_info_printer, NULL);
+}
 
-  for (info = folder_info; info < end; info++)
-    printf ("%s\n", info->name);
+static void
+print_fast ()
+{
+  mu_list_foreach (folder_info_list, _folder_name_printer, NULL);
 }
 
 static int
@@ -449,7 +452,7 @@ action_print ()
     name_prefix_len++;
   name_prefix_len++;  /* skip past the slash */
 
-  obstack_init (&folder_info_stack);
+  mu_list_create (&folder_info_list);
 
   if (show_all)
     {
@@ -462,9 +465,7 @@ action_print ()
       free (p);
     }
   
-  folder_info = obstack_finish (&folder_info_stack);
-  qsort (folder_info, folder_info_count, sizeof (folder_info[0]),
-	 folder_info_comp);
+  mu_list_sort (folder_info_list, folder_info_comp);
 
   if (fast_mode)
     print_fast ();
@@ -477,13 +478,16 @@ action_print ()
 
       if (OPTION_IS_SET (print_total))
 	{
+	  size_t folder_count;
+
+	  mu_list_count (folder_info_list, &folder_count);
 	  printf ("\n%24.24s=", _("TOTAL"));
 	  printf (ngettext ("%4lu message  ", "%4lu messages ",
 			    message_count),
 		  (unsigned long) message_count);
 	  printf (ngettext ("in %4lu folder", "in %4lu folders",
-			    folder_info_count),
-		  (unsigned long) folder_info_count);
+			    folder_count),
+		  (unsigned long) folder_count);
 	  printf ("\n");
 	}
     }


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 02 07:33:38 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RWSJ8-0001YV-BY
	for mharc-commit-mailutils@gnu.org; Fri, 02 Dec 2011 07:33:38 -0500
Received: from eggs.gnu.org ([140.186.70.92]:33779)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWSIx-0001YE-Re
	for commit-mailutils@gnu.org; Fri, 02 Dec 2011 07:33:36 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWSIr-00008e-5Q
	for commit-mailutils@gnu.org; Fri, 02 Dec 2011 07:33:27 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:51497)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWSIr-00008Z-1W
	for commit-mailutils@gnu.org; Fri, 02 Dec 2011 07:33:21 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RWSIY-0000UJ-85
	for commit-mailutils@gnu.org; Fri, 02 Dec 2011 12:33:18 +0000
Date: Fri, 02 Dec 2011 12:33:04 +0000
Message-Id: <E1RWSIY-0000UJ-85@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-495-g217ae7a
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 1ae9bae29836a20784c836b630264b9e15e5af13
X-Git-Newrev: 217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 02 Dec 2011 12:33:36 -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=217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4

The branch, master has been updated
       via  217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4 (commit)
      from  1ae9bae29836a20784c836b630264b9e15e5af13 (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 217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 2 00:06:31 2011 +0200

    Split ibmailutils/mailbox/message.c into logically separated and
    easily manageable parts.

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

Summary of changes:
 libmailutils/mailbox/Makefile.am                   |   24 +-
 libmailutils/mailbox/message.c                     | 1363 --------------------
 libmailutils/mailbox/msgattr.c                     |   59 +
 libmailutils/mailbox/msgbody.c                     |   85 ++
 libmailutils/mailbox/msgcpy.c                      |   86 ++
 libmailutils/{list/create.c => mailbox/msgcreat.c} |   42 +-
 libmailutils/mailbox/msgenv.c                      |  161 +++
 libmailutils/mailbox/msgheader.c                   |  163 +++
 libmailutils/mailbox/msglines.c                    |   65 +
 libmu_dbm/name.c => libmailutils/mailbox/msgmbx.c  |   35 +-
 libmailutils/mailbox/msgmod.c                      |   62 +
 .../create.c => libmailutils/mailbox/msgmulti.c    |   66 +-
 .../create.c => libmailutils/mailbox/msgnumparts.c |   60 +-
 libmu_dbm/name.c => libmailutils/mailbox/msgobs.c  |   31 +-
 .../create.c => libmailutils/mailbox/msgpart.c     |   60 +-
 libmu_dbm/name.c => libmailutils/mailbox/msgqid.c  |   38 +-
 libmailutils/mailbox/msgqlines.c                   |   62 +
 libmailutils/mailbox/msgref.c                      |  136 ++
 libmailutils/mailbox/msgsave.c                     |   76 ++
 libmailutils/mailbox/msgsize.c                     |   66 +
 libmailutils/mailbox/msgstream.c                   |  365 ++++++
 libmu_dbm/name.c => libmailutils/mailbox/msguid.c  |   38 +-
 libmailutils/mailbox/msguidl.c                     |  102 ++
 23 files changed, 1696 insertions(+), 1549 deletions(-)
 delete mode 100644 libmailutils/mailbox/message.c
 create mode 100644 libmailutils/mailbox/msgattr.c
 create mode 100644 libmailutils/mailbox/msgbody.c
 create mode 100644 libmailutils/mailbox/msgcpy.c
 copy libmailutils/{list/create.c => mailbox/msgcreat.c} (54%)
 create mode 100644 libmailutils/mailbox/msgenv.c
 create mode 100644 libmailutils/mailbox/msgheader.c
 create mode 100644 libmailutils/mailbox/msglines.c
 copy libmu_dbm/name.c => libmailutils/mailbox/msgmbx.c (61%)
 create mode 100644 libmailutils/mailbox/msgmod.c
 copy libproto/imap/create.c => libmailutils/mailbox/msgmulti.c (50%)
 copy libproto/imap/create.c => libmailutils/mailbox/msgnumparts.c (51%)
 copy libmu_dbm/name.c => libmailutils/mailbox/msgobs.c (61%)
 copy libproto/imap/create.c => libmailutils/mailbox/msgpart.c (51%)
 copy libmu_dbm/name.c => libmailutils/mailbox/msgqid.c (58%)
 create mode 100644 libmailutils/mailbox/msgqlines.c
 create mode 100644 libmailutils/mailbox/msgref.c
 create mode 100644 libmailutils/mailbox/msgsave.c
 create mode 100644 libmailutils/mailbox/msgsize.c
 create mode 100644 libmailutils/mailbox/msgstream.c
 copy libmu_dbm/name.c => libmailutils/mailbox/msguid.c (59%)
 create mode 100644 libmailutils/mailbox/msguidl.c

diff --git a/libmailutils/mailbox/Makefile.am b/libmailutils/mailbox/Makefile.am
index 7f2fe01..27e6c04 100644
--- a/libmailutils/mailbox/Makefile.am
+++ b/libmailutils/mailbox/Makefile.am
@@ -28,7 +28,27 @@ libmailbox_la_SOURCES =  \
  hdrfirst.c\
  hdritr.c\
  header.c\
- message.c\
- msgscan.c
+ msgcpy.c\
+ msgattr.c\
+ msgbody.c\
+ msgcreat.c\
+ msgheader.c\
+ msgenv.c\
+ msglines.c\
+ msgmbx.c\
+ msgmod.c\
+ msgmulti.c\
+ msgnumparts.c\
+ msgobs.c\
+ msgpart.c\
+ msgqid.c\
+ msgqlines.c\
+ msgref.c\
+ msgsave.c\
+ msgscan.c\
+ msgsize.c\
+ msgstream.c\
+ msguid.c\
+ msguidl.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/mailbox/message.c b/libmailutils/mailbox/message.c
deleted file mode 100644
index 8cf233a..0000000
--- a/libmailutils/mailbox/message.c
+++ /dev/null
@@ -1,1363 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
-   2010, 2011 Free Software Foundation, Inc.
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see
-   <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <unistd.h>
-#include <string.h>
-#include <pwd.h>
-
-#include <mailutils/cctype.h>
-#include <mailutils/address.h>
-#include <mailutils/attribute.h>
-#include <mailutils/auth.h>
-#include <mailutils/body.h>
-#include <mailutils/debug.h>
-#include <mailutils/envelope.h>
-#include <mailutils/errno.h>
-#include <mailutils/folder.h>
-#include <mailutils/header.h>
-#include <mailutils/mailbox.h>
-#include <mailutils/util.h>
-#include <mailutils/observer.h>
-#include <mailutils/stream.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/nls.h>
-#include <mailutils/md5.h>
-#include <mailutils/io.h>
-
-#include <mailutils/sys/message.h>
-#include <mailutils/sys/stream.h>
-
-
-/* Message stream */
-
-enum _message_stream_state
-  {
-    _mss_init,
-    _mss_header,
-    _mss_body,
-    _mss_eof
-  };
-
-struct _mu_message_stream
-{
-  struct _mu_stream stream;
-  mu_message_t msg;
-  enum _message_stream_state state;
-  mu_stream_t transport;
-  mu_off_t limit;
-};
-
-static int
-_check_stream_state (struct _mu_message_stream *str)
-{
-  int rc = 0;
-  
-  if (str->transport && mu_stream_eof (str->transport))
-    mu_stream_destroy (&str->transport);
-  
-  switch (str->state)
-    {
-    case _mss_init:
-      if (!str->transport)
-	{
-	  rc = mu_header_get_streamref (str->msg->header, &str->transport);
-	  if (rc == 0)
-	    {
-	      str->state = _mss_header;
-	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
-	    }
-	}
-      break;
-      
-    case _mss_header:
-      if (!str->transport)
-	{
-	  rc = mu_body_get_streamref (str->msg->body, &str->transport);
-	  if (rc == 0)
-	    {
-	      str->state = _mss_body;
-	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
-	    }
-	}
-      break;
-      
-    case _mss_body:
-      if (!str->transport)
-	str->state = _mss_eof;
-    case _mss_eof:
-      break;
-    }
-  return rc;
-}
-
-static void
-_message_stream_done (struct _mu_stream *str)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  mu_stream_destroy (&sp->transport);
-}
-
-static int
-_message_stream_flush (struct _mu_stream *str)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  int rc = _check_stream_state (sp);
-  if (rc)
-    return rc;
-  return mu_stream_flush (sp->transport);
-}
-  
-static int
-_message_stream_size (struct _mu_stream *str, mu_off_t *psize)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t hsize, bsize;
-  mu_header_size (sp->msg->header, &hsize);
-  mu_body_size (sp->msg->body, &bsize);
-  if (psize)
-    *psize = hsize + bsize;
-  return 0;
-}
-
-static int
-_message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t hsize, size;
-  int rc;
-  
-  rc = _check_stream_state (sp);
-  if (rc)
-    return rc;
-  mu_header_size (sp->msg->header, &hsize);
-  mu_body_size (sp->msg->body, &size);
-  
-  if (off < 0 || off >= size + hsize)
-    return ESPIPE;
-
-  switch (sp->state)
-    {
-    case _mss_eof:
-      sp->state = _mss_init;
-      rc = _check_stream_state (sp);
-      if (rc)
-	return rc;
-      /* fall through */
-    case _mss_header:
-      if (off < hsize)
-	break;
-      mu_stream_destroy (&sp->transport);
-      rc = _check_stream_state (sp);
-      if (rc)
-	return rc;
-      /* fall through */
-    case _mss_body:
-      if (off > hsize)
-	off -= hsize;	
-      else
-	{
-	  mu_stream_destroy (&sp->transport);
-	  sp->state = _mss_init;
-	  rc = _check_stream_state (sp);
-	  if (rc)
-	    return rc;
-	}
-
-      break;
-
-    default:
-      break;
-    }
-  rc = mu_stream_seek (sp->transport, off, MU_SEEK_SET, &off);
-  if (rc == 0)
-    {
-      if (sp->state == _mss_body)
-	off += hsize;
-      *ppos = off;
-    }
-  return rc;
-}
-
-static int
-_message_stream_read (struct _mu_stream *str, char *buf, size_t bufsize,
-		      size_t *pnread)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t nread = 0;
-  int rc;
-  
-  while (bufsize)
-    {
-      size_t n;
-      rc = _check_stream_state (sp);
-      if (rc)
-	break;
-      if (sp->state == _mss_eof)
-	break;
-      rc = mu_stream_read (sp->transport, buf, bufsize, &n);
-      if (rc)
-	break;
-      if (n == 0)
-	continue;
-      nread += n;
-      buf += n;
-      bufsize -= n;
-    }
-  *pnread = nread;
-  return rc;
-}
-
-static int
-_message_stream_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
-			   int delim, size_t *pnread)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  size_t nread = 0;
-  int rc;
-  
-  while (bufsize)
-    {
-      size_t n;
-      rc = _check_stream_state (sp);
-      if (rc)
-	break;
-      if (sp->state == _mss_eof)
-	break;
-      rc = mu_stream_readdelim (sp->transport, buf, bufsize, delim, &n);
-      if (rc)
-	break;
-      if (n == 0)
-	continue;
-      nread += n;
-      if (buf[n-1] == delim)
-	break;
-      buf += n;
-      bufsize -= n;
-    }
-  *pnread = nread;
-  return rc;
-}  
-
-#if 0
-static int
-_message_stream_write (struct _mu_stream *str,
-		       const char *buf, size_t bufsize,
-		       size_t *pnwritten)
-{
-  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
-  
-  /* FIXME */
-}
-#endif
-
-static int
-_message_stream_create (mu_stream_t *pmsg, mu_message_t msg, int flags)
-{
-  struct _mu_message_stream *sp;
-
-  sp = (struct _mu_message_stream *) _mu_stream_create (sizeof (*sp),
-							flags |
-							MU_STREAM_SEEK |
-							_MU_STR_OPEN);
-  if (!sp)
-    return ENOMEM;
-
-  sp->stream.read = _message_stream_read;
-  sp->stream.readdelim = _message_stream_readdelim;
-  /* FIXME: Write is not defined */
-  /*  sp->stream.write = _message_stream_write;*/
-  sp->stream.done = _message_stream_done;
-  sp->stream.flush = _message_stream_flush;
-  sp->stream.seek = _message_stream_seek; 
-  sp->stream.size = _message_stream_size;
-  sp->state = _mss_init;
-  sp->msg = msg;
-  *pmsg = (mu_stream_t) sp;
-  return 0;
-}
-
-
-enum eoh_state
-  {
-    eoh_no,
-    eoh_maybe,
-    eoh_yes
-  };
-
-/* Message header stuff */
-static enum eoh_state
-string_find_eoh (enum eoh_state eoh, const char *str, size_t len,
-		 size_t *ppos)
-{
-  size_t pos;
-
-  if (eoh == eoh_maybe && *str == '\n')
-    {
-      *ppos = 0;
-      return eoh_yes;
-    }
-  
-  for (pos = 0; pos < len - 1; pos++)
-    if (str[pos] == '\n' && str[pos + 1] == '\n')
-      {
-	*ppos = pos + 1;
-	return eoh_yes;
-      }
-  
-  *ppos = pos + 1;
-  return str[pos] == '\n' ? eoh_maybe : eoh_no;
-}
-
-#define MIN_HEADER_BUF_SIZE 2048
-
-static int
-_header_fill (mu_stream_t stream, char **pbuf, size_t *plen)
-{
-  int status = 0;
-  char *buffer = NULL;
-  size_t bufsize = 0;
-  char inbuf[MIN_HEADER_BUF_SIZE];
-  size_t nread;
-  enum eoh_state eoh = eoh_no;
-  
-  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-  if (status)
-    return status;
-      
-  while (eoh != eoh_yes
-	 && (status = mu_stream_read (stream, inbuf, sizeof (inbuf), &nread))
-	    == 0
-	 && nread)
-    {
-      char *nbuf;
-      size_t len;
-
-      eoh = string_find_eoh (eoh, inbuf, nread, &len);
-      
-      nbuf = realloc (buffer, bufsize + len);
-      if (!nbuf)
-	{
-	  status = ENOMEM;
-	  break;
-	}
-      memcpy (nbuf + bufsize, inbuf, len);
-      buffer = nbuf;
-      bufsize += len;
-    }
-
-  if (status)
-    free (buffer);
-  else
-    {
-      *pbuf = buffer;
-      *plen = bufsize;
-    }
-  return status;
-}
-    
-static int
-message_header_fill (void *data, char **pbuf, size_t *plen)
-{
-  int status = 0;
-  mu_message_t msg = data;
-  mu_stream_t stream;
-
-  status = mu_message_get_streamref (msg, &stream);
-  if (status == 0)
-    {
-      status = _header_fill (stream, pbuf, plen);
-      mu_stream_destroy (&stream);
-    }
-  return status;
-}
-
-
-/* Message envelope */
-static int
-message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
-		       size_t *pnwrite)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  time_t t;
-  size_t n;
-
-  if (msg == NULL)
-    return EINVAL;
-
-  /* FIXME: extract the time from "Date:".  */
-
-  if (buf == NULL || len == 0)
-    {
-      n = MU_ENVELOPE_DATE_LENGTH;
-    }
-  else
-    {
-      char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
-      t = time (NULL);
-      n = mu_strftime (tmpbuf, sizeof tmpbuf, 
-                       MU_ENVELOPE_DATE_FORMAT, localtime (&t));
-      n = mu_cpystr (buf, tmpbuf, len);
-    }
-  if (pnwrite)
-    *pnwrite = n;
-  return 0;
-}
-
-static int
-message_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
-			 size_t *pnwrite)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  mu_header_t header;
-  int status;
-  const char *sender;
-  struct mu_auth_data *auth = NULL;
-  static char *hdrnames[] = {
-    "X-Envelope-Sender",
-    "X-Envelope-From",
-    "X-Original-Sender",
-    "From",
-    NULL
-  };
-  mu_address_t address = NULL;
-
-  if (msg == NULL)
-    return EINVAL;
-
-  /* First, try the header  */
-  status = mu_message_get_header (msg, &header);
-  if (status)
-    return status;
-  status = mu_header_sget_firstof (header, hdrnames, &sender, NULL);
-  if (status)
-    {
-      auth = mu_get_auth_by_uid (getuid ());
-      if (!auth)
-	return MU_ERR_NOENT;
-      sender = auth->name;
-    }
-
-  status = mu_address_create (&address, sender);
-  if (status == 0)
-    {
-      status = mu_address_sget_email (address, 1, &sender);
-      if (status == 0)
-	{
-	  size_t n = strlen (sender);
-	  if (buf && len > 0)
-	    {
-	      len--; /* One for the null.  */
-	      n = (n < len) ? n : len;
-	      memcpy (buf, sender, n);
-	      buf[n] = '\0';
-	    }
-	  if (pnwrite)
-	    *pnwrite = n;
-	}
-      mu_address_destroy (&address);
-    }
-  
-  if (auth)
-    mu_auth_data_free (auth);
-
-  return status;
-}
-
-
-
-/*  Allocate ressources for the mu_message_t.  */
-int
-mu_message_create (mu_message_t *pmsg, void *owner)
-{
-  mu_message_t msg;
-  int status;
-
-  if (pmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  msg = calloc (1, sizeof (*msg));
-  if (msg == NULL)
-    return ENOMEM;
-  status = mu_monitor_create (&msg->monitor, 0, msg);
-  if (status != 0)
-    {
-      free (msg);
-      return status;
-    }
-  msg->owner = owner;
-  msg->ref_count = 1;
-  *pmsg = msg;
-  return 0;
-}
-
-/* Free the message and all associated stuff */
-static void
-_mu_message_free (mu_message_t msg)
-{
-  /* Notify the listeners.  */
-  /* FIXME: to be removed since we do not support this event.  */
-  if (msg->observable)
-    {
-      mu_observable_notify (msg->observable, MU_EVT_MESSAGE_DESTROY, msg);
-      mu_observable_destroy (&msg->observable, msg);
-    }
-      
-  /* Envelope.  */
-  if (msg->envelope)
-    mu_envelope_destroy (&msg->envelope, msg);
-      
-  /* Header.  */
-  if (msg->header)
-    mu_header_destroy (&msg->header);
-      
-  /* Body.  */
-  if (msg->body)
-    mu_body_destroy (&msg->body, msg);
-      
-  /* Attribute.  */
-  if (msg->attribute)
-    mu_attribute_destroy (&msg->attribute, msg);
-      
-  /* Stream.  */
-  if (msg->stream)
-    mu_stream_destroy (&msg->stream);
-      
-  /*  Mime.  */
-  if (msg->flags & MESSAGE_MIME_OWNER)
-    mu_mime_destroy (&msg->mime);
-      
-  /* Loose the owner.  */
-  msg->owner = NULL;
-      
-  free (msg);
-}
-
-void
-mu_message_unref (mu_message_t msg)
-{
-  if (msg)
-    {
-      mu_monitor_t monitor = msg->monitor;
-      mu_monitor_wrlock (monitor);
-      /* Note: msg->ref may be incremented by mu_message_ref without
-	 additional checking for its owner, therefore decrementing
-	 it must also occur independently of the owner checking. Due
-	 to this inconsistency ref may reach negative values, which
-	 is very unfortunate.
-	 
-	 The `owner' stuff is a leftover from older mailutils versions.
-	 We are heading to removing it altogether. */
-      if (msg->ref_count > 0)
-	msg->ref_count--;
-      if (msg->ref_count == 0)
-	{
-	  _mu_message_free (msg);
-	  mu_monitor_unlock (monitor);
-	  mu_monitor_destroy (&monitor, msg);
-	}
-      else
-	mu_monitor_unlock (monitor);
-    }
-}
-
-void
-mu_message_destroy (mu_message_t *pmsg, void *owner)
-{
-  if (pmsg && *pmsg)
-    {
-      mu_message_t msg = *pmsg;
-      
-      mu_monitor_t monitor = msg->monitor;
-      mu_monitor_wrlock (monitor);
-
-      if (msg->owner && msg->owner == owner)
-	{
-	  _mu_message_free (msg);
-	  mu_monitor_unlock (monitor);
-	  mu_monitor_destroy (&monitor, msg);
-	  *pmsg = NULL;
-	  return;
-	}
-      mu_monitor_unlock (monitor);
-    }
-}
-
-int
-mu_message_create_copy (mu_message_t *to, mu_message_t from)
-{
-  int status = 0;
-  mu_stream_t fromstr = NULL;
-  mu_stream_t tmp = NULL;
-
-  if (!to)
-    return MU_ERR_OUT_PTR_NULL;
-  if (!from)
-    return EINVAL;
-
-  status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
-  if (status)
-    return status;
-
-  status = mu_message_get_streamref (from, &fromstr);
-  if (status)
-    {
-      mu_stream_destroy (&tmp);
-      return status;
-    }
-
-  status = mu_stream_copy (tmp, fromstr, 0, NULL);
-  if (status == 0)
-    {
-      status = mu_message_create (to, NULL);
-      if (status == 0)
-	mu_message_set_stream (*to, tmp, NULL);
-    }
-
-  if (status)
-    mu_stream_destroy (&tmp);
-  mu_stream_destroy (&fromstr);
-
-  return status;
-}
-
-void
-mu_message_ref (mu_message_t msg)
-{
-  if (msg)
-    {
-      mu_monitor_wrlock (msg->monitor);
-      msg->ref_count++;
-      mu_monitor_unlock (msg->monitor);
-    }
-}
-
-void *
-mu_message_get_owner (mu_message_t msg)
-{
-  return (msg == NULL) ? NULL : msg->owner;
-}
-
-int
-mu_message_is_modified (mu_message_t msg)
-{
-  int mod = 0;
-  if (msg)
-    {
-      if (mu_header_is_modified (msg->header))
-	mod |= MU_MSG_HEADER_MODIFIED;
-      if (mu_attribute_is_modified (msg->attribute))
-	mod |= MU_MSG_ATTRIBUTE_MODIFIED;
-      if (mu_body_is_modified (msg->body))
-	mod |= MU_MSG_BODY_MODIFIED;
-      if (msg->flags & MESSAGE_MODIFIED)
-	mod |= MU_MSG_BODY_MODIFIED | MU_MSG_HEADER_MODIFIED;
-    }
-  return mod;
-}
-
-int
-mu_message_clear_modified (mu_message_t msg)
-{
-  if (msg)
-    {
-      if (msg->header)
-	mu_header_clear_modified (msg->header);
-      if (msg->attribute)
-	mu_attribute_clear_modified (msg->attribute);
-      if (msg->body)
-	mu_body_clear_modified (msg->body);
-      msg->flags &= ~MESSAGE_MODIFIED;
-    }
-  return 0;
-}
-
-int
-mu_message_get_mailbox (mu_message_t msg, mu_mailbox_t *pmailbox)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (pmailbox == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  *pmailbox = msg->mailbox;
-  return 0;
-}
-
-int
-mu_message_set_mailbox (mu_message_t msg, mu_mailbox_t mailbox, void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->mailbox = mailbox;
-  return 0;
-}
-
-int
-mu_message_get_header (mu_message_t msg, mu_header_t *phdr)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (phdr == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg->header == NULL)
-    {
-      mu_header_t header;
-      int status = mu_header_create (&header, NULL, 0);
-      if (status != 0)
-	return status;
-      if (msg->stream)
-	mu_header_set_fill (header, message_header_fill, msg);
-      status = mu_header_size (header, &msg->orig_header_size);
-      if (status)
-	return status;
-      msg->header = header;
-    }
-  *phdr = msg->header;
-  return 0;
-}
-
-/* Note: mu_message_set_header steals the reference to hdr */
-int
-mu_message_set_header (mu_message_t msg, mu_header_t hdr, void *owner)
-{
-  if (msg == NULL )
-    return EINVAL;
-  if (msg->owner != owner)
-     return EACCES;
-  if (msg->header)
-    mu_header_destroy (&msg->header);
-  msg->header = hdr;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (pbody == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  /* Is it a floating mesg.  */
-  if (msg->body == NULL)
-    {
-      mu_body_t body;
-      int status = mu_body_create (&body, msg);
-      if (status != 0)
-	return status;
-      /* If a stream is already set, use it to create the body stream.  */
-      /* FIXME: I'm not sure if the second condition is really needed */
-      if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
-	{
-	  mu_stream_t stream;
-	  int flags = 0;
-
-	  /* FIXME: The actual mu_header_size cannot be used as offset,
-	     because the headers might have been modified in between. */
-	  
-	  mu_stream_get_flags (msg->stream, &flags);
-	  status = mu_streamref_create_abridged (&stream, msg->stream,
-						 msg->orig_header_size, 0);
-	  if (status)
-	    {
-	      mu_body_destroy (&body, msg);
-	      return status;
-	    }
-	  mu_body_set_stream (body, stream, msg);
-	}
-      msg->body = body;
-    }
-  *pbody = msg->body;
-  return 0;
-}
-
-int
-mu_message_set_body (mu_message_t msg, mu_body_t body, void *owner)
-{
-  if (msg == NULL )
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  /* Make sure we destroy the old if it was owned by the mesg.  */
-  /* FIXME:  I do not know if somebody has already a ref on this ? */
-  if (msg->body)
-    mu_body_destroy (&msg->body, msg);
-  msg->body = body;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_set_stream (mu_message_t msg, mu_stream_t stream, void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  if (msg->stream)
-    mu_stream_destroy (&msg->stream);
-  msg->stream = stream;
-  msg->flags |= MESSAGE_MODIFIED;
-  msg->flags &= ~MESSAGE_INTERNAL_STREAM;
-  return 0;
-}
-
-static int
-_message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
-{
-  int status;
-
-  if (msg == NULL)
-    return EINVAL;
-  if (pstream == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg->stream == NULL)
-    {
-      if (msg->_get_stream)
-	{
-	  status = msg->_get_stream (msg, &msg->stream);
-	  if (status)
-	    return status;
-	}
-      else
-	{
-	  mu_header_t hdr;
-	  mu_body_t body;
-
-	  /* FIXME: Kind of a kludge: make sure the message has header
-	     and body initialized. */
-	  status = mu_message_get_header (msg, &hdr);
-	  if (status)
-	    return status;
-	  status = mu_message_get_body (msg, &body);
-	  if (status)
-	    return status;
-	  
-	  status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
-	  if (status)
-	    return status;
-	  msg->flags |= MESSAGE_INTERNAL_STREAM;
-	}
-    }
-  
-  if (!ref)
-    {
-      *pstream = msg->stream;
-      return 0;
-    }
-  return mu_streamref_create (pstream, msg->stream);
-}
-
-int
-mu_message_get_stream (mu_message_t msg, mu_stream_t *pstream)
-{
-  /* FIXME: Deprecation warning */
-  return _message_get_stream (msg, pstream, 0);
-}
-
-int
-mu_message_get_streamref (mu_message_t msg, mu_stream_t *pstream)
-{
-  return _message_get_stream (msg, pstream, 1);
-}
-
-int
-mu_message_set_get_stream (mu_message_t msg,
-			   int (*_getstr) (mu_message_t, mu_stream_t *),
-			   void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_stream = _getstr;
-  return 0;
-}
-
-int
-mu_message_set_lines (mu_message_t msg, int (*_lines)
-		      (mu_message_t, size_t *, int), void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_lines = _lines;
-  return 0;
-}
-
-int
-mu_message_lines (mu_message_t msg, size_t *plines)
-{
-  size_t hlines, blines;
-  int ret = 0;
-
-  if (msg == NULL)
-    return EINVAL;
-  /* Overload.  */
-  if (msg->_lines)
-    return msg->_lines (msg, plines, 0);
-  if (plines)
-    {
-      mu_header_t hdr = NULL;
-      mu_body_t body = NULL;
-
-      hlines = blines = 0;
-      mu_message_get_header (msg, &hdr);
-      mu_message_get_body (msg, &body);
-      if ( ( ret = mu_header_lines (hdr, &hlines) ) == 0 )
-	      ret = mu_body_lines (body, &blines);
-      *plines = hlines + blines;
-    }
-  return ret;
-}
-
-/* Return the number of lines in the message, without going into
-   excess trouble for calculating it.  If obtaining the result
-   means downloading the entire message (as is the case for POP3,
-   for example), return MU_ERR_INFO_UNAVAILABLE. */
-int
-mu_message_quick_lines (mu_message_t msg, size_t *plines)
-{
-  size_t hlines, blines;
-  int rc;
-  
-  if (msg == NULL)
-    return EINVAL;
-  /* Overload.  */
-  if (msg->_lines)
-    {
-      int rc = msg->_lines (msg, plines, 1);
-      if (rc != ENOSYS)
-	return rc;
-    }
-  if (plines)
-    {
-      mu_header_t hdr = NULL;
-      mu_body_t body = NULL;
-
-      hlines = blines = 0;
-      mu_message_get_header (msg, &hdr);
-      mu_message_get_body (msg, &body);
-      if ((rc = mu_header_lines (hdr, &hlines)) == 0)
-	rc = mu_body_lines (body, &blines);
-      if (rc == 0)
-	*plines = hlines + blines;
-    }
-  return rc;
-}
-
-int
-mu_message_set_size (mu_message_t msg, int (*_size)
-		     (mu_message_t, size_t *), void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_size = _size;
-  return 0;
-}
-
-int
-mu_message_size (mu_message_t msg, size_t *psize)
-{
-  size_t hsize, bsize;
-  int ret = 0;
-
-  if (msg == NULL)
-    return EINVAL;
-  /* Overload ? */
-  if (msg->_size)
-    return msg->_size (msg, psize);
-  if (psize)
-    {
-      mu_header_t hdr = NULL;
-      mu_body_t body = NULL;
-      
-      hsize = bsize = 0;
-      mu_message_get_header (msg, &hdr);
-      mu_message_get_body (msg, &body);
-      if ( ( ret = mu_header_size (hdr, &hsize) ) == 0 )
-	ret = mu_body_size (body, &bsize);
-      *psize = hsize + bsize;
-    }
-  return ret;
-}
-
-int
-mu_message_get_envelope (mu_message_t msg, mu_envelope_t *penvelope)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (penvelope == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-
-  if (msg->envelope == NULL)
-    {
-      mu_envelope_t envelope;
-      int status = mu_envelope_create (&envelope, msg);
-      if (status != 0)
-	return status;
-      mu_envelope_set_sender (envelope, message_envelope_sender, msg);
-      mu_envelope_set_date (envelope, message_envelope_date, msg);
-      msg->envelope = envelope;
-    }
-  *penvelope = msg->envelope;
-  return 0;
-}
-
-int
-mu_message_set_envelope (mu_message_t msg, mu_envelope_t envelope, void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  if (msg->envelope)
-    mu_envelope_destroy (&msg->envelope, msg);
-  msg->envelope = envelope;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_get_attribute (mu_message_t msg, mu_attribute_t *pattribute)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (pattribute == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (msg->attribute == NULL)
-    {
-      mu_attribute_t attribute;
-      int status = mu_attribute_create (&attribute, msg);
-      if (status != 0)
-	return status;
-      msg->attribute = attribute;
-    }
-  *pattribute = msg->attribute;
-  return 0;
-}
-
-int
-mu_message_set_attribute (mu_message_t msg, mu_attribute_t attribute, void *owner)
-{
-  if (msg == NULL)
-   return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  if (msg->attribute)
-    mu_attribute_destroy (&msg->attribute, owner);
-  msg->attribute = attribute;
-  msg->flags |= MESSAGE_MODIFIED;
-  return 0;
-}
-
-int
-mu_message_get_uid (mu_message_t msg, size_t *puid)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->_get_uid)
-    return msg->_get_uid (msg, puid);
-  *puid = 0;
-  return 0;
-}
-
-int
-mu_message_get_uidl (mu_message_t msg, char *buffer, size_t buflen,
-		     size_t *pwriten)
-{
-  mu_header_t header = NULL;
-  size_t n = 0;
-  int status;
-
-  if (msg == NULL || buffer == NULL || buflen == 0)
-    return EINVAL;
-
-  buffer[0] = '\0';
-  /* Try the function overload if error fallback.  */
-  if (msg->_get_uidl)
-    {
-      status = msg->_get_uidl (msg, buffer, buflen, pwriten);
-      if (status == 0)
-	return status;
-    }
-
-  /* Be compatible with Qpopper ? qppoper saves the UIDL in "X-UIDL".
-     We generate a chksum and save it in the header.  */
-  mu_message_get_header (msg, &header);
-  status = mu_header_get_value_unfold (header, "X-UIDL", buffer, buflen, &n);
-  if (status != 0 || n == 0)
-    {
-      size_t uid = 0;
-      struct mu_md5_ctx md5context;
-      mu_stream_t stream = NULL;
-      char buf[1024];
-      unsigned char md5digest[16];
-      char *tmp;
-      n = 0;
-      mu_message_get_uid (msg, &uid);
-      mu_message_get_streamref (msg, &stream);
-      mu_md5_init_ctx (&md5context);
-      status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
-      if (status == 0)
-	{
-	  while (mu_stream_read (stream, buf, sizeof (buf), &n) == 0
-		 && n > 0)
-	    mu_md5_process_bytes (buf, n, &md5context);
-	  mu_md5_finish_ctx (&md5context, md5digest);
-	  tmp = buf;
-	  for (n = 0; n < 16; n++, tmp += 2)
-	    sprintf (tmp, "%02x", md5digest[n]);
-	  *tmp = '\0';
-	  /* POP3 rfc says that an UID should not be longer than 70.  */
-	  snprintf (buf + 32, 70, ".%lu.%lu", (unsigned long)time (NULL), 
-		    (unsigned long) uid);
-
-	  mu_header_set_value (header, "X-UIDL", buf, 1);
-	  buflen--; /* leave space for the NULL.  */
-	  strncpy (buffer, buf, buflen)[buflen] = '\0';
-	}
-      mu_stream_destroy (&stream);
-    }
-  return status;
-}
-
-int
-mu_message_get_qid (mu_message_t msg, mu_message_qid_t *pqid)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (!msg->_get_qid)
-    return ENOSYS;
-  return msg->_get_qid (msg, pqid);
-}
-    
-int
-mu_message_set_qid (mu_message_t msg,
-		    int (*_get_qid) (mu_message_t, mu_message_qid_t *),
-		    void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_qid = _get_qid;
-  return 0;
-}
-
-int
-mu_message_set_uid (mu_message_t msg, int (*_get_uid) (mu_message_t, size_t *),
-		    void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_uid = _get_uid;
-  return 0;
-}
-
-int
-mu_message_set_uidl (mu_message_t msg,
-		  int (* _get_uidl) (mu_message_t, char *, size_t, size_t *),
-		  void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_uidl = _get_uidl;
-  return 0;
-}
-
-int
-mu_message_set_is_multipart (mu_message_t msg,
-			  int (*_is_multipart) (mu_message_t, int *),
-			  void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_is_multipart = _is_multipart;
-  return 0;
-}
-
-int
-mu_message_is_multipart (mu_message_t msg, int *pmulti)
-{
-  if (msg && pmulti)
-    {
-      if (msg->_is_multipart)
-	return msg->_is_multipart (msg, pmulti);
-      if (msg->mime == NULL)
-	{
-	  int status = mu_mime_create (&msg->mime, msg, 0);
-	  if (status != 0)
-	    return 0;
-	  msg->flags |= MESSAGE_MIME_OWNER;
-	}
-      *pmulti = mu_mime_is_multipart(msg->mime);
-    }
-  return 0;
-}
-
-int
-mu_message_get_num_parts (mu_message_t msg, size_t *pparts)
-{
-  if (msg == NULL || pparts == NULL)
-    return EINVAL;
-
-  if (msg->_get_num_parts)
-    return msg->_get_num_parts (msg, pparts);
-
-  if (msg->mime == NULL)
-    {
-      int status = mu_mime_create (&msg->mime, msg, 0);
-      if (status != 0)
-	return status;
-    }
-  return mu_mime_get_num_parts (msg->mime, pparts);
-}
-
-int
-mu_message_set_get_num_parts (mu_message_t msg,
-			   int (*_get_num_parts) (mu_message_t, size_t *),
-			   void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_num_parts = _get_num_parts;
-  return 0;
-}
-
-int
-mu_message_get_part (mu_message_t msg, size_t part, mu_message_t *pmsg)
-{
-  if (msg == NULL || pmsg == NULL)
-    return EINVAL;
-
-  /* Overload.  */
-  if (msg->_get_part)
-    return msg->_get_part (msg, part, pmsg);
-
-  if (msg->mime == NULL)
-    {
-      int status = mu_mime_create (&msg->mime, msg, 0);
-      if (status != 0)
-	return status;
-    }
-  return mu_mime_get_part (msg->mime, part, pmsg);
-}
-
-int
-mu_message_set_get_part (mu_message_t msg, int (*_get_part)
-		      (mu_message_t, size_t, mu_message_t *),
-		      void *owner)
-{
-  if (msg == NULL)
-    return EINVAL;
-  if (msg->owner != owner)
-    return EACCES;
-  msg->_get_part = _get_part;
-  return 0;
-}
-
-int
-mu_message_get_observable (mu_message_t msg, mu_observable_t *pobservable)
-{
-  if (msg == NULL || pobservable == NULL)
-    return EINVAL;
-
-  if (msg->observable == NULL)
-    {
-      int status = mu_observable_create (&msg->observable, msg);
-      if (status != 0)
-	return status;
-    }
-  *pobservable = msg->observable;
-  return 0;
-}
-
-int
-mu_message_save_to_mailbox (mu_message_t msg, const char *toname, int perms)
-{
-  int rc = 0;
-  mu_mailbox_t to = 0;
-
-  if ((rc = mu_mailbox_create_default (&to, toname)))
-    {
-      mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
-		("mu_mailbox_create_default (%s) failed: %s\n", toname,
-		 mu_strerror (rc)));
-      goto end;
-    }
-
-  if ((rc = mu_mailbox_open (to,
-			     MU_STREAM_WRITE | MU_STREAM_CREAT
-			     | (perms & MU_STREAM_IMASK))))
-    {
-      mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
-		("mu_mailbox_open (%s) failed: %s", toname,
-		 mu_strerror (rc)));
-      goto end;
-    }
-
-  if ((rc = mu_mailbox_append_message (to, msg)))
-    {
-      mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
-		("mu_mailbox_append_message (%s) failed: %s", toname,
-		 mu_strerror (rc)));
-      goto end;
-    }
-
-end:
-
-  if (!rc)
-    {
-      if ((rc = mu_mailbox_close (to)))
-        mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
-		  ("mu_mailbox_close (%s) failed: %s", toname,
-		   mu_strerror (rc)));
-    }
-  else
-    mu_mailbox_close (to);
-
-  mu_mailbox_destroy (&to);
-
-  return rc;
-}
-
diff --git a/libmailutils/mailbox/msgattr.c b/libmailutils/mailbox/msgattr.c
new file mode 100644
index 0000000..6703c5a
--- /dev/null
+++ b/libmailutils/mailbox/msgattr.c
@@ -0,0 +1,59 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/attribute.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_get_attribute (mu_message_t msg, mu_attribute_t *pattribute)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (pattribute == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (msg->attribute == NULL)
+    {
+      mu_attribute_t attribute;
+      int status = mu_attribute_create (&attribute, msg);
+      if (status != 0)
+	return status;
+      msg->attribute = attribute;
+    }
+  *pattribute = msg->attribute;
+  return 0;
+}
+
+int
+mu_message_set_attribute (mu_message_t msg, mu_attribute_t attribute, void *owner)
+{
+  if (msg == NULL)
+   return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  if (msg->attribute)
+    mu_attribute_destroy (&msg->attribute, owner);
+  msg->attribute = attribute;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
diff --git a/libmailutils/mailbox/msgbody.c b/libmailutils/mailbox/msgbody.c
new file mode 100644
index 0000000..beb5a27
--- /dev/null
+++ b/libmailutils/mailbox/msgbody.c
@@ -0,0 +1,85 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_get_body (mu_message_t msg, mu_body_t *pbody)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (pbody == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  /* Is it a floating mesg.  */
+  if (msg->body == NULL)
+    {
+      mu_body_t body;
+      int status = mu_body_create (&body, msg);
+      if (status != 0)
+	return status;
+      /* If a stream is already set, use it to create the body stream.  */
+      /* FIXME: I'm not sure if the second condition is really needed */
+      if (msg->stream/* && (msg->flags & MESSAGE_INTERNAL_STREAM)*/)
+	{
+	  mu_stream_t stream;
+	  int flags = 0;
+
+	  /* FIXME: The actual mu_header_size cannot be used as offset,
+	     because the headers might have been modified in between. */
+	  
+	  mu_stream_get_flags (msg->stream, &flags);
+	  status = mu_streamref_create_abridged (&stream, msg->stream,
+						 msg->orig_header_size, 0);
+	  if (status)
+	    {
+	      mu_body_destroy (&body, msg);
+	      return status;
+	    }
+	  mu_body_set_stream (body, stream, msg);
+	}
+      msg->body = body;
+    }
+  *pbody = msg->body;
+  return 0;
+}
+
+int
+mu_message_set_body (mu_message_t msg, mu_body_t body, void *owner)
+{
+  if (msg == NULL )
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  /* Make sure we destroy the old if it was owned by the mesg.  */
+  /* FIXME:  I do not know if somebody has already a ref on this ? */
+  if (msg->body)
+    mu_body_destroy (&msg->body, msg);
+  msg->body = body;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
diff --git a/libmailutils/mailbox/msgcpy.c b/libmailutils/mailbox/msgcpy.c
new file mode 100644
index 0000000..29f1f28
--- /dev/null
+++ b/libmailutils/mailbox/msgcpy.c
@@ -0,0 +1,86 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_create_copy (mu_message_t *to, mu_message_t from)
+{
+  int status = 0;
+  mu_stream_t fromstr = NULL;
+  mu_stream_t tmp = NULL;
+
+  if (!to)
+    return MU_ERR_OUT_PTR_NULL;
+  if (!from)
+    return EINVAL;
+
+  status = mu_memory_stream_create (&tmp, MU_STREAM_RDWR|MU_STREAM_SEEK);
+  if (status)
+    return status;
+
+  status = mu_message_get_streamref (from, &fromstr);
+  if (status)
+    {
+      mu_stream_destroy (&tmp);
+      return status;
+    }
+
+  status = mu_stream_copy (tmp, fromstr, 0, NULL);
+  if (status == 0)
+    {
+      status = mu_message_create (to, NULL);
+      if (status == 0)
+	mu_message_set_stream (*to, tmp, NULL);
+    }
+
+  if (status)
+    mu_stream_destroy (&tmp);
+  mu_stream_destroy (&fromstr);
+
+  return status;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/libmailutils/list/create.c b/libmailutils/mailbox/msgcreat.c
similarity index 54%
copy from libmailutils/list/create.c
copy to libmailutils/mailbox/msgcreat.c
index c5d59e1..244bf1f 100644
--- a/libmailutils/list/create.c
+++ b/libmailutils/mailbox/msgcreat.c
@@ -1,6 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -13,37 +13,43 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see 
+   Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
+#include <config.h>
 #include <stdlib.h>
 
-#include <mailutils/sys/list.h>
+#include <mailutils/types.h>
+#include <mailutils/message.h>
 #include <mailutils/errno.h>
+#include <mailutils/sys/message.h>
 
+/*  Allocate ressources for the mu_message_t.  */
 int
-mu_list_create (mu_list_t *plist)
+mu_message_create (mu_message_t *pmsg, void *owner)
 {
-  mu_list_t list;
+  mu_message_t msg;
   int status;
 
-  if (plist == NULL)
+  if (pmsg == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  list = calloc (sizeof (*list), 1);
-  if (list == NULL)
+  msg = calloc (1, sizeof (*msg));
+  if (msg == NULL)
     return ENOMEM;
-  status = mu_monitor_create (&list->monitor, 0,  list);
+  status = mu_monitor_create (&msg->monitor, 0, msg);
   if (status != 0)
     {
-      free (list);
+      free (msg);
       return status;
     }
-  list->head.next = &list->head;
-  list->head.prev = &list->head;
-  *plist = list;
+  msg->owner = owner;
+  msg->ref_count = 1;
+  *pmsg = msg;
   return 0;
 }
+
+void *
+mu_message_get_owner (mu_message_t msg)
+{
+  return (msg == NULL) ? NULL : msg->owner;
+}
diff --git a/libmailutils/mailbox/msgenv.c b/libmailutils/mailbox/msgenv.c
new file mode 100644
index 0000000..a478891
--- /dev/null
+++ b/libmailutils/mailbox/msgenv.c
@@ -0,0 +1,161 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/envelope.h>
+#include <mailutils/util.h>
+#include <mailutils/header.h>
+#include <mailutils/mu_auth.h>
+#include <mailutils/address.h>
+#include <mailutils/sys/message.h>
+
+/* Message envelope */
+static int
+message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
+		       size_t *pnwrite)
+{
+  mu_message_t msg = mu_envelope_get_owner (envelope);
+  time_t t;
+  size_t n;
+
+  if (msg == NULL)
+    return EINVAL;
+
+  /* FIXME: extract the time from "Date:".  */
+
+  if (buf == NULL || len == 0)
+    {
+      n = MU_ENVELOPE_DATE_LENGTH;
+    }
+  else
+    {
+      char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
+      t = time (NULL);
+      n = mu_strftime (tmpbuf, sizeof tmpbuf, 
+                       MU_ENVELOPE_DATE_FORMAT, localtime (&t));
+      n = mu_cpystr (buf, tmpbuf, len);
+    }
+  if (pnwrite)
+    *pnwrite = n;
+  return 0;
+}
+
+static int
+message_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
+			 size_t *pnwrite)
+{
+  mu_message_t msg = mu_envelope_get_owner (envelope);
+  mu_header_t header;
+  int status;
+  const char *sender;
+  struct mu_auth_data *auth = NULL;
+  static char *hdrnames[] = {
+    "X-Envelope-Sender",
+    "X-Envelope-From",
+    "X-Original-Sender",
+    "From",
+    NULL
+  };
+  mu_address_t address = NULL;
+
+  if (msg == NULL)
+    return EINVAL;
+
+  /* First, try the header  */
+  status = mu_message_get_header (msg, &header);
+  if (status)
+    return status;
+  status = mu_header_sget_firstof (header, hdrnames, &sender, NULL);
+  if (status)
+    {
+      auth = mu_get_auth_by_uid (getuid ());
+      if (!auth)
+	return MU_ERR_NOENT;
+      sender = auth->name;
+    }
+
+  status = mu_address_create (&address, sender);
+  if (status == 0)
+    {
+      status = mu_address_sget_email (address, 1, &sender);
+      if (status == 0)
+	{
+	  size_t n = strlen (sender);
+	  if (buf && len > 0)
+	    {
+	      len--; /* One for the null.  */
+	      n = (n < len) ? n : len;
+	      memcpy (buf, sender, n);
+	      buf[n] = '\0';
+	    }
+	  if (pnwrite)
+	    *pnwrite = n;
+	}
+      mu_address_destroy (&address);
+    }
+  
+  if (auth)
+    mu_auth_data_free (auth);
+
+  return status;
+}
+
+int
+mu_message_get_envelope (mu_message_t msg, mu_envelope_t *penvelope)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (penvelope == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg->envelope == NULL)
+    {
+      mu_envelope_t envelope;
+      int status = mu_envelope_create (&envelope, msg);
+      if (status != 0)
+	return status;
+      mu_envelope_set_sender (envelope, message_envelope_sender, msg);
+      mu_envelope_set_date (envelope, message_envelope_date, msg);
+      msg->envelope = envelope;
+    }
+  *penvelope = msg->envelope;
+  return 0;
+}
+
+int
+mu_message_set_envelope (mu_message_t msg, mu_envelope_t envelope, void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  if (msg->envelope)
+    mu_envelope_destroy (&msg->envelope, msg);
+  msg->envelope = envelope;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
diff --git a/libmailutils/mailbox/msgheader.c b/libmailutils/mailbox/msgheader.c
new file mode 100644
index 0000000..09052d8
--- /dev/null
+++ b/libmailutils/mailbox/msgheader.c
@@ -0,0 +1,163 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/message.h>
+
+
+enum eoh_state
+  {
+    eoh_no,
+    eoh_maybe,
+    eoh_yes
+  };
+
+/* Message header stuff */
+static enum eoh_state
+string_find_eoh (enum eoh_state eoh, const char *str, size_t len,
+		 size_t *ppos)
+{
+  size_t pos;
+
+  if (eoh == eoh_maybe && *str == '\n')
+    {
+      *ppos = 0;
+      return eoh_yes;
+    }
+  
+  for (pos = 0; pos < len - 1; pos++)
+    if (str[pos] == '\n' && str[pos + 1] == '\n')
+      {
+	*ppos = pos + 1;
+	return eoh_yes;
+      }
+  
+  *ppos = pos + 1;
+  return str[pos] == '\n' ? eoh_maybe : eoh_no;
+}
+
+#define MIN_HEADER_BUF_SIZE 2048
+
+static int
+_header_fill (mu_stream_t stream, char **pbuf, size_t *plen)
+{
+  int status = 0;
+  char *buffer = NULL;
+  size_t bufsize = 0;
+  char inbuf[MIN_HEADER_BUF_SIZE];
+  size_t nread;
+  enum eoh_state eoh = eoh_no;
+  
+  status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+  if (status)
+    return status;
+      
+  while (eoh != eoh_yes
+	 && (status = mu_stream_read (stream, inbuf, sizeof (inbuf), &nread))
+	    == 0
+	 && nread)
+    {
+      char *nbuf;
+      size_t len;
+
+      eoh = string_find_eoh (eoh, inbuf, nread, &len);
+      
+      nbuf = realloc (buffer, bufsize + len);
+      if (!nbuf)
+	{
+	  status = ENOMEM;
+	  break;
+	}
+      memcpy (nbuf + bufsize, inbuf, len);
+      buffer = nbuf;
+      bufsize += len;
+    }
+
+  if (status)
+    free (buffer);
+  else
+    {
+      *pbuf = buffer;
+      *plen = bufsize;
+    }
+  return status;
+}
+
+static int
+message_header_fill (void *data, char **pbuf, size_t *plen)
+{
+  int status = 0;
+  mu_message_t msg = data;
+  mu_stream_t stream;
+
+  status = mu_message_get_streamref (msg, &stream);
+  if (status == 0)
+    {
+      status = _header_fill (stream, pbuf, plen);
+      mu_stream_destroy (&stream);
+    }
+  return status;
+}
+
+int
+mu_message_get_header (mu_message_t msg, mu_header_t *phdr)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (phdr == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg->header == NULL)
+    {
+      mu_header_t header;
+      int status = mu_header_create (&header, NULL, 0);
+      if (status != 0)
+	return status;
+      if (msg->stream)
+	mu_header_set_fill (header, message_header_fill, msg);
+      status = mu_header_size (header, &msg->orig_header_size);
+      if (status)
+	return status;
+      msg->header = header;
+    }
+  *phdr = msg->header;
+  return 0;
+}
+
+/* Note: mu_message_set_header steals the reference to hdr */
+int
+mu_message_set_header (mu_message_t msg, mu_header_t hdr, void *owner)
+{
+  if (msg == NULL )
+    return EINVAL;
+  if (msg->owner != owner)
+     return EACCES;
+  if (msg->header)
+    mu_header_destroy (&msg->header);
+  msg->header = hdr;
+  msg->flags |= MESSAGE_MODIFIED;
+  return 0;
+}
diff --git a/libmailutils/mailbox/msglines.c b/libmailutils/mailbox/msglines.c
new file mode 100644
index 0000000..194cee8
--- /dev/null
+++ b/libmailutils/mailbox/msglines.c
@@ -0,0 +1,65 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_lines (mu_message_t msg, size_t *plines)
+{
+  size_t hlines, blines;
+  int ret = 0;
+
+  if (msg == NULL)
+    return EINVAL;
+  /* Overload.  */
+  if (msg->_lines)
+    return msg->_lines (msg, plines, 0);
+  if (plines)
+    {
+      mu_header_t hdr = NULL;
+      mu_body_t body = NULL;
+
+      hlines = blines = 0;
+      mu_message_get_header (msg, &hdr);
+      mu_message_get_body (msg, &body);
+      if ( ( ret = mu_header_lines (hdr, &hlines) ) == 0 )
+	      ret = mu_body_lines (body, &blines);
+      *plines = hlines + blines;
+    }
+  return ret;
+}
+
+int
+mu_message_set_lines (mu_message_t msg, int (*_lines)
+		      (mu_message_t, size_t *, int), void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_lines = _lines;
+  return 0;
+}
diff --git a/libmu_dbm/name.c b/libmailutils/mailbox/msgmbx.c
similarity index 61%
copy from libmu_dbm/name.c
copy to libmailutils/mailbox/msgmbx.c
index 94aabae..ed421f0 100644
--- a/libmu_dbm/name.c
+++ b/libmailutils/mailbox/msgmbx.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,26 +16,32 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <unistd.h>
+#include <config.h>
 #include <stdlib.h>
+
 #include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
+#include <mailutils/message.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/sys/message.h>
 
 int
-mu_dbm_get_name (mu_dbm_file_t db, const char **pname)
+mu_message_get_mailbox (mu_message_t msg, mu_mailbox_t *pmailbox)
 {
-  if (!db)
+  if (msg == NULL)
     return EINVAL;
-  if (!pname)
+  if (pmailbox == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  *pname = db->db_name;
+  *pmailbox = msg->mailbox;
+  return 0;
+}
+
+int
+mu_message_set_mailbox (mu_message_t msg, mu_mailbox_t mailbox, void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->mailbox = mailbox;
   return 0;
 }
diff --git a/libmailutils/mailbox/msgmod.c b/libmailutils/mailbox/msgmod.c
new file mode 100644
index 0000000..5582a42
--- /dev/null
+++ b/libmailutils/mailbox/msgmod.c
@@ -0,0 +1,62 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/attribute.h>
+#include <mailutils/body.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_is_modified (mu_message_t msg)
+{
+  int mod = 0;
+  if (msg)
+    {
+      if (mu_header_is_modified (msg->header))
+	mod |= MU_MSG_HEADER_MODIFIED;
+      if (mu_attribute_is_modified (msg->attribute))
+	mod |= MU_MSG_ATTRIBUTE_MODIFIED;
+      if (mu_body_is_modified (msg->body))
+	mod |= MU_MSG_BODY_MODIFIED;
+      if (msg->flags & MESSAGE_MODIFIED)
+	mod |= MU_MSG_BODY_MODIFIED | MU_MSG_HEADER_MODIFIED;
+    }
+  return mod;
+}
+
+int
+mu_message_clear_modified (mu_message_t msg)
+{
+  if (msg)
+    {
+      if (msg->header)
+	mu_header_clear_modified (msg->header);
+      if (msg->attribute)
+	mu_attribute_clear_modified (msg->attribute);
+      if (msg->body)
+	mu_body_clear_modified (msg->body);
+      msg->flags &= ~MESSAGE_MODIFIED;
+    }
+  return 0;
+}
diff --git a/libproto/imap/create.c b/libmailutils/mailbox/msgmulti.c
similarity index 50%
copy from libproto/imap/create.c
copy to libmailutils/mailbox/msgmulti.c
index 9ae92eb..9dffb94 100644
--- a/libproto/imap/create.c
+++ b/libmailutils/mailbox/msgmulti.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,52 +16,43 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
+#include <config.h>
 #include <stdlib.h>
-#include <errno.h>
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/mime.h>
 #include <mailutils/errno.h>
-#include <mailutils/sys/imap.h>
-#include <mailutils/list.h>
+#include <mailutils/sys/message.h>
 
 int
-mu_imap_create (mu_imap_t *pimap)
+mu_message_is_multipart (mu_message_t msg, int *pmulti)
 {
-  mu_imap_t imap;
-
-  /* Sanity check.  */
-  if (pimap == NULL)
-    return EINVAL;
-
-  imap = calloc (1, sizeof *imap);
-  if (imap == NULL)
-    return ENOMEM;
-
-  _mu_imap_init (imap);
-
-  *pimap = imap;
+  if (msg && pmulti)
+    {
+      if (msg->_is_multipart)
+	return msg->_is_multipart (msg, pmulti);
+      if (msg->mime == NULL)
+	{
+	  int status = mu_mime_create (&msg->mime, msg, 0);
+	  if (status != 0)
+	    return 0;
+	  msg->flags |= MESSAGE_MIME_OWNER;
+	}
+      *pmulti = mu_mime_is_multipart(msg->mime);
+    }
   return 0;
 }
 
 int
-_mu_imap_init (mu_imap_t imap)
+mu_message_set_is_multipart (mu_message_t msg,
+			     int (*_is_multipart) (mu_message_t, int *),
+			     void *owner)
 {
-  if (imap == NULL)
+  if (msg == NULL)
     return EINVAL;
-  if (!imap->io)
-    {
-      int rc;
-      
-      mu_list_destroy (&imap->capa);
-      _mu_imap_clrerrstr (imap);
-      rc = _mu_imap_tag_clr (imap);
-      imap->flags = 0;
-      if (rc)
-	return rc;
-    }
-  imap->client_state = MU_IMAP_CLIENT_READY; 
-  imap->session_state = MU_IMAP_SESSION_INIT;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_is_multipart = _is_multipart;
   return 0;
 }
+
diff --git a/libproto/imap/create.c b/libmailutils/mailbox/msgnumparts.c
similarity index 51%
copy from libproto/imap/create.c
copy to libmailutils/mailbox/msgnumparts.c
index 9ae92eb..fb51872 100644
--- a/libproto/imap/create.c
+++ b/libmailutils/mailbox/msgnumparts.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,52 +16,41 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
+#include <config.h>
 #include <stdlib.h>
-#include <errno.h>
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/mime.h>
 #include <mailutils/errno.h>
-#include <mailutils/sys/imap.h>
-#include <mailutils/list.h>
+#include <mailutils/sys/message.h>
 
 int
-mu_imap_create (mu_imap_t *pimap)
+mu_message_get_num_parts (mu_message_t msg, size_t *pparts)
 {
-  mu_imap_t imap;
-
-  /* Sanity check.  */
-  if (pimap == NULL)
+  if (msg == NULL || pparts == NULL)
     return EINVAL;
 
-  imap = calloc (1, sizeof *imap);
-  if (imap == NULL)
-    return ENOMEM;
+  if (msg->_get_num_parts)
+    return msg->_get_num_parts (msg, pparts);
 
-  _mu_imap_init (imap);
-
-  *pimap = imap;
-  return 0;
+  if (msg->mime == NULL)
+    {
+      int status = mu_mime_create (&msg->mime, msg, 0);
+      if (status != 0)
+	return status;
+    }
+  return mu_mime_get_num_parts (msg->mime, pparts);
 }
 
 int
-_mu_imap_init (mu_imap_t imap)
+mu_message_set_get_num_parts (mu_message_t msg,
+			   int (*_get_num_parts) (mu_message_t, size_t *),
+			   void *owner)
 {
-  if (imap == NULL)
+  if (msg == NULL)
     return EINVAL;
-  if (!imap->io)
-    {
-      int rc;
-      
-      mu_list_destroy (&imap->capa);
-      _mu_imap_clrerrstr (imap);
-      rc = _mu_imap_tag_clr (imap);
-      imap->flags = 0;
-      if (rc)
-	return rc;
-    }
-  imap->client_state = MU_IMAP_CLIENT_READY; 
-  imap->session_state = MU_IMAP_SESSION_INIT;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_num_parts = _get_num_parts;
   return 0;
 }
diff --git a/libmu_dbm/name.c b/libmailutils/mailbox/msgobs.c
similarity index 61%
copy from libmu_dbm/name.c
copy to libmailutils/mailbox/msgobs.c
index 94aabae..aa8065e 100644
--- a/libmu_dbm/name.c
+++ b/libmailutils/mailbox/msgobs.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,26 +16,26 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <unistd.h>
+#include <config.h>
 #include <stdlib.h>
 #include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
+#include <mailutils/message.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/observer.h>
+#include <mailutils/sys/message.h>
 
 int
-mu_dbm_get_name (mu_dbm_file_t db, const char **pname)
+mu_message_get_observable (mu_message_t msg, mu_observable_t *pobservable)
 {
-  if (!db)
+  if (msg == NULL || pobservable == NULL)
     return EINVAL;
-  if (!pname)
-    return MU_ERR_OUT_PTR_NULL;
-  *pname = db->db_name;
+
+  if (msg->observable == NULL)
+    {
+      int status = mu_observable_create (&msg->observable, msg);
+      if (status != 0)
+	return status;
+    }
+  *pobservable = msg->observable;
   return 0;
 }
diff --git a/libproto/imap/create.c b/libmailutils/mailbox/msgpart.c
similarity index 51%
copy from libproto/imap/create.c
copy to libmailutils/mailbox/msgpart.c
index 9ae92eb..4be2478 100644
--- a/libproto/imap/create.c
+++ b/libmailutils/mailbox/msgpart.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,52 +16,41 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
+#include <config.h>
 #include <stdlib.h>
-#include <errno.h>
+#include <mailutils/types.h>
+#include <mailutils/message.h>
 #include <mailutils/errno.h>
-#include <mailutils/sys/imap.h>
-#include <mailutils/list.h>
+#include <mailutils/sys/message.h>
 
 int
-mu_imap_create (mu_imap_t *pimap)
+mu_message_get_part (mu_message_t msg, size_t part, mu_message_t *pmsg)
 {
-  mu_imap_t imap;
-
-  /* Sanity check.  */
-  if (pimap == NULL)
+  if (msg == NULL || pmsg == NULL)
     return EINVAL;
 
-  imap = calloc (1, sizeof *imap);
-  if (imap == NULL)
-    return ENOMEM;
+  /* Overload.  */
+  if (msg->_get_part)
+    return msg->_get_part (msg, part, pmsg);
 
-  _mu_imap_init (imap);
-
-  *pimap = imap;
-  return 0;
+  if (msg->mime == NULL)
+    {
+      int status = mu_mime_create (&msg->mime, msg, 0);
+      if (status != 0)
+	return status;
+    }
+  return mu_mime_get_part (msg->mime, part, pmsg);
 }
 
 int
-_mu_imap_init (mu_imap_t imap)
+mu_message_set_get_part (mu_message_t msg, int (*_get_part)
+		      (mu_message_t, size_t, mu_message_t *),
+		      void *owner)
 {
-  if (imap == NULL)
+  if (msg == NULL)
     return EINVAL;
-  if (!imap->io)
-    {
-      int rc;
-      
-      mu_list_destroy (&imap->capa);
-      _mu_imap_clrerrstr (imap);
-      rc = _mu_imap_tag_clr (imap);
-      imap->flags = 0;
-      if (rc)
-	return rc;
-    }
-  imap->client_state = MU_IMAP_CLIENT_READY; 
-  imap->session_state = MU_IMAP_SESSION_INIT;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_part = _get_part;
   return 0;
 }
diff --git a/libmu_dbm/name.c b/libmailutils/mailbox/msgqid.c
similarity index 58%
copy from libmu_dbm/name.c
copy to libmailutils/mailbox/msgqid.c
index 94aabae..04e3ab9 100644
--- a/libmu_dbm/name.c
+++ b/libmailutils/mailbox/msgqid.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,26 +16,33 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <unistd.h>
+#include <config.h>
 #include <stdlib.h>
+
 #include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
+#include <mailutils/message.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/sys/message.h>
 
 int
-mu_dbm_get_name (mu_dbm_file_t db, const char **pname)
+mu_message_get_qid (mu_message_t msg, mu_message_qid_t *pqid)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (!msg->_get_qid)
+    return ENOSYS;
+  return msg->_get_qid (msg, pqid);
+}
+    
+int
+mu_message_set_qid (mu_message_t msg,
+		    int (*_get_qid) (mu_message_t, mu_message_qid_t *),
+		    void *owner)
 {
-  if (!db)
+  if (msg == NULL)
     return EINVAL;
-  if (!pname)
-    return MU_ERR_OUT_PTR_NULL;
-  *pname = db->db_name;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_qid = _get_qid;
   return 0;
 }
diff --git a/libmailutils/mailbox/msgqlines.c b/libmailutils/mailbox/msgqlines.c
new file mode 100644
index 0000000..a52249a
--- /dev/null
+++ b/libmailutils/mailbox/msgqlines.c
@@ -0,0 +1,62 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/sys/message.h>
+
+/* Return the number of lines in the message, without going into
+   excess trouble for calculating it.  If obtaining the result
+   means downloading the entire message (as is the case for POP3,
+   for example), return MU_ERR_INFO_UNAVAILABLE. */
+int
+mu_message_quick_lines (mu_message_t msg, size_t *plines)
+{
+  size_t hlines, blines;
+  int rc;
+  
+  if (msg == NULL)
+    return EINVAL;
+  /* Overload.  */
+  if (msg->_lines)
+    {
+      int rc = msg->_lines (msg, plines, 1);
+      if (rc != ENOSYS)
+	return rc;
+    }
+  if (plines)
+    {
+      mu_header_t hdr = NULL;
+      mu_body_t body = NULL;
+
+      hlines = blines = 0;
+      mu_message_get_header (msg, &hdr);
+      mu_message_get_body (msg, &body);
+      if ((rc = mu_header_lines (hdr, &hlines)) == 0)
+	rc = mu_body_lines (body, &blines);
+      if (rc == 0)
+	*plines = hlines + blines;
+    }
+  return rc;
+}
diff --git a/libmailutils/mailbox/msgref.c b/libmailutils/mailbox/msgref.c
new file mode 100644
index 0000000..1419dbb
--- /dev/null
+++ b/libmailutils/mailbox/msgref.c
@@ -0,0 +1,136 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/monitor.h>
+#include <mailutils/observer.h>
+#include <mailutils/envelope.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/attribute.h>
+#include <mailutils/stream.h>
+#include <mailutils/sys/message.h>
+
+void
+mu_message_ref (mu_message_t msg)
+{
+  if (msg)
+    {
+      mu_monitor_wrlock (msg->monitor);
+      msg->ref_count++;
+      mu_monitor_unlock (msg->monitor);
+    }
+}
+
+/* Free the message and all associated stuff */
+static void
+_mu_message_free (mu_message_t msg)
+{
+  /* Notify the listeners.  */
+  /* FIXME: to be removed since we do not support this event.  */
+  if (msg->observable)
+    {
+      mu_observable_notify (msg->observable, MU_EVT_MESSAGE_DESTROY, msg);
+      mu_observable_destroy (&msg->observable, msg);
+    }
+      
+  /* Envelope.  */
+  if (msg->envelope)
+    mu_envelope_destroy (&msg->envelope, msg);
+      
+  /* Header.  */
+  if (msg->header)
+    mu_header_destroy (&msg->header);
+      
+  /* Body.  */
+  if (msg->body)
+    mu_body_destroy (&msg->body, msg);
+      
+  /* Attribute.  */
+  if (msg->attribute)
+    mu_attribute_destroy (&msg->attribute, msg);
+      
+  /* Stream.  */
+  if (msg->stream)
+    mu_stream_destroy (&msg->stream);
+      
+  /*  Mime.  */
+  if (msg->flags & MESSAGE_MIME_OWNER)
+    mu_mime_destroy (&msg->mime);
+      
+  /* Loose the owner.  */
+  msg->owner = NULL;
+      
+  free (msg);
+}
+
+void
+mu_message_unref (mu_message_t msg)
+{
+  if (msg)
+    {
+      mu_monitor_t monitor = msg->monitor;
+      mu_monitor_wrlock (monitor);
+      /* Note: msg->ref may be incremented by mu_message_ref without
+	 additional checking for its owner, therefore decrementing
+	 it must also occur independently of the owner checking. Due
+	 to this inconsistency ref may reach negative values, which
+	 is very unfortunate.
+	 
+	 The `owner' stuff is a leftover from older mailutils versions.
+	 We are heading to removing it altogether. */
+      if (msg->ref_count > 0)
+	msg->ref_count--;
+      if (msg->ref_count == 0)
+	{
+	  _mu_message_free (msg);
+	  mu_monitor_unlock (monitor);
+	  mu_monitor_destroy (&monitor, msg);
+	}
+      else
+	mu_monitor_unlock (monitor);
+    }
+}
+
+void
+mu_message_destroy (mu_message_t *pmsg, void *owner)
+{
+  if (pmsg && *pmsg)
+    {
+      mu_message_t msg = *pmsg;
+      
+      mu_monitor_t monitor = msg->monitor;
+      mu_monitor_wrlock (monitor);
+
+      if (msg->owner && msg->owner == owner)
+	{
+	  _mu_message_free (msg);
+	  mu_monitor_unlock (monitor);
+	  mu_monitor_destroy (&monitor, msg);
+	  *pmsg = NULL;
+	  return;
+	}
+      mu_monitor_unlock (monitor);
+    }
+}
+
diff --git a/libmailutils/mailbox/msgsave.c b/libmailutils/mailbox/msgsave.c
new file mode 100644
index 0000000..89f0e8e
--- /dev/null
+++ b/libmailutils/mailbox/msgsave.c
@@ -0,0 +1,76 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <mailutils/types.h>
+#include <mailutils/diag.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/message.h>
+#include <mailutils/stream.h>
+
+int
+mu_message_save_to_mailbox (mu_message_t msg, const char *toname, int perms)
+{
+  int rc = 0;
+  mu_mailbox_t to = 0;
+
+  if ((rc = mu_mailbox_create_default (&to, toname)))
+    {
+      mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
+		("mu_mailbox_create_default (%s) failed: %s\n", toname,
+		 mu_strerror (rc)));
+      goto end;
+    }
+
+  if ((rc = mu_mailbox_open (to,
+			     MU_STREAM_WRITE | MU_STREAM_CREAT
+			     | (perms & MU_STREAM_IMASK))))
+    {
+      mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
+		("mu_mailbox_open (%s) failed: %s", toname,
+		 mu_strerror (rc)));
+      goto end;
+    }
+
+  if ((rc = mu_mailbox_append_message (to, msg)))
+    {
+      mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
+		("mu_mailbox_append_message (%s) failed: %s", toname,
+		 mu_strerror (rc)));
+      goto end;
+    }
+
+end:
+
+  if (!rc)
+    {
+      if ((rc = mu_mailbox_close (to)))
+        mu_debug (MU_DEBCAT_MESSAGE, MU_DEBUG_ERROR, 		 
+		  ("mu_mailbox_close (%s) failed: %s", toname,
+		   mu_strerror (rc)));
+    }
+  else
+    mu_mailbox_close (to);
+
+  mu_mailbox_destroy (&to);
+
+  return rc;
+}
diff --git a/libmailutils/mailbox/msgsize.c b/libmailutils/mailbox/msgsize.c
new file mode 100644
index 0000000..874a45d
--- /dev/null
+++ b/libmailutils/mailbox/msgsize.c
@@ -0,0 +1,66 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_size (mu_message_t msg, size_t *psize)
+{
+  size_t hsize, bsize;
+  int ret = 0;
+
+  if (msg == NULL)
+    return EINVAL;
+  /* Overload ? */
+  if (msg->_size)
+    return msg->_size (msg, psize);
+  if (psize)
+    {
+      mu_header_t hdr = NULL;
+      mu_body_t body = NULL;
+      
+      hsize = bsize = 0;
+      mu_message_get_header (msg, &hdr);
+      mu_message_get_body (msg, &body);
+      if ( ( ret = mu_header_size (hdr, &hsize) ) == 0 )
+	ret = mu_body_size (body, &bsize);
+      *psize = hsize + bsize;
+    }
+  return ret;
+}
+
+int
+mu_message_set_size (mu_message_t msg, int (*_size)
+		     (mu_message_t, size_t *), void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_size = _size;
+  return 0;
+}
+
diff --git a/libmailutils/mailbox/msgstream.c b/libmailutils/mailbox/msgstream.c
new file mode 100644
index 0000000..a65135c
--- /dev/null
+++ b/libmailutils/mailbox/msgstream.c
@@ -0,0 +1,365 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/sys/message.h>
+#include <mailutils/sys/stream.h>
+
+/* Message stream */
+
+enum _message_stream_state
+  {
+    _mss_init,
+    _mss_header,
+    _mss_body,
+    _mss_eof
+  };
+
+struct _mu_message_stream
+{
+  struct _mu_stream stream;
+  mu_message_t msg;
+  enum _message_stream_state state;
+  mu_stream_t transport;
+  mu_off_t limit;
+};
+
+static int
+_check_stream_state (struct _mu_message_stream *str)
+{
+  int rc = 0;
+  
+  if (str->transport && mu_stream_eof (str->transport))
+    mu_stream_destroy (&str->transport);
+  
+  switch (str->state)
+    {
+    case _mss_init:
+      if (!str->transport)
+	{
+	  rc = mu_header_get_streamref (str->msg->header, &str->transport);
+	  if (rc == 0)
+	    {
+	      str->state = _mss_header;
+	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
+	    }
+	}
+      break;
+      
+    case _mss_header:
+      if (!str->transport)
+	{
+	  rc = mu_body_get_streamref (str->msg->body, &str->transport);
+	  if (rc == 0)
+	    {
+	      str->state = _mss_body;
+	      rc = mu_stream_seek (str->transport, 0, MU_SEEK_SET, NULL);
+	    }
+	}
+      break;
+      
+    case _mss_body:
+      if (!str->transport)
+	str->state = _mss_eof;
+    case _mss_eof:
+      break;
+    }
+  return rc;
+}
+
+static void
+_message_stream_done (struct _mu_stream *str)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  mu_stream_destroy (&sp->transport);
+}
+
+static int
+_message_stream_flush (struct _mu_stream *str)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  int rc = _check_stream_state (sp);
+  if (rc)
+    return rc;
+  return mu_stream_flush (sp->transport);
+}
+  
+static int
+_message_stream_size (struct _mu_stream *str, mu_off_t *psize)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t hsize, bsize;
+  mu_header_size (sp->msg->header, &hsize);
+  mu_body_size (sp->msg->body, &bsize);
+  if (psize)
+    *psize = hsize + bsize;
+  return 0;
+}
+
+static int
+_message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t hsize, size;
+  int rc;
+  
+  rc = _check_stream_state (sp);
+  if (rc)
+    return rc;
+  mu_header_size (sp->msg->header, &hsize);
+  mu_body_size (sp->msg->body, &size);
+  
+  if (off < 0 || off >= size + hsize)
+    return ESPIPE;
+
+  switch (sp->state)
+    {
+    case _mss_eof:
+      sp->state = _mss_init;
+      rc = _check_stream_state (sp);
+      if (rc)
+	return rc;
+      /* fall through */
+    case _mss_header:
+      if (off < hsize)
+	break;
+      mu_stream_destroy (&sp->transport);
+      rc = _check_stream_state (sp);
+      if (rc)
+	return rc;
+      /* fall through */
+    case _mss_body:
+      if (off > hsize)
+	off -= hsize;	
+      else
+	{
+	  mu_stream_destroy (&sp->transport);
+	  sp->state = _mss_init;
+	  rc = _check_stream_state (sp);
+	  if (rc)
+	    return rc;
+	}
+
+      break;
+
+    default:
+      break;
+    }
+  rc = mu_stream_seek (sp->transport, off, MU_SEEK_SET, &off);
+  if (rc == 0)
+    {
+      if (sp->state == _mss_body)
+	off += hsize;
+      *ppos = off;
+    }
+  return rc;
+}
+
+static int
+_message_stream_read (struct _mu_stream *str, char *buf, size_t bufsize,
+		      size_t *pnread)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t nread = 0;
+  int rc;
+  
+  while (bufsize)
+    {
+      size_t n;
+      rc = _check_stream_state (sp);
+      if (rc)
+	break;
+      if (sp->state == _mss_eof)
+	break;
+      rc = mu_stream_read (sp->transport, buf, bufsize, &n);
+      if (rc)
+	break;
+      if (n == 0)
+	continue;
+      nread += n;
+      buf += n;
+      bufsize -= n;
+    }
+  *pnread = nread;
+  return rc;
+}
+
+static int
+_message_stream_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
+			   int delim, size_t *pnread)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  size_t nread = 0;
+  int rc;
+  
+  while (bufsize)
+    {
+      size_t n;
+      rc = _check_stream_state (sp);
+      if (rc)
+	break;
+      if (sp->state == _mss_eof)
+	break;
+      rc = mu_stream_readdelim (sp->transport, buf, bufsize, delim, &n);
+      if (rc)
+	break;
+      if (n == 0)
+	continue;
+      nread += n;
+      if (buf[n-1] == delim)
+	break;
+      buf += n;
+      bufsize -= n;
+    }
+  *pnread = nread;
+  return rc;
+}  
+
+#if 0
+static int
+_message_stream_write (struct _mu_stream *str,
+		       const char *buf, size_t bufsize,
+		       size_t *pnwritten)
+{
+  struct _mu_message_stream *sp = (struct _mu_message_stream *)str;
+  
+  /* FIXME */
+}
+#endif
+
+static int
+_message_stream_create (mu_stream_t *pmsg, mu_message_t msg, int flags)
+{
+  struct _mu_message_stream *sp;
+
+  sp = (struct _mu_message_stream *) _mu_stream_create (sizeof (*sp),
+							flags |
+							MU_STREAM_SEEK |
+							_MU_STR_OPEN);
+  if (!sp)
+    return ENOMEM;
+
+  sp->stream.read = _message_stream_read;
+  sp->stream.readdelim = _message_stream_readdelim;
+  /* FIXME: Write is not defined */
+  /*  sp->stream.write = _message_stream_write;*/
+  sp->stream.done = _message_stream_done;
+  sp->stream.flush = _message_stream_flush;
+  sp->stream.seek = _message_stream_seek; 
+  sp->stream.size = _message_stream_size;
+  sp->state = _mss_init;
+  sp->msg = msg;
+  *pmsg = (mu_stream_t) sp;
+  return 0;
+}
+
+int
+mu_message_set_stream (mu_message_t msg, mu_stream_t stream, void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  if (msg->stream)
+    mu_stream_destroy (&msg->stream);
+  msg->stream = stream;
+  msg->flags |= MESSAGE_MODIFIED;
+  msg->flags &= ~MESSAGE_INTERNAL_STREAM;
+  return 0;
+}
+
+static int
+_message_get_stream (mu_message_t msg, mu_stream_t *pstream, int ref)
+{
+  int status;
+
+  if (msg == NULL)
+    return EINVAL;
+  if (pstream == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+
+  if (msg->stream == NULL)
+    {
+      if (msg->_get_stream)
+	{
+	  status = msg->_get_stream (msg, &msg->stream);
+	  if (status)
+	    return status;
+	}
+      else
+	{
+	  mu_header_t hdr;
+	  mu_body_t body;
+
+	  /* FIXME: Kind of a kludge: make sure the message has header
+	     and body initialized. */
+	  status = mu_message_get_header (msg, &hdr);
+	  if (status)
+	    return status;
+	  status = mu_message_get_body (msg, &body);
+	  if (status)
+	    return status;
+	  
+	  status = _message_stream_create (&msg->stream, msg, MU_STREAM_RDWR);
+	  if (status)
+	    return status;
+	  msg->flags |= MESSAGE_INTERNAL_STREAM;
+	}
+    }
+  
+  if (!ref)
+    {
+      *pstream = msg->stream;
+      return 0;
+    }
+  return mu_streamref_create (pstream, msg->stream);
+}
+
+int
+mu_message_get_stream (mu_message_t msg, mu_stream_t *pstream)
+{
+  /* FIXME: Deprecation warning */
+  return _message_get_stream (msg, pstream, 0);
+}
+
+int
+mu_message_get_streamref (mu_message_t msg, mu_stream_t *pstream)
+{
+  return _message_get_stream (msg, pstream, 1);
+}
+
+int
+mu_message_set_get_stream (mu_message_t msg,
+			   int (*_getstr) (mu_message_t, mu_stream_t *),
+			   void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_stream = _getstr;
+  return 0;
+}
diff --git a/libmu_dbm/name.c b/libmailutils/mailbox/msguid.c
similarity index 59%
copy from libmu_dbm/name.c
copy to libmailutils/mailbox/msguid.c
index 94aabae..8402e0e 100644
--- a/libmu_dbm/name.c
+++ b/libmailutils/mailbox/msguid.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -15,26 +16,33 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <unistd.h>
+#include <config.h>
 #include <stdlib.h>
+
 #include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
+#include <mailutils/message.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/sys/message.h>
+
+int
+mu_message_get_uid (mu_message_t msg, size_t *puid)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->_get_uid)
+    return msg->_get_uid (msg, puid);
+  *puid = 0;
+  return 0;
+}
 
 int
-mu_dbm_get_name (mu_dbm_file_t db, const char **pname)
+mu_message_set_uid (mu_message_t msg, int (*_get_uid) (mu_message_t, size_t *),
+		    void *owner)
 {
-  if (!db)
+  if (msg == NULL)
     return EINVAL;
-  if (!pname)
-    return MU_ERR_OUT_PTR_NULL;
-  *pname = db->db_name;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_uid = _get_uid;
   return 0;
 }
diff --git a/libmailutils/mailbox/msguidl.c b/libmailutils/mailbox/msguidl.c
new file mode 100644
index 0000000..2d5a2f2
--- /dev/null
+++ b/libmailutils/mailbox/msguidl.c
@@ -0,0 +1,102 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
+   2010, 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/header.h>
+#include <mailutils/stream.h>
+#include <mailutils/md5.h>
+#include <mailutils/sys/message.h>
+
+int
+mu_message_get_uidl (mu_message_t msg, char *buffer, size_t buflen,
+		     size_t *pwriten)
+{
+  mu_header_t header = NULL;
+  size_t n = 0;
+  int status;
+
+  if (msg == NULL || buffer == NULL || buflen == 0)
+    return EINVAL;
+
+  buffer[0] = '\0';
+  /* Try the function overload if error fallback.  */
+  if (msg->_get_uidl)
+    {
+      status = msg->_get_uidl (msg, buffer, buflen, pwriten);
+      if (status == 0)
+	return status;
+    }
+
+  /* Be compatible with Qpopper ? qppoper saves the UIDL in "X-UIDL".
+     We generate a chksum and save it in the header.  */
+  mu_message_get_header (msg, &header);
+  status = mu_header_get_value_unfold (header, "X-UIDL", buffer, buflen, &n);
+  if (status != 0 || n == 0)
+    {
+      size_t uid = 0;
+      struct mu_md5_ctx md5context;
+      mu_stream_t stream = NULL;
+      char buf[1024];
+      unsigned char md5digest[16];
+      char *tmp;
+      n = 0;
+      mu_message_get_uid (msg, &uid);
+      mu_message_get_streamref (msg, &stream);
+      mu_md5_init_ctx (&md5context);
+      status = mu_stream_seek (stream, 0, MU_SEEK_SET, NULL);
+      if (status == 0)
+	{
+	  while (mu_stream_read (stream, buf, sizeof (buf), &n) == 0
+		 && n > 0)
+	    mu_md5_process_bytes (buf, n, &md5context);
+	  mu_md5_finish_ctx (&md5context, md5digest);
+	  tmp = buf;
+	  for (n = 0; n < 16; n++, tmp += 2)
+	    sprintf (tmp, "%02x", md5digest[n]);
+	  *tmp = '\0';
+	  /* POP3 rfc says that an UID should not be longer than 70.  */
+	  snprintf (buf + 32, 70, ".%lu.%lu", (unsigned long)time (NULL), 
+		    (unsigned long) uid);
+
+	  mu_header_set_value (header, "X-UIDL", buf, 1);
+	  buflen--; /* leave space for the NULL.  */
+	  strncpy (buffer, buf, buflen)[buflen] = '\0';
+	}
+      mu_stream_destroy (&stream);
+    }
+  return status;
+}
+
+int
+mu_message_set_uidl (mu_message_t msg,
+		  int (* _get_uidl) (mu_message_t, char *, size_t, size_t *),
+		  void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_get_uidl = _get_uidl;
+  return 0;
+}


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 05 13:31:26 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RXdK2-0005JH-Ck
	for mharc-commit-mailutils@gnu.org; Mon, 05 Dec 2011 13:31:26 -0500
Received: from eggs.gnu.org ([140.186.70.92]:45185)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXdJo-00052B-Mn
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 13:31:24 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXdJh-0002tp-K2
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 13:31:12 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:53924)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXdJh-0002tV-EZ
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 13:31:05 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXdJf-000547-VP
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 18:31:04 +0000
Date: Mon, 05 Dec 2011 18:31:04 +0000
Message-Id: <E1RXdJf-000547-VP@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-497-g9371735
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4
X-Git-Newrev: 937173596bd77d773c3fc8330a1ebbe2c8c13df6
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 05 Dec 2011 18:31:24 -0000

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

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

The branch, master has been updated
       via  937173596bd77d773c3fc8330a1ebbe2c8c13df6 (commit)
       via  025c888ab07458c7b2a1e73d6b607cc9ce26cca6 (commit)
      from  217ae7a2228c6f7054b4e8e1731ec5e17f0a86a4 (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 937173596bd77d773c3fc8330a1ebbe2c8c13df6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 5 20:09:36 2011 +0200

    Implement mu_scan_datetime.
    
    * include/mailutils/stream.h (mu_fixed_memory_stream_create): New proto.
    * libmailutils/stream/memory_stream.c
    (mu_fixed_memory_stream_create): New function.
    
    * include/mailutils/util.h (mu_parse_imap_date_time)
    (mu_parse_ctime_date_time): Remove.
    (mu_scan_datetime): New proto.
    (mu_strftime): Remove const from the last arg.
    (MU_DATETIME_FROM,MU_DATETIME_IMAP)
    (MU_DATETIME_IMAP_SEARCH,MU_DATETIME_INTERNALDATE): New defines.
    * libmailutils/base/strftime.c: New file.
    * libmailutils/base/Makefile.am (libbase_la_SOURCES): Add strftime.c.
    * libmailutils/base/date.c (mu_scan_datetime): New function.
    * libmailutils/base/mutil.c (mu_strftime): Remove.
    
    * libmailutils/tests/scantime.at: New file.
    * libmailutils/tests/scantime.c: New file.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add scantime.
    (TESTSUITE_AT): Add scantime.at.
    * libmailutils/tests/strftime.c (main): Call mu_set_program_name.
    * libmailutils/tests/testsuite.at: Include scantime.at
    
    * libmu_sieve/actions.c (mime_create_reason): Use mu_c_streamftime.
    
    * imap4d/fetch.c (_frt_internaldate): Use mu_scan_datetime.
    * imap4d/util.c (util_parse_internal_date): Likewise.
    * libmu_scm/mu_message.c (mu-message-get-envelope-date): Likewise.
    * libproto/imap/fetch.c (_date_mapper): Likewise.
    * mail/from.c (hdr_date): Use mu_scan_datetime.

commit 025c888ab07458c7b2a1e73d6b607cc9ce26cca6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 3 04:44:34 2011 +0200

    Implement locale-independent strftime function.
    
    * include/mailutils/util.h (mu_c_streamftime): New prototype.
    * libmailutils/base/date.c (mu_c_streamftime): New function.
    * libmailutils/tests/.gitignore: Update.
    * libmailutils/tests/strftime.at: New test script.
    * libmailutils/tests/strftime.c: New test program.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add strftime.
    (TESTSUITE_AT): Add strftime.at.
    * libmailutils/tests/testsuite.at: Include strftime.at.

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

Summary of changes:
 imap4d/fetch.c                                     |   17 +-
 imap4d/util.c                                      |    5 +-
 include/mailutils/stream.h                         |    2 +
 include/mailutils/util.h                           |   19 +-
 libmailutils/base/Makefile.am                      |    1 +
 libmailutils/base/date.c                           |  856 ++++++++--
 libmailutils/base/mutil.c                          |   12 -
 libmu_dbm/create.c => libmailutils/base/strftime.c |   36 +-
 libmailutils/stream/memory_stream.c                |   57 +
 libmailutils/tests/.gitignore                      |    1 +
 libmailutils/tests/Makefile.am                     |    4 +
 libmailutils/tests/scantime.at                     |   64 +
 libmailutils/tests/{imapio.c => scantime.c}        |   82 +-
 libmailutils/tests/strftime.at                     | 1921 ++++++++++++++++++++
 libmailutils/tests/{imapio.c => strftime.c}        |   97 +-
 libmailutils/tests/testsuite.at                    |    7 +-
 libmu_scm/mu_message.c                             |    4 +-
 libmu_sieve/actions.c                              |   12 +-
 libproto/imap/fetch.c                              |   13 +-
 mail/from.c                                        |    2 +-
 20 files changed, 2951 insertions(+), 261 deletions(-)
 copy libmu_dbm/create.c => libmailutils/base/strftime.c (60%)
 create mode 100644 libmailutils/tests/scantime.at
 copy libmailutils/tests/{imapio.c => scantime.c} (51%)
 create mode 100644 libmailutils/tests/strftime.at
 copy libmailutils/tests/{imapio.c => strftime.c} (50%)

diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index bf6ad9e..cd7baa6 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -960,16 +960,12 @@ _frt_internaldate (struct fetch_function_closure *ffc,
   mu_envelope_t env = NULL;
   struct tm tm, *tmp = NULL;
   mu_timezone tz;
-  char datebuf[sizeof ("13-Jul-2002 00:00:00")];
-  int tzoff;
-  int tzh = 0, tzm = 0;
   
   mu_message_get_envelope (frt->msg, &env);
   if (mu_envelope_sget_date (env, &date) == 0
-      && mu_parse_ctime_date_time (&date, &tm, &tz) == 0)
+      && mu_scan_datetime (date, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
     {
       tmp = &tm;
-      tzoff = tz.utc_offset;
     }
   else
     {
@@ -977,18 +973,13 @@ _frt_internaldate (struct fetch_function_closure *ffc,
       struct timeval stv;
       struct timezone stz;
       
-      gettimeofday(&stv, &stz);
+      gettimeofday (&stv, &stz);
       t = stv.tv_sec;
-      tzoff = - stz.tz_minuteswest;
+      tz.utc_offset = - stz.tz_minuteswest;
       tmp = localtime (&t);
     }
-  tzh = tzoff / 3600;
-  if (tzoff < 0)
-    tzoff = - tzoff;
-  tzm = tzoff % 3600;
-  mu_strftime (datebuf, sizeof (datebuf), "%d-%b-%Y %H:%M:%S", tmp);
   io_sendf ("%s", ffc->name);
-  io_sendf (" \"%s %+03d%02d\"", datebuf, tzh, tzm);
+  mu_c_streamftime (iostream, " \"%d-%b-%Y %H:%M:%S %z\"", tmp, &tz);
   return 0;
 }
 
diff --git a/imap4d/util.c b/imap4d/util.c
index 4e960b2..24b0aec 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -373,9 +373,8 @@ util_parse_internal_date (char *date, time_t *timep,
   struct tm tm;
   mu_timezone tz;
   time_t time;
-  char **datep = &date;
 
-  if (mu_parse_imap_date_time ((const char **) datep, &tm, &tz))
+  if (mu_scan_datetime (date, MU_DATETIME_IMAP_SEARCH, &tm, &tz, NULL))
     return 1;
 
   adjust_tm (&tm, &tz, flag);
@@ -412,7 +411,7 @@ util_parse_ctime_date (const char *date, time_t *timep,
   struct tm tm;
   mu_timezone tz;
 
-  if (mu_parse_ctime_date_time (&date, &tm, &tz) == 0)
+  if (mu_scan_datetime (date, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
     {
       adjust_tm (&tm, &tz, flag);
       *timep = mu_tm2time (&tm, &tz);
diff --git a/include/mailutils/stream.h b/include/mailutils/stream.h
index 8bea907..dbd6654 100644
--- a/include/mailutils/stream.h
+++ b/include/mailutils/stream.h
@@ -305,6 +305,8 @@ int mu_stdio_stream_create (mu_stream_t *pstream, int fd, int flags);
 int mu_memory_stream_create (mu_stream_t *pstream, int flags);
 int mu_static_memory_stream_create (mu_stream_t *pstream, const void *mem,
 				    size_t size);
+int mu_fixed_memory_stream_create (mu_stream_t *pstream, void *mem,
+				   size_t size, int flags);
 
 int mu_mapfile_stream_create (mu_stream_t *pstream, const char *filename,
 			      int flags);
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index d0ffd63..88e561a 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -61,15 +61,22 @@ struct mu_timezone
 typedef struct mu_timezone mu_timezone;
 
 int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
-int mu_parse_imap_date_time (const char **p, struct tm *tm,
-			     mu_timezone *tz);
-int mu_parse_ctime_date_time (const char **p, struct tm *tm,
-			      mu_timezone *tz);
 
 time_t mu_utc_offset (void);
 time_t mu_tm2time (struct tm *timeptr, mu_timezone *tz);
-size_t mu_strftime (char *s, size_t max, const char *format,
-		    const struct tm *tm);
+size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
+
+int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
+		      struct mu_timezone *tz);
+int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
+		      struct mu_timezone *tz, char **endp);
+
+  /* Common datetime formats: */
+#define MU_DATETIME_FROM         "%a %b %e %H:%M:%S %Y"
+#define MU_DATETIME_IMAP         "%d-%b-%Y %H:%M:%S %z"
+#define MU_DATETIME_IMAP_SEARCH  "%d-%b-%Y%? %H:%M:%S %z"
+#define MU_DATETIME_INTERNALDATE "%a, %e %b %Y %H:%M:%S %z"
+
   
   /* ----------------------- */
   /* File & path names.      */
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index cb6c521..a2e5ac5 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -61,6 +61,7 @@ libbase_la_SOURCES = \
  sha1.c\
  secret.c\
  spawnvp.c\
+ strftime.c\
  symlink.c\
  tempfile.c\
  ticket.c\
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
index e1e03fb..f784bea 100644
--- a/libmailutils/base/date.c
+++ b/libmailutils/base/date.c
@@ -23,16 +23,25 @@
 #include <stdlib.h>
 #include <string.h>
 #include <mailutils/util.h>
+#include <mailutils/stream.h>
+#include <mailutils/errno.h>
 #include <mailutils/cstr.h>
+#include <mailutils/cctype.h>
 
 #define SECS_PER_DAY 86400
 #define ADJUSTMENT -719162L
 
-static time_t
+/* Julian day is the number of days since Jan 1, 4713 BC (ouch!).
+   Eg. Jan 1, 1900 is: */
+#define  JULIAN_1900    1721425L
+
+/* Computes the number of days elapsed since January, 1 1900 to the
+   January, 1 of the given year */
+static unsigned
 jan1st (int year)
 {
   year--;               /* Do not consider the current year */
-  return  year*365L
+  return  year * 365L
                + year/4L    /* Years divisible by 4 are leap years */
                + year/400L  /* Years divisible by 400 are always leap years */
                - year/100L; /* Years divisible by 100 but not 400 aren't */
@@ -48,7 +57,7 @@ static int  month_start[]=
 #define leap_year(y) ((y) % 4 == 0 && (y) % 100 != 0 || (y) % 400 == 0)
 
 static int
-dayofyear (time_t *pday, int year, int month, int day)
+dayofyear (int year, int month, int day)
 {
   int  leap, month_days;
 
@@ -66,10 +75,50 @@ dayofyear (time_t *pday, int year, int month, int day)
   if (month <= 2)
     leap = 0;
 
-  *pday = month_start[month] + day + leap;
+  return month_start[month] + day + leap;
+}
+
+/* Returns number of days in a given year */
+static int
+year_days (int year)
+{
+  return dayofyear (year, 11, 31);
+}
+
+static int
+julianday (unsigned *pd, int year, int month, int day)
+{
+  int total = dayofyear (year, month, day);
+  if (total == -1)
+    return -1;
+  *pd = JULIAN_1900 + total + jan1st (year);
   return 0;
 }
 
+static int
+dayofweek (int year, int month, int day)
+{
+  unsigned jd;
+
+  if (julianday (&jd, year, month, day))
+    return -1;
+
+  /* January 1, 1900 was Monday, hence +1 */
+  return (jd + 1) % 7;
+}
+
+#define ISO_8601_START_WDAY 1 /* Monday */
+#define ISO_8601_MAX_WDAY   4 /* Thursday */
+#define MAXDAYS 366 /* Max. number of days in a year */
+#define ISO_8601_OFF ((MAXDAYS / 7 + 2) * 7)
+
+int
+ISO_8601_weekdays (int yday, int wday)
+{
+  return (yday
+	  - (yday - wday + ISO_8601_MAX_WDAY + ISO_8601_OFF) % 7
+	  + ISO_8601_MAX_WDAY - ISO_8601_START_WDAY);
+}
 
 /* Convert struct tm into time_t, taking into account timezone offset. */
 /* FIXME: It does not take DST into account */
@@ -77,12 +126,14 @@ time_t
 mu_tm2time (struct tm *tm, mu_timezone *tz)
 {
   time_t t;
+  int day;
   
-  if (dayofyear (&t, tm->tm_year, tm->tm_mon, tm->tm_mday - 1))
+  day = dayofyear (tm->tm_year, tm->tm_mon, tm->tm_mday - 1);
+  if (day == -1)
     return -1;
-  t = (t + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
+  t = (day + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
             + (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
-      - tz->utc_offset;
+       - (tz ? tz->utc_offset : 0);
   return t;
 }
 
@@ -97,151 +148,730 @@ mu_utc_offset (void)
   return - mktime (tm);
 }
 
-static const char *months[] =
+static const char *short_month[] =
 {
   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
+  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 };
 
-static const char *wdays[] =
+static const char *full_month[] =
 {
-  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
+  "January", "February", "March", "April",
+  "May", "June", "July", "August",
+  "September", "October", "November", "December"
 };
 
-int
-mu_parse_imap_date_time (const char **p, struct tm *tm, mu_timezone *tz)
-{
-  int year, mon, day, hour, min, sec;
-  char zone[6] = "+0000";	/* ( "+" / "-" ) hhmm */
-  char month[5] = "";
-  int hh = 0;
-  int mm = 0;
-  int sign = 1;
-  int scanned = 0, scanned3;
-  int i;
-  int tzoffset;
-
-  day = mon = year = hour = min = sec = 0;
+static const char *short_wday[] =
+{
+  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+};
 
-  memset (tm, 0, sizeof (*tm));
+static const char *full_wday[] =
+{
+  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
+  "Friday", "Saturday"
+};
 
-  switch (sscanf (*p,
-		  "%2d-%3s-%4d%n %2d:%2d:%2d %5s%n",
-		  &day, month, &year, &scanned3, &hour, &min, &sec, zone,
-		  &scanned))
+int
+mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *input_tm,
+		  struct mu_timezone *tz)
+{
+  int rc = 0;
+  struct tm tm;
+
+  /* Copy input TM because it might have been received from gmtime and
+     the fmt might result in further calls to gmtime which will clobber
+     it. */
+  tm = *input_tm;
+  while (*fmt && rc == 0)
     {
-    case 3:
-      scanned = scanned3;
-      break;
-    case 7:
-      break;
-    default:
-      return -1;
-    }
+      size_t len = strcspn (fmt, "%");
+      if (len)
+	{
+	  rc = mu_stream_write (str, fmt, len, NULL);
+	  if (rc)
+	    break;
+	}
 
-  tm->tm_sec = sec;
-  tm->tm_min = min;
-  tm->tm_hour = hour;
-  tm->tm_mday = day;
+      fmt += len;
 
-  for (i = 0; i < 12; i++)
-    {
-      if (mu_c_strncasecmp (month, months[i], 3) == 0)
+    restart:
+      if (!*fmt || !*++fmt)
+	break;
+
+      switch (*fmt)
 	{
-	  mon = i;
+	case 'a':
+	  /* The abbreviated weekday name. */
+	  if (tm.tm_wday < 0 || tm.tm_wday > 6)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, short_wday[tm.tm_wday],
+				  strlen (short_wday[tm.tm_wday]), NULL);
+	  break;
+	  
+	case 'A':
+	  /* The full weekday name. */
+	  if (tm.tm_wday < 0 || tm.tm_wday > 6)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, full_wday[tm.tm_wday],
+				  strlen (full_wday[tm.tm_wday]), NULL);
+	  break;
+	  
+	case 'b':
+	case 'h':
+	  /* The abbreviated month name. */
+	  if (tm.tm_mon < 0 || tm.tm_mon > 11)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, short_month[tm.tm_mon],
+				  strlen (short_month[tm.tm_mon]), NULL);
+	  break;
+	  
+	case 'B':
+	  /* The full month name. */
+	  if (tm.tm_mon < 0 || tm.tm_mon > 11)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, full_month[tm.tm_mon],
+				  strlen (full_month[tm.tm_mon]), NULL);
+	  break;
+	  
+	case 'c':
+	  /* The preferred date and time representation. */
+	  rc = mu_c_streamftime (str, "%a %b %e %H:%M:%S %Y", &tm, tz);
+	  break;
+	    
+	case 'C':
+	  /* The century number (year/100) as a 2-digit integer. */
+	  rc = mu_stream_printf (str, "%02d", (tm.tm_year + 1900) / 100);
+	  break;
+	  
+	case 'd':
+	  /* The day of the month as a decimal number (range 01 to 31). */
+	  if (tm.tm_mday < 1 || tm.tm_mday > 31)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_printf (str, "%02d", tm.tm_mday);
+	  break;
+	  
+	case 'D':
+	  /* Equivalent to %m/%d/%y. */
+	  rc = mu_c_streamftime (str, "%m/%d/%y", &tm, tz);
+	  break;
+	  
+	case 'e':
+	  /* Like %d, the day of the month as a decimal number, but a leading
+	     zero is replaced by a space. */
+	  if (tm.tm_mday < 1 || tm.tm_mday > 31)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_printf (str, "%2d", tm.tm_mday);
+	  break;
+	  
+	case 'E':
+	  /* Modifier. The Single Unix Specification mentions %Ec, %EC, %Ex,
+	     %EX, %Ey, and %EY, which are supposed to use a corresponding
+	     locale-dependent alternative representation.
+	     A no-op in POSIX locale */
+	  goto restart;
+	  
+	case 'F':
+	  /* Equivalent to %Y-%m-%d (the ISO 8601 date format). */
+	  rc = mu_c_streamftime (str, "%Y-%m-%d", &tm, tz);
+	  break;
+	  	  
+	case 'V':
+	  /* The ISO 8601:1988 week number of the current year as a decimal
+	     number range 01 to 53, where week 1 is the first week that has
+	     at least 4 days in the current year, and with Monday as the
+	     first day of the week.
+	  */
+	case 'G':
+	  /* The ISO 8601 year with century as a decimal number.  The 4-digit
+	     year corresponding to the ISO week number (see  %V).  This has
+	     the same format and value as %y, except that if the ISO week
+	     number belongs to the previous or next year, that year is used
+	     instead.
+	  */
+	case 'g':
+	  /* Like  %G, but without century, that is, with a 2-digit year
+	     (00-99). */
+	  {
+	    int year = tm.tm_year + 1900;
+	    int days = ISO_8601_weekdays (tm.tm_yday, tm.tm_wday);
+
+	    if (days < 0)
+	      {
+		days = ISO_8601_weekdays (tm.tm_yday + year_days (year - 1),
+					  tm.tm_wday);
+		year--;
+	      }
+	    else
+	      {
+		int d = ISO_8601_weekdays (tm.tm_yday - year_days (year),
+					   tm.tm_wday);
+		if (d >= 0)
+		  {
+		    year++;
+		    days = d;
+		  }
+	      }
+
+	    switch (*fmt)
+	      {
+	      case 'V':
+		rc = mu_stream_printf (str, "%02d", days / 7 + 1);
+		break;
+
+	      case 'G':
+		rc = mu_stream_printf (str, "%4d", year);
+		break;
+
+	      case 'g':
+		rc = mu_stream_printf (str, "%02d", year % 100);
+	      }
+	  }
+	  break;
+	  
+	case 'H':
+	  /* The hour as a decimal number using a 24-hour clock (range 00 to
+	     23). */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_hour);
+	  break;
+	  
+	case 'I':
+	  /* The hour as a decimal number using a 12-hour clock (range 01 to
+	     12). */
+	  {
+	    unsigned n = tm.tm_hour % 12;
+	    rc = mu_stream_printf (str, "%02d", n == 0 ? 12 : n);
+	  }
 	  break;
-	}
-    }
-  tm->tm_mon = mon;
-  tm->tm_year = (year > 1900) ? year - 1900 : year;
-  tm->tm_yday = 0;		/* unknown. */
-  tm->tm_wday = 0;		/* unknown. */
-#if HAVE_STRUCT_TM_TM_ISDST
-  tm->tm_isdst = -1;		/* unknown. */
-#endif
 
-  hh = (zone[1] - '0') * 10 + (zone[2] - '0');
-  mm = (zone[3] - '0') * 10 + (zone[4] - '0');
-  sign = (zone[0] == '-') ? -1 : +1;
-  tzoffset = sign * (hh * 60 * 60 + mm * 60);
+	case 'j':
+	  /* The day of the year as a decimal number (range 001 to 366). */
+	  rc = mu_stream_printf (str, "%03d", tm.tm_yday + 1);
+	  break;
+	  
+	case 'k':
+	  /* The hour (24-hour clock) as a decimal number (range 0 to 23);
+	     single digits are preceded by a blank. */
+	  rc = mu_stream_printf (str, "%2d", tm.tm_hour);
+	  break;
+	  
+	case 'l':
+	  /* The hour (12-hour clock) as a decimal number (range 1 to 12);
+	     single digits are preceded by a blank. */
+	  {
+	    unsigned n = tm.tm_hour % 12;
+	    rc = mu_stream_printf (str, "%2d", n == 0 ? 12 : n);
+	  }
+	  break;
+	  
+	case 'm':
+	  /* The month as a decimal number (range 01 to 12). */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_mon + 1);
+	  break;
+				   
+	case 'M':
+	  /* The minute as a decimal number (range 00 to 59). */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_min);
+	  break;
+	  
+	case 'n':
+	  /* A newline character. */
+	  rc = mu_stream_write (str, "\n", 1, NULL);
+	  break;
+	  
+	case 'O':
+	  /* Modifier.  The Single Unix Specification mentions %Od, %Oe, %OH,
+	     %OI, %Om, %OM, %OS, %Ou, %OU, %OV, %Ow, %OW, and %Oy, which are
+	     supposed to use alternative numeric symbols.
+
+	     Hardly of any use for our purposes, hence a no-op. */
+	  goto restart;
+	  
+	case 'p':
+	  /* Either "AM" or "PM" according to the given time value.
+	     Noon is treated as "PM" and midnight as "AM". */
+	  rc = mu_stream_write (str,
+				tm.tm_hour < 12 ? "AM" : "PM",
+				2, NULL);
+	  break;
+				
+	case 'P':
+	  /* Like %p but in lowercase: "am" or "pm". */
+	  rc = mu_stream_write (str,
+				tm.tm_hour < 12 ? "am" : "pm",
+				2, NULL);
+	  break;
+	  
+	case 'r':
+	  /* The time in a.m. or p.m. notation, i.e. %I:%M:%S %p. */
+	  rc = mu_c_streamftime (str, "%I:%M:%S %p", &tm, tz);
+	  break;
+	  
+	case 'R':
+	  /* The time in 24-hour notation (%H:%M) */
+	  rc = mu_c_streamftime (str, "%H:%M", &tm, tz);
+	  break;
+	  
+	case 's':
+	  /* The number of seconds since the Epoch */
+	  rc = mu_stream_printf (str, "%lu",
+				 (unsigned long) mu_tm2time (&tm, tz));
+	  break;
+	  
+	case 'S':
+	  /* The second as a decimal number (range 00 to 60) */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_sec);
+	  break;
+	  
+	case 't':
+	  /* A tab character. */
+	  rc = mu_stream_write (str, "\t", 1, NULL);
+	  break;
+	  
+	case 'T':
+	  /* The time in 24-hour notation (%H:%M:%S) */
+	  rc = mu_c_streamftime (str, "%H:%M:%S", &tm, tz);
+	  break;
+	  
+	case 'u':
+	  /* The day of the week as a decimal, range 1 to 7, Monday being 1.
+	   */
+	  rc = mu_stream_printf (str, "%1d",
+				 tm.tm_wday == 0 ? 7 : tm.tm_wday);
+	  break;
+	  
+	case 'U':
+	  /* The week number of the current year as a decimal number, range
+	     00 to 53, starting with the first Sunday as the first day of
+	     week 01.
+	  */
+	  rc = mu_stream_printf (str, "%02d",
+				 (tm.tm_yday - tm.tm_wday + 7) / 7);
+	  break;
+	  
+	case 'w':
+	  /* The day of the week as a decimal, range 0 to 6, Sunday being 0.
+	   */
+	  rc = mu_stream_printf (str, "%01d", tm.tm_wday);
+	  break;
+	  
+	case 'W':
+	  /* The week number of the current year as a decimal number, range
+	     00 to 53, starting with the first Monday as the first day of
+	     week 01. */
+	  rc = mu_stream_printf (str, "%02d", 
+			 (tm.tm_yday - (tm.tm_wday - 1 + 7) % 7 + 7) / 7);
+	  break;
 
-#if HAVE_STRUCT_TM_TM_GMTOFF
-  tm->tm_gmtoff = tzoffset;
-#endif
+	  /* The preferred date representation without the time:
+	     equivalent to %D */
+	case 'x':
+	  rc = mu_c_streamftime (str, "%m/%d/%y", &tm, tz);
+	  break;
+	  
+	case 'X':
+	  /* The preferred date representation without the date */
+	  rc = mu_c_streamftime (str, "%H:%M:%S", &tm, tz);
+	  break;
 
-  if (tz)
-    {
-      tz->utc_offset = tzoffset;
-      tz->tz_name = NULL;
+	case 'y':
+	  /* The year as a decimal number without a century (range 00 to 99).
+	   */
+	  rc = mu_stream_printf (str, "%02d", (tm.tm_year + 1900) % 100);
+	  break;
+	  
+	case 'Y':
+	  /* The year as a decimal number including the century. */
+	  rc = mu_stream_printf (str, "%d", tm.tm_year + 1900);
+	  break;
+	  
+	case 'z':
+	case 'Z':
+	  /* The time-zone as hour offset from GMT, for formatting RFC-822
+	     dates (e.g. "%a, %d %b %Y %H:%M:%S %z") */
+	  {
+	    int utc_off = tz ? tz->utc_offset : mu_utc_offset ();
+	    int sign;
+	    if (utc_off < 0)
+	      {
+		sign = '-';
+		utc_off = - utc_off;
+	      }
+	    else
+	      sign = '+';
+	    utc_off /= 60;
+	    rc = mu_stream_printf (str, "%c%02u%02u", sign,
+				   utc_off / 60, utc_off % 60);
+	  }
+	  break;
+	  
+	case '%':
+	  /* A literal '%' character. */
+	  rc = mu_stream_write (str, "%", 1, NULL);
+	  break;
+	  
+	case '+':
+	  /* Not supported (date and time in date(1) format. */
+	default:
+	  rc = mu_stream_write (str, fmt-1, 2, NULL);
+	  break;
+	}
+      fmt++;
     }
-
-  *p += scanned;
-
-  return 0;
+  /* Restore input tm */
+  *input_tm = tm;
+  return rc;
 }
 
-/* "ctime" format is: Thu Jul 01 15:58:27 1999, with no trailing \n.  */
-int
-mu_parse_ctime_date_time (const char **p, struct tm *tm, mu_timezone *tz)
-{
-  int wday = 0;
-  int year = 0;
-  int mon = 0;
-  int day = 0;
-  int hour = 0;
-  int min = 0;
-  int sec = 0;
-  int n = 0;
+static int
+_mu_short_weekday_string (const char *str)
+{
   int i;
-  char weekday[5] = "";
-  char month[5] = "";
-
-  if (sscanf (*p, "%3s %3s %2d %2d:%2d:%2d %d%n\n",
-	weekday, month, &day, &hour, &min, &sec, &year, &n) != 7)
-    return -1;
-
-  *p += n;
+  
+  for (i = 0; i < 7; i++)
+    {
+      if (mu_c_strncasecmp (str, short_wday[i], 3) == 0)
+	return i;
+    }
+  return -1;
+}
 
+static int
+_mu_full_weekday_string (const char *str, char **endp)
+{
+  int i;
+  
   for (i = 0; i < 7; i++)
     {
-      if (mu_c_strncasecmp (weekday, wdays[i], 3) == 0)
+      if (mu_c_strcasecmp (str, full_wday[i]) == 0)
 	{
-	  wday = i;
-	  break;
+	  if (endp)
+	    *endp = (char*) (str + strlen (full_wday[i]));
+	  return i;
 	}
     }
+  return -1;
+}
+
 
+static int
+_mu_short_month_string (const char *str)
+{  
+  int i;
+  
   for (i = 0; i < 12; i++)
     {
-      if (mu_c_strncasecmp (month, months[i], 3) == 0)
+      if (mu_c_strncasecmp (str, short_month[i], 3) == 0)
+	return i;
+    }
+  return -1;
+}
+
+static int
+_mu_full_month_string (const char *str, char **endp)
+{  
+  int i;
+  
+  for (i = 0; i < 12; i++)
+    {
+      if (mu_c_strcasecmp (str, full_month[i]) == 0)
 	{
-	  mon = i;
-	  break;
+	  if (endp)
+	    *endp = (char*) (str + strlen (full_month[i]));
+	  return i;
 	}
     }
+  return -1;
+}
 
-  if (tm)
-    {
-      memset (tm, 0, sizeof (struct tm));
-
-      tm->tm_sec = sec;
-      tm->tm_min = min;
-      tm->tm_hour = hour;
-      tm->tm_mday = day;
-      tm->tm_wday = wday;
-      tm->tm_mon = mon;
-      tm->tm_year = (year > 1900) ? year - 1900 : year;
+int
+get_num (const char *str, char **endp, int ndig, int minval, int maxval,
+	 int *pn)
+{
+  int x = 0;
+  int i;
+  
+  errno = 0;
+  for (i = 0; i < ndig && *str && mu_isdigit (*str); str++, i++)
+    x = x * 10 + *str - '0';
+
+  *endp = (char*) str;
+  if (i == 0)
+    return -1;
+  else if (pn)
+    *pn = i;
+  else if (i != ndig)
+    return -1;
+  if (x < minval || x > maxval)
+    return -1;
+  return x;
+}
+
+#define DT_YEAR  0x01
+#define DT_MONTH 0x02
+#define DT_MDAY  0x04
+#define DT_WDAY  0x08
+#define DT_HOUR  0x10
+#define DT_MIN   0x20
+#define DT_SEC   0x40
+
+int
+mu_scan_datetime (const char *input, const char *fmt,
+		  struct tm *tm, struct mu_timezone *tz, char **endp)
+{
+  int rc = 0;
+  char *p;
+  int n;
+  int eof_ok = 0;
+  int datetime_parts = 0;
+  
+  memset (tm, 0, sizeof *tm);
 #ifdef HAVE_STRUCT_TM_TM_ISDST
-      tm->tm_isdst = -1;	/* unknown. */
+  tm->tm_isdst = -1;	/* unknown. */
 #endif
-    }
-
-  /* ctime has no timezone information, set tz to local TZ if they ask. */
+  /* provide default timezone, in case it is not supplied in input */
   if (tz)
     {
+      memset (tz, 0, sizeof *tz);
       tz->utc_offset = mu_utc_offset ();
-      tz->tz_name = NULL;
     }
+
+  /* Skip leading whitespace */
+  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
+  for (; *fmt && rc == 0; fmt++)
+    {
+      if (mu_isspace (*fmt))
+	{
+	  fmt = mu_str_skip_class (fmt, MU_CTYPE_BLANK);
+	  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
+	  if (!*fmt)
+	    break;
+	}
+      eof_ok = 0;
+      
+      if (*fmt == '%')
+	{
+	  switch (*++fmt)
+	    {
+	    case 'a':
+	      /* The abbreviated weekday name. */
+	      n = _mu_short_weekday_string (input);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_wday = n;
+		  datetime_parts |= DT_WDAY;
+		  input += 3;
+		}
+	      break;
+		  
+	    case 'A':
+	      /* The full weekday name. */
+	      n = _mu_full_weekday_string (input, &p);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_wday = n;
+		  datetime_parts |= DT_WDAY;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'b':
+	      /* The abbreviated month name. */
+	      n = _mu_short_month_string (input);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mon = n;
+		  datetime_parts |= DT_MONTH;
+		  input += 3;
+		}
+	      break;
+
+	    case 'B':
+	      /* The full month name. */
+	      n = _mu_full_month_string (input, &p);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mon = n;
+		  datetime_parts |= DT_MONTH;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'd':
+	      /* The day of the month as a decimal number (range 01 to 31). */
+	      n = get_num (input, &p, 2, 1, 31, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mday = n;
+		  datetime_parts |= DT_MDAY;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'e':
+	      /* Like %d, the day of the month as a decimal number, but a
+		 leading zero is replaced by a space. */
+	      {
+		int ndig;
+		
+		n = get_num (input, &p, 2, 1, 31, &ndig);
+		if (n == -1)
+		  rc = MU_ERR_PARSE;
+		else
+		  {
+		    tm->tm_mday = n;
+		    datetime_parts |= DT_MDAY;
+		    input = p;
+		  }
+	      }
+	      break;
+	      
+	    case 'H':
+	      /* The hour as a decimal number using a 24-hour clock (range
+		 00 to 23). */
+	      n = get_num (input, &p, 2, 0, 23, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_hour = n;
+		  datetime_parts |= DT_HOUR;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'm':
+	      /* The month as a decimal number (range 01 to 12). */
+	      n = get_num (input, &p, 2, 1, 12, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mon = n - 1;
+		  datetime_parts |= DT_MONTH;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'M':
+	      /* The minute as a decimal number (range 00 to 59). */
+	      n = get_num (input, &p, 2, 0, 59, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_min = n;
+		  datetime_parts |= DT_MIN;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'S':
+	      /* The second as a decimal number (range 00 to 60) */
+	      n = get_num (input, &p, 2, 0, 60, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_sec = n;
+		  datetime_parts |= DT_SEC;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'Y':
+	      /* The year as a decimal number including the century. */
+	      errno = 0;
+	      n = strtoul (input, &p, 10);
+	      if (errno || p == input)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_year = n - 1900;
+		  datetime_parts |= DT_YEAR;
+		  input = p;
+		}
+	      break;
+		  
+	    case 'z':
+	      /* The time-zone as hour offset from GMT */
+	      {
+		int sign = 1;
+		int hr;
+		
+		if (*input == '+')
+		  input++;
+		else if (*input == '-')
+		  {
+		    input++;
+		    sign = -1;
+		  }
+		n = get_num (input, &p, 2, 0, 11, NULL);
+		if (n == -1)
+		  rc = MU_ERR_PARSE;
+		else
+		  {
+		    input = p;
+		    hr = n;
+		    n = get_num (input, &p, 2, 0, 59, NULL);
+		    if (n == -1)
+		      rc = MU_ERR_PARSE;
+		    else
+		      {
+			input = p;
+			if (tz)
+			  tz->utc_offset = sign * (hr * 60 + n) * 60;
+		      }
+		  }
+	      }
+	      break;
+		      
+	    case '%':
+	      if (*input == '%')
+		input++;
+	      else
+		rc = MU_ERR_PARSE;
+	      break;
+	      
+	    case '?':
+	      eof_ok = 1;
+	      break;
+	    }
+	  if (eof_ok && rc == 0 && *input == 0)
+	    break;
+	}
+      else if (*input != *fmt)
+	rc = MU_ERR_PARSE;
+      else
+	input++;
+    }
+
+  if (!eof_ok && rc == 0 && *input == 0 && *fmt)
+    rc = MU_ERR_PARSE;
+
+  if (!(datetime_parts & DT_WDAY) &&
+      (datetime_parts & (DT_YEAR|DT_MONTH|DT_MDAY)) ==
+      (DT_YEAR|DT_MONTH|DT_MDAY))
+    tm->tm_wday = dayofweek (tm->tm_year + 1900, tm->tm_mon, tm->tm_mday);
   
-  return 0;
+  if (endp)
+    *endp = (char*) input;
+  
+  return rc;
 }
diff --git a/libmailutils/base/mutil.c b/libmailutils/base/mutil.c
index 87b2586..6cf4cfb 100644
--- a/libmailutils/base/mutil.c
+++ b/libmailutils/base/mutil.c
@@ -56,18 +56,6 @@ mu_mh_delim (const char *str)
   return str[0] == '\n';
 }
 
-/* A locale-independent version of strftime */
-size_t
-mu_strftime (char *s, size_t max, const char *format, const struct tm *tm)
-{
-  size_t size;
-  mu_set_locale ("C");
-  size = strftime (s, max, format, tm);
-  mu_restore_locale ();
-  return size;
-}
-  
-
 static void
 assoc_str_free (void *data)
 {
diff --git a/libmu_dbm/create.c b/libmailutils/base/strftime.c
similarity index 60%
copy from libmu_dbm/create.c
copy to libmailutils/base/strftime.c
index 44c161d..5c1d89e 100644
--- a/libmu_dbm/create.c
+++ b/libmailutils/base/strftime.c
@@ -18,27 +18,27 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <unistd.h>
-#include <stdlib.h>
-#include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
 #include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/stream.h>
+#include <mailutils/errno.h>
+#include <mailutils/cstr.h>
 
-int
-mu_dbm_create (char *name, mu_dbm_file_t *db, int defsafety)
+/* A locale-independent version of strftime */
+size_t
+mu_strftime (char *buf, size_t size, const char *format, struct tm *tm)
 {
   int rc;
-  mu_url_t url;
+  mu_stream_t str;
+  mu_stream_stat_buffer stat;
 
-  mu_dbm_init ();
-  rc = mu_url_create_hint (&url, name, 0, mu_dbm_hint);
-  if (rc)
-    return rc;
-  rc = mu_dbm_create_from_url (url, db, defsafety);
-  mu_url_destroy (&url);
-  return rc;
+  if (mu_fixed_memory_stream_create (&str, buf, size, MU_STREAM_WRITE))
+    return 0;
+  mu_stream_set_stat (str, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT), stat);
+  rc = mu_c_streamftime (str, format, tm, NULL);
+  if (rc == 0)
+    rc = mu_stream_write (str, "", 1, NULL);
+  mu_stream_unref (str);
+  return rc ? 0 : stat[MU_STREAM_STAT_OUT] - 1;
 }
+  
+
diff --git a/libmailutils/stream/memory_stream.c b/libmailutils/stream/memory_stream.c
index 8bd260f..70e009d 100644
--- a/libmailutils/stream/memory_stream.c
+++ b/libmailutils/stream/memory_stream.c
@@ -88,6 +88,27 @@ _memory_write (mu_stream_t stream, const char *iptr, size_t isize,
 }
 
 static int
+_fixed_size_memory_write (mu_stream_t stream, const char *iptr, size_t isize,
+			  size_t *nbytes)
+{
+  struct _mu_memory_stream *mfs = (struct _mu_memory_stream *) stream;
+  
+  if (mfs->capacity < mfs->offset + isize)
+    isize = mfs->capacity - mfs->offset;
+
+  memcpy (mfs->ptr + mfs->offset, iptr, isize);
+  
+  mfs->offset += isize;
+
+  if (mfs->offset > mfs->size)
+    mfs->size = mfs->offset;
+
+  if (nbytes)
+    *nbytes = isize;
+  return 0;
+}
+
+static int
 _memory_truncate (mu_stream_t stream, mu_off_t len)
 {
   struct _mu_memory_stream *mfs = (struct _mu_memory_stream *) stream;
@@ -271,3 +292,39 @@ mu_static_memory_stream_create (mu_stream_t *pstream, const void *mem,
   return 0;
 }
   
+int
+mu_fixed_memory_stream_create (mu_stream_t *pstream, void *mem,
+			       size_t size, int flags)
+{
+  mu_stream_t stream;
+  struct _mu_memory_stream *str;
+
+  flags &= (MU_STREAM_READ|MU_STREAM_WRITE);
+  if (!flags)
+    return EINVAL;
+  
+  str = (struct _mu_memory_stream *)
+    _mu_stream_create (sizeof (*str), flags | MU_STREAM_SEEK);
+  
+  if (!str)
+    return ENOMEM;
+
+  str->ptr = (void*) mem;
+  str->size = size;
+  str->offset = 0;
+  str->capacity = size;
+
+  str->stream.flags |= _MU_STR_OPEN;
+  if (flags & MU_STREAM_READ)
+    str->stream.read = _memory_read;
+  if (flags & MU_STREAM_WRITE)
+    str->stream.write = _fixed_size_memory_write;
+  str->stream.size = _memory_size;
+  str->stream.ctl = _memory_ioctl;
+  str->stream.seek = _memory_seek;
+  
+  stream = (mu_stream_t) str;
+  *pstream = stream;
+
+  return 0;
+}
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index 444e39e..ca22f53 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -16,6 +16,7 @@ imapio
 listop
 mailcap
 prop
+strftime
 url-comp
 url-parse
 wicket
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 8463ffe..01c01ac 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -51,6 +51,8 @@ noinst_PROGRAMS = \
  listop\
  mailcap\
  prop\
+ scantime\
+ strftime\
  tempfile\
  url-comp\
  url-parse\
@@ -81,6 +83,8 @@ TESTSUITE_AT = \
  list.at\
  mailcap.at\
  prop.at\
+ scantime.at\
+ strftime.at\
  testsuite.at\
  url.at\
  url-comp.at\
diff --git a/libmailutils/tests/scantime.at b/libmailutils/tests/scantime.at
new file mode 100644
index 0000000..e4d7e2d
--- /dev/null
+++ b/libmailutils/tests/scantime.at
@@ -0,0 +1,64 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_BANNER(mu_scan_datetime)
+
+dnl ---------------------------------------------------------------------
+dnl SCANTIME([NAME], [KW], [FMT], [INPUT], [STDOUT = `'], [STDERR = `'])
+dnl
+m4_pushdef([SCANTIME],[
+m4_pushdef([MU_TEST_GROUP],[scantime])
+m4_pushdef([MU_TEST_KEYWORDS],[scantime mu_scan_datetime])
+m4_pushdef([MU_TEST_COMMAND],[TZ=0 scantime -format='$3'])
+MU_GENERIC_TEST([$1],[$2],[$4],[],[$5],[$6])
+m4_popdef([MU_TEST_COMMAND])
+m4_popdef([MU_TEST_KEYWORDS])
+m4_popdef([MU_TEST_GROUP])
+])
+dnl ---------------------------------------------------------------------
+
+SCANTIME([RFC-822 time],[rfc822],
+[%a %b %e %H:%M:%S %Y],
+[Tue May  3 13:25:26 2011
+Fri Nov 11 11:55:01 2011],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=0
+sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,tz=0
+])
+
+SCANTIME([IMAP time format],[imap],
+[%d-%b-%Y %H:%M:%S %z],
+[03-May-2011 13:25:26 +0100
+11-Nov-2011 11:55:01 +0100],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=3600
+sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,tz=3600
+])
+
+SCANTIME([IMAP search time format],[imap-search],
+[%d-%b-%Y%? %H:%M:%S %z],
+[03-May-2011 13:25:26 +0100
+03-May-2011],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=3600
+sec=0,min=0,hour=0,mday=3,mon=4,year=111,wday=2,tz=0
+])
+
+SCANTIME([IMAP INTERNALDATE],[imap-internaldate],
+[%a, %d %b %Y %H:%M:%S %z],
+[Tue, 03 May 2011 13:25:26 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=7200
+])
+
+m4_popdef([SCANTIME])
+dnl ---------------------------------------------------------------------
diff --git a/libmailutils/tests/imapio.c b/libmailutils/tests/scantime.c
similarity index 51%
copy from libmailutils/tests/imapio.c
copy to libmailutils/tests/scantime.c
index 9783115..58464ac 100644
--- a/libmailutils/tests/imapio.c
+++ b/libmailutils/tests/scantime.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2007, 2009, 2010, 2011 Free Software
+   Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -16,20 +17,22 @@
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
-#endif 
+#endif
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <mailutils/imapio.h>
-#include <mailutils/errno.h>
 #include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/util.h>
 #include <mailutils/stream.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
 #include <mailutils/stdstream.h>
 
 void
 usage ()
 {
-  mu_stream_printf (mu_strout, "usage: %s [debug=SPEC] [-transcript] [-server]\n",
+  mu_stream_printf (mu_strout, "usage: %s [-format=FMT] [-tz=TZ]\n",
 		    mu_program_name);
   exit (0);
 }
@@ -37,24 +40,22 @@ usage ()
 int
 main (int argc, char **argv)
 {
-  int i, rc;
-  int transcript = 0;
-  mu_imapio_t io;
-  mu_stream_t str;
-  int imapio_mode = MU_IMAPIO_CLIENT;
+  int rc, i;
+  char *format = "%d-%b-%Y%? %H:%M:%S %z";
+  char *buf = NULL;
+  size_t size = 0;
+  size_t n;
+
+  mu_set_program_name (argv[0]);
   
   mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
-  
+
   for (i = 1; i < argc; i++)
     {
       char *opt = argv[i];
 
-      if (strncmp (opt, "debug=", 6) == 0)
-	mu_debug_parse_spec (opt + 6);
-      else if (strcmp (opt, "-transcript") == 0)
-	transcript = 1;
-      else if (strcmp (opt, "-server") == 0)
-	imapio_mode = MU_IMAPIO_SERVER;
+      if (strncmp (opt, "-format=", 8) == 0)
+	format = opt + 8;
       else if (strcmp (opt, "-h") == 0)
 	usage ();
       else
@@ -64,34 +65,37 @@ main (int argc, char **argv)
 	}
     }
 
-  MU_ASSERT (mu_iostream_create (&str, mu_strin, mu_strout));
-
-  
-  MU_ASSERT (mu_imapio_create (&io, str, imapio_mode));
-
-  if (transcript)
-    mu_imapio_trace_enable (io);
-  mu_stream_unref (str);
-
-  while ((rc = mu_imapio_getline (io)) == 0)
+  while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
     {
-      size_t wc;
-      char **wv;
+      char *endp;
+      struct tm tm;
+      struct mu_timezone tz;
 
-      MU_ASSERT (mu_imapio_get_words (io, &wc, &wv));
-      if (wc == 0)
-	break;
+      mu_rtrim_class (buf, MU_CTYPE_ENDLN);
 
-      mu_stream_printf (mu_strerr, "%d\n", wc);
-      for (i = 0; i < wc; i++)
+      rc = mu_scan_datetime (buf, format, &tm, &tz, &endp);
+      if (rc)
 	{
-	  mu_stream_printf (mu_strerr, "%d: '%s'\n", i, wv[i]);
+	  if (*endp)
+	    mu_error ("parse failed near %s", endp);
+	  else
+	    mu_error ("parse failed at end of input");
+	  continue;
 	}
+      if (*endp)
+	mu_printf ("# stopped at %s\n", endp);
+      /* FIXME: add tm_yday? */
+      mu_printf ("sec=%d,min=%d,hour=%d,mday=%d,mon=%d,year=%d,wday=%d,tz=%d\n",
+		 tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday, tm.tm_mon,
+		 tm.tm_year, tm.tm_wday, tz.utc_offset);
+		 
+      //mu_c_streamftime (mu_strout, "%c %z%n", &tm, &tz);
     }
-
+  
   if (rc)
-    mu_error ("mu_imap_getline: %s", mu_strerror (rc));
-
-  mu_imapio_free (io);
+    {
+      mu_error ("%s", mu_strerror (rc));
+      return 1;
+    }
   return 0;
 }
diff --git a/libmailutils/tests/strftime.at b/libmailutils/tests/strftime.at
new file mode 100644
index 0000000..bba6351
--- /dev/null
+++ b/libmailutils/tests/strftime.at
@@ -0,0 +1,1921 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_BANNER(mu_c_streamftime)
+
+m4_pushdef([STRFTIME_TZ],[0])
+
+dnl ------------------------------------------------------------------
+dnl STRFTIME([FMT], [INPUT], [STDOUT = `'], [STDERR = `'])
+dnl
+m4_pushdef([STRFTIME],[
+m4_pushdef([MU_TEST_GROUP],[streamftime])
+m4_pushdef([MU_TEST_KEYWORDS],[strftime strftime-$1])
+m4_pushdef([MU_TEST_COMMAND],[strftime -tz=STRFTIME_TZ -format='$1'])
+MU_GENERIC_TEST([$1],[],[$2],[],[$3],[$4])
+m4_popdef([MU_TEST_COMMAND])
+m4_popdef([MU_TEST_KEYWORDS])
+m4_popdef([MU_TEST_GROUP])
+])
+
+dnl ------------------------------------------------------------
+# Some test data
+
+#    January 1970
+# Su Mo Tu We Th Fr Sa
+#              1  2  3
+#  4  5  6  7  8  9 10
+# 11 12 13 14 15 16 17
+# 18 19 20 21 22 23 24
+# 25 26 27 28 29 30 31
+#
+#  1 0
+#  2 86400
+#  3 172800
+#  4 259200
+#  5 345600
+#  6 432000
+#  7 518400
+#  8 604800
+#  9 691200
+# 10 777600
+# 11 864000
+# 12 950400
+# 13 1036800
+# 14 1123200
+# 15 1209600
+# 16 1296000
+# 17 1382400
+# 18 1468800
+# 19 1555200
+# 20 1641600
+# 21 1728000
+# 22 1814400
+# 23 1900800
+# 24 1987200
+# 25 2073600
+# 26 2160000
+# 27 2246400
+# 28 2332800
+# 29 2419200
+# 30 2505600
+# 31 2592000
+
+#    November 1970
+# Su Mo Tu We Th Fr Sa
+#  1  2  3  4  5  6  7
+#  8  9 10 11 12 13 14
+# 15 16 17 18 19 20 21
+# 22 23 24 25 26 27 28
+# 29 30
+ 
+# 1 26265600
+# 2 26352000
+# 3 26438400
+# 4 26524800
+# 5 26611200
+# 6 26697600
+# 7 26784000
+
+# Months
+# Jan   0
+# Feb   2678400 
+# Mar	5097600 
+# Apr	7776000 
+# May	10368000
+# Jun	13046400
+# Jul	15638400
+# Aug	18316800
+# Sep	20995200
+# Oct	23587200
+# Nov	26265600
+# Dec	28857600
+#
+dnl ------------------------------------------------------------
+# The abbreviated weekday name.
+STRFTIME([%a],
+[26265600
+26352000
+26438400
+26524800
+26611200
+26697600
+26784000],
+[Sun
+Mon
+Tue
+Wed
+Thu
+Fri
+Sat
+])
+
+# The full weekday name.
+STRFTIME([%A],
+[26265600
+26352000
+26438400
+26524800
+26611200
+26697600
+26784000],
+[Sunday
+Monday
+Tuesday
+Wednesday
+Thursday
+Friday
+Saturday
+])
+
+The abbreviated month name.
+STRFTIME([%b],
+[0
+2678400 
+5097600 
+7776000 
+10368000
+13046400
+15638400
+18316800
+20995200
+23587200
+26265600
+28857600],
+[Jan
+Feb
+Mar
+Apr
+May
+Jun
+Jul
+Aug
+Sep
+Oct
+Nov
+Dec
+])
+
+# The abbreviated month name.
+STRFTIME([%h],
+[0
+2678400 
+5097600 
+7776000 
+10368000
+13046400
+15638400
+18316800
+20995200
+23587200
+26265600
+28857600],
+[Jan
+Feb
+Mar
+Apr
+May
+Jun
+Jul
+Aug
+Sep
+Oct
+Nov
+Dec
+])
+
+STRFTIME([%B],
+[0
+2678400 
+5097600 
+7776000 
+10368000
+13046400
+15638400
+18316800
+20995200
+23587200
+26265600
+28857600],
+[January
+February
+March
+April
+May
+June
+July
+August
+September
+October
+November
+December
+])
+
+# The century number (year/100) as a 2-digit integer.
+STRFTIME([%C],
+[26697600
+1322872282],
+[19
+20
+])
+
+# The day of the month as a decimal number (range 01 to 31).
+STRFTIME([%d],
+[26265600
+28771200],
+[01
+30
+])
+
+# Like %d, the day of the month as a decimal number, but a leading
+# zero is replaced by a space.
+STRFTIME([%e],
+[26265600
+28771200],
+[ 1
+30
+])
+
+# A no-op modifier.
+STRFTIME([%EC],
+[26697600
+1322872282],
+[19
+20
+])
+
+# The ISO 8601 year with century as a decimal number.  The 4-digit
+# year corresponding to the ISO week number (see %V).  This has
+# the same format and value as %y, except that if the ISO week
+# number belongs to the previous or next year, that year is used
+# instead.
+
+# Test data:
+#   Date	TS		%V	%G
+# 1972-12-31	94608000	52	1972
+#
+# 1973-01-01	94694400	01	1973
+# 1973-12-01	123552000	48	1973
+# 1973-12-30	126057600	52	1973
+# 1973-12-31	126144000	01	1974
+#
+# 1975-01-01	157766400	01	1975
+# 1975-12-28	188956800	52	1975
+# 1975-12-29	189043200	01	1976
+# 1975-12-31	189216000	01	1976
+#
+# 1977-01-01	220924800	53	1976
+# 1977-01-02	221011200	53	1976
+# 1977-01-03	221097600	01	1977
+# 1977-12-25	251856000	51	1977
+# 1977-12-26	251942400	52	1977
+# 1977-12-31	252374400	52	1977
+
+STRFTIME([%G],
+[;1972
+94608000
+;1973
+94694400
+123552000
+126057600
+126144000
+;1975
+157766400
+188956800
+189043200
+189216000
+;1977
+220924800
+221011200
+221097600
+251856000
+251942400
+252374400],
+[;1972
+1972
+;1973
+1973
+1973
+1973
+1974
+;1975
+1975
+1975
+1976
+1976
+;1977
+1976
+1976
+1977
+1977
+1977
+1977
+])
+
+# Like %G, but without century, that is, with a 2-digit year (00-99).
+STRFTIME([%g],
+[;1972
+94608000
+;1973
+94694400
+123552000
+126057600
+126144000
+;1975
+157766400
+188956800
+189043200
+189216000
+;1977
+220924800
+221011200
+221097600
+251856000
+251942400
+252374400],
+[;1972
+72
+;1973
+73
+73
+73
+74
+;1975
+75
+75
+76
+76
+;1977
+76
+76
+77
+77
+77
+77
+])
+
+# The hour as a decimal number using a 24-hour clock (range 00 to 23).
+STRFTIME([%H],
+[1322870400
+1322874000
+1322877600
+1322881200
+1322884800
+1322888400
+1322892000
+1322895600
+1322899200
+1322902800
+1322906400
+1322910000
+1322913600
+1322917200
+1322920800
+1322924400
+1322928000
+1322931600
+1322935200
+1322938800
+1322942400
+1322946000
+1322949600
+1322953200],
+[00
+01
+02
+03
+04
+05
+06
+07
+08
+09
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+])
+
+# The hour as a decimal number using a 12-hour clock (range 01 to 12). 
+STRFTIME([%I],
+[1322870400
+1322874000
+1322877600
+1322881200
+1322884800
+1322888400
+1322892000
+1322895600
+1322899200
+1322902800
+1322906400
+1322910000
+1322913600
+1322917200
+1322920800
+1322924400
+1322928000
+1322931600
+1322935200
+1322938800
+1322942400
+1322946000
+1322949600
+1322953200],
+[12
+01
+02
+03
+04
+05
+06
+07
+08
+09
+10
+11
+12
+01
+02
+03
+04
+05
+06
+07
+08
+09
+10
+11
+])
+	
+# The day of the year as a decimal number (range 001 to 366).
+STRFTIME([%j],
+[1322872282
+1204329600],
+[337
+061
+])
+
+# The hour (24-hour clock) as a decimal number (range 0 to 23);
+# single digits are preceded by a blank.
+STRFTIME([%k],
+[1322870400
+1322874000
+1322877600
+1322881200
+1322884800
+1322888400
+1322892000
+1322895600
+1322899200
+1322902800
+1322906400
+1322910000
+1322913600
+1322917200
+1322920800
+1322924400
+1322928000
+1322931600
+1322935200
+1322938800
+1322942400
+1322946000
+1322949600
+1322953200],
+[ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+])
+
+# The hour (12-hour clock) as a decimal number (range 1 to 12);
+# single digits are preceded by a blank.
+STRFTIME([%l],
+[1322870400
+1322874000
+1322877600
+1322881200
+1322884800
+1322888400
+1322892000
+1322895600
+1322899200
+1322902800
+1322906400
+1322910000
+1322913600
+1322917200
+1322920800
+1322924400
+1322928000
+1322931600
+1322935200
+1322938800
+1322942400
+1322946000
+1322949600
+1322953200],
+[12
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+])
+
+# The month as a decimal number (range 01 to 12)
+STRFTIME([%m],
+[0
+2678400 
+5097600 
+7776000 
+10368000
+13046400
+15638400
+18316800
+20995200
+23587200
+26265600
+28857600],
+[01
+02
+03
+04
+05
+06
+07
+08
+09
+10
+11
+12
+])
+
+# The minute as a decimal number (range 00 to 59).
+STRFTIME([%M],
+[1322870400
+1322870460
+1322870520
+1322870580
+1322870640
+1322870700
+1322870760
+1322870820
+1322870880
+1322870940
+1322871000
+1322871060
+1322871120
+1322871180
+1322871240
+1322871300
+1322871360
+1322871420
+1322871480
+1322871540
+1322871600
+1322871660
+1322871720
+1322871780
+1322871840
+1322871900
+1322871960
+1322872020
+1322872080
+1322872140
+1322872200
+1322872260
+1322872320
+1322872380
+1322872440
+1322872500
+1322872560
+1322872620
+1322872680
+1322872740
+1322872800
+1322872860
+1322872920
+1322872980
+1322873040
+1322873100
+1322873160
+1322873220
+1322873280
+1322873340
+1322873400
+1322873460
+1322873520
+1322873580
+1322873640
+1322873700
+1322873760
+1322873820
+1322873880
+1322873940],
+[00
+01
+02
+03
+04
+05
+06
+07
+08
+09
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+])
+
+# A newline character
+STRFTIME([<%n>],
+[0],
+[<
+>
+])
+
+# %O - a no-op modifier
+STRFTIME([%OC],
+[26697600
+1322872282],
+[19
+20
+])
+
+# Either "AM" or "PM" according to the given time value.
+# Noon is treated as "PM" and midnight as "AM".
+STRFTIME([%p],
+[1322870400
+1322910000
+1322913600
+1322917200],
+[AM
+AM
+PM
+PM
+])
+
+# Like %p but in lowercase: "am" or "pm".
+STRFTIME([%P],
+[1322870400
+1322910000
+1322913600
+1322917200],
+[am
+am
+pm
+pm
+])
+
+# The number of seconds since the Epoch
+STRFTIME([%s],
+[0
+1322913600],
+[0
+1322913600
+])
+
+# The second as a decimal number (range 00 to 60)
+STRFTIME([%S],
+[1322870400
+1322870401
+1322870402
+1322870403
+1322870404
+1322870405
+1322870406
+1322870407
+1322870408
+1322870409
+1322870410
+1322870411
+1322870412
+1322870413
+1322870414
+1322870415
+1322870416
+1322870417
+1322870418
+1322870419
+1322870420
+1322870421
+1322870422
+1322870423
+1322870424
+1322870425
+1322870426
+1322870427
+1322870428
+1322870429
+1322870430
+1322870431
+1322870432
+1322870433
+1322870434
+1322870435
+1322870436
+1322870437
+1322870438
+1322870439
+1322870440
+1322870441
+1322870442
+1322870443
+1322870444
+1322870445
+1322870446
+1322870447
+1322870448
+1322870449
+1322870450
+1322870451
+1322870452
+1322870453
+1322870454
+1322870455
+1322870456
+1322870457
+1322870458
+1322870459],
+[00
+01
+02
+03
+04
+05
+06
+07
+08
+09
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+])
+
+# A tab character
+STRFTIME([<%t>],
+[0],
+[<	>
+])
+
+# The day of the week as a decimal, range 1 to 7, Monday being 1.
+STRFTIME([%u],
+[26265600
+26352000
+26438400
+26524800
+26611200
+26697600
+26784000],
+[7
+1
+2
+3
+4
+5
+6
+])
+
+# The week number of the current year as a decimal number, range
+# 00 to 53, starting with the first Sunday as the first day of
+# week 01.
+#
+# Test data are days of January of years from 1973 to 1978:
+# Year       Day of Jan 1st	
+# 1973		Mon		
+# 1974		Tue		
+# 1975		Wed		
+# 1976		Thu		
+# 1977		Sat
+# 1978		Sun
+STRFTIME([%U],
+[; 1973 - Monday
+94694400
+94780800
+94867200
+94953600
+95040000
+95126400
+95212800
+95299200
+95385600
+95472000
+95558400
+95644800
+95731200
+95817600
+95904000
+95990400
+96076800
+96163200
+96249600
+96336000
+96422400
+96508800
+96595200
+96681600
+96768000
+96854400
+96940800
+97027200
+97113600
+97200000
+97286400
+; 1974 - Tuesday
+126230400
+126316800
+126403200
+126489600
+126576000
+126662400
+126748800
+126835200
+126921600
+127008000
+127094400
+127180800
+127267200
+127353600
+127440000
+127526400
+127612800
+127699200
+127785600
+127872000
+127958400
+128044800
+128131200
+128217600
+128304000
+128390400
+128476800
+128563200
+128649600
+128736000
+128822400
+; 1975 - Wednesday
+157766400
+157852800
+157939200
+158025600
+158112000
+158198400
+158284800
+158371200
+158457600
+158544000
+158630400
+158716800
+158803200
+158889600
+158976000
+159062400
+159148800
+159235200
+159321600
+159408000
+159494400
+159580800
+159667200
+159753600
+159840000
+159926400
+160012800
+160099200
+160185600
+160272000
+160358400
+; 1976 - Thursday
+189302400
+189388800
+189475200
+189561600
+189648000
+189734400
+189820800
+189907200
+189993600
+190080000
+190166400
+190252800
+190339200
+190425600
+190512000
+190598400
+190684800
+190771200
+190857600
+190944000
+191030400
+191116800
+191203200
+191289600
+191376000
+191462400
+191548800
+191635200
+191721600
+191808000
+191894400
+; 1977 - Saturday
+220924800
+221011200
+221097600
+221184000
+221270400
+221356800
+221443200
+221529600
+221616000
+221702400
+221788800
+221875200
+221961600
+222048000
+222134400
+222220800
+222307200
+222393600
+222480000
+222566400
+222652800
+222739200
+222825600
+222912000
+222998400
+223084800
+223171200
+223257600
+223344000
+223430400
+223516800
+; 1978 - Sunday
+252460800
+252547200
+252633600
+252720000
+252806400
+252892800
+252979200
+253065600
+253152000
+253238400
+253324800
+253411200
+253497600
+253584000
+253670400
+253756800
+253843200
+253929600
+254016000
+254102400
+254188800
+254275200
+254361600
+254448000
+254534400
+254620800
+254707200
+254793600
+254880000
+254966400
+255052800],
+[; 1973 - Monday
+00
+00
+00
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+; 1974 - Tuesday
+00
+00
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+; 1975 - Wednesday
+00
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+; 1976 - Thursday
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+04
+; 1977 - Saturday
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+04
+05
+05
+; 1978 - Sunday
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+04
+05
+05
+05
+])
+
+# The ISO 8601:1988 week number of the current year as a decimal
+# number range 01 to 53, where week 1 is the first week that has
+# at least 4 days in the current year, and with Monday as the
+# first day of the week.
+#
+# Test data:
+#   Date	TS		%V	%G
+# 1972-12-31	94608000	52	1972
+#
+# 1973-01-01	94694400	01	1973
+# 1973-12-01	123552000	48	1973
+# 1973-12-30	126057600	52	1973
+# 1973-12-31	126144000	01	1974
+#
+# 1975-01-01	157766400	01	1975
+# 1975-12-28	188956800	52	1975
+# 1975-12-29	189043200	01	1976
+# 1975-12-31	189216000	01	1976
+#
+# 1977-01-01	220924800	53	1976
+# 1977-01-02	221011200	53	1976
+# 1977-01-03	221097600	01	1977
+# 1977-12-25	251856000	51	1977
+# 1977-12-26	251942400	52	1977
+# 1977-12-31	252374400	52	1977
+STRFTIME([%V],
+[;1972
+94608000
+;1973
+94694400
+123552000
+126057600
+126144000
+;1975
+157766400
+188956800
+189043200
+189216000
+;1977
+220924800
+221011200
+221097600
+251856000
+251942400
+252374400],
+[;1972
+52
+;1973
+01
+48
+52
+01
+;1975
+01
+52
+01
+01
+;1977
+53
+53
+01
+51
+52
+52
+])
+
+# The day of the week as a decimal, range 0 to 6, Sunday being 0.
+STRFTIME([%w],
+[26265600
+26352000
+26438400
+26524800
+26611200
+26697600
+26784000],
+[0
+1
+2
+3
+4
+5
+6
+])
+
+# The week number of the current year as a decimal number, range
+# 00 to 53, starting with the first Monday as the first day of
+# week 01.
+STRFTIME([%W],
+[; 1973 - Monday
+94694400
+94780800
+94867200
+94953600
+95040000
+95126400
+95212800
+95299200
+95385600
+95472000
+95558400
+95644800
+95731200
+95817600
+95904000
+95990400
+96076800
+96163200
+96249600
+96336000
+96422400
+96508800
+96595200
+96681600
+96768000
+96854400
+96940800
+97027200
+97113600
+97200000
+97286400
+; 1974 - Tuesday
+126230400
+126316800
+126403200
+126489600
+126576000
+126662400
+126748800
+126835200
+126921600
+127008000
+127094400
+127180800
+127267200
+127353600
+127440000
+127526400
+127612800
+127699200
+127785600
+127872000
+127958400
+128044800
+128131200
+128217600
+128304000
+128390400
+128476800
+128563200
+128649600
+128736000
+128822400
+; 1975 - Wednesday
+157766400
+157852800
+157939200
+158025600
+158112000
+158198400
+158284800
+158371200
+158457600
+158544000
+158630400
+158716800
+158803200
+158889600
+158976000
+159062400
+159148800
+159235200
+159321600
+159408000
+159494400
+159580800
+159667200
+159753600
+159840000
+159926400
+160012800
+160099200
+160185600
+160272000
+160358400
+; 1976 - Thursday
+189302400
+189388800
+189475200
+189561600
+189648000
+189734400
+189820800
+189907200
+189993600
+190080000
+190166400
+190252800
+190339200
+190425600
+190512000
+190598400
+190684800
+190771200
+190857600
+190944000
+191030400
+191116800
+191203200
+191289600
+191376000
+191462400
+191548800
+191635200
+191721600
+191808000
+191894400
+; 1977 - Saturday
+220924800
+221011200
+221097600
+221184000
+221270400
+221356800
+221443200
+221529600
+221616000
+221702400
+221788800
+221875200
+221961600
+222048000
+222134400
+222220800
+222307200
+222393600
+222480000
+222566400
+222652800
+222739200
+222825600
+222912000
+222998400
+223084800
+223171200
+223257600
+223344000
+223430400
+223516800
+; 1978 - Sunday
+252460800
+252547200
+252633600
+252720000
+252806400
+252892800
+252979200
+253065600
+253152000
+253238400
+253324800
+253411200
+253497600
+253584000
+253670400
+253756800
+253843200
+253929600
+254016000
+254102400
+254188800
+254275200
+254361600
+254448000
+254534400
+254620800
+254707200
+254793600
+254880000
+254966400
+255052800],
+[; 1973 - Monday
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+04
+05
+05
+05
+; 1974 - Tuesday
+00
+00
+00
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+; 1975 - Wednesday
+00
+00
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+; 1976 - Thursday
+00
+00
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+; 1977 - Saturday
+00
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+04
+05
+; 1978 - Sunday
+00
+01
+01
+01
+01
+01
+01
+01
+02
+02
+02
+02
+02
+02
+02
+03
+03
+03
+03
+03
+03
+03
+04
+04
+04
+04
+04
+04
+04
+05
+05
+])
+
+# The year as a decimal number without a century (range 00 to 99).
+STRFTIME([%y],
+[10
+80870400
+1322870453],
+[70
+72
+11
+])
+
+# The year as a decimal number including the century.
+STRFTIME([%Y],
+[10
+80870400
+1322870453],
+[1970
+1972
+2011
+])
+
+# The time-zone as hour offset from GMT, for formatting RFC-822
+# dates (e.g. "%a, %d %b %Y %H:%M:%S %z") */
+m4_define([STRFTIME_TZ],[0200])
+STRFTIME([%z],
+[1322870453],
+[+0200
+])
+
+m4_define([STRFTIME_TZ],[-0200])
+STRFTIME([%z],
+[1322870453],
+[-0200
+])
+m4_define([STRFTIME_TZ],[0])
+
+# A literal '%' character.
+STRFTIME([<%%>],
+[1],
+[<%>
+])
+
+# Any other character after % is reproduced verbatim
+STRFTIME([<%5d>],
+[1],
+[<%5d>
+])
+
+dnl ------------------------------------------------------------------
+STRFTIME([Today is %A, %B %e %Y%n%H:%M:%S (%z).],
+[1322879591],
+[Today is Saturday, December  3 2011
+02:33:11 (+0000).
+])
+
+dnl ------------------------------------------------------------------
+# Equivalent to %a %b %e %H:%M:%S %Y
+STRFTIME([%c],
+[26697600],
+[Fri Nov  6 00:00:00 1970
+])
+
+# Equivalent to %m/%d/%y
+STRFTIME([%D],
+[80870400
+1322872282],
+[07/25/72
+12/03/11
+])
+
+# Equivalent to %Y-%m-%d (the ISO 8601 date format)
+STRFTIME([%F],
+[80870400
+1322872282],
+[1972-07-25
+2011-12-03
+])
+
+# The time in a.m. or p.m. notation, i.e. %I:%M:%S %p. */
+STRFTIME([%r],
+[1322872282],
+[12:31:22 AM
+])
+
+# The time in 24-hour notation (%H:%M)
+STRFTIME([%R],
+[1322872282],
+[00:31
+])
+
+# The time in 24-hour notation (%H:%M:%S)
+STRFTIME([%T],
+[1322872282],
+[00:31:22
+])
+
+# The preferred date representation without the time:
+# equivalent to %D
+STRFTIME([%x],
+[1322872282],
+[12/03/11
+])
+
+# The preferred date representation without the date:
+# equivalent to %H:%M:%S
+STRFTIME([%X],
+[1322872282],
+[00:31:22
+])
+
+dnl ------------------------------------------------------------------
+
+m4_popdef([STRFTIME])
+m4_popdef([STRFTIME_TZ])
diff --git a/libmailutils/tests/imapio.c b/libmailutils/tests/strftime.c
similarity index 50%
copy from libmailutils/tests/imapio.c
copy to libmailutils/tests/strftime.c
index 9783115..5a57666 100644
--- a/libmailutils/tests/imapio.c
+++ b/libmailutils/tests/strftime.c
@@ -1,5 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2007, 2009, 2010, 2011 Free Software
+   Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -16,20 +17,22 @@
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
-#endif 
+#endif
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <mailutils/imapio.h>
-#include <mailutils/errno.h>
 #include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/util.h>
 #include <mailutils/stream.h>
+#include <mailutils/cctype.h>
+#include <mailutils/cstr.h>
 #include <mailutils/stdstream.h>
 
 void
 usage ()
 {
-  mu_stream_printf (mu_strout, "usage: %s [debug=SPEC] [-transcript] [-server]\n",
+  mu_stream_printf (mu_strout, "usage: %s [-format=FMT] [-tz=TZ]\n",
 		    mu_program_name);
   exit (0);
 }
@@ -37,24 +40,39 @@ usage ()
 int
 main (int argc, char **argv)
 {
-  int i, rc;
-  int transcript = 0;
-  mu_imapio_t io;
-  mu_stream_t str;
-  int imapio_mode = MU_IMAPIO_CLIENT;
+  int rc, i;
+  char *format = "%c";
+  char *buf = NULL;
+  size_t size = 0;
+  size_t n;
+  struct mu_timezone tz, *tzp = NULL;
+
+  mu_set_program_name (argv[0]);
   
+  mu_set_program_name (argv[0]);
   mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
-  
+
+  memset (&tz, 0, sizeof tz);
   for (i = 1; i < argc; i++)
     {
       char *opt = argv[i];
 
-      if (strncmp (opt, "debug=", 6) == 0)
-	mu_debug_parse_spec (opt + 6);
-      else if (strcmp (opt, "-transcript") == 0)
-	transcript = 1;
-      else if (strcmp (opt, "-server") == 0)
-	imapio_mode = MU_IMAPIO_SERVER;
+      if (strncmp (opt, "-format=", 8) == 0)
+	format = opt + 8;
+      else if (strncmp (opt, "-tz=", 4) == 0)
+	{
+	  int sign;
+	  int n = atoi (opt + 4);
+	  if (n < 0)
+	    {
+	      sign = -1;
+	      n = - n;
+	    }
+	  else
+	    sign = 1;
+	  tz.utc_offset = sign * ((n / 100 * 60) + n % 100) * 60;
+	  tzp = &tz;
+	}
       else if (strcmp (opt, "-h") == 0)
 	usage ();
       else
@@ -64,34 +82,35 @@ main (int argc, char **argv)
 	}
     }
 
-  MU_ASSERT (mu_iostream_create (&str, mu_strin, mu_strout));
-
-  
-  MU_ASSERT (mu_imapio_create (&io, str, imapio_mode));
-
-  if (transcript)
-    mu_imapio_trace_enable (io);
-  mu_stream_unref (str);
-
-  while ((rc = mu_imapio_getline (io)) == 0)
+  while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
     {
-      size_t wc;
-      char **wv;
+      char *p;
+      struct tm *tm;
+      time_t t;
 
-      MU_ASSERT (mu_imapio_get_words (io, &wc, &wv));
-      if (wc == 0)
-	break;
+      mu_rtrim_class (buf, MU_CTYPE_ENDLN);
 
-      mu_stream_printf (mu_strerr, "%d\n", wc);
-      for (i = 0; i < wc; i++)
+      if (*buf == ';')
 	{
-	  mu_stream_printf (mu_strerr, "%d: '%s'\n", i, wv[i]);
+	  mu_printf ("%s\n", buf);
+	  continue;
+	}
+      t = strtoul (buf, &p, 10);
+      if (*p)
+	{
+	  mu_error ("bad input line near %s", p);
+	  continue;
 	}
-    }
 
+      tm = gmtime (&t);
+      mu_c_streamftime (mu_strout, format, tm, tzp);
+      mu_printf ("\n");
+    }
+  
   if (rc)
-    mu_error ("mu_imap_getline: %s", mu_strerror (rc));
-
-  mu_imapio_free (io);
+    {
+      mu_error ("%s", mu_strerror (rc));
+      return 1;
+    }
   return 0;
 }
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 59366ab..9235686 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -24,7 +24,7 @@ m4_define([MU_TEST_KEYWORDS])
 dnl ------------------------------------------------------------
 m4_define([MU_TEST_COMMAND])
 
-m4_define([mu_trimstr],[m4_if([$2],-1,[$1],[m4_substr($1,0,$2)...])])
+m4_define([mu_trimstr],[m4_if([$2],-1,[$1],[m4_substr([$1],0,$2)...])])
 m4_define([mu_firstline],[mu_trimstr([$1],m4_index([$1],[
 ]))])
 
@@ -43,7 +43,7 @@ dnl RUN-IF-FAIL $7
 dnl RUN-IF-PASS $8
 dnl
 m4_define([MU_GENERIC_TEST],[
-AT_SETUP([m4_if(MU_TEST_GROUP,[],,MU_TEST_GROUP: )m4_if([$1],[],mu_firstline([$3]),[$1])])
+AT_SETUP([m4_if(MU_TEST_GROUP,[],,MU_TEST_GROUP: )m4_if([$1],[],[mu_firstline([$3])],[$1])])
 AT_KEYWORDS([MU_TEST_KEYWORDS $2])
 AT_CHECK([
 AT_DATA([input],[$3
@@ -83,4 +83,7 @@ m4_include([debugspec.at])
 AT_BANNER([IMAP IO])
 m4_include([imapio.at])
 
+m4_include([scantime.at])
+m4_include([strftime.at])
+
 m4_include([fsaf.at])
\ No newline at end of file
diff --git a/libmu_scm/mu_message.c b/libmu_scm/mu_message.c
index 4ee603e..14d8821 100644
--- a/libmu_scm/mu_message.c
+++ b/libmu_scm/mu_message.c
@@ -104,7 +104,7 @@ mu_scm_message_print (SCM message_smob, SCM port, scm_print_state * pstate)
 	scm_puts ("UNKNOWN", port);
       
       if (mu_envelope_sget_date (env, &p) == 0
-          && mu_parse_ctime_date_time (&p, &tm, &tz) == 0)
+          && mu_scan_datetime (p, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
 	{
 	  strftime (datebuf, sizeof (datebuf), "%a %b %e %H:%M", &tm);
 	  buffer = datebuf;
@@ -416,7 +416,7 @@ SCM_DEFINE_PUBLIC (scm_mu_message_get_envelope_date, "mu-message-get-envelope-da
   if (status)
     mu_scm_error (FUNC_NAME, status, "cannot get envelope date",
 		  scm_list_1 (mesg));
-  status = mu_parse_ctime_date_time (&sdate, &tm, &tz);
+  status = mu_scan_datetime (sdate, MU_DATETIME_FROM, &tm, &tz, NULL);
   if (status)
     mu_scm_error (FUNC_NAME, status, "invalid envelope date",
 		  scm_list_1 (scm_from_locale_string (sdate)));
diff --git a/libmu_sieve/actions.c b/libmu_sieve/actions.c
index 56bf2bf..c0a50d1 100644
--- a/libmu_sieve/actions.c
+++ b/libmu_sieve/actions.c
@@ -145,7 +145,6 @@ mime_create_reason (mu_mime_t mime, mu_message_t msg, const char *text)
   char *sender;
   mu_body_t body;
   mu_header_t hdr;
-  char datestr[80];
   static char *content_header =
     "Content-Type: text/plain;charset=" MU_SIEVE_CHARSET "\n"
     "Content-Transfer-Encoding: 8bit\n";
@@ -156,13 +155,14 @@ mime_create_reason (mu_mime_t mime, mu_message_t msg, const char *text)
 
   time (&t);
   tm = localtime (&t);
-  mu_strftime (datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tm);
 
   mu_sieve_get_message_sender (msg, &sender);
 
-  mu_stream_printf (stream, 
-		    "The original message was received at %s from %s.\n",
-		    datestr, sender);
+  mu_c_streamftime (stream,
+		    "The original message was received at "
+		    "%a, %b %d %H:%M:%S %Y %Z", tm, NULL);
+  
+  mu_stream_printf (stream, " from %s.\n", sender);
   free (sender);
   mu_stream_printf (stream,
 		    "Message was refused by recipient's mail filtering program.\n");
@@ -201,7 +201,7 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
 
   mu_message_get_envelope (orig, &env);
   if (mu_envelope_sget_date (env, &p) == 0
-      && mu_parse_ctime_date_time (&p, &tm, &tz) == 0)
+      && mu_scan_datetime (p, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
     t = mu_tm2time (&tm, &tz);
   else
     /* Use local time instead */
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index 1b6a70a..01b4791 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -339,14 +339,13 @@ _date_mapper (union mu_imap_fetch_response *resp,
 	      struct imap_list_element *elt,
 	      struct parse_response_env *parse_env)
 {
-  const char *p;
   struct tm tm;
   struct mu_timezone tz;
   
   if (elt->type != imap_eltype_string)
     return MU_ERR_FAILURE;
-  p = elt->v.string;
-  if (mu_parse_imap_date_time (&p, &tm, &tz))
+  if (mu_scan_datetime (elt->v.string, MU_DATETIME_INTERNALDATE, &tm, &tz,
+			NULL))
     return MU_ERR_FAILURE;
   resp->internaldate.tm = tm;
   resp->internaldate.tz = tz;
@@ -472,10 +471,10 @@ _fill_response (void *item, void *data)
 	rc = MU_ERR_FAILURE;
       else
 	{
-	  char const *p = elt->v.string;
-	  if (mu_parse_imap_date_time (&p,
-				       &env->envelope->date,
-				       &env->envelope->tz))
+	  if (mu_scan_datetime (elt->v.string,
+				MU_DATETIME_IMAP,
+				&env->envelope->date,
+				&env->envelope->tz, NULL))
 	    rc = MU_ERR_FAILURE;
 	  else
 	    rc = 0;
diff --git a/mail/from.c b/mail/from.c
index f3a2912..e4aeda2 100644
--- a/mail/from.c
+++ b/mail/from.c
@@ -213,7 +213,7 @@ hdr_date (struct header_call_args *args, void *data)
       
       mu_message_get_envelope (args->msg, &env);
       if (mu_envelope_sget_date (env, &p) == 0
-          && mu_parse_ctime_date_time (&p, &tm, &tz) == 0)
+          && mu_scan_datetime (p, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
 	strftime (date, sizeof(date), "%a %b %e %H:%M", &tm);
     }
   return header_buf_string (args, date);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 05 15:53:51 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RXfXr-0000rb-89
	for mharc-commit-mailutils@gnu.org; Mon, 05 Dec 2011 15:53:51 -0500
Received: from eggs.gnu.org ([140.186.70.92]:50470)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXfXn-0000pi-JR
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 15:53:49 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXfXl-0008Ue-Jl
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 15:53:47 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:53927)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXfXl-0008Ua-Fz
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 15:53:45 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXfXl-0007nW-05
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 20:53:45 +0000
Date: Mon, 05 Dec 2011 20:53:45 +0000
Message-Id: <E1RXfXl-0007nW-05@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-498-g4a151fd
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 937173596bd77d773c3fc8330a1ebbe2c8c13df6
X-Git-Newrev: 4a151fd82add56d993437bcceb969a7cf8dd3bdf
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 05 Dec 2011 20:53:49 -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=4a151fd82add56d993437bcceb969a7cf8dd3bdf

The branch, master has been updated
       via  4a151fd82add56d993437bcceb969a7cf8dd3bdf (commit)
      from  937173596bd77d773c3fc8330a1ebbe2c8c13df6 (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 4a151fd82add56d993437bcceb969a7cf8dd3bdf
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 5 22:49:37 2011 +0200

    Bugfixes.
    
    * imap4d/util.c (util_parse_internal_date): Use MU_DATETIME_INTERNALDATE
    format.
    * include/mailutils/util.h (MU_DATETIME_IMAP_SEARCH): Rename to
    MU_DATETIME_INTERNALDATE (previous incorrect definition removed).
    (MU_DATETIME_RFC822): New format.
    * libmailutils/address/address.c: Accessors for local_part and
    domain invalidate email field.
    (validate_email): New static function.
    (mu_address_sget_email): Reconstruct email, if necessary.
    (mu_address_format_string): Likewise.
    * libmailutils/base/date.c (mu_c_streamftime): Allow for %$ specifier
    for compatibility with mu_scan_datetime.
    (mu_scan_datetime): Use %$ to idicate optional end of string.
    * libmailutils/tests/scantime.at: Update.
    * libproto/imap/fetch.c (_fill_response): Use MU_DATETIME_RFC822 format.
    * mu/imap.c (fetch_response_printer): Print subject.

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

Summary of changes:
 imap4d/util.c                  |    2 +-
 include/mailutils/util.h       |    5 +-
 libmailutils/address/address.c |  199 ++++++++++++++++++++++++++++++++++------
 libmailutils/base/date.c       |    6 +-
 libmailutils/tests/scantime.at |    2 +-
 libproto/imap/fetch.c          |    2 +-
 mu/imap.c                      |    3 +
 7 files changed, 185 insertions(+), 34 deletions(-)

diff --git a/imap4d/util.c b/imap4d/util.c
index 24b0aec..b494650 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -374,7 +374,7 @@ util_parse_internal_date (char *date, time_t *timep,
   mu_timezone tz;
   time_t time;
 
-  if (mu_scan_datetime (date, MU_DATETIME_IMAP_SEARCH, &tm, &tz, NULL))
+  if (mu_scan_datetime (date, MU_DATETIME_INTERNALDATE, &tm, &tz, NULL))
     return 1;
 
   adjust_tm (&tm, &tz, flag);
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index 88e561a..77d57c8 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -74,8 +74,9 @@ int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
   /* Common datetime formats: */
 #define MU_DATETIME_FROM         "%a %b %e %H:%M:%S %Y"
 #define MU_DATETIME_IMAP         "%d-%b-%Y %H:%M:%S %z"
-#define MU_DATETIME_IMAP_SEARCH  "%d-%b-%Y%? %H:%M:%S %z"
-#define MU_DATETIME_INTERNALDATE "%a, %e %b %Y %H:%M:%S %z"
+#define MU_DATETIME_INTERNALDATE "%d-%b-%Y%$ %H:%M:%S %z"
+  /* FIXME: [%a, ] part is actually optional */
+#define MU_DATETIME_RFC822       "%a, %e %b %Y %H:%M:%S %z"
 
   
   /* ----------------------- */
diff --git a/libmailutils/address/address.c b/libmailutils/address/address.c
index 117d10e..80ddb14 100644
--- a/libmailutils/address/address.c
+++ b/libmailutils/address/address.c
@@ -13,7 +13,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see 
+   Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
@@ -213,11 +213,11 @@ mu_address_concatenate (mu_address_t to, mu_address_t *from)
   return 0;
 }
 
-mu_address_t 
+mu_address_t
 _address_get_nth (mu_address_t addr, size_t no)
 {
   int i;
-  
+
   for (i = 1; addr; addr = addr->next, i++)
     if (i == no)
       break;
@@ -239,13 +239,13 @@ mu_address_get_nth (mu_address_t addr, size_t no, mu_address_t *pret)
 #define AC4(a,b,c,d) a ## b ## c ## d
 #define ACCESSOR(action,field) AC4(mu_address_,action,_,field)
 
-#define DECL_SET(field)							\
+#define DECL_SET(field)						        \
 int									\
 ACCESSOR(set, field) (mu_address_t addr, size_t no, const char *buf)	\
 {									\
   char *s;								\
   mu_address_t subaddr;							\
-  									\
+									\
   if (addr == NULL)							\
     return EINVAL;							\
 									\
@@ -253,22 +253,59 @@ ACCESSOR(set, field) (mu_address_t addr, size_t no, const char *buf)	\
   if (!subaddr)								\
     return MU_ERR_NOENT;						\
 									\
-  s = strdup (buf);							\
-  if (!s)								\
-    return errno;							\
-  									\
+  if (buf)						                \
+    {                                                                   \
+       s = strdup (buf);						\
+       if (!s)								\
+	 return errno;							\
+    }							                \
+  else									\
+    s = (char *) buf;							\
+									\
   free (subaddr->field);						\
   subaddr->field = s;							\
 									\
   return 0;								\
 }
 
+#define DECL_SET_EI(field)					        \
+int									\
+ACCESSOR(set, field) (mu_address_t addr, size_t no, const char *buf)	\
+{									\
+  char *s;								\
+  mu_address_t subaddr;							\
+									\
+  if (addr == NULL)							\
+    return EINVAL;							\
+									\
+  subaddr = _address_get_nth (addr, no);				\
+  if (!subaddr)								\
+    return MU_ERR_NOENT;						\
+									\
+  if (buf)						                \
+    {                                                                   \
+       s = strdup (buf);						\
+       if (!s)								\
+	 return errno;							\
+    }							                \
+  else									\
+    s = (char *) buf;							\
+									\
+  free (subaddr->field);						\
+  subaddr->field = s;							\
+									\
+  free (subaddr->email);						\
+  subaddr->email = NULL;						\
+									\
+  return 0;								\
+}
+
 #define DECL_SGET(field)						\
 int									\
 ACCESSOR(sget,field) (mu_address_t addr, size_t no, char const **sptr)	\
 {									\
   mu_address_t subaddr;							\
-  									\
+									\
   if (addr == NULL)							\
     return EINVAL;							\
 									\
@@ -287,7 +324,7 @@ ACCESSOR(get,field) (mu_address_t addr, size_t no, char *buf, size_t len, \
   size_t i;								  \
   const char *str;							  \
   int status = ACCESSOR(sget, field) (addr, no, &str);			  \
-  									  \
+									  \
   if (status)								  \
     return status;							  \
 									  \
@@ -322,7 +359,13 @@ ACCESSOR(aget, field) (mu_address_t addr, size_t no, char **buf)	\
 DECL_SET(field)					\
 DECL_SGET(field)				\
 DECL_GET(field)					\
-DECL_AGET(field)          
+DECL_AGET(field)
+
+#define DECL_ACCESSORS_EI(field)		\
+DECL_SET_EI(field)				\
+DECL_SGET(field)			        \
+DECL_GET(field)					\
+DECL_AGET(field)
 
 
 
@@ -330,15 +373,115 @@ DECL_AGET(field)
 DECL_ACCESSORS(personal)
 /* Comments */
 DECL_ACCESSORS(comments)
-/* Email */
-DECL_ACCESSORS(email)     
 /* Local part */
-DECL_ACCESSORS(local_part)
+DECL_ACCESSORS_EI(local_part)
 /* Domain */
-DECL_ACCESSORS(domain)
+DECL_ACCESSORS_EI(domain)
 /* Route */
 DECL_ACCESSORS(route)
 
+/* Email */
+int
+mu_address_set_email (mu_address_t addr, size_t no, const char *buf)
+{
+  char *s;
+  mu_address_t subaddr;
+
+  if (addr == NULL)
+    return EINVAL;
+
+  subaddr = _address_get_nth (addr, no);
+  if (!subaddr)
+    return MU_ERR_NOENT;
+
+  if (buf)
+    {
+      s = strdup (buf);
+      if (!s)
+	return errno;
+    }
+  else
+    s = (char *) buf;
+
+  free (subaddr->email);
+  subaddr->email = s;
+
+  free (subaddr->local_part);
+  free (subaddr->domain);
+  if (s)
+    {
+      char *p = strchr (subaddr->email, '@');
+      if (p)
+	{
+	  size_t len = p - subaddr->email;
+	  subaddr->local_part = malloc (len + 1);
+	  if (subaddr->local_part)
+	    {
+	      memcpy (subaddr->local_part, p, len);
+	      subaddr->local_part[len] = 0;
+	    }
+	  subaddr->domain = strdup (p + 1);
+	}
+    }
+  else
+    {
+      subaddr->local_part = NULL;
+      subaddr->domain = NULL;
+    }
+
+  return 0;
+}
+
+static int
+validate_email (mu_address_t subaddr)
+{
+  if (!subaddr->email)
+    {
+      if (subaddr->local_part)
+	{
+	  const char *domain;
+
+	  if (subaddr->domain)
+	    domain = subaddr->domain;
+	  else
+	    mu_get_user_email_domain (&domain);
+	  if (domain)
+	    {
+	      char *p;
+	      subaddr->email = malloc (strlen (subaddr->local_part) +
+				       strlen (domain) + 2);
+	      if (!subaddr->email)
+		return ENOMEM;
+	      p = mu_stpcpy (subaddr->email, subaddr->local_part);
+	      *p++ = '@';
+	      mu_stpcpy (p, (char*) domain);
+	    }
+	}
+    }
+  return 0;
+}
+
+int
+mu_address_sget_email (mu_address_t addr, size_t no, char const **sptr)
+{
+  mu_address_t subaddr;
+
+  if (addr == NULL)
+    return EINVAL;
+
+  subaddr = _address_get_nth (addr, no);
+  if (!subaddr)
+    return MU_ERR_NOENT;
+
+  validate_email (subaddr);
+  *sptr = subaddr->email;
+  return 0;
+}
+
+DECL_GET(email)
+DECL_AGET(email)
+
+
 
 
 #define format_char(c) do {\
@@ -349,7 +492,7 @@ DECL_ACCESSORS(route)
    }\
  else\
    rc++;\
-} while(0) 
+} while(0)
 
 #define format_string(str) do {\
  if (buflen) \
@@ -361,22 +504,23 @@ DECL_ACCESSORS(route)
  else\
    rc += strlen (str);\
 } while (0)
-     
+
 size_t
 mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
 {
   int rc = 0;
   int comma = 0;
-  
+
   for (;addr; addr = addr->next)
     {
+      validate_email (addr);
       if (addr->email)
 	{
 	  int space = 0;
 
 	  if (comma)
 	    format_char (',');
-	  
+
 	  if (addr->personal)
 	    {
 	      format_char ('"');
@@ -384,7 +528,7 @@ mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
 	      format_char ('"');
 	      space++;
 	    }
-	  
+
 	  if (addr->comments)
 	    {
 	      if (space)
@@ -394,7 +538,7 @@ mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
 	      format_char (')');
 	      space++;
 	    }
-	  
+
 	  if (space)
 	    format_char (' ');
 	  format_char ('<');
@@ -435,14 +579,14 @@ int
 mu_address_is_group (mu_address_t addr, size_t no, int *yes)
 {
   mu_address_t subaddr;
-  
+
   if (addr == NULL)
     return EINVAL;
 
   subaddr = _address_get_nth (addr, no);
   if (!subaddr)
     return MU_ERR_NOENT;
-  
+
   if (yes)
     *yes = _address_is_group (subaddr);
   return 0;
@@ -465,7 +609,7 @@ mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t *n)
 	return ENOMEM;
       mu_address_format_string (addr, addr->addr, i+1);
     }
-      
+
   i = mu_cpystr (buf, addr->addr, len);
   if (n)
     *n = i;
@@ -562,12 +706,12 @@ mu_address_dup (mu_address_t src)
 
   return dst;
 }
-  
+
 int
 mu_address_union (mu_address_t *a, mu_address_t b)
 {
   mu_address_t last = NULL;
-    
+
   if (!a || !b)
     return EINVAL;
 
@@ -601,4 +745,3 @@ mu_address_union (mu_address_t *a, mu_address_t b)
       }
   return 0;
 }
-  
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
index f784bea..5c52ecd 100644
--- a/libmailutils/base/date.c
+++ b/libmailutils/base/date.c
@@ -515,6 +515,10 @@ mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *input_tm,
 	  /* A literal '%' character. */
 	  rc = mu_stream_write (str, "%", 1, NULL);
 	  break;
+
+	case '$':
+	  /* Ignored for compatibilty with mu_scan_datetime */
+	  break;
 	  
 	case '+':
 	  /* Not supported (date and time in date(1) format. */
@@ -849,7 +853,7 @@ mu_scan_datetime (const char *input, const char *fmt,
 		rc = MU_ERR_PARSE;
 	      break;
 	      
-	    case '?':
+	    case '$':
 	      eof_ok = 1;
 	      break;
 	    }
diff --git a/libmailutils/tests/scantime.at b/libmailutils/tests/scantime.at
index e4d7e2d..43611f8 100644
--- a/libmailutils/tests/scantime.at
+++ b/libmailutils/tests/scantime.at
@@ -47,7 +47,7 @@ sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,tz=3600
 ])
 
 SCANTIME([IMAP search time format],[imap-search],
-[%d-%b-%Y%? %H:%M:%S %z],
+[%d-%b-%Y%$ %H:%M:%S %z],
 [03-May-2011 13:25:26 +0100
 03-May-2011],
 [sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=3600
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index 01b4791..5fa3f5c 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -472,7 +472,7 @@ _fill_response (void *item, void *data)
       else
 	{
 	  if (mu_scan_datetime (elt->v.string,
-				MU_DATETIME_IMAP,
+				MU_DATETIME_RFC822,
 				&env->envelope->date,
 				&env->envelope->tz, NULL))
 	    rc = MU_ERR_FAILURE;
diff --git a/mu/imap.c b/mu/imap.c
index 54ebca7..8132b27 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -266,6 +266,9 @@ fetch_response_printer (void *item, void *data)
 	mu_stream_printf (str, "ENVELOPE:\n");
 	
 	format_date (str, "date", &resp->envelope.date, &resp->envelope.tz);
+	mu_stream_printf (str, "  subject = %s\n",
+			  resp->envelope.subject ?
+			   resp->envelope.subject : "NIL");
 
 	format_email (str, "from", resp->envelope.from);
 	format_email (str, "sender", resp->envelope.sender);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 05 17:34:52 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RXh7c-00021C-EZ
	for mharc-commit-mailutils@gnu.org; Mon, 05 Dec 2011 17:34:52 -0500
Received: from eggs.gnu.org ([140.186.70.92]:34458)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXh7Z-00020l-8F
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 17:34:50 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXh7X-00032U-O1
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 17:34:49 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:52852)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXh7X-00032Q-Is
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 17:34:47 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXh7W-0001pY-FN
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 22:34:46 +0000
Date: Mon, 05 Dec 2011 22:34:46 +0000
Message-Id: <E1RXh7W-0001pY-FN@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-499-gf606ab2
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 4a151fd82add56d993437bcceb969a7cf8dd3bdf
X-Git-Newrev: f606ab21eafbd947113daae4c41328118d51a0eb
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 05 Dec 2011 22:34:50 -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=f606ab21eafbd947113daae4c41328118d51a0eb

The branch, master has been updated
       via  f606ab21eafbd947113daae4c41328118d51a0eb (commit)
      from  4a151fd82add56d993437bcceb969a7cf8dd3bdf (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 f606ab21eafbd947113daae4c41328118d51a0eb
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 6 00:20:30 2011 +0200

    Implement LIFO access functions for lists.
    
    * include/mailutils/list.h (mu_list_push)
    (mu_list_pop): New functions.
    * libmailutils/list/pop.c: New file.
    * libmailutils/list/push.c: New file.
    * libmailutils/list/Makefile.am (liblist_la_SOURCES): Add new files.
    * libmailutils/tests/list.at: Test LIFO access.
    * libmailutils/tests/listop.c: Implement push & pop.

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

Summary of changes:
 include/mailutils/list.h                   |    7 ++++
 libmailutils/list/Makefile.am              |    2 +
 libmailutils/list/{head.c => pop.c}        |   22 +++++++++++---
 libmailutils/{url/urlinv.c => list/push.c} |   12 ++-----
 libmailutils/tests/list.at                 |   25 ++++++++++++++++
 libmailutils/tests/listop.c                |   42 ++++++++++++++++++++++++++++
 6 files changed, 97 insertions(+), 13 deletions(-)
 copy libmailutils/list/{head.c => pop.c} (73%)
 copy libmailutils/{url/urlinv.c => list/push.c} (83%)

diff --git a/include/mailutils/list.h b/include/mailutils/list.h
index 60f0031..7934aa2 100644
--- a/include/mailutils/list.h
+++ b/include/mailutils/list.h
@@ -124,6 +124,13 @@ int mu_list_replace  (mu_list_t _list, void *_old_item, void *_new_item);
   /* A non-destructive version of mu_list_replace: the removed item is not
      deallocated. */
 int mu_list_replace_nd (mu_list_t _list, void *_old_item, void *_new_item);
+
+  /* ************************************************* */
+  /* LIFO Access                                       */
+  /* ************************************************* */
+int mu_list_push (mu_list_t list, void *item);
+int mu_list_pop (mu_list_t list, void **item);
+  
 
   /* ************************************************* */
   /* Interation over lists.                            */
diff --git a/libmailutils/list/Makefile.am b/libmailutils/list/Makefile.am
index eb6084a..21a600b 100644
--- a/libmailutils/list/Makefile.am
+++ b/libmailutils/list/Makefile.am
@@ -37,7 +37,9 @@ liblist_la_SOURCES = \
  listlist.c\
  locate.c\
  map.c\
+ pop.c\
  prepend.c\
+ push.c\
  remove.c\
  replace.c\
  rfold.c\
diff --git a/libmailutils/list/head.c b/libmailutils/list/pop.c
similarity index 73%
copy from libmailutils/list/head.c
copy to libmailutils/list/pop.c
index 7c52b89..9c1af0f 100644
--- a/libmailutils/list/head.c
+++ b/libmailutils/list/pop.c
@@ -23,14 +23,26 @@
 #include <mailutils/errno.h>
 
 int
-mu_list_head (mu_list_t list, void **pitem)
+mu_list_pop (mu_list_t list, void **item)
 {
+  struct list_data *last, *prev;
+  
   if (list == NULL)
     return EINVAL;
-  if (pitem == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (!list->head.next)
+
+  if (list->count == 0)
     return MU_ERR_NOENT;
-  *pitem = list->head.next->item;
+
+  last = list->head.prev;
+  prev = last->prev;
+  mu_iterator_advance (list->itr, last);
+  prev->next = last->next;
+  prev->next->prev = prev;
+  if (item)
+    *item = last->item;
+  else
+    DESTROY_ITEM (list, last);
+  free (last);
+  list->count--;
   return 0;
 }
diff --git a/libmailutils/url/urlinv.c b/libmailutils/list/push.c
similarity index 83%
copy from libmailutils/url/urlinv.c
copy to libmailutils/list/push.c
index c4f8113..c6bead7 100644
--- a/libmailutils/url/urlinv.c
+++ b/libmailutils/list/push.c
@@ -19,15 +19,11 @@
 # include <config.h>
 #endif
 #include <stdlib.h>
-#include <errno.h>
-#include <mailutils/sys/url.h>
+#include <mailutils/sys/list.h>
+#include <mailutils/errno.h>
 
 int
-mu_url_invalidate (mu_url_t url)
+mu_list_push (mu_list_t list, void *item)
 {
-  if (!url)
-    return EINVAL;
-  free (url->name);
-  url->name = NULL;
-  return 0;
+  return mu_list_append (list, item);
 }
diff --git a/libmailutils/tests/list.at b/libmailutils/tests/list.at
index d49ab43..e909fef 100644
--- a/libmailutils/tests/list.at
+++ b/libmailutils/tests/list.at
@@ -97,6 +97,31 @@ tre
 fem
 ])
 
+TESTLIST([push],[],
+[push en to tre
+print],
+[# items: 3
+en
+to
+tre
+])
+
+TESTLIST([pop],[],
+[push en to tre
+pop
+pop
+pop],
+[tre
+to
+en
+])
+
+TESTLIST([pop-null],[],
+[pop],
+[],
+[mu_list_pop: Requested item not found
+])
+
 TESTLIST([get],[],
 [add en to tre fire fem
 3],
diff --git a/libmailutils/tests/listop.c b/libmailutils/tests/listop.c
index 00d3949..8287239 100644
--- a/libmailutils/tests/listop.c
+++ b/libmailutils/tests/listop.c
@@ -781,6 +781,42 @@ sort (mu_list_t list)
 }
 
 void
+push (mu_list_t list, int argc, char **argv)
+{
+  if (argc < 2)
+    {
+      fprintf (stderr, "push arg [arg] ?\n");
+      return;
+    }
+
+  while (--argc)
+    {
+      int rc = mu_list_push (list, strdup (*++argv));
+      if (rc)
+	fprintf (stderr, "mu_list_push: %s\n", mu_strerror (rc));
+    }
+}
+
+void
+pop (mu_list_t list, int argc, char **argv)
+{
+  char *text;
+  int rc;
+  
+  if (argc != 1)
+    {
+      fprintf (stderr, "pop ?\n");
+      return;
+    }
+
+  rc = mu_list_pop (list, (void**) &text);
+  if (rc)
+    fprintf (stderr, "mu_list_pop: %s\n", mu_strerror (rc));
+  else
+    printf ("%s\n", text);
+}
+  
+void
 help ()
 {
   printf ("count\n");
@@ -808,6 +844,8 @@ help ()
   printf ("help\n");
   printf ("head\n");
   printf ("tail\n");
+  printf ("push item\n");
+  printf ("pop\n");
   printf ("sort\n");
   printf ("NUMBER\n");
 }
@@ -928,6 +966,10 @@ shell (mu_list_t list)
 	    head (ws.ws_wordc, list);
 	  else if (strcmp (ws.ws_wordv[0], "tail") == 0)
 	    tail (ws.ws_wordc, list);
+	  else if (strcmp (ws.ws_wordv[0], "push") == 0)
+	    push (list, ws.ws_wordc, ws.ws_wordv);
+	  else if (strcmp (ws.ws_wordv[0], "pop") == 0)
+	    pop (list, ws.ws_wordc, ws.ws_wordv);
 	  else if (strcmp (ws.ws_wordv[0], "sort") == 0)
 	    {
 	      int i;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 05 18:31:41 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RXi0b-00068n-Dp
	for mharc-commit-mailutils@gnu.org; Mon, 05 Dec 2011 18:31:41 -0500
Received: from eggs.gnu.org ([140.186.70.92]:55676)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXi0Y-00068X-Ks
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 18:31:39 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXi0X-0004MP-60
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 18:31:38 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:48946)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXi0X-0004ML-3E
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 18:31:37 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RXi0W-0007QR-Ei
	for commit-mailutils@gnu.org; Mon, 05 Dec 2011 23:31:36 +0000
Date: Mon, 05 Dec 2011 23:31:36 +0000
Message-Id: <E1RXi0W-0007QR-Ei@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-500-g561a427
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: f606ab21eafbd947113daae4c41328118d51a0eb
X-Git-Newrev: 561a4274464e2e35ce5f696ae6fb9c6431032db1
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 05 Dec 2011 23:31:39 -0000

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

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

The branch, master has been updated
       via  561a4274464e2e35ce5f696ae6fb9c6431032db1 (commit)
      from  f606ab21eafbd947113daae4c41328118d51a0eb (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 561a4274464e2e35ce5f696ae6fb9c6431032db1
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 6 00:36:56 2011 +0200

    Use list LIFO access where appropriate.
    
    * libmu_sieve/runtime.c (_mu_sv_instr_push)
    (_mu_sv_instr_pop): Use mu_list_push and mu_list_pop.
    * mail/alias.c (recursive_alias_expand): Likewise.

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

Summary of changes:
 libmu_sieve/runtime.c |    5 ++---
 mail/alias.c          |    4 ++--
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/libmu_sieve/runtime.c b/libmu_sieve/runtime.c
index 79e625c..6863df8 100644
--- a/libmu_sieve/runtime.c
+++ b/libmu_sieve/runtime.c
@@ -131,7 +131,7 @@ _mu_sv_instr_push (mu_sieve_machine_t mach)
       mu_sieve_error (mach, _("cannot create stack"));
       mu_sieve_abort (mach);
     }
-  mu_list_prepend (mach->stack, (void*) mach->reg);
+  mu_list_push (mach->stack, (void*) mach->reg);
 }
 
 void
@@ -149,8 +149,7 @@ _mu_sv_instr_pop (mu_sieve_machine_t mach)
       mu_sieve_error (mach, _("stack underflow"));
       mu_sieve_abort (mach);
     }
-  mu_list_get (mach->stack, 0, (void **)&mach->reg);
-  mu_list_remove (mach->stack, (void *)mach->reg);
+  mu_list_pop (mach->stack, (void **)&mach->reg);
 }
 
 void
diff --git a/mail/alias.c b/mail/alias.c
index 16e8e8a..beb294b 100644
--- a/mail/alias.c
+++ b/mail/alias.c
@@ -131,9 +131,9 @@ recursive_alias_expand (const char *name, mu_list_t exlist, mu_list_t origlist)
       mu_iterator_current (itr, (void **)&word);
       if (mu_list_locate (origlist, word, NULL) == MU_ERR_NOENT)
 	{
-	  mu_list_prepend (origlist, word);
+	  mu_list_push (origlist, word);
 	  recursive_alias_expand (word, exlist, origlist);
-	  mu_list_remove (origlist, word);
+	  mu_list_pop (origlist, NULL);
 	}
     }
   mu_iterator_destroy (&itr);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 06 14:33:00 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RY0lA-0001AM-C8
	for mharc-commit-mailutils@gnu.org; Tue, 06 Dec 2011 14:33:00 -0500
Received: from eggs.gnu.org ([140.186.70.92]:58698)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RY0l6-00019T-E8
	for commit-mailutils@gnu.org; Tue, 06 Dec 2011 14:32:58 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RY0l3-0001dr-ON
	for commit-mailutils@gnu.org; Tue, 06 Dec 2011 14:32:56 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:43014)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RY0l3-0001dn-MT
	for commit-mailutils@gnu.org; Tue, 06 Dec 2011 14:32:53 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RY0l3-0006Vv-AX
	for commit-mailutils@gnu.org; Tue, 06 Dec 2011 19:32:53 +0000
Date: Tue, 06 Dec 2011 19:32:53 +0000
Message-Id: <E1RY0l3-0006Vv-AX@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-501-gea8772c
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 561a4274464e2e35ce5f696ae6fb9c6431032db1
X-Git-Newrev: ea8772cd3bb401e2a3e2f992a1ae570d3b99abdb
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 06 Dec 2011 19:32:58 -0000

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

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

The branch, master has been updated
       via  ea8772cd3bb401e2a3e2f992a1ae570d3b99abdb (commit)
      from  561a4274464e2e35ce5f696ae6fb9c6431032db1 (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 ea8772cd3bb401e2a3e2f992a1ae570d3b99abdb
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 6 13:35:54 2011 +0200

    Improve mu_scan_datetime
    
    * libmailutils/base/date.c (mu_scan_datetime): Handle optional
    blocks (%[ %| %], alternatives %( %| %), "any character"
    wildcards (%?) and strict character matches (%\C).  Return
    MU_ERR_FORMAT on errors in format string.
    Compute tm->tm_yday.
    * libmailutils/diag/errors (MU_ERR_FORMAT): New error code.
    * libmailutils/tests/scantime.c: Print yday.  Print input line with
    diagnostic messages.
    * libmailutils/tests/scantime.at: Add more tests.
    * include/mailutils/util.h (MU_DATETIME_RFC822): Split into two
    formats: MU_DATETIME_FORM_RFC822, for output formatting, and
    MU_DATETIME_SCAN_RFC822 for input scanning.  All uses changed.

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

Summary of changes:
 include/mailutils/util.h       |    7 +-
 libmailutils/base/date.c       |  308 +++++++++++++++++++++++++++++++++++++++-
 libmailutils/diag/errors       |    2 +
 libmailutils/tests/scantime.at |  115 ++++++++++++++--
 libmailutils/tests/scantime.c  |   22 ++--
 libproto/imap/fetch.c          |    2 +-
 6 files changed, 426 insertions(+), 30 deletions(-)

diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index 77d57c8..e6f4b89 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -75,9 +75,12 @@ int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
 #define MU_DATETIME_FROM         "%a %b %e %H:%M:%S %Y"
 #define MU_DATETIME_IMAP         "%d-%b-%Y %H:%M:%S %z"
 #define MU_DATETIME_INTERNALDATE "%d-%b-%Y%$ %H:%M:%S %z"
-  /* FIXME: [%a, ] part is actually optional */
-#define MU_DATETIME_RFC822       "%a, %e %b %Y %H:%M:%S %z"
 
+  /* RFC2822 date.  Scan format contains considerable allowances which would
+     stun formatting functions, therefore two distinct formats are provided:
+     one for outputting and one for scanning: */
+#define MU_DATETIME_FORM_RFC822  "%a, %e %b %Y %H:%M:%S %z"  
+#define MU_DATETIME_SCAN_RFC822  "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
   
   /* ----------------------- */
   /* File & path names.      */
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
index 5c52ecd..543efab 100644
--- a/libmailutils/base/date.c
+++ b/libmailutils/base/date.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <mailutils/diag.h>
 #include <mailutils/util.h>
 #include <mailutils/stream.h>
 #include <mailutils/errno.h>
@@ -53,8 +54,7 @@ static int  month_start[]=
          31  28  31   30   31   30   31   31   30   31   30   31
      */
 
-/* NOTE: ignore GCC warning. The precedence of operators is OK here */
-#define leap_year(y) ((y) % 4 == 0 && (y) % 100 != 0 || (y) % 400 == 0)
+#define leap_year(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
 
 static int
 dayofyear (int year, int month, int day)
@@ -625,6 +625,203 @@ get_num (const char *str, char **endp, int ndig, int minval, int maxval,
 #define DT_MIN   0x20
 #define DT_SEC   0x40
 
+#define ST_NON   -1
+#define ST_OPT   0
+#define ST_ALT   1
+
+struct save_input
+{
+  int state;
+  const char *input;
+};
+
+static int
+push_input (mu_list_t *plist, int state, const char *input)
+{
+  mu_list_t list = *plist;
+  struct save_input *inp = malloc (sizeof (*inp));
+  if (!inp)
+    return ENOMEM;
+  if (!list)
+    {
+      int rc = mu_list_create (&list);
+      if (rc)
+	{
+	  free (inp);
+	  return rc;
+	}
+      mu_list_set_destroy_item (list, mu_list_free_item);
+      *plist = list;
+    }
+  inp->state = state;
+  inp->input = input;
+  return mu_list_push (list, (void*)inp);
+}
+
+static int
+peek_state (mu_list_t list, int *state, const char **input)
+{
+  int rc;
+  struct save_input *inp;
+
+  rc = mu_list_tail (list, (void**)&inp);
+  if (rc)
+    return rc;
+  *state = inp->state;
+  if (input)
+    *input = inp->input;
+  return 0;
+}      
+
+static int
+change_top_input (mu_list_t list, const char *input)
+{
+  int rc;
+  struct save_input *inp;
+
+  rc = mu_list_tail (list, (void**)&inp);
+  if (rc)
+    return rc;
+  inp->input = input;
+  return 0;
+}
+
+static int
+pop_input (mu_list_t list, int *state, const char **input)
+{
+  int rc;
+  struct save_input *inp;
+
+  rc = mu_list_pop (list, (void**)&inp);
+  if (rc)
+    return rc;
+  *state = inp->state;
+  if (input)
+    *input = inp->input;
+  return 0;
+}
+
+static int
+bracket_to_state (int c)
+{
+  switch (c)
+    {
+    case '[':
+    case ']':
+      return ST_OPT;
+    case '(':
+    case ')':
+      return ST_ALT;
+    }
+  return ST_NON;
+}
+
+static int
+state_to_closing_bracket (int st)
+{
+  switch (st)
+    {
+    case ST_OPT:
+      return ']';
+    case ST_ALT:
+      return ')';
+    }
+  return '?';
+}
+
+static int
+scan_recovery (const char *fmt, mu_list_t *plist, int skip_alt,
+	       const char **endp,
+	       const char **input)
+{
+  int c, rc = 0;
+  int nesting_level = 1;
+  int st;
+  const char *p;
+  
+  while (*fmt)
+    {
+      c = *fmt++;
+      
+      if (c == '%')
+	{
+	  c = *fmt++;
+	  if (!c)
+	    {
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			("%s:%d: error in format: %% at the end of input",
+			 __FILE__, __LINE__));
+	      rc = MU_ERR_FORMAT;
+	      break;
+	    }
+	      
+	  switch (c)
+	    {
+	    case '[':
+	    case '(':
+	      nesting_level++;
+	      rc = push_input (plist, bracket_to_state (c), NULL);
+	      break;
+	      
+	    case ')':
+	    case ']':
+	      rc = pop_input (*plist, &st, &p);
+	      if (rc || st != bracket_to_state (c))
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: %%%c out of context",
+			     __FILE__, __LINE__, c));
+		  rc = MU_ERR_FORMAT;
+		  break;
+		}
+	      if (--nesting_level == 0)
+		{
+		  *endp = fmt;
+		  if (skip_alt)
+		    return 0;
+		  *input = p;
+		  if (st == ST_ALT)
+		    {
+		      if (*fmt == '%' && (fmt[1] == '|' || fmt[1] == ']'))
+			return 0;
+		      return MU_ERR_PARSE; /* No match found */
+		    }
+		  return 0;
+		}
+	      break;
+
+	    case '|':
+	      if (skip_alt)
+		continue;
+	      if (nesting_level == 1)
+		{
+		  *endp = fmt;
+		  return peek_state (*plist, &st, input);
+		}
+	      break;
+
+	    case '\\':
+	      if (*++fmt == 0)
+		{
+		  peek_state (*plist, &st, NULL);
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: missing closing %%%c",
+			     __FILE__, __LINE__,
+			     state_to_closing_bracket (st)));
+		  return MU_ERR_FORMAT;		  
+		}
+	    }
+	}
+    }
+  
+  peek_state (*plist, &st, NULL);
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+	    ("%s:%d: error in format: missing closing %%%c",
+	     __FILE__, __LINE__,
+	     state_to_closing_bracket (st)));
+  return MU_ERR_FORMAT;		  
+}
+
 int
 mu_scan_datetime (const char *input, const char *fmt,
 		  struct tm *tm, struct mu_timezone *tz, char **endp)
@@ -632,8 +829,12 @@ mu_scan_datetime (const char *input, const char *fmt,
   int rc = 0;
   char *p;
   int n;
+  int c;
+  int st;
+  int recovery = 0;
   int eof_ok = 0;
   int datetime_parts = 0;
+  mu_list_t save_input_list = NULL;
   
   memset (tm, 0, sizeof *tm);
 #ifdef HAVE_STRUCT_TM_TM_ISDST
@@ -661,7 +862,17 @@ mu_scan_datetime (const char *input, const char *fmt,
       
       if (*fmt == '%')
 	{
-	  switch (*++fmt)
+	  c = *++fmt;
+	  if (!c)
+	    {
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			("%s:%d: error in format: %% at the end of input",
+			 __FILE__, __LINE__));
+	      rc = MU_ERR_FORMAT;
+	      break;
+	    }
+	  
+	  switch (c)
 	    {
 	    case 'a':
 	      /* The abbreviated weekday name. */
@@ -852,27 +1063,110 @@ mu_scan_datetime (const char *input, const char *fmt,
 	      else
 		rc = MU_ERR_PARSE;
 	      break;
+
+	      rc = push_input (&save_input_list, ST_ALT, (void*)input);
+	      break;
+	      
+	    case '(':
+	    case '[':
+	      rc = push_input (&save_input_list, bracket_to_state (c),
+			       (void*)input);
+	      break;
+
+	    case ')':
+	    case ']':
+	      if (pop_input (save_input_list, &st, NULL))
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: unbalanced %%%c near %s",
+			     __FILE__, __LINE__, c, fmt));
+		  rc = MU_ERR_FORMAT;
+		}
+	      else if (st != bracket_to_state (c))
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: %%%c out of context",
+			     __FILE__, __LINE__, c));
+		  rc = MU_ERR_FORMAT;
+		}
+	      break;
+
+	    case '|':
+	      rc = scan_recovery (fmt, &save_input_list, 1, &fmt, NULL);
+	      if (rc == 0)
+		fmt--;
+	      break;
 	      
 	    case '$':
 	      eof_ok = 1;
 	      break;
+
+	    case '\\':
+	      c = *++fmt;
+	      if (!c)
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: %% at the end of input",
+			     __FILE__, __LINE__));
+		  rc = MU_ERR_FORMAT;
+		}
+	      else if (c == *input)
+		input++;
+	      else
+		rc = MU_ERR_PARSE;
+	      break;
+
+	    case '?':
+	      input++;
+	      break;
+	      
+	    default:
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			("%s:%d: error in format: unrecognized conversion type"
+			 " near %s",
+			 __FILE__, __LINE__, fmt));
+	      rc = MU_ERR_FORMAT;
+	      break;
 	    }
+
 	  if (eof_ok && rc == 0 && *input == 0)
 	    break;
 	}
-      else if (*input != *fmt)
+      else if (!recovery && *input != *fmt)
 	rc = MU_ERR_PARSE;
       else
 	input++;
+
+      if (rc == MU_ERR_PARSE && !mu_list_is_empty (save_input_list))
+	{
+	  rc = scan_recovery (fmt, &save_input_list, 0, &fmt, &input);
+	  if (rc == 0)
+	    --fmt;
+	}
     }
 
+  if (!mu_list_is_empty (save_input_list))
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		("%s:%d: error in format: closing bracket missing",
+		 __FILE__, __LINE__));
+      rc = MU_ERR_FORMAT;
+    }
+  mu_list_destroy (&save_input_list);
+  
+  if (rc == 0 && recovery)
+    rc = MU_ERR_PARSE;
+  
   if (!eof_ok && rc == 0 && *input == 0 && *fmt)
     rc = MU_ERR_PARSE;
 
-  if (!(datetime_parts & DT_WDAY) &&
-      (datetime_parts & (DT_YEAR|DT_MONTH|DT_MDAY)) ==
+  if ((datetime_parts & (DT_YEAR|DT_MONTH|DT_MDAY)) ==
       (DT_YEAR|DT_MONTH|DT_MDAY))
-    tm->tm_wday = dayofweek (tm->tm_year + 1900, tm->tm_mon, tm->tm_mday);
+    {
+      if (!(datetime_parts & DT_WDAY))
+	tm->tm_wday = dayofweek (tm->tm_year + 1900, tm->tm_mon, tm->tm_mday);
+      tm->tm_yday = dayofyear (tm->tm_year + 1900, tm->tm_mon, tm->tm_mday);
+    }
   
   if (endp)
     *endp = (char*) input;
diff --git a/libmailutils/diag/errors b/libmailutils/diag/errors
index 6e54fad..f919549 100644
--- a/libmailutils/diag/errors
+++ b/libmailutils/diag/errors
@@ -123,3 +123,5 @@ MU_ERR_PERM_DIR_IWGRP       _("File in group writable directory")
 MU_ERR_PERM_DIR_IWOTH       _("File in world writable directory")
 
 MU_ERR_DISABLED             _("Requested feature disabled in configuration")
+
+MU_ERR_FORMAT		    _("Error in format string")
diff --git a/libmailutils/tests/scantime.at b/libmailutils/tests/scantime.at
index 43611f8..2db8540 100644
--- a/libmailutils/tests/scantime.at
+++ b/libmailutils/tests/scantime.at
@@ -30,34 +30,127 @@ m4_popdef([MU_TEST_GROUP])
 ])
 dnl ---------------------------------------------------------------------
 
-SCANTIME([RFC-822 time],[rfc822],
+SCANTIME([Envelope (From) time],[envelope],
 [%a %b %e %H:%M:%S %Y],
 [Tue May  3 13:25:26 2011
 Fri Nov 11 11:55:01 2011],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=0
-sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,tz=0
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=0
+sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,yday=315,tz=0
 ])
 
-SCANTIME([IMAP time format],[imap],
+SCANTIME([IMAP INTERNALDATE],[imap-internaldate],
 [%d-%b-%Y %H:%M:%S %z],
 [03-May-2011 13:25:26 +0100
 11-Nov-2011 11:55:01 +0100],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=3600
-sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,tz=3600
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,yday=315,tz=3600
 ])
 
-SCANTIME([IMAP search time format],[imap-search],
+SCANTIME([IMAP INTERNALDATE (optional time)],[imap-search],
 [%d-%b-%Y%$ %H:%M:%S %z],
 [03-May-2011 13:25:26 +0100
 03-May-2011],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=3600
-sec=0,min=0,hour=0,mday=3,mon=4,year=111,wday=2,tz=0
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+sec=0,min=0,hour=0,mday=3,mon=4,year=111,wday=2,yday=123,tz=0
 ])
 
-SCANTIME([IMAP INTERNALDATE],[imap-internaldate],
+SCANTIME([RFC-822 Strict],[rfc822-strict],
 [%a, %d %b %Y %H:%M:%S %z],
 [Tue, 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+])
+
+SCANTIME([RFC-822],[rfc822],
+[[%[%a, %]%d %b %Y %H:%M%[:%S%] %z]],
+[Tue, 03 May 2011 13:25:26 +0200
+03 May 2011 13:25:26 +0200
+Tue, 03 May 2011 13:25 +0200
+03 May 2011 13:25 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=0,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=0,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+])
+
+SCANTIME([Any char],[anychar],
+[%a%? %d %b %Y %H:%M:%S %z],
+[Tue, 03 May 2011 13:25:26 +0200
+Tue: 03 May 2011 13:25:26 +0200
+Tue; 03 May 2011 13:25:26 +0200
+Tue 03 May 2011 13:25:26 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+])
+
+SCANTIME([Percent],[percent],
+[%d%%%b%%%Y %H:%M:%S %z],
+[03%May%2011 13:25:26 +0100],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+])
+
+SCANTIME([Fixed WS],[fixws],
+[%d-%b-%Y%\ %H:%M:%S %z],
+[03-May-2011 13:25:26 +0100
+03-May-2011  13:25:26 +0100],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+],
+[scantime: 2: parse failed near  13:25:26 +0100
+])
+
+SCANTIME([endp return],[endp],
+[%a, %d %b %Y %H:%M:%S %z],
+[Tue, 03 May 2011 13:25:26 +0100 other data
+],
+[# 1: stopped at  other data
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+])
+
+SCANTIME([Optional blocks],[opt],
+[[%[%a, %]%d %b %Y %H:%M:%S %z]],
+[Tue, 03 May 2011 13:25:26 +0200
+03 May 2011 13:25:26 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+])
+
+SCANTIME([Nested optional blocks],[nested-opt],
+[[%[%a%[,%] %]%d %b %Y %H:%M:%S %z]],
+[Tue, 03 May 2011 13:25:26 +0200
+Tue 03 May 2011 13:25:26 +0200
+03 May 2011 13:25:26 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+])
+
+SCANTIME([Optional alternatives],[opt-alt],
+[[%a%[,%|:%] %d %b %Y %H:%M:%S %z]],
+[Tue, 03 May 2011 13:25:26 +0200
+Tue: 03 May 2011 13:25:26 +0200
+Tue 03 May 2011 13:25:26 +0200
+Tue; 03 May 2011 13:25:26 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+],
+[scantime: 4: parse failed near ; 03 May 2011 13:25:26 +0200
+])
+
+SCANTIME([Alternatives],[alt],
+[%a%(,%|:%|/%) %d %b %Y %H:%M:%S %z],
+[Tue, 03 May 2011 13:25:26 +0200
+Tue: 03 May 2011 13:25:26 +0200
+Tue/ 03 May 2011 13:25:26 +0200
+Tue 03 May 2011 13:25:26 +0200
+Tue; 03 May 2011 13:25:26 +0200],
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+],
+[scantime: 4: parse failed near  03 May 2011 13:25:26 +0200
+scantime: 5: parse failed near ; 03 May 2011 13:25:26 +0200
 ])
 
 m4_popdef([SCANTIME])
diff --git a/libmailutils/tests/scantime.c b/libmailutils/tests/scantime.c
index 58464ac..962a7b3 100644
--- a/libmailutils/tests/scantime.c
+++ b/libmailutils/tests/scantime.c
@@ -45,7 +45,8 @@ main (int argc, char **argv)
   char *buf = NULL;
   size_t size = 0;
   size_t n;
-
+  int line;
+  
   mu_set_program_name (argv[0]);
   
   mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
@@ -65,31 +66,34 @@ main (int argc, char **argv)
 	}
     }
 
+  line = 0;
   while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
     {
       char *endp;
       struct tm tm;
       struct mu_timezone tz;
 
+      line++;
+      mu_ltrim_class (buf, MU_CTYPE_BLANK);
       mu_rtrim_class (buf, MU_CTYPE_ENDLN);
-
+      if (!*buf)
+	continue;
       rc = mu_scan_datetime (buf, format, &tm, &tz, &endp);
       if (rc)
 	{
 	  if (*endp)
-	    mu_error ("parse failed near %s", endp);
+	    mu_error ("%d: parse failed near %s", line, endp);
 	  else
-	    mu_error ("parse failed at end of input");
+	    mu_error ("%d: parse failed at end of input", line);
 	  continue;
 	}
       if (*endp)
-	mu_printf ("# stopped at %s\n", endp);
-      /* FIXME: add tm_yday? */
-      mu_printf ("sec=%d,min=%d,hour=%d,mday=%d,mon=%d,year=%d,wday=%d,tz=%d\n",
+	mu_printf ("# %d: stopped at %s\n", line, endp);
+      mu_printf ("sec=%d,min=%d,hour=%d,mday=%d,mon=%d,year=%d,wday=%d,yday=%d,tz=%d\n",
 		 tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday, tm.tm_mon,
-		 tm.tm_year, tm.tm_wday, tz.utc_offset);
+		 tm.tm_year, tm.tm_wday, tm.tm_yday, tz.utc_offset);
 		 
-      //mu_c_streamftime (mu_strout, "%c %z%n", &tm, &tz);
+      /*mu_c_streamftime (mu_strout, "%c %z%n", &tm, &tz);*/
     }
   
   if (rc)
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index 5fa3f5c..e15fc1d 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -472,7 +472,7 @@ _fill_response (void *item, void *data)
       else
 	{
 	  if (mu_scan_datetime (elt->v.string,
-				MU_DATETIME_RFC822,
+				MU_DATETIME_SCAN_RFC822,
 				&env->envelope->date,
 				&env->envelope->tz, NULL))
 	    rc = MU_ERR_FAILURE;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Dec 07 12:26:13 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RYLG1-00089o-51
	for mharc-commit-mailutils@gnu.org; Wed, 07 Dec 2011 12:26:13 -0500
Received: from eggs.gnu.org ([140.186.70.92]:53765)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYLFp-00088S-3b
	for commit-mailutils@gnu.org; Wed, 07 Dec 2011 12:26:10 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYLFg-0003nz-Q5
	for commit-mailutils@gnu.org; Wed, 07 Dec 2011 12:26:01 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:52981)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYLFg-0003nv-Me
	for commit-mailutils@gnu.org; Wed, 07 Dec 2011 12:25:52 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYLFe-0004Jb-Sh
	for commit-mailutils@gnu.org; Wed, 07 Dec 2011 17:25:51 +0000
Date: Wed, 07 Dec 2011 17:25:50 +0000
Message-Id: <E1RYLFe-0004Jb-Sh@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-503-gaa0debf
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: ea8772cd3bb401e2a3e2f992a1ae570d3b99abdb
X-Git-Newrev: aa0debf5d7f45684db96fe2a5be171ad870975ab
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 07 Dec 2011 17:26:10 -0000

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

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

The branch, master has been updated
       via  aa0debf5d7f45684db96fe2a5be171ad870975ab (commit)
       via  b12c4638805b9528800f319283536b3e0bb2fb13 (commit)
      from  ea8772cd3bb401e2a3e2f992a1ae570d3b99abdb (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 aa0debf5d7f45684db96fe2a5be171ad870975ab
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 7 13:48:44 2011 +0200

    imap client: implement STORE, DELETE, RENAME, CHECK, EXPUNGE, COPY, CLOSE and UNSELECT.
    
    * include/mailutils/imap.h (imap_command): New struct.
    (mu_imap_gencom): New function.
    (mu_imap_store,mu_imap_delete)
    (mu_imap_rename,mu_imap_mailbox_close,mu_imap_close)
    (mu_imap_check): New functions.
    (_mu_close_handler): New function.
    * include/mailutils/sys/imap.h (mu_imap_client_state): Add new states.
    * libproto/imap/gencom.c: New file.
    * libproto/imap/close.c: New file.
    * libproto/imap/delete.c: New file.
    * libproto/imap/rename.c: New file.
    * libproto/imap/store.c: New file.
    * libproto/imap/unselect.c: New file.
    * libproto/imap/check.c: New file.
    * libproto/imap/expunge.c: New file.
    * libproto/imap/copy.c: New file.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Add new files.
    
    * mu/imap.c: Implement new commands.
    * mu/shell.c (execute_line): Treat backslash as escape only before
    another backslash or double-quote.

commit b12c4638805b9528800f319283536b3e0bb2fb13
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 7 13:42:28 2011 +0200

    imap4d: CLOSE should not send EXPUNGE responses. Fix error checking in STORE.
    
    * imap4d/imap4d.h (silent_expunge): New global.
    * imap4d/sync.c (silent_expunge): New variable.
    (action): Suppress EXPUNGE responses if silent_expunge is set.
    * imap4d/close.c (imap4d_close0): Set silent_expunge before calling
    mu_mailbox_flush.
    
    * imap4d/tests/close-expunge.at: New test.
    * imap4d/tests/testsuite.at: Include close-expunge.at.
    * imap4d/tests/Makefile.am (TESTSUITE_AT): Add close-expunge.at.
    
    * imap4d/fetch.c (fetch_thunk): Emit BAD response if failed to parse
    message set.
    * imap4d/store.c (store_thunk): Emit BAD response if failed to parse
    flags.

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

Summary of changes:
 imap4d/close.c                                 |   10 ++-
 imap4d/fetch.c                                 |    5 +-
 imap4d/imap4d.h                                |    3 +-
 imap4d/store.c                                 |   14 ++--
 imap4d/sync.c                                  |   15 ++-
 imap4d/tests/Makefile.am                       |    1 +
 imap4d/tests/{expunge.at => close-expunge.at}  |   21 +++--
 imap4d/tests/testsuite.at                      |    1 +
 include/mailutils/imap.h                       |   33 +++++++-
 include/mailutils/sys/imap.h                   |   10 ++
 libmailutils/tests/.gitignore                  |    1 +
 libproto/imap/Makefile.am                      |   11 ++-
 libmu_dbm/delete.c => libproto/imap/check.c    |   25 +++--
 {libmu_dbm => libproto/imap}/close.c           |   32 +++++--
 libmu_dbm/delete.c => libproto/imap/copy.c     |   28 ++++--
 libmu_dbm/create.c => libproto/imap/delete.c   |   39 +++++----
 libmu_dbm/delete.c => libproto/imap/expunge.c  |   25 +++--
 libproto/imap/fetch.c                          |   57 +++---------
 libproto/imap/{noop.c => gencom.c}             |   52 +++++++++---
 libproto/imap/logout.c                         |    2 +-
 libproto/imap/noop.c                           |   56 +++----------
 libmu_dbm/delete.c => libproto/imap/rename.c   |   29 +++++--
 libmu_dbm/create.c => libproto/imap/store.c    |   40 +++++----
 libmu_dbm/delete.c => libproto/imap/unselect.c |   25 +++--
 mu/imap.c                                      |  109 +++++++++++++++++++++---
 mu/mu.h                                        |    4 +-
 mu/shell.c                                     |    3 +-
 27 files changed, 411 insertions(+), 240 deletions(-)
 copy imap4d/tests/{expunge.at => close-expunge.at} (73%)
 copy libmu_dbm/delete.c => libproto/imap/check.c (72%)
 copy {libmu_dbm => libproto/imap}/close.c (64%)
 copy libmu_dbm/delete.c => libproto/imap/copy.c (64%)
 copy libmu_dbm/create.c => libproto/imap/delete.c (66%)
 copy libmu_dbm/delete.c => libproto/imap/expunge.c (71%)
 copy libproto/imap/{noop.c => gencom.c} (61%)
 copy libmu_dbm/delete.c => libproto/imap/rename.c (64%)
 copy libmu_dbm/create.c => libproto/imap/store.c (63%)
 copy libmu_dbm/delete.c => libproto/imap/unselect.c (70%)

diff --git a/imap4d/close.c b/imap4d/close.c
index 352de98..a1535dd 100644
--- a/imap4d/close.c
+++ b/imap4d/close.c
@@ -30,9 +30,11 @@ imap4d_close0 (struct imap4d_command *command, imap4d_tokbuf_t tok,
   mu_mailbox_get_flags (mbox, &flags);
   if (flags & MU_STREAM_WRITE)
     {
+      silent_expunge = expunge;
       imap4d_enter_critical ();
       status = mu_mailbox_flush (mbox, expunge);
       imap4d_leave_critical ();
+      silent_expunge = 0;
       if (status)
 	{
 	  mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_flush", NULL, status);
@@ -69,9 +71,11 @@ imap4d_close0 (struct imap4d_command *command, imap4d_tokbuf_t tok,
                NO - close failure: no mailbox selected
                BAD - command unknown or arguments invalid
 
-   The CLOSE command permanently removes from the currently selected
-   mailbox all messages that have the \\Deleted flag set, and returns
-   to authenticated state from selected state.  */
+      The CLOSE command permanently removes all messages that have the
+      \Deleted flag set from the currently selected mailbox, and returns
+      to the authenticated state from the selected state.  No untagged
+      EXPUNGE responses are sent. */
+
 int
 imap4d_close (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index cd7baa6..40a963e 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -1778,13 +1778,10 @@ fetch_thunk (imap4d_parsebuf_t pb)
 	 FIXME: This code also causes imap4d to silently ignore erroneous
 	 msgset specifications (e.g. FETCH foobar (FLAGS)), which should
 	 be fixed.  */
-      
-      pb->err_text = "Completed";
       return RESP_OK;
 
     default:
-      pb->err_text = "Failed to parse message set";
-      return RESP_NO;
+      imap4d_parsebuf_exit (pb, "Failed to parse message set");
     }
 
   /* Compile the expression */
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index b2290a1..f74eba3 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -205,7 +205,8 @@ extern char **imap4d_argv;
 extern jmp_buf child_jmp;
 
 extern int test_mode;
-  
+extern int silent_expunge;
+
 /* Input functions */
 extern mu_stream_t iostream;
 extern int  io_untagged_response (int, const char *, ...) MU_PRINTFLIKE(2,3);
diff --git a/imap4d/store.c b/imap4d/store.c
index b4e5cbe..7e9d9ad 100644
--- a/imap4d/store.c
+++ b/imap4d/store.c
@@ -79,12 +79,10 @@ store_thunk (imap4d_parsebuf_t p)
     case EINVAL:
       /* See RFC 3501, section 6.4.8, and a comment to the equivalent code
 	 in fetch.c */
-      p->err_text = "Completed";
       return RESP_OK;
 
     default:
-      p->err_text = "Failed to parse message set";
-      return RESP_NO;
+      imap4d_parsebuf_exit (p, "Failed to parse message set");
     }      
 
   if (p->token[0] != '(')
@@ -94,7 +92,9 @@ store_thunk (imap4d_parsebuf_t p)
   do
     {
       int t;
-      if (!util_attribute_to_type (p->token, &t))
+      if (util_attribute_to_type (p->token, &t))
+	imap4d_parsebuf_exit (p, "Failed to parse flags");
+      else
 	pclos->type |= t;
     }
   while (imap4d_parsebuf_next (p, 1) && p->token[0] != ')');
@@ -163,7 +163,7 @@ imap4d_store0 (imap4d_tokbuf_t tok, int isuid, char **ptext)
 
       *ptext = "Completed";
     }
-
+  
   free (pclos.set);
   
   return rc;
@@ -173,9 +173,9 @@ int
 imap4d_store (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
   int rc;
-  char *err_text;
+  char *err_text = NULL;
   
   rc = imap4d_store0 (tok, 0, &err_text);
-  return io_completion_response (command, rc, "%s", err_text);
+  return io_completion_response (command, rc, "%s", err_text ? err_text : "");
 }
 
diff --git a/imap4d/sync.c b/imap4d/sync.c
index 4033ec0..1a5d0c5 100644
--- a/imap4d/sync.c
+++ b/imap4d/sync.c
@@ -136,7 +136,11 @@ imap4d_sync_flags (size_t msgno)
   return 0;
 }
 
+int silent_expunge;
+/* When true, non-tagged EXPUNGE responses are suppressed. */
+
 static int mailbox_corrupt;
+/* When true, mailbox has been altered by another party. */
 
 static int
 action (mu_observer_t observer, size_t type, void *data, void *action_data)
@@ -158,11 +162,12 @@ action (mu_observer_t observer, size_t type, void *data, void *action_data)
 	 immediately decremented by 1, and this decrement is reflected in
 	 message sequence numbers in subsequent responses (including other
 	 untagged EXPUNGE responses). */
-      {
-	size_t *exp = data;
-	io_untagged_response (RESP_NONE, "%lu EXPUNGED",
-			      (unsigned long) (exp[0] - exp[1]));
-      }
+      if (!silent_expunge)
+	{
+	  size_t *exp = data;
+	  io_untagged_response (RESP_NONE, "%lu EXPUNGED",
+				(unsigned long) (exp[0] - exp[1]));
+	}
     }
   return 0;
 }
diff --git a/imap4d/tests/Makefile.am b/imap4d/tests/Makefile.am
index 6d471c5..ecde2b6 100644
--- a/imap4d/tests/Makefile.am
+++ b/imap4d/tests/Makefile.am
@@ -42,6 +42,7 @@ TESTSUITE_AT = \
  anystate.at\
  append00.at\
  append01.at\
+ close-expunge.at\
  create01.at\
  create02.at\
  examine.at\
diff --git a/imap4d/tests/expunge.at b/imap4d/tests/close-expunge.at
similarity index 73%
copy from imap4d/tests/expunge.at
copy to imap4d/tests/close-expunge.at
index fe01f79..714ecc8 100644
--- a/imap4d/tests/expunge.at
+++ b/imap4d/tests/close-expunge.at
@@ -14,14 +14,19 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([expunge])
+AT_SETUP([Close with expunge])
+AT_KEYWORDS([close close-expunge])
+
+# According to RFC3501, CLOSE command should not send untagged EXPUNGE
+# responses even if expunge actually took place.
 
 IMAP4D_CHECK([
 MUT_MBCOPY($abs_top_srcdir/testsuite/spool/search.mbox,temp)
 sed 's/^\(Status: .*\)/\1D/' temp > INBOX
 ],
 [1 SELECT INBOX
-2 EXPUNGE
+2 CLOSE
+3 EXAMINE INBOX
 X LOGOUT
 ],
 [* PREAUTH IMAP4rev1 Test mode
@@ -32,12 +37,14 @@ X LOGOUT
 * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
 * OK [[PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)]] Permanent flags
 1 OK [[READ-WRITE]] SELECT Completed
-* 1 EXPUNGED
-* 1 EXPUNGED
-* 1 EXPUNGED
+2 OK CLOSE Completed
 * 5 EXISTS
-* 5 RECENT
-2 OK EXPUNGE Completed
+* 0 RECENT
+* OK [[UIDNEXT 9]] Predicted next uid
+* OK [[UNSEEN 1]] first unseen messsage
+* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
+* OK [[PERMANENTFLAGS ()]] No permanent flags
+3 OK [[READ-ONLY]] EXAMINE Completed
 * BYE Session terminating.
 X OK LOGOUT Completed
 ])
diff --git a/imap4d/tests/testsuite.at b/imap4d/tests/testsuite.at
index 0a18641..fc8fcae 100644
--- a/imap4d/tests/testsuite.at
+++ b/imap4d/tests/testsuite.at
@@ -70,6 +70,7 @@ m4_include([select.at])
 m4_include([examine.at])
 m4_include([status.at])
 m4_include([expunge.at])
+m4_include([close-expunge.at])
 m4_include([create01.at])
 m4_include([create02.at])
 
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 86e365c..7a77ac3 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -38,13 +38,25 @@ enum mu_imap_session_state
     MU_IMAP_SESSION_INIT,     /* Initial state (disconnected) */
     MU_IMAP_SESSION_NONAUTH,  /* Non-Authenticated State */
     MU_IMAP_SESSION_AUTH,     /* Authenticated State */
-    MU_IMAP_SESSION_SELECTED, /* Selected State */
-    MU_IMAP_SESSION_LOGOUT,   /* Logout State */
+    MU_IMAP_SESSION_SELECTED  /* Selected State */
   };
   
 int mu_imap_create (mu_imap_t *pimap);
 void mu_imap_destroy (mu_imap_t *pimap);
 
+struct imap_command
+{
+  int session_state;
+  char *capa;
+  int rx_state;
+  int uid;
+  int argc;
+  char const **argv;
+  void (*handler) (mu_imap_t);
+};
+
+int mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd);
+  
 int mu_imap_connect (mu_imap_t imap);
 int mu_imap_disconnect (mu_imap_t imap);
 
@@ -58,8 +70,23 @@ int mu_imap_logout (mu_imap_t imap);
 int mu_imap_id (mu_imap_t imap, char **idenv, mu_assoc_t *passoc);
 
 int mu_imap_noop (mu_imap_t imap);
+int mu_imap_check (mu_imap_t imap);
+
+int mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset,
+		   const char *items);
+int mu_imap_store (mu_imap_t imap, int uid, const char *msgset,
+		   const char *items);
+
+int mu_imap_delete (mu_imap_t imap, const char *mailbox);
+int mu_imap_rename (mu_imap_t imap, const char *mailbox,
+		    const char *new_mailbox);
+int mu_imap_copy (mu_imap_t imap, int uid, const char *msgset,
+		  const char *mailbox);
+
+int mu_imap_close (mu_imap_t imap);
+int mu_imap_unselect (mu_imap_t imap);
 
-int mu_imap_fetch (mu_imap_t imap, const char *msgset, const char *items);
+int mu_imap_expunge (mu_imap_t imap);
   
 int mu_imap_set_carrier (mu_imap_t imap, mu_stream_t carrier);
 int mu_imap_get_carrier (mu_imap_t imap, mu_stream_t *pcarrier);
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index 02deead..adb258b 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -57,6 +57,14 @@ enum mu_imap_client_state
     MU_IMAP_CLIENT_STATUS_RX,
     MU_IMAP_CLIENT_NOOP_RX,
     MU_IMAP_CLIENT_FETCH_RX,
+    MU_IMAP_CLIENT_STORE_RX,
+    MU_IMAP_CLIENT_DELETE_RX,
+    MU_IMAP_CLIENT_RENAME_RX,
+    MU_IMAP_CLIENT_CLOSE_RX,
+    MU_IMAP_CLIENT_UNSELECT_RX,
+    MU_IMAP_CLIENT_CHECK_RX,
+    MU_IMAP_CLIENT_COPY_RX,
+    MU_IMAP_CLIENT_EXPUNGE_RX,
     MU_IMAP_CLIENT_CLOSING
   };
 
@@ -194,6 +202,8 @@ int _mu_imap_collect_flags (struct imap_list_element *arg, int *res);
 struct imap_list_element *_mu_imap_list_at (mu_list_t list, int idx);
   
 int _mu_imap_parse_fetch_response (mu_list_t resp, mu_list_t *result_list);
+
+void _mu_close_handler (mu_imap_t imap);
   
 # ifdef __cplusplus
 }
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index ca22f53..aa5a1f3 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -16,6 +16,7 @@ imapio
 listop
 mailcap
 prop
+scantime
 strftime
 url-comp
 url-parse
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index fbd14bd..224dc26 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -29,25 +29,34 @@ libmu_imap_la_LIBADD = ${MU_LIB_AUTH} ${MU_LIB_MAILUTILS} @INTLLIBS@
 libmu_imap_la_SOURCES = \
  fake-folder.c\
  fetch.c\
+ gencom.c\
  callback.c\
  capability.c\
  capatst.c\
  carrier.c\
+ check.c\
+ close.c\
  connect.c\
+ copy.c\
  create.c\
+ delete.c\
  destroy.c\
  disconnect.c\
  err.c\
+ expunge.c\
  id.c\
  login.c\
  logout.c\
  noop.c\
+ rename.c\
  resplist.c\
  response.c\
  resproc.c\
  select.c\
  state.c\
  status.c\
+ store.c\
  tag.c\
- trace.c
+ trace.c\
+ unselect.c
 
diff --git a/libmu_dbm/delete.c b/libproto/imap/check.c
similarity index 72%
copy from libmu_dbm/delete.c
copy to libproto/imap/check.c
index f36f875..0346906 100644
--- a/libmu_dbm/delete.c
+++ b/libproto/imap/check.c
@@ -19,17 +19,22 @@
 # include <config.h>
 #endif
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
+mu_imap_check (mu_imap_t imap)
 {
-  DBMSYSCK (db, _dbm_delete);
-  if (!db->db_descr)
-    return EINVAL;
-  return db->db_sys->_dbm_delete (db, key);
+  static char const *command = "CHECK";
+  static struct imap_command com = {
+    MU_IMAP_SESSION_SELECTED,
+    NULL,
+    MU_IMAP_CLIENT_CHECK_RX,
+    0,
+    1,
+    &command,
+    NULL
+  };
+  return mu_imap_gencom (imap, &com);
 }
-
+      
diff --git a/libmu_dbm/close.c b/libproto/imap/close.c
similarity index 64%
copy from libmu_dbm/close.c
copy to libproto/imap/close.c
index a84574a..a92d598 100644
--- a/libmu_dbm/close.c
+++ b/libproto/imap/close.c
@@ -19,17 +19,29 @@
 # include <config.h>
 #endif
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
-int
-mu_dbm_close (mu_dbm_file_t db)
+void
+_mu_close_handler (mu_imap_t imap)
 {
-  DBMSYSCK (db, _dbm_close);
-  if (!db->db_descr)
-    return 0;
-  return db->db_sys->_dbm_close (db);
+  if (imap->resp_code == MU_IMAP_OK)
+    imap->session_state = MU_IMAP_SESSION_AUTH;
 }
 
+int
+mu_imap_close (mu_imap_t imap)
+{
+  static char const *command = "CLOSE";
+  static struct imap_command com = {
+    MU_IMAP_SESSION_SELECTED,
+    NULL,
+    MU_IMAP_CLIENT_CLOSE_RX,
+    0,
+    1,
+    &command,
+    _mu_close_handler
+  };
+  return mu_imap_gencom (imap, &com);
+}
+      
diff --git a/libmu_dbm/delete.c b/libproto/imap/copy.c
similarity index 64%
copy from libmu_dbm/delete.c
copy to libproto/imap/copy.c
index f36f875..5a11a1c 100644
--- a/libmu_dbm/delete.c
+++ b/libproto/imap/copy.c
@@ -19,17 +19,27 @@
 # include <config.h>
 #endif
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
 #include <mailutils/errno.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
+mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
 {
-  DBMSYSCK (db, _dbm_delete);
-  if (!db->db_descr)
-    return EINVAL;
-  return db->db_sys->_dbm_delete (db, key);
-}
+  char const *argv[3];
+  static struct imap_command com;
+
+  argv[0] = "COPY";
+  argv[1] = msgset;
+  argv[2] = mailbox;
 
+  com.session_state = MU_IMAP_SESSION_SELECTED;
+  com.capa = NULL;
+  com.rx_state = MU_IMAP_CLIENT_COPY_RX;
+  com.uid = 0;
+  com.argc = 3;
+  com.argv = argv;
+  com.handler = NULL;
+
+  return mu_imap_gencom (imap, &com);
+}
diff --git a/libmu_dbm/create.c b/libproto/imap/delete.c
similarity index 66%
copy from libmu_dbm/create.c
copy to libproto/imap/delete.c
index 44c161d..e29f028 100644
--- a/libmu_dbm/create.c
+++ b/libproto/imap/delete.c
@@ -18,27 +18,30 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <unistd.h>
+
 #include <stdlib.h>
-#include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
+#include <string.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_create (char *name, mu_dbm_file_t *db, int defsafety)
+mu_imap_delete (mu_imap_t imap, const char *mailbox)
 {
-  int rc;
-  mu_url_t url;
-
-  mu_dbm_init ();
-  rc = mu_url_create_hint (&url, name, 0, mu_dbm_hint);
-  if (rc)
-    return rc;
-  rc = mu_dbm_create_from_url (url, db, defsafety);
-  mu_url_destroy (&url);
-  return rc;
+  char const *argv[2];
+  static struct imap_command com;
+
+  argv[0] = "DELETE";
+  argv[1] = mailbox;
+
+  com.session_state = MU_IMAP_SESSION_AUTH;
+  com.capa = NULL;
+  com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
+  com.uid = 0;
+  com.argc = 2;
+  com.argv = argv;
+  com.handler = NULL;
+
+  return mu_imap_gencom (imap, &com);
 }
+      
diff --git a/libmu_dbm/delete.c b/libproto/imap/expunge.c
similarity index 71%
copy from libmu_dbm/delete.c
copy to libproto/imap/expunge.c
index f36f875..376ee4b 100644
--- a/libmu_dbm/delete.c
+++ b/libproto/imap/expunge.c
@@ -19,17 +19,22 @@
 # include <config.h>
 #endif
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
+mu_imap_expunge (mu_imap_t imap)
 {
-  DBMSYSCK (db, _dbm_delete);
-  if (!db->db_descr)
-    return EINVAL;
-  return db->db_sys->_dbm_delete (db, key);
+  static char const *command = "EXPUNGE";
+  static struct imap_command com = {
+    MU_IMAP_SESSION_SELECTED,
+    NULL,
+    MU_IMAP_CLIENT_EXPUNGE_RX,
+    0,
+    1,
+    &command,
+    NULL
+  };
+  return mu_imap_gencom (imap, &com);
 }
-
+      
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index e15fc1d..1d7c0f2 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -29,53 +29,24 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_fetch (mu_imap_t imap, const char *msgset, const char *items)
+mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
 {
-  int status;
-  
-  if (imap == NULL)
-    return EINVAL;
-  if (!imap->io)
-    return MU_ERR_NO_TRANSPORT;
+  char const *argv[3];
+  static struct imap_command com;
 
-  if (imap->session_state != MU_IMAP_SESSION_SELECTED)
-    return MU_ERR_SEQ;
+  argv[0] = "FETCH";
+  argv[1] = msgset;
+  argv[2] = items;
   
-  switch (imap->client_state)
-    {
-    case MU_IMAP_CLIENT_READY:
-      status = _mu_imap_tag_next (imap);
-      MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s FETCH %s %s\r\n",
-				 imap->tag_str, msgset, items);
-      MU_IMAP_CHECK_ERROR (imap, status);
-      MU_IMAP_FCLR (imap, MU_IMAP_RESP);
-      imap->client_state = MU_IMAP_CLIENT_FETCH_RX;
-
-    case MU_IMAP_CLIENT_FETCH_RX:
-      status = _mu_imap_response (imap, NULL, NULL);
-      MU_IMAP_CHECK_EAGAIN (imap, status);
-      switch (imap->resp_code)
-	{
-	case MU_IMAP_OK:
-	  status = 0;
-	  break;
-
-	case MU_IMAP_NO:
-	  status = MU_ERR_FAILURE;
-	  break;
-
-	case MU_IMAP_BAD:
-	  status = MU_ERR_BADREPLY;
-	  break;
-	}
-      imap->client_state = MU_IMAP_CLIENT_READY;
-      break;
-
-    default:
-      status = EINPROGRESS;
-    }
-  return status;
+  com.session_state = MU_IMAP_SESSION_SELECTED;
+  com.capa = NULL;
+  com.rx_state = MU_IMAP_CLIENT_FETCH_RX;
+  com.uid = uid;
+  com.argc = 3;
+  com.argv = argv;
+  com.handler = NULL;
+
+  return mu_imap_gencom (imap, &com);
 }
 
 static void
diff --git a/libproto/imap/noop.c b/libproto/imap/gencom.c
similarity index 61%
copy from libproto/imap/noop.c
copy to libproto/imap/gencom.c
index a4b22e7..5786a2b 100644
--- a/libproto/imap/noop.c
+++ b/libproto/imap/gencom.c
@@ -25,30 +25,59 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_noop (mu_imap_t imap)
+mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
 {
   int status;
+  int i;
   
-  if (imap == NULL)
+  if (imap == NULL || !cmd || cmd->argc < 1)
     return EINVAL;
   if (!imap->io)
     return MU_ERR_NO_TRANSPORT;
-  if (imap->session_state == MU_IMAP_SESSION_INIT)
+  if (imap->session_state < cmd->session_state)
     return MU_ERR_SEQ;
 
-  switch (imap->client_state)
+  if (cmd->capa)
+    {
+      status = mu_imap_capability_test (imap, cmd->capa, NULL);
+      switch (status)
+	{
+	case 0:
+	  break;
+
+	case MU_ERR_NOENT:
+	  return ENOSYS;
+
+	default:
+	  return status;
+	}
+    }
+  
+  if (imap->client_state == MU_IMAP_CLIENT_READY)
     {
-    case MU_IMAP_CLIENT_READY:
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s NOOP\r\n", imap->tag_str);
+      status = mu_imapio_printf (imap->io, "%s", imap->tag_str);
+      if (status == 0 && cmd->uid)
+	status = mu_imapio_printf (imap->io, " UID");
+      MU_IMAP_CHECK_ERROR (imap, status);
+      for (i = 0; i < cmd->argc; i++)
+	{
+	  status = mu_imapio_printf (imap->io, " %s", cmd->argv[i]);
+	  MU_IMAP_CHECK_ERROR (imap, status);
+	}
+      status = mu_imapio_send (imap->io, "\r\n", 2);
       MU_IMAP_CHECK_ERROR (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
-      imap->client_state = MU_IMAP_CLIENT_NOOP_RX;
+      imap->client_state = cmd->rx_state;
+    }
 
-    case MU_IMAP_CLIENT_NOOP_RX:
+  if (imap->client_state == cmd->rx_state)
+    {
       status = _mu_imap_response (imap, NULL, NULL);
       MU_IMAP_CHECK_EAGAIN (imap, status);
+      if (cmd->handler)
+	  cmd->handler (imap);
       switch (imap->resp_code)
 	{
 	case MU_IMAP_OK:
@@ -64,11 +93,10 @@ mu_imap_noop (mu_imap_t imap)
 	  break;
 	}
       imap->client_state = MU_IMAP_CLIENT_READY;
-      break;
-
-    default:
-      status = EINPROGRESS;
     }
+  else
+    status = EINPROGRESS;
+
   return status;
 }
       
diff --git a/libproto/imap/logout.c b/libproto/imap/logout.c
index e91e1ce..9b39b39 100644
--- a/libproto/imap/logout.c
+++ b/libproto/imap/logout.c
@@ -51,7 +51,7 @@ mu_imap_logout (mu_imap_t imap)
       status = _mu_imap_response (imap, NULL, NULL);
       MU_IMAP_CHECK_EAGAIN (imap, status);
       imap->client_state = MU_IMAP_CLIENT_READY;
-      imap->session_state = MU_IMAP_SESSION_LOGOUT;
+      imap->session_state = MU_IMAP_SESSION_INIT;
       break;
 
     default:
diff --git a/libproto/imap/noop.c b/libproto/imap/noop.c
index a4b22e7..62d5035 100644
--- a/libproto/imap/noop.c
+++ b/libproto/imap/noop.c
@@ -19,56 +19,22 @@
 # include <config.h>
 #endif
 
-#include <stdlib.h>
-#include <mailutils/errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
 int
 mu_imap_noop (mu_imap_t imap)
 {
-  int status;
-  
-  if (imap == NULL)
-    return EINVAL;
-  if (!imap->io)
-    return MU_ERR_NO_TRANSPORT;
-  if (imap->session_state == MU_IMAP_SESSION_INIT)
-    return MU_ERR_SEQ;
-
-  switch (imap->client_state)
-    {
-    case MU_IMAP_CLIENT_READY:
-      status = _mu_imap_tag_next (imap);
-      MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s NOOP\r\n", imap->tag_str);
-      MU_IMAP_CHECK_ERROR (imap, status);
-      MU_IMAP_FCLR (imap, MU_IMAP_RESP);
-      imap->client_state = MU_IMAP_CLIENT_NOOP_RX;
-
-    case MU_IMAP_CLIENT_NOOP_RX:
-      status = _mu_imap_response (imap, NULL, NULL);
-      MU_IMAP_CHECK_EAGAIN (imap, status);
-      switch (imap->resp_code)
-	{
-	case MU_IMAP_OK:
-	  status = 0;
-	  break;
-
-	case MU_IMAP_NO:
-	  status = MU_ERR_FAILURE;
-	  break;
-
-	case MU_IMAP_BAD:
-	  status = MU_ERR_BADREPLY;
-	  break;
-	}
-      imap->client_state = MU_IMAP_CLIENT_READY;
-      break;
-
-    default:
-      status = EINPROGRESS;
-    }
-  return status;
+  static char const *command = "NOOP";
+  static struct imap_command com = {
+    MU_IMAP_SESSION_INIT,
+    NULL,
+    MU_IMAP_CLIENT_NOOP_RX,
+    0,
+    1,
+    &command,
+    NULL
+  };
+  return mu_imap_gencom (imap, &com);
 }
       
diff --git a/libmu_dbm/delete.c b/libproto/imap/rename.c
similarity index 64%
copy from libmu_dbm/delete.c
copy to libproto/imap/rename.c
index f36f875..79b3778 100644
--- a/libmu_dbm/delete.c
+++ b/libproto/imap/rename.c
@@ -19,17 +19,28 @@
 # include <config.h>
 #endif
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
 #include <mailutils/errno.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
+mu_imap_rename (mu_imap_t imap, const char *mailbox, const char *new_mailbox)
 {
-  DBMSYSCK (db, _dbm_delete);
-  if (!db->db_descr)
-    return EINVAL;
-  return db->db_sys->_dbm_delete (db, key);
-}
+  char const *argv[3];
+  static struct imap_command com;
+
+  argv[0] = "RENAME";
+  argv[1] = mailbox;
+  argv[2] = new_mailbox;
 
+  com.session_state = MU_IMAP_SESSION_AUTH;
+  com.capa = NULL;
+  com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
+  com.uid = 0;
+  com.argc = 3;
+  com.argv = argv;
+  com.handler = NULL;
+
+  return mu_imap_gencom (imap, &com);
+}
+      
diff --git a/libmu_dbm/create.c b/libproto/imap/store.c
similarity index 63%
copy from libmu_dbm/create.c
copy to libproto/imap/store.c
index 44c161d..be0d728 100644
--- a/libmu_dbm/create.c
+++ b/libproto/imap/store.c
@@ -18,27 +18,31 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <unistd.h>
+
 #include <stdlib.h>
-#include <mailutils/types.h>
-#include <mailutils/list.h>
-#include <mailutils/url.h>
-#include <mailutils/dbm.h>
+#include <string.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_create (char *name, mu_dbm_file_t *db, int defsafety)
+mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
 {
-  int rc;
-  mu_url_t url;
-
-  mu_dbm_init ();
-  rc = mu_url_create_hint (&url, name, 0, mu_dbm_hint);
-  if (rc)
-    return rc;
-  rc = mu_dbm_create_from_url (url, db, defsafety);
-  mu_url_destroy (&url);
-  return rc;
+  char const *argv[3];
+  static struct imap_command com;
+
+  argv[0] = "STORE";
+  argv[1] = msgset;
+  argv[2] = items;
+  
+  com.session_state = MU_IMAP_SESSION_SELECTED;
+  com.capa = NULL;
+  com.rx_state = MU_IMAP_CLIENT_STORE_RX;
+  com.uid = uid;
+  com.argc = 3;
+  com.argv = argv;
+  com.handler = NULL;
+
+  return mu_imap_gencom (imap, &com);
 }
+      
diff --git a/libmu_dbm/delete.c b/libproto/imap/unselect.c
similarity index 70%
copy from libmu_dbm/delete.c
copy to libproto/imap/unselect.c
index f36f875..f45c4aa 100644
--- a/libmu_dbm/delete.c
+++ b/libproto/imap/unselect.c
@@ -19,17 +19,22 @@
 # include <config.h>
 #endif
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
 
 int
-mu_dbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
+mu_imap_unselect (mu_imap_t imap)
 {
-  DBMSYSCK (db, _dbm_delete);
-  if (!db->db_descr)
-    return EINVAL;
-  return db->db_sys->_dbm_delete (db, key);
+  static char const *command = "UNSELECT";
+  static struct imap_command com = {
+    MU_IMAP_SESSION_SELECTED,
+    NULL,
+    MU_IMAP_CLIENT_UNSELECT_RX,
+    0,
+    1,
+    &command,
+    _mu_close_handler
+  };
+  return mu_imap_gencom (imap, &com);
 }
-
+      
diff --git a/mu/imap.c b/mu/imap.c
index 8132b27..5468058 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -69,11 +69,7 @@ current_imap_state ()
   if (imap == NULL)
     state = MU_IMAP_SESSION_INIT;
   else
-    {
-      mu_imap_state (imap, &state);
-      if (state == MU_IMAP_SESSION_LOGOUT)
-	state = MU_IMAP_SESSION_INIT;
-    }
+    mu_imap_state (imap, &state);
   return state;
 }
 
@@ -731,6 +727,15 @@ com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
 }
 
 static int
+com_check (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  int status = mu_imap_check (imap);
+  if (status)
+    report_failure ("check", status);
+  return 0;
+}
+
+static int
 com_fetch (int argc, char **argv)
 {
   int status;
@@ -739,7 +744,7 @@ com_fetch (int argc, char **argv)
   mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
 				      imap_fetch_callback,
 				      out);
-  status = mu_imap_fetch (imap, argv[1], argv[2]);
+  status = mu_imap_fetch (imap, 0, argv[1], argv[2]);
   mu_stream_destroy (&out);
   mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
 				      imap_fetch_callback,
@@ -749,22 +754,76 @@ com_fetch (int argc, char **argv)
   return 0;  
 }
 
+static int
+com_store (int argc, char **argv)
+{
+  int status = mu_imap_store (imap, 0, argv[1], argv[2]);
+  if (status)
+    report_failure ("store", status);
+  return 0;
+}
+
+static int
+com_close (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  int status = mu_imap_close (imap);
+  if (status)
+    report_failure ("close", status);
+  return 0;
+}
+
+static int
+com_unselect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  int status = mu_imap_noop (imap);
+  if (status)
+    report_failure ("unselect", status);
+  return 0;
+}
+
+static int
+com_delete (int argc, char **argv)
+{
+  int status = mu_imap_delete (imap, argv[1]);
+  if (status)
+    report_failure ("delete", status);
+  return 0;
+}
+
+static int
+com_rename (int argc, char **argv)
+{
+  int status = mu_imap_rename (imap, argv[1], argv[2]);
+  if (status)
+    report_failure ("rename", status);
+  return 0;
+}
+
+static int
+com_expunge (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  int status = mu_imap_expunge (imap);
+  if (status)
+    report_failure ("expunge", status);
+  return 0;
+}
+
 struct mutool_command imap_comtab[] = {
-  { "capability", 1, -1, 0,
+  { "capability",   1, -1, 0,
     com_capability,
     /* TRANSLATORS: -reread is a keyword; do not translate. */
     N_("[-reread] [NAME...]"),
     N_("list server capabilities") },
-  { "verbose",    1, 4, 0,
+  { "verbose",      1, 4, 0,
     com_verbose,
     "[on|off|mask|unmask] [secure [payload]]",
     N_("control the protocol tracing") },
-  { "connect",    1, 4, 0,
+  { "connect",      1, 4, 0,
     com_connect,
-    /* TRANSLATORS: --tls is a keyword. */
+    /* TRANSLATORS: -tls is a keyword. */
     N_("[-tls] HOSTNAME [PORT]"),
     N_("open connection") },
-  { "disconnect", 1, 1, 0,
+  { "disconnect",   1, 1, 0,
     com_disconnect,
     NULL,
     N_("close connection") },
@@ -784,6 +843,10 @@ struct mutool_command imap_comtab[] = {
     com_noop,
     NULL,
     N_("no operation (keepalive)") },
+  { "check",        1, 1, 0,
+    com_check,
+    NULL,
+    N_("request a server checkpoint") },
   { "select",       1, 2, 0,
     com_select,
     N_("[MBOX]"),
@@ -800,6 +863,30 @@ struct mutool_command imap_comtab[] = {
     com_fetch,
     N_("MSGSET ITEMS"),
     N_("fetch message data") },
+  { "store",        3, 3, CMD_COALESCE_EXTRA_ARGS,
+    com_store,
+    N_("MSGSET ITEMS"),
+    N_("alter mailbox data") },
+  { "close",        1, 1, 0,
+    com_close,
+    NULL,
+    N_("close the mailbox (with expunge)") },
+  { "unselect",     1, 1, 0,
+    com_unselect,
+    NULL,
+    N_("close the mailbox (without expunge)") },
+  { "delete",       2, 2, 0,
+    com_delete,
+    N_("MAILBOX"),
+    N_("delete the mailbox") },
+  { "rename",       3, 3, 0,
+    com_rename,
+    N_("OLD-NAME NEW-NAME"),
+    N_("rename existing mailbox") },
+  { "expunge",      1, 1, 0,
+    com_expunge,
+    NULL,
+    N_("permanently remove messages marked for deletion") },
   { "quit",         1, 1, 0,
     com_logout,
     NULL,
diff --git a/mu/mu.h b/mu/mu.h
index 74544b5..76bdb3a 100644
--- a/mu/mu.h
+++ b/mu/mu.h
@@ -23,13 +23,13 @@ typedef int (*mutool_action_t) (int argc, char **argv);
 struct mutool_command
 {
   const char *name;	/* User printable name of the function. */
-  int argmin;           /* Min. acceptable number of arguments (> 1) */ 
+  int argmin;           /* Min. acceptable number of arguments (>= 1) */ 
   int argmax;           /* Max. allowed number of arguments (-1 means not
 			   limited */
   int flags;
   mutool_action_t func;	/* Function to call to do the job. */
   const char *argdoc;   /* Documentation for the arguments */
-  const char *docstring;/* Documentation for this function.  */
+  const char *docstring;/* Documentation for this function. */
 };
 
 extern char *mutool_shell_prompt;
diff --git a/mu/shell.c b/mu/shell.c
index 0426788..891b08b 100644
--- a/mu/shell.c
+++ b/mu/shell.c
@@ -521,8 +521,9 @@ execute_line (char *line)
   int status = 0;
   
   ws.ws_comment = "#";
+  ws.ws_escape = "\\\"";
   rc = mu_wordsplit (line, &ws,
-		     MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT|
+		     MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT|MU_WRDSF_ESCAPE|
 		     MU_WRDSF_INCREMENTAL|MU_WRDSF_APPEND);
   if (rc == MU_WRDSE_NOINPUT)
     {


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 08 04:38:12 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RYaQe-000627-Tu
	for mharc-commit-mailutils@gnu.org; Thu, 08 Dec 2011 04:38:12 -0500
Received: from eggs.gnu.org ([140.186.70.92]:43222)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYaQa-0005zb-6l
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 04:38:11 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYaQT-0004sQ-Nj
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 04:38:08 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:33467)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYaQT-0004sF-LN
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 04:38:01 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYaQS-0003lh-U4
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 09:38:01 +0000
Date: Thu, 08 Dec 2011 09:38:00 +0000
Message-Id: <E1RYaQS-0003lh-U4@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-506-g6301266
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: aa0debf5d7f45684db96fe2a5be171ad870975ab
X-Git-Newrev: 6301266fabab03f0e1dfcab76e7b6fc1e6eda1c6
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 08 Dec 2011 09:38:11 -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=6301266fabab03f0e1dfcab76e7b6fc1e6eda1c6

The branch, master has been updated
       via  6301266fabab03f0e1dfcab76e7b6fc1e6eda1c6 (commit)
       via  5357fe739bdec1b99abff548098decd4e8c6c5ef (commit)
       via  fae5d5cd60e96989115706ff07e7f7e8134a8185 (commit)
      from  aa0debf5d7f45684db96fe2a5be171ad870975ab (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 6301266fabab03f0e1dfcab76e7b6fc1e6eda1c6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 11:20:51 2011 +0200

    imap client: implement CREATE and APPEND. Fix APPEND in imap4d.
    
    * imap4d/append.c (imap4d_append0): Use
    mu_message_from_stream_with_envelope with crafted envelope.  The effect is
    that the envelope of the message always reflects the actual sender, as
    deduced from the header (X-Envelope-Sender, Sender, From, in that order)
    and the date given with the APPEND command (or current date/time, if not
    given).
    * imap4d/tests/append00.at: Reflect changes in the envelope.
    * imap4d/tests/append01.at: Likewise.
    * imap4d/io.c (io_format_completion_response): Call imap4d_sync to
    emit eventual non-tagged responses before the tagged one.
    
    * include/mailutils/envelope.h (mu_envelope_set_destroy): New proto.
    * include/mailutils/header.h (MU_HEADER_SENDER): Remove duplicate define.
    * include/mailutils/imap.h (mu_imap_mailbox_create)
    (mu_imap_append_stream_size,mu_imap_append_stream)
    (mu_imap_append_message,mu_imapio_send_flags)
    (mu_imapio_send_time): New protos.
    * include/mailutils/imapio.h (mu_imapio_send_literal): Remove proto.
    (mu_imapio_send_literal_string)
    (mu_imapio_send_literal_stream): New protos.
    * include/mailutils/message.h
    (mu_message_from_stream_with_envelope): New proto.
    * include/mailutils/sys/imap.h (MU_IMAP_CLIENT_APPEND_RX): New state.
    * include/mailutils/sys/message_stream.h (_mu_message_stream)
    <envelope>: Rename to envelope_string.
    <construct_envelope>: New member.
    
    * libmailutils/imapio/literal.c: Remove.
    * libmailutils/imapio/litstream.c: New file.
    * libmailutils/imapio/litstring.c: New file.
    * libmailutils/imapio/time.c: New file.
    * libmailutils/imapio/Makefile.am: Add new files.
    * libmailutils/imapio/flags.c (mu_imapio_send_flags): New function.
    * libmailutils/imapio/qstring.c (mu_imapio_send_qstring_unfold): Use
    mu_imapio_send_literal_string.
    
    * libmailutils/mailbox/envelope.c (mu_envelope_set_destroy): New
    function.
    * libmailutils/stream/message_stream.c
    (mu_message_from_stream_with_envelope): New function.
    (mu_stream_to_message): Rewrite as an alternative entry point to the
    above.
    
    * libproto/imap/Makefile.am: Add new files.
    * libproto/imap/appmsg.c: New file.
    * libproto/imap/appstr.c: New file.
    * libproto/imap/appstrsiz.c: New file.
    * libproto/imap/mbcreate.c: New file.
    
    * mu/imap.c: Implement create and append.

commit 5357fe739bdec1b99abff548098decd4e8c6c5ef
Merge: fae5d5c aa0debf
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 7 19:31:14 2011 +0200

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

commit fae5d5cd60e96989115706ff07e7f7e8134a8185
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 6 21:37:10 2011 +0200

    Minor change

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

Summary of changes:
 imap4d/append.c                                |  166 +++++++++++++++---------
 imap4d/io.c                                    |    2 +
 imap4d/tests/append00.at                       |    2 +-
 imap4d/tests/append01.at                       |    2 +-
 include/mailutils/envelope.h                   |    3 +
 include/mailutils/header.h                     |    1 -
 include/mailutils/imap.h                       |   12 ++
 include/mailutils/imapio.h                     |   10 ++-
 include/mailutils/message.h                    |    3 +
 include/mailutils/sys/imap.h                   |    1 +
 include/mailutils/sys/message_stream.h         |    3 +-
 libmailutils/base/date.c                       |   13 --
 libmailutils/imapio/Makefile.am                |    4 +-
 libmailutils/imapio/flags.c                    |   14 ++
 libmailutils/imapio/{literal.c => litstream.c} |    9 +-
 libmailutils/imapio/{literal.c => litstring.c} |    4 +-
 libmailutils/imapio/qstring.c                  |    2 +-
 libmailutils/imapio/{send.c => time.c}         |   15 +-
 libmailutils/mailbox/envelope.c                |   13 ++
 libmailutils/stream/message_stream.c           |  144 ++++++++++++---------
 libproto/imap/Makefile.am                      |    4 +
 libproto/imap/{delete.c => appmsg.c}           |   36 +++---
 libproto/imap/{delete.c => appstr.c}           |   33 +++---
 libproto/imap/appstrsiz.c                      |  148 +++++++++++++++++++++
 libproto/imap/{delete.c => mbcreate.c}         |    4 +-
 mu/imap.c                                      |   95 ++++++++++++++
 26 files changed, 548 insertions(+), 195 deletions(-)
 copy libmailutils/imapio/{literal.c => litstream.c} (83%)
 rename libmailutils/imapio/{literal.c => litstring.c} (89%)
 copy libmailutils/imapio/{send.c => time.c} (75%)
 copy libproto/imap/{delete.c => appmsg.c} (68%)
 copy libproto/imap/{delete.c => appstr.c} (70%)
 create mode 100644 libproto/imap/appstrsiz.c
 copy libproto/imap/{delete.c => mbcreate.c} (93%)

diff --git a/imap4d/append.c b/imap4d/append.c
index ed01200..b8c9198 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -17,46 +17,68 @@
 
 #include "imap4d.h"
 
+struct _temp_envelope
+{
+  struct tm tm;
+  mu_timezone tz;
+  char *sender;
+};
+
 static int
-_append_date (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
+_temp_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
+		     size_t *pnwrite)
 {
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  size_t size;
+  struct _temp_envelope *tenv = mu_envelope_get_owner (envelope);
+  int rc;
+  mu_stream_t str;
+  mu_stream_stat_buffer stat;
+  
   if (!buf)
-    size = MU_ENVELOPE_DATE_LENGTH;
+    {
+      if (!pnwrite)
+	return MU_ERR_OUT_PTR_NULL;
+      
+      rc = mu_nullstream_create (&str, MU_STREAM_WRITE);
+    }
   else
+    rc = mu_fixed_memory_stream_create (&str, buf, len, MU_STREAM_WRITE);
+
+  if (rc)
+    return rc;
+  mu_stream_set_stat (str, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT), stat);
+      
+  rc = mu_c_streamftime (str, MU_DATETIME_FROM, &tenv->tm, &tenv->tz);
+  if (rc == 0)
     {
-      struct tm **tm = mu_message_get_owner (msg);
-      size = mu_strftime (buf, len, "%a %b %d %H:%M:%S %Y", *tm);
+      mu_stream_flush (str);
+      if (pnwrite)
+	*pnwrite = stat[MU_STREAM_STAT_OUT];
+      rc = mu_stream_write (str, "", 1, NULL);
     }
-  if (pnwrite)
-    *pnwrite = size;
+  mu_stream_unref (str);
+  
+  if (rc)
+    return rc;
   return 0;
 }
 
 static int
-_append_sender (mu_envelope_t envelope, char *buf, size_t len, size_t *pnwrite)
+_temp_envelope_sender (mu_envelope_t envelope, char *buf, size_t len,
+		       size_t *pnwrite)
 {
-  size_t n = mu_cpystr (buf, "GNU-imap4d", len);
+  struct _temp_envelope *tenv = mu_envelope_get_owner (envelope);
+  size_t n = mu_cpystr (buf, tenv->sender, len);
   if (pnwrite)
-    *pnwrite = n;
+    *pnwrite = n;       
   return 0;
 }
 
-/* FIXME: Why not use mu_message_size instead? */
 static int
-_append_size (mu_message_t msg, size_t *psize)
+_temp_envelope_destroy (mu_envelope_t envelope)
 {
-  mu_stream_t str;
-  int status = mu_message_get_stream (msg, &str);
-  if (status == 0)
-    {
-      mu_off_t size;
-      status = mu_stream_size (str, &size);
-      if (status == 0 && psize)
-	*psize = size;
-    }
-  return status;
+  struct _temp_envelope *tenv = mu_envelope_get_owner (envelope);
+  free (tenv->sender);
+  return 0;
 }
 
 int
@@ -66,65 +88,84 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
   mu_stream_t stream;
   int rc = 0;
   mu_message_t msg = 0;
-  struct tm *tm;
-  time_t t;
-  mu_envelope_t env;
+  mu_envelope_t env = NULL;
   size_t size;
-  
-  if (mu_message_create (&msg, &tm))
-    return 1;
+  struct _temp_envelope tenv;
+
+  memset (&tenv, 0, sizeof (tenv));
+	  
+  text = mu_str_skip_class (text, MU_CTYPE_BLANK);
+
+  size = strlen (text);
+  rc = quota_check (size);
+  if (rc != RESP_OK)
+    {
+      *err_text = rc == RESP_NO ?
+	                   "Mailbox quota exceeded" : "Operation failed";
+      return 1;
+    }
 
   /* If a date_time is specified, the internal date SHOULD be set in the
      resulting message; otherwise, the internal date of the resulting
      message is set to the current date and time by default. */
   if (date_time)
     {
-      if (util_parse_internal_date (date_time, &t, datetime_default))
+      if (mu_scan_datetime (date_time, MU_DATETIME_INTERNALDATE, &tenv.tm,
+			    &tenv.tz, NULL))
 	{
 	  *err_text = "Invalid date/time format";
 	  return 1;
 	}
+      rc = mu_envelope_create (&env, &tenv);
+      if (rc)
+	return rc;
+      mu_envelope_set_date (env, _temp_envelope_date, &tenv);
+      mu_envelope_set_sender (env, _temp_envelope_sender, &tenv);
+      mu_envelope_set_destroy (env, _temp_envelope_destroy, &tenv);
     }
-  else
-    time (&t);
-  
-  tm = gmtime (&t);
 
-  text = mu_str_skip_class (text, MU_CTYPE_BLANK);
-
-  if (mu_static_memory_stream_create (&stream, text, strlen (text)))
+  if (mu_static_memory_stream_create (&stream, text, size))
     {
-      mu_message_destroy (&msg, &tm);
+      if (env)
+	mu_envelope_destroy (&env, mu_envelope_get_owner (env));
       return 1;
     }
+  
+  rc = mu_message_from_stream_with_envelope (&msg, stream, env);
+  mu_stream_unref (stream);
 
-  mu_message_set_stream (msg, stream, &tm);
-  mu_message_set_size (msg, _append_size, &tm);
-
-  mu_envelope_create (&env, msg);
-  mu_envelope_set_date (env, _append_date, msg);
-  mu_envelope_set_sender (env, _append_sender, msg);
-  mu_message_set_envelope (msg, env, &tm);
-
-  rc = _append_size (msg, &size);
   if (rc)
     {
-      mu_diag_output (MU_DIAG_NOTICE,
-		      _("cannot compute size of the message being appended; "
-			"using estimated value: %s"),
-		      mu_strerror (rc));
-      /* raw estimate */
-      size = strlen (text);
-    }
-  rc = quota_check (size);
-  if (rc != RESP_OK)
-    {
-      *err_text = rc == RESP_NO ?
-	                   "Mailbox quota exceeded" : "Operation failed";
-      mu_message_destroy (&msg, &tm);
+      if (env)
+	mu_envelope_destroy (&env, mu_envelope_get_owner (env));
       return 1;
     }
 
+  if (env)
+    {
+      /* Restore sender */
+      mu_header_t hdr = NULL;
+      char *val;
+      
+      mu_message_get_header (msg, &hdr);
+      if (mu_header_aget_value_unfold (hdr, MU_HEADER_ENV_SENDER, &val) == 0 ||
+	  mu_header_aget_value_unfold (hdr, MU_HEADER_SENDER, &val) == 0 ||
+	  mu_header_aget_value_unfold (hdr, MU_HEADER_FROM, &val) == 0)
+	{
+	  mu_address_t addr;
+	  rc = mu_address_create (&addr, val);
+	  free (val);
+	  if (rc == 0)
+	    {
+	      mu_address_aget_email (addr, 1, &tenv.sender);
+	      mu_address_destroy (&addr);
+	    }
+	}
+
+      if (!tenv.sender)
+	tenv.sender = strdup ("GNU-imap4d");
+    }
+      
   imap4d_enter_critical ();
   rc = mu_mailbox_append_message (mbox, msg);
   if (rc == 0)
@@ -143,7 +184,10 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
     }
   imap4d_leave_critical ();
   
-  mu_message_destroy (&msg, &tm);
+  mu_message_unref (msg);
+  if (env)
+    mu_envelope_destroy (&env, mu_envelope_get_owner (env));
+
   return rc;
 }
 
diff --git a/imap4d/io.c b/imap4d/io.c
index c622162..7a0ffeb 100644
--- a/imap4d/io.c
+++ b/imap4d/io.c
@@ -235,6 +235,8 @@ io_format_completion_response (mu_stream_t str,
   int status = 0;
   const char *sc = sc2string (rc);
 
+  imap4d_sync ();
+  
   mu_stream_printf (str, "%s %s%s ",
 		    command->tag, sc, command->name);
   mu_stream_vprintf (str, format, ap);
diff --git a/imap4d/tests/append00.at b/imap4d/tests/append00.at
index 2e02c0c..894113f 100644
--- a/imap4d/tests/append00.at
+++ b/imap4d/tests/append00.at
@@ -44,7 +44,7 @@ awk 'NR==1 {print "1:",$1,$2; next} NF==0 {print NR":"; next} {print NR":",$0}'
 * BYE Session terminating.
 X OK LOGOUT Completed
 ==
-1: From GNU-imap4d
+1: From foobar@Blurdybloop.COM
 2: Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
 3: From: Fred Foobar <foobar@Blurdybloop.COM>
 4: Subject: afternoon meeting
diff --git a/imap4d/tests/append01.at b/imap4d/tests/append01.at
index 9a6e347..dcb2587 100644
--- a/imap4d/tests/append01.at
+++ b/imap4d/tests/append01.at
@@ -44,7 +44,7 @@ awk 'NF==0 {print NR":"; next} {print NR":",$0}' mbox
 * BYE Session terminating.
 X OK LOGOUT Completed
 ==
-1: From GNU-imap4d Sun Aug 25 16:00:00 2002
+1: From foobar@Blurdybloop.COM Sun Aug 25 18:00:00 2002
 2: Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
 3: From: Fred Foobar <foobar@Blurdybloop.COM>
 4: Subject: afternoon meeting again
diff --git a/include/mailutils/envelope.h b/include/mailutils/envelope.h
index 8b8b42e..246cfd5 100644
--- a/include/mailutils/envelope.h
+++ b/include/mailutils/envelope.h
@@ -47,6 +47,9 @@ int mu_envelope_set_date (mu_envelope_t,
 			  int (*_date) (mu_envelope_t, char *, size_t ,
 					size_t *), 
 			  void *);
+int mu_envelope_set_destroy (mu_envelope_t envelope,
+			     int (*_destroy) (mu_envelope_t),
+			     void *owner);
 
 /* mu_strftime format for envelope dates */
 #define MU_ENVELOPE_DATE_FORMAT "%a %b %d %H:%M:%S %Y"
diff --git a/include/mailutils/header.h b/include/mailutils/header.h
index eafb06d..ed13ab3 100644
--- a/include/mailutils/header.h
+++ b/include/mailutils/header.h
@@ -34,7 +34,6 @@ extern "C" {
 #define MU_HEADER_SENDER                    "Sender"
 #define MU_HEADER_RESENT_FROM               "Resent-From"
 #define MU_HEADER_SUBJECT                   "Subject"
-#define MU_HEADER_SENDER                    "Sender"
 #define MU_HEADER_RESENT_SENDER             "Resent-Sender"
 #define MU_HEADER_TO                        "To"
 #define MU_HEADER_RESENT_TO                 "Resent-To"
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 7a77ac3..b3cf521 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -87,6 +87,18 @@ int mu_imap_close (mu_imap_t imap);
 int mu_imap_unselect (mu_imap_t imap);
 
 int mu_imap_expunge (mu_imap_t imap);
+
+int mu_imap_mailbox_create (mu_imap_t imap, const char *mailbox);
+
+int mu_imap_append_stream_size (mu_imap_t imap, const char *mailbox, int flags,
+				struct tm *tm, struct mu_timezone *tz,
+				mu_stream_t stream, mu_off_t size);
+int mu_imap_append_stream (mu_imap_t imap, const char *mailbox, int flags,
+			   struct tm *tm, struct mu_timezone *tz,
+			   mu_stream_t stream);
+int mu_imap_append_message (mu_imap_t imap, const char *mailbox, int flags,
+			    struct tm *tm, struct mu_timezone *tz,
+			    mu_message_t msg);
   
 int mu_imap_set_carrier (mu_imap_t imap, mu_stream_t carrier);
 int mu_imap_get_carrier (mu_imap_t imap, mu_stream_t *pcarrier);
diff --git a/include/mailutils/imapio.h b/include/mailutils/imapio.h
index 0ae7fb2..3a4b1e3 100644
--- a/include/mailutils/imapio.h
+++ b/include/mailutils/imapio.h
@@ -22,6 +22,8 @@ extern "C" {
 #endif
 
 # include <mailutils/types.h>
+# include <mailutils/util.h>  
+# include <time.h>
 
 #define MU_IMAPIO_CLIENT 0
 #define MU_IMAPIO_SERVER 1
@@ -36,11 +38,17 @@ int mu_imapio_get_words (mu_imapio_t io, size_t *pwc, char ***pwv);
 
 int mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes);
 int mu_imapio_printf (mu_imapio_t io, const char *fmt, ...);
-int mu_imapio_send_literal (struct _mu_imapio *io, const char *buffer);
+int mu_imapio_send_literal_string (struct _mu_imapio *io, const char *buffer);
+int mu_imapio_send_literal_stream (struct _mu_imapio *io, mu_stream_t stream,
+				   mu_off_t size);  
 int mu_imapio_send_qstring (struct _mu_imapio *io, const char *buffer);
 int mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
 				   int unfold);
 
+int mu_imapio_send_flags (struct _mu_imapio *io, int flags);
+int mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
+			 struct mu_timezone *tz);
+  
 int mu_imapio_trace_enable (mu_imapio_t io);
 int mu_imapio_trace_disable (mu_imapio_t io);
 int mu_imapio_get_trace (mu_imapio_t io);
diff --git a/include/mailutils/message.h b/include/mailutils/message.h
index 79878ac..b448dda 100644
--- a/include/mailutils/message.h
+++ b/include/mailutils/message.h
@@ -196,6 +196,9 @@ extern int mu_message_save_to_mailbox (mu_message_t msg, const char *toname,
 				       int perms);
 
 
+extern int mu_message_from_stream_with_envelope (mu_message_t *pmsg,
+						 mu_stream_t instream,
+						 mu_envelope_t env);
 extern int mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg);
 
   
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index adb258b..05ec9db 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -65,6 +65,7 @@ enum mu_imap_client_state
     MU_IMAP_CLIENT_CHECK_RX,
     MU_IMAP_CLIENT_COPY_RX,
     MU_IMAP_CLIENT_EXPUNGE_RX,
+    MU_IMAP_CLIENT_APPEND_RX,
     MU_IMAP_CLIENT_CLOSING
   };
 
diff --git a/include/mailutils/sys/message_stream.h b/include/mailutils/sys/message_stream.h
index af54cbd..d72b891 100644
--- a/include/mailutils/sys/message_stream.h
+++ b/include/mailutils/sys/message_stream.h
@@ -25,7 +25,8 @@ struct _mu_message_stream
   mu_stream_t transport;  /* Actual stream */
   mu_off_t offset;
 
-  char *envelope;
+  int construct_envelope;
+  char *envelope_string;
   size_t envelope_length; 
   char *from;
   char *date;
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
index 543efab..b5c157b 100644
--- a/libmailutils/base/date.c
+++ b/libmailutils/base/date.c
@@ -674,19 +674,6 @@ peek_state (mu_list_t list, int *state, const char **input)
 }      
 
 static int
-change_top_input (mu_list_t list, const char *input)
-{
-  int rc;
-  struct save_input *inp;
-
-  rc = mu_list_tail (list, (void**)&inp);
-  if (rc)
-    return rc;
-  inp->input = input;
-  return 0;
-}
-
-static int
 pop_input (mu_list_t list, int *state, const char **input)
 {
   int rc;
diff --git a/libmailutils/imapio/Makefile.am b/libmailutils/imapio/Makefile.am
index c856d9d..d3ce8a1 100644
--- a/libmailutils/imapio/Makefile.am
+++ b/libmailutils/imapio/Makefile.am
@@ -21,11 +21,13 @@ libimapio_la_SOURCES = \
  create.c\
  flags.c\
  getline.c\
- literal.c\
+ litstream.c\
+ litstring.c\
  printf.c\
  qstring.c\
  replstr.c\
  send.c\
+ time.c\
  trace.c\
  words.c\
  xscript.c
diff --git a/libmailutils/imapio/flags.c b/libmailutils/imapio/flags.c
index 5d07297..ed20726 100644
--- a/libmailutils/imapio/flags.c
+++ b/libmailutils/imapio/flags.c
@@ -83,3 +83,17 @@ mu_imap_format_flags (mu_stream_t str, int flags)
   
   return 0;
 }
+
+int
+mu_imapio_send_flags (struct _mu_imapio *io, int flags)
+{
+  int rc;
+
+  rc = mu_stream_write (io->_imap_stream, "(", 1, NULL);
+  if (rc)
+    return rc;
+  rc = mu_imap_format_flags (io->_imap_stream, flags);
+  if (rc == 0)
+    rc = mu_stream_write (io->_imap_stream, ")", 1, NULL);
+  return rc;
+}
diff --git a/libmailutils/imapio/literal.c b/libmailutils/imapio/litstream.c
similarity index 83%
copy from libmailutils/imapio/literal.c
copy to libmailutils/imapio/litstream.c
index 5908dd8..f4d2c9d 100644
--- a/libmailutils/imapio/literal.c
+++ b/libmailutils/imapio/litstream.c
@@ -24,11 +24,10 @@
 #include <mailutils/stream.h>
 
 int
-mu_imapio_send_literal (struct _mu_imapio *io, const char *buffer)
+mu_imapio_send_literal_stream (struct _mu_imapio *io, mu_stream_t stream,
+			       mu_off_t len)
 {
-  size_t len = strlen (buffer);
-
-  mu_stream_printf (io->_imap_stream, "{%lu}\n", (unsigned long) len);
+  mu_stream_printf (io->_imap_stream, "{%lu}\r\n", (unsigned long) len);
 
   if (!io->_imap_server)
     {
@@ -39,5 +38,5 @@ mu_imapio_send_literal (struct _mu_imapio *io, const char *buffer)
 	return MU_ERR_BADREPLY;
     }
 
-  return mu_stream_write (io->_imap_stream, buffer, len, NULL);
+  return mu_stream_copy (io->_imap_stream, stream, len, NULL);
 }
diff --git a/libmailutils/imapio/literal.c b/libmailutils/imapio/litstring.c
similarity index 89%
rename from libmailutils/imapio/literal.c
rename to libmailutils/imapio/litstring.c
index 5908dd8..5ccd5fa 100644
--- a/libmailutils/imapio/literal.c
+++ b/libmailutils/imapio/litstring.c
@@ -24,11 +24,11 @@
 #include <mailutils/stream.h>
 
 int
-mu_imapio_send_literal (struct _mu_imapio *io, const char *buffer)
+mu_imapio_send_literal_string (struct _mu_imapio *io, const char *buffer)
 {
   size_t len = strlen (buffer);
 
-  mu_stream_printf (io->_imap_stream, "{%lu}\n", (unsigned long) len);
+  mu_stream_printf (io->_imap_stream, "{%lu}\r\n", (unsigned long) len);
 
   if (!io->_imap_server)
     {
diff --git a/libmailutils/imapio/qstring.c b/libmailutils/imapio/qstring.c
index f50c15c..e4e7ea5 100644
--- a/libmailutils/imapio/qstring.c
+++ b/libmailutils/imapio/qstring.c
@@ -60,7 +60,7 @@ mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
 	    }
 	}
       else
-	return mu_imapio_send_literal (io, buffer);
+	return mu_imapio_send_literal_string (io, buffer);
     }
   return mu_imapio_printf (io, "\"%s\"", buffer);
 }
diff --git a/libmailutils/imapio/send.c b/libmailutils/imapio/time.c
similarity index 75%
copy from libmailutils/imapio/send.c
copy to libmailutils/imapio/time.c
index 11ee049..02f0d8a 100644
--- a/libmailutils/imapio/send.c
+++ b/libmailutils/imapio/time.c
@@ -15,15 +15,16 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdarg.h>
-#include <mailutils/types.h>
-#include <mailutils/imapio.h>
+#include <stdlib.h>
 #include <mailutils/stream.h>
+#include <mailutils/errno.h>
 #include <mailutils/sys/imapio.h>
+#include <mailutils/util.h>
 
 int
-mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes)
+mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
+		     struct mu_timezone *tz)
 {
-  return mu_stream_write (io->_imap_stream, buf, bytes, NULL);
-}
-    
+  return mu_c_streamftime (io->_imap_stream,
+			   "\"" MU_DATETIME_INTERNALDATE "\"", tm, tz);
+}  
diff --git a/libmailutils/mailbox/envelope.c b/libmailutils/mailbox/envelope.c
index e324d74..5ab6de0 100644
--- a/libmailutils/mailbox/envelope.c
+++ b/libmailutils/mailbox/envelope.c
@@ -91,6 +91,19 @@ mu_envelope_set_date (mu_envelope_t envelope,
   return 0;
 }
 
+int
+mu_envelope_set_destroy (mu_envelope_t envelope,
+			 int (*_destroy) (mu_envelope_t),
+			 void *owner)
+{
+  if (envelope == NULL)
+    return EINVAL;
+  if (envelope->owner != owner)
+    return EACCES;
+  envelope->_destroy = _destroy;
+  return 0;
+}
+
 
 /* General accessors: */
 #define AC2(a,b) a ## b
diff --git a/libmailutils/stream/message_stream.c b/libmailutils/stream/message_stream.c
index a4d9033..4bbbe9e 100644
--- a/libmailutils/stream/message_stream.c
+++ b/libmailutils/stream/message_stream.c
@@ -187,30 +187,33 @@ _message_open (mu_stream_t stream)
     {
       if (offset == 0 && memcmp (buffer, "From ", 5) == 0)
 	{
-	  char *s, *p;
-	  
 	  str->envelope_length = len;
-	  str->envelope = mu_strdup (buffer);
-	  if (!str->envelope)
-	    return ENOMEM;
-	  str->envelope[len - 1] = 0;
-
-	  s = str->envelope + 5;
-	  p = strchr (s, ' ');
-
-	  if (p)
+	  if (str->construct_envelope)
 	    {
-	      size_t n = p - s;
-	      env_from = mu_alloc (n + 1);
-	      if (!env_from)
+	      char *s, *p;
+	  
+	      str->envelope_string = mu_strdup (buffer);
+	      if (!str->envelope_string)
 		return ENOMEM;
-	      memcpy (env_from, s, n);
-	      env_from[n] = 0;
-	      env_date = mu_strdup (p + 1);
-	      if (!env_date)
+	      str->envelope_string[len - 1] = 0;
+	      
+	      s = str->envelope_string + 5;
+	      p = strchr (s, ' ');
+	      
+	      if (p)
 		{
-		  free (env_from);
-		  return ENOMEM;
+		  size_t n = p - s;
+		  env_from = mu_alloc (n + 1);
+		  if (!env_from)
+		    return ENOMEM;
+		  memcpy (env_from, s, n);
+		  env_from[n] = 0;
+		  env_date = mu_strdup (p + 1);
+		  if (!env_date)
+		    {
+		      free (env_from);
+		      return ENOMEM;
+		    }
 		}
 	    }
 	}
@@ -233,7 +236,7 @@ _message_open (mu_stream_t stream)
 	      return MU_ERR_INVALID_EMAIL;
 	    }
 	  has_headers = 1;
-	  if (!env_from || !env_date)
+	  if (str->construct_envelope && (!env_from || !env_date))
 	    {
 	      if (!from && mu_c_strncasecmp (buffer, MU_HEADER_FROM,
 					     sizeof (MU_HEADER_FROM) - 1) == 0)
@@ -263,40 +266,43 @@ _message_open (mu_stream_t stream)
   if (rc)
     return rc;
   
-  if (!env_from)
+  if (str->construct_envelope)
     {
-      if (from)
+      if (!env_from)
 	{
-	  mu_address_t addr;
-	  
-	  mu_address_create (&addr, from);
-	  if (addr)
+	  if (from)
 	    {
-	      mu_address_aget_email (addr, 1, &env_from);
-	      mu_address_destroy (&addr);
+	      mu_address_t addr;
+	      
+	      mu_address_create (&addr, from);
+	      if (addr)
+		{
+		  mu_address_aget_email (addr, 1, &env_from);
+		  mu_address_destroy (&addr);
+		}
 	    }
-	}
 
-      if (!env_from)
-	env_from = mu_get_user_email (NULL);
+	  if (!env_from)
+	    env_from = mu_get_user_email (NULL);
+	}
+      free (from);
+      
+      if (!env_date)
+	{
+	  struct tm *tm;
+	  time_t t;
+	  char date[80]; /* FIXME: This size is way too big */
+	  
+	  time(&t);
+	  tm = gmtime(&t);
+	  mu_strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y", tm);
+	  env_date = strdup (date);
+	}
+      
+      str->from = env_from;
+      str->date = env_date;
     }
-  free (from);
   
-  if (!env_date)
-    {
-      struct tm *tm;
-      time_t t;
-      char date[80]; /* FIXME: This size is way too big */
-
-      time(&t);
-      tm = gmtime(&t);
-      mu_strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y", tm);
-      env_date = strdup (date);
-    }
-
-  str->from = env_from;
-  str->date = env_date;
-
   str->body_start = body_start;
   str->body_end = body_end - 1;
   
@@ -315,7 +321,7 @@ _message_done (mu_stream_t stream)
 {
   struct _mu_message_stream *s = (struct _mu_message_stream*) stream;
 
-  free (s->envelope);
+  free (s->envelope_string);
   free (s->date);
   free (s->from);
   mu_stream_destroy (&s->transport);
@@ -342,8 +348,9 @@ _message_error_string (struct _mu_stream *stream, int rc)
   return mu_stream_strerror (str->transport, rc);
 }
 
-int
-mu_message_stream_create (mu_stream_t *pstream, mu_stream_t src, int flags)
+static int
+mu_message_stream_create (mu_stream_t *pstream, mu_stream_t src, int flags,
+			  int construct_envelope)
 {
   struct _mu_message_stream *s;
   int sflag;
@@ -368,6 +375,7 @@ mu_message_stream_create (mu_stream_t *pstream, mu_stream_t src, int flags)
       free (s);
       return rc;
     }
+  s->construct_envelope = construct_envelope;
   s->stream.open = _message_open;
   s->stream.close = _message_close;
   s->stream.done = _message_done;
@@ -400,13 +408,12 @@ _body_obj_size (mu_body_t body, size_t *size)
     *size = str->body_end - str->body_start + 1;
   return 0;
 }
-
-
 
 int
-mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
+mu_message_from_stream_with_envelope (mu_message_t *pmsg,
+				      mu_stream_t instream,
+				      mu_envelope_t env)
 {
-  mu_envelope_t env;
   mu_message_t msg;
   mu_body_t body;
   mu_stream_t bstream;
@@ -415,7 +422,7 @@ mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
   struct _mu_message_stream *sp;
   
   /* FIXME: Perhaps MU_STREAM_NO_CLOSE is needed */
-  if ((rc = mu_message_stream_create (&draftstream, instream, 0)))
+  if ((rc = mu_message_stream_create (&draftstream, instream, 0, !env)))
     return rc;
 
   if ((rc = mu_message_create (&msg, draftstream)))
@@ -425,16 +432,19 @@ mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
     }
   
   mu_message_set_stream (msg, draftstream, draftstream);
-  
-  if ((rc = mu_envelope_create (&env, draftstream)))
+
+  if (!env)
     {
-      mu_message_destroy (&msg, draftstream);
-      mu_stream_destroy (&draftstream);
-      return rc;
-    }
+      if ((rc = mu_envelope_create (&env, draftstream)))
+	{
+	  mu_message_destroy (&msg, draftstream);
+	  mu_stream_destroy (&draftstream);
+	  return rc;
+	}
   
-  mu_envelope_set_date (env, _env_msg_date, draftstream);
-  mu_envelope_set_sender (env, _env_msg_sender, draftstream);
+      mu_envelope_set_date (env, _env_msg_date, draftstream);
+      mu_envelope_set_sender (env, _env_msg_sender, draftstream);
+    }
   mu_message_set_envelope (msg, env, draftstream);
 
   mu_body_create (&body, msg);
@@ -457,3 +467,9 @@ mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
   *pmsg = msg;
   return 0;
 }
+
+int
+mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg)
+{
+  return mu_message_from_stream_with_envelope (pmsg, instream, NULL);
+}
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index 224dc26..41d023d 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -27,6 +27,9 @@ libmu_imap_la_LIBADD = ${MU_LIB_AUTH} ${MU_LIB_MAILUTILS} @INTLLIBS@
 #  mbox.c\
 #  url.c
 libmu_imap_la_SOURCES = \
+ appmsg.c\
+ appstr.c\
+ appstrsiz.c\
  fake-folder.c\
  fetch.c\
  gencom.c\
@@ -47,6 +50,7 @@ libmu_imap_la_SOURCES = \
  id.c\
  login.c\
  logout.c\
+ mbcreate.c\
  noop.c\
  rename.c\
  resplist.c\
diff --git a/libproto/imap/delete.c b/libproto/imap/appmsg.c
similarity index 68%
copy from libproto/imap/delete.c
copy to libproto/imap/appmsg.c
index e29f028..b5f0a1b 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/appmsg.c
@@ -20,28 +20,30 @@
 #endif
 
 #include <stdlib.h>
-#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/message.h>
+#include <mailutils/stream.h>
 #include <mailutils/errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_delete (mu_imap_t imap, const char *mailbox)
+mu_imap_append_message (mu_imap_t imap, const char *mailbox, int flags,
+			struct tm *tm, struct mu_timezone *tz,
+			mu_message_t msg)
 {
-  char const *argv[2];
-  static struct imap_command com;
-
-  argv[0] = "DELETE";
-  argv[1] = mailbox;
-
-  com.session_state = MU_IMAP_SESSION_AUTH;
-  com.capa = NULL;
-  com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
-  com.argc = 2;
-  com.argv = argv;
-  com.handler = NULL;
-
-  return mu_imap_gencom (imap, &com);
+  mu_stream_t str;
+  int rc;
+
+  rc = mu_message_get_streamref (msg, &str);
+  if (rc)
+    {
+      rc = mu_imap_append_stream (imap, mailbox, flags, tm, tz, str);
+      mu_stream_unref (str);
+    }
+  return rc;
 }
+
+      
       
+
diff --git a/libproto/imap/delete.c b/libproto/imap/appstr.c
similarity index 70%
copy from libproto/imap/delete.c
copy to libproto/imap/appstr.c
index e29f028..6ec690e 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/appstr.c
@@ -20,28 +20,27 @@
 #endif
 
 #include <stdlib.h>
-#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/stream.h>
 #include <mailutils/errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_delete (mu_imap_t imap, const char *mailbox)
+mu_imap_append_stream (mu_imap_t imap, const char *mailbox, int flags,
+		       struct tm *tm, struct mu_timezone *tz,
+		       mu_stream_t stream)
 {
-  char const *argv[2];
-  static struct imap_command com;
-
-  argv[0] = "DELETE";
-  argv[1] = mailbox;
-
-  com.session_state = MU_IMAP_SESSION_AUTH;
-  com.capa = NULL;
-  com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
-  com.argc = 2;
-  com.argv = argv;
-  com.handler = NULL;
-
-  return mu_imap_gencom (imap, &com);
+  mu_off_t size;
+  int rc;
+
+  rc = mu_stream_size (stream, &size);
+  if (rc == 0)
+    rc = mu_imap_append_stream_size (imap, mailbox, flags, tm, tz, stream,
+				     size);
+  return rc;
 }
+
+      
       
+
diff --git a/libproto/imap/appstrsiz.c b/libproto/imap/appstrsiz.c
new file mode 100644
index 0000000..d42b616
--- /dev/null
+++ b/libproto/imap/appstrsiz.c
@@ -0,0 +1,148 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/stream.h>
+#include <mailutils/filter.h>
+#include <mailutils/errno.h>
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
+
+static int
+get_crlf_stream_size (mu_stream_t str, mu_off_t size, mu_off_t *prealsize)
+{
+  mu_stream_t null, flt;
+  mu_stream_stat_buffer stat;
+  int rc;
+  mu_off_t needle;
+  
+  rc = mu_nullstream_create (&null, MU_STREAM_WRITE);
+  if (rc)
+    return rc;
+  mu_stream_set_stat (null, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT), stat);
+  rc = mu_filter_create (&flt, null, "CRLF", MU_FILTER_ENCODE,
+			 MU_STREAM_WRITE);
+  mu_stream_unref (null);
+  if (rc)
+    return rc;
+  rc = mu_stream_seek (str, 0, MU_SEEK_CUR, &needle);
+  if (rc)
+    return rc;
+  rc = mu_stream_copy (flt, str, size, NULL);
+  if (rc == 0)
+    rc = mu_stream_seek (str, 0, MU_SEEK_SET, NULL);
+  mu_stream_unref (flt);
+  if (rc == 0)
+    *prealsize = stat[MU_STREAM_STAT_OUT];
+  return rc;
+}
+
+int
+mu_imap_append_stream_size (mu_imap_t imap, const char *mailbox, int flags,
+			    struct tm *tm, struct mu_timezone *tz,
+			    mu_stream_t stream, mu_off_t size)
+{
+  int status;
+  mu_off_t realsize;
+  
+  if (imap == NULL || !mailbox || !stream || size == 0)
+    return EINVAL;
+  if (!imap->io)
+    return MU_ERR_NO_TRANSPORT;
+  if (imap->session_state < MU_IMAP_SESSION_AUTH)
+    return MU_ERR_SEQ;
+
+  switch (imap->client_state)
+    {
+    case MU_IMAP_CLIENT_READY:
+      status = get_crlf_stream_size (stream, size, &realsize);
+      if (status)
+	return status;
+      
+      status = _mu_imap_tag_next (imap);
+      MU_IMAP_CHECK_EAGAIN (imap, status);
+      status = mu_imapio_printf (imap->io, "%s APPEND %s",
+				 imap->tag_str, mailbox);
+      MU_IMAP_CHECK_ERROR (imap, status);
+      if (flags)
+	{
+	  status = mu_imapio_send (imap->io, " ", 1);
+	  if (status == 0)
+	    status = mu_imapio_send_flags (imap->io, flags);
+	  MU_IMAP_CHECK_ERROR (imap, status);
+	}
+
+      if (tm)
+	{
+	  status = mu_imapio_send (imap->io, " ", 1);
+	  if (status == 0)
+	    status = mu_imapio_send_time (imap->io, tm, tz);
+	  MU_IMAP_CHECK_ERROR (imap, status);
+	}
+
+      status = mu_imapio_send (imap->io, " ", 1);
+      if (status == 0)
+	{
+	  mu_stream_t flt;
+	  status = mu_filter_create (&flt, stream, "CRLF", MU_FILTER_ENCODE,
+				     MU_STREAM_READ);
+	  if (status == 0)
+	    {
+	      status = mu_imapio_send_literal_stream (imap->io, flt, realsize);
+	      mu_stream_unref (flt);
+	    }
+	}
+      MU_IMAP_CHECK_ERROR (imap, status);
+      status = mu_imapio_send (imap->io, "\r\n", 2);
+      MU_IMAP_CHECK_ERROR (imap, status);
+      MU_IMAP_FCLR (imap, MU_IMAP_RESP);
+      imap->client_state = MU_IMAP_CLIENT_APPEND_RX;
+
+    case MU_IMAP_CLIENT_APPEND_RX:
+      status = _mu_imap_response (imap, NULL, NULL);
+      MU_IMAP_CHECK_EAGAIN (imap, status);
+      switch (imap->resp_code)
+	{
+	case MU_IMAP_OK:
+	  status = 0;
+	  break;
+
+	case MU_IMAP_NO:
+	  status = MU_ERR_FAILURE;
+	  break;
+
+	case MU_IMAP_BAD:
+	  status = MU_ERR_BADREPLY;
+	  break;
+	}
+      imap->client_state = MU_IMAP_CLIENT_READY;
+      break;
+
+    default:
+      status = EINPROGRESS;
+    }
+  return status;
+}
+
+      
+      
+
diff --git a/libproto/imap/delete.c b/libproto/imap/mbcreate.c
similarity index 93%
copy from libproto/imap/delete.c
copy to libproto/imap/mbcreate.c
index e29f028..845d1ba 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/mbcreate.c
@@ -26,12 +26,12 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_delete (mu_imap_t imap, const char *mailbox)
+mu_imap_mailbox_create (mu_imap_t imap, const char *mailbox)
 {
   char const *argv[2];
   static struct imap_command com;
 
-  argv[0] = "DELETE";
+  argv[0] = "CREATE";
   argv[1] = mailbox;
 
   com.session_state = MU_IMAP_SESSION_AUTH;
diff --git a/mu/imap.c b/mu/imap.c
index 5468058..26a3401 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -808,6 +808,93 @@ com_expunge (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
   return 0;
 }
 
+static int
+com_create (int argc, char **argv)
+{
+  int status = mu_imap_mailbox_create (imap, argv[1]);
+  if (status)
+    report_failure ("create", status);
+  return 0;
+}
+
+static int
+com_append (int argc, char **argv)
+{
+  struct tm tmbuf, *tm = NULL;
+  struct mu_timezone tzbuf, *tz = NULL;
+  int flags = 0;
+  mu_stream_t stream;
+  int rc, i;
+  
+  for (i = 1; i < argc; i++)
+    {
+      if (strcmp (argv[i], "-time") == 0)
+	{
+	  char *p;
+	  
+	  if (++i == argc)
+	    {
+	      mu_error (_("-time requires argument"));
+	      return 0;
+	    }
+	  rc = mu_scan_datetime (argv[i], MU_DATETIME_INTERNALDATE,
+				 &tmbuf, &tzbuf, &p);
+	  if (rc || *p)
+	    {
+	      mu_error (_("cannot parse time"));
+	      return 0;
+	    }
+	  tm = &tmbuf;
+	  tz = &tzbuf;
+	}
+      else if (strcmp (argv[i], "-flag") == 0)
+	{
+	  if (++i == argc)
+	    {
+	      mu_error (_("-flag requires argument"));
+	      return 0;
+	    }
+	  if (mu_imap_flag_to_attribute (argv[i], &flags))
+	    {
+	      mu_error (_("unrecognized flag: %s"), argv[i]);
+	      return 0;
+	    }
+	}
+      else if (strcmp (argv[i], "--") == 0)
+	{
+	  i++;
+	  break;
+	}
+      else if (argv[i][0] == '-')
+	{
+	  mu_error (_("unrecognized option: %s"), argv[i]);
+	  return 0;
+	}
+      else
+	break;
+    }
+
+  if (argc - i != 2)
+    {
+      mu_error (_("wrong number of arguments"));
+      return 0;
+    }
+
+  rc = mu_file_stream_create (&stream, argv[i + 1],
+			      MU_STREAM_READ|MU_STREAM_SEEK);
+  if (rc)
+    {
+      mu_error (_("cannot open file %s: %s"), argv[i + 1], mu_strerror (rc));
+      return 0;
+    }
+
+  rc = mu_imap_append_stream (imap, argv[i], flags, tm, tz, stream);
+  mu_stream_unref (stream);
+  if (rc)
+    report_failure ("append", rc);
+  return 0;
+}
+
 struct mutool_command imap_comtab[] = {
   { "capability",   1, -1, 0,
     com_capability,
@@ -887,6 +974,14 @@ struct mutool_command imap_comtab[] = {
     com_expunge,
     NULL,
     N_("permanently remove messages marked for deletion") },
+  { "create",       2, 2, 0,
+    com_create,
+    N_("MAILBOX"),
+    N_("create new mailbox") },
+  { "append",       3, -1, 0,
+    com_append,
+    N_("[-time DATETIME] [-flag FLAG] MAILBOX FILE"),
+    N_("append message text from FILE to MAILBOX") },
   { "quit",         1, 1, 0,
     com_logout,
     NULL,


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 08 11:48:11 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RYh8l-0004hP-0j
	for mharc-commit-mailutils@gnu.org; Thu, 08 Dec 2011 11:48:11 -0500
Received: from eggs.gnu.org ([140.186.70.92]:55781)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYh8c-0004gz-8q
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 11:48:09 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYh8V-0001aC-71
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 11:48:02 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:44629)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYh8V-0001a0-1G
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 11:47:55 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYh8T-0004gv-L3
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 16:47:54 +0000
Date: Thu, 08 Dec 2011 16:47:53 +0000
Message-Id: <E1RYh8T-0004gv-L3@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-509-g7efa477
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 6301266fabab03f0e1dfcab76e7b6fc1e6eda1c6
X-Git-Newrev: 7efa4777bddb68299355dce1f4125533d6467023
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 08 Dec 2011 16:48:09 -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=7efa4777bddb68299355dce1f4125533d6467023

The branch, master has been updated
       via  7efa4777bddb68299355dce1f4125533d6467023 (commit)
       via  acec689ef648adaa5c420049bfb775953e184bda (commit)
       via  5af1d3a7bd8149139ad59baa11501e3d11b138a7 (commit)
      from  6301266fabab03f0e1dfcab76e7b6fc1e6eda1c6 (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 7efa4777bddb68299355dce1f4125533d6467023
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 18:43:21 2011 +0200

    Bugfixes.
    
    * imap4d/append.c: Don't reuse msg variable.
    * po/POTFILES.in: Remove unneeded file.

commit acec689ef648adaa5c420049bfb775953e184bda
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 17:04:29 2011 +0200

    Revamp date/time calculations.
    
    * configure.ac: Build libmailutils/datetime/Makefile.am
    * include/mailutils/datetime.h (mu_datetime_julianday)
    (mu_datetime_dayofweek,mu_datetime_dayofyear)
    (mu_datetime_year_days): New protos.
    * libmailutils/Makefile.am (SUBDIRS): Add datetime
    (libmailutils_la_LIBADD): Add libdatetime.la
    * libmailutils/base/Makefile.am (libbase_la_SOURCES): Remove date.c
    and strftime.c.
    * libmailutils/base/date.c: Remove.
    * libmailutils/base/strftime.c: Move to libmailutils/datetime
    * libmailutils/datetime/Makefile.am: New file.
    * libmailutils/datetime/dow.c: New file.
    * libmailutils/datetime/doy.c: New file.
    * libmailutils/datetime/jd.c: New file.
    * libmailutils/datetime/scantime.c: New file.
    * libmailutils/datetime/streamftime.c: New file.
    * libmailutils/datetime/tab.c: New file.
    * libmailutils/datetime/unixtime.c: New file.
    * libmailutils/datetime/utcoff.c: New file.
    * libmailutils/datetime/yd.c: New file.
    * libmailutils/tests/scantime.at: Fix yday numbers.

commit 5af1d3a7bd8149139ad59baa11501e3d11b138a7
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 13:20:17 2011 +0200

    Move date/time declarations into a separate header: mailutils/datetime.h
    
    Additionally, do not typedef mu_timezone, leave it in struct namespace.

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

Summary of changes:
 comsat/comsat.h                                    |    1 +
 configure.ac                                       |    1 +
 imap4d/append.c                                    |    8 +-
 imap4d/fetch.c                                     |    2 +-
 imap4d/util.c                                      |    8 +-
 include/mailutils/Makefile.am                      |    1 +
 include/mailutils/datetime.h                       |   85 ++
 include/mailutils/envelope.h                       |    6 -
 include/mailutils/imap.h                           |    2 +-
 include/mailutils/imapio.h                         |    2 +-
 include/mailutils/mailutils.h                      |    1 +
 include/mailutils/parse822.h                       |    4 +-
 include/mailutils/util.h                           |   38 -
 libmailutils/Makefile.am                           |    3 +-
 libmailutils/base/Makefile.am                      |    2 -
 libmailutils/base/amd.c                            |    1 +
 libmailutils/base/date.c                           | 1162 --------------------
 libmailutils/base/msgid.c                          |    1 +
 libmailutils/{cidr => datetime}/Makefile.am        |   19 +-
 libmu_dbm/close.c => libmailutils/datetime/dow.c   |   16 +-
 .../imap/close.c => libmailutils/datetime/doy.c    |   48 +-
 libmu_dbm/close.c => libmailutils/datetime/jd.c    |   19 +-
 libmailutils/datetime/scantime.c                   |  660 +++++++++++
 libmailutils/datetime/streamftime.c                |  411 +++++++
 libmailutils/{base => datetime}/strftime.c         |    2 +-
 libmailutils/{diag/muerror.c => datetime/tab.c}    |   46 +-
 .../muemail.c => libmailutils/datetime/unixtime.c  |   41 +-
 mail/list.c => libmailutils/datetime/utcoff.c      |   24 +-
 libmailutils/{list/push.c => datetime/yd.c}        |   10 +-
 libmailutils/imapio/time.c                         |    2 +-
 libmailutils/mailbox/msgenv.c                      |    7 +-
 libmailutils/stream/message_stream.c               |    9 +-
 libmailutils/tests/scantime.at                     |   58 +-
 libmailutils/tests/scantime.c                      |    4 +-
 libmailutils/tests/strftime.c                      |    2 +-
 libmu_scm/mu_message.c                             |    4 +-
 libmu_scm/mu_scm.h                                 |    1 +
 libmu_sieve/actions.c                              |    2 +-
 libproto/mbox/mbox.c                               |   14 +-
 mail/from.c                                        |    2 +-
 mail/mail.h                                        |    1 +
 mh/mh.h                                            |    1 +
 mh/mh_format.c                                     |   44 +-
 mh/sortm.c                                         |    2 +-
 po/POTFILES.in                                     |    1 -
 readmsg/readmsg.c                                  |    4 +-
 readmsg/readmsg.h                                  |    2 +-
 47 files changed, 1366 insertions(+), 1418 deletions(-)
 create mode 100644 include/mailutils/datetime.h
 delete mode 100644 libmailutils/base/date.c
 copy libmailutils/{cidr => datetime}/Makefile.am (83%)
 copy libmu_dbm/close.c => libmailutils/datetime/dow.c (77%)
 copy libproto/imap/close.c => libmailutils/datetime/doy.c (52%)
 copy libmu_dbm/close.c => libmailutils/datetime/jd.c (76%)
 create mode 100644 libmailutils/datetime/scantime.c
 create mode 100644 libmailutils/datetime/streamftime.c
 rename libmailutils/{base => datetime}/strftime.c (97%)
 copy libmailutils/{diag/muerror.c => datetime/tab.c} (56%)
 copy examples/muemail.c => libmailutils/datetime/unixtime.c (57%)
 copy mail/list.c => libmailutils/datetime/utcoff.c (67%)
 copy libmailutils/{list/push.c => datetime/yd.c} (79%)

diff --git a/comsat/comsat.h b/comsat/comsat.h
index ec9dab8..41efc41 100644
--- a/comsat/comsat.h
+++ b/comsat/comsat.h
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <time.h>
 #include <signal.h>
 #include <errno.h>
 #include <termios.h>
diff --git a/configure.ac b/configure.ac
index 62b4409..dfacf09 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1470,6 +1470,7 @@ AC_CONFIG_FILES([
  libmailutils/sockaddr/Makefile
  libmailutils/cidr/Makefile
  libmailutils/cfg/Makefile
+ libmailutils/datetime/Makefile
  libmailutils/diag/Makefile
  libmailutils/filter/Makefile
  libmailutils/imapio/Makefile
diff --git a/imap4d/append.c b/imap4d/append.c
index b8c9198..632333a 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -20,7 +20,7 @@
 struct _temp_envelope
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char *sender;
 };
 
@@ -174,9 +174,11 @@ imap4d_append0 (mu_mailbox_t mbox, int flags, char *date_time, char *text,
 	{
 	  size_t num = 0;
 	  mu_attribute_t attr = NULL;
+	  mu_message_t temp;
+	  
 	  mu_mailbox_messages_count (mbox, &num);
-	  mu_mailbox_get_message (mbox, num, &msg);
-	  mu_message_get_attribute (msg, &attr);
+	  mu_mailbox_get_message (mbox, num, &temp);
+	  mu_message_get_attribute (temp, &attr);
 	  mu_attribute_set_flags (attr, flags);
 	}
       /* FIXME: If not INBOX */
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index 40a963e..6e955c0 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -959,7 +959,7 @@ _frt_internaldate (struct fetch_function_closure *ffc,
   const char *date;
   mu_envelope_t env = NULL;
   struct tm tm, *tmp = NULL;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   mu_message_get_envelope (frt->msg, &env);
   if (mu_envelope_sget_date (env, &date) == 0
diff --git a/imap4d/util.c b/imap4d/util.c
index b494650..2482b9b 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -336,7 +336,7 @@ add2set (size_t ** set, int *n, unsigned long val)
 }
 
 static void
-adjust_tm (struct tm *tm, mu_timezone *tz, enum datetime_parse_mode flag)
+adjust_tm (struct tm *tm, struct mu_timezone *tz, enum datetime_parse_mode flag)
 {
   switch (flag)
     {
@@ -371,7 +371,7 @@ util_parse_internal_date (char *date, time_t *timep,
 			  enum datetime_parse_mode flag)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   time_t time;
 
   if (mu_scan_datetime (date, MU_DATETIME_INTERNALDATE, &tm, &tz, NULL))
@@ -392,7 +392,7 @@ util_parse_822_date (const char *date, time_t *timep,
 		     enum datetime_parse_mode flag)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   const char *p = date;
 
   if (mu_parse822_date_time (&p, date + strlen (date), &tm, &tz) == 0)
@@ -409,7 +409,7 @@ util_parse_ctime_date (const char *date, time_t *timep,
 		       enum datetime_parse_mode flag)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
 
   if (mu_scan_datetime (date, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
     {
diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am
index 27c1b5a..a7b116d 100644
--- a/include/mailutils/Makefile.am
+++ b/include/mailutils/Makefile.am
@@ -38,6 +38,7 @@ pkginclude_HEADERS = \
  cfg.h\
  cidr.h\
  cstr.h\
+ datetime.h\
  daemon.h\
  dbm.h\
  debug.h\
diff --git a/include/mailutils/datetime.h b/include/mailutils/datetime.h
new file mode 100644
index 0000000..de00618
--- /dev/null
+++ b/include/mailutils/datetime.h
@@ -0,0 +1,85 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifndef _MAILUTILS_DATETIME_H
+#define _MAILUTILS_DATETIME_H
+
+#include <time.h>
+#include <mailutils/types.h>
+
+  /* ----------------------- */
+  /* Date & time functions   */
+  /* ----------------------- */
+
+/* Argument ranges:
+
+   year != 0, AD if > 0, BC if < 0
+   1 <= month <= 12
+   1 <= day <= maxday(month)
+*/
+/* Compute Julian Day for the given date */
+int mu_datetime_julianday (int year, int month, int day);
+/* Compute day of week (Sunday - 0) */
+int mu_datetime_dayofweek (int year, int month, int day);
+/* Compute ordinal date (1-based) */
+int mu_datetime_dayofyear (int year, int month, int day);
+/* Return number of days in the year */
+int mu_datetime_year_days (int year);
+
+/* Day of week and month names in C locale */
+extern const char *_mu_datetime_short_month[];
+extern const char *_mu_datetime_full_month[];
+extern const char *_mu_datetime_short_wday[];
+extern const char *_mu_datetime_full_wday[];
+
+
+struct mu_timezone
+{
+  int utc_offset;  /* Seconds east of UTC. */
+
+  const char *tz_name;
+    /* Nickname for this timezone, if known. It is always considered
+       to be a pointer to static string, so will never be freed. */
+};
+
+int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
+
+time_t mu_utc_offset (void);
+time_t mu_tm2time (struct tm *timeptr, struct mu_timezone *tz);
+size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
+
+int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
+		      struct mu_timezone *tz);
+int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
+		      struct mu_timezone *tz, char **endp);
+
+  /* Common datetime formats: */
+#define MU_DATETIME_FROM         "%a %b %e %H:%M:%S %Y"
+/* Length of an envelope date in C locale,
+   not counting the terminating nul character */
+#define MU_DATETIME_FROM_LENGTH 24
+
+#define MU_DATETIME_IMAP         "%d-%b-%Y %H:%M:%S %z"
+#define MU_DATETIME_INTERNALDATE "%d-%b-%Y%$ %H:%M:%S %z"
+
+  /* RFC2822 date.  Scan format contains considerable allowances which would
+     stun formatting functions, therefore two distinct formats are provided:
+     one for outputting and one for scanning: */
+#define MU_DATETIME_FORM_RFC822  "%a, %e %b %Y %H:%M:%S %z"
+#define MU_DATETIME_SCAN_RFC822  "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
+
+#endif
diff --git a/include/mailutils/envelope.h b/include/mailutils/envelope.h
index 246cfd5..6125971 100644
--- a/include/mailutils/envelope.h
+++ b/include/mailutils/envelope.h
@@ -51,12 +51,6 @@ int mu_envelope_set_destroy (mu_envelope_t envelope,
 			     int (*_destroy) (mu_envelope_t),
 			     void *owner);
 
-/* mu_strftime format for envelope dates */
-#define MU_ENVELOPE_DATE_FORMAT "%a %b %d %H:%M:%S %Y"
-/* Length of an envelope date in C locale, 
-   not counting the terminating nul character */
-#define MU_ENVELOPE_DATE_LENGTH 24
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index b3cf521..b247676 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -21,7 +21,7 @@
 #include <mailutils/iterator.h>
 #include <mailutils/debug.h>
 #include <mailutils/stream.h>
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/kwd.h>
 
 #ifdef __cplusplus
diff --git a/include/mailutils/imapio.h b/include/mailutils/imapio.h
index 3a4b1e3..e6d7594 100644
--- a/include/mailutils/imapio.h
+++ b/include/mailutils/imapio.h
@@ -22,7 +22,7 @@ extern "C" {
 #endif
 
 # include <mailutils/types.h>
-# include <mailutils/util.h>  
+# include <mailutils/datetime.h>  
 # include <time.h>
 
 #define MU_IMAPIO_CLIENT 0
diff --git a/include/mailutils/mailutils.h b/include/mailutils/mailutils.h
index 08a2fc0..eb2a616 100644
--- a/include/mailutils/mailutils.h
+++ b/include/mailutils/mailutils.h
@@ -25,6 +25,7 @@
 #include <mailutils/attribute.h>
 #include <mailutils/auth.h>
 #include <mailutils/body.h>
+#include <mailutils/datetime.h>
 #include <mailutils/debug.h>
 #include <mailutils/envelope.h>
 #include <mailutils/errno.h>
diff --git a/include/mailutils/parse822.h b/include/mailutils/parse822.h
index 353ed3e..c73abc6 100644
--- a/include/mailutils/parse822.h
+++ b/include/mailutils/parse822.h
@@ -24,7 +24,7 @@
 #define _MAILUTILS_PARSE822_H
 
 #include <mailutils/types.h>
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -131,7 +131,7 @@ extern int mu_parse822_date      (const char **p, const char *e, int *day,
 extern int mu_parse822_time      (const char **p, const char *e, int *h,
 				  int *m, int *s, int *tz, const char **tz_name);
 extern int mu_parse822_date_time (const char **p, const char *e,
-				  struct tm *tm, mu_timezone *tz);
+				  struct tm *tm, struct mu_timezone *tz);
 
 
 #ifdef __cplusplus
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index e6f4b89..237ff3f 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -21,8 +21,6 @@
 
 /* A collection of utility routines that don't belong somewhere else. */
 
-#include <time.h>
-
 #include <mailutils/list.h>
 #include <mailutils/types.h>
 
@@ -47,42 +45,6 @@ void mu_str_url_decode_inline (char *str);
 int mu_str_url_decode (char **ptr, const char *s);
 
   /* ----------------------- */
-  /* Date & time functions   */
-  /* ----------------------- */
-struct mu_timezone
-{
-  int utc_offset;  /* Seconds east of UTC. */
-
-  const char *tz_name;
-    /* Nickname for this timezone, if known. It is always considered
-       to be a pointer to static string, so will never be freed. */
-};
-
-typedef struct mu_timezone mu_timezone;
-
-int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
-
-time_t mu_utc_offset (void);
-time_t mu_tm2time (struct tm *timeptr, mu_timezone *tz);
-size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
-
-int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
-		      struct mu_timezone *tz);
-int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
-		      struct mu_timezone *tz, char **endp);
-
-  /* Common datetime formats: */
-#define MU_DATETIME_FROM         "%a %b %e %H:%M:%S %Y"
-#define MU_DATETIME_IMAP         "%d-%b-%Y %H:%M:%S %z"
-#define MU_DATETIME_INTERNALDATE "%d-%b-%Y%$ %H:%M:%S %z"
-
-  /* RFC2822 date.  Scan format contains considerable allowances which would
-     stun formatting functions, therefore two distinct formats are provided:
-     one for outputting and one for scanning: */
-#define MU_DATETIME_FORM_RFC822  "%a, %e %b %Y %H:%M:%S %z"  
-#define MU_DATETIME_SCAN_RFC822  "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
-  
-  /* ----------------------- */
   /* File & path names.      */
   /* ----------------------- */
 char *mu_get_homedir (void);
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index b321382..c01b321 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -19,7 +19,7 @@
 SUBDIRS = \
  auth base address list sockaddr cidr cfg diag\
  filter mailbox mailer mime server string stream stdstream\
- property url imapio . tests 
+ property url imapio datetime . tests 
 
 lib_LTLIBRARIES = libmailutils.la
 
@@ -33,6 +33,7 @@ libmailutils_la_LIBADD = \
  sockaddr/libsockaddr.la\
  cidr/libcidr.la\
  cfg/libcfg.la\
+ datetime/libdatetime.la\
  diag/libdiag.la\
  filter/libfilter.la\
  imapio/libimapio.la\
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index a2e5ac5..4b9a375 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -26,7 +26,6 @@ libbase_la_SOURCES = \
  assoc.c\
  filesafety.c\
  daemon.c\
- date.c\
  fdwait.c\
  fgetpwent.c\
  filename.c\
@@ -61,7 +60,6 @@ libbase_la_SOURCES = \
  sha1.c\
  secret.c\
  spawnvp.c\
- strftime.c\
  symlink.c\
  tempfile.c\
  ticket.c\
diff --git a/libmailutils/base/amd.c b/libmailutils/base/amd.c
index bf08f50..769d9c5 100644
--- a/libmailutils/base/amd.c
+++ b/libmailutils/base/amd.c
@@ -63,6 +63,7 @@
 #include <mailutils/locker.h>
 #include <mailutils/message.h>
 #include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/property.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
deleted file mode 100644
index b5c157b..0000000
--- a/libmailutils/base/date.c
+++ /dev/null
@@ -1,1162 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2007, 2009, 2010, 2011 Free
-   Software Foundation, Inc.
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see
-   <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/diag.h>
-#include <mailutils/util.h>
-#include <mailutils/stream.h>
-#include <mailutils/errno.h>
-#include <mailutils/cstr.h>
-#include <mailutils/cctype.h>
-
-#define SECS_PER_DAY 86400
-#define ADJUSTMENT -719162L
-
-/* Julian day is the number of days since Jan 1, 4713 BC (ouch!).
-   Eg. Jan 1, 1900 is: */
-#define  JULIAN_1900    1721425L
-
-/* Computes the number of days elapsed since January, 1 1900 to the
-   January, 1 of the given year */
-static unsigned
-jan1st (int year)
-{
-  year--;               /* Do not consider the current year */
-  return  year * 365L
-               + year/4L    /* Years divisible by 4 are leap years */
-               + year/400L  /* Years divisible by 400 are always leap years */
-               - year/100L; /* Years divisible by 100 but not 400 aren't */
-}
-
-static int  month_start[]=
-    {    0, 31, 59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
-     /* Jan Feb Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
-         31  28  31   30   31   30   31   31   30   31   30   31
-     */
-
-#define leap_year(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
-
-static int
-dayofyear (int year, int month, int day)
-{
-  int  leap, month_days;
-
-  if (year < 0 || month < 0 || month > 11)
-    return -1;
-    
-  leap = leap_year (year);
-  
-  month_days = month_start[month + 1] - month_start[month]
-               + ((month == 2) ? leap : 0);
-
-  if (day < 0 || day > month_days)
-    return -1;  /* Illegal Date */
-
-  if (month <= 2)
-    leap = 0;
-
-  return month_start[month] + day + leap;
-}
-
-/* Returns number of days in a given year */
-static int
-year_days (int year)
-{
-  return dayofyear (year, 11, 31);
-}
-
-static int
-julianday (unsigned *pd, int year, int month, int day)
-{
-  int total = dayofyear (year, month, day);
-  if (total == -1)
-    return -1;
-  *pd = JULIAN_1900 + total + jan1st (year);
-  return 0;
-}
-
-static int
-dayofweek (int year, int month, int day)
-{
-  unsigned jd;
-
-  if (julianday (&jd, year, month, day))
-    return -1;
-
-  /* January 1, 1900 was Monday, hence +1 */
-  return (jd + 1) % 7;
-}
-
-#define ISO_8601_START_WDAY 1 /* Monday */
-#define ISO_8601_MAX_WDAY   4 /* Thursday */
-#define MAXDAYS 366 /* Max. number of days in a year */
-#define ISO_8601_OFF ((MAXDAYS / 7 + 2) * 7)
-
-int
-ISO_8601_weekdays (int yday, int wday)
-{
-  return (yday
-	  - (yday - wday + ISO_8601_MAX_WDAY + ISO_8601_OFF) % 7
-	  + ISO_8601_MAX_WDAY - ISO_8601_START_WDAY);
-}
-
-/* Convert struct tm into time_t, taking into account timezone offset. */
-/* FIXME: It does not take DST into account */
-time_t
-mu_tm2time (struct tm *tm, mu_timezone *tz)
-{
-  time_t t;
-  int day;
-  
-  day = dayofyear (tm->tm_year, tm->tm_mon, tm->tm_mday - 1);
-  if (day == -1)
-    return -1;
-  t = (day + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
-            + (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
-       - (tz ? tz->utc_offset : 0);
-  return t;
-}
-
-/* Convert time 0 at UTC to our localtime, that tells us the offset
-   of our current timezone from UTC. */
-time_t
-mu_utc_offset (void)
-{
-  time_t t = 0;
-  struct tm *tm = gmtime (&t);
-
-  return - mktime (tm);
-}
-
-static const char *short_month[] =
-{
-  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
-static const char *full_month[] =
-{
-  "January", "February", "March", "April",
-  "May", "June", "July", "August",
-  "September", "October", "November", "December"
-};
-
-static const char *short_wday[] =
-{
-  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-};
-
-static const char *full_wday[] =
-{
-  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
-  "Friday", "Saturday"
-};
-
-int
-mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *input_tm,
-		  struct mu_timezone *tz)
-{
-  int rc = 0;
-  struct tm tm;
-
-  /* Copy input TM because it might have been received from gmtime and
-     the fmt might result in further calls to gmtime which will clobber
-     it. */
-  tm = *input_tm;
-  while (*fmt && rc == 0)
-    {
-      size_t len = strcspn (fmt, "%");
-      if (len)
-	{
-	  rc = mu_stream_write (str, fmt, len, NULL);
-	  if (rc)
-	    break;
-	}
-
-      fmt += len;
-
-    restart:
-      if (!*fmt || !*++fmt)
-	break;
-
-      switch (*fmt)
-	{
-	case 'a':
-	  /* The abbreviated weekday name. */
-	  if (tm.tm_wday < 0 || tm.tm_wday > 6)
-	    rc = ERANGE;
-	  else
-	    rc = mu_stream_write (str, short_wday[tm.tm_wday],
-				  strlen (short_wday[tm.tm_wday]), NULL);
-	  break;
-	  
-	case 'A':
-	  /* The full weekday name. */
-	  if (tm.tm_wday < 0 || tm.tm_wday > 6)
-	    rc = ERANGE;
-	  else
-	    rc = mu_stream_write (str, full_wday[tm.tm_wday],
-				  strlen (full_wday[tm.tm_wday]), NULL);
-	  break;
-	  
-	case 'b':
-	case 'h':
-	  /* The abbreviated month name. */
-	  if (tm.tm_mon < 0 || tm.tm_mon > 11)
-	    rc = ERANGE;
-	  else
-	    rc = mu_stream_write (str, short_month[tm.tm_mon],
-				  strlen (short_month[tm.tm_mon]), NULL);
-	  break;
-	  
-	case 'B':
-	  /* The full month name. */
-	  if (tm.tm_mon < 0 || tm.tm_mon > 11)
-	    rc = ERANGE;
-	  else
-	    rc = mu_stream_write (str, full_month[tm.tm_mon],
-				  strlen (full_month[tm.tm_mon]), NULL);
-	  break;
-	  
-	case 'c':
-	  /* The preferred date and time representation. */
-	  rc = mu_c_streamftime (str, "%a %b %e %H:%M:%S %Y", &tm, tz);
-	  break;
-	    
-	case 'C':
-	  /* The century number (year/100) as a 2-digit integer. */
-	  rc = mu_stream_printf (str, "%02d", (tm.tm_year + 1900) / 100);
-	  break;
-	  
-	case 'd':
-	  /* The day of the month as a decimal number (range 01 to 31). */
-	  if (tm.tm_mday < 1 || tm.tm_mday > 31)
-	    rc = ERANGE;
-	  else
-	    rc = mu_stream_printf (str, "%02d", tm.tm_mday);
-	  break;
-	  
-	case 'D':
-	  /* Equivalent to %m/%d/%y. */
-	  rc = mu_c_streamftime (str, "%m/%d/%y", &tm, tz);
-	  break;
-	  
-	case 'e':
-	  /* Like %d, the day of the month as a decimal number, but a leading
-	     zero is replaced by a space. */
-	  if (tm.tm_mday < 1 || tm.tm_mday > 31)
-	    rc = ERANGE;
-	  else
-	    rc = mu_stream_printf (str, "%2d", tm.tm_mday);
-	  break;
-	  
-	case 'E':
-	  /* Modifier. The Single Unix Specification mentions %Ec, %EC, %Ex,
-	     %EX, %Ey, and %EY, which are supposed to use a corresponding
-	     locale-dependent alternative representation.
-	     A no-op in POSIX locale */
-	  goto restart;
-	  
-	case 'F':
-	  /* Equivalent to %Y-%m-%d (the ISO 8601 date format). */
-	  rc = mu_c_streamftime (str, "%Y-%m-%d", &tm, tz);
-	  break;
-	  	  
-	case 'V':
-	  /* The ISO 8601:1988 week number of the current year as a decimal
-	     number range 01 to 53, where week 1 is the first week that has
-	     at least 4 days in the current year, and with Monday as the
-	     first day of the week.
-	  */
-	case 'G':
-	  /* The ISO 8601 year with century as a decimal number.  The 4-digit
-	     year corresponding to the ISO week number (see  %V).  This has
-	     the same format and value as %y, except that if the ISO week
-	     number belongs to the previous or next year, that year is used
-	     instead.
-	  */
-	case 'g':
-	  /* Like  %G, but without century, that is, with a 2-digit year
-	     (00-99). */
-	  {
-	    int year = tm.tm_year + 1900;
-	    int days = ISO_8601_weekdays (tm.tm_yday, tm.tm_wday);
-
-	    if (days < 0)
-	      {
-		days = ISO_8601_weekdays (tm.tm_yday + year_days (year - 1),
-					  tm.tm_wday);
-		year--;
-	      }
-	    else
-	      {
-		int d = ISO_8601_weekdays (tm.tm_yday - year_days (year),
-					   tm.tm_wday);
-		if (d >= 0)
-		  {
-		    year++;
-		    days = d;
-		  }
-	      }
-
-	    switch (*fmt)
-	      {
-	      case 'V':
-		rc = mu_stream_printf (str, "%02d", days / 7 + 1);
-		break;
-
-	      case 'G':
-		rc = mu_stream_printf (str, "%4d", year);
-		break;
-
-	      case 'g':
-		rc = mu_stream_printf (str, "%02d", year % 100);
-	      }
-	  }
-	  break;
-	  
-	case 'H':
-	  /* The hour as a decimal number using a 24-hour clock (range 00 to
-	     23). */
-	  rc = mu_stream_printf (str, "%02d", tm.tm_hour);
-	  break;
-	  
-	case 'I':
-	  /* The hour as a decimal number using a 12-hour clock (range 01 to
-	     12). */
-	  {
-	    unsigned n = tm.tm_hour % 12;
-	    rc = mu_stream_printf (str, "%02d", n == 0 ? 12 : n);
-	  }
-	  break;
-
-	case 'j':
-	  /* The day of the year as a decimal number (range 001 to 366). */
-	  rc = mu_stream_printf (str, "%03d", tm.tm_yday + 1);
-	  break;
-	  
-	case 'k':
-	  /* The hour (24-hour clock) as a decimal number (range 0 to 23);
-	     single digits are preceded by a blank. */
-	  rc = mu_stream_printf (str, "%2d", tm.tm_hour);
-	  break;
-	  
-	case 'l':
-	  /* The hour (12-hour clock) as a decimal number (range 1 to 12);
-	     single digits are preceded by a blank. */
-	  {
-	    unsigned n = tm.tm_hour % 12;
-	    rc = mu_stream_printf (str, "%2d", n == 0 ? 12 : n);
-	  }
-	  break;
-	  
-	case 'm':
-	  /* The month as a decimal number (range 01 to 12). */
-	  rc = mu_stream_printf (str, "%02d", tm.tm_mon + 1);
-	  break;
-				   
-	case 'M':
-	  /* The minute as a decimal number (range 00 to 59). */
-	  rc = mu_stream_printf (str, "%02d", tm.tm_min);
-	  break;
-	  
-	case 'n':
-	  /* A newline character. */
-	  rc = mu_stream_write (str, "\n", 1, NULL);
-	  break;
-	  
-	case 'O':
-	  /* Modifier.  The Single Unix Specification mentions %Od, %Oe, %OH,
-	     %OI, %Om, %OM, %OS, %Ou, %OU, %OV, %Ow, %OW, and %Oy, which are
-	     supposed to use alternative numeric symbols.
-
-	     Hardly of any use for our purposes, hence a no-op. */
-	  goto restart;
-	  
-	case 'p':
-	  /* Either "AM" or "PM" according to the given time value.
-	     Noon is treated as "PM" and midnight as "AM". */
-	  rc = mu_stream_write (str,
-				tm.tm_hour < 12 ? "AM" : "PM",
-				2, NULL);
-	  break;
-				
-	case 'P':
-	  /* Like %p but in lowercase: "am" or "pm". */
-	  rc = mu_stream_write (str,
-				tm.tm_hour < 12 ? "am" : "pm",
-				2, NULL);
-	  break;
-	  
-	case 'r':
-	  /* The time in a.m. or p.m. notation, i.e. %I:%M:%S %p. */
-	  rc = mu_c_streamftime (str, "%I:%M:%S %p", &tm, tz);
-	  break;
-	  
-	case 'R':
-	  /* The time in 24-hour notation (%H:%M) */
-	  rc = mu_c_streamftime (str, "%H:%M", &tm, tz);
-	  break;
-	  
-	case 's':
-	  /* The number of seconds since the Epoch */
-	  rc = mu_stream_printf (str, "%lu",
-				 (unsigned long) mu_tm2time (&tm, tz));
-	  break;
-	  
-	case 'S':
-	  /* The second as a decimal number (range 00 to 60) */
-	  rc = mu_stream_printf (str, "%02d", tm.tm_sec);
-	  break;
-	  
-	case 't':
-	  /* A tab character. */
-	  rc = mu_stream_write (str, "\t", 1, NULL);
-	  break;
-	  
-	case 'T':
-	  /* The time in 24-hour notation (%H:%M:%S) */
-	  rc = mu_c_streamftime (str, "%H:%M:%S", &tm, tz);
-	  break;
-	  
-	case 'u':
-	  /* The day of the week as a decimal, range 1 to 7, Monday being 1.
-	   */
-	  rc = mu_stream_printf (str, "%1d",
-				 tm.tm_wday == 0 ? 7 : tm.tm_wday);
-	  break;
-	  
-	case 'U':
-	  /* The week number of the current year as a decimal number, range
-	     00 to 53, starting with the first Sunday as the first day of
-	     week 01.
-	  */
-	  rc = mu_stream_printf (str, "%02d",
-				 (tm.tm_yday - tm.tm_wday + 7) / 7);
-	  break;
-	  
-	case 'w':
-	  /* The day of the week as a decimal, range 0 to 6, Sunday being 0.
-	   */
-	  rc = mu_stream_printf (str, "%01d", tm.tm_wday);
-	  break;
-	  
-	case 'W':
-	  /* The week number of the current year as a decimal number, range
-	     00 to 53, starting with the first Monday as the first day of
-	     week 01. */
-	  rc = mu_stream_printf (str, "%02d", 
-			 (tm.tm_yday - (tm.tm_wday - 1 + 7) % 7 + 7) / 7);
-	  break;
-
-	  /* The preferred date representation without the time:
-	     equivalent to %D */
-	case 'x':
-	  rc = mu_c_streamftime (str, "%m/%d/%y", &tm, tz);
-	  break;
-	  
-	case 'X':
-	  /* The preferred date representation without the date */
-	  rc = mu_c_streamftime (str, "%H:%M:%S", &tm, tz);
-	  break;
-
-	case 'y':
-	  /* The year as a decimal number without a century (range 00 to 99).
-	   */
-	  rc = mu_stream_printf (str, "%02d", (tm.tm_year + 1900) % 100);
-	  break;
-	  
-	case 'Y':
-	  /* The year as a decimal number including the century. */
-	  rc = mu_stream_printf (str, "%d", tm.tm_year + 1900);
-	  break;
-	  
-	case 'z':
-	case 'Z':
-	  /* The time-zone as hour offset from GMT, for formatting RFC-822
-	     dates (e.g. "%a, %d %b %Y %H:%M:%S %z") */
-	  {
-	    int utc_off = tz ? tz->utc_offset : mu_utc_offset ();
-	    int sign;
-	    if (utc_off < 0)
-	      {
-		sign = '-';
-		utc_off = - utc_off;
-	      }
-	    else
-	      sign = '+';
-	    utc_off /= 60;
-	    rc = mu_stream_printf (str, "%c%02u%02u", sign,
-				   utc_off / 60, utc_off % 60);
-	  }
-	  break;
-	  
-	case '%':
-	  /* A literal '%' character. */
-	  rc = mu_stream_write (str, "%", 1, NULL);
-	  break;
-
-	case '$':
-	  /* Ignored for compatibilty with mu_scan_datetime */
-	  break;
-	  
-	case '+':
-	  /* Not supported (date and time in date(1) format. */
-	default:
-	  rc = mu_stream_write (str, fmt-1, 2, NULL);
-	  break;
-	}
-      fmt++;
-    }
-  /* Restore input tm */
-  *input_tm = tm;
-  return rc;
-}
-
-static int
-_mu_short_weekday_string (const char *str)
-{
-  int i;
-  
-  for (i = 0; i < 7; i++)
-    {
-      if (mu_c_strncasecmp (str, short_wday[i], 3) == 0)
-	return i;
-    }
-  return -1;
-}
-
-static int
-_mu_full_weekday_string (const char *str, char **endp)
-{
-  int i;
-  
-  for (i = 0; i < 7; i++)
-    {
-      if (mu_c_strcasecmp (str, full_wday[i]) == 0)
-	{
-	  if (endp)
-	    *endp = (char*) (str + strlen (full_wday[i]));
-	  return i;
-	}
-    }
-  return -1;
-}
-
-
-static int
-_mu_short_month_string (const char *str)
-{  
-  int i;
-  
-  for (i = 0; i < 12; i++)
-    {
-      if (mu_c_strncasecmp (str, short_month[i], 3) == 0)
-	return i;
-    }
-  return -1;
-}
-
-static int
-_mu_full_month_string (const char *str, char **endp)
-{  
-  int i;
-  
-  for (i = 0; i < 12; i++)
-    {
-      if (mu_c_strcasecmp (str, full_month[i]) == 0)
-	{
-	  if (endp)
-	    *endp = (char*) (str + strlen (full_month[i]));
-	  return i;
-	}
-    }
-  return -1;
-}
-
-int
-get_num (const char *str, char **endp, int ndig, int minval, int maxval,
-	 int *pn)
-{
-  int x = 0;
-  int i;
-  
-  errno = 0;
-  for (i = 0; i < ndig && *str && mu_isdigit (*str); str++, i++)
-    x = x * 10 + *str - '0';
-
-  *endp = (char*) str;
-  if (i == 0)
-    return -1;
-  else if (pn)
-    *pn = i;
-  else if (i != ndig)
-    return -1;
-  if (x < minval || x > maxval)
-    return -1;
-  return x;
-}
-
-#define DT_YEAR  0x01
-#define DT_MONTH 0x02
-#define DT_MDAY  0x04
-#define DT_WDAY  0x08
-#define DT_HOUR  0x10
-#define DT_MIN   0x20
-#define DT_SEC   0x40
-
-#define ST_NON   -1
-#define ST_OPT   0
-#define ST_ALT   1
-
-struct save_input
-{
-  int state;
-  const char *input;
-};
-
-static int
-push_input (mu_list_t *plist, int state, const char *input)
-{
-  mu_list_t list = *plist;
-  struct save_input *inp = malloc (sizeof (*inp));
-  if (!inp)
-    return ENOMEM;
-  if (!list)
-    {
-      int rc = mu_list_create (&list);
-      if (rc)
-	{
-	  free (inp);
-	  return rc;
-	}
-      mu_list_set_destroy_item (list, mu_list_free_item);
-      *plist = list;
-    }
-  inp->state = state;
-  inp->input = input;
-  return mu_list_push (list, (void*)inp);
-}
-
-static int
-peek_state (mu_list_t list, int *state, const char **input)
-{
-  int rc;
-  struct save_input *inp;
-
-  rc = mu_list_tail (list, (void**)&inp);
-  if (rc)
-    return rc;
-  *state = inp->state;
-  if (input)
-    *input = inp->input;
-  return 0;
-}      
-
-static int
-pop_input (mu_list_t list, int *state, const char **input)
-{
-  int rc;
-  struct save_input *inp;
-
-  rc = mu_list_pop (list, (void**)&inp);
-  if (rc)
-    return rc;
-  *state = inp->state;
-  if (input)
-    *input = inp->input;
-  return 0;
-}
-
-static int
-bracket_to_state (int c)
-{
-  switch (c)
-    {
-    case '[':
-    case ']':
-      return ST_OPT;
-    case '(':
-    case ')':
-      return ST_ALT;
-    }
-  return ST_NON;
-}
-
-static int
-state_to_closing_bracket (int st)
-{
-  switch (st)
-    {
-    case ST_OPT:
-      return ']';
-    case ST_ALT:
-      return ')';
-    }
-  return '?';
-}
-
-static int
-scan_recovery (const char *fmt, mu_list_t *plist, int skip_alt,
-	       const char **endp,
-	       const char **input)
-{
-  int c, rc = 0;
-  int nesting_level = 1;
-  int st;
-  const char *p;
-  
-  while (*fmt)
-    {
-      c = *fmt++;
-      
-      if (c == '%')
-	{
-	  c = *fmt++;
-	  if (!c)
-	    {
-	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			("%s:%d: error in format: %% at the end of input",
-			 __FILE__, __LINE__));
-	      rc = MU_ERR_FORMAT;
-	      break;
-	    }
-	      
-	  switch (c)
-	    {
-	    case '[':
-	    case '(':
-	      nesting_level++;
-	      rc = push_input (plist, bracket_to_state (c), NULL);
-	      break;
-	      
-	    case ')':
-	    case ']':
-	      rc = pop_input (*plist, &st, &p);
-	      if (rc || st != bracket_to_state (c))
-		{
-		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			    ("%s:%d: error in format: %%%c out of context",
-			     __FILE__, __LINE__, c));
-		  rc = MU_ERR_FORMAT;
-		  break;
-		}
-	      if (--nesting_level == 0)
-		{
-		  *endp = fmt;
-		  if (skip_alt)
-		    return 0;
-		  *input = p;
-		  if (st == ST_ALT)
-		    {
-		      if (*fmt == '%' && (fmt[1] == '|' || fmt[1] == ']'))
-			return 0;
-		      return MU_ERR_PARSE; /* No match found */
-		    }
-		  return 0;
-		}
-	      break;
-
-	    case '|':
-	      if (skip_alt)
-		continue;
-	      if (nesting_level == 1)
-		{
-		  *endp = fmt;
-		  return peek_state (*plist, &st, input);
-		}
-	      break;
-
-	    case '\\':
-	      if (*++fmt == 0)
-		{
-		  peek_state (*plist, &st, NULL);
-		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			    ("%s:%d: error in format: missing closing %%%c",
-			     __FILE__, __LINE__,
-			     state_to_closing_bracket (st)));
-		  return MU_ERR_FORMAT;		  
-		}
-	    }
-	}
-    }
-  
-  peek_state (*plist, &st, NULL);
-  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-	    ("%s:%d: error in format: missing closing %%%c",
-	     __FILE__, __LINE__,
-	     state_to_closing_bracket (st)));
-  return MU_ERR_FORMAT;		  
-}
-
-int
-mu_scan_datetime (const char *input, const char *fmt,
-		  struct tm *tm, struct mu_timezone *tz, char **endp)
-{
-  int rc = 0;
-  char *p;
-  int n;
-  int c;
-  int st;
-  int recovery = 0;
-  int eof_ok = 0;
-  int datetime_parts = 0;
-  mu_list_t save_input_list = NULL;
-  
-  memset (tm, 0, sizeof *tm);
-#ifdef HAVE_STRUCT_TM_TM_ISDST
-  tm->tm_isdst = -1;	/* unknown. */
-#endif
-  /* provide default timezone, in case it is not supplied in input */
-  if (tz)
-    {
-      memset (tz, 0, sizeof *tz);
-      tz->utc_offset = mu_utc_offset ();
-    }
-
-  /* Skip leading whitespace */
-  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
-  for (; *fmt && rc == 0; fmt++)
-    {
-      if (mu_isspace (*fmt))
-	{
-	  fmt = mu_str_skip_class (fmt, MU_CTYPE_BLANK);
-	  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
-	  if (!*fmt)
-	    break;
-	}
-      eof_ok = 0;
-      
-      if (*fmt == '%')
-	{
-	  c = *++fmt;
-	  if (!c)
-	    {
-	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			("%s:%d: error in format: %% at the end of input",
-			 __FILE__, __LINE__));
-	      rc = MU_ERR_FORMAT;
-	      break;
-	    }
-	  
-	  switch (c)
-	    {
-	    case 'a':
-	      /* The abbreviated weekday name. */
-	      n = _mu_short_weekday_string (input);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_wday = n;
-		  datetime_parts |= DT_WDAY;
-		  input += 3;
-		}
-	      break;
-		  
-	    case 'A':
-	      /* The full weekday name. */
-	      n = _mu_full_weekday_string (input, &p);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_wday = n;
-		  datetime_parts |= DT_WDAY;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'b':
-	      /* The abbreviated month name. */
-	      n = _mu_short_month_string (input);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_mon = n;
-		  datetime_parts |= DT_MONTH;
-		  input += 3;
-		}
-	      break;
-
-	    case 'B':
-	      /* The full month name. */
-	      n = _mu_full_month_string (input, &p);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_mon = n;
-		  datetime_parts |= DT_MONTH;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'd':
-	      /* The day of the month as a decimal number (range 01 to 31). */
-	      n = get_num (input, &p, 2, 1, 31, NULL);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_mday = n;
-		  datetime_parts |= DT_MDAY;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'e':
-	      /* Like %d, the day of the month as a decimal number, but a
-		 leading zero is replaced by a space. */
-	      {
-		int ndig;
-		
-		n = get_num (input, &p, 2, 1, 31, &ndig);
-		if (n == -1)
-		  rc = MU_ERR_PARSE;
-		else
-		  {
-		    tm->tm_mday = n;
-		    datetime_parts |= DT_MDAY;
-		    input = p;
-		  }
-	      }
-	      break;
-	      
-	    case 'H':
-	      /* The hour as a decimal number using a 24-hour clock (range
-		 00 to 23). */
-	      n = get_num (input, &p, 2, 0, 23, NULL);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_hour = n;
-		  datetime_parts |= DT_HOUR;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'm':
-	      /* The month as a decimal number (range 01 to 12). */
-	      n = get_num (input, &p, 2, 1, 12, NULL);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_mon = n - 1;
-		  datetime_parts |= DT_MONTH;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'M':
-	      /* The minute as a decimal number (range 00 to 59). */
-	      n = get_num (input, &p, 2, 0, 59, NULL);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_min = n;
-		  datetime_parts |= DT_MIN;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'S':
-	      /* The second as a decimal number (range 00 to 60) */
-	      n = get_num (input, &p, 2, 0, 60, NULL);
-	      if (n == -1)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_sec = n;
-		  datetime_parts |= DT_SEC;
-		  input = p;
-		}
-	      break;
-	      
-	    case 'Y':
-	      /* The year as a decimal number including the century. */
-	      errno = 0;
-	      n = strtoul (input, &p, 10);
-	      if (errno || p == input)
-		rc = MU_ERR_PARSE;
-	      else
-		{
-		  tm->tm_year = n - 1900;
-		  datetime_parts |= DT_YEAR;
-		  input = p;
-		}
-	      break;
-		  
-	    case 'z':
-	      /* The time-zone as hour offset from GMT */
-	      {
-		int sign = 1;
-		int hr;
-		
-		if (*input == '+')
-		  input++;
-		else if (*input == '-')
-		  {
-		    input++;
-		    sign = -1;
-		  }
-		n = get_num (input, &p, 2, 0, 11, NULL);
-		if (n == -1)
-		  rc = MU_ERR_PARSE;
-		else
-		  {
-		    input = p;
-		    hr = n;
-		    n = get_num (input, &p, 2, 0, 59, NULL);
-		    if (n == -1)
-		      rc = MU_ERR_PARSE;
-		    else
-		      {
-			input = p;
-			if (tz)
-			  tz->utc_offset = sign * (hr * 60 + n) * 60;
-		      }
-		  }
-	      }
-	      break;
-		      
-	    case '%':
-	      if (*input == '%')
-		input++;
-	      else
-		rc = MU_ERR_PARSE;
-	      break;
-
-	      rc = push_input (&save_input_list, ST_ALT, (void*)input);
-	      break;
-	      
-	    case '(':
-	    case '[':
-	      rc = push_input (&save_input_list, bracket_to_state (c),
-			       (void*)input);
-	      break;
-
-	    case ')':
-	    case ']':
-	      if (pop_input (save_input_list, &st, NULL))
-		{
-		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			    ("%s:%d: error in format: unbalanced %%%c near %s",
-			     __FILE__, __LINE__, c, fmt));
-		  rc = MU_ERR_FORMAT;
-		}
-	      else if (st != bracket_to_state (c))
-		{
-		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			    ("%s:%d: error in format: %%%c out of context",
-			     __FILE__, __LINE__, c));
-		  rc = MU_ERR_FORMAT;
-		}
-	      break;
-
-	    case '|':
-	      rc = scan_recovery (fmt, &save_input_list, 1, &fmt, NULL);
-	      if (rc == 0)
-		fmt--;
-	      break;
-	      
-	    case '$':
-	      eof_ok = 1;
-	      break;
-
-	    case '\\':
-	      c = *++fmt;
-	      if (!c)
-		{
-		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			    ("%s:%d: error in format: %% at the end of input",
-			     __FILE__, __LINE__));
-		  rc = MU_ERR_FORMAT;
-		}
-	      else if (c == *input)
-		input++;
-	      else
-		rc = MU_ERR_PARSE;
-	      break;
-
-	    case '?':
-	      input++;
-	      break;
-	      
-	    default:
-	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-			("%s:%d: error in format: unrecognized conversion type"
-			 " near %s",
-			 __FILE__, __LINE__, fmt));
-	      rc = MU_ERR_FORMAT;
-	      break;
-	    }
-
-	  if (eof_ok && rc == 0 && *input == 0)
-	    break;
-	}
-      else if (!recovery && *input != *fmt)
-	rc = MU_ERR_PARSE;
-      else
-	input++;
-
-      if (rc == MU_ERR_PARSE && !mu_list_is_empty (save_input_list))
-	{
-	  rc = scan_recovery (fmt, &save_input_list, 0, &fmt, &input);
-	  if (rc == 0)
-	    --fmt;
-	}
-    }
-
-  if (!mu_list_is_empty (save_input_list))
-    {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-		("%s:%d: error in format: closing bracket missing",
-		 __FILE__, __LINE__));
-      rc = MU_ERR_FORMAT;
-    }
-  mu_list_destroy (&save_input_list);
-  
-  if (rc == 0 && recovery)
-    rc = MU_ERR_PARSE;
-  
-  if (!eof_ok && rc == 0 && *input == 0 && *fmt)
-    rc = MU_ERR_PARSE;
-
-  if ((datetime_parts & (DT_YEAR|DT_MONTH|DT_MDAY)) ==
-      (DT_YEAR|DT_MONTH|DT_MDAY))
-    {
-      if (!(datetime_parts & DT_WDAY))
-	tm->tm_wday = dayofweek (tm->tm_year + 1900, tm->tm_mon, tm->tm_mday);
-      tm->tm_yday = dayofyear (tm->tm_year + 1900, tm->tm_mon, tm->tm_mday);
-    }
-  
-  if (endp)
-    *endp = (char*) input;
-  
-  return rc;
-}
diff --git a/libmailutils/base/msgid.c b/libmailutils/base/msgid.c
index 0207cb8..8683810 100644
--- a/libmailutils/base/msgid.c
+++ b/libmailutils/base/msgid.c
@@ -28,6 +28,7 @@
 #include <mailutils/cctype.h>
 #include <mailutils/message.h>
 #include <mailutils/header.h>
+#include <mailutils/datetime.h>
 #include <mailutils/util.h>
 #include <mailutils/io.h>
 #include <mailutils/envelope.h>
diff --git a/libmailutils/cidr/Makefile.am b/libmailutils/datetime/Makefile.am
similarity index 83%
copy from libmailutils/cidr/Makefile.am
copy to libmailutils/datetime/Makefile.am
index 408806f..f9a4f45 100644
--- a/libmailutils/cidr/Makefile.am
+++ b/libmailutils/datetime/Makefile.am
@@ -15,13 +15,18 @@
 # Public License along with this library.  If not, see
 # <http://www.gnu.org/licenses/>.
 
-noinst_LTLIBRARIES = libcidr.la
+noinst_LTLIBRARIES = libdatetime.la
 
-libcidr_la_SOURCES = \
- fromsa.c\
- fromstr.c\
- match.c\
- tosa.c\
- tostr.c
+libdatetime_la_SOURCES = \
+ dow.c\
+ doy.c\
+ jd.c\
+ scantime.c\
+ streamftime.c\
+ strftime.c\
+ tab.c\
+ unixtime.c\
+ utcoff.c\
+ yd.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmu_dbm/close.c b/libmailutils/datetime/dow.c
similarity index 77%
copy from libmu_dbm/close.c
copy to libmailutils/datetime/dow.c
index a84574a..9f94a68 100644
--- a/libmu_dbm/close.c
+++ b/libmailutils/datetime/dow.c
@@ -18,18 +18,12 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#include <stdlib.h>
+#include <mailutils/datetime.h>
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
-
+/* Compute day of week (0 - Sunday) */
 int
-mu_dbm_close (mu_dbm_file_t db)
+mu_datetime_dayofweek (int year, int month, int day)
 {
-  DBMSYSCK (db, _dbm_close);
-  if (!db->db_descr)
-    return 0;
-  return db->db_sys->_dbm_close (db);
+  return (mu_datetime_julianday (year, month, day) + 1) % 7;
 }
-
diff --git a/libproto/imap/close.c b/libmailutils/datetime/doy.c
similarity index 52%
copy from libproto/imap/close.c
copy to libmailutils/datetime/doy.c
index a92d598..49883cd 100644
--- a/libproto/imap/close.c
+++ b/libmailutils/datetime/doy.c
@@ -18,30 +18,36 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#include <stdlib.h>
+#include <mailutils/datetime.h>
 
-#include <mailutils/imap.h>
-#include <mailutils/sys/imap.h>
+static int  month_start[]=
+    {    0, 31, 59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
+     /* Jan Feb Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
+         31  28  31   30   31   30   31   31   30   31   30   31
+     */
 
-void
-_mu_close_handler (mu_imap_t imap)
-{
-  if (imap->resp_code == MU_IMAP_OK)
-    imap->session_state = MU_IMAP_SESSION_AUTH;
-}
+#define leap_year(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
 
+/* Compute number of days since January 1 to the given day of year. */
 int
-mu_imap_close (mu_imap_t imap)
+mu_datetime_dayofyear (int year, int month, int day)
 {
-  static char const *command = "CLOSE";
-  static struct imap_command com = {
-    MU_IMAP_SESSION_SELECTED,
-    NULL,
-    MU_IMAP_CLIENT_CLOSE_RX,
-    0,
-    1,
-    &command,
-    _mu_close_handler
-  };
-  return mu_imap_gencom (imap, &com);
+  int  leap, month_days;
+
+  if (year < 0 || month < 1 || month > 12 || day < 1)
+    return -1;
+    
+  leap = leap_year (year);
+  
+  month_days = month_start[month] - month_start[month - 1]
+               + ((month == 2) ? leap : 0);
+
+  if (day > month_days)
+    return -1;
+
+  if (month <= 2)
+    leap = 0;
+
+  return month_start[month-1] + day + leap;
 }
-      
diff --git a/libmu_dbm/close.c b/libmailutils/datetime/jd.c
similarity index 76%
copy from libmu_dbm/close.c
copy to libmailutils/datetime/jd.c
index a84574a..39fdf80 100644
--- a/libmu_dbm/close.c
+++ b/libmailutils/datetime/jd.c
@@ -18,18 +18,15 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
+#include <stdlib.h>
+#include <mailutils/datetime.h>
 
 int
-mu_dbm_close (mu_dbm_file_t db)
+mu_datetime_julianday (int year, int month, int day)
 {
-  DBMSYSCK (db, _dbm_close);
-  if (!db->db_descr)
-    return 0;
-  return db->db_sys->_dbm_close (db);
-}
+  int a = (14 - month) / 12;
+  int y = year + 4800 - a;
+  int m = month + 12*a - 3;
 
+  return day + (153*m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045; 
+}
diff --git a/libmailutils/datetime/scantime.c b/libmailutils/datetime/scantime.c
new file mode 100644
index 0000000..670052b
--- /dev/null
+++ b/libmailutils/datetime/scantime.c
@@ -0,0 +1,660 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2007, 2009, 2010, 2011 Free
+   Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/diag.h>
+#include <mailutils/datetime.h>
+#include <mailutils/util.h>
+#include <mailutils/stream.h>
+#include <mailutils/errno.h>
+#include <mailutils/cstr.h>
+#include <mailutils/cctype.h>
+
+static int
+_mu_short_weekday_string (const char *str)
+{
+  int i;
+  
+  for (i = 0; i < 7; i++)
+    {
+      if (mu_c_strncasecmp (str, _mu_datetime_short_wday[i], 3) == 0)
+	return i;
+    }
+  return -1;
+}
+
+static int
+_mu_full_weekday_string (const char *str, char **endp)
+{
+  int i;
+  
+  for (i = 0; i < 7; i++)
+    {
+      if (mu_c_strcasecmp (str, _mu_datetime_full_wday[i]) == 0)
+	{
+	  if (endp)
+	    *endp = (char*) (str + strlen (_mu_datetime_full_wday[i]));
+	  return i;
+	}
+    }
+  return -1;
+}
+
+static int
+_mu_short_month_string (const char *str)
+{  
+  int i;
+  
+  for (i = 0; i < 12; i++)
+    {
+      if (mu_c_strncasecmp (str, _mu_datetime_short_month[i], 3) == 0)
+	return i;
+    }
+  return -1;
+}
+
+static int
+_mu_full_month_string (const char *str, char **endp)
+{  
+  int i;
+  
+  for (i = 0; i < 12; i++)
+    {
+      if (mu_c_strcasecmp (str, _mu_datetime_full_month[i]) == 0)
+	{
+	  if (endp)
+	    *endp = (char*) (str + strlen (_mu_datetime_full_month[i]));
+	  return i;
+	}
+    }
+  return -1;
+}
+
+int
+get_num (const char *str, char **endp, int ndig, int minval, int maxval,
+	 int *pn)
+{
+  int x = 0;
+  int i;
+  
+  errno = 0;
+  for (i = 0; i < ndig && *str && mu_isdigit (*str); str++, i++)
+    x = x * 10 + *str - '0';
+
+  *endp = (char*) str;
+  if (i == 0)
+    return -1;
+  else if (pn)
+    *pn = i;
+  else if (i != ndig)
+    return -1;
+  if (x < minval || x > maxval)
+    return -1;
+  return x;
+}
+
+#define DT_YEAR  0x01
+#define DT_MONTH 0x02
+#define DT_MDAY  0x04
+#define DT_WDAY  0x08
+#define DT_HOUR  0x10
+#define DT_MIN   0x20
+#define DT_SEC   0x40
+
+#define ST_NON   -1
+#define ST_OPT   0
+#define ST_ALT   1
+
+struct save_input
+{
+  int state;
+  const char *input;
+};
+
+static int
+push_input (mu_list_t *plist, int state, const char *input)
+{
+  mu_list_t list = *plist;
+  struct save_input *inp = malloc (sizeof (*inp));
+  if (!inp)
+    return ENOMEM;
+  if (!list)
+    {
+      int rc = mu_list_create (&list);
+      if (rc)
+	{
+	  free (inp);
+	  return rc;
+	}
+      mu_list_set_destroy_item (list, mu_list_free_item);
+      *plist = list;
+    }
+  inp->state = state;
+  inp->input = input;
+  return mu_list_push (list, (void*)inp);
+}
+
+static int
+peek_state (mu_list_t list, int *state, const char **input)
+{
+  int rc;
+  struct save_input *inp;
+
+  rc = mu_list_tail (list, (void**)&inp);
+  if (rc)
+    return rc;
+  *state = inp->state;
+  if (input)
+    *input = inp->input;
+  return 0;
+}      
+
+static int
+pop_input (mu_list_t list, int *state, const char **input)
+{
+  int rc;
+  struct save_input *inp;
+
+  rc = mu_list_pop (list, (void**)&inp);
+  if (rc)
+    return rc;
+  *state = inp->state;
+  if (input)
+    *input = inp->input;
+  return 0;
+}
+
+static int
+bracket_to_state (int c)
+{
+  switch (c)
+    {
+    case '[':
+    case ']':
+      return ST_OPT;
+    case '(':
+    case ')':
+      return ST_ALT;
+    }
+  return ST_NON;
+}
+
+static int
+state_to_closing_bracket (int st)
+{
+  switch (st)
+    {
+    case ST_OPT:
+      return ']';
+    case ST_ALT:
+      return ')';
+    }
+  return '?';
+}
+
+static int
+scan_recovery (const char *fmt, mu_list_t *plist, int skip_alt,
+	       const char **endp,
+	       const char **input)
+{
+  int c, rc = 0;
+  int nesting_level = 1;
+  int st;
+  const char *p;
+  
+  while (*fmt)
+    {
+      c = *fmt++;
+      
+      if (c == '%')
+	{
+	  c = *fmt++;
+	  if (!c)
+	    {
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			("%s:%d: error in format: %% at the end of input",
+			 __FILE__, __LINE__));
+	      rc = MU_ERR_FORMAT;
+	      break;
+	    }
+	      
+	  switch (c)
+	    {
+	    case '[':
+	    case '(':
+	      nesting_level++;
+	      rc = push_input (plist, bracket_to_state (c), NULL);
+	      break;
+	      
+	    case ')':
+	    case ']':
+	      rc = pop_input (*plist, &st, &p);
+	      if (rc || st != bracket_to_state (c))
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: %%%c out of context",
+			     __FILE__, __LINE__, c));
+		  rc = MU_ERR_FORMAT;
+		  break;
+		}
+	      if (--nesting_level == 0)
+		{
+		  *endp = fmt;
+		  if (skip_alt)
+		    return 0;
+		  *input = p;
+		  if (st == ST_ALT)
+		    {
+		      if (*fmt == '%' && (fmt[1] == '|' || fmt[1] == ']'))
+			return 0;
+		      return MU_ERR_PARSE; /* No match found */
+		    }
+		  return 0;
+		}
+	      break;
+
+	    case '|':
+	      if (skip_alt)
+		continue;
+	      if (nesting_level == 1)
+		{
+		  *endp = fmt;
+		  return peek_state (*plist, &st, input);
+		}
+	      break;
+
+	    case '\\':
+	      if (*++fmt == 0)
+		{
+		  peek_state (*plist, &st, NULL);
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: missing closing %%%c",
+			     __FILE__, __LINE__,
+			     state_to_closing_bracket (st)));
+		  return MU_ERR_FORMAT;		  
+		}
+	    }
+	}
+    }
+  
+  peek_state (*plist, &st, NULL);
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+	    ("%s:%d: error in format: missing closing %%%c",
+	     __FILE__, __LINE__,
+	     state_to_closing_bracket (st)));
+  return MU_ERR_FORMAT;		  
+}
+
+int
+mu_scan_datetime (const char *input, const char *fmt,
+		  struct tm *tm, struct mu_timezone *tz, char **endp)
+{
+  int rc = 0;
+  char *p;
+  int n;
+  int c;
+  int st;
+  int recovery = 0;
+  int eof_ok = 0;
+  int datetime_parts = 0;
+  mu_list_t save_input_list = NULL;
+  
+  memset (tm, 0, sizeof *tm);
+#ifdef HAVE_STRUCT_TM_TM_ISDST
+  tm->tm_isdst = -1;	/* unknown. */
+#endif
+  /* provide default timezone, in case it is not supplied in input */
+  if (tz)
+    {
+      memset (tz, 0, sizeof *tz);
+      tz->utc_offset = mu_utc_offset ();
+    }
+
+  /* Skip leading whitespace */
+  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
+  for (; *fmt && rc == 0; fmt++)
+    {
+      if (mu_isspace (*fmt))
+	{
+	  fmt = mu_str_skip_class (fmt, MU_CTYPE_BLANK);
+	  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
+	  if (!*fmt)
+	    break;
+	}
+      eof_ok = 0;
+      
+      if (*fmt == '%')
+	{
+	  c = *++fmt;
+	  if (!c)
+	    {
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			("%s:%d: error in format: %% at the end of input",
+			 __FILE__, __LINE__));
+	      rc = MU_ERR_FORMAT;
+	      break;
+	    }
+	  
+	  switch (c)
+	    {
+	    case 'a':
+	      /* The abbreviated weekday name. */
+	      n = _mu_short_weekday_string (input);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_wday = n;
+		  datetime_parts |= DT_WDAY;
+		  input += 3;
+		}
+	      break;
+		  
+	    case 'A':
+	      /* The full weekday name. */
+	      n = _mu_full_weekday_string (input, &p);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_wday = n;
+		  datetime_parts |= DT_WDAY;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'b':
+	      /* The abbreviated month name. */
+	      n = _mu_short_month_string (input);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mon = n;
+		  datetime_parts |= DT_MONTH;
+		  input += 3;
+		}
+	      break;
+
+	    case 'B':
+	      /* The full month name. */
+	      n = _mu_full_month_string (input, &p);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mon = n;
+		  datetime_parts |= DT_MONTH;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'd':
+	      /* The day of the month as a decimal number (range 01 to 31). */
+	      n = get_num (input, &p, 2, 1, 31, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mday = n;
+		  datetime_parts |= DT_MDAY;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'e':
+	      /* Like %d, the day of the month as a decimal number, but a
+		 leading zero is replaced by a space. */
+	      {
+		int ndig;
+		
+		n = get_num (input, &p, 2, 1, 31, &ndig);
+		if (n == -1)
+		  rc = MU_ERR_PARSE;
+		else
+		  {
+		    tm->tm_mday = n;
+		    datetime_parts |= DT_MDAY;
+		    input = p;
+		  }
+	      }
+	      break;
+	      
+	    case 'H':
+	      /* The hour as a decimal number using a 24-hour clock (range
+		 00 to 23). */
+	      n = get_num (input, &p, 2, 0, 23, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_hour = n;
+		  datetime_parts |= DT_HOUR;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'm':
+	      /* The month as a decimal number (range 01 to 12). */
+	      n = get_num (input, &p, 2, 1, 12, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_mon = n - 1;
+		  datetime_parts |= DT_MONTH;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'M':
+	      /* The minute as a decimal number (range 00 to 59). */
+	      n = get_num (input, &p, 2, 0, 59, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_min = n;
+		  datetime_parts |= DT_MIN;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'S':
+	      /* The second as a decimal number (range 00 to 60) */
+	      n = get_num (input, &p, 2, 0, 60, NULL);
+	      if (n == -1)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_sec = n;
+		  datetime_parts |= DT_SEC;
+		  input = p;
+		}
+	      break;
+	      
+	    case 'Y':
+	      /* The year as a decimal number including the century. */
+	      errno = 0;
+	      n = strtoul (input, &p, 10);
+	      if (errno || p == input)
+		rc = MU_ERR_PARSE;
+	      else
+		{
+		  tm->tm_year = n - 1900;
+		  datetime_parts |= DT_YEAR;
+		  input = p;
+		}
+	      break;
+		  
+	    case 'z':
+	      /* The time-zone as hour offset from GMT */
+	      {
+		int sign = 1;
+		int hr;
+		
+		if (*input == '+')
+		  input++;
+		else if (*input == '-')
+		  {
+		    input++;
+		    sign = -1;
+		  }
+		n = get_num (input, &p, 2, 0, 11, NULL);
+		if (n == -1)
+		  rc = MU_ERR_PARSE;
+		else
+		  {
+		    input = p;
+		    hr = n;
+		    n = get_num (input, &p, 2, 0, 59, NULL);
+		    if (n == -1)
+		      rc = MU_ERR_PARSE;
+		    else
+		      {
+			input = p;
+			if (tz)
+			  tz->utc_offset = sign * (hr * 60 + n) * 60;
+		      }
+		  }
+	      }
+	      break;
+		      
+	    case '%':
+	      if (*input == '%')
+		input++;
+	      else
+		rc = MU_ERR_PARSE;
+	      break;
+
+	      rc = push_input (&save_input_list, ST_ALT, (void*)input);
+	      break;
+	      
+	    case '(':
+	    case '[':
+	      rc = push_input (&save_input_list, bracket_to_state (c),
+			       (void*)input);
+	      break;
+
+	    case ')':
+	    case ']':
+	      if (pop_input (save_input_list, &st, NULL))
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: unbalanced %%%c near %s",
+			     __FILE__, __LINE__, c, fmt));
+		  rc = MU_ERR_FORMAT;
+		}
+	      else if (st != bracket_to_state (c))
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: %%%c out of context",
+			     __FILE__, __LINE__, c));
+		  rc = MU_ERR_FORMAT;
+		}
+	      break;
+
+	    case '|':
+	      rc = scan_recovery (fmt, &save_input_list, 1, &fmt, NULL);
+	      if (rc == 0)
+		fmt--;
+	      break;
+	      
+	    case '$':
+	      eof_ok = 1;
+	      break;
+
+	    case '\\':
+	      c = *++fmt;
+	      if (!c)
+		{
+		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			    ("%s:%d: error in format: %% at the end of input",
+			     __FILE__, __LINE__));
+		  rc = MU_ERR_FORMAT;
+		}
+	      else if (c == *input)
+		input++;
+	      else
+		rc = MU_ERR_PARSE;
+	      break;
+
+	    case '?':
+	      input++;
+	      break;
+	      
+	    default:
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+			("%s:%d: error in format: unrecognized conversion type"
+			 " near %s",
+			 __FILE__, __LINE__, fmt));
+	      rc = MU_ERR_FORMAT;
+	      break;
+	    }
+
+	  if (eof_ok && rc == 0 && *input == 0)
+	    break;
+	}
+      else if (!recovery && *input != *fmt)
+	rc = MU_ERR_PARSE;
+      else
+	input++;
+
+      if (rc == MU_ERR_PARSE && !mu_list_is_empty (save_input_list))
+	{
+	  rc = scan_recovery (fmt, &save_input_list, 0, &fmt, &input);
+	  if (rc == 0)
+	    --fmt;
+	}
+    }
+
+  if (!mu_list_is_empty (save_input_list))
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		("%s:%d: error in format: closing bracket missing",
+		 __FILE__, __LINE__));
+      rc = MU_ERR_FORMAT;
+    }
+  mu_list_destroy (&save_input_list);
+  
+  if (rc == 0 && recovery)
+    rc = MU_ERR_PARSE;
+  
+  if (!eof_ok && rc == 0 && *input == 0 && *fmt)
+    rc = MU_ERR_PARSE;
+
+  if ((datetime_parts & (DT_YEAR|DT_MONTH|DT_MDAY)) ==
+      (DT_YEAR|DT_MONTH|DT_MDAY))
+    {
+      if (!(datetime_parts & DT_WDAY))
+	tm->tm_wday = mu_datetime_dayofweek (tm->tm_year + 1900,
+					     tm->tm_mon + 1, tm->tm_mday);
+      tm->tm_yday = mu_datetime_dayofyear (tm->tm_year + 1900,
+					   tm->tm_mon + 1, tm->tm_mday) - 1;
+    }
+  
+  if (endp)
+    *endp = (char*) input;
+  
+  return rc;
+}
diff --git a/libmailutils/datetime/streamftime.c b/libmailutils/datetime/streamftime.c
new file mode 100644
index 0000000..e67d7d4
--- /dev/null
+++ b/libmailutils/datetime/streamftime.c
@@ -0,0 +1,411 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 1999, 2000, 2001, 2002, 2007, 2009, 2010, 2011 Free
+   Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/diag.h>
+#include <mailutils/datetime.h>
+#include <mailutils/util.h>
+#include <mailutils/stream.h>
+#include <mailutils/errno.h>
+#include <mailutils/cstr.h>
+#include <mailutils/cctype.h>
+
+#define ISO_8601_START_WDAY 1 /* Monday */
+#define ISO_8601_MAX_WDAY   4 /* Thursday */
+#define MAXDAYS 366 /* Max. number of days in a year */
+#define ISO_8601_OFF ((MAXDAYS / 7 + 2) * 7)
+
+int
+ISO_8601_weekdays (int yday, int wday)
+{
+  return (yday
+	  - (yday - wday + ISO_8601_MAX_WDAY + ISO_8601_OFF) % 7
+	  + ISO_8601_MAX_WDAY - ISO_8601_START_WDAY);
+}
+
+int
+mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *input_tm,
+		  struct mu_timezone *tz)
+{
+  int rc = 0;
+  struct tm tm;
+
+  /* Copy input TM because it might have been received from gmtime and
+     the fmt might result in further calls to gmtime which will clobber
+     it. */
+  tm = *input_tm;
+  while (*fmt && rc == 0)
+    {
+      size_t len = strcspn (fmt, "%");
+      if (len)
+	{
+	  rc = mu_stream_write (str, fmt, len, NULL);
+	  if (rc)
+	    break;
+	}
+
+      fmt += len;
+
+    restart:
+      if (!*fmt || !*++fmt)
+	break;
+
+      switch (*fmt)
+	{
+	case 'a':
+	  /* The abbreviated weekday name. */
+	  if (tm.tm_wday < 0 || tm.tm_wday > 6)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, _mu_datetime_short_wday[tm.tm_wday],
+				  strlen (_mu_datetime_short_wday[tm.tm_wday]),
+				  NULL);
+	  break;
+	  
+	case 'A':
+	  /* The full weekday name. */
+	  if (tm.tm_wday < 0 || tm.tm_wday > 6)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, _mu_datetime_full_wday[tm.tm_wday],
+				  strlen (_mu_datetime_full_wday[tm.tm_wday]),
+				  NULL);
+	  break;
+	  
+	case 'b':
+	case 'h':
+	  /* The abbreviated month name. */
+	  if (tm.tm_mon < 0 || tm.tm_mon > 11)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, _mu_datetime_short_month[tm.tm_mon],
+				  strlen (_mu_datetime_short_month[tm.tm_mon]),
+				  NULL);
+	  break;
+	  
+	case 'B':
+	  /* The full month name. */
+	  if (tm.tm_mon < 0 || tm.tm_mon > 11)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_write (str, _mu_datetime_full_month[tm.tm_mon],
+				  strlen (_mu_datetime_full_month[tm.tm_mon]),
+				  NULL);
+	  break;
+	  
+	case 'c':
+	  /* The preferred date and time representation. */
+	  rc = mu_c_streamftime (str, "%a %b %e %H:%M:%S %Y", &tm, tz);
+	  break;
+	    
+	case 'C':
+	  /* The century number (year/100) as a 2-digit integer. */
+	  rc = mu_stream_printf (str, "%02d", (tm.tm_year + 1900) / 100);
+	  break;
+	  
+	case 'd':
+	  /* The day of the month as a decimal number (range 01 to 31). */
+	  if (tm.tm_mday < 1 || tm.tm_mday > 31)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_printf (str, "%02d", tm.tm_mday);
+	  break;
+	  
+	case 'D':
+	  /* Equivalent to %m/%d/%y. */
+	  rc = mu_c_streamftime (str, "%m/%d/%y", &tm, tz);
+	  break;
+	  
+	case 'e':
+	  /* Like %d, the day of the month as a decimal number, but a leading
+	     zero is replaced by a space. */
+	  if (tm.tm_mday < 1 || tm.tm_mday > 31)
+	    rc = ERANGE;
+	  else
+	    rc = mu_stream_printf (str, "%2d", tm.tm_mday);
+	  break;
+	  
+	case 'E':
+	  /* Modifier. The Single Unix Specification mentions %Ec, %EC, %Ex,
+	     %EX, %Ey, and %EY, which are supposed to use a corresponding
+	     locale-dependent alternative representation.
+	     A no-op in POSIX locale */
+	  goto restart;
+	  
+	case 'F':
+	  /* Equivalent to %Y-%m-%d (the ISO 8601 date format). */
+	  rc = mu_c_streamftime (str, "%Y-%m-%d", &tm, tz);
+	  break;
+	  	  
+	case 'V':
+	  /* The ISO 8601:1988 week number of the current year as a decimal
+	     number range 01 to 53, where week 1 is the first week that has
+	     at least 4 days in the current year, and with Monday as the
+	     first day of the week.
+	  */
+	case 'G':
+	  /* The ISO 8601 year with century as a decimal number.  The 4-digit
+	     year corresponding to the ISO week number (see  %V).  This has
+	     the same format and value as %y, except that if the ISO week
+	     number belongs to the previous or next year, that year is used
+	     instead.
+	  */
+	case 'g':
+	  /* Like  %G, but without century, that is, with a 2-digit year
+	     (00-99). */
+	  {
+	    int year = tm.tm_year + 1900;
+	    int days = ISO_8601_weekdays (tm.tm_yday, tm.tm_wday);
+
+	    if (days < 0)
+	      {
+		days = ISO_8601_weekdays (tm.tm_yday +
+					  mu_datetime_year_days (year - 1),
+					  tm.tm_wday);
+		year--;
+	      }
+	    else
+	      {
+		int d = ISO_8601_weekdays (tm.tm_yday -
+					   mu_datetime_year_days (year),
+					   tm.tm_wday);
+		if (d >= 0)
+		  {
+		    year++;
+		    days = d;
+		  }
+	      }
+
+	    switch (*fmt)
+	      {
+	      case 'V':
+		rc = mu_stream_printf (str, "%02d", days / 7 + 1);
+		break;
+
+	      case 'G':
+		rc = mu_stream_printf (str, "%4d", year);
+		break;
+
+	      case 'g':
+		rc = mu_stream_printf (str, "%02d", year % 100);
+	      }
+	  }
+	  break;
+	  
+	case 'H':
+	  /* The hour as a decimal number using a 24-hour clock (range 00 to
+	     23). */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_hour);
+	  break;
+	  
+	case 'I':
+	  /* The hour as a decimal number using a 12-hour clock (range 01 to
+	     12). */
+	  {
+	    unsigned n = tm.tm_hour % 12;
+	    rc = mu_stream_printf (str, "%02d", n == 0 ? 12 : n);
+	  }
+	  break;
+
+	case 'j':
+	  /* The day of the year as a decimal number (range 001 to 366). */
+	  rc = mu_stream_printf (str, "%03d", tm.tm_yday + 1);
+	  break;
+	  
+	case 'k':
+	  /* The hour (24-hour clock) as a decimal number (range 0 to 23);
+	     single digits are preceded by a blank. */
+	  rc = mu_stream_printf (str, "%2d", tm.tm_hour);
+	  break;
+	  
+	case 'l':
+	  /* The hour (12-hour clock) as a decimal number (range 1 to 12);
+	     single digits are preceded by a blank. */
+	  {
+	    unsigned n = tm.tm_hour % 12;
+	    rc = mu_stream_printf (str, "%2d", n == 0 ? 12 : n);
+	  }
+	  break;
+	  
+	case 'm':
+	  /* The month as a decimal number (range 01 to 12). */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_mon + 1);
+	  break;
+				   
+	case 'M':
+	  /* The minute as a decimal number (range 00 to 59). */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_min);
+	  break;
+	  
+	case 'n':
+	  /* A newline character. */
+	  rc = mu_stream_write (str, "\n", 1, NULL);
+	  break;
+	  
+	case 'O':
+	  /* Modifier.  The Single Unix Specification mentions %Od, %Oe, %OH,
+	     %OI, %Om, %OM, %OS, %Ou, %OU, %OV, %Ow, %OW, and %Oy, which are
+	     supposed to use alternative numeric symbols.
+
+	     Hardly of any use for our purposes, hence a no-op. */
+	  goto restart;
+	  
+	case 'p':
+	  /* Either "AM" or "PM" according to the given time value.
+	     Noon is treated as "PM" and midnight as "AM". */
+	  rc = mu_stream_write (str,
+				tm.tm_hour < 12 ? "AM" : "PM",
+				2, NULL);
+	  break;
+				
+	case 'P':
+	  /* Like %p but in lowercase: "am" or "pm". */
+	  rc = mu_stream_write (str,
+				tm.tm_hour < 12 ? "am" : "pm",
+				2, NULL);
+	  break;
+	  
+	case 'r':
+	  /* The time in a.m. or p.m. notation, i.e. %I:%M:%S %p. */
+	  rc = mu_c_streamftime (str, "%I:%M:%S %p", &tm, tz);
+	  break;
+	  
+	case 'R':
+	  /* The time in 24-hour notation (%H:%M) */
+	  rc = mu_c_streamftime (str, "%H:%M", &tm, tz);
+	  break;
+	  
+	case 's':
+	  /* The number of seconds since the Epoch */
+	  rc = mu_stream_printf (str, "%lu",
+				 (unsigned long) mu_tm2time (&tm, tz));
+	  break;
+	  
+	case 'S':
+	  /* The second as a decimal number (range 00 to 60) */
+	  rc = mu_stream_printf (str, "%02d", tm.tm_sec);
+	  break;
+	  
+	case 't':
+	  /* A tab character. */
+	  rc = mu_stream_write (str, "\t", 1, NULL);
+	  break;
+	  
+	case 'T':
+	  /* The time in 24-hour notation (%H:%M:%S) */
+	  rc = mu_c_streamftime (str, "%H:%M:%S", &tm, tz);
+	  break;
+	  
+	case 'u':
+	  /* The day of the week as a decimal, range 1 to 7, Monday being 1.
+	   */
+	  rc = mu_stream_printf (str, "%1d",
+				 tm.tm_wday == 0 ? 7 : tm.tm_wday);
+	  break;
+	  
+	case 'U':
+	  /* The week number of the current year as a decimal number, range
+	     00 to 53, starting with the first Sunday as the first day of
+	     week 01.
+	  */
+	  rc = mu_stream_printf (str, "%02d",
+				 (tm.tm_yday - tm.tm_wday + 7) / 7);
+	  break;
+	  
+	case 'w':
+	  /* The day of the week as a decimal, range 0 to 6, Sunday being 0.
+	   */
+	  rc = mu_stream_printf (str, "%01d", tm.tm_wday);
+	  break;
+	  
+	case 'W':
+	  /* The week number of the current year as a decimal number, range
+	     00 to 53, starting with the first Monday as the first day of
+	     week 01. */
+	  rc = mu_stream_printf (str, "%02d", 
+			 (tm.tm_yday - (tm.tm_wday - 1 + 7) % 7 + 7) / 7);
+	  break;
+
+	  /* The preferred date representation without the time:
+	     equivalent to %D */
+	case 'x':
+	  rc = mu_c_streamftime (str, "%m/%d/%y", &tm, tz);
+	  break;
+	  
+	case 'X':
+	  /* The preferred date representation without the date */
+	  rc = mu_c_streamftime (str, "%H:%M:%S", &tm, tz);
+	  break;
+
+	case 'y':
+	  /* The year as a decimal number without a century (range 00 to 99).
+	   */
+	  rc = mu_stream_printf (str, "%02d", (tm.tm_year + 1900) % 100);
+	  break;
+	  
+	case 'Y':
+	  /* The year as a decimal number including the century. */
+	  rc = mu_stream_printf (str, "%d", tm.tm_year + 1900);
+	  break;
+	  
+	case 'z':
+	case 'Z':
+	  /* The time-zone as hour offset from GMT, for formatting RFC-822
+	     dates (e.g. "%a, %d %b %Y %H:%M:%S %z") */
+	  {
+	    int utc_off = tz ? tz->utc_offset : mu_utc_offset ();
+	    int sign;
+	    if (utc_off < 0)
+	      {
+		sign = '-';
+		utc_off = - utc_off;
+	      }
+	    else
+	      sign = '+';
+	    utc_off /= 60;
+	    rc = mu_stream_printf (str, "%c%02u%02u", sign,
+				   utc_off / 60, utc_off % 60);
+	  }
+	  break;
+	  
+	case '%':
+	  /* A literal '%' character. */
+	  rc = mu_stream_write (str, "%", 1, NULL);
+	  break;
+
+	case '$':
+	  /* Ignored for compatibilty with mu_scan_datetime */
+	  break;
+	  
+	case '+':
+	  /* Not supported (date and time in date(1) format. */
+	default:
+	  rc = mu_stream_write (str, fmt-1, 2, NULL);
+	  break;
+	}
+      fmt++;
+    }
+  /* Restore input tm */
+  *input_tm = tm;
+  return rc;
+}
diff --git a/libmailutils/base/strftime.c b/libmailutils/datetime/strftime.c
similarity index 97%
rename from libmailutils/base/strftime.c
rename to libmailutils/datetime/strftime.c
index 5c1d89e..01b6faf 100644
--- a/libmailutils/base/strftime.c
+++ b/libmailutils/datetime/strftime.c
@@ -18,7 +18,7 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/stream.h>
 #include <mailutils/errno.h>
 #include <mailutils/cstr.h>
diff --git a/libmailutils/diag/muerror.c b/libmailutils/datetime/tab.c
similarity index 56%
copy from libmailutils/diag/muerror.c
copy to libmailutils/datetime/tab.c
index e8a95a6..2ff9293 100644
--- a/libmailutils/diag/muerror.c
+++ b/libmailutils/datetime/tab.c
@@ -1,6 +1,6 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2005, 2007, 2010, 2011 Free Software
-   Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2007, 2009, 2010, 2011 Free
+   Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -20,32 +20,26 @@
 # include <config.h>
 #endif
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <syslog.h>
-#include <string.h>
-#include <mailutils/error.h>
-
-
-/* Historic shortcuts for mu_diag_ functions */
-
-int
-mu_verror (const char *fmt, va_list ap)
+const char *_mu_datetime_short_month[] =
 {
-  mu_diag_voutput (MU_DIAG_ERROR, fmt, ap);
-  return 0;
-}
+  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+};
 
-int
-mu_error (const char *fmt, ...)
+const char *_mu_datetime_full_month[] =
 {
-  va_list ap;
-  va_start (ap, fmt);
-  mu_verror (fmt, ap);
-  va_end (ap);
-  return 0;
-}
-
-
+  "January", "February", "March", "April",
+  "May", "June", "July", "August",
+  "September", "October", "November", "December"
+};
 
+const char *_mu_datetime_short_wday[] =
+{
+  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+};
 
+const char *_mu_datetime_full_wday[] =
+{
+  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
+  "Friday", "Saturday"
+};
diff --git a/examples/muemail.c b/libmailutils/datetime/unixtime.c
similarity index 57%
copy from examples/muemail.c
copy to libmailutils/datetime/unixtime.c
index 39a8099..a893117 100644
--- a/examples/muemail.c
+++ b/libmailutils/datetime/unixtime.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2007, 2010, 2011 Free Software
+   Copyright (C) 1999, 2000, 2001, 2007, 2009, 2010, 2011 Free Software
    Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
@@ -18,32 +18,21 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-#include <stdlib.h>
-#include <stdio.h>
-#include <mailutils/util.h>
-#include "mailutils/libargp.h"
+#include <time.h>
+#include <mailutils/datetime.h>
 
-const char *capa[] = {
-  "address",
-  NULL
-};
+#define SECS_PER_DAY 86400
 
-int
-main (int argc, char *argv[])
-{
-  int arg = 1;
-
-  if (mu_app_init (NULL, capa, NULL, argc, argv, 0, &arg, NULL))
-    exit (1);
-
-  if (!argv[arg])
-    printf ("current user -> %s\n", mu_get_user_email (0));
-  else
-    {
-      for (; argv[arg]; arg++)
-        printf ("%s -> %s\n", argv[arg], mu_get_user_email (argv[arg]));
-    }
+#define JD_OF_EPOCH 2440588
 
-  return 0;
+/* Convert struct tm into time_t, taking into account timezone offset. */
+/* FIXME: Ignores DST */
+time_t
+mu_tm2time (struct tm *tm, struct mu_timezone *tz)
+{
+  int jd = mu_datetime_julianday (tm->tm_year + 1900, tm->tm_mon + 1,
+				  tm->tm_mday);
+  return (jd - JD_OF_EPOCH) * SECS_PER_DAY +
+         (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
+          - (tz ? tz->utc_offset : 0);
 }
-
diff --git a/mail/list.c b/libmailutils/datetime/utcoff.c
similarity index 67%
copy from mail/list.c
copy to libmailutils/datetime/utcoff.c
index b7ec73e..cfa3b89 100644
--- a/mail/list.c
+++ b/libmailutils/datetime/utcoff.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2001, 2005, 2007, 2010, 2011 Free Software
+   Copyright (C) 1999, 2000, 2001, 2007, 2009, 2010, 2011 Free Software
    Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
@@ -15,16 +15,18 @@
    You should have received a copy of the GNU General Public License
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
-#include "mail.h"
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <time.h>
 
-/*
- * l[ist]
- * *
- */
-
-int
-mail_list (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+/* Convert time 0 at UTC to our localtime, that tells us the offset
+   of our current timezone from UTC. */
+time_t
+mu_utc_offset (void)
 {
-  mail_command_list ();
-  return 0;
+  time_t t = 0;
+  struct tm *tm = gmtime (&t);
+
+  return - mktime (tm);
 }
diff --git a/libmailutils/list/push.c b/libmailutils/datetime/yd.c
similarity index 79%
copy from libmailutils/list/push.c
copy to libmailutils/datetime/yd.c
index c6bead7..abdee16 100644
--- a/libmailutils/list/push.c
+++ b/libmailutils/datetime/yd.c
@@ -12,18 +12,18 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see 
+   Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 #include <stdlib.h>
-#include <mailutils/sys/list.h>
-#include <mailutils/errno.h>
+#include <mailutils/datetime.h>
 
+/* Return number of days in year */
 int
-mu_list_push (mu_list_t list, void *item)
+mu_datetime_year_days (int year)
 {
-  return mu_list_append (list, item);
+  return mu_datetime_dayofyear (year, 12, 31);
 }
diff --git a/libmailutils/imapio/time.c b/libmailutils/imapio/time.c
index 02f0d8a..089a7db 100644
--- a/libmailutils/imapio/time.c
+++ b/libmailutils/imapio/time.c
@@ -19,7 +19,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/errno.h>
 #include <mailutils/sys/imapio.h>
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 
 int
 mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
diff --git a/libmailutils/mailbox/msgenv.c b/libmailutils/mailbox/msgenv.c
index a478891..57f3a07 100644
--- a/libmailutils/mailbox/msgenv.c
+++ b/libmailutils/mailbox/msgenv.c
@@ -28,6 +28,7 @@
 #include <mailutils/errno.h>
 #include <mailutils/envelope.h>
 #include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/header.h>
 #include <mailutils/mu_auth.h>
 #include <mailutils/address.h>
@@ -49,14 +50,14 @@ message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
 
   if (buf == NULL || len == 0)
     {
-      n = MU_ENVELOPE_DATE_LENGTH;
+      n = MU_DATETIME_FROM_LENGTH;
     }
   else
     {
-      char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
+      char tmpbuf[MU_DATETIME_FROM_LENGTH+1];
       t = time (NULL);
       n = mu_strftime (tmpbuf, sizeof tmpbuf, 
-                       MU_ENVELOPE_DATE_FORMAT, localtime (&t));
+                       MU_DATETIME_FROM, localtime (&t));
       n = mu_cpystr (buf, tmpbuf, len);
     }
   if (pnwrite)
diff --git a/libmailutils/stream/message_stream.c b/libmailutils/stream/message_stream.c
index 4bbbe9e..557ce76 100644
--- a/libmailutils/stream/message_stream.c
+++ b/libmailutils/stream/message_stream.c
@@ -36,6 +36,7 @@
 #include <mailutils/body.h>
 #include <mailutils/stream.h>
 #include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/errno.h>
 #include <mailutils/error.h>
 #include <mailutils/cctype.h>
@@ -291,11 +292,11 @@ _message_open (mu_stream_t stream)
 	{
 	  struct tm *tm;
 	  time_t t;
-	  char date[80]; /* FIXME: This size is way too big */
+	  char date[MU_DATETIME_FROM_LENGTH+1];
 	  
-	  time(&t);
-	  tm = gmtime(&t);
-	  mu_strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y", tm);
+	  time (&t);
+	  tm = gmtime (&t);
+	  mu_strftime (date, sizeof (date), MU_DATETIME_FROM, tm);
 	  env_date = strdup (date);
 	}
       
diff --git a/libmailutils/tests/scantime.at b/libmailutils/tests/scantime.at
index 2db8540..a7a6f85 100644
--- a/libmailutils/tests/scantime.at
+++ b/libmailutils/tests/scantime.at
@@ -34,30 +34,30 @@ SCANTIME([Envelope (From) time],[envelope],
 [%a %b %e %H:%M:%S %Y],
 [Tue May  3 13:25:26 2011
 Fri Nov 11 11:55:01 2011],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=0
-sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,yday=315,tz=0
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=0
+sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,yday=314,tz=0
 ])
 
 SCANTIME([IMAP INTERNALDATE],[imap-internaldate],
 [%d-%b-%Y %H:%M:%S %z],
 [03-May-2011 13:25:26 +0100
 11-Nov-2011 11:55:01 +0100],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
-sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,yday=315,tz=3600
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=3600
+sec=1,min=55,hour=11,mday=11,mon=10,year=111,wday=5,yday=314,tz=3600
 ])
 
 SCANTIME([IMAP INTERNALDATE (optional time)],[imap-search],
 [%d-%b-%Y%$ %H:%M:%S %z],
 [03-May-2011 13:25:26 +0100
 03-May-2011],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
-sec=0,min=0,hour=0,mday=3,mon=4,year=111,wday=2,yday=123,tz=0
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=3600
+sec=0,min=0,hour=0,mday=3,mon=4,year=111,wday=2,yday=122,tz=0
 ])
 
 SCANTIME([RFC-822 Strict],[rfc822-strict],
 [%a, %d %b %Y %H:%M:%S %z],
 [Tue, 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ])
 
 SCANTIME([RFC-822],[rfc822],
@@ -66,10 +66,10 @@ SCANTIME([RFC-822],[rfc822],
 03 May 2011 13:25:26 +0200
 Tue, 03 May 2011 13:25 +0200
 03 May 2011 13:25 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=0,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=0,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=0,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=0,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ])
 
 SCANTIME([Any char],[anychar],
@@ -78,23 +78,23 @@ SCANTIME([Any char],[anychar],
 Tue: 03 May 2011 13:25:26 +0200
 Tue; 03 May 2011 13:25:26 +0200
 Tue 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ])
 
 SCANTIME([Percent],[percent],
 [%d%%%b%%%Y %H:%M:%S %z],
 [03%May%2011 13:25:26 +0100],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=3600
 ])
 
 SCANTIME([Fixed WS],[fixws],
 [%d-%b-%Y%\ %H:%M:%S %z],
 [03-May-2011 13:25:26 +0100
 03-May-2011  13:25:26 +0100],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=3600
 ],
 [scantime: 2: parse failed near  13:25:26 +0100
 ])
@@ -104,15 +104,15 @@ SCANTIME([endp return],[endp],
 [Tue, 03 May 2011 13:25:26 +0100 other data
 ],
 [# 1: stopped at  other data
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=3600
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=3600
 ])
 
 SCANTIME([Optional blocks],[opt],
 [[%[%a, %]%d %b %Y %H:%M:%S %z]],
 [Tue, 03 May 2011 13:25:26 +0200
 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ])
 
 SCANTIME([Nested optional blocks],[nested-opt],
@@ -120,9 +120,9 @@ SCANTIME([Nested optional blocks],[nested-opt],
 [Tue, 03 May 2011 13:25:26 +0200
 Tue 03 May 2011 13:25:26 +0200
 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ])
 
 SCANTIME([Optional alternatives],[opt-alt],
@@ -131,9 +131,9 @@ SCANTIME([Optional alternatives],[opt-alt],
 Tue: 03 May 2011 13:25:26 +0200
 Tue 03 May 2011 13:25:26 +0200
 Tue; 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ],
 [scantime: 4: parse failed near ; 03 May 2011 13:25:26 +0200
 ])
@@ -145,9 +145,9 @@ Tue: 03 May 2011 13:25:26 +0200
 Tue/ 03 May 2011 13:25:26 +0200
 Tue 03 May 2011 13:25:26 +0200
 Tue; 03 May 2011 13:25:26 +0200],
-[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
-sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=123,tz=7200
+[sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
+sec=26,min=25,hour=13,mday=3,mon=4,year=111,wday=2,yday=122,tz=7200
 ],
 [scantime: 4: parse failed near  03 May 2011 13:25:26 +0200
 scantime: 5: parse failed near ; 03 May 2011 13:25:26 +0200
diff --git a/libmailutils/tests/scantime.c b/libmailutils/tests/scantime.c
index 962a7b3..d889add 100644
--- a/libmailutils/tests/scantime.c
+++ b/libmailutils/tests/scantime.c
@@ -23,7 +23,7 @@
 #include <string.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/stream.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
@@ -92,8 +92,6 @@ main (int argc, char **argv)
       mu_printf ("sec=%d,min=%d,hour=%d,mday=%d,mon=%d,year=%d,wday=%d,yday=%d,tz=%d\n",
 		 tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday, tm.tm_mon,
 		 tm.tm_year, tm.tm_wday, tm.tm_yday, tz.utc_offset);
-		 
-      /*mu_c_streamftime (mu_strout, "%c %z%n", &tm, &tz);*/
     }
   
   if (rc)
diff --git a/libmailutils/tests/strftime.c b/libmailutils/tests/strftime.c
index 5a57666..8866a60 100644
--- a/libmailutils/tests/strftime.c
+++ b/libmailutils/tests/strftime.c
@@ -23,7 +23,7 @@
 #include <string.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/stream.h>
 #include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
diff --git a/libmu_scm/mu_message.c b/libmu_scm/mu_message.c
index 14d8821..7f8342f 100644
--- a/libmu_scm/mu_message.c
+++ b/libmu_scm/mu_message.c
@@ -79,7 +79,7 @@ mu_scm_message_print (SCM message_smob, SCM port, scm_print_state * pstate)
   const char *p;
   size_t m_size = 0, m_lines = 0;
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char datebuf[sizeof ("Mon Jan 01 00:00")]; /* Warning: length must be > 9 */
 
   mu_message_get_envelope (mum->msg, &env);
@@ -404,7 +404,7 @@ SCM_DEFINE_PUBLIC (scm_mu_message_get_envelope_date, "mu-message-get-envelope-da
   int status;
   const char *sdate;
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   SCM_ASSERT (mu_scm_is_message (mesg), mesg, SCM_ARG1, FUNC_NAME);
   msg = mu_scm_message_get (mesg);
diff --git a/libmu_scm/mu_scm.h b/libmu_scm/mu_scm.h
index 5e68e91..de946c4 100644
--- a/libmu_scm/mu_scm.h
+++ b/libmu_scm/mu_scm.h
@@ -38,6 +38,7 @@
 #include <mailutils/address.h>
 #include <mailutils/registrar.h>
 #include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/stream.h>
 #include <mailutils/debug.h>
 #include <mailutils/attribute.h>
diff --git a/libmu_sieve/actions.c b/libmu_sieve/actions.c
index c0a50d1..5f2ff51 100644
--- a/libmu_sieve/actions.c
+++ b/libmu_sieve/actions.c
@@ -188,7 +188,7 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
   char datestr[80];
   time_t t = time (NULL);
   struct tm tm, *tmp;
-  mu_timezone tz;
+  struct mu_timezone tz;
   mu_envelope_t env;
   const char *p;
   
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index 42a35ef..559a480 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -29,6 +29,7 @@
 #include <mailutils/io.h>
 #include <mailutils/filter.h>
 #include <mailutils/cctype.h>
+#include <mailutils/datetime.h>
 
 #define ATTRIBUTE_IS_DELETED(flag)        (flag & MU_ATTRIBUTE_DELETED)
 #define ATTRIBUTE_IS_EQUAL(flag1, flag2)  (flag1 == flag2)
@@ -859,22 +860,23 @@ restore_date (mu_message_t msg, char **pret)
   mu_header_t hdr;
   const char *s;
   char *date = NULL;
-  time_t t;
+  struct tm tm;
+  struct mu_timezone tz;
   
   if (mu_message_get_header (msg, &hdr) == 0)
     mu_header_sget_value (hdr, MU_HEADER_DATE, &s);
 
-  if (s && mu_parse_date (s, &t, NULL))
+  if (s && mu_scan_datetime (s, MU_DATETIME_SCAN_RFC822, &tm, &tz, NULL) == 0)
     {
-      char datebuf[MU_ENVELOPE_DATE_LENGTH+1];
+      char datebuf[MU_DATETIME_FROM_LENGTH+1];
       
-      /* FIXME: 1. Preserve TZ info */
-      mu_strftime (datebuf, sizeof datebuf, MU_ENVELOPE_DATE_FORMAT,
-		   localtime (&t));
+      /* FIXME: Compensate for TZ differences. */
+      mu_strftime (datebuf, sizeof datebuf, MU_DATETIME_FROM, &tm);
       date = strdup (datebuf);
     }
   else
     {
+      time_t t;
       time (&t);
       date = strdup (ctime (&t));
     }
diff --git a/mail/from.c b/mail/from.c
index e4aeda2..ddfd952 100644
--- a/mail/from.c
+++ b/mail/from.c
@@ -208,7 +208,7 @@ hdr_date (struct header_call_args *args, void *data)
     {
       const char *p;
       struct tm tm;
-      mu_timezone tz;
+      struct mu_timezone tz;
       mu_envelope_t env;
       
       mu_message_get_envelope (args->msg, &env);
diff --git a/mail/mail.h b/mail/mail.h
index e1c9d4a..92b3582 100644
--- a/mail/mail.h
+++ b/mail/mail.h
@@ -71,6 +71,7 @@
 #include <mailutils/mailer.h>
 #include <mailutils/message.h>
 #include <mailutils/util.h>
+#include <mailutils/datetime.h>
 #include <mailutils/registrar.h>
 #include <mailutils/stream.h>
 #include <mailutils/url.h>
diff --git a/mh/mh.h b/mh/mh.h
index 81c717b..0d883b9 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -59,6 +59,7 @@
 #include <mailutils/prog.h>
 #include <mailutils/mh.h>
 #include <mailutils/stdstream.h>
+#include <mailutils/datetime.h>
 
 #include <mu_umaxtostr.h>
 
diff --git a/mh/mh_format.c b/mh/mh_format.c
index 0e51496..9633cdb 100644
--- a/mh/mh_format.c
+++ b/mh/mh_format.c
@@ -1160,7 +1160,7 @@ builtin_putnumf (struct mh_machine *mach)
 }
 
 static int
-_parse_date (struct mh_machine *mach, struct tm *tm, mu_timezone *tz)
+_parse_date (struct mh_machine *mach, struct tm *tm, struct mu_timezone *tz)
 {
   char *date = strobj_ptr (&mach->arg_str);
   const char *p = date;
@@ -1183,7 +1183,7 @@ static void
 builtin_sec (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1196,7 +1196,7 @@ static void
 builtin_min (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1209,7 +1209,7 @@ static void
 builtin_hour (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1222,7 +1222,7 @@ static void
 builtin_wday (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1235,7 +1235,7 @@ static void
 builtin_day (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char buf[80];
   
   if (_parse_date (mach, &tm, &tz))
@@ -1251,7 +1251,7 @@ static void
 builtin_weekday (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char buf[80];
   
   if (_parse_date (mach, &tm, &tz))
@@ -1268,7 +1268,7 @@ static void
 builtin_sday (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
 
   /*FIXME: more elaborate check needed */
   if (_parse_date (mach, &tm, &tz))
@@ -1282,7 +1282,7 @@ static void
 builtin_mday (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1295,7 +1295,7 @@ static void
 builtin_yday (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1308,7 +1308,7 @@ static void
 builtin_mon (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1321,7 +1321,7 @@ static void
 builtin_month (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char buf[80];
   
   if (_parse_date (mach, &tm, &tz))
@@ -1337,7 +1337,7 @@ static void
 builtin_lmonth (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char buf[80];
   
   if (_parse_date (mach, &tm, &tz))
@@ -1353,7 +1353,7 @@ static void
 builtin_year (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1366,7 +1366,7 @@ static void
 builtin_zone (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1379,7 +1379,7 @@ static void
 builtin_tzone (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1410,7 +1410,7 @@ static void
 builtin_szone (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
 
   /*FIXME: more elaborate check needed */
   if (_parse_date (mach, &tm, &tz))
@@ -1438,7 +1438,7 @@ static void
 builtin_dst (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
 
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1454,7 +1454,7 @@ static void
 builtin_clock (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
 
   if (_parse_date (mach, &tm, &tz))
     return;
@@ -1466,7 +1466,7 @@ void
 builtin_rclock (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   time_t now = time (NULL);
   
   if (_parse_date (mach, &tm, &tz))
@@ -1493,7 +1493,7 @@ static void
 date_cvt (struct mh_machine *mach, int pretty)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   char buf[80];
   int i, len;
   const char *tzname = NULL;
@@ -1567,7 +1567,7 @@ static void
 builtin_nodate (struct mh_machine *mach)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   
   mach->arg_num = _parse_date (mach, &tm, &tz);
 }
diff --git a/mh/sortm.c b/mh/sortm.c
index 841e4eb..4e6d052 100644
--- a/mh/sortm.c
+++ b/mh/sortm.c
@@ -336,7 +336,7 @@ static int
 _parse_822_date (char *date, time_t * timep)
 {
   struct tm tm;
-  mu_timezone tz;
+  struct mu_timezone tz;
   const char *p = date;
 
   if (mu_parse822_date_time (&p, date + strlen (date), &tm, &tz) == 0)
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 314490e..201f63a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -133,7 +133,6 @@ libmailutils/stream/logstream.c
 libmailutils/server/ipsrv.c
 libmailutils/server/msrv.c
 
-libmailutils/mailbox/message.c
 libmailutils/mailbox/mbx_default.c
 libmailutils/mailer/mailer.c
 
diff --git a/readmsg/readmsg.c b/readmsg/readmsg.c
index 1ae2abc..367c746 100644
--- a/readmsg/readmsg.c
+++ b/readmsg/readmsg.c
@@ -179,13 +179,13 @@ print_unix_header (mu_message_t message)
   
   if (mu_envelope_sget_date (envelope, &buf))
     { 
-      char datebuf[MU_ENVELOPE_DATE_LENGTH+1];
+      char datebuf[MU_DATETIME_FROM_LENGTH+1];
       time_t t;
       struct tm *tm;
 
       t = time (NULL);
       tm = localtime (&t);
-      mu_strftime (datebuf, sizeof datebuf, "%a %b %d %H:%M:%S %Y", tm);
+      mu_strftime (datebuf, sizeof datebuf, MU_DATETIME_FROM, tm);
       buf = datebuf;
     }
 
diff --git a/readmsg/readmsg.h b/readmsg/readmsg.h
index 8bfa61f..3d29088 100644
--- a/readmsg/readmsg.h
+++ b/readmsg/readmsg.h
@@ -50,7 +50,7 @@
 #include <mailutils/error.h>
 #include <mailutils/envelope.h>
 #include <mailutils/wordsplit.h>
-#include <mailutils/util.h>
+#include <mailutils/datetime.h>
 
 int msglist (mu_mailbox_t mbox, int show_all, int argc, char **argv, int **set, int *n);
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 08 12:51:44 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RYi8G-0007Yv-AU
	for mharc-commit-mailutils@gnu.org; Thu, 08 Dec 2011 12:51:44 -0500
Received: from eggs.gnu.org ([140.186.70.92]:41550)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYi8E-0007Yn-05
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 12:51:43 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYi8C-0004sU-0R
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 12:51:41 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:52133)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYi8B-0004sJ-V1
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 12:51:39 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYi8A-00073t-1T
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 17:51:39 +0000
Date: Thu, 08 Dec 2011 17:51:38 +0000
Message-Id: <E1RYi8A-00073t-1T@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-511-g76cc330
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 7efa4777bddb68299355dce1f4125533d6467023
X-Git-Newrev: 76cc330864a9680365123c86f8d5a394f952dcc5
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 08 Dec 2011 17:51:43 -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=76cc330864a9680365123c86f8d5a394f952dcc5

The branch, master has been updated
       via  76cc330864a9680365123c86f8d5a394f952dcc5 (commit)
       via  912224f8b9fd7638fd48b86670fffca693c2bf9f (commit)
      from  7efa4777bddb68299355dce1f4125533d6467023 (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 76cc330864a9680365123c86f8d5a394f952dcc5
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 19:49:49 2011 +0200

    New function mu_datetime_tz_local.
    
    The function initializes mu_timezone structure to the local timezone.
    
    * include/mailutils/datetime.h (mu_utc_offset): Change return type.
    (mu_datetime_tz_local): New proto.
    * libmailutils/datetime/tzlocal.c: New file.
    * libmailutils/datetime/Makefile.am: Add new file.
    * libmailutils/datetime/scantime.c (mu_scan_datetime): Use
    mu_datetime_tz_local to initialize local TZ.
    * mh/mh_format.c (_parse_date): Likewise.
    * libmailutils/datetime/utcoff.c (mu_utc_offset): Returns int.
    * libmu_sieve/actions.c (mime_create_ds): Use mu_c_streamftime
    to format time directly to stream.

commit 912224f8b9fd7638fd48b86670fffca693c2bf9f
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 19:23:03 2011 +0200

    Rename mu_tm2time to mu_datetime_to_utc.

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

Summary of changes:
 imap4d/util.c                                  |    6 ++--
 include/mailutils/datetime.h                   |    5 ++-
 libmailutils/datetime/Makefile.am              |    1 +
 libmailutils/datetime/scantime.c               |    5 +---
 libmailutils/datetime/streamftime.c            |    2 +-
 mail/save.c => libmailutils/datetime/tzlocal.c |   21 ++++++++-----------
 libmailutils/datetime/unixtime.c               |    5 +--
 libmailutils/datetime/utcoff.c                 |    2 +-
 libmu_sieve/actions.c                          |   25 ++++++++++++-----------
 libproto/imap/mbox.c                           |    2 +-
 mh/mh_format.c                                 |    6 ++--
 mh/sortm.c                                     |    2 +-
 12 files changed, 39 insertions(+), 43 deletions(-)
 copy mail/save.c => libmailutils/datetime/tzlocal.c (74%)

diff --git a/imap4d/util.c b/imap4d/util.c
index 2482b9b..4165f42 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -379,7 +379,7 @@ util_parse_internal_date (char *date, time_t *timep,
 
   adjust_tm (&tm, &tz, flag);
   
-  time = mu_tm2time (&tm, &tz);
+  time = mu_datetime_to_utc (&tm, &tz);
   if (time == (time_t) - 1)
     return 2;
 
@@ -398,7 +398,7 @@ util_parse_822_date (const char *date, time_t *timep,
   if (mu_parse822_date_time (&p, date + strlen (date), &tm, &tz) == 0)
     {
       adjust_tm (&tm, &tz, flag);
-      *timep = mu_tm2time (&tm, &tz);
+      *timep = mu_datetime_to_utc (&tm, &tz);
       return 0;
     }
   return 1;
@@ -414,7 +414,7 @@ util_parse_ctime_date (const char *date, time_t *timep,
   if (mu_scan_datetime (date, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
     {
       adjust_tm (&tm, &tz, flag);
-      *timep = mu_tm2time (&tm, &tz);
+      *timep = mu_datetime_to_utc (&tm, &tz);
       return 0;
     }
   return 1;
diff --git a/include/mailutils/datetime.h b/include/mailutils/datetime.h
index de00618..bc1aa98 100644
--- a/include/mailutils/datetime.h
+++ b/include/mailutils/datetime.h
@@ -58,8 +58,9 @@ struct mu_timezone
 
 int mu_parse_date (const char *p, time_t *rettime, const time_t *now);
 
-time_t mu_utc_offset (void);
-time_t mu_tm2time (struct tm *timeptr, struct mu_timezone *tz);
+int mu_utc_offset (void);
+void mu_datetime_tz_local (struct mu_timezone *tz);
+time_t mu_datetime_to_utc (struct tm *timeptr, struct mu_timezone *tz);
 size_t mu_strftime (char *s, size_t max, const char *format, struct tm *tm);
 
 int mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *tm,
diff --git a/libmailutils/datetime/Makefile.am b/libmailutils/datetime/Makefile.am
index f9a4f45..943b32b 100644
--- a/libmailutils/datetime/Makefile.am
+++ b/libmailutils/datetime/Makefile.am
@@ -25,6 +25,7 @@ libdatetime_la_SOURCES = \
  streamftime.c\
  strftime.c\
  tab.c\
+ tzlocal.c\
  unixtime.c\
  utcoff.c\
  yd.c
diff --git a/libmailutils/datetime/scantime.c b/libmailutils/datetime/scantime.c
index 670052b..7a4803f 100644
--- a/libmailutils/datetime/scantime.c
+++ b/libmailutils/datetime/scantime.c
@@ -325,10 +325,7 @@ mu_scan_datetime (const char *input, const char *fmt,
 #endif
   /* provide default timezone, in case it is not supplied in input */
   if (tz)
-    {
-      memset (tz, 0, sizeof *tz);
-      tz->utc_offset = mu_utc_offset ();
-    }
+    mu_datetime_tz_local (tz);
 
   /* Skip leading whitespace */
   input = mu_str_skip_class (input, MU_CTYPE_BLANK);
diff --git a/libmailutils/datetime/streamftime.c b/libmailutils/datetime/streamftime.c
index e67d7d4..311fe00 100644
--- a/libmailutils/datetime/streamftime.c
+++ b/libmailutils/datetime/streamftime.c
@@ -298,7 +298,7 @@ mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *input_tm,
 	case 's':
 	  /* The number of seconds since the Epoch */
 	  rc = mu_stream_printf (str, "%lu",
-				 (unsigned long) mu_tm2time (&tm, tz));
+				 (unsigned long) mu_datetime_to_utc (&tm, tz));
 	  break;
 	  
 	case 'S':
diff --git a/mail/save.c b/libmailutils/datetime/tzlocal.c
similarity index 74%
copy from mail/save.c
copy to libmailutils/datetime/tzlocal.c
index 50dd061..cc561f5 100644
--- a/mail/save.c
+++ b/libmailutils/datetime/tzlocal.c
@@ -1,6 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2001, 2007, 2010, 2011 Free Software Foundation,
-   Inc.
+   Copyright (C) 2011 Free Software Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -15,16 +14,14 @@
    You should have received a copy of the GNU General Public License
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
-#include "mail.h"
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <mailutils/datetime.h>
 
-/*
- * s[ave] [file]
- * s[ave] [msglist] file
- * S[ave] [msglist]
- */
-
-int
-mail_save (int argc, char **argv)
+void
+mu_datetime_tz_local (struct mu_timezone *tz)
 {
-  return mail_copy0 (argc, argv, 1);
+  tz->utc_offset = mu_utc_offset ();
+  tz->tz_name = NULL;
 }
diff --git a/libmailutils/datetime/unixtime.c b/libmailutils/datetime/unixtime.c
index a893117..38c1590 100644
--- a/libmailutils/datetime/unixtime.c
+++ b/libmailutils/datetime/unixtime.c
@@ -25,10 +25,9 @@
 
 #define JD_OF_EPOCH 2440588
 
-/* Convert struct tm into time_t, taking into account timezone offset. */
-/* FIXME: Ignores DST */
+/* Convert struct tm into UTC. */
 time_t
-mu_tm2time (struct tm *tm, struct mu_timezone *tz)
+mu_datetime_to_utc (struct tm *tm, struct mu_timezone *tz)
 {
   int jd = mu_datetime_julianday (tm->tm_year + 1900, tm->tm_mon + 1,
 				  tm->tm_mday);
diff --git a/libmailutils/datetime/utcoff.c b/libmailutils/datetime/utcoff.c
index cfa3b89..56264a7 100644
--- a/libmailutils/datetime/utcoff.c
+++ b/libmailutils/datetime/utcoff.c
@@ -22,7 +22,7 @@
 
 /* Convert time 0 at UTC to our localtime, that tells us the offset
    of our current timezone from UTC. */
-time_t
+int
 mu_utc_offset (void)
 {
   time_t t = 0;
diff --git a/libmu_sieve/actions.c b/libmu_sieve/actions.c
index 5f2ff51..ebc3015 100644
--- a/libmu_sieve/actions.c
+++ b/libmu_sieve/actions.c
@@ -185,12 +185,11 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
   mu_header_t hdr;
   mu_body_t body;
   char *email;
-  char datestr[80];
-  time_t t = time (NULL);
   struct tm tm, *tmp;
   struct mu_timezone tz;
   mu_envelope_t env;
   const char *p;
+  time_t t = time (NULL);
   
   mu_message_create (&newmsg, NULL);
   mu_message_get_header (newmsg, &hdr); 
@@ -202,15 +201,17 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
   mu_message_get_envelope (orig, &env);
   if (mu_envelope_sget_date (env, &p) == 0
       && mu_scan_datetime (p, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
-    t = mu_tm2time (&tm, &tz);
+    {
+      tmp = &tm;
+    }
   else
-    /* Use local time instead */
-    t = time (NULL);
-  tmp = localtime (&t);
+    {
+      tmp = localtime (&t);
+      mu_datetime_tz_local (&tz);
+    }
       
-  /* FIXME: timezone info is lost */
-  mu_strftime (datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tmp);
-  mu_stream_printf (stream, "Arrival-Date: %s\n", datestr);
+  mu_c_streamftime (stream, "Arrival-Date: %a, %b %d %H:%M:%S %Y %Z%n",
+		    tmp, &tz);
 
   email = mu_get_user_email (NULL);
   mu_stream_printf (stream, "Final-Recipient: RFC822; %s\n",
@@ -220,10 +221,10 @@ mime_create_ds (mu_mime_t mime, mu_message_t orig)
   mu_stream_printf (stream,  
 		    "Disposition: automatic-action/MDN-sent-automatically;deleted\n");
 
-  t = time (NULL);
   tmp = localtime (&t);
-  mu_strftime (datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tmp);
-  mu_stream_printf (stream, "Last-Attempt-Date: %s\n", datestr);
+  mu_datetime_tz_local (&tz);
+  mu_c_streamftime (stream, "Last-Attempt-Date: %a, %b %d %H:%M:%S %Y %Z%n",
+		    tmp, &tz);
 
   mu_stream_close (stream);
   mu_stream_destroy (&stream);
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 5a5f69f..c8a0dea 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -1530,7 +1530,7 @@ imap_envelope_date (mu_envelope_t envelope, char *buffer, size_t buflen,
   if (mu_parse_imap_date_time(datep, &tm, &tz) != 0)
     now = (time_t)-1;
   else
-    now = mu_tm2time (&tm, &tz);
+    now = mu_datetime_to_utc (&tm, &tz);
 
   /* if the time was unparseable, or mktime() didn't like what we
      parsed, use the calendar time. */
diff --git a/mh/mh_format.c b/mh/mh_format.c
index 9633cdb..78bc3ed 100644
--- a/mh/mh_format.c
+++ b/mh/mh_format.c
@@ -1172,7 +1172,7 @@ _parse_date (struct mh_machine *mach, struct tm *tm, struct mu_timezone *tz)
       /*mu_error ("can't parse date: [%s]", date);*/
       time (&t);
       *tm = *localtime (&t);
-      tz->utc_offset = mu_utc_offset ();
+      mu_datetime_tz_local (tz);
     }
   
   return 0;
@@ -1458,7 +1458,7 @@ builtin_clock (struct mh_machine *mach)
 
   if (_parse_date (mach, &tm, &tz))
     return;
-  mach->arg_num = mu_tm2time (&tm, &tz);
+  mach->arg_num = mu_datetime_to_utc (&tm, &tz);
 }
 
 /*     rclock     date     integer  seconds prior to current time*/
@@ -1471,7 +1471,7 @@ builtin_rclock (struct mh_machine *mach)
   
   if (_parse_date (mach, &tm, &tz))
     return;
-  mach->arg_num = now - mu_tm2time (&tm, &tz);
+  mach->arg_num = now - mu_datetime_to_utc (&tm, &tz);
 }
 
 struct
diff --git a/mh/sortm.c b/mh/sortm.c
index 4e6d052..5778793 100644
--- a/mh/sortm.c
+++ b/mh/sortm.c
@@ -341,7 +341,7 @@ _parse_822_date (char *date, time_t * timep)
 
   if (mu_parse822_date_time (&p, date + strlen (date), &tm, &tz) == 0)
     {
-      *timep = mu_tm2time (&tm, &tz);
+      *timep = mu_datetime_to_utc (&tm, &tz);
       return 0;
     }
   return 1;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 08 14:13:41 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RYjPZ-0007Di-IZ
	for mharc-commit-mailutils@gnu.org; Thu, 08 Dec 2011 14:13:41 -0500
Received: from eggs.gnu.org ([140.186.70.92]:49461)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYjPW-0007Db-Jo
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 14:13:39 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYjPU-0002qw-I3
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 14:13:38 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:54263)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYjPU-0002qq-GW
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 14:13:36 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RYjPT-0003eJ-50
	for commit-mailutils@gnu.org; Thu, 08 Dec 2011 19:13:36 +0000
Date: Thu, 08 Dec 2011 19:13:35 +0000
Message-Id: <E1RYjPT-0003eJ-50@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-512-gada8b8c
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 76cc330864a9680365123c86f8d5a394f952dcc5
X-Git-Newrev: ada8b8cfa6957d99831c7318e572628006b1ca66
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 08 Dec 2011 19:13:39 -0000

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

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

The branch, master has been updated
       via  ada8b8cfa6957d99831c7318e572628006b1ca66 (commit)
      from  76cc330864a9680365123c86f8d5a394f952dcc5 (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 ada8b8cfa6957d99831c7318e572628006b1ca66
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 8 21:11:55 2011 +0200

    Use UTC in From_ separators in accordance with RFC 4155.
    
    * examples/mta.c (make_tmp): Use gmtime + mu_c_streamftime to
    create From_ line.
    * libmailutils/mailbox/msgenv.c (message_envelope_date): Use gmtime.
    * maidag/deliver.c (make_tmp): Likewise.
    * maidag/lmtp.c (cfun_data): Likewise.
    * readmsg/readmsg.c (print_unix_header): Use gmtime.

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

Summary of changes:
 examples/mta.c                |    5 ++++-
 libmailutils/mailbox/msgenv.c |    2 +-
 libproto/imap/mbox.c          |    5 +----
 maidag/deliver.c              |    5 ++++-
 maidag/lmtp.c                 |    6 +++++-
 readmsg/readmsg.c             |    2 +-
 6 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/examples/mta.c b/examples/mta.c
index 4b15317..c9572b9 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -222,6 +222,7 @@ make_tmp (mu_stream_t in)
   if (n >= 5 && memcmp (buf, "From ", 5))
     {
       time_t t;
+      struct tm *tm;
       const char *from = from_address ();
       if (!from)
 	{
@@ -230,7 +231,9 @@ make_tmp (mu_stream_t in)
 	}
 	  
       time (&t);
-      mu_stream_printf (out, "From %s %s", from, ctime (&t));
+      tm = gmtime (&t);
+      mu_stream_printf (out, "From %s ", from);
+      mu_c_streamftime (out, "%c%n", tm, NULL);
     }
 
   mu_stream_write (out, buf, n, NULL);
diff --git a/libmailutils/mailbox/msgenv.c b/libmailutils/mailbox/msgenv.c
index 57f3a07..9577c0d 100644
--- a/libmailutils/mailbox/msgenv.c
+++ b/libmailutils/mailbox/msgenv.c
@@ -57,7 +57,7 @@ message_envelope_date (mu_envelope_t envelope, char *buf, size_t len,
       char tmpbuf[MU_DATETIME_FROM_LENGTH+1];
       t = time (NULL);
       n = mu_strftime (tmpbuf, sizeof tmpbuf, 
-                       MU_DATETIME_FROM, localtime (&t));
+                       MU_DATETIME_FROM, gmtime (&t));
       n = mu_cpystr (buf, tmpbuf, len);
     }
   if (pnwrite)
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index c8a0dea..87cc10c 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -1536,11 +1536,8 @@ imap_envelope_date (mu_envelope_t envelope, char *buffer, size_t buflen,
      parsed, use the calendar time. */
   if (now == (time_t)-1)
     {
-      struct tm *gmt;
-
       time (&now);
-      gmt = gmtime (&now);
-      tm = *gmt;
+      tm = *gmtime (&now);
     }
 
   {
diff --git a/maidag/deliver.c b/maidag/deliver.c
index 4cff626..d6279e7 100644
--- a/maidag/deliver.c
+++ b/maidag/deliver.c
@@ -69,9 +69,12 @@ make_tmp (const char *from)
       if (from)
 	{
 	  time_t t;
+	  struct tm *tm;
 	  
 	  time (&t);
-	  mu_stream_printf (out, "From %s %s", from, ctime (&t));
+	  tm = gmtime (&t);
+	  mu_stream_printf (out, "From %s ", from);
+	  mu_c_streamftime (out, "%c%n", tm, NULL);
 	}
       else
 	{
diff --git a/maidag/lmtp.c b/maidag/lmtp.c
index 0273e20..b06927e 100644
--- a/maidag/lmtp.c
+++ b/maidag/lmtp.c
@@ -346,6 +346,7 @@ cfun_data (mu_stream_t iostr, char *arg)
   int rc;
   mu_stream_t flt, tempstr;
   time_t t;
+  struct tm *tm;
   int xlev = MU_XSCRIPT_PAYLOAD, xlev_switch = 0, buf_switch = 0;
   struct mu_buffer_query oldbuf;
   
@@ -375,7 +376,10 @@ cfun_data (mu_stream_t iostr, char *arg)
 
   /* Write out envelope */
   time (&t);
-  rc = mu_stream_printf (tempstr, "From %s %s", mail_from, ctime (&t));
+  tm = gmtime (&t);
+  rc = mu_stream_printf (tempstr, "From %s ", mail_from);
+  if (rc == 0)
+    rc = mu_c_streamftime (tempstr, "%c%n", tm, NULL);
   if (rc)
     {
       maidag_error (_("copy error: %s"), mu_strerror (rc));
diff --git a/readmsg/readmsg.c b/readmsg/readmsg.c
index 367c746..d06977f 100644
--- a/readmsg/readmsg.c
+++ b/readmsg/readmsg.c
@@ -184,7 +184,7 @@ print_unix_header (mu_message_t message)
       struct tm *tm;
 
       t = time (NULL);
-      tm = localtime (&t);
+      tm = gmtime (&t);
       mu_strftime (datebuf, sizeof datebuf, MU_DATETIME_FROM, tm);
       buf = datebuf;
     }


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 09 11:50:59 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RZ3f1-0003cr-Kw
	for mharc-commit-mailutils@gnu.org; Fri, 09 Dec 2011 11:50:59 -0500
Received: from eggs.gnu.org ([140.186.70.92]:46362)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZ3ey-0003ce-N1
	for commit-mailutils@gnu.org; Fri, 09 Dec 2011 11:50:58 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZ3eu-00016w-3F
	for commit-mailutils@gnu.org; Fri, 09 Dec 2011 11:50:56 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:48175)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZ3eu-00016r-0L
	for commit-mailutils@gnu.org; Fri, 09 Dec 2011 11:50:52 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZ3et-0007gi-7t
	for commit-mailutils@gnu.org; Fri, 09 Dec 2011 16:50:51 +0000
Date: Fri, 09 Dec 2011 16:50:51 +0000
Message-Id: <E1RZ3et-0007gi-7t@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-514-gf9a034c
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: ada8b8cfa6957d99831c7318e572628006b1ca66
X-Git-Newrev: f9a034c7af98b56f62114b4e390a9c7f5971db68
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 09 Dec 2011 16:50:58 -0000

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

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

The branch, master has been updated
       via  f9a034c7af98b56f62114b4e390a9c7f5971db68 (commit)
       via  e20cd8ae1e349af0a13008c1dc31704e64658455 (commit)
      from  ada8b8cfa6957d99831c7318e572628006b1ca66 (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 f9a034c7af98b56f62114b4e390a9c7f5971db68
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 9 17:29:26 2011 +0200

    Make sure mu_mailbox_get_message returns MU_ERR_NOENT if the requested message number is not found in the mailbox.
    
    * libmailutils/base/amd.c (amd_get_message): Return MU_ERR_NOENT if
    msgno is not found in the mailbox, but not if it is 0, in which case
    return EINVAL.
    * libproto/mbox/mbox.c (mbox_get_message): Likewise.
    * libproto/pop/mbox.c (pop_create_pop3_message): Likewise.
    
    * libproto/mbox/mboxscan.c (mbox_scan_internal): Fix calculation of UIDs.

commit e20cd8ae1e349af0a13008c1dc31704e64658455
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 9 17:27:23 2011 +0200

    Bugfix in mu_list_sort.
    
    * libmailutils/list/sort.c (_list_qsort): Fix end-of-list condition.

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

Summary of changes:
 libmailutils/base/amd.c        |    4 ++--
 libmailutils/list/sort.c       |    2 +-
 libmailutils/mailbox/mailbox.c |    2 +-
 libproto/mbox/mbox.c           |    4 ++--
 libproto/mbox/mboxscan.c       |   10 +++++-----
 libproto/pop/mbox.c            |    2 +-
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/libmailutils/base/amd.c b/libmailutils/base/amd.c
index 769d9c5..af2c3cf 100644
--- a/libmailutils/base/amd.c
+++ b/libmailutils/base/amd.c
@@ -723,7 +723,7 @@ amd_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
   /* Sanity checks.  */
   if (pmsg == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  if (amd == NULL)
+  if (amd == NULL || msgno < 1)
     return EINVAL;
 
   /* If we did not start a scanning yet do it now.  */
@@ -735,7 +735,7 @@ amd_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
     }
 
   if ((mhm = _amd_get_message (amd, msgno)) == NULL)
-    return EINVAL;
+    return MU_ERR_NOENT;
   return _amd_attach_message (mailbox, mhm, pmsg);
 }
 
diff --git a/libmailutils/list/sort.c b/libmailutils/list/sort.c
index a021abb..fef4f6f 100644
--- a/libmailutils/list/sort.c
+++ b/libmailutils/list/sort.c
@@ -65,7 +65,7 @@ _list_qsort (mu_list_t list, mu_list_comparator_t cmp)
   cur = list->head.next;
   do {
     cur = cur->next;
-    if (!cur)
+    if (cur == &list->head)
       return;
   } while ((rc = cmp (list->head.next->item, cur->item)) == 0);
 
diff --git a/libmailutils/mailbox/mailbox.c b/libmailutils/mailbox/mailbox.c
index e25ded3..a31682a 100644
--- a/libmailutils/mailbox/mailbox.c
+++ b/libmailutils/mailbox/mailbox.c
@@ -840,7 +840,7 @@ _search_message_uid (mu_mailbox_t mbox, size_t uid, size_t *result)
   return _uid_bsearch (mbox, 1, count, uid, result);
 }
 
-/* Translat message UIDs to message numbers and vice versa. */
+/* Translate message UIDs to message numbers and vice versa. */
 int
 mu_mailbox_translate (mu_mailbox_t mbox, int cmd, size_t from, size_t *to)
 {
diff --git a/libproto/mbox/mbox.c b/libproto/mbox/mbox.c
index 559a480..5fcf4cf 100644
--- a/libproto/mbox/mbox.c
+++ b/libproto/mbox/mbox.c
@@ -591,7 +591,7 @@ mbox_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
   /* Sanity checks.  */
   if (pmsg == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  if (mud == NULL)
+  if (mud == NULL || msgno < 1)
     return EINVAL;
 
   /* If we did not start a scanning yet do it now.  */
@@ -606,7 +606,7 @@ mbox_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
   if (!(mud->messages_count > 0
 	&& msgno > 0
 	&& msgno <= mud->messages_count))
-    return EINVAL;
+    return MU_ERR_NOENT;
 
   mum = mud->umessages[msgno - 1];
 
diff --git a/libproto/mbox/mboxscan.c b/libproto/mbox/mboxscan.c
index 7492c8e..4fcceea 100644
--- a/libproto/mbox/mboxscan.c
+++ b/libproto/mbox/mboxscan.c
@@ -320,7 +320,7 @@ mbox_scan_internal (mu_mailbox_t mailbox, mbox_message_t mum,
   int newline;
   size_t n = 0;
   mu_stream_t stream;
-  size_t min_uid = 1;
+  size_t min_uid = 0;
   int zn, isfrom = 0;
   char *temp;
   
@@ -367,9 +367,9 @@ mbox_scan_internal (mu_mailbox_t mailbox, mbox_message_t mum,
 		  mum->body_end = total - n - newline;
 		  mum->body_lines = --lines - newline;
 
-		  if (mum->uid < min_uid)
+		  if (mum->uid <= min_uid)
 		    {
-		      mum->uid = min_uid++;
+		      mum->uid = ++min_uid;
 		      /* Note that modification for when expunging.  */
 		      mum->attr_flags |= MU_ATTRIBUTE_MODIFIED;
 		    }
@@ -451,9 +451,9 @@ mbox_scan_internal (mu_mailbox_t mailbox, mbox_message_t mum,
       mum->body_end = total - newline;
       mum->body_lines = lines - newline;
 
-      if (mum->uid < min_uid)
+      if (mum->uid <= min_uid)
 	{
-	  mum->uid = min_uid++;
+	  mum->uid = ++min_uid;
 	  /* Note that modification for when expunging.  */
 	  mum->attr_flags |= MU_ATTRIBUTE_MODIFIED;
 	}
diff --git a/libproto/pop/mbox.c b/libproto/pop/mbox.c
index ba03ef2..1b64247 100644
--- a/libproto/pop/mbox.c
+++ b/libproto/pop/mbox.c
@@ -604,7 +604,7 @@ pop_create_pop3_message (struct _pop3_mailbox *mpd, size_t msgno,
   struct _pop3_message *mpm;
   
   if (msgno > mpd->msg_count)
-    return EINVAL;
+    return MU_ERR_NOENT;
 
   if (!mpd->msg)
     {


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sat Dec 10 05:05:15 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RZJnv-0001cF-R9
	for mharc-commit-mailutils@gnu.org; Sat, 10 Dec 2011 05:05:15 -0500
Received: from eggs.gnu.org ([140.186.70.92]:41207)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZJnp-0001bp-Vy
	for commit-mailutils@gnu.org; Sat, 10 Dec 2011 05:05:13 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZJnl-0005zL-T0
	for commit-mailutils@gnu.org; Sat, 10 Dec 2011 05:05:09 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:46682)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZJnl-0005z6-M6
	for commit-mailutils@gnu.org; Sat, 10 Dec 2011 05:05:05 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZJnl-00050U-84
	for commit-mailutils@gnu.org; Sat, 10 Dec 2011 10:05:05 +0000
Date: Sat, 10 Dec 2011 10:05:05 +0000
Message-Id: <E1RZJnl-00050U-84@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-516-gea01b2f
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: f9a034c7af98b56f62114b4e390a9c7f5971db68
X-Git-Newrev: ea01b2f9a46d6eb0666e613f1c536f9ee69dcf3e
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 10 Dec 2011 10:05:13 -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=ea01b2f9a46d6eb0666e613f1c536f9ee69dcf3e

The branch, master has been updated
       via  ea01b2f9a46d6eb0666e613f1c536f9ee69dcf3e (commit)
       via  cef5f35da5f0e9b326d4d9a56f6b62ad565ad643 (commit)
      from  f9a034c7af98b56f62114b4e390a9c7f5971db68 (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 ea01b2f9a46d6eb0666e613f1c536f9ee69dcf3e
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 10 12:02:55 2011 +0200

    imap4d: remove functions duplicated in the library
    
    * imap4d/imap4d.h (util_format_attribute_flags)
    (util_attribute_to_type): Remove. All uses changed to
    mu_imap_format_flags and mu_imap_flag_to_attribute, respectively.
    (util_type_to_attribute): Remove.

commit cef5f35da5f0e9b326d4d9a56f6b62ad565ad643
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 10 11:32:37 2011 +0200

    Redo message set parser in imap4d
    
    The new parser complies to RFC3501. Its output message sets are
    formatted as MU lists of non-overlapping contiguous message ranges,
    which reduces memory consumption and imposes less strain on CPU.
    The parser automatically translates message UIDs to sequence numbers
    and provides better error handling.
    
    * imap4d/imap4d.h (util_msgset): Remove.
    (util_parse_msgset): New proto.
    (imap4d_message_action_t): New typedef.
    (util_foreach_message): New proto.
    * imap4d/util.c  (util_msgset): Remove.
    (util_parse_msgset): New function.
    
    * imap4d/copy.c: Use util_parse_msgset to parse message set specs and
    util_foreach_message to iterate over the returned list.
    * imap4d/fetch.c: Likewise.
    * imap4d/search.c: Likewise.
    * imap4d/store.c: Likewise.
    * imap4d/tests/IDEF0955.at: Update the test.
    * include/mailutils/list.h (mu_list_action_t): Fix typedef.
    * libmailutils/list/foreach.c (mu_list_foreach)
    (mu_list_do): Update signatures.

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

Summary of changes:
 imap4d/append.c             |    8 +-
 imap4d/copy.c               |  193 ++++++++-------
 imap4d/fetch.c              |   76 +++----
 imap4d/imap4d.c             |    1 -
 imap4d/imap4d.h             |   14 +-
 imap4d/search.c             |   51 ++---
 imap4d/store.c              |  127 +++++-----
 imap4d/sync.c               |    2 +-
 imap4d/tests/IDEF0955.at    |    5 +
 imap4d/util.c               |  567 ++++++++++++++++++++++---------------------
 include/mailutils/list.h    |    6 +-
 libmailutils/list/foreach.c |    4 +-
 12 files changed, 520 insertions(+), 534 deletions(-)

diff --git a/imap4d/append.c b/imap4d/append.c
index 632333a..74aebef 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -220,13 +220,13 @@ imap4d_append (struct imap4d_command *command, imap4d_tokbuf_t tok)
     {
       while (++i < argc)
 	{
-	  int type;
 	  char *arg = imap4d_tokbuf_getarg (tok, i);
 	  
-	  if (!util_attribute_to_type (arg, &type))
-	    flags |= type;
-	  else if (arg[0] == ')')
+	  if (arg[0] == ')')
 	    break;
+	  if (mu_imap_flag_to_attribute (arg, &flags))
+	    return io_completion_response (command, RESP_BAD,
+					   "Unrecognized flag");
 	}
       if (i == argc)
 	return io_completion_response (command, RESP_BAD, 
diff --git a/imap4d/copy.c b/imap4d/copy.c
index 69715df..607c278 100644
--- a/imap4d/copy.c
+++ b/imap4d/copy.c
@@ -55,88 +55,111 @@ imap4d_copy (struct imap4d_command *command, imap4d_tokbuf_t tok)
   return io_completion_response (command, rc, "%s", text);
 }
 
+struct copy_env
+{
+  mu_mailbox_t dst;
+  mu_mailbox_t src;
+  mu_off_t total;
+  int ret;
+  char **err_text;
+};
+
 static int
-copy_check_size (mu_mailbox_t mbox, size_t n, size_t *set, mu_off_t *size)
+size_sum (size_t msgno, void *data)
 {
-  int status;
-  size_t i;
-  mu_off_t total = 0;
+  struct copy_env *env = data;
+  mu_message_t msg = NULL;
+  int rc;
   
-  for (i = 0; i < n; i++)
+  rc = mu_mailbox_get_message (env->src, msgno, &msg);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL, rc);
+      env->ret = RESP_NO;
+      return MU_ERR_FAILURE;
+    }
+  else 
     {
-      mu_message_t msg = NULL;
-      size_t msgno = set[i];
-      if (msgno)
+      size_t size;
+      rc = mu_message_size (msg, &size);
+      if (rc)
 	{
-	  status = mu_mailbox_get_message (mbox, msgno, &msg);
-	  if (status)
-	    {
-	      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL,
-			       status);
-	      return RESP_BAD;
-	    }
-	  else 
-	    {
-	      size_t size;
-	      status = mu_message_size (msg, &size);
-	      if (status)
-		{
-		  mu_diag_funcall (MU_DIAG_ERROR, "mu_message_size", NULL,
-				   status);
-		  return RESP_BAD;
-		}
-	      total += size;
-	    }
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_message_size", NULL, rc);
+	  env->ret = RESP_BAD;
+	  return MU_ERR_FAILURE;
 	}
+      env->total += size;
     }
-  *size = total;
-  return quota_check (total);
+  return 0;
 }
 
 static int
-try_copy (mu_mailbox_t dst, mu_mailbox_t src, size_t n, size_t *set)
+do_copy (size_t msgno, void *data)
 {
-  int result;
-  size_t i;
-  mu_off_t total;
-  
-  result = copy_check_size (src, n, set, &total);
-  if (result)
-    return result;
-  
-  for (i = 0; i < n; i++)
-    {
-      mu_message_t msg = NULL;
-      size_t msgno = set[i];
+  struct copy_env *env = data;
+  mu_message_t msg = NULL;
+  int status;
 
-      if (msgno)
-	{
-	  int status = mu_mailbox_get_message (src, msgno, &msg);
-	  if (status)
-	    {
-	      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL,
-			       status);
-	      return RESP_BAD;
-	    }
+  status = mu_mailbox_get_message (env->src, msgno, &msg);
+  if (status)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL,
+		       status);
+      env->ret = RESP_BAD;
+      return MU_ERR_FAILURE;
+    }
 
-	  imap4d_enter_critical ();
-	  status = mu_mailbox_append_message (dst, msg);
-	  imap4d_leave_critical ();
-	  if (status)
-	    {
-	      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_append_message",
-			       NULL,
-			       status);
-	      return RESP_BAD;
-	    }
-	}
+  imap4d_enter_critical ();
+  status = mu_mailbox_append_message (env->dst, msg);
+  imap4d_leave_critical ();
+  if (status)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_append_message", NULL,
+		       status);
+      env->ret = RESP_BAD;
+      return MU_ERR_FAILURE;
     }
-  quota_update (total);
-  return RESP_OK;
+
+  return 0;
 }
 
 static int
-safe_copy (mu_mailbox_t dst, mu_mailbox_t src, size_t n, size_t *set,
+try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
+	  char **err_text)
+{
+  int rc;
+  struct copy_env env;
+
+  env.dst = dst;
+  env.src = src;
+  env.total = 0;
+  env.ret = RESP_OK;
+  env.err_text = err_text;
+
+  *env.err_text = "Operation failed";
+
+  /* Check size */
+  rc = util_foreach_message (msglist, size_sum, &env);
+  if (rc)
+    return RESP_NO;
+  if (env.ret != RESP_OK)
+    return env.ret;
+  rc = quota_check (env.total);
+  if (rc)
+    {
+      *env.err_text = "Mailbox quota exceeded";
+      return RESP_NO;
+    }
+  env.total = 0;
+  rc = util_foreach_message (msglist, do_copy, &env);
+  quota_update (env.total);
+  if (rc)
+    return RESP_NO;
+  return env.ret;
+}
+  
+static int
+safe_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
 	   char **err_text)
 {
   size_t nmesg;
@@ -151,16 +174,11 @@ safe_copy (mu_mailbox_t dst, mu_mailbox_t src, size_t n, size_t *set,
       return RESP_NO;
     }
 
-  status = try_copy (dst, src, n, set);
-  if (status)
+  status = try_copy (dst, src, msglist, err_text);
+  if (status != RESP_OK)
     {
       size_t maxmesg;
 
-      if (status == RESP_NO)
-	*err_text = "Mailbox quota exceeded";
-      else
-	*err_text = "Operation failed";
-      
       /* If the COPY command is unsuccessful for any reason, server
 	 implementations MUST restore the destination mailbox to its state
 	 before the COPY attempt. */
@@ -211,11 +229,11 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 {
   int status;
   char *msgset;
+  mu_list_t msglist;
   char *name;
   char *mailbox_name;
   const char *delim = "/";
-  size_t *set = NULL;
-  int n = 0;
+  char *end;
   mu_mailbox_t cmbox = NULL;
   int arg = IMAP4_ARG_1 + !!isuid;
   int ns;
@@ -230,23 +248,14 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
   msgset = imap4d_tokbuf_getarg (tok, arg);
   name = imap4d_tokbuf_getarg (tok, arg + 1);
   /* Get the message numbers in set[].  */
-  status = util_msgset (msgset, &set, &n, isuid);
-  if (status != 0)
+  status = util_parse_msgset (msgset, isuid, mbox, &msglist, &end);
+  if (status)
     {
-      /* See RFC 3501, section 6.4.8, and a comment to the equivalent code
-	 in fetch.c */
-      *err_text = "Completed";
-      return RESP_OK;
+      *err_text = "Error parsing message set";
+      /* FIXME: print error location */
+      return RESP_BAD;
     }
 
-  if (isuid)
-    {
-      int i;
-      /* Fixup the message set. Perhaps util_msgset should do it itself? */
-      for (i = 0; i < n; i++)
-	set[i] = uid_to_msgno (set[i]);
-    }
-  
   mailbox_name = namespace_getfullpath (name, delim, &ns);
 
   if (!mailbox_name)
@@ -256,7 +265,7 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
     }
 
   /* If the destination mailbox does not exist, a server should return
-     an error.  */
+     an error. */
   status = mu_mailbox_create_default (&cmbox, mailbox_name);
   if (status == 0)
     {
@@ -264,13 +273,13 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
       status = mu_mailbox_open (cmbox, MU_STREAM_RDWR | mailbox_mode[ns]);
       if (status == 0)
 	{
-	  status = safe_copy (cmbox, mbox, n, set, err_text);
+	  if (!mu_list_is_empty (msglist))
+	    status = safe_copy (cmbox, mbox, msglist, err_text);
 	  mu_mailbox_close (cmbox);
 	}
       mu_mailbox_destroy (&cmbox);
     }
-  free (set);
-  free (mailbox_name);
+  mu_list_destroy (&msglist);
 
   if (status == 0)
     {
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index 6e955c0..d1698b6 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -37,6 +37,8 @@ struct fetch_runtime_closure
   mu_message_t msg;    /* The message itself */
   mu_list_t msglist;   /* A list of referenced messages.  See KLUDGE below. */
   char *err_text;      /* On return: error description if failed. */
+
+  mu_list_t fnlist;
 };
 
 struct fetch_function_closure;
@@ -62,8 +64,7 @@ struct fetch_parse_closure
 {
   int isuid;
   mu_list_t fnlist;
-  size_t *set;
-  int count;
+  mu_list_t msgnumlist;
 };
 
 
@@ -1751,38 +1752,16 @@ fetch_thunk (imap4d_parsebuf_t pb)
 {
   int status;
   char *msgset;
+  char *end;
   struct fetch_parse_closure *pclos = imap4d_parsebuf_data (pb);
   
   msgset = imap4d_parsebuf_next (pb, 1);
 
-  /* Get the message numbers in set[].  */
-  status = util_msgset (msgset, &pclos->set, &pclos->count, pclos->isuid);
-  switch (status)
-    {
-    case 0:
-      /* Very good! */
-      break;
-      
-    case EINVAL:
-      /* RFC3501, section 6.4.8.
-	 
-	 A non-existent unique identifier is ignored without any error
-	 message generated.  Thus, it is possible for a UID FETCH command
-	 to return an OK without any data or a UID COPY or UID STORE to
-	 return an OK without performing any operations.
-
-	 Obviously the same holds true for non-existing message numbers
-	 as well, although I did not find any explicit mention thereof
-	 in the RFC.
-
-	 FIXME: This code also causes imap4d to silently ignore erroneous
-	 msgset specifications (e.g. FETCH foobar (FLAGS)), which should
-	 be fixed.  */
-      return RESP_OK;
-
-    default:
-      imap4d_parsebuf_exit (pb, "Failed to parse message set");
-    }
+  /* Parse sequence numbers. */
+  status = util_parse_msgset (msgset, pclos->isuid, mbox,
+			      &pclos->msgnumlist, &end);
+  if (status)
+    imap4d_parsebuf_exit (pb, "Failed to parse message set");
 
   /* Compile the expression */
 
@@ -1797,6 +1776,23 @@ fetch_thunk (imap4d_parsebuf_t pb)
   return RESP_OK;
 }
 
+int
+_fetch_from_message (size_t msgno, void *data)
+{
+  int rc = 0;
+  struct fetch_runtime_closure *frc = data;
+
+  frc->msgno = msgno;
+  if (mu_mailbox_get_message (mbox, msgno, &frc->msg) == 0)
+    {
+      io_sendf ("* %lu FETCH (", (unsigned long) msgno);
+      frc->eltno = 0;
+      rc = mu_list_foreach (frc->fnlist, _do_fetch, frc);
+      io_sendf (")\n");
+    }
+  return rc;
+}
+
 /* Where the real implementation is.  It is here since UID command also
    calls FETCH.  */
 int
@@ -1823,32 +1819,20 @@ imap4d_fetch0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 
   if (rc == RESP_OK)
     {
-      size_t i;
       struct fetch_runtime_closure frc;
 
       memset (&frc, 0, sizeof (frc));
+      frc.fnlist = pclos.fnlist;
       /* Prepare status code. It will be replaced if an error occurs in the
 	 loop below */
       frc.err_text = "Completed";
-  
-      for (i = 0; i < pclos.count && rc == RESP_OK; i++)
-	{
-	  frc.msgno = (isuid) ? uid_to_msgno (pclos.set[i]) : pclos.set[i];
-	  
-	  if (frc.msgno &&
-	      mu_mailbox_get_message (mbox, frc.msgno, &frc.msg) == 0)
-	    {
-	      io_sendf ("* %lu FETCH (", (unsigned long) frc.msgno);
-	      frc.eltno = 0;
-	      rc = mu_list_foreach (pclos.fnlist, _do_fetch, &frc);
-	      io_sendf (")\n");
-	    }
-	}
+
+      util_foreach_message (pclos.msgnumlist, _fetch_from_message, &frc);
       mu_list_destroy (&frc.msglist);
     }
   
   mu_list_destroy (&pclos.fnlist);
-  free (pclos.set);
+  mu_list_destroy (&pclos.msgnumlist);
   return rc;
 }
 
diff --git a/imap4d/imap4d.c b/imap4d/imap4d.c
index e27948f..3fbbf8f 100644
--- a/imap4d/imap4d.c
+++ b/imap4d/imap4d.c
@@ -554,7 +554,6 @@ imap4d_master_signal (int signo)
 }
 
 
-
 int
 main (int argc, char **argv)
 {
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index f74eba3..09e7f64 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -105,6 +105,7 @@
 #include <mailutils/cstr.h>
 #include <mailutils/io.h>
 #include <mailutils/prog.h>
+#include <mailutils/imapio.h>
 
 #include <mu_umaxtostr.h>
 #include <muaux.h>
@@ -368,10 +369,17 @@ extern int  util_getstate (void);
 extern int  util_do_command (imap4d_tokbuf_t);
 extern char *util_tilde_expansion (const char *, const char *);
 extern char *util_getfullpath (const char *, const char *);
-extern int  util_msgset (char *, size_t **, int *, int);
 extern struct imap4d_command *util_getcommand (char *, 
                                                struct imap4d_command []);
 
+extern int util_parse_msgset (char *s, int isuid, mu_mailbox_t mbx,
+			      mu_list_t *plist, char **end);
+
+typedef int (*imap4d_message_action_t) (size_t, void *);
+
+int util_foreach_message (mu_list_t list, imap4d_message_action_t action,
+			  void *data);
+
 enum datetime_parse_mode     /* how to parse date/time strings */
   {
     datetime_default,        /* default mode */
@@ -390,10 +398,8 @@ extern char *util_localname (void);
 
 extern int util_wcard_match (const char *string, const char *pattern,
 			     const char *delim);
-int util_format_attribute_flags (mu_stream_t str, int flags);
+
 void util_print_flags (mu_attribute_t attr);
-int util_attribute_to_type (const char *item, int *type);
-int util_type_to_attribute (int type, char **attr_str);
 int util_attribute_matches_flag (mu_attribute_t attr, const char *item);
 int util_uidvalidity (mu_mailbox_t smbox, unsigned long *uidvp);
 
diff --git a/imap4d/search.c b/imap4d/search.c
index bb9656b..54c212e 100644
--- a/imap4d/search.c
+++ b/imap4d/search.c
@@ -62,11 +62,7 @@ struct value
     char *string;
     mu_off_t number;
     time_t date;
-    struct
-    {
-      int n;
-      size_t *set;
-    } msgset;
+    mu_list_t msgset;
   } v;
 };
 
@@ -168,7 +164,7 @@ struct cond condlist[] =
   { "SUBJECT",    "s",  cond_subject },
   { "TEXT",       "s",  cond_text },
   { "TO",         "s",  cond_to },
-  { "UID",        "m",  cond_uid },
+  { "UID",        "u",  cond_uid },
   { NULL }
 };
 
@@ -584,8 +580,6 @@ parse_simple_key (struct parsebuf *pb)
   struct search_node *node;
   struct cond *condp;
   time_t time;
-  size_t *set = NULL;
-  int n = 0;
   
   for (condp = condlist; condp->name && mu_c_strcasecmp (condp->name, pb->token);
        condp++)
@@ -593,13 +587,14 @@ parse_simple_key (struct parsebuf *pb)
 
   if (!condp->name)
     {
-      if (util_msgset (pb->token, &set, &n, 0) == 0) 
+      mu_list_t msglist;
+      
+      if (util_parse_msgset (pb->token, pb->isuid, mbox, &msglist, NULL) == 0) 
 	{
 	  struct search_node *np = parse_alloc (pb, sizeof *np);
 	  np->type = node_value;
 	  np->v.value.type = value_msgset;
-	  np->v.value.v.msgset.n = n;
-	  np->v.value.v.msgset.set = parse_regmem (pb, set);
+	  np->v.value.v.msgset = msglist;
 	  
 	  node = parse_alloc (pb, sizeof *node);
 	  node->type = node_call;
@@ -630,9 +625,7 @@ parse_simple_key (struct parsebuf *pb)
     {
       char *t = condp->argtypes;
       char *s;
-      int n;
       mu_off_t number;
-      size_t *set;
       struct search_node *arg;
       
       for (; *t; t++, parse_gettoken (pb, 0))
@@ -680,20 +673,20 @@ parse_simple_key (struct parsebuf *pb)
 	      arg->v.value.v.date = time;
 	      break;
 	      
-	    case 'm': /* message set */
-	      if (util_msgset (pb->token, &set, &n, 1)) /*FIXME: isuid?*/
+	    case 'u': /* UID message set */
+	      if (util_parse_msgset (pb->token, 0, NULL,
+				     &arg->v.value.v.msgset, NULL)) 
 		{
 		  pb->err_mesg = "Bogus number set";
 		  return NULL;
 		}
 	      arg->v.value.type = value_msgset;
-	      arg->v.value.v.msgset.n = n;
-	      arg->v.value.v.msgset.set = parse_regmem (pb, set);
 	      break;
 	      
 	    default:
-	      mu_diag_output (MU_DIAG_CRIT, _("%s:%d: INTERNAL ERROR (please report)"),
-		     __FILE__, __LINE__);
+	      mu_diag_output (MU_DIAG_CRIT,
+			      _("%s:%d: INTERNAL ERROR (please report)"),
+			      __FILE__, __LINE__);
 	      abort (); /* should never happen */
 	    }
 	  node->v.key.arg[node->v.key.narg++] = arg;
@@ -860,15 +853,9 @@ static void
 cond_msgset (struct parsebuf *pb, struct search_node *node, struct value *arg,
 	     struct value *retval)
 {
-  int  n = arg[0].v.msgset.n;
-  size_t *set = arg[0].v.msgset.set;
-  int i, rc;
-  
-  for (i = rc = 0; rc == 0 && i < n; i++)
-    rc = set[i] == pb->msgno;
-      
+  int rc = mu_list_locate (arg[0].v.msgset, &pb->msgno, NULL);
   retval->type = value_number;
-  retval->v.number = rc;
+  retval->v.number = rc == 0;
 }
 
 static void
@@ -1084,16 +1071,12 @@ static void
 cond_uid (struct parsebuf *pb, struct search_node *node, struct value *arg,
 	  struct value *retval)
 {
-  int  n = arg[0].v.msgset.n;
-  size_t *set = arg[0].v.msgset.set;
+  int rc;
   size_t uid = 0;
-  int i, rc;
   
   mu_message_get_uid (pb->msg, &uid);
-  for (i = rc = 0; rc == 0 && i < n; i++)
-    rc = set[i] == uid;
-      
+  rc = mu_list_locate (arg[0].v.msgset, &pb->msgno, NULL);
   retval->type = value_number;
-  retval->v.number = rc;
+  retval->v.number = rc == 0;
 }                      
 
diff --git a/imap4d/store.c b/imap4d/store.c
index 7e9d9ad..17f853c 100644
--- a/imap4d/store.c
+++ b/imap4d/store.c
@@ -25,8 +25,7 @@ struct store_parse_closure
   int ack;
   int type;
   int isuid;
-  size_t *set;
-  int count;
+  mu_list_t msgnumlist;
 };
   
 static int
@@ -36,6 +35,7 @@ store_thunk (imap4d_parsebuf_t p)
   char *msgset;
   char *data;
   int status;
+  char *end;
   
   msgset = imap4d_parsebuf_next (p, 1);
   data = imap4d_parsebuf_next (p, 1);
@@ -70,43 +70,75 @@ store_thunk (imap4d_parsebuf_t p)
     }
 
   /* Get the message numbers in set[].  */
-  status = util_msgset (msgset, &pclos->set, &pclos->count, pclos->isuid);
-  switch (status)
-    {
-    case 0:
-      break;
-
-    case EINVAL:
-      /* See RFC 3501, section 6.4.8, and a comment to the equivalent code
-	 in fetch.c */
-      return RESP_OK;
-
-    default:
-      imap4d_parsebuf_exit (p, "Failed to parse message set");
-    }      
+  status = util_parse_msgset (msgset, pclos->isuid, mbox,
+			      &pclos->msgnumlist, &end);
+  if (status)
+    imap4d_parsebuf_exit (p, "Failed to parse message set");
 
   if (p->token[0] != '(')
     imap4d_parsebuf_exit (p, "Syntax error");
   imap4d_parsebuf_next (p, 1);
   
   do
-    {
-      int t;
-      if (util_attribute_to_type (p->token, &t))
-	imap4d_parsebuf_exit (p, "Failed to parse flags");
-      else
-	pclos->type |= t;
-    }
+    if (mu_imap_flag_to_attribute (p->token, &pclos->type))
+      imap4d_parsebuf_exit (p, "Unrecognized flag");
   while (imap4d_parsebuf_next (p, 1) && p->token[0] != ')');
   return RESP_OK;
 }
 
+static int
+_do_store (size_t msgno, void *data)
+{
+  struct store_parse_closure *pclos = data;
+  mu_message_t msg = NULL;
+  mu_attribute_t attr = NULL;
+      
+  mu_mailbox_get_message (mbox, msgno, &msg);
+  mu_message_get_attribute (msg, &attr);
+	      
+  switch (pclos->how)
+    {
+    case STORE_ADD:
+      mu_attribute_set_flags (attr, pclos->type);
+      break;
+      
+    case STORE_UNSET:
+      mu_attribute_unset_flags (attr, pclos->type);
+      break;
+      
+    case STORE_SET:
+      mu_attribute_unset_flags (attr, 0xffffffff); /* FIXME */
+      mu_attribute_set_flags (attr, pclos->type);
+    }
+
+	  
+  if (pclos->ack)
+    {
+      io_sendf ("* %lu FETCH (", (unsigned long) msgno);
+      
+      if (pclos->isuid)
+	{
+	  size_t uid;
+	  int rc = mu_mailbox_translate (mbox, MU_MAILBOX_UID_TO_MSGNO,
+					 msgno, &uid);
+	  if (rc == 0)
+	    io_sendf ("UID %lu ", (unsigned long) uid);
+	}
+      io_sendf ("FLAGS (");
+      util_print_flags (attr);
+      io_sendf ("))\n");
+    }
+  /* Update the flags of uid table.  */
+  imap4d_sync_flags (msgno);
+  return 0;
+}
+
 int
 imap4d_store0 (imap4d_tokbuf_t tok, int isuid, char **ptext)
 {
   int rc;
   struct store_parse_closure pclos;
-
+  
   memset (&pclos, 0, sizeof pclos);
   pclos.ack = 1;
   pclos.isuid = isuid;
@@ -118,53 +150,12 @@ imap4d_store0 (imap4d_tokbuf_t tok, int isuid, char **ptext)
 			     ptext);
   if (rc == RESP_OK)
     {
-      size_t i;
-      
-      for (i = 0; i < pclos.count; i++)
-	{
-	  mu_message_t msg = NULL;
-	  mu_attribute_t attr = NULL;
-	  size_t msgno = isuid ? uid_to_msgno (pclos.set[i]) : pclos.set[i];
-      
-	  if (msgno)
-	    {
-	      mu_mailbox_get_message (mbox, msgno, &msg);
-	      mu_message_get_attribute (msg, &attr);
-	      
-	      switch (pclos.how)
-		{
-		case STORE_ADD:
-		  mu_attribute_set_flags (attr, pclos.type);
-		  break;
-		  
-		case STORE_UNSET:
-		  mu_attribute_unset_flags (attr, pclos.type);
-		  break;
-      
-		case STORE_SET:
-		  mu_attribute_unset_flags (attr, 0xffffffff); /* FIXME */
-		  mu_attribute_set_flags (attr, pclos.type);
-		}
-	    }
-	  
-	  if (pclos.ack)
-	    {
-	      io_sendf ("* %lu FETCH (", (unsigned long) msgno);
-	      
-	      if (isuid)
-		io_sendf ("UID %lu ", (unsigned long) msgno);
-	      io_sendf ("FLAGS (");
-	      util_print_flags (attr);
-	      io_sendf ("))\n");
-	    }
-	  /* Update the flags of uid table.  */
-	  imap4d_sync_flags (pclos.set[i]);
-	}
-
+      util_foreach_message (pclos.msgnumlist, _do_store, &pclos);
+    
       *ptext = "Completed";
     }
   
-  free (pclos.set);
+  mu_list_destroy (&pclos.msgnumlist);
   
   return rc;
 }
diff --git a/imap4d/sync.c b/imap4d/sync.c
index 1a5d0c5..e14b015 100644
--- a/imap4d/sync.c
+++ b/imap4d/sync.c
@@ -100,7 +100,7 @@ notify (void)
 	      if (nflags != attr_table[i-1])
 		{
 		  io_sendf ("* %lu FETCH FLAGS (",  (unsigned long) i);
-		  util_format_attribute_flags (iostream, nflags);
+		  mu_imap_format_flags (iostream, nflags);
 		  io_sendf (")\n");
 		  attr_table[i-1] = nflags;
 		}
diff --git a/imap4d/tests/IDEF0955.at b/imap4d/tests/IDEF0955.at
index 63fd765..19fe99c 100644
--- a/imap4d/tests/IDEF0955.at
+++ b/imap4d/tests/IDEF0955.at
@@ -39,6 +39,11 @@ imap4d IMAP4D_OPTIONS < input | remove_uidvalidity | tr -d '\r'
 * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
 * OK [[PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)]] Permanent flags
 1 OK [[READ-WRITE]] SELECT Completed
+* 1 FETCH (FLAGS (\Recent))
+* 2 FETCH (FLAGS (\Recent))
+* 3 FETCH (FLAGS (\Recent))
+* 4 FETCH (FLAGS (\Recent))
+* 5 FETCH (FLAGS (\Recent))
 2 OK FETCH Completed
 * 1 FETCH (UID 1 FLAGS (\Recent))
 * 2 FETCH (UID 2 FLAGS (\Recent))
diff --git a/imap4d/util.c b/imap4d/util.c
index 4165f42..a017cd8 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -17,10 +17,8 @@
 
 #include "imap4d.h"
 
-static int add2set (size_t **, int *, unsigned long);
-
 /* NOTE: Allocates Memory.  */
-/* Expand: ~ --> /home/user and to ~guest --> /home/guest.  */
+/* Expand: ~ --> /home/user, and ~guest --> /home/guest.  */
 char *
 util_tilde_expansion (const char *ref, const char *delim)
 {
@@ -32,29 +30,23 @@ util_tilde_expansion (const char *ref, const char *delim)
 char *
 util_getfullpath (const char *name, const char *delim)
 {
-  char *p = util_tilde_expansion (name, delim);
-  if (*p != delim[0])
+  char *exp = util_tilde_expansion (name, delim);
+  if (*exp != delim[0])
     {
-      char *s =
-	calloc (strlen (imap4d_homedir) + strlen (delim) + strlen (p) + 1, 1);
-      sprintf (s, "%s%s%s", imap4d_homedir, delim, p);
-      free (p);
-      p = s;
+      char *p, *s =
+	malloc (strlen (imap4d_homedir) + strlen (delim) + strlen (exp) + 1);
+      if (!s)
+	imap4d_bye (ERR_NO_MEM);
+      p = strcpy (s, imap4d_homedir);
+      p = mu_stpcpy (p, (char*) delim);
+      strcpy (p, exp);
+      free (exp);
+      exp = s;
     }
-  return mu_normalize_path (p);
-}
-
-static int
-comp_int (const void *a, const void *b)
-{
-  return *(int *) a - *(int *) b;
+  return mu_normalize_path (exp);
 }
-
-/* Parse the message set specification from S. Store message numbers
-   in SET, store number of element in the SET into the memory pointed to
-   by N.
-
-   A message set is defined as:
+
+/* A message set is defined as:
 
    set ::= sequence_num / (sequence_num ":" sequence_num) / (set "," set)
    sequence_num    ::= nz_number / "*"
@@ -65,204 +57,301 @@ comp_int (const void *a, const void *b)
    ;; the mailbox.
    nz_number       ::= digit_nz *digit
 
-   FIXME: The message sets like <,,,> or <:12> or <20:10> are not considered
-   an error */
-int
-util_msgset (char *s, size_t ** set, int *n, int isuid)
+   Non-existing sequence numbers are ignored, except when they form part
+   of a message range (sequence_num ":" sequence_num), in which case they
+   are treated as minimal or maximal sequence numbers (uids) available in
+   the mailbox depending on whether they appear to the left or to the right
+   of ":".
+*/
+
+/* Message set is a list of non-overlapping msgranges, in ascending
+   order.  No matter what command flavor is used, msg_beg and msg_end are
+   treated as sequence numbers (not UIDs). */
+struct msgrange
+{
+  size_t msg_beg;           /* beginning message number */
+  size_t msg_end;           /* ending message number */
+};
+
+/* Comparator function used to sort the message set list. */
+static int
+compare_msgrange (const void *a, const void *b)
+{
+  struct msgrange const *sa = a;
+  struct msgrange const *sb = b;
+  
+  if (sa->msg_beg < sb->msg_beg)
+    return -1;
+  if (sa->msg_beg > sb->msg_beg)
+    return 1;
+  if (sa->msg_end < sb->msg_end)
+    return -1;
+  if (sa->msg_end > sb->msg_end)
+    return 1;
+  return 0;
+}
+
+/* Comparator function used to locate a message in the list.  Second argument
+   (b) is a pointer to the message number. */
+static int
+compare_msgnum (const void *a, const void *b)
+{
+  struct msgrange const *range = a;
+  size_t msgno = *(size_t*)b;
+
+  if (range->msg_beg <= msgno && msgno <= range->msg_end)
+    return 0;
+  return 1;
+}
+
+/* This structure keeps parser state while parsing message set. */
+struct parse_msgnum_env
+{
+  char *s;               /* Current position in string */
+  size_t minval;         /* Min. sequence number or UID */
+  size_t maxval;         /* Max. sequence number or UID */
+  mu_list_t list;        /* List being built. */
+  int isuid:1;           /* True, if parsing an UID command. */
+  mu_mailbox_t mailbox;  /* Reference mailbox (can be NULL). */
+};
+
+/* Get a single message number/UID from env->s and store it into *PN.
+   Return 0 on success and error code on error.
+
+   Advance env->s to the point past the parsed message number.
+ */
+static int
+get_msgnum (struct parse_msgnum_env *env, size_t *pn)
 {
-  unsigned long val = 0;
-  unsigned long low = 0;
-  int done = 0;
-  int status = 0;
-  size_t max = 0;
-  size_t *tmp;
-  int i, j;
-  unsigned long invalid_uid = 0; /* For UID mode only: have we 
-				    encountered an uid > max uid? */
+  size_t msgnum;
+  char *p;
   
-  status = mu_mailbox_messages_count (mbox, &max);
-  if (status != 0)
-    return status;
-  /* If it is a uid sequence, override max with the UID.  */
-  if (isuid)
+  errno = 0;
+  msgnum = strtoul (env->s, &p, 10);
+  if (msgnum == ULONG_MAX && errno == ERANGE)
+    return MU_ERR_PARSE;
+  env->s = p;
+  if (msgnum > env->maxval)
+    msgnum = env->maxval;
+  *pn = msgnum;
+  return 0;
+}
+
+/* Parse a single message range (A:B). Treat '*' as the largest number/UID
+   in use. */
+static int
+parse_msgrange (struct parse_msgnum_env *env)
+{
+  int rc;
+  struct msgrange msgrange, *mp;
+  
+  if (*env->s == '*')
     {
-      mu_message_t msg = NULL;
-      mu_mailbox_get_message (mbox, max, &msg);
-      mu_message_get_uid (msg, &max);
+      msgrange.msg_beg = env->maxval;
+      env->s++;
     }
+  else if ((rc = get_msgnum (env, &msgrange.msg_beg)))
+    return rc;
 
-  *n = 0;
-  *set = NULL;
-  while (*s)
+  if (*env->s == ':')
     {
-      switch (*s)
+      if (*++env->s == '*')
 	{
-	  /* isdigit */
-	case '0':
-	case '1':
-	case '2':
-	case '3':
-	case '4':
-	case '5':
-	case '6':
-	case '7':
-	case '8':
-	case '9':
-	  {
-	    errno = 0;
-	    val = strtoul (s, &s, 10);
-	    if (val == ULONG_MAX && errno == ERANGE)
-	      {
-		if (*set)
-		  free (*set);
-		*set = NULL;
-		*n = 0;
-		return EINVAL;
-	      }
-	    else if (val > max)
-	      {
-		if (isuid)
-		  {
-		    invalid_uid = 1;
-		    continue;
-		  }
-		if (*set)
-		  free (*set);
-		*set = NULL;
-		*n = 0;
-		return EINVAL;
-	      }
-	    
-	    if (low)
-	      {
-		/* Reverse it. */
-		if (low > val)
-		  {
-		    long tmp = low;
-		    tmp -= 2;
-		    if (tmp < 0 || val == 0)
-		      {
-			free (*set);
-			*set = NULL;
-			*n = 0;
-			return EINVAL;
-		      }
-		    low = val;
-		    val = tmp;
-		  }
-		for (; low && low <= val; low++)
-		  {
-		    status = add2set (set, n, low);
-		    if (status != 0)
-		      return status;
-		  }
-		low = 0;
-	      }
-	    else
-	      {
-		status = add2set (set, n, val);
-		if (status != 0)
-		  return status;
-	      }
-	    break;
-	  }
-
-	  /* A pair of numbers separated by a ':' character indicates a
-	     contiguous set of mesages ranging from the first number to the
-	     second:
-	     3:5  --> 3 4 5
-	   */
-	case ':':
-	  low = val + 1;
-	  s++;
-	  break;
-
-	  /* As a convenience. '*' is provided to refer to the highest
-	     message number int the mailbox:
-	     5:*  --> 5 6 7 8
-	   */
-	case '*':
-	  {
-	    val = max;
-	    s++;
-	    status = add2set (set, n, val);
-	    if (status != 0)
-	      return status;
-	  }
-	  break;
-
-	  /* IMAP also allows a set of noncontiguous numbers to be specified
-	     with the ',' character:
-	     1,3,5,7  --> 1 3 5 7
-	   */
-	case ',':
-	  s++;
-	  break;
+	  msgrange.msg_end = env->maxval;
+	  ++env->s;
+	}
+      else if (*env->s == 0)
+	return MU_ERR_PARSE;
+      else if ((rc = get_msgnum (env, &msgrange.msg_end)))
+	return rc;
+    }
+  else
+    msgrange.msg_end = msgrange.msg_beg;
 
-	default:
-	  done = 1;
-	  if (*set)
-	    free (*set);
-	  *set = NULL;
-	  *n = 0;
-	  return EINVAL;
+  if (msgrange.msg_end < msgrange.msg_beg)
+    {
+      size_t tmp = msgrange.msg_end;
+      msgrange.msg_end = msgrange.msg_beg;
+      msgrange.msg_beg = tmp;
+    }
 
-	}			/* switch */
+  if (env->isuid && env->mailbox)
+    {
+      int rc;
+
+      rc = mu_mailbox_translate (env->mailbox,
+				 MU_MAILBOX_UID_TO_MSGNO,
+				 msgrange.msg_beg, &msgrange.msg_beg);
+      if (rc == MU_ERR_NOENT)
+	msgrange.msg_beg = env->minval;
+      else if (rc)
+	return rc;
+      
+      rc = mu_mailbox_translate (env->mailbox,
+				 MU_MAILBOX_UID_TO_MSGNO,
+				 msgrange.msg_end, &msgrange.msg_end);
+      if (rc == MU_ERR_NOENT)
+	msgrange.msg_end = env->maxval;
+      else if (rc)
+	return rc;
+    }      
+
+  mp = malloc (sizeof (*mp));
+  if (!mp)
+    return ENOMEM;
+
+  *mp = msgrange;
+
+  rc = mu_list_append (env->list, mp);
+  if (rc)
+    free (mp);
+  return rc;
+}
 
-      if (done)
-	break;
-    }				/* while */
+/* Parse message set specification S. ISUID indicates if the set is supposed
+   to contain UIDs (they will be translated to message sequence numbers).
+   MBX is a reference mailbox or NULL.
 
-  if (*n == 0)
-    return 0;
+   On success, return 0 and place the resulting set into *PLIST. On error,
+   return error code and point END to the position in the input string where
+   the parsing failed. */
+int
+util_parse_msgset (char *s, int isuid, mu_mailbox_t mbx,
+		   mu_list_t *plist, char **end)
+{
+  int rc;
+  struct parse_msgnum_env env;
+  size_t n;
   
-  /* For message sets in form X:Y where Y is a not-existing UID greater
-     than max UID, replace Y with the max UID in the mailbox */
-  if (*n == 1 && invalid_uid)
+  if (!s)
+    return EINVAL;
+  if (!*s)
+    return MU_ERR_PARSE;
+
+  memset (&env, 0, sizeof (env));
+  env.s = s;
+  if (mbox)
     {
-      val = max;
-      status = add2set (set, n, val);
-      if (status != 0)
-	return status;
+      size_t lastmsgno;      /* Max. sequence number. */
+
+      rc = mu_mailbox_messages_count (mbx, &lastmsgno);
+      if (rc == 0)
+	{
+	  if (isuid)
+	    {
+	      rc = mu_mailbox_translate (mbox, MU_MAILBOX_MSGNO_TO_UID,
+					 lastmsgno, &env.maxval);
+	      if (rc == 0)
+		rc = mu_mailbox_translate (mbox, MU_MAILBOX_MSGNO_TO_UID,
+					   1, &env.minval);
+	    }
+	  else
+	    env.maxval = lastmsgno;
+	}
+      if (rc)
+	return rc;
     }
+  rc = mu_list_create (&env.list);
+  if (rc)
+    return rc;
+  mu_list_set_destroy_item (env.list, mu_list_free_item);
+  
+  env.isuid = isuid;
+  env.mailbox = mbx;
   
-  if (low)
+  while ((rc = parse_msgrange (&env)) == 0 && *env.s)
     {
-      /* Reverse it. */
-      if (low > val)
+      if (*env.s != ',' || *++env.s == 0)
 	{
-	  long tmp = low;
-	  tmp -= 2;
-	  if (tmp < 0 || val == 0)
-	    {
-	      free (*set);
-	      *set = NULL;
-	      *n = 0;
-	      return EINVAL;
-	    }
-	  low = val;
-	  val = tmp;
+	  rc = MU_ERR_PARSE;
+	  break;
 	}
-      for (; low && low <= val; low++)
+    }
+
+  if (end)
+    *end = env.s;
+  if (rc)
+    {
+      mu_list_destroy (&env.list);
+      return rc;
+    }
+
+  mu_list_count (env.list, &n);
+  if (n > 1)
+    {
+      mu_iterator_t itr;
+      struct msgrange *last = NULL;
+
+      /* Sort the list and coalesce overlapping message ranges. */
+      mu_list_sort (env.list, compare_msgrange);
+
+      mu_list_get_iterator (env.list, &itr);
+      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+	   mu_iterator_next (itr))
 	{
-	  status = add2set (set, n, low);
-	  if (status != 0)
-	    return status;
+	  struct msgrange *msgrange;
+
+	  mu_iterator_current (itr, (void**)&msgrange);
+	  if (last)
+	    {
+	      if (last->msg_beg <= msgrange->msg_beg &&
+		  msgrange->msg_beg <= last->msg_end)
+		{
+		  if (msgrange->msg_end > last->msg_end)
+		    last->msg_end = msgrange->msg_end;
+		  mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+		  continue;
+		}
+		  
+	    }
+	  last = msgrange;
 	}
+      mu_iterator_destroy (&itr);
     }
 
-  /* Sort the resulting message set */
-  qsort (*set, *n, sizeof (**set), comp_int);
+  if (rc == 0)
+    {
+      mu_list_set_comparator (env.list, compare_msgnum);
+      *plist = env.list;
+    }
+  else
+    mu_list_destroy (&env.list);
+  return rc;
+}
+
+struct action_closure
+{
+  imap4d_message_action_t action;
+  void *data;
+};
+
+static int
+procrange (void *item, void *data)
+{
+  struct msgrange *mp = item;
+  struct action_closure *clos = data;
+  size_t i;
 
-  /* Remove duplicates. tmp serves to avoid extra dereferences */
-  tmp = *set;
-  for (i = 0, j = 1; i < *n; i++)
-    if (tmp[j - 1] != tmp[i])
-      tmp[j++] = tmp[i];
-  *n = j;
+  for (i = mp->msg_beg; i <= mp->msg_end; i++)
+    {
+      int rc = clos->action (i, clos->data);
+      if (rc)
+	return rc;
+    }
   return 0;
 }
 
+/* Apply ACTION to each message number from LIST. */
+int
+util_foreach_message (mu_list_t list, imap4d_message_action_t action,
+		      void *data)
+{
+  struct action_closure clos;
+  clos.action = action;
+  clos.data = data;
+  return mu_list_foreach (list, procrange, &clos);
+}
+
 int
 util_do_command (imap4d_tokbuf_t tok)
 {
@@ -317,26 +406,9 @@ util_getcommand (char *cmd, struct imap4d_command command_table[])
   return NULL;
 }
 
-static int
-add2set (size_t ** set, int *n, unsigned long val)
-{
-  size_t *tmp;
-  tmp = realloc (*set, (*n + 1) * sizeof (**set));
-  if (tmp == NULL)
-    {
-      if (*set)
-	free (*set);
-      *n = 0;
-      return ENOMEM;
-    }
-  *set = tmp;
-  (*set)[*n] = val;
-  (*n)++;
-  return 0;
-}
-
 static void
-adjust_tm (struct tm *tm, struct mu_timezone *tz, enum datetime_parse_mode flag)
+adjust_tm (struct tm *tm, struct mu_timezone *tz,
+	   enum datetime_parse_mode flag)
 {
   switch (flag)
     {
@@ -428,76 +500,13 @@ util_strcasestr (const char *haystack, const char *needle)
   return mu_c_strcasestr (haystack, needle);
 }
 
-struct
-{
-  char *name;
-  int flag;
-}
-_imap4d_attrlist[] =
-{
-  { "\\Answered", MU_ATTRIBUTE_ANSWERED },
-  { "\\Flagged", MU_ATTRIBUTE_FLAGGED },
-  { "\\Deleted", MU_ATTRIBUTE_DELETED },
-  { "\\Draft", MU_ATTRIBUTE_DRAFT },
-  { "\\Seen", MU_ATTRIBUTE_SEEN|MU_ATTRIBUTE_READ },
-};
-
-#define NATTR sizeof(_imap4d_attrlist)/sizeof(_imap4d_attrlist[0])
-
-int _imap4d_nattr = NATTR;
-
-int
-util_attribute_to_type (const char *item, int *type)
-{
-  int i;
-
-  if (mu_c_strcasecmp (item, "\\Recent") == 0)
-    {
-      *type = MU_ATTRIBUTE_RECENT;
-      return 0;
-    }
-  
-  for (i = 0; i < _imap4d_nattr; i++)
-    if (mu_c_strcasecmp (item, _imap4d_attrlist[i].name) == 0)
-      {
-	*type = _imap4d_attrlist[i].flag;
-	return 0;
-      }
-  return 1;
-}
-
-int
-util_format_attribute_flags (mu_stream_t str, int flags)
-{
-  int i;
-  int delim = 0;
-  
-  for (i = 0; i < _imap4d_nattr; i++)
-    if (flags & _imap4d_attrlist[i].flag)
-      {
-	if (delim)
-	  mu_stream_printf (str, " ");
-	mu_stream_printf (str, "%s", _imap4d_attrlist[i].name);
-	delim = 1;
-      }
-
-  if (MU_ATTRIBUTE_IS_UNSEEN (flags))
-    {
-      if (delim)
-	mu_stream_printf (str, " ");
-      mu_stream_printf (str, "\\Recent");
-    }
-  
-  return 0;
-}
-
 void
 util_print_flags (mu_attribute_t attr)
 {
   int flags = 0;
 
   mu_attribute_get_flags (attr, &flags);
-  util_format_attribute_flags (iostream, flags);
+  mu_imap_format_flags (iostream, flags);
 }
 
 int
@@ -506,7 +515,7 @@ util_attribute_matches_flag (mu_attribute_t attr, const char *item)
   int flags = 0, mask = 0;
 
   mu_attribute_get_flags (attr, &flags);
-  util_attribute_to_type (item, &mask);
+  mu_imap_flag_to_attribute (item, &mask);
   if (mask == MU_ATTRIBUTE_RECENT)
     return MU_ATTRIBUTE_IS_UNSEEN (flags);
 
diff --git a/include/mailutils/list.h b/include/mailutils/list.h
index 7934aa2..184c7ec 100644
--- a/include/mailutils/list.h
+++ b/include/mailutils/list.h
@@ -141,13 +141,13 @@ int mu_list_get_iterator (mu_list_t _list, mu_iterator_t *_pitr);
 
   /* A general-purpose iteration function.  When called, _item points to
      the item currently visited and _data points to call-specific data. */
-typedef int mu_list_action_t (void *_item, void *_data);
+typedef int (*mu_list_action_t) (void *_item, void *_data);
 
   /* Execute _action for each element in _list.  Use _data as the call-specific
      data. */
-int mu_list_foreach (mu_list_t _list, mu_list_action_t *_action, void *_data);
+int mu_list_foreach (mu_list_t _list, mu_list_action_t _action, void *_data);
   /* A historical alias to the above. */
-int mu_list_do (mu_list_t, mu_list_action_t *, void *) MU_DEPRECATED;
+int mu_list_do (mu_list_t, mu_list_action_t, void *) MU_DEPRECATED;
 
   /* ************************************************* */
   /* Functions for combining two lists.                */
diff --git a/libmailutils/list/foreach.c b/libmailutils/list/foreach.c
index 131f587..8c0d2a3 100644
--- a/libmailutils/list/foreach.c
+++ b/libmailutils/list/foreach.c
@@ -24,7 +24,7 @@
 #include <mailutils/errno.h>
 
 int
-mu_list_foreach (mu_list_t list, mu_list_action_t *action, void *cbdata)
+mu_list_foreach (mu_list_t list, mu_list_action_t action, void *cbdata)
 {
   mu_iterator_t itr;
   int status = 0;
@@ -49,7 +49,7 @@ mu_list_foreach (mu_list_t list, mu_list_action_t *action, void *cbdata)
 /* Retained for compatibility with previous versions.
    In the future it will be removed, or changed to a define or weak alias. */
 int
-mu_list_do (mu_list_t list, mu_list_action_t *action, void *cbdata)
+mu_list_do (mu_list_t list, mu_list_action_t action, void *cbdata)
 {
   return mu_list_foreach (list, action, cbdata);
 }


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Dec 11 06:26:50 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RZhYQ-0005LR-0q
	for mharc-commit-mailutils@gnu.org; Sun, 11 Dec 2011 06:26:50 -0500
Received: from eggs.gnu.org ([140.186.70.92]:43142)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZhYM-0005LI-Qn
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 06:26:48 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZhYL-0000If-FS
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 06:26:46 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:35913)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZhYL-0000IZ-E5
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 06:26:45 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZhYK-0008Uc-O0
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 11:26:44 +0000
Date: Sun, 11 Dec 2011 11:26:44 +0000
Message-Id: <E1RZhYK-0008Uc-O0@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-517-g61378ab
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: ea01b2f9a46d6eb0666e613f1c536f9ee69dcf3e
X-Git-Newrev: 61378abc8e046b457365a30c6b80559b0540177e
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sun, 11 Dec 2011 11:26:48 -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=61378abc8e046b457365a30c6b80559b0540177e

The branch, master has been updated
       via  61378abc8e046b457365a30c6b80559b0540177e (commit)
      from  ea01b2f9a46d6eb0666e613f1c536f9ee69dcf3e (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 61378abc8e046b457365a30c6b80559b0540177e
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Dec 11 13:26:07 2011 +0200

    Bugfix in libmu_sieve.
    
    * libmu_sieve/sieve.y (mu_sieve_machine_init_ex): Fix typo. Reference
    errstream, not mu_strerr.

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

Summary of changes:
 libmu_sieve/sieve.y |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/libmu_sieve/sieve.y b/libmu_sieve/sieve.y
index e5218fb..cdb59e2 100644
--- a/libmu_sieve/sieve.y
+++ b/libmu_sieve/sieve.y
@@ -364,7 +364,7 @@ mu_sieve_machine_init_ex (mu_sieve_machine_t *pmach,
 
   mach->data = data;
   mach->errstream = errstream;
-  mu_stream_ref (mu_strerr);
+  mu_stream_ref (errstream);
   
   *pmach = mach;
   return 0;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sun Dec 11 19:18:16 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RZtay-0007fs-A2
	for mharc-commit-mailutils@gnu.org; Sun, 11 Dec 2011 19:18:16 -0500
Received: from eggs.gnu.org ([140.186.70.92]:36173)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZtav-0007fm-M1
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 19:18:15 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZtat-0002LQ-Qg
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 19:18:13 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:39439)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZtat-0002LK-N1
	for commit-mailutils@gnu.org; Sun, 11 Dec 2011 19:18:11 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RZtar-0007Ym-Po
	for commit-mailutils@gnu.org; Mon, 12 Dec 2011 00:18:11 +0000
Date: Mon, 12 Dec 2011 00:18:09 +0000
Message-Id: <E1RZtar-0007Ym-Po@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-519-gc42e619
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 61378abc8e046b457365a30c6b80559b0540177e
X-Git-Newrev: c42e619475166b5a6385d52fa0ed5f301d9db0b8
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 12 Dec 2011 00:18:15 -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=c42e619475166b5a6385d52fa0ed5f301d9db0b8

The branch, master has been updated
       via  c42e619475166b5a6385d52fa0ed5f301d9db0b8 (commit)
       via  eae05c42b532d99b2b946eeb04e442d462d223b5 (commit)
      from  61378abc8e046b457365a30c6b80559b0540177e (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 c42e619475166b5a6385d52fa0ed5f301d9db0b8
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 12 02:16:21 2011 +0200

    Bugfix in imap4d.
    
    * imap4d/util.c (util_getfullpath): Use mu_stpcpy.

commit eae05c42b532d99b2b946eeb04e442d462d223b5
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 12 01:48:21 2011 +0200

    Fix UID generation in mboxes (complements f9a034c7)
    
    * libproto/mbox/mboxscan.c (mbox_scan_internal): Fix UID generation.
    * testsuite/lstuid.c: New test program.
    * testsuite/lstuid00.at: New test case.
    * testsuite/lstuid01.at: Likewise.
    * testsuite/lstuid02.at: Likewise.
    * testsuite/Makefile.am: Add new test cases.
    * testsuite/testsuite.at: Likewise.

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

Summary of changes:
 imap4d/util.c                                     |    2 +-
 libproto/mbox/mboxscan.c                          |   10 ++----
 testsuite/Makefile.am                             |    4 ++
 testsuite/{mbdel.c => lstuid.c}                   |   34 ++++++++++----------
 testsuite/{testsuite.at => lstuid00.at}           |   20 ++++++++----
 imap4d/tests/create01.at => testsuite/lstuid01.at |   32 ++++++++++---------
 imap4d/tests/create01.at => testsuite/lstuid02.at |   32 ++++++++++---------
 testsuite/testsuite.at                            |    3 ++
 8 files changed, 76 insertions(+), 61 deletions(-)
 copy testsuite/{mbdel.c => lstuid.c} (61%)
 copy testsuite/{testsuite.at => lstuid00.at} (76%)
 copy imap4d/tests/create01.at => testsuite/lstuid01.at (73%)
 copy imap4d/tests/create01.at => testsuite/lstuid02.at (73%)

diff --git a/imap4d/util.c b/imap4d/util.c
index a017cd8..e520735 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -37,7 +37,7 @@ util_getfullpath (const char *name, const char *delim)
 	malloc (strlen (imap4d_homedir) + strlen (delim) + strlen (exp) + 1);
       if (!s)
 	imap4d_bye (ERR_NO_MEM);
-      p = strcpy (s, imap4d_homedir);
+      p = mu_stpcpy (s, imap4d_homedir);
       p = mu_stpcpy (p, (char*) delim);
       strcpy (p, exp);
       free (exp);
diff --git a/libproto/mbox/mboxscan.c b/libproto/mbox/mboxscan.c
index 4fcceea..d0c5ace 100644
--- a/libproto/mbox/mboxscan.c
+++ b/libproto/mbox/mboxscan.c
@@ -367,14 +367,12 @@ mbox_scan_internal (mu_mailbox_t mailbox, mbox_message_t mum,
 		  mum->body_end = total - n - newline;
 		  mum->body_lines = --lines - newline;
 
-		  if (mum->uid <= min_uid)
+		  if (mum->uid == 0)
 		    {
 		      mum->uid = ++min_uid;
 		      /* Note that modification for when expunging.  */
 		      mum->attr_flags |= MU_ATTRIBUTE_MODIFIED;
 		    }
-		  else
-		    min_uid = mum->uid;
 
 		  if (flags & MBOX_SCAN_ONEMSG)
 		    break;
@@ -403,7 +401,7 @@ mbox_scan_internal (mu_mailbox_t mailbox, mbox_message_t mum,
 	    {
 	      char *p;
 	      unsigned long n = strtoul (buf + 6, &p, 10);
-	      if (*p == 0 || mu_isspace (*p))
+	      if ((*p == 0 || mu_isspace (*p)) && n > min_uid)
 		mum->uid = min_uid = n;
 	    }
 	  else if (mud->messages_count == 1 && IS_X_IMAPBASE (buf))
@@ -451,14 +449,12 @@ mbox_scan_internal (mu_mailbox_t mailbox, mbox_message_t mum,
       mum->body_end = total - newline;
       mum->body_lines = lines - newline;
 
-      if (mum->uid <= min_uid)
+      if (mum->uid == 0)
 	{
 	  mum->uid = ++min_uid;
 	  /* Note that modification for when expunging.  */
 	  mum->attr_flags |= MU_ATTRIBUTE_MODIFIED;
 	}
-      else
-	min_uid = mum->uid;
       
       if (flags & MBOX_SCAN_NOTIFY)
 	DISPATCH_ADD_MSG (mailbox, mud);
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 61144e3..1707985 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -50,6 +50,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 noinst_PROGRAMS = \
+ lstuid\
  mbdel\
  mimetest\
  smtpsend\
@@ -77,6 +78,9 @@ smtpsend_LDADD = \
 ## ------------ ##
 
 TESTSUITE_AT = \
+ lstuid00.at\
+ lstuid01.at\
+ lstuid02.at\
  mbdel.at\
  mime.at\
  ufms.at\
diff --git a/testsuite/mbdel.c b/testsuite/lstuid.c
similarity index 61%
copy from testsuite/mbdel.c
copy to testsuite/lstuid.c
index 8451df5..95274cd 100644
--- a/testsuite/mbdel.c
+++ b/testsuite/lstuid.c
@@ -1,5 +1,5 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+/* lstuid.c: List UIDs in mailbox
+   Copyright (C) 2011 Free Software Foundation, Inc.
 
    GNU Mailutils is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -25,30 +25,30 @@
 int
 main (int argc, char **argv)
 {
-  int rc;
   mu_mailbox_t mbox;
+  size_t i, count;
+  mu_message_t msg;
   
   if (argc != 2)
     {
-      fprintf (stderr, "usage: %s URL\n", argv[0]);
+      fprintf (stderr, "usage: %s MBOX\n", argv[0]);
       return 1;
     }
 
-  mu_register_all_mbox_formats ();
-  
+  mu_registrar_record (mu_mbox_record);
+
+  /* Open the mailbox */
   MU_ASSERT (mu_mailbox_create (&mbox, argv[1]));
-  rc = mu_mailbox_remove (mbox);
-  if (rc)
+  MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_RDWR));
+  mu_mailbox_messages_count (mbox, &count);
+  for (i = 1; i <= count; i++)
     {
-      if (rc == ENOTEMPTY)
-	{
-	  printf ("mailbox removed, but has subfolders\n");
-	  rc = 0;
-	}
-      else
-	fprintf (stderr, "%s\n", mu_strerror (rc));
+      size_t uid;
+      MU_ASSERT (mu_mailbox_get_message (mbox, i, &msg));
+      MU_ASSERT (mu_message_get_uid (msg, &uid));
+      printf ("%lu: %lu\n", (unsigned long) i, (unsigned long) uid);
     }
+  mu_mailbox_close (mbox);
   mu_mailbox_destroy (&mbox);
-  
-  return rc != 0;
+  return 0;
 }
diff --git a/testsuite/testsuite.at b/testsuite/lstuid00.at
similarity index 76%
copy from testsuite/testsuite.at
copy to testsuite/lstuid00.at
index 92e9c54..ee37189 100644
--- a/testsuite/testsuite.at
+++ b/testsuite/lstuid00.at
@@ -1,5 +1,5 @@
 # This file is part of GNU Mailutils. -*- Autotest -*-
-# Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+# Copyright (C) 2011 Free Software Foundation, Inc.
 #
 # GNU Mailutils is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -14,10 +14,18 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-m4_include([testsuite.inc])
+AT_SETUP([Assigning UIDs])
 
-AT_INIT
+AT_CHECK([
+MUT_MBCOPY($abs_top_srcdir/testsuite/spool/mbox1)
+lstuid mbox:mbox1
+],
+[0],
+[1: 1
+2: 2
+3: 3
+4: 4
+5: 5
+])
 
-m4_include([mime.at])
-m4_include([mbdel.at])
-m4_include([ufms.at])
+AT_CLEANUP
\ No newline at end of file
diff --git a/imap4d/tests/create01.at b/testsuite/lstuid01.at
similarity index 73%
copy from imap4d/tests/create01.at
copy to testsuite/lstuid01.at
index 986eb86..06c13a7 100644
--- a/imap4d/tests/create01.at
+++ b/testsuite/lstuid01.at
@@ -14,25 +14,27 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([create])
-AT_KEYWORDS([create create00])
+AT_SETUP([Assigning UIDs (with gaps)])
+
+AT_DATA([x-uid.sed],
+[/^Subject: Jabberwocky/a\
+X-UID: 2
+
+/^Subject: Simple MIME/a\
+X-UID: 10
 
-AT_CHECK([
-AT_DATA([input],[dnl
-1 create flat
-X LOGOUT
 ])
 
-imap4d IMAP4D_OPTIONS < input | tr -d '\r'
-find . -name flat
+AT_CHECK([
+sed -f x-uid.sed $abs_top_srcdir/testsuite/spool/mbox1 > mbox1
+lstuid mbox:mbox1
 ],
 [0],
-[* PREAUTH IMAP4rev1 Test mode
-1 OK CREATE Completed
-* BYE Session terminating.
-X OK LOGOUT Completed
-./flat
+[1: 2
+2: 3
+3: 10
+4: 11
+5: 12
 ])
 
-AT_CLEANUP
-
+AT_CLEANUP
\ No newline at end of file
diff --git a/imap4d/tests/create01.at b/testsuite/lstuid02.at
similarity index 73%
copy from imap4d/tests/create01.at
copy to testsuite/lstuid02.at
index 986eb86..42d9729 100644
--- a/imap4d/tests/create01.at
+++ b/testsuite/lstuid02.at
@@ -14,25 +14,27 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([create])
-AT_KEYWORDS([create create00])
+AT_SETUP([Assigning UIDs (with fixups)])
+
+AT_DATA([x-uid.sed],
+[/^Subject: Jabberwocky/a\
+X-UID: 2
+
+/^Subject: Simple MIME/a\
+X-UID: 1
 
-AT_CHECK([
-AT_DATA([input],[dnl
-1 create flat
-X LOGOUT
 ])
 
-imap4d IMAP4D_OPTIONS < input | tr -d '\r'
-find . -name flat
+AT_CHECK([
+sed -f x-uid.sed $abs_top_srcdir/testsuite/spool/mbox1 > mbox1
+lstuid mbox:mbox1
 ],
 [0],
-[* PREAUTH IMAP4rev1 Test mode
-1 OK CREATE Completed
-* BYE Session terminating.
-X OK LOGOUT Completed
-./flat
+[1: 2
+2: 3
+3: 4
+4: 5
+5: 6
 ])
 
-AT_CLEANUP
-
+AT_CLEANUP
\ No newline at end of file
diff --git a/testsuite/testsuite.at b/testsuite/testsuite.at
index 92e9c54..48bbe1a 100644
--- a/testsuite/testsuite.at
+++ b/testsuite/testsuite.at
@@ -18,6 +18,9 @@ m4_include([testsuite.inc])
 
 AT_INIT
 
+m4_include([lstuid00.at])
+m4_include([lstuid01.at])
+m4_include([lstuid02.at])
 m4_include([mime.at])
 m4_include([mbdel.at])
 m4_include([ufms.at])


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 12 07:16:46 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Ra4oI-0002oy-FE
	for mharc-commit-mailutils@gnu.org; Mon, 12 Dec 2011 07:16:46 -0500
Received: from eggs.gnu.org ([140.186.70.92]:57890)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Ra4o8-0002nx-0l
	for commit-mailutils@gnu.org; Mon, 12 Dec 2011 07:16:43 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Ra4o1-0006Bv-FS
	for commit-mailutils@gnu.org; Mon, 12 Dec 2011 07:16:35 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:45660)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Ra4o1-0006Bp-Br
	for commit-mailutils@gnu.org; Mon, 12 Dec 2011 07:16:29 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Ra4nx-0006lZ-40
	for commit-mailutils@gnu.org; Mon, 12 Dec 2011 12:16:28 +0000
Date: Mon, 12 Dec 2011 12:16:25 +0000
Message-Id: <E1Ra4nx-0006lZ-40@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-522-ga4b7feb
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: c42e619475166b5a6385d52fa0ed5f301d9db0b8
X-Git-Newrev: a4b7febbcb907b96181361824fac23e24485c023
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 12 Dec 2011 12:16:43 -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=a4b7febbcb907b96181361824fac23e24485c023

The branch, master has been updated
       via  a4b7febbcb907b96181361824fac23e24485c023 (commit)
       via  89669008fd50fee7403d896dcdee4506f72b2eea (commit)
       via  1e4c44886b7dd9d9995f60ae038e04b08ecbd786 (commit)
      from  c42e619475166b5a6385d52fa0ed5f301d9db0b8 (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 a4b7febbcb907b96181361824fac23e24485c023
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 12 14:07:26 2011 +0200

    Fix possible output buffer overflow in base64 encoder.
    
    * libmailutils/filter/base64.c (_base64_encoder): Continue after
    incrementing nbytes.
    * mh/mhn.c: Additional error checking.

commit 89669008fd50fee7403d896dcdee4506f72b2eea
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 12 13:42:20 2011 +0200

    Fix declaration of mu_sieve_vlist_do in libmu_sieve.

commit 1e4c44886b7dd9d9995f60ae038e04b08ecbd786
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 12 13:09:19 2011 +0200

    Initialize standard I/O streams statically.
    
    This allows programmers to use them right away, much like their stdio
    counterparts.  The mu_stdstream_setup and friends can be used to
    re-initialize them, should the need be.
    
    * include/mailutils/sys/file_stream.h
    (_MU_FILE_STREAM_STATIC_FILENAME): New flag.
    (_mu_file_stream_setup): New proto.
    * include/mailutils/sys/logstream.h (_mu_log_stream_setup): New proto.
    * include/mailutils/sys/stream.h (_MU_STR_EVENT_BOOTSTRAP): New event
    code.
    (_mu_stream) <destroy>: New method.
    * libmailutils/stdstream/basestr.c: Define the three standard streams
    statically, use bootstrap event to initialize them.
    * libmailutils/stdstream/strerr.c (mu_stdstream_strerr_create): Accept
    tag==NULL.
    * libmailutils/stream/file_stream.c (fd_done): Do not free filename if
    _MU_FILE_STREAM_STATIC_FILENAME is set.
    (_mu_file_stream_setup): New function.
    * libmailutils/stream/logstream.c (NEXT): Check size before advancing
    buffer pointer.
    (_mu_log_stream_setup): New function.
    * libmailutils/stream/stream.c (_bootstrap_event): New macro.
    (mu_stream_destroy): If .destroy is defined, use it instead of free(2).
    (mu_stream_open,mu_stream_seek,mu_stream_set_buffer)
    (mu_stream_read,mu_stream_readdelim,mu_stream_getdelim)
    (mu_stream_write,mu_stream_size,mu_stream_wait)
    (mu_stream_truncate,mu_stream_shutdown): Call _bootstrap_event at the
    beginning.
    * libmailutils/tests/.gitignore: Add strin, strout.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add strin, strout.
    (TESTSUITE_AT): Add stdstream tests.
    * libmailutils/tests/testsuite.at: Include stdstream tests.
    * libmailutils/tests/strerr.at: New test case.
    * libmailutils/tests/strin.at: Likewise.
    * libmailutils/tests/strout.at: Likewise.
    * libmailutils/tests/strin.c: New test program.
    * libmailutils/tests/strout.c: Likewise.

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

Summary of changes:
 include/mailutils/sieve.h                          |    2 +-
 include/mailutils/sys/file_stream.h                |    2 +
 include/mailutils/sys/logstream.h                  |    2 +
 include/mailutils/sys/stream.h                     |    2 +
 libmailutils/filter/base64.c                       |    4 +-
 libmailutils/stdstream/basestr.c                   |  141 ++++++++++++++++++--
 libmailutils/stdstream/strerr.c                    |   10 +-
 libmailutils/stream/file_stream.c                  |   24 ++--
 libmailutils/stream/logstream.c                    |   32 +++--
 libmailutils/stream/stream.c                       |   44 ++++++-
 libmailutils/tests/.gitignore                      |    2 +
 libmailutils/tests/Makefile.am                     |    5 +
 .../lstuid00.at => libmailutils/tests/strerr.at    |   24 ++--
 .../create01.at => libmailutils/tests/strin.at     |   25 ++--
 libmailutils/tests/{fsaf.c => strin.c}             |   68 ++++------
 .../lstuid00.at => libmailutils/tests/strout.at    |   23 ++--
 libmailutils/tests/{fsaf.c => strout.c}            |   71 +++++-----
 libmailutils/tests/testsuite.at                    |    5 +
 libmu_sieve/util.c                                 |    4 +-
 mh/mhn.c                                           |   16 ++-
 20 files changed, 354 insertions(+), 152 deletions(-)
 copy testsuite/lstuid00.at => libmailutils/tests/strerr.at (78%)
 copy imap4d/tests/create01.at => libmailutils/tests/strin.at (73%)
 copy libmailutils/tests/{fsaf.c => strin.c} (53%)
 copy testsuite/lstuid00.at => libmailutils/tests/strout.at (85%)
 copy libmailutils/tests/{fsaf.c => strout.c} (52%)

diff --git a/include/mailutils/sieve.h b/include/mailutils/sieve.h
index 0acad18..d8d60c0 100644
--- a/include/mailutils/sieve.h
+++ b/include/mailutils/sieve.h
@@ -187,7 +187,7 @@ int mu_sieve_match_part_checker (const char *name, mu_list_t tags,
 				 mu_list_t args);
 /* Operations in value lists */
 mu_sieve_value_t *mu_sieve_value_get (mu_list_t vlist, size_t index);
-int mu_sieve_vlist_do (mu_sieve_value_t * val, mu_list_action_t * ac,
+int mu_sieve_vlist_do (mu_sieve_value_t * val, mu_list_action_t ac,
 		       void *data);
 int mu_sieve_vlist_compare (mu_sieve_value_t * a, mu_sieve_value_t * b,
 			    mu_sieve_comparator_t comp,
diff --git a/include/mailutils/sys/file_stream.h b/include/mailutils/sys/file_stream.h
index 7905606..2255591 100644
--- a/include/mailutils/sys/file_stream.h
+++ b/include/mailutils/sys/file_stream.h
@@ -24,6 +24,7 @@
 #define _MU_FILE_STREAM_TEMP        0x01
 #define _MU_FILE_STREAM_ECHO_OFF    0x02
 #define _MU_FILE_STREAM_FD_BORROWED 0x04
+#define _MU_FILE_STREAM_STATIC_FILENAME 0x08
 
 struct _mu_file_stream
 {
@@ -36,5 +37,6 @@ struct _mu_file_stream
 
 int _mu_file_stream_create (struct _mu_file_stream **pstream, size_t size,
 			    const char *filename, int fd, int flags);
+void _mu_file_stream_setup (struct _mu_file_stream *str);
 
 #endif
diff --git a/include/mailutils/sys/logstream.h b/include/mailutils/sys/logstream.h
index e437296..720bc2c 100644
--- a/include/mailutils/sys/logstream.h
+++ b/include/mailutils/sys/logstream.h
@@ -33,4 +33,6 @@ struct _mu_log_stream
   struct mu_locus locus;   /* Location */
 };
 
+void _mu_log_stream_setup (struct _mu_log_stream *sp, mu_stream_t transport);
+  
 #endif
diff --git a/include/mailutils/sys/stream.h b/include/mailutils/sys/stream.h
index 352173c..d3e8615 100644
--- a/include/mailutils/sys/stream.h
+++ b/include/mailutils/sys/stream.h
@@ -30,6 +30,7 @@
 #define _MU_STR_EVENT_FILLBUF   2
 #define _MU_STR_EVENT_FLUSHBUF  3
 #define _MU_STR_EVENT_CLOSE     4
+#define _MU_STR_EVENT_BOOTSTRAP 5
 
 #define _MU_STR_EVMASK(n) (1<<(n))
 
@@ -58,6 +59,7 @@ struct _mu_stream
   int (*open) (struct _mu_stream *);
   int (*close) (struct _mu_stream *);
   void (*done) (struct _mu_stream *);
+  void (*destroy) (struct _mu_stream *);
   int (*seek) (struct _mu_stream *, mu_off_t, mu_off_t *);
   int (*size) (struct _mu_stream *, mu_off_t *);
   int (*ctl) (struct _mu_stream *, int, int, void *);
diff --git a/libmailutils/filter/base64.c b/libmailutils/filter/base64.c
index d99cdc5..1ad2c67 100644
--- a/libmailutils/filter/base64.c
+++ b/libmailutils/filter/base64.c
@@ -267,12 +267,14 @@ _base64_encoder (void *xd,
 	{
 	case base64_init:
 	  break;
+
 	case base64_newline:
 	  *optr++ = '\n';
 	  nbytes++;
 	  lp->cur_len = 0;
 	  lp->state = base64_rollback;
-	  /* Fall through */
+	  continue;
+
 	case base64_rollback:
 	  if (lp->idx < 3)
 	    {
diff --git a/libmailutils/stdstream/basestr.c b/libmailutils/stdstream/basestr.c
index 74247f0..ec9ae29 100644
--- a/libmailutils/stdstream/basestr.c
+++ b/libmailutils/stdstream/basestr.c
@@ -28,10 +28,127 @@
 #include <mailutils/stream.h>
 #include <mailutils/stdstream.h>
 #include <mailutils/util.h>
+#include <mailutils/io.h>
+#include <mailutils/filter.h>
+#include <mailutils/sys/stream.h>
+#include <mailutils/sys/file_stream.h>
+#include <mailutils/sys/logstream.h>
 
-mu_stream_t mu_strin;
-mu_stream_t mu_strout;
-mu_stream_t mu_strerr;
+static void stdstream_flushall_setup (void);
+
+/* This event callback bootstraps standard I/O streams mu_strin and
+   mu_strout. It is invoked when the stream core emits the bootstrap
+   event for the stream. */
+static void
+std_bootstrap (struct _mu_stream *str, int code,
+	       unsigned long lval, void *pval)
+{
+  struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
+  _mu_file_stream_setup (fstr);
+  str->event_cb = NULL;
+  str->event_mask = 0;
+  str->event_cb_data = 0;
+  fstr->stream.flags |= _MU_STR_OPEN;
+  mu_stream_set_buffer ((mu_stream_t) fstr, mu_buffer_line, 0);
+  stdstream_flushall_setup ();
+}
+
+/* This event callback bootstraps standard error stream (mu_strerr).
+   It is invoked when the stream core emits the bootstrap event for
+   the stream. */
+static void
+std_log_bootstrap (struct _mu_stream *str, int code,
+		   unsigned long lval, void *pval)
+{
+  struct _mu_log_stream *logstr = (struct _mu_log_stream *) str;
+  int yes = 1;
+  mu_stream_t errstr, transport;
+  int rc;
+  
+  rc = mu_stdio_stream_create (&errstr, MU_STDERR_FD, 0);
+  if (rc)
+    {
+      fprintf (stderr, "%s: cannot open error stream: %s\n",
+	       mu_program_name ? mu_program_name : "<unknown>",
+	       mu_strerror (rc));
+      abort ();
+    }
+  /* Make sure 2 is not closed when errstr is destroyed. */
+  mu_stream_ioctl (errstr, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
+  if (!mu_program_name)
+    transport = errstr;
+  else
+    {
+      char *fltargs[3] = { "INLINE-COMMENT", };
+      mu_asprintf (&fltargs[1], "%s: ", mu_program_name);
+      fltargs[2] = NULL;
+      rc = mu_filter_create_args (&transport, errstr,
+				  "INLINE-COMMENT",
+				  2, (const char**)fltargs,
+				  MU_FILTER_ENCODE, MU_STREAM_WRITE);
+      mu_stream_unref (errstr);
+      free (fltargs[1]);
+      if (rc)
+	{
+	  fprintf (stderr,
+		   "%s: cannot open output filter stream: %s",
+		   mu_program_name ? mu_program_name : "<unknown>",
+		   mu_strerror (rc));
+	  abort ();
+	}
+      mu_stream_set_buffer (transport, mu_buffer_line, 0);
+    }
+
+  str->event_cb = NULL;
+  str->event_mask = 0;
+  str->event_cb_data = 0;
+
+  _mu_log_stream_setup (logstr, transport);
+  stdstream_flushall_setup ();
+}
+
+/* The noop destroy function is necessary to prevent stream core from
+   freeing the stream on mu_stream_unref. */
+static void
+bootstrap_destroy (struct _mu_stream *str)
+{
+  /* Nothing */
+}
+
+/* Standard I/O streams: */
+static struct _mu_file_stream stdstream[2] = {
+  { { ref_count: 1,
+      buftype: mu_buffer_none,
+      flags: MU_STREAM_READ,
+      destroy: bootstrap_destroy,
+      event_cb: std_bootstrap,
+      event_mask: _MU_STR_EVMASK (_MU_STR_EVENT_BOOTSTRAP)
+    }, fd: MU_STDIN_FD, filename: "<stdin>",
+    flags: _MU_FILE_STREAM_FD_BORROWED|_MU_FILE_STREAM_STATIC_FILENAME },
+  { { ref_count: 1,
+      buftype: mu_buffer_none,
+      flags: MU_STREAM_WRITE,
+      event_cb: std_bootstrap,
+      event_mask: _MU_STR_EVMASK (_MU_STR_EVENT_BOOTSTRAP)
+    }, fd: MU_STDOUT_FD, filename: "<stdout>",
+    flags: _MU_FILE_STREAM_FD_BORROWED|_MU_FILE_STREAM_STATIC_FILENAME }
+};
+
+/* Standard error stream: */
+static struct _mu_log_stream default_strerr = {
+  { ref_count: 1,
+    buftype: mu_buffer_none,
+    flags: MU_STREAM_WRITE,
+    destroy: bootstrap_destroy,
+    event_cb: std_log_bootstrap,
+    event_mask: _MU_STR_EVMASK (_MU_STR_EVENT_BOOTSTRAP)
+  }
+};
+
+/* Pointers to these: */
+mu_stream_t mu_strin  = (mu_stream_t) &stdstream[MU_STDIN_FD];
+mu_stream_t mu_strout = (mu_stream_t) &stdstream[MU_STDOUT_FD];
+mu_stream_t mu_strerr = (mu_stream_t) &default_strerr;
 
 static void
 stdstream_flushall (void *data MU_ARG_UNUSED)
@@ -41,7 +158,17 @@ stdstream_flushall (void *data MU_ARG_UNUSED)
   mu_stream_flush (mu_strerr);
 }
 
-static int stdstream_flushall_setup = 0;
+static void
+stdstream_flushall_setup (void)
+{
+  static int _setup = 0;
+
+  if (!_setup)
+    {
+      mu_onexit (stdstream_flushall, NULL);
+      _setup = 1;
+    }
+}
 
 void
 mu_stdstream_setup (int flags)
@@ -120,11 +247,7 @@ mu_stdstream_setup (int flags)
 	abort ();
     }
 
-  if (!stdstream_flushall_setup)
-    {
-      mu_onexit (stdstream_flushall, NULL);
-      stdstream_flushall_setup = 1;
-    }
+  stdstream_flushall_setup ();
 }
 
 int
diff --git a/libmailutils/stdstream/strerr.c b/libmailutils/stdstream/strerr.c
index 7cc621d..cf03418 100644
--- a/libmailutils/stdstream/strerr.c
+++ b/libmailutils/stdstream/strerr.c
@@ -50,7 +50,7 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
 	if (rc)
 	  {
 	    fprintf (stderr, _("%s: cannot open error stream: %s\n"),
-		     tag, mu_strerror (rc));
+		     tag ? tag : "<unknown>", mu_strerror (rc));
 	    return MU_ERR_FAILURE;
 	  }
         /* Make sure 2 is not closed when str is destroyed.
@@ -74,7 +74,7 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
 	      {
 		fprintf (stderr,
 			 _("%s: cannot open output filter stream: %s"),
-			 tag, mu_strerror (rc));
+			 tag ? tag : "<unknown>", mu_strerror (rc));
 		return MU_ERR_FAILURE;
 	      }
 	    mu_stream_set_buffer (transport, mu_buffer_line, 0);
@@ -88,14 +88,14 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
       if (rc)
 	{
 	  fprintf (stderr, _("%s: cannot create syslog stream: %s\n"),
-		   tag, mu_strerror (rc));
+		   tag ? tag : "<unknown>", mu_strerror (rc));
 	  return MU_ERR_FAILURE;
 	}
       break;
 
     default:
       fprintf (stderr, _("%s: cannot create error stream: %s\n"),
-	       tag, mu_strerror (EINVAL));
+	       tag ? tag : "<unknown>", mu_strerror (EINVAL));
       return EINVAL;
     }
 
@@ -104,7 +104,7 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
   if (rc)
     {
       fprintf (stderr, _("%s: cannot open logger stream: %s\n"),
-	       tag , mu_strerror (rc));
+	       tag ? tag : "<unknown>", mu_strerror (rc));
       return MU_ERR_FAILURE;
     }
   return 0;
diff --git a/libmailutils/stream/file_stream.c b/libmailutils/stream/file_stream.c
index 9a7dc6b..2971408 100644
--- a/libmailutils/stream/file_stream.c
+++ b/libmailutils/stream/file_stream.c
@@ -153,7 +153,7 @@ fd_done (struct _mu_stream *str)
   struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
   if (fstr->fd != -1)
     fd_close (str);
-  if (fstr->filename)
+  if (fstr->filename && !(fstr->flags & _MU_FILE_STREAM_STATIC_FILENAME))
     free (fstr->filename);
   if (fstr->echo_state)
     free (fstr->echo_state);
@@ -312,15 +312,9 @@ fd_truncate (mu_stream_t stream, mu_off_t size)
   return 0;
 }
 
-int
-_mu_file_stream_create (struct _mu_file_stream **pstream, size_t size,
-			const char *filename, int fd, int flags)
+void
+_mu_file_stream_setup (struct _mu_file_stream *str)
 {
-  struct _mu_file_stream *str =
-    (struct _mu_file_stream *) _mu_stream_create (size, flags);
-  if (!str)
-    return ENOMEM;
-
   str->stream.read = fd_read;
   str->stream.write = fd_write;
   str->stream.open = fd_open;
@@ -331,6 +325,18 @@ _mu_file_stream_create (struct _mu_file_stream **pstream, size_t size,
   str->stream.ctl = fd_ioctl;
   str->stream.wait = fd_wait;
   str->stream.truncate = fd_truncate;
+}  
+
+int
+_mu_file_stream_create (struct _mu_file_stream **pstream, size_t size,
+			const char *filename, int fd, int flags)
+{
+  struct _mu_file_stream *str =
+    (struct _mu_file_stream *) _mu_stream_create (size, flags);
+  if (!str)
+    return ENOMEM;
+
+  _mu_file_stream_setup (str);
 
   if (filename)
     str->filename = mu_strdup (filename);
diff --git a/libmailutils/stream/logstream.c b/libmailutils/stream/logstream.c
index 80a8e09..94b35f5 100644
--- a/libmailutils/stream/logstream.c
+++ b/libmailutils/stream/logstream.c
@@ -103,7 +103,7 @@ _log_write (struct _mu_stream *str, const char *buf, size_t size,
   int rc;
   int escape_error = 0;
   
-#define NEXT do { buf++; size--; if (size == 0) return EINVAL; } while (0)
+#define NEXT do { if (size == 0) return EINVAL; buf++; size--; } while (0)
 #define READNUM(n) do {				\
     unsigned __x = 0;				\
     if (*buf != '<')				\
@@ -470,17 +470,11 @@ _log_ctl (struct _mu_stream *str, int code, int opcode, void *arg)
   return 0;
 }
 
-int
-mu_log_stream_create (mu_stream_t *pstr, mu_stream_t transport)
+void
+_mu_log_stream_setup (struct _mu_log_stream *sp, mu_stream_t transport)
 {
-  struct _mu_log_stream *sp;
   mu_stream_t stream;
-  int rc;
   
-  sp = (struct _mu_log_stream *)
-    _mu_stream_create (sizeof (*sp), MU_STREAM_WRITE);
-  if (!sp)
-    return ENOMEM;
   sp->base.write = _log_write;
   sp->base.flush = _log_flush;
   sp->base.close = _log_close;
@@ -494,13 +488,21 @@ mu_log_stream_create (mu_stream_t *pstr, mu_stream_t transport)
   
   stream = (mu_stream_t) sp;
   mu_stream_set_buffer (stream, mu_buffer_line, 0);
-  rc = mu_stream_open (stream);
-  if (rc)
-    mu_stream_destroy (&stream);
-  else
-    *pstr = stream;
+}
 
-  return rc;
+int
+mu_log_stream_create (mu_stream_t *pstr, mu_stream_t transport)
+{
+  struct _mu_log_stream *sp;
+  
+  sp = (struct _mu_log_stream *)
+    _mu_stream_create (sizeof (*sp), MU_STREAM_WRITE);
+  if (!sp)
+    return ENOMEM;
+  _mu_log_stream_setup (sp, transport);
+  *pstr = (mu_stream_t) sp;
+  
+  return 0;
 }
 
 
diff --git a/libmailutils/stream/stream.c b/libmailutils/stream/stream.c
index 7078d74..2669a17 100644
--- a/libmailutils/stream/stream.c
+++ b/libmailutils/stream/stream.c
@@ -46,6 +46,19 @@ size_t mu_stream_default_buffer_size = MU_STREAM_DEFBUFSIZ;
     }								\
   while (0)
 
+#define _bootstrap_event(stream)					    \
+  do									    \
+    {									    \
+      if ((stream)->event_cb &&						    \
+          ((stream)->event_mask & _MU_STR_EVMASK(_MU_STR_EVENT_BOOTSTRAP))) \
+	{								    \
+	  (stream)->event_cb (stream, _MU_STR_EVENT_BOOTSTRAP, 0, NULL);    \
+	  (stream)->event_mask &= ~_MU_STR_EVMASK(_MU_STR_EVENT_BOOTSTRAP); \
+	}								    \
+    }									    \
+  while (0)
+  
+
 #define _stream_stat_incr(s, k, n) \
   (((s)->statmask & MU_STREAM_STAT_MASK(k)) ? ((s)->statbuf[k] += n) : 0)
 
@@ -292,7 +305,10 @@ mu_stream_destroy (mu_stream_t *pstream)
 	  mu_stream_close (str);
 	  if (str->done)
 	    str->done (str);
-	  free (str);
+	  if (str->destroy)
+	    str->destroy (str);
+	  else
+	    free (str);
 	  *pstream = NULL;
 	}
     }
@@ -336,6 +352,7 @@ mu_stream_open (mu_stream_t stream)
 
   if (stream->flags & _MU_STR_OPEN)
     return MU_ERR_OPEN;
+  _bootstrap_event (stream);
   if (stream->open)
     {
       if ((rc = stream->open (stream)))
@@ -399,6 +416,7 @@ mu_stream_seek (mu_stream_t stream, mu_off_t offset, int whence,
   int rc;
   mu_off_t size;
   
+  _bootstrap_event (stream);
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -551,6 +569,8 @@ int
 mu_stream_set_buffer (mu_stream_t stream, enum mu_buffer_type type,
 		      size_t size)
 {
+  _bootstrap_event (stream);
+  
   if (size == 0)
     size = mu_stream_default_buffer_size;
 
@@ -717,6 +737,8 @@ _stream_write_unbuffered (mu_stream_t stream,
 int
 mu_stream_read (mu_stream_t stream, void *buf, size_t size, size_t *pread)
 {
+  _bootstrap_event (stream);
+  
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -857,6 +879,8 @@ mu_stream_readdelim (mu_stream_t stream, char *buf, size_t size,
 {
   int rc;
   
+  _bootstrap_event (stream);
+  
   if (size == 0)
     return EINVAL;
 
@@ -904,7 +928,9 @@ mu_stream_getdelim (mu_stream_t stream, char **pbuf, size_t *psize,
   char *lineptr = *pbuf;
   size_t n = *psize;
   size_t cur_len = 0;
-    
+
+  _bootstrap_event (stream);
+  
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -987,7 +1013,7 @@ int
 mu_stream_getline (mu_stream_t stream, char **pbuf, size_t *psize,
 		   size_t *pread)
 {
-    return mu_stream_getdelim (stream, pbuf, psize, '\n', pread);
+  return mu_stream_getdelim (stream, pbuf, psize, '\n', pread);
 }
 
 int
@@ -995,6 +1021,8 @@ mu_stream_write (mu_stream_t stream, const void *buf, size_t size,
 		 size_t *pnwritten)
 {
   int rc = 0;
+
+  _bootstrap_event (stream);
   
   if (!(stream->flags & _MU_STR_OPEN))
     {
@@ -1063,6 +1091,7 @@ mu_stream_flush (mu_stream_t stream)
   
   if (!stream)
     return EINVAL;
+  _bootstrap_event (stream);
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -1104,7 +1133,8 @@ mu_stream_size (mu_stream_t stream, mu_off_t *psize)
 {
   int rc;
   mu_off_t size;
-  
+
+  _bootstrap_event (stream);
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -1126,6 +1156,7 @@ mu_stream_size (mu_stream_t stream, mu_off_t *psize)
 int
 mu_stream_ioctl (mu_stream_t stream, int family, int opcode, void *ptr)
 {
+  _bootstrap_event (stream);
   if (stream->ctl == NULL)
     return ENOSYS;
   return stream->ctl (stream, family, opcode, ptr);
@@ -1138,6 +1169,7 @@ mu_stream_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
 
   if (stream == NULL)
     return EINVAL;
+  _bootstrap_event (stream);
 #if 0
   /* NOTE: Sometimes mu_stream_wait is called after a failed mu_stream_open.
      In particular, this is needed for a TCP stream opened with a
@@ -1181,6 +1213,8 @@ mu_stream_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
 int
 mu_stream_truncate (mu_stream_t stream, mu_off_t size)
 {
+  _bootstrap_event (stream);
+  
   if (!(stream->flags & _MU_STR_OPEN))
     {
       if (stream->open)
@@ -1203,6 +1237,8 @@ int
 mu_stream_shutdown (mu_stream_t stream, int how)
 {
   int rc;
+
+  _bootstrap_event (stream);
   
   if (!(stream->flags & _MU_STR_OPEN))
     {
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index aa5a1f3..d67519f 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -18,6 +18,8 @@ mailcap
 prop
 scantime
 strftime
+strin
+strout
 url-comp
 url-parse
 wicket
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 01c01ac..1cb2968 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -53,6 +53,8 @@ noinst_PROGRAMS = \
  prop\
  scantime\
  strftime\
+ strin\
+ strout\
  tempfile\
  url-comp\
  url-parse\
@@ -85,6 +87,9 @@ TESTSUITE_AT = \
  prop.at\
  scantime.at\
  strftime.at\
+ strerr.at\
+ strin.at\
+ strout.at\
  testsuite.at\
  url.at\
  url-comp.at\
diff --git a/testsuite/lstuid00.at b/libmailutils/tests/strerr.at
similarity index 78%
copy from testsuite/lstuid00.at
copy to libmailutils/tests/strerr.at
index ee37189..1bdbe6f 100644
--- a/testsuite/lstuid00.at
+++ b/libmailutils/tests/strerr.at
@@ -14,18 +14,24 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([Assigning UIDs])
+AT_SETUP([strerr])
+AT_KEYWORDS([stdstream])
 
 AT_CHECK([
-MUT_MBCOPY($abs_top_srcdir/testsuite/spool/mbox1)
-lstuid mbox:mbox1
+strout -err now is the time for all good men
 ],
 [0],
-[1: 1
-2: 2
-3: 3
-4: 4
-5: 5
+[],
+[strout: now
+strout: is
+strout: the
+strout: time
+strout: for
+strout: all
+strout: good
+strout: men
 ])
 
-AT_CLEANUP
\ No newline at end of file
+AT_CLEANUP
+
+
diff --git a/imap4d/tests/create01.at b/libmailutils/tests/strin.at
similarity index 73%
copy from imap4d/tests/create01.at
copy to libmailutils/tests/strin.at
index 986eb86..ebc6e41 100644
--- a/imap4d/tests/create01.at
+++ b/libmailutils/tests/strin.at
@@ -14,25 +14,24 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([create])
-AT_KEYWORDS([create create00])
+AT_SETUP([strin])
+AT_KEYWORDS([stdstream])
 
-AT_CHECK([
-AT_DATA([input],[dnl
-1 create flat
-X LOGOUT
+AT_DATA([input],
+[Omnis enim res, quae dando non deficit,
+dum habetur et non datur, nondum habetur,
+quomodo habenda est.
 ])
 
-imap4d IMAP4D_OPTIONS < input | tr -d '\r'
-find . -name flat
+AT_CHECK([
+strin < input
 ],
 [0],
-[* PREAUTH IMAP4rev1 Test mode
-1 OK CREATE Completed
-* BYE Session terminating.
-X OK LOGOUT Completed
-./flat
+[Omnis enim res, quae dando non deficit,
+dum habetur et non datur, nondum habetur,
+quomodo habenda est.
 ])
 
 AT_CLEANUP
 
+
diff --git a/libmailutils/tests/fsaf.c b/libmailutils/tests/strin.c
similarity index 53%
copy from libmailutils/tests/fsaf.c
copy to libmailutils/tests/strin.c
index d548744..67fb2db 100644
--- a/libmailutils/tests/fsaf.c
+++ b/libmailutils/tests/strin.c
@@ -16,57 +16,49 @@
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
-#endif
-#include <stdio.h>
+#endif 
 #include <stdlib.h>
-#include <unistd.h>
-#include <mailutils/types.h>
-#include <mailutils/error.h>
+#include <stdio.h>
+#include <string.h>
+#include <mailutils/stream.h>
+#include <mailutils/stdstream.h>
+#include <mailutils/diag.h>
 #include <mailutils/errno.h>
-#include <mailutils/util.h>
 
 int
 main (int argc, char **argv)
 {
-  int i;
-  int mode = MU_FILE_SAFETY_OWNER_MISMATCH, rc, m;
-  mu_list_t idlist;
-  uid_t uid;
+  int i, rc;
+  int echo_state = 0;
+  size_t n;
+  char buf[80];
 
-  if (argc == 1)
-    {
-      mu_error ("usage: %s [+-]mode... file...", argv[0]);
-      exit (1);
-    }
-  
-  rc = mu_list_create (&idlist);
-  if (rc)
-    {
-      mu_error ("mu_list_create: %s", mu_strerror (rc));
-      exit (1);
-    }
-  uid = getuid ();
-  
   for (i = 1; i < argc; i++)
     {
-      if (argv[i][0] == '-' || argv[i][0] == '+')
+      char *arg = argv[i];
+      if (arg[0] == '-')
 	{
-	  rc = mu_file_safety_name_to_code (argv[i] + 1, &m);
-	  if (rc)
+	  if (strcmp (arg, "-noecho") == 0)
 	    {
-	      mu_error ("%s: %s", argv[i], mu_strerror (rc));
-	      exit (1);
+	      MU_ASSERT (mu_stream_ioctl (mu_strin, MU_IOCTL_ECHO,
+					  MU_IOCTL_OP_SET,
+					  &echo_state));
+	      echo_state = 1;
 	    }
-	  if (argv[i][0] == '-')
-	    mode &= ~m;
 	  else
-	    mode |= m;
-	}
-      else
-	{
-	  rc = mu_file_safety_check (argv[i], mode, uid, idlist);
-	  printf ("%s: %s\n", argv[i], mu_strerror (rc));
+	    {
+	      fprintf (stderr, "usage: %s [-noecho]\n", argv[0]);
+	      return 1;
+	    }
 	}
     }
-  exit (0);
+	
+  while ((rc = mu_stream_read (mu_strin, buf, sizeof (buf), &n) == 0) &&
+	 n > 0)
+    fwrite (buf, 1, n, stdout);
+
+  if (echo_state)
+    MU_ASSERT (mu_stream_ioctl (mu_strin, MU_IOCTL_ECHO, MU_IOCTL_OP_SET,
+				&echo_state));
+  return 0;
 }
diff --git a/testsuite/lstuid00.at b/libmailutils/tests/strout.at
similarity index 85%
copy from testsuite/lstuid00.at
copy to libmailutils/tests/strout.at
index ee37189..c58f719 100644
--- a/testsuite/lstuid00.at
+++ b/libmailutils/tests/strout.at
@@ -14,18 +14,23 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([Assigning UIDs])
+AT_SETUP([strout])
+AT_KEYWORDS([stdstream])
 
 AT_CHECK([
-MUT_MBCOPY($abs_top_srcdir/testsuite/spool/mbox1)
-lstuid mbox:mbox1
+strout now is the time for all good men
 ],
 [0],
-[1: 1
-2: 2
-3: 3
-4: 4
-5: 5
+[now
+is
+the
+time
+for
+all
+good
+men
 ])
 
-AT_CLEANUP
\ No newline at end of file
+AT_CLEANUP
+
+
diff --git a/libmailutils/tests/fsaf.c b/libmailutils/tests/strout.c
similarity index 52%
copy from libmailutils/tests/fsaf.c
copy to libmailutils/tests/strout.c
index d548744..d9913a5 100644
--- a/libmailutils/tests/fsaf.c
+++ b/libmailutils/tests/strout.c
@@ -16,57 +16,60 @@
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
-#endif
-#include <stdio.h>
+#endif 
 #include <stdlib.h>
-#include <unistd.h>
-#include <mailutils/types.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/util.h>
+#include <stdio.h>
+#include <string.h>
+#include <mailutils/stream.h>
+#include <mailutils/stdstream.h>
+#include <mailutils/diag.h>
 
 int
 main (int argc, char **argv)
 {
+  mu_stream_t str = mu_strout;
   int i;
-  int mode = MU_FILE_SAFETY_OWNER_MISMATCH, rc, m;
-  mu_list_t idlist;
-  uid_t uid;
-
-  if (argc == 1)
-    {
-      mu_error ("usage: %s [+-]mode... file...", argv[0]);
-      exit (1);
-    }
   
-  rc = mu_list_create (&idlist);
-  if (rc)
+  if (argc == 1)
     {
-      mu_error ("mu_list_create: %s", mu_strerror (rc));
-      exit (1);
+      fprintf (stderr, "usage: %s: word|option [word|option...]\n", argv[0]);
+      fprintf (stderr, "options are: -out, -err, -reset\n");
+      return 1;
     }
-  uid = getuid ();
+
+  mu_set_program_name (argv[0]);
   
   for (i = 1; i < argc; i++)
     {
-      if (argv[i][0] == '-' || argv[i][0] == '+')
+      char *arg = argv[i];
+      if (arg[0] == '-')
 	{
-	  rc = mu_file_safety_name_to_code (argv[i] + 1, &m);
-	  if (rc)
+	  if (strcmp (arg, "-out") == 0)
+	    str = mu_strout;
+	  else if (strcmp (arg, "-err") == 0)
+	    str = mu_strerr;
+	  else if (strcmp (arg, "-reset") == 0)
 	    {
-	      mu_error ("%s: %s", argv[i], mu_strerror (rc));
-	      exit (1);
+	      if (str == mu_strout)
+		{
+		  mu_stdstream_setup (MU_STDSTREAM_RESET_STROUT);
+		  str = mu_strout;
+		}
+	      else
+		{
+		  mu_stdstream_setup (MU_STDSTREAM_RESET_STRERR);
+		  str = mu_strerr;
+		}
 	    }
-	  if (argv[i][0] == '-')
-	    mode &= ~m;
 	  else
-	    mode |= m;
+	    {
+	      fprintf (stderr, "%s: unrecognized option %s\n", argv[0], arg);
+	      return 1;
+	    }
 	}
       else
-	{
-	  rc = mu_file_safety_check (argv[i], mode, uid, idlist);
-	  printf ("%s: %s\n", argv[i], mu_strerror (rc));
-	}
+	mu_stream_printf (str, "%s\n", arg);
     }
-  exit (0);
+  
+  return 0;
 }
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 9235686..888c25a 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -54,6 +54,11 @@ AT_CLEANUP
 
 AT_INIT
 
+AT_BANNER([Standard streams])
+m4_include([strin.at])
+m4_include([strout.at])
+m4_include([strerr.at])
+
 m4_include([list.at])
 m4_include([address.at])
 m4_include([wordsplit.at])
diff --git a/libmu_sieve/util.c b/libmu_sieve/util.c
index 6dddfb4..bb0345d 100644
--- a/libmu_sieve/util.c
+++ b/libmu_sieve/util.c
@@ -419,7 +419,7 @@ mu_sieve_tag_lookup (mu_list_t taglist, char *name, mu_sieve_value_t **arg)
 }
 
 int
-mu_sieve_vlist_do (mu_sieve_value_t *val, mu_list_action_t *ac, void *data)
+mu_sieve_vlist_do (mu_sieve_value_t *val, mu_list_action_t ac, void *data)
 {
   switch (val->type)
     {
@@ -428,7 +428,7 @@ mu_sieve_vlist_do (mu_sieve_value_t *val, mu_list_action_t *ac, void *data)
       return mu_list_foreach (val->v.list, ac, data);
       
     default:
-      return -1;
+      return EINVAL;
     }
 }
 
diff --git a/mh/mhn.c b/mh/mhn.c
index 4882fc9..ead6c83 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -2187,9 +2187,14 @@ finish_text_msg (struct compose_env *env, mu_message_t *msg, int ascii)
 			     MU_STREAM_READ);
       if (rc == 0)
 	{
-	  mu_stream_copy (output, fstr, 0, NULL);
+	  rc = mu_stream_copy (output, fstr, 0, NULL);
 	  mu_stream_destroy (&fstr);
 	  mu_message_unref (*msg);
+	  if (rc)
+	    {
+	      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_copy", NULL, rc);
+	      exit (1);
+	    }
 	  *msg = newmsg;
 	}
       else
@@ -2533,8 +2538,13 @@ edit_mime (char *cmd, struct compose_env *env, mu_message_t *msg, int level)
 
   mu_message_get_body (*msg, &body);
   mu_body_get_streamref (body, &out);
-  mu_stream_copy (out, fstr, 0, NULL);
-
+  rc = mu_stream_copy (out, fstr, 0, NULL);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_copy", NULL, rc);
+      exit (1);
+    }
+  
   mu_stream_close (out);
   mu_stream_destroy (&out);
   mu_stream_destroy (&fstr);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 13 10:07:07 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RaTwh-0003gO-JO
	for mharc-commit-mailutils@gnu.org; Tue, 13 Dec 2011 10:07:07 -0500
Received: from eggs.gnu.org ([140.186.70.92]:54505)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaTwf-0003fy-7d
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 10:07:06 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaTwY-0002C1-Pi
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 10:07:05 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:40732)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaTwY-0002Bp-OB
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 10:06:58 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaTwX-0008PD-PR
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 15:06:58 +0000
Date: Tue, 13 Dec 2011 15:06:57 +0000
Message-Id: <E1RaTwX-0008PD-PR@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-523-gc8a26bc
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: a4b7febbcb907b96181361824fac23e24485c023
X-Git-Newrev: c8a26bc9014fbf2660072fd25faf1fa91c474dff
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 13 Dec 2011 15:07:06 -0000

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

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

The branch, master has been updated
       via  c8a26bc9014fbf2660072fd25faf1fa91c474dff (commit)
      from  a4b7febbcb907b96181361824fac23e24485c023 (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 c8a26bc9014fbf2660072fd25faf1fa91c474dff
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 13 17:05:50 2011 +0200

    Minor changes.
    
    * libmailutils/datetime/streamftime.c (mu_c_streamftime): Handle
    %Z separately.
    * libmu_sieve/sieve.y (mu_sieve_compile): Return MU_ERR_PARSE on
    errors.

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

Summary of changes:
 libmailutils/datetime/streamftime.c |    9 ++++++++-
 libmu_sieve/sieve.y                 |    7 +++----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/libmailutils/datetime/streamftime.c b/libmailutils/datetime/streamftime.c
index 311fe00..873a070 100644
--- a/libmailutils/datetime/streamftime.c
+++ b/libmailutils/datetime/streamftime.c
@@ -368,8 +368,15 @@ mu_c_streamftime (mu_stream_t str, const char *fmt, struct tm *input_tm,
 	  rc = mu_stream_printf (str, "%d", tm.tm_year + 1900);
 	  break;
 	  
-	case 'z':
 	case 'Z':
+	  /* The timezone or name or abbreviation. */
+	  if (tz && tz->tz_name)
+	    {
+	      rc = mu_stream_printf (str, "%s", tz->tz_name);
+	      break;
+	    }
+	  /* fall through */
+	case 'z':
 	  /* The time-zone as hour offset from GMT, for formatting RFC-822
 	     dates (e.g. "%a, %d %b %Y %H:%M:%S %z") */
 	  {
diff --git a/libmu_sieve/sieve.y b/libmu_sieve/sieve.y
index cdb59e2..897a194 100644
--- a/libmu_sieve/sieve.y
+++ b/libmu_sieve/sieve.y
@@ -612,13 +612,12 @@ mu_sieve_compile (mu_sieve_machine_t mach, const char *name)
 
   if (mu_sv_lex_begin (name) == 0)
     {
-      rc = yyparse ();
-      if (mu_sieve_error_count)
-	rc = 1;
+      if (yyparse () || mu_sieve_error_count)
+	rc = MU_ERR_PARSE;
       mu_sv_lex_finish ();
     }
   else
-    rc = 1;
+    rc = MU_ERR_FAILURE;
   
   mu_sieve_machine_finish (mach);
   return rc;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 13 13:06:22 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RaWkA-0004kw-2C
	for mharc-commit-mailutils@gnu.org; Tue, 13 Dec 2011 13:06:22 -0500
Received: from eggs.gnu.org ([140.186.70.92]:36899)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaWk1-0004kC-6K
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 13:06:19 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaWjw-0008Q5-Da
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 13:06:13 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:37752)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaWjw-0008Py-Bz
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 13:06:08 -0500
Received: from polak by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaWjv-000184-L8
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 18:06:07 +0000
Date: Tue, 13 Dec 2011 18:06:07 +0000
Message-Id: <E1RaWjv-000184-L8@vcs.savannah.gnu.org>
From: "Wojciech Polak" <polak@gnu.org>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-524-gaed829a
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: c8a26bc9014fbf2660072fd25faf1fa91c474dff
X-Git-Newrev: aed829a6c5a640f3e92015004b933db898e00d87
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 13 Dec 2011 18:06:19 -0000

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

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

The branch, master has been updated
       via  aed829a6c5a640f3e92015004b933db898e00d87 (commit)
      from  c8a26bc9014fbf2660072fd25faf1fa91c474dff (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 aed829a6c5a640f3e92015004b933db898e00d87
Author: Wojciech Polak <polak@gnu.org>
Date:   Tue Dec 13 18:57:43 2011 +0100

    Improve Python API. Add MemoryStream class.
    
    * python/libmu_py/sieve.c (api_sieve_machine_init): Improve.
    (api_sieve_machine_error_text): Add new function.
    * python/libmu_py/stream.c (api_memory_stream_create): Likewise.
    (api_stream_to_message): Likewise.
    * python/mailutils/error.py (Error): Add optional str_error argument.
    * python/mailutils/sieve.py: Update.
    * python/mailutils/stream.py (to_message): Add new method.
    (MemoryStream): Add new class.

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

Summary of changes:
 python/libmu_py/sieve.c    |   66 ++++++++++++++++++++++++++++++++++++++-----
 python/libmu_py/stream.c   |   42 ++++++++++++++++++++++++++++
 python/mailutils/error.py  |    4 +-
 python/mailutils/sieve.py  |    7 +++-
 python/mailutils/stream.py |   14 +++++++++
 5 files changed, 121 insertions(+), 12 deletions(-)

diff --git a/python/libmu_py/sieve.c b/python/libmu_py/sieve.c
index e654cbd..c32e5d7 100644
--- a/python/libmu_py/sieve.c
+++ b/python/libmu_py/sieve.c
@@ -94,15 +94,61 @@ api_sieve_machine_init (PyObject *self, PyObject *args)
 {
   int status;
   PySieveMachine *py_mach;
-
+  mu_stream_t str, estr;
+  
   if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
     return NULL;
 
-  status = mu_sieve_machine_init (&py_mach->mach);
+  status = mu_memory_stream_create (&str, MU_STREAM_RDWR);
+  if (status)
+    return _ro (PyInt_FromLong (status));
+  status = mu_log_stream_create (&estr, str);
+  mu_stream_unref (str);
+  if (status)
+    return _ro (PyInt_FromLong (status));
+  
+  status = mu_sieve_machine_init_ex (&py_mach->mach, NULL, estr);
+  mu_stream_unref (estr);
   return _ro (PyInt_FromLong (status));
 }
 
 static PyObject *
+api_sieve_machine_error_text (PyObject *self, PyObject *args)
+{
+  int status;
+  PySieveMachine *py_mach;
+  mu_stream_t estr;
+  mu_transport_t trans[2];
+  PyObject *retval;
+  size_t length = 0;
+  
+  if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
+    return NULL;
+  if (!py_mach->mach)
+    PyErr_SetString (PyExc_RuntimeError, "Uninitialized Sieve machine");
+      
+  mu_sieve_get_diag_stream (py_mach->mach, &estr);
+  status = mu_stream_ioctl (estr, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET,
+			    trans);
+  if (status == 0)
+    {
+      mu_stream_t str = (mu_stream_t) trans[0];
+
+      mu_stream_size (str, &length);
+      status = mu_stream_ioctl (str, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET,
+				trans);
+      mu_stream_truncate (str, 0);
+    }
+  
+  if (status)
+    PyErr_SetString (PyExc_RuntimeError, mu_strerror (status));
+  retval = PyString_FromStringAndSize ((char*) trans[0], length);
+
+  mu_stream_unref (estr);
+  return _ro (retval);
+}
+
+static PyObject *
 api_sieve_machine_destroy (PyObject *self, PyObject *args)
 {
   PySieveMachine *py_mach;
@@ -110,12 +156,13 @@ api_sieve_machine_destroy (PyObject *self, PyObject *args)
   if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
     return NULL;
 
-  if (py_mach->mach) {
-    struct _mu_py_sieve_logger *s = mu_sieve_get_data (py_mach->mach);
-    if (s)
-      free (s);
-    mu_sieve_machine_destroy (&py_mach->mach);
-  }
+  if (py_mach->mach)
+    {
+      struct _mu_py_sieve_logger *s = mu_sieve_get_data (py_mach->mach);
+      if (s)
+	free (s);
+      mu_sieve_machine_destroy (&py_mach->mach);
+    }
   return _ro (Py_None);
 }
 
@@ -280,6 +327,9 @@ static PyMethodDef methods[] = {
   { "machine_destroy", (PyCFunction) api_sieve_machine_destroy, METH_VARARGS,
     "Destroy Sieve 'machine'." },
 
+  { "errortext", (PyCFunction) api_sieve_machine_error_text, METH_VARARGS,
+    "Return the most recent error description." },
+  
   { "compile", (PyCFunction) api_sieve_compile, METH_VARARGS,
     "Compile the sieve script from the file 'name'." },
 
diff --git a/python/libmu_py/stream.c b/python/libmu_py/stream.c
index 34da70d..aa8e04c 100644
--- a/python/libmu_py/stream.c
+++ b/python/libmu_py/stream.c
@@ -126,6 +126,25 @@ api_stdio_stream_create (PyObject *self, PyObject *args)
 }
 
 static PyObject *
+api_memory_stream_create (PyObject *self, PyObject *args)
+{
+  int status;
+  char *s;
+  PyStream *py_stm;
+
+  if (!PyArg_ParseTuple (args, "O!s", &PyStreamType, &py_stm, &s))
+    return NULL;
+
+  status = mu_memory_stream_create (&py_stm->stm, MU_STREAM_RDWR);
+  if (status == 0)
+    {
+      status = mu_stream_write (py_stm->stm, s, strlen (s), NULL);
+      mu_stream_seek (py_stm->stm, MU_SEEK_SET, 0, NULL);
+    }
+  return _ro (PyInt_FromLong (status));
+}
+
+static PyObject *
 api_prog_stream_create (PyObject *self, PyObject *args)
 {
   int status, flags;
@@ -317,6 +336,22 @@ api_stream_readline (PyObject *self, PyObject *args)
   return _ro (py_ret);
 }
 
+static PyObject *
+api_stream_to_message (PyObject *self, PyObject *args)
+{
+  int status;
+  PyStream *py_stm;
+  PyMessage *py_msg = PyMessage_NEW ();
+
+  if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
+    return NULL;
+
+  Py_INCREF (py_msg);
+
+  status = mu_stream_to_message (py_stm->stm, &py_msg->msg);
+  return status_object (status, (PyObject *)py_msg);
+}
+
 static PyMethodDef methods[] = {
   { "tcp_stream_create", (PyCFunction) api_tcp_stream_create, METH_VARARGS,
     "" },
@@ -327,6 +362,10 @@ static PyMethodDef methods[] = {
   { "stdio_stream_create", (PyCFunction) api_stdio_stream_create, METH_VARARGS,
     "" },
 
+  { "memory_stream_create",
+    (PyCFunction) api_memory_stream_create, METH_VARARGS,
+    "" },
+
   { "prog_stream_create", (PyCFunction) api_prog_stream_create, METH_VARARGS,
     "" },
 
@@ -364,6 +403,9 @@ static PyMethodDef methods[] = {
   { "readline", (PyCFunction) api_stream_readline, METH_VARARGS,
     "" },
 
+  { "to_message", (PyCFunction) api_stream_to_message, METH_VARARGS,
+    "" },
+
   { NULL, NULL, 0, NULL }
 };
 
diff --git a/python/mailutils/error.py b/python/mailutils/error.py
index 503ffef..fcf24b2 100644
--- a/python/mailutils/error.py
+++ b/python/mailutils/error.py
@@ -22,9 +22,9 @@ def strerror (status):
     return error.strerror (status)
 
 class Error (Exception):
-    def __init__ (self, status):
+    def __init__ (self, status, str_error=None):
         self.status = status
-        self.strerror = strerror (status)
+        self.strerror = str_error or strerror (status)
     def __str__ (self):
         return "%d: %s" % (self.status, self.strerror)
 
diff --git a/python/mailutils/sieve.py b/python/mailutils/sieve.py
index 16a2331..ea28f4f 100644
--- a/python/mailutils/sieve.py
+++ b/python/mailutils/sieve.py
@@ -26,6 +26,9 @@ class Machine:
             raise SieveMachineError (status)
 
     def __del__ (self):
+        s = sieve.errortext (self.mach)
+        if s:
+            raise SieveMachineError (MU_ERR_FAILURE, s)
         sieve.machine_destroy (self.mach)
         del self.mach
 
@@ -38,7 +41,7 @@ class Machine:
         """Compile the sieve script from the file 'name'."""
         status = sieve.compile (self.mach, name)
         if status:
-            raise SieveMachineError (status)
+            raise SieveMachineError (status, sieve.errortext (self.mach))
 
     def disass (self):
         """Dump the disassembled code of the sieve machine."""
@@ -58,4 +61,4 @@ class Machine:
         over the 'msg'."""
         status = sieve.message (self.mach, msg.msg)
         if status:
-            raise SieveMachineError (status)
+            raise SieveMachineError (status, sieve.errortext (self.mach))
diff --git a/python/mailutils/stream.py b/python/mailutils/stream.py
index 8452301..1c2aa6a 100644
--- a/python/mailutils/stream.py
+++ b/python/mailutils/stream.py
@@ -103,6 +103,13 @@ class Stream:
             raise StreamError (status)
         return rbuf
 
+    def to_message (self):
+        from mailutils import message
+        status, msg = stream.to_message (self.stm)
+        if status:
+            raise StreamError (status)
+        return message.Message (msg)
+
 class TcpStream (Stream):
     def __init__ (self, host, port, flags=MU_STREAM_READ):
         Stream.__init__ (self)
@@ -124,6 +131,13 @@ class StdioStream (Stream):
         if status:
             raise StreamError (status)
 
+class MemoryStream (Stream):
+    def __init__ (self, s):
+        Stream.__init__ (self)
+        status = stream.memory_stream_create (self.stm, s)
+        if status:
+            raise StreamError (status)
+
 class ProgStream (Stream):
     def __init__ (self, progname, flags=MU_STREAM_READ):
         Stream.__init__ (self)


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 13 14:22:50 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RaXwA-0005bL-4Z
	for mharc-commit-mailutils@gnu.org; Tue, 13 Dec 2011 14:22:50 -0500
Received: from eggs.gnu.org ([140.186.70.92]:41816)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaXw7-0005bF-TB
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 14:22:48 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaXw6-0007cF-GN
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 14:22:47 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:59159)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaXw6-0007c9-Db
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 14:22:46 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaXw5-0002ud-TA
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 19:22:46 +0000
Date: Tue, 13 Dec 2011 19:22:45 +0000
Message-Id: <E1RaXw5-0002ud-TA@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-525-g85c5f9a
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: aed829a6c5a640f3e92015004b933db898e00d87
X-Git-Newrev: 85c5f9a8decef2583674bc957caef1818e9628a9
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 13 Dec 2011 19:22:48 -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=85c5f9a8decef2583674bc957caef1818e9628a9

The branch, master has been updated
       via  85c5f9a8decef2583674bc957caef1818e9628a9 (commit)
      from  aed829a6c5a640f3e92015004b933db898e00d87 (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 85c5f9a8decef2583674bc957caef1818e9628a9
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 13 21:21:46 2011 +0200

    Bugfixes.
    
    * libmu_sieve/sieve.y (mu_sieve_compile): Initialize rc.
    * python/libmu_py/sieve.c (api_sieve_machine_error_text): Fix
    type of length.

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

Summary of changes:
 libmu_sieve/sieve.y     |    2 +-
 python/libmu_py/sieve.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libmu_sieve/sieve.y b/libmu_sieve/sieve.y
index 897a194..8a1ee9e 100644
--- a/libmu_sieve/sieve.y
+++ b/libmu_sieve/sieve.y
@@ -606,7 +606,7 @@ mu_sieve_machine_finish (mu_sieve_machine_t mach)
 int
 mu_sieve_compile (mu_sieve_machine_t mach, const char *name)
 {
-  int rc;
+  int rc = 0;
   
   mu_sieve_machine_begin (mach, name);
 
diff --git a/python/libmu_py/sieve.c b/python/libmu_py/sieve.c
index c32e5d7..293c3ab 100644
--- a/python/libmu_py/sieve.c
+++ b/python/libmu_py/sieve.c
@@ -120,7 +120,7 @@ api_sieve_machine_error_text (PyObject *self, PyObject *args)
   mu_stream_t estr;
   mu_transport_t trans[2];
   PyObject *retval;
-  size_t length = 0;
+  mu_off_t length = 0;
   
   if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
     return NULL;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 13 16:02:36 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RaZUi-0005Kn-Dz
	for mharc-commit-mailutils@gnu.org; Tue, 13 Dec 2011 16:02:36 -0500
Received: from eggs.gnu.org ([140.186.70.92]:46186)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaZUe-0005KD-Kq
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 16:02:34 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaZUc-0004FY-69
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 16:02:32 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:47728)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaZUc-0004FT-2K
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 16:02:30 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RaZUI-0004K5-Cr
	for commit-mailutils@gnu.org; Tue, 13 Dec 2011 21:02:24 +0000
Date: Tue, 13 Dec 2011 21:02:10 +0000
Message-Id: <E1RaZUI-0004K5-Cr@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-526-g7e78c6a
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 85c5f9a8decef2583674bc957caef1818e9628a9
X-Git-Newrev: 7e78c6af4aa397ff422db07489850d40a9360a8c
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 13 Dec 2011 21:02: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=7e78c6af4aa397ff422db07489850d40a9360a8c

The branch, master has been updated
       via  7e78c6af4aa397ff422db07489850d40a9360a8c (commit)
      from  85c5f9a8decef2583674bc957caef1818e9628a9 (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 7e78c6af4aa397ff422db07489850d40a9360a8c
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 13 22:56:32 2011 +0200

    Imap client: implement list.
    
    * libmailutils/stdstream/basestr.c (mu_strout): Bugfix: initialize
    destroy function.
    * include/mailutils/imap.h (imap_command)
    (mu_imap_gencom): Move to sys/imap.h
    (mu_imap_list,mu_imap_list_new): New protos.
    * include/mailutils/sys/imap.h (imap_command): New struct (from ../imap.h).
    <handler>: Rename to tagged_handler.
    (untagged_handler,untagged_handler_data): New members. All uses changed.
    (mu_imap_gencom): New proto.
    
    * libproto/imap/list.c: New file.
    * libproto/imap/Makefile.am: Add list.c
    * libproto/imap/gencom.c: Use supplied untagged_handler to
    analize untagged response.
    
    * mu/imap.c: Implement list command.

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

Summary of changes:
 include/mailutils/imap.h         |   18 +---
 include/mailutils/sys/imap.h     |   22 ++++-
 libmailutils/stdstream/basestr.c |    1 +
 libproto/imap/Makefile.am        |    1 +
 libproto/imap/copy.c             |    3 +-
 libproto/imap/delete.c           |    3 +-
 libproto/imap/fetch.c            |    3 +-
 libproto/imap/gencom.c           |    7 +-
 libproto/imap/list.c             |  172 ++++++++++++++++++++++++++++++++++++++
 libproto/imap/mbcreate.c         |    3 +-
 libproto/imap/rename.c           |    3 +-
 libproto/imap/store.c            |    3 +-
 mu/imap.c                        |   41 +++++++++
 13 files changed, 255 insertions(+), 25 deletions(-)
 create mode 100644 libproto/imap/list.c

diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index b247676..bdce14c 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -44,19 +44,6 @@ enum mu_imap_session_state
 int mu_imap_create (mu_imap_t *pimap);
 void mu_imap_destroy (mu_imap_t *pimap);
 
-struct imap_command
-{
-  int session_state;
-  char *capa;
-  int rx_state;
-  int uid;
-  int argc;
-  char const **argv;
-  void (*handler) (mu_imap_t);
-};
-
-int mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd);
-  
 int mu_imap_connect (mu_imap_t imap);
 int mu_imap_disconnect (mu_imap_t imap);
 
@@ -100,6 +87,11 @@ int mu_imap_append_message (mu_imap_t imap, const char *mailbox, int flags,
 			    struct tm *tm, struct mu_timezone *tz,
 			    mu_message_t msg);
   
+int mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
+		  mu_list_t retlist);
+int mu_imap_list_new (mu_imap_t imap, const char *refname, const char *mboxname,
+		      mu_list_t *plist);
+  
 int mu_imap_set_carrier (mu_imap_t imap, mu_stream_t carrier);
 int mu_imap_get_carrier (mu_imap_t imap, mu_stream_t *pcarrier);
 
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index 05ec9db..9c50e84 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -66,6 +66,7 @@ enum mu_imap_client_state
     MU_IMAP_CLIENT_COPY_RX,
     MU_IMAP_CLIENT_EXPUNGE_RX,
     MU_IMAP_CLIENT_APPEND_RX,
+    MU_IMAP_CLIENT_LIST_RX,
     MU_IMAP_CLIENT_CLOSING
   };
 
@@ -135,6 +136,24 @@ int _mu_imap_trace_enable (mu_imap_t imap);
 int _mu_imap_trace_disable (mu_imap_t imap);
 int _mu_imap_xscript_level (mu_imap_t imap, int xlev);
 
+typedef void (*mu_imap_response_action_t) (mu_imap_t imap, mu_list_t resp,
+					   void *data);
+
+struct imap_command
+{
+  int session_state;
+  char *capa;
+  int rx_state;
+  int uid;
+  int argc;
+  char const **argv;
+  void (*tagged_handler) (mu_imap_t);
+  mu_imap_response_action_t untagged_handler;
+  void *untagged_handler_data;
+};
+
+int mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd);
+  
 /* If status indicates an error, return.
   */
 #define MU_IMAP_CHECK_ERROR(imap, status)			\
@@ -182,9 +201,6 @@ int _mu_imap_tag_next (mu_imap_t imap);
 int _mu_imap_tag_clr (mu_imap_t imap);
 
 
-typedef void (*mu_imap_response_action_t) (mu_imap_t imap, mu_list_t resp,
-					   void *data);
-
 int _mu_imap_untagged_response_to_list (mu_imap_t imap, mu_list_t *plist);
 int _mu_imap_process_untagged_response (mu_imap_t imap, mu_list_t list,
 					mu_imap_response_action_t fun,
diff --git a/libmailutils/stdstream/basestr.c b/libmailutils/stdstream/basestr.c
index ec9ae29..1e6df45 100644
--- a/libmailutils/stdstream/basestr.c
+++ b/libmailutils/stdstream/basestr.c
@@ -128,6 +128,7 @@ static struct _mu_file_stream stdstream[2] = {
   { { ref_count: 1,
       buftype: mu_buffer_none,
       flags: MU_STREAM_WRITE,
+      destroy: bootstrap_destroy,
       event_cb: std_bootstrap,
       event_mask: _MU_STR_EVMASK (_MU_STR_EVENT_BOOTSTRAP)
     }, fd: MU_STDOUT_FD, filename: "<stdout>",
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index 41d023d..dfed78c 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -48,6 +48,7 @@ libmu_imap_la_SOURCES = \
  err.c\
  expunge.c\
  id.c\
+ list.c\
  login.c\
  logout.c\
  mbcreate.c\
diff --git a/libproto/imap/copy.c b/libproto/imap/copy.c
index 5a11a1c..507b701 100644
--- a/libproto/imap/copy.c
+++ b/libproto/imap/copy.c
@@ -39,7 +39,8 @@ mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
   com.uid = 0;
   com.argc = 3;
   com.argv = argv;
-  com.handler = NULL;
+  com.tagged_handler = NULL;
+  com.untagged_handler = NULL;
 
   return mu_imap_gencom (imap, &com);
 }
diff --git a/libproto/imap/delete.c b/libproto/imap/delete.c
index e29f028..6f986e9 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/delete.c
@@ -40,7 +40,8 @@ mu_imap_delete (mu_imap_t imap, const char *mailbox)
   com.uid = 0;
   com.argc = 2;
   com.argv = argv;
-  com.handler = NULL;
+  com.tagged_handler = NULL;
+  com.untagged_handler = NULL;
 
   return mu_imap_gencom (imap, &com);
 }
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index 1d7c0f2..cecf794 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -44,7 +44,8 @@ mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
   com.uid = uid;
   com.argc = 3;
   com.argv = argv;
-  com.handler = NULL;
+  com.tagged_handler = NULL;
+  com.untagged_handler = NULL;
 
   return mu_imap_gencom (imap, &com);
 }
diff --git a/libproto/imap/gencom.c b/libproto/imap/gencom.c
index 5786a2b..ce4b350 100644
--- a/libproto/imap/gencom.c
+++ b/libproto/imap/gencom.c
@@ -74,10 +74,11 @@ mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
 
   if (imap->client_state == cmd->rx_state)
     {
-      status = _mu_imap_response (imap, NULL, NULL);
+      status = _mu_imap_response (imap, cmd->untagged_handler,
+				  cmd->untagged_handler_data);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      if (cmd->handler)
-	  cmd->handler (imap);
+      if (cmd->tagged_handler)
+	  cmd->tagged_handler (imap);
       switch (imap->resp_code)
 	{
 	case MU_IMAP_OK:
diff --git a/libproto/imap/list.c b/libproto/imap/list.c
new file mode 100644
index 0000000..9fb9696
--- /dev/null
+++ b/libproto/imap/list.c
@@ -0,0 +1,172 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/address.h>
+#include <mailutils/cstr.h>
+#include <mailutils/cctype.h>
+#include <mailutils/list.h>
+#include <mailutils/imap.h>
+#include <mailutils/sys/imap.h>
+
+struct list_closure
+{
+  int error_code;
+  mu_list_t retlist;
+};
+
+static int
+count_level (const char *name, int delim)
+{
+  int level = 0;
+
+  while (*name)
+    if (*name++ == delim)
+      level++;
+  return level;
+}
+
+static int
+list_attr_conv (void *item, void *data)
+{
+  struct imap_list_element *elt = item;
+  struct mu_list_response *rp = data;
+  
+  if (elt->type != imap_eltype_string)
+    return 0;
+  if (mu_c_strcasecmp (elt->v.string, "\\Noinferiors"))
+    rp->type |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
+  if (mu_c_strcasecmp (elt->v.string, "\\Noselect"))
+    rp->type |= MU_FOLDER_ATTRIBUTE_FILE;
+  /* FIXME: \Marked nad \Unmarked have no correspondence in flags. */
+  return 0;
+}
+  
+static void
+list_untagged_handler (mu_imap_t imap, mu_list_t resp, void *data)
+{
+  struct list_closure *clos = data;
+  struct imap_list_element *elt;
+  size_t count;
+  
+  if (clos->error_code)
+    return;
+
+  mu_list_count (resp, &count);
+  if (count == 4 &&
+      _mu_imap_list_nth_element_is_string (resp, 0, "LIST"))
+    {
+      struct mu_list_response *rp;
+
+      rp = calloc (1, sizeof (*rp));
+      if (!rp)
+	{
+	  clos->error_code = ENOMEM;
+	  return;
+	}
+	  
+      elt = _mu_imap_list_at (resp, 1);
+      if (!(elt && elt->type == imap_eltype_list))
+	return;
+      rp->type = 0;
+      mu_list_foreach (elt->v.list, list_attr_conv, rp);
+
+      elt = _mu_imap_list_at (resp, 3);
+      if (!(elt && elt->type == imap_eltype_string))
+	return;
+      rp->name = strdup (elt->v.string);
+      if (!rp->name)
+	{
+	  free (rp);
+	  clos->error_code = ENOMEM;
+	  return;
+	}
+
+      elt = _mu_imap_list_at (resp, 2);
+      if (!(elt && elt->type == imap_eltype_string))
+	return;
+      if (mu_c_strcasecmp (elt->v.string, "NIL") == 0)
+	{
+	  rp->separator = 0;
+	  rp->level = 0;
+	}
+      else
+	{
+	  rp->separator = elt->v.string[0];
+	  rp->level = count_level (rp->name, rp->separator);
+	}
+      if ((clos->error_code = mu_list_append (clos->retlist, rp)))
+	mu_list_response_free (rp);
+    }
+}
+  
+int
+mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
+	      mu_list_t retlist)
+{
+  char const *argv[3];
+  static struct imap_command com;
+  struct list_closure clos;
+  int rc;
+
+  argv[0] = "LIST";
+  argv[1] = refname;
+  argv[2] = mboxname;
+
+  clos.error_code = 0;
+  clos.retlist = retlist;
+  
+  com.session_state = MU_IMAP_SESSION_AUTH;
+  com.capa = NULL;
+  com.rx_state = MU_IMAP_CLIENT_LIST_RX;
+  com.uid = 0;
+  com.argc = 3;
+  com.argv = argv;
+  com.tagged_handler = NULL;
+  com.untagged_handler = list_untagged_handler;
+  com.untagged_handler_data = &clos;
+
+  rc = mu_imap_gencom (imap, &com);
+  if (rc == 0)
+    rc = clos.error_code;
+
+  return rc;
+}
+
+int
+mu_imap_list_new (mu_imap_t imap, const char *refname, const char *mboxname,
+		  mu_list_t *plist)
+{
+  mu_list_t list;
+  int rc = mu_list_create (&list);
+  if (rc == 0)
+    {
+      mu_list_set_destroy_item (list, mu_list_response_free);
+      rc = mu_imap_list (imap, refname, mboxname, list);
+      if (rc)
+	mu_list_destroy (&list);
+      else
+	*plist = list;
+    }
+  return rc;
+}
diff --git a/libproto/imap/mbcreate.c b/libproto/imap/mbcreate.c
index 845d1ba..b7b9063 100644
--- a/libproto/imap/mbcreate.c
+++ b/libproto/imap/mbcreate.c
@@ -40,7 +40,8 @@ mu_imap_mailbox_create (mu_imap_t imap, const char *mailbox)
   com.uid = 0;
   com.argc = 2;
   com.argv = argv;
-  com.handler = NULL;
+  com.tagged_handler = NULL;
+  com.untagged_handler = NULL;
 
   return mu_imap_gencom (imap, &com);
 }
diff --git a/libproto/imap/rename.c b/libproto/imap/rename.c
index 79b3778..44d0773 100644
--- a/libproto/imap/rename.c
+++ b/libproto/imap/rename.c
@@ -39,7 +39,8 @@ mu_imap_rename (mu_imap_t imap, const char *mailbox, const char *new_mailbox)
   com.uid = 0;
   com.argc = 3;
   com.argv = argv;
-  com.handler = NULL;
+  com.tagged_handler = NULL;
+  com.untagged_handler = NULL;
 
   return mu_imap_gencom (imap, &com);
 }
diff --git a/libproto/imap/store.c b/libproto/imap/store.c
index be0d728..8faecac 100644
--- a/libproto/imap/store.c
+++ b/libproto/imap/store.c
@@ -41,7 +41,8 @@ mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
   com.uid = uid;
   com.argc = 3;
   com.argv = argv;
-  com.handler = NULL;
+  com.tagged_handler = NULL;
+  com.untagged_handler = NULL;
 
   return mu_imap_gencom (imap, &com);
 }
diff --git a/mu/imap.c b/mu/imap.c
index 26a3401..699e270 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -895,6 +895,43 @@ com_append (int argc, char **argv)
   return 0;
 }
 
+static int
+print_list_item (void *item, void *data)
+{
+  struct mu_list_response *resp = item;
+  mu_stream_t out = data;
+
+  mu_stream_printf (out,
+		    "%c%c %c %4d %s\n",
+		    (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY) ? 'd' : '-',
+		    (resp->type & MU_FOLDER_ATTRIBUTE_FILE) ? 'f' : '-',
+		    resp->separator,
+		    resp->level,
+		    resp->name);
+  return 0;
+}
+
+static int
+com_list (int argc, char **argv)
+{
+  mu_list_t list;
+  int rc;
+  mu_stream_t out;
+  
+  rc = mu_imap_list_new (imap, argv[1], argv[2], &list);
+  if (rc)
+    {
+      report_failure ("list", rc);
+      return 0;
+    }
+
+  out = mutool_open_pager ();
+  mu_list_foreach (list, print_list_item, out);
+  mu_stream_unref (out);
+  return 0;
+}
+
+
 struct mutool_command imap_comtab[] = {
   { "capability",   1, -1, 0,
     com_capability,
@@ -982,6 +1019,10 @@ struct mutool_command imap_comtab[] = {
     com_append,
     N_("[-time DATETIME] [-flag FLAG] MAILBOX FILE"),
     N_("append message text from FILE to MAILBOX") },
+  { "list",         3, 3, 0,
+    com_list,
+    N_("REF MBOX"),
+    N_("List matching mailboxes") },
   { "quit",         1, 1, 0,
     com_logout,
     NULL,


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Dec 14 04:52:29 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RalVl-0005U5-Nm
	for mharc-commit-mailutils@gnu.org; Wed, 14 Dec 2011 04:52:29 -0500
Received: from eggs.gnu.org ([140.186.70.92]:57510)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RalVg-0005Tf-Kk
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 04:52:27 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RalVY-0005PY-8R
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 04:52:24 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:57947)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RalVY-0005PU-2B
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 04:52:16 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RalVW-0005XF-Ql
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 09:52:15 +0000
Date: Wed, 14 Dec 2011 09:52:14 +0000
Message-Id: <E1RalVW-0005XF-Ql@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-529-gf675ddc
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 7e78c6af4aa397ff422db07489850d40a9360a8c
X-Git-Newrev: f675ddc76948a39c4b6dd4a47460a63e731e927a
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 14 Dec 2011 09:52:28 -0000

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

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

The branch, master has been updated
       via  f675ddc76948a39c4b6dd4a47460a63e731e927a (commit)
       via  bc02e5c2d416789b39aa538c3213b3365f3fab75 (commit)
       via  31459010ef8c1bf790b769260788982c714624d7 (commit)
      from  7e78c6af4aa397ff422db07489850d40a9360a8c (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 f675ddc76948a39c4b6dd4a47460a63e731e927a
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 14 11:50:04 2011 +0200

    Imap client: implement LSUB.
    
    LSUB is pretty similar to LIST, therefore it is implemented as a
    wrapper over the latter.
    
    * include/mailutils/imap.h (mu_imap_genlist,mu_imap_genlist_new)
    (mu_imap_lsub,mu_imap_lsub_new): New protos.
    * include/mailutils/sys/imap.h (MU_IMAP_CLIENT_LSUB_RX): New state.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Add new files.
    * libproto/imap/genlist.c: New file.
    * libproto/imap/list.c: Rewrite using generalized list functions.
    * libproto/imap/lsub.c: New file.
    * mu/imap.c: Implement lsub.

commit bc02e5c2d416789b39aa538c3213b3365f3fab75
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 14 11:31:37 2011 +0200

    Imap client: Implement SUBSCRIBE and UNSUBSCRIBE.
    
    * include/mailutils/imap.h (mu_imap_subscribe)
    (mu_imap_unsubscribe): New protos.
    * include/mailutils/sys/imap.h (MU_IMAP_CLIENT_SUBSCRIBE_RX)
    (MU_IMAP_CLIENT_UNSUBSCRIBE_RX): New states.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Add
    subscribe.c and unsubscribe.c
    * libproto/imap/subscribe.c: New file.
    * libproto/imap/unsubscribe.c: New file.
    * mu/imap.c: Implement subscribe/unsubscribe.

commit 31459010ef8c1bf790b769260788982c714624d7
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 14 11:10:38 2011 +0200

    Imap client: quote command arguments as necessary.
    
    * include/mailutils/imapio.h (mu_imapio_send_command)
    (mu_imapio_send_command_e,mu_imapio_send_command_v)
    (mu_imapio_last_error,mu_imapio_clearerr): New protos.
    * include/mailutils/sys/imap.h (imap_command)<uid>: Remove.
    <extra>: New member.
    * libmailutils/imapio/Makefile.am (libimapio_la_SOURCES): Add new files.
    * libmailutils/imapio/qstring.c (mu_imapio_send_qstring_unfold): Improve
    functionality.
    * libmailutils/imapio/sendcmd.c: New file.
    * libmailutils/imapio/sendcmde.c: New file.
    * libmailutils/imapio/sendcmdv.c: New file.
    * libmailutils/imapio/errstate.c: New file.
    
    * libproto/imap/gencom.c (mu_imap_gencom): Use mu_imapio_send_command_v
    * libproto/imap/appstrsiz.c: Quote command arguments as necessary.
    * libproto/imap/capability.c: Likewise.
    * libproto/imap/check.c: Likewise.
    * libproto/imap/close.c: Likewise.
    * libproto/imap/copy.c: Likewise.
    * libproto/imap/delete.c: Likewise.
    * libproto/imap/expunge.c: Likewise.
    * libproto/imap/fetch.c: Likewise.
    * libproto/imap/list.c: Likewise.
    * libproto/imap/login.c: Likewise.
    * libproto/imap/mbcreate.c: Likewise.
    * libproto/imap/noop.c: Likewise.
    * libproto/imap/rename.c: Likewise.
    * libproto/imap/select.c: Likewise.
    * libproto/imap/store.c: Likewise.
    * libproto/imap/unselect.c: Likewise.

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

Summary of changes:
 include/mailutils/imap.h                        |   19 +++-
 include/mailutils/imapio.h                      |   10 ++
 include/mailutils/sys/imap.h                    |    5 +-
 libmailutils/imapio/Makefile.am                 |    4 +
 libmailutils/imapio/{send.c => errstate.c}      |   16 ++-
 libmailutils/imapio/qstring.c                   |   74 ++++++++----
 libmailutils/imapio/{printf.c => sendcmd.c}     |   22 +++-
 libmailutils/imapio/{litstring.c => sendcmde.c} |   42 ++++---
 libmailutils/imapio/{litstring.c => sendcmdv.c} |   37 +++---
 libproto/imap/Makefile.am                       |    6 +-
 libproto/imap/appstrsiz.c                       |    5 +-
 libproto/imap/capability.c                      |    4 +-
 libproto/imap/check.c                           |    1 -
 libproto/imap/close.c                           |    2 +-
 libproto/imap/copy.c                            |   16 ++-
 libproto/imap/delete.c                          |    2 +-
 libproto/imap/expunge.c                         |    1 -
 libproto/imap/fetch.c                           |   13 ++-
 libproto/imap/gencom.c                          |   13 +--
 libproto/imap/{list.c => genlist.c}             |   22 ++--
 libproto/imap/list.c                            |  142 +----------------------
 libproto/imap/login.c                           |    4 +-
 libproto/imap/{check.c => lsub.c}               |   26 ++---
 libproto/imap/mbcreate.c                        |    2 +-
 libproto/imap/noop.c                            |    1 -
 libproto/imap/rename.c                          |    2 +-
 libproto/imap/select.c                          |    7 +-
 libproto/imap/store.c                           |   13 ++-
 libproto/imap/{delete.c => subscribe.c}         |   11 +-
 libproto/imap/unselect.c                        |    2 +-
 libproto/imap/{delete.c => unsubscribe.c}       |   11 +-
 mu/imap.c                                       |   49 ++++++++
 32 files changed, 291 insertions(+), 293 deletions(-)
 copy libmailutils/imapio/{send.c => errstate.c} (80%)
 copy libmailutils/imapio/{printf.c => sendcmd.c} (61%)
 copy libmailutils/imapio/{litstring.c => sendcmde.c} (52%)
 copy libmailutils/imapio/{litstring.c => sendcmdv.c} (54%)
 copy libproto/imap/{list.c => genlist.c} (87%)
 copy libproto/imap/{check.c => lsub.c} (72%)
 copy libproto/imap/{delete.c => subscribe.c} (85%)
 copy libproto/imap/{delete.c => unsubscribe.c} (85%)

diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index bdce14c..93d9ea3 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -87,10 +87,25 @@ int mu_imap_append_message (mu_imap_t imap, const char *mailbox, int flags,
 			    struct tm *tm, struct mu_timezone *tz,
 			    mu_message_t msg);
   
+int mu_imap_genlist (mu_imap_t imap, int lsub,
+		     const char *refname, const char *mboxname,
+		     mu_list_t retlist);
+int mu_imap_genlist_new (mu_imap_t imap, int lsub,
+			 const char *refname, const char *mboxname,
+			 mu_list_t *plist);
+  
 int mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
 		  mu_list_t retlist);
-int mu_imap_list_new (mu_imap_t imap, const char *refname, const char *mboxname,
-		      mu_list_t *plist);
+int mu_imap_list_new (mu_imap_t imap, const char *refname,
+		      const char *mboxname, mu_list_t *plist);
+
+int mu_imap_lsub (mu_imap_t imap, const char *refname, const char *mboxname,
+		  mu_list_t retlist);
+int mu_imap_lsub_new (mu_imap_t imap, const char *refname,
+		      const char *mboxname, mu_list_t *plist);
+
+int mu_imap_subscribe (mu_imap_t imap, const char *mailbox);
+int mu_imap_unsubscribe (mu_imap_t imap, const char *mailbox);
   
 int mu_imap_set_carrier (mu_imap_t imap, mu_stream_t carrier);
 int mu_imap_get_carrier (mu_imap_t imap, mu_stream_t *pcarrier);
diff --git a/include/mailutils/imapio.h b/include/mailutils/imapio.h
index e6d7594..f637fb9 100644
--- a/include/mailutils/imapio.h
+++ b/include/mailutils/imapio.h
@@ -45,6 +45,13 @@ int mu_imapio_send_qstring (struct _mu_imapio *io, const char *buffer);
 int mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
 				   int unfold);
 
+int mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
+			      int argc, char const **argv, const char *extra);
+int mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
+			char const *cmd, ...);
+int mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
+			      char const *cmd, ...);
+  
 int mu_imapio_send_flags (struct _mu_imapio *io, int flags);
 int mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
 			 struct mu_timezone *tz);
@@ -59,6 +66,9 @@ int mu_imapio_getbuf (mu_imapio_t io, char **pptr, size_t *psize);
 
 int mu_imapio_reply_string (struct _mu_imapio *io, size_t start, char **pbuf);
 
+int mu_imapio_last_error (struct _mu_imapio *io);
+void mu_imapio_clearerr (struct _mu_imapio *io);
+  
 int mu_imap_flag_to_attribute (const char *item, int *attr);
 int mu_imap_format_flags (mu_stream_t str, int flags);
 
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index 9c50e84..40c23a5 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -67,6 +67,9 @@ enum mu_imap_client_state
     MU_IMAP_CLIENT_EXPUNGE_RX,
     MU_IMAP_CLIENT_APPEND_RX,
     MU_IMAP_CLIENT_LIST_RX,
+    MU_IMAP_CLIENT_SUBSCRIBE_RX,
+    MU_IMAP_CLIENT_UNSUBSCRIBE_RX,
+    MU_IMAP_CLIENT_LSUB_RX,
     MU_IMAP_CLIENT_CLOSING
   };
 
@@ -144,9 +147,9 @@ struct imap_command
   int session_state;
   char *capa;
   int rx_state;
-  int uid;
   int argc;
   char const **argv;
+  char const *extra;
   void (*tagged_handler) (mu_imap_t);
   mu_imap_response_action_t untagged_handler;
   void *untagged_handler_data;
diff --git a/libmailutils/imapio/Makefile.am b/libmailutils/imapio/Makefile.am
index d3ce8a1..f0736b4 100644
--- a/libmailutils/imapio/Makefile.am
+++ b/libmailutils/imapio/Makefile.am
@@ -19,6 +19,7 @@ noinst_LTLIBRARIES = libimapio.la
 
 libimapio_la_SOURCES = \
  create.c\
+ errstate.c\
  flags.c\
  getline.c\
  litstream.c\
@@ -27,6 +28,9 @@ libimapio_la_SOURCES = \
  qstring.c\
  replstr.c\
  send.c\
+ sendcmd.c\
+ sendcmdv.c\
+ sendcmde.c\
  time.c\
  trace.c\
  words.c\
diff --git a/libmailutils/imapio/send.c b/libmailutils/imapio/errstate.c
similarity index 80%
copy from libmailutils/imapio/send.c
copy to libmailutils/imapio/errstate.c
index 11ee049..98fa0a6 100644
--- a/libmailutils/imapio/send.c
+++ b/libmailutils/imapio/errstate.c
@@ -15,15 +15,19 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdarg.h>
-#include <mailutils/types.h>
-#include <mailutils/imapio.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/imapio.h>
 
 int
-mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes)
+mu_imapio_last_error (struct _mu_imapio *io)
 {
-  return mu_stream_write (io->_imap_stream, buf, bytes, NULL);
+  return mu_stream_last_error (io->_imap_stream);
 }
-    
+
+void
+mu_imapio_clearerr (struct _mu_imapio *io)
+{
+  mu_stream_clearerr (io->_imap_stream);
+}
+
+
diff --git a/libmailutils/imapio/qstring.c b/libmailutils/imapio/qstring.c
index e4e7ea5..8cbeadc 100644
--- a/libmailutils/imapio/qstring.c
+++ b/libmailutils/imapio/qstring.c
@@ -19,50 +19,80 @@
 #include <mailutils/types.h>
 #include <mailutils/imapio.h>
 #include <mailutils/stream.h>
+#include <mailutils/cstr.h>
+#include <mailutils/cctype.h>
 #include <mailutils/sys/imapio.h>
 
-/* Send NIL if empty string, change the quoted string to a literal if the
-   string contains: double quotes, CR, LF, or \. */
+/* If string is NULL, send NIL.
+   If it contains \r\n, send it as a literal, replacing 
+   contiguous sequences of \r\n by a single space, if UNFOLD is set.
+   If string contains " or \, quote it,
+   Otherwise send it as is. */
 
 int
 mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
 			       int unfold)
 {
-  if (buffer == NULL || *buffer == '\0')
+  int len;
+  
+  if (buffer == NULL)
     return mu_imapio_printf (io, "NIL");
-  if (strchr (buffer, '"') ||
-      strchr (buffer, '\r') ||
-      strchr (buffer, '\n') ||
-      strchr (buffer, '\\'))
+
+  if (buffer[len = strcspn (buffer, "\r\n")])
     {
       if (unfold)
 	{
 	  int rc;
-	  size_t len = strlen (buffer);
+	  size_t size = strlen (buffer);
 
 	  rc = mu_stream_printf (io->_imap_stream,
-				 "{%lu}\n", (unsigned long) strlen (buffer));
+				 "{%lu}\n", (unsigned long) size);
 	  if (rc)
 	    return rc;
-	  for (;;)
+	  while (1)
 	    {
-	      size_t s = strcspn (buffer, "\r\n");
-	      rc = mu_stream_write (io->_imap_stream, buffer, s, NULL);
-	      if (rc)
-		return rc;
-	      len -= s;
-	      if (len == 0)
+	      mu_stream_write (io->_imap_stream, buffer, len, NULL);
+	      buffer += len;
+	      if (*buffer)
+		{
+		  mu_stream_write (io->_imap_stream, " ", 1, NULL);
+		  buffer = mu_str_skip_class (buffer, MU_CTYPE_ENDLN);
+		  len = strcspn (buffer, "\r\n");
+		}
+	      else
 		break;
-	      buffer += s;
-	      rc = mu_stream_write (io->_imap_stream, " ", 1, NULL);
-	      if (rc)
-		return rc;
 	    }
 	}
       else
-	return mu_imapio_send_literal_string (io, buffer);
+	mu_imapio_send_literal_string (io, buffer);
+    }
+  else if (buffer[len = strcspn (buffer, io->_imap_ws.ws_escape)])
+    {
+      int rc;
+      
+      rc = mu_stream_write (io->_imap_stream, "\"", 1, NULL);
+      while (1)
+	{
+	  mu_stream_write (io->_imap_stream, buffer, len, NULL);
+	  buffer += len;
+	  if (*buffer)
+	    {
+	      mu_stream_write (io->_imap_stream, "\\", 1, NULL);
+	      mu_stream_write (io->_imap_stream, buffer, 1, NULL);
+	      buffer++;
+	      len = strcspn (buffer, io->_imap_ws.ws_escape);
+	    }
+	  else
+	    break;
+	}
+      mu_stream_write (io->_imap_stream, "\"", 1, NULL);
     }
-  return mu_imapio_printf (io, "\"%s\"", buffer);
+  else if (buffer[0] == 0 || buffer[strcspn (buffer, io->_imap_ws.ws_delim)])
+    mu_stream_printf (io->_imap_stream, "\"%s\"", buffer);
+  else
+    mu_stream_write (io->_imap_stream, buffer, len, NULL);
+      
+  return mu_stream_last_error (io->_imap_stream);
 }
 
 int
diff --git a/libmailutils/imapio/printf.c b/libmailutils/imapio/sendcmd.c
similarity index 61%
copy from libmailutils/imapio/printf.c
copy to libmailutils/imapio/sendcmd.c
index 801e82a..bf4c5f6 100644
--- a/libmailutils/imapio/printf.c
+++ b/libmailutils/imapio/sendcmd.c
@@ -16,20 +16,28 @@
 
 #include <config.h>
 #include <stdarg.h>
-#include <mailutils/types.h>
+#include <string.h>
 #include <mailutils/imapio.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/imapio.h>
 
+/* Send a IMAP command to the server, quoting its arguments as necessary.
+   TAG is the command tag, CMD is the command verb.  Command arguments are
+   given in variadic list terminated with NULL. */
 int
-mu_imapio_printf (mu_imapio_t io, const char *fmt, ...)
+mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
+			char const *cmd, ...)
 {
   va_list ap;
-  int status;
   
-  va_start (ap, fmt);
-  status = mu_stream_vprintf (io->_imap_stream, fmt, ap);
+  va_start (ap, cmd);
+  mu_imapio_printf (io, "%s %s", tag, cmd);
+  while ((cmd = va_arg (ap, char *)))
+    {
+      mu_imapio_send (io, " ", 1);
+      mu_imapio_send_qstring (io, cmd);
+    }
   va_end (ap);
-  return status;
+  mu_imapio_send (io, "\r\n", 2);
+  return mu_stream_last_error (io->_imap_stream);
 }
-    
diff --git a/libmailutils/imapio/litstring.c b/libmailutils/imapio/sendcmde.c
similarity index 52%
copy from libmailutils/imapio/litstring.c
copy to libmailutils/imapio/sendcmde.c
index 5ccd5fa..1618c52 100644
--- a/libmailutils/imapio/litstring.c
+++ b/libmailutils/imapio/sendcmde.c
@@ -15,29 +15,37 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
-#include <mailutils/types.h>
-#include <mailutils/errno.h>
 #include <mailutils/imapio.h>
-#include <mailutils/sys/imapio.h>
 #include <mailutils/stream.h>
+#include <mailutils/sys/imapio.h>
 
+/* Send a IMAP command to the server, quoting its arguments as necessary.
+   TAG is the command tag, CMD is the command verb.  Command arguments are
+   given in variadic list terminated with NULL. The last argument is sent
+   over the wire as is, without quoting. */
 int
-mu_imapio_send_literal_string (struct _mu_imapio *io, const char *buffer)
+mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
+			  char const *cmd, ...)
 {
-  size_t len = strlen (buffer);
-
-  mu_stream_printf (io->_imap_stream, "{%lu}\r\n", (unsigned long) len);
-
-  if (!io->_imap_server)
+  va_list ap;
+  
+  va_start (ap, cmd);
+  mu_imapio_printf (io, "%s %s", tag, cmd);
+  cmd = va_arg (ap, char *);
+  while (cmd)
     {
-      int rc = mu_imapio_getline (io);
-      if (rc)
-	return rc;
-      if (!(io->_imap_reply_ready && io->_imap_ws.ws_wordv[0][0] == '+'))
-	return MU_ERR_BADREPLY;
+      char const *next = va_arg (ap, char *);
+      
+      mu_imapio_send (io, " ", 1);
+      if (next)
+	mu_imapio_send_qstring (io, cmd);
+      else
+	mu_imapio_send (io, cmd, strlen (cmd));
+      cmd = next;
     }
-
-  return mu_stream_write (io->_imap_stream, buffer, len, NULL);
+  va_end (ap);
+  mu_imapio_send (io, "\r\n", 2);
+  return mu_stream_last_error (io->_imap_stream);
 }
diff --git a/libmailutils/imapio/litstring.c b/libmailutils/imapio/sendcmdv.c
similarity index 54%
copy from libmailutils/imapio/litstring.c
copy to libmailutils/imapio/sendcmdv.c
index 5ccd5fa..af9fa8d 100644
--- a/libmailutils/imapio/litstring.c
+++ b/libmailutils/imapio/sendcmdv.c
@@ -15,29 +15,32 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdlib.h>
 #include <string.h>
-#include <mailutils/types.h>
-#include <mailutils/errno.h>
 #include <mailutils/imapio.h>
-#include <mailutils/sys/imapio.h>
 #include <mailutils/stream.h>
+#include <mailutils/sys/imapio.h>
 
+/* Send a IMAP command to the server, quoting its arguments as necessary.
+   TAG is the command tag, ARGV contains ARGC elements and supplies the
+   command (in ARGV[0]) and its arguments. EXTRA (if not NULL) supplies
+   additional arguments that will be sent as is. */
 int
-mu_imapio_send_literal_string (struct _mu_imapio *io, const char *buffer)
+mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
+			  int argc, char const **argv, const char *extra)
 {
-  size_t len = strlen (buffer);
-
-  mu_stream_printf (io->_imap_stream, "{%lu}\r\n", (unsigned long) len);
-
-  if (!io->_imap_server)
+  int i;
+  
+  mu_imapio_printf (io, "%s %s", tag, argv[0]);
+  for (i = 1; i < argc; i++)
     {
-      int rc = mu_imapio_getline (io);
-      if (rc)
-	return rc;
-      if (!(io->_imap_reply_ready && io->_imap_ws.ws_wordv[0][0] == '+'))
-	return MU_ERR_BADREPLY;
+      mu_imapio_send (io, " ", 1);
+      mu_imapio_send_qstring (io, argv[i]);
     }
-
-  return mu_stream_write (io->_imap_stream, buffer, len, NULL);
+  if (extra)
+    {
+      mu_imapio_send (io, " ", 1);
+      mu_imapio_send (io, extra, strlen (extra));
+    }
+  mu_imapio_send (io, "\r\n", 2);
+  return mu_stream_last_error (io->_imap_stream);
 }
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index dfed78c..d422419 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -33,6 +33,7 @@ libmu_imap_la_SOURCES = \
  fake-folder.c\
  fetch.c\
  gencom.c\
+ genlist.c\
  callback.c\
  capability.c\
  capatst.c\
@@ -51,6 +52,7 @@ libmu_imap_la_SOURCES = \
  list.c\
  login.c\
  logout.c\
+ lsub.c\
  mbcreate.c\
  noop.c\
  rename.c\
@@ -61,7 +63,9 @@ libmu_imap_la_SOURCES = \
  state.c\
  status.c\
  store.c\
+ subscribe.c\
  tag.c\
  trace.c\
- unselect.c
+ unselect.c\
+ unsubscribe.c
 
diff --git a/libproto/imap/appstrsiz.c b/libproto/imap/appstrsiz.c
index d42b616..37b7a4e 100644
--- a/libproto/imap/appstrsiz.c
+++ b/libproto/imap/appstrsiz.c
@@ -80,8 +80,9 @@ mu_imap_append_stream_size (mu_imap_t imap, const char *mailbox, int flags,
       
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s APPEND %s",
-				 imap->tag_str, mailbox);
+      status = mu_imapio_printf (imap->io, "%s APPEND ", imap->tag_str);
+      MU_IMAP_CHECK_ERROR (imap, status);
+      status = mu_imapio_send_qstring (imap->io, mailbox);
       MU_IMAP_CHECK_ERROR (imap, status);
       if (flags)
 	{
diff --git a/libproto/imap/capability.c b/libproto/imap/capability.c
index e4bb4b7..f85321d 100644
--- a/libproto/imap/capability.c
+++ b/libproto/imap/capability.c
@@ -109,8 +109,8 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
     case MU_IMAP_CLIENT_READY:
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s CAPABILITY\r\n",
-				 imap->tag_str); 
+      status = mu_imapio_send_command (imap->io, imap->tag_str,
+				       "CAPABILITY", NULL); 
       MU_IMAP_CHECK_EAGAIN (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
       imap->client_state = MU_IMAP_CLIENT_CAPABILITY_RX;
diff --git a/libproto/imap/check.c b/libproto/imap/check.c
index 0346906..d5db25b 100644
--- a/libproto/imap/check.c
+++ b/libproto/imap/check.c
@@ -30,7 +30,6 @@ mu_imap_check (mu_imap_t imap)
     MU_IMAP_SESSION_SELECTED,
     NULL,
     MU_IMAP_CLIENT_CHECK_RX,
-    0,
     1,
     &command,
     NULL
diff --git a/libproto/imap/close.c b/libproto/imap/close.c
index a92d598..50bf1c5 100644
--- a/libproto/imap/close.c
+++ b/libproto/imap/close.c
@@ -37,9 +37,9 @@ mu_imap_close (mu_imap_t imap)
     MU_IMAP_SESSION_SELECTED,
     NULL,
     MU_IMAP_CLIENT_CLOSE_RX,
-    0,
     1,
     &command,
+    NULL,
     _mu_close_handler
   };
   return mu_imap_gencom (imap, &com);
diff --git a/libproto/imap/copy.c b/libproto/imap/copy.c
index 507b701..5598c20 100644
--- a/libproto/imap/copy.c
+++ b/libproto/imap/copy.c
@@ -26,19 +26,23 @@
 int
 mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
 {
-  char const *argv[3];
+  char const *argv[4];
+  int i;
   static struct imap_command com;
 
-  argv[0] = "COPY";
-  argv[1] = msgset;
-  argv[2] = mailbox;
+  i = 0;
+  if (uid)
+    argv[i++] = "UID";
+  argv[i++] = "COPY";
+  argv[i++] = msgset;
+  argv[i++] = mailbox;
 
   com.session_state = MU_IMAP_SESSION_SELECTED;
   com.capa = NULL;
   com.rx_state = MU_IMAP_CLIENT_COPY_RX;
-  com.uid = 0;
-  com.argc = 3;
+  com.argc = i;
   com.argv = argv;
+  com.extra = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/delete.c b/libproto/imap/delete.c
index 6f986e9..0818c4b 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/delete.c
@@ -37,9 +37,9 @@ mu_imap_delete (mu_imap_t imap, const char *mailbox)
   com.session_state = MU_IMAP_SESSION_AUTH;
   com.capa = NULL;
   com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
   com.argc = 2;
   com.argv = argv;
+  com.extra = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/expunge.c b/libproto/imap/expunge.c
index 376ee4b..1643c01 100644
--- a/libproto/imap/expunge.c
+++ b/libproto/imap/expunge.c
@@ -30,7 +30,6 @@ mu_imap_expunge (mu_imap_t imap)
     MU_IMAP_SESSION_SELECTED,
     NULL,
     MU_IMAP_CLIENT_EXPUNGE_RX,
-    0,
     1,
     &command,
     NULL
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index cecf794..f32ed3a 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -32,18 +32,21 @@ int
 mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
 {
   char const *argv[3];
+  int i;
   static struct imap_command com;
 
-  argv[0] = "FETCH";
-  argv[1] = msgset;
-  argv[2] = items;
+  i = 0;
+  if (uid)
+    argv[i++] = "UID";
+  argv[i++] = "FETCH";
+  argv[i++] = msgset;
   
   com.session_state = MU_IMAP_SESSION_SELECTED;
   com.capa = NULL;
   com.rx_state = MU_IMAP_CLIENT_FETCH_RX;
-  com.uid = uid;
-  com.argc = 3;
+  com.argc = i;
   com.argv = argv;
+  com.extra = items;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/gencom.c b/libproto/imap/gencom.c
index ce4b350..57e8643 100644
--- a/libproto/imap/gencom.c
+++ b/libproto/imap/gencom.c
@@ -28,7 +28,6 @@ int
 mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
 {
   int status;
-  int i;
   
   if (imap == NULL || !cmd || cmd->argc < 1)
     return EINVAL;
@@ -57,16 +56,8 @@ mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
     {
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s", imap->tag_str);
-      if (status == 0 && cmd->uid)
-	status = mu_imapio_printf (imap->io, " UID");
-      MU_IMAP_CHECK_ERROR (imap, status);
-      for (i = 0; i < cmd->argc; i++)
-	{
-	  status = mu_imapio_printf (imap->io, " %s", cmd->argv[i]);
-	  MU_IMAP_CHECK_ERROR (imap, status);
-	}
-      status = mu_imapio_send (imap->io, "\r\n", 2);
+      status = mu_imapio_send_command_v (imap->io, imap->tag_str,
+					 cmd->argc, cmd->argv, cmd->extra);
       MU_IMAP_CHECK_ERROR (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
       imap->client_state = cmd->rx_state;
diff --git a/libproto/imap/list.c b/libproto/imap/genlist.c
similarity index 87%
copy from libproto/imap/list.c
copy to libproto/imap/genlist.c
index 9fb9696..5931963 100644
--- a/libproto/imap/list.c
+++ b/libproto/imap/genlist.c
@@ -33,6 +33,7 @@ struct list_closure
 {
   int error_code;
   mu_list_t retlist;
+  const char *command;
 };
 
 static int
@@ -74,7 +75,7 @@ list_untagged_handler (mu_imap_t imap, mu_list_t resp, void *data)
 
   mu_list_count (resp, &count);
   if (count == 4 &&
-      _mu_imap_list_nth_element_is_string (resp, 0, "LIST"))
+      _mu_imap_list_nth_element_is_string (resp, 0, clos->command))
     {
       struct mu_list_response *rp;
 
@@ -121,27 +122,29 @@ list_untagged_handler (mu_imap_t imap, mu_list_t resp, void *data)
 }
   
 int
-mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
-	      mu_list_t retlist)
+mu_imap_genlist (mu_imap_t imap, int lsub,
+		 const char *refname, const char *mboxname,
+		 mu_list_t retlist)
 {
   char const *argv[3];
   static struct imap_command com;
   struct list_closure clos;
   int rc;
 
-  argv[0] = "LIST";
+  argv[0] = lsub ? "LSUB" : "LIST";
   argv[1] = refname;
   argv[2] = mboxname;
 
   clos.error_code = 0;
   clos.retlist = retlist;
+  clos.command = argv[0];
   
   com.session_state = MU_IMAP_SESSION_AUTH;
   com.capa = NULL;
-  com.rx_state = MU_IMAP_CLIENT_LIST_RX;
-  com.uid = 0;
+  com.rx_state = lsub ? MU_IMAP_CLIENT_LSUB_RX : MU_IMAP_CLIENT_LIST_RX;
   com.argc = 3;
   com.argv = argv;
+  com.extra = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = list_untagged_handler;
   com.untagged_handler_data = &clos;
@@ -154,15 +157,16 @@ mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
 }
 
 int
-mu_imap_list_new (mu_imap_t imap, const char *refname, const char *mboxname,
-		  mu_list_t *plist)
+mu_imap_genlist_new (mu_imap_t imap, int lsub,
+		     const char *refname, const char *mboxname,
+		     mu_list_t *plist)
 {
   mu_list_t list;
   int rc = mu_list_create (&list);
   if (rc == 0)
     {
       mu_list_set_destroy_item (list, mu_list_response_free);
-      rc = mu_imap_list (imap, refname, mboxname, list);
+      rc = mu_imap_genlist (imap, lsub, refname, mboxname, list);
       if (rc)
 	mu_list_destroy (&list);
       else
diff --git a/libproto/imap/list.c b/libproto/imap/list.c
index 9fb9696..3fd7f59 100644
--- a/libproto/imap/list.c
+++ b/libproto/imap/list.c
@@ -19,154 +19,20 @@
 # include <config.h>
 #endif
 
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/errno.h>
-#include <mailutils/address.h>
-#include <mailutils/cstr.h>
-#include <mailutils/cctype.h>
-#include <mailutils/list.h>
 #include <mailutils/imap.h>
-#include <mailutils/sys/imap.h>
 
-struct list_closure
-{
-  int error_code;
-  mu_list_t retlist;
-};
-
-static int
-count_level (const char *name, int delim)
-{
-  int level = 0;
-
-  while (*name)
-    if (*name++ == delim)
-      level++;
-  return level;
-}
-
-static int
-list_attr_conv (void *item, void *data)
-{
-  struct imap_list_element *elt = item;
-  struct mu_list_response *rp = data;
-  
-  if (elt->type != imap_eltype_string)
-    return 0;
-  if (mu_c_strcasecmp (elt->v.string, "\\Noinferiors"))
-    rp->type |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
-  if (mu_c_strcasecmp (elt->v.string, "\\Noselect"))
-    rp->type |= MU_FOLDER_ATTRIBUTE_FILE;
-  /* FIXME: \Marked nad \Unmarked have no correspondence in flags. */
-  return 0;
-}
-  
-static void
-list_untagged_handler (mu_imap_t imap, mu_list_t resp, void *data)
-{
-  struct list_closure *clos = data;
-  struct imap_list_element *elt;
-  size_t count;
-  
-  if (clos->error_code)
-    return;
-
-  mu_list_count (resp, &count);
-  if (count == 4 &&
-      _mu_imap_list_nth_element_is_string (resp, 0, "LIST"))
-    {
-      struct mu_list_response *rp;
-
-      rp = calloc (1, sizeof (*rp));
-      if (!rp)
-	{
-	  clos->error_code = ENOMEM;
-	  return;
-	}
-	  
-      elt = _mu_imap_list_at (resp, 1);
-      if (!(elt && elt->type == imap_eltype_list))
-	return;
-      rp->type = 0;
-      mu_list_foreach (elt->v.list, list_attr_conv, rp);
-
-      elt = _mu_imap_list_at (resp, 3);
-      if (!(elt && elt->type == imap_eltype_string))
-	return;
-      rp->name = strdup (elt->v.string);
-      if (!rp->name)
-	{
-	  free (rp);
-	  clos->error_code = ENOMEM;
-	  return;
-	}
-
-      elt = _mu_imap_list_at (resp, 2);
-      if (!(elt && elt->type == imap_eltype_string))
-	return;
-      if (mu_c_strcasecmp (elt->v.string, "NIL") == 0)
-	{
-	  rp->separator = 0;
-	  rp->level = 0;
-	}
-      else
-	{
-	  rp->separator = elt->v.string[0];
-	  rp->level = count_level (rp->name, rp->separator);
-	}
-      if ((clos->error_code = mu_list_append (clos->retlist, rp)))
-	mu_list_response_free (rp);
-    }
-}
-  
 int
 mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
 	      mu_list_t retlist)
 {
-  char const *argv[3];
-  static struct imap_command com;
-  struct list_closure clos;
-  int rc;
-
-  argv[0] = "LIST";
-  argv[1] = refname;
-  argv[2] = mboxname;
-
-  clos.error_code = 0;
-  clos.retlist = retlist;
-  
-  com.session_state = MU_IMAP_SESSION_AUTH;
-  com.capa = NULL;
-  com.rx_state = MU_IMAP_CLIENT_LIST_RX;
-  com.uid = 0;
-  com.argc = 3;
-  com.argv = argv;
-  com.tagged_handler = NULL;
-  com.untagged_handler = list_untagged_handler;
-  com.untagged_handler_data = &clos;
-
-  rc = mu_imap_gencom (imap, &com);
-  if (rc == 0)
-    rc = clos.error_code;
-
-  return rc;
+  return mu_imap_genlist (imap, 0, refname, mboxname, retlist);
 }
 
 int
 mu_imap_list_new (mu_imap_t imap, const char *refname, const char *mboxname,
 		  mu_list_t *plist)
 {
-  mu_list_t list;
-  int rc = mu_list_create (&list);
-  if (rc == 0)
-    {
-      mu_list_set_destroy_item (list, mu_list_response_free);
-      rc = mu_imap_list (imap, refname, mboxname, list);
-      if (rc)
-	mu_list_destroy (&list);
-      else
-	*plist = list;
-    }
-  return rc;
+  return mu_imap_genlist_new (imap, 0, refname, mboxname, plist);
 }
+
+
diff --git a/libproto/imap/login.c b/libproto/imap/login.c
index 2a8f363..807d7b4 100644
--- a/libproto/imap/login.c
+++ b/libproto/imap/login.c
@@ -43,8 +43,8 @@ mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
 	_mu_imap_xscript_level (imap, MU_XSCRIPT_SECURE);
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s LOGIN \"%s\" \"%s\"\r\n",
-				 imap->tag_str, user, pass);
+      status = mu_imapio_send_command (imap->io, imap->tag_str,
+				       "LOGIN", user, pass, NULL);
       _mu_imap_xscript_level (imap, MU_XSCRIPT_NORMAL);
       /* FIXME: how to obscure the passwd in the stream buffer? */
       MU_IMAP_CHECK_EAGAIN (imap, status);
diff --git a/libproto/imap/check.c b/libproto/imap/lsub.c
similarity index 72%
copy from libproto/imap/check.c
copy to libproto/imap/lsub.c
index 0346906..106f70a 100644
--- a/libproto/imap/check.c
+++ b/libproto/imap/lsub.c
@@ -20,21 +20,19 @@
 #endif
 
 #include <mailutils/imap.h>
-#include <mailutils/sys/imap.h>
 
 int
-mu_imap_check (mu_imap_t imap)
+mu_imap_lsub (mu_imap_t imap, const char *refname, const char *mboxname,
+	      mu_list_t retlist)
 {
-  static char const *command = "CHECK";
-  static struct imap_command com = {
-    MU_IMAP_SESSION_SELECTED,
-    NULL,
-    MU_IMAP_CLIENT_CHECK_RX,
-    0,
-    1,
-    &command,
-    NULL
-  };
-  return mu_imap_gencom (imap, &com);
+  return mu_imap_genlist (imap, 1, refname, mboxname, retlist);
 }
-      
+
+int
+mu_imap_lsub_new (mu_imap_t imap, const char *refname, const char *mboxname,
+		  mu_list_t *plist)
+{
+  return mu_imap_genlist_new (imap, 1, refname, mboxname, plist);
+}
+
+
diff --git a/libproto/imap/mbcreate.c b/libproto/imap/mbcreate.c
index b7b9063..a9b7edd 100644
--- a/libproto/imap/mbcreate.c
+++ b/libproto/imap/mbcreate.c
@@ -37,9 +37,9 @@ mu_imap_mailbox_create (mu_imap_t imap, const char *mailbox)
   com.session_state = MU_IMAP_SESSION_AUTH;
   com.capa = NULL;
   com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
   com.argc = 2;
   com.argv = argv;
+  com.extra = 0;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/noop.c b/libproto/imap/noop.c
index 62d5035..b5676f8 100644
--- a/libproto/imap/noop.c
+++ b/libproto/imap/noop.c
@@ -30,7 +30,6 @@ mu_imap_noop (mu_imap_t imap)
     MU_IMAP_SESSION_INIT,
     NULL,
     MU_IMAP_CLIENT_NOOP_RX,
-    0,
     1,
     &command,
     NULL
diff --git a/libproto/imap/rename.c b/libproto/imap/rename.c
index 44d0773..334df7d 100644
--- a/libproto/imap/rename.c
+++ b/libproto/imap/rename.c
@@ -36,9 +36,9 @@ mu_imap_rename (mu_imap_t imap, const char *mailbox, const char *new_mailbox)
   com.session_state = MU_IMAP_SESSION_AUTH;
   com.capa = NULL;
   com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
   com.argc = 3;
   com.argv = argv;
+  com.extra = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/select.c b/libproto/imap/select.c
index be19668..05dd550 100644
--- a/libproto/imap/select.c
+++ b/libproto/imap/select.c
@@ -101,10 +101,9 @@ mu_imap_select (mu_imap_t imap, const char *mbox, int writable,
     case MU_IMAP_CLIENT_READY:
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_printf (imap->io, "%s %s %s\r\n",
-				 imap->tag_str,
-				 writable ? "SELECT" : "EXAMINE",
-				 mbox);
+      status = mu_imapio_send_command (imap->io, imap->tag_str,
+				       writable ? "SELECT" : "EXAMINE",
+				       mbox, NULL);
       MU_IMAP_CHECK_ERROR (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
       imap->client_state = MU_IMAP_CLIENT_SELECT_RX;
diff --git a/libproto/imap/store.c b/libproto/imap/store.c
index 8faecac..be1349f 100644
--- a/libproto/imap/store.c
+++ b/libproto/imap/store.c
@@ -29,18 +29,21 @@ int
 mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
 {
   char const *argv[3];
+  int i;
   static struct imap_command com;
 
-  argv[0] = "STORE";
-  argv[1] = msgset;
-  argv[2] = items;
+  i = 0;
+  if (uid)
+    argv[i++] = "UID";
+  argv[i++] = "STORE";
+  argv[i++] = msgset;
   
   com.session_state = MU_IMAP_SESSION_SELECTED;
   com.capa = NULL;
   com.rx_state = MU_IMAP_CLIENT_STORE_RX;
-  com.uid = uid;
-  com.argc = 3;
+  com.argc = i;
   com.argv = argv;
+  com.extra = items;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/delete.c b/libproto/imap/subscribe.c
similarity index 85%
copy from libproto/imap/delete.c
copy to libproto/imap/subscribe.c
index 6f986e9..3cf71a6 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/subscribe.c
@@ -19,27 +19,24 @@
 # include <config.h>
 #endif
 
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_delete (mu_imap_t imap, const char *mailbox)
+mu_imap_subscribe (mu_imap_t imap, const char *mailbox)
 {
   char const *argv[2];
   static struct imap_command com;
 
-  argv[0] = "DELETE";
+  argv[0] = "SUBSCRIBE";
   argv[1] = mailbox;
 
   com.session_state = MU_IMAP_SESSION_AUTH;
   com.capa = NULL;
-  com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
+  com.rx_state = MU_IMAP_CLIENT_SUBSCRIBE_RX;
   com.argc = 2;
   com.argv = argv;
+  com.extra = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/unselect.c b/libproto/imap/unselect.c
index f45c4aa..c1ac499 100644
--- a/libproto/imap/unselect.c
+++ b/libproto/imap/unselect.c
@@ -30,9 +30,9 @@ mu_imap_unselect (mu_imap_t imap)
     MU_IMAP_SESSION_SELECTED,
     NULL,
     MU_IMAP_CLIENT_UNSELECT_RX,
-    0,
     1,
     &command,
+    NULL,
     _mu_close_handler
   };
   return mu_imap_gencom (imap, &com);
diff --git a/libproto/imap/delete.c b/libproto/imap/unsubscribe.c
similarity index 85%
copy from libproto/imap/delete.c
copy to libproto/imap/unsubscribe.c
index 6f986e9..4831829 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/unsubscribe.c
@@ -19,27 +19,24 @@
 # include <config.h>
 #endif
 
-#include <stdlib.h>
-#include <string.h>
-#include <mailutils/errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_delete (mu_imap_t imap, const char *mailbox)
+mu_imap_unsubscribe (mu_imap_t imap, const char *mailbox)
 {
   char const *argv[2];
   static struct imap_command com;
 
-  argv[0] = "DELETE";
+  argv[0] = "UNSUBSCRIBE";
   argv[1] = mailbox;
 
   com.session_state = MU_IMAP_SESSION_AUTH;
   com.capa = NULL;
-  com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
-  com.uid = 0;
+  com.rx_state = MU_IMAP_CLIENT_UNSUBSCRIBE_RX;
   com.argc = 2;
   com.argv = argv;
+  com.extra = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/mu/imap.c b/mu/imap.c
index 699e270..ffa7d68 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -818,6 +818,24 @@ com_create (int argc, char **argv)
 }
 
 static int
+com_subscribe (int argc, char **argv)
+{
+  int status = mu_imap_subscribe (imap, argv[1]);
+  if (status)
+    report_failure ("subscribe", status);
+  return 0;
+}
+
+static int
+com_unsubscribe (int argc, char **argv)
+{
+  int status = mu_imap_unsubscribe (imap, argv[1]);
+  if (status)
+    report_failure ("unsubscribe", status);
+  return 0;
+}
+
+static int
 com_append (int argc, char **argv)
 {
   struct tm tmbuf, *tm = NULL;
@@ -931,6 +949,25 @@ com_list (int argc, char **argv)
   return 0;
 }
 
+static int
+com_lsub (int argc, char **argv)
+{
+  mu_list_t list;
+  int rc;
+  mu_stream_t out;
+  
+  rc = mu_imap_lsub_new (imap, argv[1], argv[2], &list);
+  if (rc)
+    {
+      report_failure ("lsub", rc);
+      return 0;
+    }
+
+  out = mutool_open_pager ();
+  mu_list_foreach (list, print_list_item, out);
+  mu_stream_unref (out);
+  return 0;
+}
 
 struct mutool_command imap_comtab[] = {
   { "capability",   1, -1, 0,
@@ -1023,6 +1060,18 @@ struct mutool_command imap_comtab[] = {
     com_list,
     N_("REF MBOX"),
     N_("List matching mailboxes") },
+  { "lsub",         3, 3, 0,
+    com_lsub,
+    N_("REF MBOX"),
+    N_("List subscribed mailboxes") },
+  { "subscribe",    2, 2, 0,
+    com_subscribe,
+    N_("MBOX"),
+    N_("Subscribe to a mailbox") },
+  { "unsubscribe",    2, 2, 0,
+    com_unsubscribe,
+    N_("MBOX"),
+    N_("Remove mailbox from subscription list") },
   { "quit",         1, 1, 0,
     com_logout,
     NULL,


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Dec 14 11:23:10 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rarbq-0007tC-BW
	for mharc-commit-mailutils@gnu.org; Wed, 14 Dec 2011 11:23:10 -0500
Received: from eggs.gnu.org ([140.186.70.92]:60151)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rarbn-0007si-Cr
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 11:23:09 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rarbh-0004Bc-Us
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 11:23:07 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:33334)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rarbh-0004BR-Sz
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 11:23:01 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rarbg-00024S-12
	for commit-mailutils@gnu.org; Wed, 14 Dec 2011 16:23:01 +0000
Date: Wed, 14 Dec 2011 16:23:00 +0000
Message-Id: <E1Rarbg-00024S-12@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-530-g7d68b3c
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: f675ddc76948a39c4b6dd4a47460a63e731e927a
X-Git-Newrev: 7d68b3cd17901190e6030327230df8e124161643
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 14 Dec 2011 16:23:09 -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=7d68b3cd17901190e6030327230df8e124161643

The branch, master has been updated
       via  7d68b3cd17901190e6030327230df8e124161643 (commit)
      from  f675ddc76948a39c4b6dd4a47460a63e731e927a (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 7d68b3cd17901190e6030327230df8e124161643
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 14 18:18:19 2011 +0200

    Imap client: implement starttls.
    
    * include/mailutils/imap.h (mu_imap_starttls): New proto.
    * include/mailutils/imapio.h: Use mu_imapio_t instead of struct _mu_imapio *.
    * include/mailutils/sys/imap.h (MU_IMAP_CLIENT_STARTTLS_RX): New state.
    * libmailutils/imapio/transport.c: New file.
    * libmailutils/imapio/Makefile.am (libimapio_la_SOURCES): Add transport.c
    * libproto/imap/starttls.c: New file.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Add starttls.c.
    * libproto/imap/capability.c (mu_imap_capability): Bugfix: set up
    comparator and reclaim function after successful return from mu_list_map.
    * libproto/imap/capatst.c (mu_imap_capability_test): Clear MU_IMAP_RESP
    bit.
    * libproto/imap/err.c (mu_imap_strerror): Return errstr only if
    MU_IMAP_RESP is set.
    * mu/imap.c: Implement starttls.

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

Summary of changes:
 include/mailutils/imap.h                       |    2 +
 include/mailutils/imapio.h                     |   27 +++++++-----
 include/mailutils/sys/imap.h                   |    1 +
 libmailutils/imapio/Makefile.am                |    1 +
 libmailutils/imapio/{xscript.c => transport.c} |   54 ++++++++++++++++++------
 libproto/imap/Makefile.am                      |    1 +
 libproto/imap/capability.c                     |   12 ++----
 libproto/imap/capatst.c                        |    1 +
 libproto/imap/err.c                            |    5 +-
 libproto/imap/{login.c => starttls.c}          |   54 +++++++++++++++++-------
 mu/imap.c                                      |   13 ++++++
 11 files changed, 121 insertions(+), 50 deletions(-)
 copy libmailutils/imapio/{xscript.c => transport.c} (52%)
 copy libproto/imap/{login.c => starttls.c} (58%)

diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 93d9ea3..88a7207 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -51,6 +51,8 @@ int mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter);
 int mu_imap_capability_test (mu_imap_t imap, const char *name,
 			     const char **pret);
 
+int mu_imap_starttls (mu_imap_t imap);
+  
 int mu_imap_login (mu_imap_t imap, const char *user, const char *pass);
 int mu_imap_logout (mu_imap_t imap);
 
diff --git a/include/mailutils/imapio.h b/include/mailutils/imapio.h
index f637fb9..87f55ce 100644
--- a/include/mailutils/imapio.h
+++ b/include/mailutils/imapio.h
@@ -38,22 +38,22 @@ int mu_imapio_get_words (mu_imapio_t io, size_t *pwc, char ***pwv);
 
 int mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes);
 int mu_imapio_printf (mu_imapio_t io, const char *fmt, ...);
-int mu_imapio_send_literal_string (struct _mu_imapio *io, const char *buffer);
-int mu_imapio_send_literal_stream (struct _mu_imapio *io, mu_stream_t stream,
+int mu_imapio_send_literal_string (mu_imapio_t io, const char *buffer);
+int mu_imapio_send_literal_stream (mu_imapio_t io, mu_stream_t stream,
 				   mu_off_t size);  
-int mu_imapio_send_qstring (struct _mu_imapio *io, const char *buffer);
-int mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
+int mu_imapio_send_qstring (mu_imapio_t io, const char *buffer);
+int mu_imapio_send_qstring_unfold (mu_imapio_t io, const char *buffer,
 				   int unfold);
 
-int mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
+int mu_imapio_send_command_v (mu_imapio_t io, const char *tag,
 			      int argc, char const **argv, const char *extra);
-int mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
+int mu_imapio_send_command (mu_imapio_t io, const char *tag,
 			char const *cmd, ...);
-int mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
+int mu_imapio_send_command_e (mu_imapio_t io, const char *tag,
 			      char const *cmd, ...);
   
-int mu_imapio_send_flags (struct _mu_imapio *io, int flags);
-int mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
+int mu_imapio_send_flags (mu_imapio_t io, int flags);
+int mu_imapio_send_time (mu_imapio_t io, struct tm *tm,
 			 struct mu_timezone *tz);
   
 int mu_imapio_trace_enable (mu_imapio_t io);
@@ -62,12 +62,15 @@ int mu_imapio_get_trace (mu_imapio_t io);
 void mu_imapio_trace_payload (mu_imapio_t io, int val);
 int mu_imapio_get_trace_payload (mu_imapio_t io);
 
+int mu_imapio_get_streams (mu_imapio_t io, mu_stream_t *streams);
+int mu_imapio_set_streams (mu_imapio_t io, mu_stream_t *streams);
+  
 int mu_imapio_getbuf (mu_imapio_t io, char **pptr, size_t *psize);
 
-int mu_imapio_reply_string (struct _mu_imapio *io, size_t start, char **pbuf);
+int mu_imapio_reply_string (mu_imapio_t io, size_t start, char **pbuf);
 
-int mu_imapio_last_error (struct _mu_imapio *io);
-void mu_imapio_clearerr (struct _mu_imapio *io);
+int mu_imapio_last_error (mu_imapio_t io);
+void mu_imapio_clearerr (mu_imapio_t io);
   
 int mu_imap_flag_to_attribute (const char *item, int *attr);
 int mu_imap_format_flags (mu_stream_t str, int flags);
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index 40c23a5..0063a27 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -70,6 +70,7 @@ enum mu_imap_client_state
     MU_IMAP_CLIENT_SUBSCRIBE_RX,
     MU_IMAP_CLIENT_UNSUBSCRIBE_RX,
     MU_IMAP_CLIENT_LSUB_RX,
+    MU_IMAP_CLIENT_STARTTLS_RX,
     MU_IMAP_CLIENT_CLOSING
   };
 
diff --git a/libmailutils/imapio/Makefile.am b/libmailutils/imapio/Makefile.am
index f0736b4..d196b50 100644
--- a/libmailutils/imapio/Makefile.am
+++ b/libmailutils/imapio/Makefile.am
@@ -33,6 +33,7 @@ libimapio_la_SOURCES = \
  sendcmde.c\
  time.c\
  trace.c\
+ transport.c\
  words.c\
  xscript.c
 
diff --git a/libmailutils/imapio/xscript.c b/libmailutils/imapio/transport.c
similarity index 52%
copy from libmailutils/imapio/xscript.c
copy to libmailutils/imapio/transport.c
index ebb003b..f958509 100644
--- a/libmailutils/imapio/xscript.c
+++ b/libmailutils/imapio/transport.c
@@ -24,23 +24,51 @@
 #include <mailutils/sys/imapio.h>
 
 int
-mu_imapio_set_xscript_level (struct _mu_imapio *io, int xlev)
+mu_imapio_get_streams (struct _mu_imapio *io, mu_stream_t *streams)
 {
-  if (!io)
-    return EINVAL;
+  int rc;
+
+  mu_stream_flush (io->_imap_stream);
+  if (io->_imap_transcript)
+    rc = mu_stream_ioctl (io->_imap_stream, MU_IOCTL_SUBSTREAM, 
+			  MU_IOCTL_OP_GET, streams);
+  else
+    {
+      streams[0] = io->_imap_stream;
+      mu_stream_ref (streams[0]);
+      streams[1] = io->_imap_stream;
+      mu_stream_ref (streams[1]);
+      rc = 0;
+    }
+  return rc;
+}
+      
+int
+mu_imapio_set_streams (struct _mu_imapio *io, mu_stream_t *streams)
+{
+  int rc;
+  
   if (io->_imap_transcript)
+    rc = mu_stream_ioctl (io->_imap_stream, MU_IOCTL_SUBSTREAM, 
+                          MU_IOCTL_OP_SET, streams);
+  else
     {
-      if (xlev != MU_XSCRIPT_NORMAL)
+      mu_stream_t tmp;
+      
+      if (streams[0] == streams[1])
 	{
-	  if (mu_debug_level_p (MU_DEBCAT_REMOTE, 
-	                        xlev == MU_XSCRIPT_SECURE ?
-				  MU_DEBUG_TRACE6 : MU_DEBUG_TRACE7))
-	    return MU_XSCRIPT_NORMAL;
+	  tmp = streams[0];
+	  mu_stream_ref (tmp);
+	  mu_stream_ref (tmp);
+	  rc = 0;
+	}
+      else
+	rc = mu_iostream_create (&tmp, streams[0], streams[1]);
+      if (rc == 0)
+	{
+	  mu_stream_unref (io->_imap_stream);
+	  io->_imap_stream = tmp;
 	}
-
-      if (mu_stream_ioctl (io->_imap_stream, MU_IOCTL_XSCRIPTSTREAM,
-                           MU_IOCTL_XSCRIPTSTREAM_LEVEL, &xlev) == 0)
-	return xlev;
     }
-  return MU_XSCRIPT_NORMAL;
+  return rc;
 }
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index d422419..82c7c87 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -62,6 +62,7 @@ libmu_imap_la_SOURCES = \
  select.c\
  state.c\
  status.c\
+ starttls.c\
  store.c\
  subscribe.c\
  tag.c\
diff --git a/libproto/imap/capability.c b/libproto/imap/capability.c
index f85321d..bc3c8a3 100644
--- a/libproto/imap/capability.c
+++ b/libproto/imap/capability.c
@@ -94,14 +94,7 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
 	    return 0;
 	  return mu_list_get_iterator (imap->capa, piter);
 	}
-      mu_list_clear (imap->capa);
-    }
-  else
-    {
-      status = mu_list_create (&imap->capa);
-      MU_IMAP_CHECK_ERROR (imap, status);
-      mu_list_set_comparator (imap->capa, capa_comp);
-      mu_list_set_destroy_item (imap->capa, mu_list_free_item);
+      mu_list_destroy (&imap->capa);
     }
 
   switch (imap->client_state)
@@ -124,6 +117,9 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
 	return MU_ERR_REPLY;
       else
 	{
+	  mu_list_set_comparator (imap->capa, capa_comp);
+	  mu_list_set_destroy_item (imap->capa, mu_list_free_item);
+
 	  if (piter)
 	    status = mu_list_get_iterator (imap->capa, piter);
 	  else
diff --git a/libproto/imap/capatst.c b/libproto/imap/capatst.c
index 90db663..c8c18ff 100644
--- a/libproto/imap/capatst.c
+++ b/libproto/imap/capatst.c
@@ -31,5 +31,6 @@ mu_imap_capability_test (mu_imap_t imap, const char *name, const char **pret)
   rc = mu_imap_capability (imap, 0, NULL);
   if (rc)
     return rc;
+  MU_IMAP_FCLR (imap, MU_IMAP_RESP);
   return mu_list_locate (imap->capa, (void*) name, (void**)pret);
 }
diff --git a/libproto/imap/err.c b/libproto/imap/err.c
index 0145b7e..b3dc52a 100644
--- a/libproto/imap/err.c
+++ b/libproto/imap/err.c
@@ -62,11 +62,12 @@ mu_imap_strerror (mu_imap_t imap, const char **pstr)
       *pstr = "(imap not initialized)";
       return EINVAL;
     }
-  if (imap->errstr)
+
+  if (MU_IMAP_FISSET (imap, MU_IMAP_RESP))
     {
       *pstr = imap->errstr;
       return 0;
     }
-  *pstr = "(no error)";
+  *pstr = "(no recent reply)";
   return MU_ERR_NOENT;
 }
diff --git a/libproto/imap/login.c b/libproto/imap/starttls.c
similarity index 58%
copy from libproto/imap/login.c
copy to libproto/imap/starttls.c
index 807d7b4..6eb6674 100644
--- a/libproto/imap/login.c
+++ b/libproto/imap/starttls.c
@@ -18,62 +18,86 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <mailutils/errno.h>
 #include <mailutils/stream.h>
+#include <mailutils/tls.h>
+#include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
+mu_imap_starttls (mu_imap_t imap)
 {
+#ifdef WITH_TLS
   int status;
+  mu_stream_t tlsstream, streams[2];
   
   if (imap == NULL)
     return EINVAL;
   if (!imap->io)
     return MU_ERR_NO_TRANSPORT;
-  if (imap->session_state != MU_IMAP_SESSION_NONAUTH)
+  if (imap->session_state == MU_IMAP_SESSION_INIT)
     return MU_ERR_SEQ;
+
+  status = mu_imap_capability_test (imap, "STARTTLS", NULL);
+  if (status == MU_ERR_NOENT)
+    return ENOSYS;
+  else if (status)
+    return status;
   
   switch (imap->client_state)
     {
     case MU_IMAP_CLIENT_READY:
-      if (mu_imap_trace_mask (imap, MU_IMAP_TRACE_QRY, MU_XSCRIPT_SECURE))
-	_mu_imap_xscript_level (imap, MU_XSCRIPT_SECURE);
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_send_command (imap->io, imap->tag_str,
-				       "LOGIN", user, pass, NULL);
-      _mu_imap_xscript_level (imap, MU_XSCRIPT_NORMAL);
-      /* FIXME: how to obscure the passwd in the stream buffer? */
-      MU_IMAP_CHECK_EAGAIN (imap, status);
+      status = mu_imapio_printf (imap->io, "%s STARTTLS\r\n", imap->tag_str);
+      MU_IMAP_CHECK_ERROR (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
-      imap->client_state = MU_IMAP_CLIENT_LOGIN_RX;
-
-    case MU_IMAP_CLIENT_LOGIN_RX:
+      imap->client_state = MU_IMAP_CLIENT_STARTTLS_RX;
+      
+    case MU_IMAP_CLIENT_STARTTLS_RX:
       status = _mu_imap_response (imap, NULL, NULL);
-      imap->client_state = MU_IMAP_CLIENT_READY;
       MU_IMAP_CHECK_EAGAIN (imap, status);
       switch (imap->resp_code)
 	{
 	case MU_IMAP_OK:
-	  imap->session_state = MU_IMAP_SESSION_AUTH;
+	  status = mu_imapio_get_streams (imap->io, streams);
+	  MU_IMAP_CHECK_EAGAIN (imap, status);
+	  status = mu_tls_client_stream_create (&tlsstream,
+						streams[0], streams[1], 0);
+	  mu_stream_unref (streams[0]);
+	  mu_stream_unref (streams[1]);
+	  MU_IMAP_CHECK_EAGAIN (imap, status);
+	  streams[0] = streams[1] = tlsstream;
+	  status = mu_imapio_set_streams (imap->io, streams);
+	  mu_stream_unref (streams[0]);
+	  mu_stream_unref (streams[1]);
+	  MU_IMAP_CHECK_EAGAIN (imap, status);
+	  /* Invalidate the capability list */
+	  mu_list_destroy (&imap->capa);
+	  status = 0;
 	  break;
 
 	case MU_IMAP_NO:
-	  status = EACCES;
+	  status = MU_ERR_FAILURE;
 	  break;
 
 	case MU_IMAP_BAD:
 	  status = MU_ERR_BADREPLY;
 	  break;
 	}
+      
+      imap->client_state = MU_IMAP_CLIENT_READY;
       break;
 
     default:
       status = EINPROGRESS;
     }
   return status;
+#else
+  return ENOSYS;
+#endif
 }
 
diff --git a/mu/imap.c b/mu/imap.c
index ffa7d68..4c114f3 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -455,6 +455,15 @@ com_connect (int argc, char **argv)
 }
 
 static int
+com_starttls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
+{
+  int status = mu_imap_starttls (imap);
+  if (status)
+    report_failure ("starttls", status);
+  return 0;
+}
+
+static int
 com_logout (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
 {
   int status = 0;
@@ -988,6 +997,10 @@ struct mutool_command imap_comtab[] = {
     com_disconnect,
     NULL,
     N_("close connection") },
+  { "starttls",     1, 1, 0,
+    com_starttls,
+    NULL,
+    N_("Establish TLS encrypted channel") },
   { "login",        2, 3, 0,
     com_login,
     N_("USER [PASS]"),


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 15 03:17:34 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rb6VS-0000mv-RG
	for mharc-commit-mailutils@gnu.org; Thu, 15 Dec 2011 03:17:34 -0500
Received: from eggs.gnu.org ([140.186.70.92]:45041)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rb6VL-0000j7-Ts
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 03:17:32 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rb6VH-0004cj-Nj
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 03:17:27 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:54397)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rb6VH-0004cc-D9
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 03:17:23 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rb6VG-0001cQ-Ue
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 08:17:23 +0000
Date: Thu, 15 Dec 2011 08:17:23 +0000
Message-Id: <E1Rb6VG-0001cQ-Ue@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-531-g0e8ae1c
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 7d68b3cd17901190e6030327230df8e124161643
X-Git-Newrev: 0e8ae1c37611759936ce66eefff2ef392c3b8de0
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 15 Dec 2011 08:17:33 -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=0e8ae1c37611759936ce66eefff2ef392c3b8de0

The branch, master has been updated
       via  0e8ae1c37611759936ce66eefff2ef392c3b8de0 (commit)
      from  7d68b3cd17901190e6030327230df8e124161643 (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 0e8ae1c37611759936ce66eefff2ef392c3b8de0
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 14 23:15:58 2011 +0200

    Implement UID command in mu imap

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

Summary of changes:
 mu/imap.c |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/mu/imap.c b/mu/imap.c
index 4c114f3..f615de9 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -61,6 +61,7 @@ static struct argp imap_argp = {
 
 
 static mu_imap_t imap;
+static int uid_mode;
 
 static enum mu_imap_session_state
 current_imap_state ()
@@ -315,7 +316,20 @@ imap_fetch_callback (void *data, int code, size_t sdat, void *pdat)
   mu_list_foreach (list, fetch_response_printer, str);
   mu_stream_printf (str, "\n\n");
 }
+
+static int
+com_uid (int argc, char **argv)
+{
+  int bv;
   
+  if (argc == 1)
+    mu_printf ("%s\n", uid_mode ? _("UID is on") : _("UID is off"));
+  else if (get_bool (argv[1], &bv) == 0)
+    uid_mode = bv;
+  else
+    mu_error (_("invalid boolean value"));
+  return 0;
+}
 
 static int
 com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
@@ -346,10 +360,10 @@ com_connect (int argc, char **argv)
       if (strcmp (argv[i], "-tls") == 0)
 	{
 #ifdef WITH_TLS
-	    tls = 1;
+	  tls = 1;
 #else
-            mu_error (_("TLS not supported"));
-	    return 0;
+	  mu_error (_("TLS not supported"));
+	  return 0;
 #endif
 	}
       else
@@ -753,7 +767,7 @@ com_fetch (int argc, char **argv)
   mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
 				      imap_fetch_callback,
 				      out);
-  status = mu_imap_fetch (imap, 0, argv[1], argv[2]);
+  status = mu_imap_fetch (imap, uid_mode, argv[1], argv[2]);
   mu_stream_destroy (&out);
   mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
 				      imap_fetch_callback,
@@ -766,7 +780,7 @@ com_fetch (int argc, char **argv)
 static int
 com_store (int argc, char **argv)
 {
-  int status = mu_imap_store (imap, 0, argv[1], argv[2]);
+  int status = mu_imap_store (imap, uid_mode, argv[1], argv[2]);
   if (status)
     report_failure ("store", status);
   return 0;
@@ -1085,6 +1099,10 @@ struct mutool_command imap_comtab[] = {
     com_unsubscribe,
     N_("MBOX"),
     N_("Remove mailbox from subscription list") },
+  { "uid",          1, 2, 0,
+    com_uid,
+    N_("[on|off]"),
+    N_("control UID mode") },
   { "quit",         1, 1, 0,
     com_logout,
     NULL,


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 15 11:48:14 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RbETe-0003Jd-1O
	for mharc-commit-mailutils@gnu.org; Thu, 15 Dec 2011 11:48:14 -0500
Received: from eggs.gnu.org ([140.186.70.92]:59427)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbETP-0003I7-6b
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 11:48:12 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbETA-00023X-OM
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 11:47:59 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:43437)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbETA-00023T-Ir
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 11:47:44 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbET9-0004E9-Gq
	for commit-mailutils@gnu.org; Thu, 15 Dec 2011 16:47:44 +0000
Date: Thu, 15 Dec 2011 16:47:43 +0000
Message-Id: <E1RbET9-0004E9-Gq@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-532-gfa42589
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 0e8ae1c37611759936ce66eefff2ef392c3b8de0
X-Git-Newrev: fa42589cef090379bda4ea4e0838474520f9f4f6
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 15 Dec 2011 16:48:12 -0000

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

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

The branch, master has been updated
       via  fa42589cef090379bda4ea4e0838474520f9f4f6 (commit)
      from  0e8ae1c37611759936ce66eefff2ef392c3b8de0 (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 fa42589cef090379bda4ea4e0838474520f9f4f6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 15 10:20:07 2011 +0200

    imap client: implement folder API.
    
    * libmailutils/list/listlist.c (mu_list_append_list): Do nothing if the
    source list is empty.
    
    * include/mailutils/sys/imap.h (_mu_imap_url_init)
    (_mu_imaps_url_init): New protos.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Restore url.c
    * libproto/imap/mbox.c: Deleted
    * libproto/imap/url.c: Rewrite.
    * libproto/imap/folder.c: Rewrite from scratch.
    * configure.ac: Build libproto/imap/tests/Makefile
    * include/mailutils/imap.h (mu_imap_session_state)
    (mu_imap_iserror, mu_imap_clearerr)
    (mu_imap_login_secret): New protos.
    * include/mailutils/sys/imap.h (_mu_imap_list_element_is_nil): New proto.
    
    * libmailutils/mailbox/folder.c (mu_folder_list): Pass
    MU_FOLDER_ATTRIBUTE_ALL.
    * libproto/imap/fake-folder.c: Remove.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Remove fake-folder.c
    Add url.c and folder.c
    * libproto/imap/create.c (mu_imap_session_state)
    (mu_imap_iserror, mu_imap_clearerr): New functions.
    * libproto/imap/delete.c: Check input parameters.
    
    * libproto/imap/fetch.c: Use _mu_imap_list_element_is_nil to check for
    empty lists.
    * libproto/imap/genlist.c: Likewise.
    * libproto/imap/rename.c: Likewise.
    * libproto/imap/subscribe.c: Likewise.
    * libproto/imap/unsubscribe.c: Likewise.
    
    * libproto/imap/resplist.c: Treat NIL and () equally.
    * libproto/imap/login.c (mu_imap_login_secret): New function.
    
    * mu/imap.c: Fix a typo.

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

Summary of changes:
 configure.ac                              |    1 +
 include/mailutils/imap.h                  |    6 +
 include/mailutils/sys/imap.h              |   15 +-
 libmailutils/list/listlist.c              |    4 +-
 libmailutils/mailbox/folder.c             |    3 +-
 libproto/imap/Makefile.am                 |   13 +-
 libproto/imap/create.c                    |   27 +
 libproto/imap/delete.c                    |    3 +
 libproto/imap/fake-folder.c               |    7 -
 libproto/imap/fetch.c                     |   25 +-
 libproto/imap/folder.c                    | 2916 +++++------------------------
 libproto/imap/genlist.c                   |    9 +-
 libproto/imap/login.c                     |   10 +
 libproto/imap/mbox.c                      | 2142 ---------------------
 libproto/imap/rename.c                    |    3 +
 libproto/imap/resplist.c                  |   18 +
 libproto/imap/subscribe.c                 |    4 +
 libproto/imap/tests/.gitignore            |    1 +
 libproto/{mbox => imap/tests}/Makefile.am |   20 +-
 libproto/imap/tests/imapfolder.c          |  242 +++
 libproto/imap/unsubscribe.c               |    4 +
 libproto/imap/url.c                       |   12 +-
 mu/imap.c                                 |    4 +-
 23 files changed, 819 insertions(+), 4670 deletions(-)
 delete mode 100644 libproto/imap/fake-folder.c
 delete mode 100644 libproto/imap/mbox.c
 create mode 100644 libproto/imap/tests/.gitignore
 copy libproto/{mbox => imap/tests}/Makefile.am (77%)
 create mode 100644 libproto/imap/tests/imapfolder.c

diff --git a/configure.ac b/configure.ac
index dfacf09..4edf484 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1460,6 +1460,7 @@ AC_CONFIG_FILES([
  libproto/pop/Makefile
  libproto/nntp/Makefile
  libproto/imap/Makefile
+ libproto/imap/tests/Makefile
  libmu_compat/Makefile
  maidag/Makefile
  mail/Makefile
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 88a7207..ab20716 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -47,6 +47,10 @@ void mu_imap_destroy (mu_imap_t *pimap);
 int mu_imap_connect (mu_imap_t imap);
 int mu_imap_disconnect (mu_imap_t imap);
 
+int mu_imap_session_state (mu_imap_t imap);
+int mu_imap_iserror (mu_imap_t imap);
+void mu_imap_clearerr (mu_imap_t imap);
+  
 int mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter);
 int mu_imap_capability_test (mu_imap_t imap, const char *name,
 			     const char **pret);
@@ -54,6 +58,8 @@ int mu_imap_capability_test (mu_imap_t imap, const char *name,
 int mu_imap_starttls (mu_imap_t imap);
   
 int mu_imap_login (mu_imap_t imap, const char *user, const char *pass);
+int mu_imap_login_secret (mu_imap_t imap, const char *user,
+			  mu_secret_t secret);
 int mu_imap_logout (mu_imap_t imap);
 
 int mu_imap_id (mu_imap_t imap, char **idenv, mu_assoc_t *passoc);
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index 0063a27..68bdf06 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -215,6 +215,8 @@ int _mu_imap_response (mu_imap_t imap, mu_imap_response_action_t fun,
   
 int _mu_imap_list_element_is_string (struct imap_list_element *elt,
 				     const char *str);
+int _mu_imap_list_element_is_nil (struct imap_list_element *elt);
+
 int _mu_imap_list_nth_element_is_string (mu_list_t list, size_t n,
 					 const char *str);
   
@@ -225,7 +227,18 @@ struct imap_list_element *_mu_imap_list_at (mu_list_t list, int idx);
 int _mu_imap_parse_fetch_response (mu_list_t resp, mu_list_t *result_list);
 
 void _mu_close_handler (mu_imap_t imap);
-  
+
+/* ----------------------------- */
+/* URL Auxiliaries               */
+/* ----------------------------- */
+int _mu_imap_url_init (mu_url_t url);
+int _mu_imaps_url_init (mu_url_t url);
+
+/* ----------------------------- */
+/* Folder interface              */
+/* ----------------------------- */
+
+
 # ifdef __cplusplus
 }
 # endif
diff --git a/libmailutils/list/listlist.c b/libmailutils/list/listlist.c
index 38bd588..cfd5eaa 100644
--- a/libmailutils/list/listlist.c
+++ b/libmailutils/list/listlist.c
@@ -99,7 +99,9 @@ mu_list_insert_list (mu_list_t list, void *item, mu_list_t new_list,
 void
 mu_list_append_list (mu_list_t list, mu_list_t new_list)
 {
-  if (list->count == 0)
+  if (new_list->count == 0)
+    return;
+  else if (list->count == 0)
     {
       list->head = new_list->head;
       list->head.next->prev = list->head.prev->next = &list->head;
diff --git a/libmailutils/mailbox/folder.c b/libmailutils/mailbox/folder.c
index 86ad0a5..3314fcd 100644
--- a/libmailutils/mailbox/folder.c
+++ b/libmailutils/mailbox/folder.c
@@ -368,7 +368,8 @@ mu_folder_list (mu_folder_t folder, const char *dirname, void *pattern,
 		size_t max_level,
 		mu_list_t *pflist)
 {
-  return mu_folder_enumerate (folder, dirname, pattern, 0, max_level,
+  return mu_folder_enumerate (folder, dirname, pattern,
+			      MU_FOLDER_ATTRIBUTE_ALL, max_level,
 			      pflist, NULL, NULL);
 }
 
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index 82c7c87..181ac35 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -21,16 +21,14 @@ lib_LTLIBRARIES = libmu_imap.la
 libmu_imap_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
 libmu_imap_la_LIBADD = ${MU_LIB_AUTH} ${MU_LIB_MAILUTILS} @INTLLIBS@ 
 
-# FIXME: Put these back when ready
+SUBDIRS = . tests
 
-#  folder.c\
-#  mbox.c\
-#  url.c
+# FIXME: Put this back when ready
+#  mbox.c
 libmu_imap_la_SOURCES = \
  appmsg.c\
  appstr.c\
  appstrsiz.c\
- fake-folder.c\
  fetch.c\
  gencom.c\
  genlist.c\
@@ -68,5 +66,6 @@ libmu_imap_la_SOURCES = \
  tag.c\
  trace.c\
  unselect.c\
- unsubscribe.c
-
+ unsubscribe.c\
+ folder.c\
+ url.c
diff --git a/libproto/imap/create.c b/libproto/imap/create.c
index 9ae92eb..5aebcae 100644
--- a/libproto/imap/create.c
+++ b/libproto/imap/create.c
@@ -64,3 +64,30 @@ _mu_imap_init (mu_imap_t imap)
   imap->session_state = MU_IMAP_SESSION_INIT;
   return 0;
 }
+
+int
+mu_imap_session_state (mu_imap_t imap)
+{
+  if (!imap)
+    return -1;
+  return imap->session_state;
+}
+
+int
+mu_imap_iserror (mu_imap_t imap)
+{
+  if (!imap)
+    return -1;
+  return imap->client_state == MU_IMAP_CLIENT_ERROR;
+}
+
+void
+mu_imap_clearerr (mu_imap_t imap)
+{
+  if (imap)
+    {
+      imap->client_state = MU_IMAP_CLIENT_READY;
+      if (imap->io)
+	mu_imapio_clearerr (imap->io);
+    }
+}
diff --git a/libproto/imap/delete.c b/libproto/imap/delete.c
index 0818c4b..5a605d0 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/delete.c
@@ -31,6 +31,9 @@ mu_imap_delete (mu_imap_t imap, const char *mailbox)
   char const *argv[2];
   static struct imap_command com;
 
+  if (!mailbox)
+    return EINVAL;
+  
   argv[0] = "DELETE";
   argv[1] = mailbox;
 
diff --git a/libproto/imap/fake-folder.c b/libproto/imap/fake-folder.c
deleted file mode 100644
index fb59c49..0000000
--- a/libproto/imap/fake-folder.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-#include <mailutils/mailutils.h>
-
-mu_record_t mu_imap_record = NULL;
-mu_record_t mu_imaps_record = NULL;
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index f32ed3a..b1994ae 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -354,11 +354,11 @@ static int
 elt_to_string (struct imap_list_element *elt, char **pstr)
 {
   char *p;
-  
-  if (elt->type != imap_eltype_string)
-    return EINVAL;
-  if (mu_c_strcasecmp (elt->v.string, "NIL") == 0)
+
+  if (_mu_imap_list_element_is_nil (elt))
     p = NULL;
+  else if (elt->type != imap_eltype_string)
+    return EINVAL;
   else
     {
       p = strdup (elt->v.string);
@@ -386,13 +386,13 @@ _fill_subaddr (void *item, void *data)
     return 0;
   
   arg = _mu_imap_list_at (elt->v.list, 0);
-  if (arg && arg->type == imap_eltype_string && strcmp (arg->v.string, "NIL"))
+  if (arg && arg->type == imap_eltype_string)
     personal = arg->v.string;
   arg = _mu_imap_list_at (elt->v.list, 2);
-  if (arg && arg->type == imap_eltype_string && strcmp (arg->v.string, "NIL"))
+  if (arg && arg->type == imap_eltype_string)
     local = arg->v.string;
   arg = _mu_imap_list_at (elt->v.list, 3);
-  if (arg && arg->type == imap_eltype_string && strcmp (arg->v.string, "NIL"))
+  if (arg && arg->type == imap_eltype_string)
     domain = arg->v.string;
 
   if (domain && local)
@@ -414,13 +414,10 @@ _fill_subaddr (void *item, void *data)
 static int
 elt_to_address (struct imap_list_element *elt, mu_address_t *paddr)
 {
-  if (elt->type != imap_eltype_list)
-    {
-      if (mu_c_strcasecmp (elt->v.string, "NIL") == 0)
-	*paddr = NULL;
-      else
-	return EINVAL;
-    }
+  if (_mu_imap_list_element_is_nil (elt))
+    *paddr = NULL;
+  else if (elt->type != imap_eltype_list)
+    return EINVAL;
   else
     {
       struct addr_env addr_env;
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index 828f2a2..1bf637f 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -1,6 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2009,
-   2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011 Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -20,352 +19,395 @@
 # include <config.h>
 #endif
 
-#ifdef ENABLE_IMAP
-
 #include <stdlib.h>
-#include <stdarg.h>
-#include <errno.h>
 #include <string.h>
-#include <assert.h>
-#include <fnmatch.h>
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <mailutils/sys/imap.h>
-#include <mailutils/sys/url.h>
+#include <netinet/in.h>
 
-#include <mailutils/auth.h>
-#include <mailutils/attribute.h>
+#include <mailutils/imap.h>
+#include <mailutils/diag.h>
 #include <mailutils/debug.h>
 #include <mailutils/error.h>
 #include <mailutils/errno.h>
-#include <mailutils/header.h>
-#include <mailutils/observer.h>
-#include <mailutils/stream.h>
-#include <mailutils/iterator.h>
-#include <mailutils/argcv.h>
-#include <mailutils/tls.h>
 #include <mailutils/nls.h>
+#include <mailutils/tls.h>
+#include <mailutils/url.h>
+#include <mailutils/list.h>
 #include <mailutils/secret.h>
-#include <mailutils/util.h>
-#include <mailutils/cctype.h>
 #include <mailutils/cstr.h>
+#include <mailutils/sockaddr.h>
+#include <mailutils/wordsplit.h>
+#include <mailutils/sys/imap.h>
+#include <mailutils/sys/folder.h>
 
-/* For dbg purposes set to one to see different level of traffic.  */
-/* Print to stderr the command sent to the IMAP server.  */
-#define DEBUG_SHOW_COMMAND 0
-/* Print to stderr the responses received from the IMAP server.  */
-#define DEBUG_SHOW_RESPONSE 0
-/* Print to stderr the literal/quoted string received from the IMAP server.  */
-#define DEBUG_SHOW_DATA 0
+/* Placeholders. */
+#define _mu_imap_mailbox_init NULL
+#define _mu_imaps_mailbox_init NULL
 
-/* Variable use for the registrar.  */
-static struct _mu_record _imap_record =
+
+static void
+_mu_imap_folder_destroy (mu_folder_t folder)
 {
-  MU_IMAP_PRIO,
-  MU_IMAP_SCHEME,
-  MU_RECORD_DEFAULT,
-  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
-  MU_URL_HOST,
-  _url_imap_init,     /* url entry.  */
-  _mailbox_imap_init, /* Mailbox entry.  */
-  NULL,               /* Mailer entry.  */
-  _folder_imap_init,  /* Folder entry.  */
-  NULL, /* No need for a back pointer.  */
-  NULL, /* _is_scheme method.  */
-  NULL, /* _get_url method.  */
-  NULL, /* _get_mailbox method.  */
-  NULL, /* _get_mailer method.  */
-  NULL  /* _get_folder method.  */
-};
+  mu_imap_t imap = folder->data;
+  if (imap)
+    {
+      /*
+	mu_imap_logout (imap);
+	mu_imap_disconnect (imap);
+      */
+      mu_imap_destroy (&imap);
+      folder->data = imap;
+    }
+}
+
+/* Imap callbacks */
+static void
+_mu_folder_preauth_callback (void *data, int code, size_t sdat, void *pdat)
+{
+  /*  mu_folder_t folder = data;*/
+  const char *text = pdat;
+  mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_TRACE1,
+	    (_("IMAP server opened in preauth mode: %s"), text));
+}
 
-/* We export this variable: url parsing and the initialisation of the mailbox,
-   via the register entry/record.  */
-mu_record_t mu_imap_record = &_imap_record;
+static void
+_mu_folder_bye_callback (void *data, int code, size_t sdat, void *pdat)
+{
+  mu_folder_t folder = data;
+  const char *text = pdat;
+  mu_imap_t imap = folder->data;
+  mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_TRACE1,
+	    (_("IMAP server closing connection: %s"), text));
+  /*FIXME:  mu_imap_disconnect (imap);*/
+}
 
-#ifdef WITH_TLS
-static struct _mu_record _imaps_record =
+static void
+_mu_folder_bad_callback (void *data, int code, size_t sdat, void *pdat)
 {
-  MU_IMAP_PRIO,
-  MU_IMAPS_SCHEME,
-  MU_RECORD_DEFAULT,
-  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
-  MU_URL_HOST,
-  _url_imaps_init,     /* url entry.  */
-  _mailbox_imaps_init, /* Mailbox entry.  */
-  NULL,                /* Mailer entry.  */
-  _folder_imap_init,   /* Folder entry.  */
-  NULL, /* No need for a back pointer.  */
-  NULL, /* _is_scheme method.  */
-  NULL, /* _get_url method.  */
-  NULL, /* _get_mailbox method.  */
-  NULL, /* _get_mailer method.  */
-  NULL  /* _get_folder method.  */
-};
-mu_record_t mu_imaps_record = &_imaps_record;
-#else
-mu_record_t mu_imaps_record = NULL;
-#endif /* WITH_TLS */
+  /*  mu_folder_t folder = data;*/
+  const char *text = pdat;
 
-#ifndef HAVE_STRTOK_R
-char *strtok_r                      (char *, const char *, char **);
+  mu_error (_("IMAP server complains: %s"), text);
+  mu_error (_("This probably indicates a bug in Mailutils client code."));
+  mu_error (_("Please, report that to <%s>."), PACKAGE_BUGREPORT);
+}
+#if 0
+static void
+_mu_folder_fetch_callback (void *data, int code, size_t sdat, void *pdat)
+{
+  mu_folder_t folder = data;
+}
 #endif
+
+/* Set up an IMAP(S) connection for this folder */
+static int
+_mu_imap_folder_open (mu_folder_t folder, int flags)
+{
+  int rc;
+  mu_imap_t imap = folder->data;
+  struct mu_sockaddr *sa;
+  struct mu_sockaddr_hints hints;
+  char const *s;
+  int tls;
+  mu_stream_t transport;
 
-/* Concrete mu_folder_t IMAP implementation.  */
-static int  folder_imap_open        (mu_folder_t, int);
-static int  folder_imap_close       (mu_folder_t);
-static void folder_imap_destroy     (mu_folder_t);
-static int  folder_imap_delete      (mu_folder_t, const char *);
-static int  folder_imap_list        (mu_folder_t, const char *, void *,
-				     int, size_t,
-				     mu_list_t,
-				     mu_folder_enumerate_fp efp, void *edp);
-static int  folder_imap_lsub        (mu_folder_t, const char *, const char *,
-				     mu_list_t);
-static int  folder_imap_rename      (mu_folder_t, const char *,
-				     const char *);
-static int  folder_imap_subscribe   (mu_folder_t, const char *);
-static int  folder_imap_unsubscribe (mu_folder_t, const char *);
-static int  folder_imap_get_authority (mu_folder_t, mu_authority_t *);
+  /* FIXME: This monitor business is suspicious */
+  mu_monitor_wrlock (folder->monitor);
+  rc = mu_imap_session_state (imap);
+  mu_monitor_unlock (folder->monitor);
+  if (rc != MU_IMAP_SESSION_INIT)
+    return 0;
 
-/* FETCH  */
-static int  imap_fetch              (f_imap_t);
-static int  imap_rfc822             (f_imap_t, char **);
-static int  imap_rfc822_size        (f_imap_t, char **);
-static int  imap_rfc822_header      (f_imap_t, char **);
-static int  imap_rfc822_text        (f_imap_t, char **);
-static int  imap_fetch_flags        (f_imap_t, char **);
-static int  imap_permanentflags     (f_imap_t, char **);
-static int  imap_flags              (char **, int *);
-static int  imap_bodystructure      (f_imap_t, char **);
-static int  imap_body               (f_imap_t, char **);
-static int  imap_internaldate       (f_imap_t, char **);
+  mu_url_sget_scheme (folder->url, &s);
+  tls = strcmp (s, "imaps") == 0;
 
-static int  imap_uid                (f_imap_t, char **);
-static int  imap_status             (f_imap_t);
-static int  imap_expunge            (f_imap_t, unsigned int);
-static int  imap_search             (f_imap_t);
+  memset (&hints, 0, sizeof (hints));
+  hints.flags = MU_AH_DETECT_FAMILY;
+  hints.port = tls ? MU_IMAP_DEFAULT_SSL_PORT : MU_IMAP_DEFAULT_PORT;
+  hints.protocol = IPPROTO_TCP;
+  hints.socktype = SOCK_STREAM;
 
-/* String.  */
-static int  imap_literal_string     (f_imap_t, char **);
-static int  imap_string             (f_imap_t, char **);
-static int  imap_quoted_string      (f_imap_t, char **);
-static int  imap_mailbox_name_match (const char* pattern, const char* mailbox);
+  rc = mu_sockaddr_from_url (&sa, folder->url, &hints);
+  if (rc)
+    {
+      s = mu_url_to_string (folder->url);
+      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		(_("cannot create sockaddr from URL %s: %s"),
+		 s, mu_strerror (rc)));
+      return rc;
+    }
 
-static int  imap_token              (char *, size_t, char **);
+  rc = mu_tcp_stream_create_from_sa (&transport, sa, NULL, 0);
+  if (rc)
+    {
+      s = mu_url_to_string (folder->url);
+      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		(_("cannot create stream from URL %s: %s"),
+		 s, mu_strerror (rc)));
+      mu_sockaddr_free (sa);
+      return rc;
+    }
+  
+#ifdef WITH_TLS
+  if (tls)
+    {
+      mu_stream_t tlsstream;
+	      
+      rc = mu_tls_client_stream_create (&tlsstream, transport, transport, 0);
+      mu_stream_unref (transport);
+      if (rc)
+	{
+	  mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		    (_("cannot create TLS stream: %s"),
+		     mu_strerror (rc)));
+	  return rc;
+	}
+      transport = tlsstream;
+    }
+#endif
 
-/* Capability */
-static int  parse_capa              (f_imap_t f_imap, char *str);
-static int  read_capa               (f_imap_t f_imap, int force);
-static int  check_capa              (f_imap_t f_imap, char *capa);
+  mu_imap_set_carrier (imap, transport);
+
+  if (mu_debug_level_p (MU_DEBCAT_FOLDER, MU_DEBUG_PROT) ||
+      mu_debug_level_p (MU_DEBCAT_MAILBOX, MU_DEBUG_PROT))
+    mu_imap_trace (imap, MU_IMAP_TRACE_SET);
+  if (mu_debug_level_p (MU_DEBCAT_FOLDER, MU_DEBUG_TRACE6) ||
+      mu_debug_level_p (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE6))
+    mu_imap_trace_mask (imap, MU_IMAP_TRACE_SET, MU_XSCRIPT_SECURE);
+  if (mu_debug_level_p (MU_DEBCAT_FOLDER, MU_DEBUG_TRACE7) ||
+      mu_debug_level_p (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE7))
+    mu_imap_trace_mask (imap, MU_IMAP_TRACE_SET, MU_XSCRIPT_PAYLOAD);
+
+  /* Set callbacks */
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_PREAUTH,
+				      _mu_folder_preauth_callback,
+				      folder);
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_BYE,
+				      _mu_folder_bye_callback,
+				      folder);
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_BAD,
+				      _mu_folder_bad_callback,
+				      folder);
+#if 0
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
+				      _mu_folder_fetch_callback,
+				      folder);
+#endif
+  rc = mu_imap_connect (imap);
+  if (rc)
+    {
+      s = mu_url_to_string (folder->url);
+      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		(_("failed to connect to %s: %s"),
+		 s, mu_strerror (rc)));
+      if (mu_imap_strerror (imap, &s))
+	mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		  (_("server response: %s"), s));
+      mu_imap_destroy (&imap);
+      return rc;
+    }
 
+  if (mu_imap_session_state (imap) == MU_IMAP_SESSION_NONAUTH)
+    {
+      rc = mu_authority_authenticate (folder->authority);
+      if (rc)
+	{
+	  mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		    (_("IMAP authentication: %s"),
+		     mu_strerror (rc)));
+	  mu_folder_close (folder);
+	}
+    }
+  
+  return rc;
+}
 
-/* Authentication methods */
-
-typedef int (*auth_method_t) (mu_authority_t);
-
-/* Simple User/pass authentication for imap.  */
+/* Close existing connection */
 static int
-authenticate_imap_login (mu_authority_t auth)
+_mu_imap_folder_close (mu_folder_t folder)
 {
-  mu_folder_t folder = mu_authority_get_owner (auth);
-  f_imap_t f_imap = folder->data;
-  mu_ticket_t ticket;
-  int status = 0;
+  mu_imap_t imap = folder->data;
 
-  if (check_capa (f_imap, "LOGINDISABLED") == 0)
+  if (mu_imap_session_state (imap) > MU_IMAP_SESSION_INIT)
     {
-      MU_DEBUG (folder->debug, MU_DEBUG_TRACE, "LOGIN command disabled\n");
-      return ENOSYS;
+      mu_imap_clearerr (imap);
+      mu_imap_logout (imap);
+      mu_imap_disconnect (imap);
     }
   
-  switch (f_imap->state)
-    {
-    case IMAP_AUTH:
-      {
-	/* Grab the User and Passwd information.  */
-	mu_authority_get_ticket (auth, &ticket);
-	if (f_imap->user)
-	  free (f_imap->user);
-	/* Was it in the URL?  */
-	status = mu_url_aget_user (folder->url, &f_imap->user);
-        if (status == MU_ERR_NOENT)
-	  status = mu_ticket_get_cred (ticket, folder->url,
-				       "Imap User: ", &f_imap->user, NULL);
-	if (status == MU_ERR_NOENT || f_imap->user == NULL)
-	  return MU_ERR_NOUSERNAME;
-	else if (status)
-	  return status;
-
-	status = mu_url_get_secret (folder->url, &f_imap->secret);
-        if (status == MU_ERR_NOENT)
-	  status = mu_ticket_get_cred (ticket, folder->url,
-				       "Imap Passwd: ",
-				       NULL, &f_imap->secret);
-	
-	if (status == MU_ERR_NOENT || !f_imap->secret)
-	  /* FIXME: Is this always right? The user might legitimately have
-	     no password */
-	  return MU_ERR_NOPASSWORD;
-	else if (status)
-	  return status;
-	
-	status = imap_writeline (f_imap, "g%lu LOGIN \"%s\" \"%s\"\r\n",
-				 (unsigned long) f_imap->seq, f_imap->user,
-				 mu_secret_password (f_imap->secret));
-	mu_secret_password_unref (f_imap->secret);
-	mu_secret_unref (f_imap->secret);
-	f_imap->secret = NULL;
-	CHECK_ERROR_CLOSE (folder, f_imap, status);
-	MU_DEBUG2 (folder->debug, MU_DEBUG_TRACE, "g%lu LOGIN %s *\n",
-		   (unsigned long) f_imap->seq, f_imap->user);
-	f_imap->seq++;
-	free (f_imap->user);
-	f_imap->user = NULL;
-	f_imap->secret = NULL;
-	f_imap->state = IMAP_LOGIN;
-      }
-
-    case IMAP_LOGIN:
-      /* Send it across.  */
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      /* Clear the buffer it contains the passwd. */
-      memset (f_imap->buffer, '\0', f_imap->buflen);
-      f_imap->state = IMAP_LOGIN_ACK;
-
-    case IMAP_LOGIN_ACK:
-      /* Get the login ack.  */
-      status = imap_parse (f_imap);
-      if (status)
-	return status;
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_AUTH_DONE;
-
-    default:
-      break;  /* We're outta here.  */
-    }
-  CLEAR_STATE (f_imap);
   return 0;
 }
+
+struct enumerate_closure
+{
+  mu_folder_t folder;
+  mu_folder_enumerate_fp fun;
+  void *data;
+};
 
-/*
-  The anonymous SASL mechanism is defined in rfc2245.txt as a single
-  message from client to server:
-
-  message         = [email / token]
-
-  So the message is optional.
-
-  The command is:
-
-  C: <tag> authenticate anonymous
-
-  The server responds with a request for continuation data (the "message"
-  in the SASL syntax). We respond with no data, which is legal.
+static int
+_enumerate_helper (void *item, void *data)
+{
+  struct mu_list_response *rp = item;
+  struct enumerate_closure *clos = data;
 
-  S: +
-  C:
+  return clos->fun (clos->folder, rp, clos->data);
+}
 
-  The server should then respond with OK on success, or else a failure
-  code (NO or BAD).
+static int
+_mu_imap_folder_list (mu_folder_t folder, const char *ref, void *name,
+		      int flags, size_t max_level,
+		      mu_list_t flist,
+		      mu_folder_enumerate_fp efp, void *edp)
+{
+  mu_imap_t imap = folder->data;
+  mu_list_t list;
+  int rc = mu_imap_list_new (imap, ref, name, &list);
 
-  If OK, then we are authenticated!
+  if (rc)
+    return rc;
 
-  So, states are:
+  if (max_level ||
+      (flags & MU_FOLDER_ATTRIBUTE_ALL) != MU_FOLDER_ATTRIBUTE_ALL)
+    {
+      /* Filter out the list, eliminating non-matching entries */
+      mu_iterator_t itr;
 
-  AUTH_ANON_REQ
+      rc = mu_list_get_iterator (list, &itr);
+      if (rc)
+	{
+	  mu_list_destroy (&list);
+	  return rc;
+	}
 
-  > g%u AUTHENTICATE ANONYMOUS
+      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+	   mu_iterator_next (itr))
+	{
+	  struct mu_list_response *rp;
 
-  AUTH_ANON_WAIT_CONT
+	  mu_iterator_current (itr, (void**) &rp);
+	  if (!(rp->type & flags) || (max_level && rp->level > max_level))
+	    mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	}
+      mu_iterator_destroy (&itr);
+    }
 
-  < +
+  if (efp)
+    {
+      struct enumerate_closure clos;
 
-  AUTH_ANON_MSG
+      clos.folder = folder;
+      clos.fun = efp;
+      clos.data = edp;
 
-  >
+      rc = mu_list_foreach (list, _enumerate_helper, &clos);
+    }
 
-  AUTH_ANON_WAIT_RESP
+  if (flist)
+    mu_list_append_list (flist, list);
 
-  < NO/BAD/OK
+  mu_list_destroy (&list);
 
-*/
+  return rc;
+}
+
+static int
+_mu_imap_folder_lsub (mu_folder_t folder, const char *ref, const char *name,
+		      mu_list_t flist)
+{
+  mu_imap_t imap = folder->data;
+  return mu_imap_lsub (imap, ref, name, flist);
+}
+
+/* Subscribe to the named mailbox. */
+static int
+_mu_imap_folder_subscribe (mu_folder_t folder, const char *name)
+{
+  mu_imap_t imap = folder->data;
+  return mu_imap_subscribe (imap, name);
+}
+/* Unsubscribe from the mailbox. */
+static int
+_mu_imap_folder_unsubscribe (mu_folder_t folder, const char *name)
+{
+  mu_imap_t imap = folder->data;
+  return mu_imap_unsubscribe (imap, name);
+}
+
+/* Remove a mailbox.  */
+static int
+_mu_imap_folder_delete (mu_folder_t folder, const char *name)
+{
+  mu_imap_t imap = folder->data;
+  return mu_imap_delete (imap, name);
+}
+
+/* Rename OLDPATH to NEWPATH */
+static int
+_mu_imap_folder_rename (mu_folder_t folder, const char *oldpath,
+			const char *newpath)
+{
+  mu_imap_t imap = folder->data;
+  return mu_imap_rename (imap, oldpath, newpath);
+}
+
+typedef int (*auth_method_t) (mu_authority_t);
 
 static int
-authenticate_imap_sasl_anon (mu_authority_t auth)
+authenticate_imap_login (mu_authority_t auth)
 {
   mu_folder_t folder = mu_authority_get_owner (auth);
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  assert (f_imap->state == IMAP_AUTH);
-
-  if (check_capa (f_imap, "AUTH=ANONYMOUS"))
+  mu_imap_t imap = folder->data;
+  mu_ticket_t ticket;
+  char *user;
+  int rc;
+  mu_secret_t secret;
+  
+  rc = mu_imap_capability_test (imap, "LOGINDISABLED", NULL);
+  if (rc == 0)
+    {
+      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		(_("IMAP LOGIN disabled")));
+      return rc;
+    }
+  else if (rc != MU_ERR_NOENT)
+    {
+      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+		(_("cannot test server capabilities (%s), continuing anyway"),
+		 mu_strerror (rc)));
+      return rc;
+    }
+
+  /* Grab the User and Passwd information.  */
+  mu_authority_get_ticket (auth, &ticket);
+  /* Was it in the URL?  */
+  rc = mu_url_aget_user (folder->url, &user);
+  if (rc == MU_ERR_NOENT)
+    rc = mu_ticket_get_cred (ticket, folder->url, "Imap User: ", &user, NULL);
+  if (rc == MU_ERR_NOENT || user == NULL)
+    return MU_ERR_NOUSERNAME;
+  else if (rc)
+    return rc;
+
+  rc = mu_url_get_secret (folder->url, &secret);
+  if (rc == MU_ERR_NOENT)
+    rc = mu_ticket_get_cred (ticket, folder->url,
+			     "Imap Passwd: ", NULL, &secret);
+	
+  if (rc == MU_ERR_NOENT || !secret)
     {
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT,
-		"ANONYMOUS capability not present\n");
-      return ENOSYS;
+      /* FIXME: Is this always right? The user might legitimately have
+	 no password */
+      free (user);
+      return MU_ERR_NOPASSWORD;
     }
-
-  /* FIXME: auth_state is never set explicitely before this function */
-  switch (f_imap->auth_state)
+  else if (rc)
     {
-    case IMAP_AUTH_ANON_REQ_WRITE:
-      {
-	MU_DEBUG1 (folder->debug, MU_DEBUG_PROT, 
-                   "g%lu AUTHENTICATE ANONYMOUS\n",
-		   (unsigned long) f_imap->seq);
-	status = imap_writeline (f_imap, "g%lu AUTHENTICATE ANONYMOUS\r\n",
-				 (unsigned long) f_imap->seq);
-	f_imap->seq++;
-	CHECK_ERROR_CLOSE (folder, f_imap, status);
-	f_imap->auth_state = IMAP_AUTH_ANON_REQ_SEND;
-      }
-
-    case IMAP_AUTH_ANON_REQ_SEND:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->auth_state = IMAP_AUTH_ANON_WAIT_CONT;
-
-    case IMAP_AUTH_ANON_WAIT_CONT:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      if (strncmp ("+", f_imap->buffer, 2) == 0)
-	{
-	  f_imap->auth_state = IMAP_AUTH_ANON_MSG;
-	}
-      else
-	{
-	  /* Something is wrong! */
-	}
-      f_imap->auth_state = IMAP_AUTH_ANON_MSG;
-
-    case IMAP_AUTH_ANON_MSG:
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, "\n");
-      status = imap_writeline (f_imap, "\r\n");
-      CHECK_ERROR_CLOSE (folder, f_imap, status);
-      f_imap->auth_state = IMAP_AUTH_ANON_MSG_SEND;
-
-    case IMAP_AUTH_ANON_MSG_SEND:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-
-      f_imap->auth_state = IMAP_AUTH_ANON_WAIT_RESP;
-
-    case IMAP_AUTH_ANON_WAIT_RESP:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;			/* We're outta here.  */
+      free (user);
+      return rc;
     }
-  CLEAR_STATE (f_imap);
-  return 0;
+  rc = mu_imap_login_secret (imap, user, secret);
+  mu_secret_unref (secret);
+  return rc;
 }
 
 struct auth_tab
@@ -379,7 +421,7 @@ struct auth_tab
 
 static struct auth_tab auth_tab[] = {
   { "login", authenticate_imap_login },
-  { "anon", authenticate_imap_sasl_anon },
+  /*  { "anon",  authenticate_imap_sasl_anon },*/
   { NULL }
 };
 
@@ -398,74 +440,13 @@ find_auth_method (const char *name)
 static int
 authenticate_imap_select (mu_authority_t auth)
 {
-  mu_folder_t folder = mu_authority_get_owner (auth);
-  f_imap_t f_imap = folder->data;
   struct auth_tab *p;
-  int status = ENOSYS;
+  int rc = ENOSYS;
   
-  for (p = auth_tab; status == ENOSYS && p->name; p++)
-    {
-      f_imap->state = IMAP_AUTH;
-      status = p->method (auth);
-    }
-
-  return status;
-}
-
-
-
-
-/* Initialize the concrete IMAP mailbox: overload the folder functions  */
-int
-_folder_imap_init (mu_folder_t folder)
-{
-  int status;
-  f_imap_t f_imap;
-
-  /* Set the authority early:
-     (1) so we can check for errors.
-     (2) allow the client to get the authority for setting the ticket
-     before the open.  */
-  status = folder_imap_get_authority (folder, NULL);
-  if (status != 0)
-    return status;
-
-  f_imap = folder->data = calloc (1, sizeof (*f_imap));
-  if (f_imap == NULL)
-    return ENOMEM;
-
-  f_imap->folder = folder;
-  f_imap->state = IMAP_NO_STATE;
-
-  folder->_destroy = folder_imap_destroy;
+  for (p = auth_tab; rc == ENOSYS && p->name; p++)
+    rc = p->method (auth);
 
-  folder->_open = folder_imap_open;
-  folder->_close = folder_imap_close;
-
-  folder->_list = folder_imap_list;
-  folder->_lsub = folder_imap_lsub;
-  folder->_subscribe = folder_imap_subscribe;
-  folder->_unsubscribe = folder_imap_unsubscribe;
-  folder->_delete = folder_imap_delete;
-  folder->_rename = folder_imap_rename;
-
-  return 0;
-}
-
-/* Destroy the folder resources.  */
-static void
-folder_imap_destroy (mu_folder_t folder)
-{
-  if (folder->data)
-    {
-      f_imap_t f_imap = folder->data;
-      if (f_imap->buffer)
-	free (f_imap->buffer);
-      if (f_imap->capav)
-	mu_argcv_free (f_imap->capac, f_imap->capav);
-      free (f_imap);
-      folder->data = NULL;
-    }
+  return rc;
 }
 
 static int
@@ -473,2152 +454,139 @@ folder_set_auth_method (mu_folder_t folder, auth_method_t method)
 {
   if (!folder->authority)
     {
-      int status = mu_authority_create (&folder->authority, NULL, folder);
-      if (status)
-	return status;
+      int rc = mu_authority_create (&folder->authority, NULL, folder);
+      if (rc)
+	return rc;
     }
   return mu_authority_set_authenticate (folder->authority, method, folder);
 }
 
 static int
-folder_imap_get_authority (mu_folder_t folder, mu_authority_t *pauth)
+_imap_folder_setup_authority (mu_folder_t folder)
 {
-  int status = 0;
-  if (folder->authority == NULL)
+  int rc = 0;
+
+  if (!folder->authority)
     {
-      /* assert (folder->url); */
+      const char *auth;
       if (folder->url == NULL)
 	return EINVAL;
 
-      if (folder->url->auth == NULL
-	  || strcmp (folder->url->auth, "*") == 0)
-	{
-	  status = folder_set_auth_method (folder, authenticate_imap_select);
-	}
+      if (mu_url_sget_auth (folder->url, &auth))
+	rc = folder_set_auth_method (folder, authenticate_imap_select);
+      else if (strcmp (auth, "*") == 0)
+	rc = folder_set_auth_method (folder, authenticate_imap_select);
       else 
 	{
-	  char *p, *sp;
+	  struct mu_wordsplit ws;
 
-	  for (p = strtok_r (folder->url->auth, ",", &sp);
-	       status == 0 && p;
-	       p = strtok_r (NULL, ",", &sp))
+	  ws.ws_delim = ",";
+	  if (mu_wordsplit (auth, &ws,
+			    MU_WRDSF_NOVAR | MU_WRDSF_NOCMD |
+			    MU_WRDSF_DELIM))
+	    {
+	      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+			(_("cannot split out auth part: %s"),
+			 mu_wordsplit_strerror (&ws)));
+	      rc = MU_ERR_FAILURE;
+	    }
+	  else
 	    {
-	      auth_method_t method = find_auth_method (p);
-	      if (method)
-		status = folder_set_auth_method (folder, method);
-	      else
-		status = MU_ERR_BAD_AUTH_SCHEME;
-	    }		  
+	      int i;
+
+	      for (i = 0; i < ws.ws_wordc; i++)
+		{
+		  auth_method_t method = find_auth_method (ws.ws_wordv[i]);
+		  if (method)
+		    rc = folder_set_auth_method (folder, method);
+		  else
+		    {
+		      mu_debug (MU_DEBCAT_FOLDER, MU_DEBUG_ERROR,
+				(_("unrecognized AUTH scheme %s"),
+				 ws.ws_wordv[i]));
+		      rc = MU_ERR_BAD_AUTH_SCHEME;
+		    }
+		}
+	      mu_wordsplit_free (&ws);
+	    }
 	}
     }
 
-  if (status)
-    return status;
-  
-  if (pauth)
-    *pauth = folder->authority;
-  return status;
+  return rc;
 }
-
 
-/* Capability handling */
-static int
-parse_capa (f_imap_t f_imap, char *str)
+int
+_mu_imap_folder_init (mu_folder_t folder)
 {
-  if (f_imap->capav)
-    mu_argcv_free (f_imap->capac, f_imap->capav);
-  return mu_argcv_get (str, "", NULL, &f_imap->capac, &f_imap->capav);
-}
+  mu_imap_t imap;
+  int rc;
 
-static int
-read_capa (f_imap_t f_imap, int force)
-{
-  int status = 0;
-  
-  if (force)
-    {
-      mu_argcv_free (f_imap->capac, f_imap->capav);
-      f_imap->capac = 0;
-      f_imap->capav = NULL;
-    }
+  rc = _imap_folder_setup_authority (folder);
+  if (rc)
+    return rc;
   
-  if (!f_imap->capav)
-    {
-      status = imap_writeline (f_imap, "g%lu CAPABILITY\r\n",
-			       (unsigned long) f_imap->seq++);
-      status = imap_send (f_imap);
-      status = imap_parse (f_imap);
-    }
-  return status;
-}
-
-static int
-check_capa (f_imap_t f_imap, char *capa)
-{
-  int i;
-
-  read_capa (f_imap, 0);
-  for (i = 0; i < f_imap->capac; i++)
-    if (mu_c_strcasecmp (f_imap->capav[i], capa) == 0)
-      return 0;
-  return 1;
-}
-
-
-static int
-imap_reader (void *iodata)
-{
-  f_imap_t iop = iodata;
-  int status = imap_parse (iop);
-  CHECK_EAGAIN (iop, status);
-  MU_DEBUG (iop->folder->debug, MU_DEBUG_PROT, iop->buffer);
-  return status;
-}
-
-static int
-imap_writer (void *iodata, char *buf)
-{
-  f_imap_t iop = iodata;
-  int status;
-    
-  MU_DEBUG2 (iop->folder->debug, MU_DEBUG_PROT, "g%lu %s\n",
-             (unsigned long)iop->seq, buf);
-  status = imap_writeline (iop, "g%lu %s\r\n",
-			   (unsigned long)iop->seq++, buf);
-  CHECK_ERROR (iop, status);
-  status = imap_send (iop);
-  CHECK_ERROR (iop, status);
-  return status;
-}
+  rc = mu_imap_create (&imap);
+  if (rc)
+    return rc;
 
-static void
-imap_stream_ctl (void *iodata, mu_stream_t *pold, mu_stream_t new)
-{
-  f_imap_t iop = iodata;
-  if (pold)
-    *pold = iop->folder->stream;
-  if (new)
-    iop->folder->stream = new;
-}  
+  folder->data = imap;
 
-static int
-tls (mu_folder_t folder)
-{
-#ifdef WITH_TLS
-  int status;
-  f_imap_t f_imap = folder->data;
-  char *keywords[] = { "STARTTLS", "CAPABILITY", NULL };
+  folder->_destroy = _mu_imap_folder_destroy;
 
-  if (!mu_tls_enable || check_capa (f_imap, "STARTTLS"))
-    return -1;
+  folder->_open = _mu_imap_folder_open;
+  folder->_close = _mu_imap_folder_close;
 
-  status = mu_tls_begin (f_imap, imap_reader, imap_writer,
-			 imap_stream_ctl, keywords);
+  folder->_list = _mu_imap_folder_list;
+  folder->_lsub = _mu_imap_folder_lsub;
 
-  MU_DEBUG1 (folder->debug, MU_DEBUG_PROT, "TLS negotiation %s\n",
-	     status == 0 ? "succeeded" : "failed");
-  return status;
-#else
-  return -1;
-#endif /* WITH_TLS */
+  folder->_subscribe = _mu_imap_folder_subscribe;
+  folder->_unsubscribe = _mu_imap_folder_unsubscribe;
+  folder->_delete = _mu_imap_folder_delete;
+  folder->_rename = _mu_imap_folder_rename;
+  return 0;
 }
-
-/* Create/Open the stream for IMAP.  */
-static int
-folder_imap_open (mu_folder_t folder, int flags)
+
+static struct _mu_record _imap_record =
 {
-  f_imap_t f_imap = folder->data;
-  const char *host;
-  unsigned port = f_imap->imaps ? MU_IMAPS_PORT : MU_IMAP_PORT;
-  int status = 0;
-
-  /* If we are already open for business, noop.  */
-  mu_monitor_wrlock (folder->monitor);
-  if (f_imap->isopen)
-    {
-      mu_monitor_unlock (folder->monitor);
-      return 0;
-    }
-  mu_monitor_unlock (folder->monitor);
+  MU_IMAP_PRIO,
+  MU_IMAP_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
+  MU_URL_HOST,
+  _mu_imap_url_init,     /* url entry.  */
+  _mu_imap_mailbox_init, /* Mailbox entry.  */
+  NULL,                  /* Mailer entry.  */
+  _mu_imap_folder_init,  /* Folder entry.  */
+  NULL, /* No need for a back pointer.  */
+  NULL, /* _is_scheme method.  */
+  NULL, /* _get_url method.  */
+  NULL, /* _get_mailbox method.  */
+  NULL, /* _get_mailer method.  */
+  NULL  /* _get_folder method.  */
+};
 
-  /* Fetch the server name and the port in the mu_url_t.  */
-  status = mu_url_sget_host (folder->url, &host);
-  if (status != 0)
-    return status;
-  mu_url_get_port (folder->url, &port);
+mu_record_t mu_imap_record = &_imap_record;
 
-  folder->flags = flags;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      /* allocate working io buffer.  */
-      if (f_imap->buffer == NULL)
-        {
-	  /* There is no particular limit on the length of a command/response
-	     in IMAP. We start with 255, which is quite reasonnable and grow
-	     as we go along.  */
-          f_imap->buflen = 255;
-          f_imap->buffer = calloc (f_imap->buflen + 1, 1);
-          if (f_imap->buffer == NULL)
-            {
-              CHECK_ERROR (f_imap, ENOMEM);
-            }
-	  status = mu_memory_stream_create (&f_imap->string.stream, NULL, MU_STREAM_RDWR);
-          CHECK_ERROR (f_imap, status);
-        }
-      else
-        {
-	  /* Clear from any residue.  */
-          memset (f_imap->buffer, '\0', f_imap->buflen);
-	  mu_stream_truncate (f_imap->string.stream, 0);
-	  f_imap->string.offset = 0;
-	  f_imap->string.nleft = 0;
-        }
-      f_imap->ptr = f_imap->buffer;
-
-      /* Create the networking stack.  */
-      if (folder->stream == NULL)
-        {
-          status = mu_tcp_stream_create (&folder->stream, host, port, folder->flags);
-          CHECK_ERROR (f_imap, status);
-
-#ifdef WITH_TLS
-	  if (f_imap->imaps)
-	    {
-	      mu_stream_t newstr;
-
-	      status = mu_tls_client_stream_create (&newstr, 
-	                                            folder->stream,
-	                                            folder->stream, 0);
-	      if (status != 0)
-		{
-		  mu_error ("folder_imap_open: mu_tls_client_stream_create: %s",
-			    mu_strerror (status));
-		  return status;
-		}
-	      folder->stream = newstr;
-	    }
-#endif /* WITH_TLS */
-
-	  /* Ask for the stream internal buffering mechanism scheme.  */
-	  mu_stream_setbufsiz (folder->stream, BUFSIZ);
-        }
-      else
-        mu_stream_close (folder->stream);
-      MU_DEBUG2 (folder->debug, MU_DEBUG_PROT, "imap_open (%s:%ld)\n",
-		 host, port);
-      f_imap->state = IMAP_OPEN_CONNECTION;
-
-    case IMAP_OPEN_CONNECTION:
-      /* Establish the connection.  */
-      if (!mu_stream_is_open (folder->stream))
-        {
-          status = mu_stream_open (folder->stream);
-          CHECK_EAGAIN (f_imap, status);
-          /* Can't recover bailout.  */
-          CHECK_ERROR_CLOSE (folder, f_imap, status);
-        }
-      f_imap->state = IMAP_GREETINGS;
-
-    case IMAP_GREETINGS:
-      {
-        /* Swallow the greetings.  */
-        status = imap_readline (f_imap);
-        CHECK_EAGAIN (f_imap, status);
-	f_imap->ptr = f_imap->buffer;
-        MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-	/* Are they open for business ?  The server send an untagged response
-	   for greeting. Tecnically it can be OK/PREAUTH/BYE.  The BYE is
-	   the one that we do not want, server being unfriendly.  */
-	if (mu_c_strncasecmp (f_imap->buffer, "* PREAUTH", 9) == 0)
-	  {
-	    f_imap->state = IMAP_AUTH_DONE;
-	  }
-	else
-	  {
-            if (mu_c_strncasecmp (f_imap->buffer, "* OK", 4) != 0)
-              CHECK_ERROR_CLOSE (folder, f_imap, EACCES);
-            f_imap->state = IMAP_AUTH;
-	  }
-      }
-      if (!f_imap->imaps)
-	tls (folder);
-      
-    case IMAP_AUTH:
-    case IMAP_LOGIN:
-    case IMAP_LOGIN_ACK:
-      assert (folder->authority);
-      {
-	status = mu_authority_authenticate (folder->authority);
-	if (status)
-	  {
-	    /* Fake folder_imap_close into closing the folder.
-	       FIXME: The entire state machine should probably
-	       be revised... */
-	    f_imap->isopen++;
-	    f_imap->state = IMAP_NO_STATE;
-	    folder_imap_close (folder);
-	    return status;
-	  }
-      }
-
-    case IMAP_AUTH_DONE:
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  mu_monitor_wrlock (folder->monitor);
-  f_imap->isopen++;
-  mu_monitor_unlock (folder->monitor);
-  return 0;
-}
-
-
-/* Shutdown the connection.  */
-static int
-folder_imap_close (mu_folder_t folder)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  mu_monitor_wrlock (folder->monitor);
-  f_imap->isopen--;
-  if (f_imap->isopen)
-    {
-      mu_monitor_unlock (folder->monitor);
-      return 0;
-    }
-  mu_monitor_unlock (folder->monitor);
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu LOGOUT\r\n",
-			       (unsigned long) f_imap->seq++);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_LOGOUT;
-
-    case IMAP_LOGOUT:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_LOGOUT_ACK;
-
-    case IMAP_LOGOUT_ACK:
-      /* Check for "* Bye" from the imap server.  */
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      /* This is done when we received the BYE in the parser code.  */
-      /* mu_stream_close (folder->stream); */
-      /* f_imap->isopen = 0 ; */
-
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  f_imap->selected = NULL;
-  return 0;
-}
-
-/* Remove a mailbox.  */
-static int
-folder_imap_delete (mu_folder_t folder, const char *name)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  if (name == NULL)
-    return EINVAL;
-
-  status = mu_folder_open (folder, folder->flags);
-  if (status != 0)
-    return status;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu DELETE %s\r\n",
-			       (unsigned long) f_imap->seq++,
-			       name);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_DELETE;
-
-    case IMAP_DELETE:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_DELETE_ACK;
-
-    case IMAP_DELETE_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-void
-guess_level (struct mu_list_response *resp, size_t prefix_len)
-{
-  size_t lev = 0;
-
-  if (!resp->separator)
-    lev = 0;
-  else
-    {
-      char *p = resp->name + prefix_len;
-      if (p[0] == resp->separator)
-	for ( ; p; p = strchr (p + 1, resp->separator))
-	  lev++;
-    }
-  resp->level = lev;
-}
-
-/* Moves all matching items from list DST to SRC.
-   Items are moved verbatim (i.e. pointers are moved). Non-matching
-   items are deleted. After calling this function, SRC must be
-   destroyed.
-
-   While moving, this function also computes the recursion level.
-   
-   Matching is determined based on PATTERN, by NAMECMP function,
-   and MAX_LEVEL. Both can be zero. */
-   
-static void
-list_copy (mu_list_t dst, mu_list_t src,
-	   size_t prefix_len,
-	   int (*namecmp) (const char* pattern, const char* mailbox),
-	   const char *pattern, size_t max_level)
-{
-  mu_iterator_t itr;
-
-  if (!src)
-    return;
-  
-  mu_list_get_iterator (src, &itr);
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    {
-      char *name;
-      struct mu_list_response *p;
-      mu_iterator_current (itr, (void **)&p);
-      guess_level (p, prefix_len);
-      name = p->name + prefix_len;
-      if (name[0] == p->separator && pattern[0] != p->separator)
-	name++;
-      if ((max_level == 0 || p->level <= max_level)
-	  && (!namecmp || namecmp (pattern, name) == 0))
-	mu_list_append (dst, p);
-      else
-	free (p);
-    }
-  mu_iterator_destroy (&itr);
-  mu_list_set_destroy_item (src, NULL);
-}
-
-/* Convert glob(3)-style pattern to IMAP one
-   Rules:
-     Wildcard          Replace with
-     --------          ------------
-     *                 * for recursive searches, % otherwise
-     ?                 %
-     [..]              %
-
-   NOTE:
-    1. The '*' can be made more selective by taking into account the
-       required maximum recursion level and counting directory separators
-       ('/') in the input pattern.
-    2. The resulting pattern matches, in general, a wider set of strings, so
-       each matched string should be additionally compared against the
-       original pattern.
- */
-char *
-glob_to_imap (const char *pat, int recursive)
-{
-  char *p, *q;
-  char *ret = strdup (pat);
-
-  if (!ret)
-    return NULL;
-
-  for (p = q = ret; *q; )
-    {
-      switch (*q)
-	{
-	case '?':
-	  *p++ = '%';
-	  q++;
-	  break;
-
-	case '*':
-	  *p++ = recursive ? '*' : '%';
-	  q++;
-	  break;
-	  
-	case '[':
-	  for (; *q; q++)
-	    if (*q == '\\')
-	      q++;
-	    else if (*q == ']')
-	      {
-		q++;
-		break;
-	      }
-	  *p++ = '%';
-	  break;
-
-	case '\\':
-	  q++;
-	  if (*q)
-	    *p++ = *q++;
-	  break;
-	  
-	default:
-	  *p++ = *q++;
-	  break;
-	}
-    }
-  *p = 0;
-  return ret;
-}
-
-/* FIXME: Flags unused */
-static int
-folder_imap_list (mu_folder_t folder, const char *ref, void *name,
-		  int flags, size_t max_level,
-		  mu_list_t flist,
-		  mu_folder_enumerate_fp efp, void *edp)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-  char *path = NULL;
-  
-  status = mu_folder_open (folder, folder->flags);
-  if (status != 0)
-    return status;
-
-  if (ref == NULL)
-    ref = "";
-  if (name == NULL)
-    name = "";
-
-  f_imap->enum_fun = efp;
-  f_imap->enum_stop = 0;
-  f_imap->enum_data = edp;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      path = glob_to_imap (name, max_level != 1);
-      status = imap_writeline (f_imap, "g%lu LIST \"%s\" \"%s\"\r\n",
-			       (unsigned long) f_imap->seq++, ref, path);
-      free (path);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_LIST;
-
-    case IMAP_LIST:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_LIST_ACK;
-
-    case IMAP_LIST_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-
-  f_imap->enum_fun = NULL;
-  f_imap->enum_stop = 0;
-  f_imap->enum_data = NULL;
-  
-  list_copy (flist, f_imap->flist, strlen (ref),
-	     imap_mailbox_name_match, name, max_level);
-
-  mu_list_destroy (&f_imap->flist);
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-static int
-folder_imap_lsub (mu_folder_t folder, const char *ref, const char *name,
-		  mu_list_t flist)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  status = mu_folder_open (folder, folder->flags);
-  if (status != 0)
-    return status;
-
-  if (ref == NULL)
-    ref = "";
-  if (name == NULL)
-    name = "";
-
-  f_imap->enum_fun = NULL;
-  f_imap->enum_stop = 0;
-  f_imap->enum_data = NULL;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu LSUB \"%s\" \"%s\"\r\n",
-			       (unsigned long) f_imap->seq++, ref, name);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_LSUB;
-
-    case IMAP_LSUB:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_LSUB_ACK;
-
-    case IMAP_LSUB_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-
-  /* Build the folder list.  */
-  list_copy (flist, f_imap->flist, strlen (ref), NULL, NULL, 0);
-  mu_list_destroy (&f_imap->flist);
-  
-  f_imap->state = IMAP_NO_STATE;
-  return 0;
-}
-
-static int
-folder_imap_rename (mu_folder_t folder, const char *oldpath,
-		    const char *newpath)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  if (oldpath == NULL || newpath == NULL)
-    return EINVAL;
-
-  status = mu_folder_open (folder, folder->flags);
-  if (status != 0)
-    return status;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu RENAME %s %s\r\n",
-			       (unsigned long) f_imap->seq++,
-			       oldpath, newpath);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_RENAME;
-
-    case IMAP_RENAME:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_RENAME_ACK;
-
-    case IMAP_RENAME_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-static int
-folder_imap_subscribe (mu_folder_t folder, const char *name)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  status = mu_folder_open (folder, folder->flags);
-  if (status != 0)
-    return status;
-
-  if (name == NULL)
-    return EINVAL;
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu SUBSCRIBE %s\r\n",
-			       (unsigned long) f_imap->seq++, name);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_SUBSCRIBE;
-
-    case IMAP_SUBSCRIBE:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_SUBSCRIBE_ACK;
-
-    case IMAP_SUBSCRIBE_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-static int
-folder_imap_unsubscribe (mu_folder_t folder, const char *name)
-{
-  f_imap_t f_imap = folder->data;
-  int status = 0;
-
-  status = mu_folder_open (folder, folder->flags);
-  if (status != 0)
-    return status;
-
-  if (name == NULL)
-    return EINVAL;
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu UNSUBSCRIBE %s\r\n",
-			       (unsigned long) f_imap->seq++, name);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_UNSUBSCRIBE;
-
-    case IMAP_UNSUBSCRIBE:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_UNSUBSCRIBE_ACK;
-
-    case IMAP_UNSUBSCRIBE_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (folder->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-/* A literal is a sequence of zero or more octets (including CR and LF),
-   prefix-quoted with an octet count in the form of an open brace ("{"),
-   the number of octets, close brace ("}"), and CRLF.  The sequence is read
-   and put in the string buffer.  */
-static int
-imap_literal_string (f_imap_t f_imap, char **ptr)
-{
-  size_t len, len0, total;
-  int status = 0;
-  int nl;
-
-  if (f_imap->string.nleft==0)
-    {
-      status = imap_readline (f_imap);
-      *ptr = f_imap->buffer;
-      return status;
-    }
-
-  /* The (len + 1) in the for is to count the strip '\r' by imap_readline.  */
-  for (len0 = len = total = 0; total < f_imap->string.nleft; total += len + 1)
-    {
-      status = imap_readline (f_imap);
-      if (DEBUG_SHOW_DATA)
-	fprintf (stderr, "%s", f_imap->buffer);
-      if (status != 0)
-	{
-	  /* Return what we got so far.  */
-	  break;
-	}
-      f_imap->ptr = f_imap->buffer;
-
-      /* How much ?  */
-      len0 = len = f_imap->nl - f_imap->buffer;
-      /* Check if the last read did not finish on a line, if yes do not copy in
-	 string buffer the terminating sequence ")\r\n".  We are doing this
-         by checking if the amount(total) we got so far + the len of the line
-         +1 (taking to account the strip '\r') goes behond the request.  */
-      if ((total + len + 1) > f_imap->string.nleft)
-	{
-	  len0 = len = f_imap->string.nleft - total;
-	  /* ALERT: if we ask for a substring, for example we have :
-	     "123456\n", and ask for body[]<0.7> the server will send
-	     body[] {7} --> "123456\r".  There was not enough space
-	     to fit the nl .. annoying.  Take care of this here.  */
-	  if (f_imap->buffer[len - 1] == '\r')
-	    len0--;
-	}
-
-      mu_stream_write (f_imap->string.stream, f_imap->buffer,
-		    len0, f_imap->string.offset, NULL);
-      f_imap->string.offset += len0;
-
-      /* Depending on the type of request we incremente the xxxx_lines
-	 and  xxxx_sizes.  */
-      nl = (memchr (f_imap->buffer, '\n', len0)) ? 1 : 0;
-      if (f_imap->string.msg_imap)
-	{
-	  switch (f_imap->string.type)
-	    {
-	    case IMAP_HEADER:
-	      f_imap->string.msg_imap->header_lines += nl;
-	      f_imap->string.msg_imap->header_size += len0;
-	      break;
-
-	    case IMAP_BODY:
-	      f_imap->string.msg_imap->body_lines += nl;
-	      f_imap->string.msg_imap->body_size += len0;
-	      break;
-
-	    case IMAP_MESSAGE:
-	      f_imap->string.msg_imap->mu_message_lines += nl;
-	      /* The message size is known by sending RFC822.SIZE.  */
-
-	    default:
-	      break;
-	    }
-	}
-    }
-  f_imap->string.nleft -= total;
-  /* We may have trailing junk like the closing ")\r\n" from a literal string
-     glob it by moving the command buffer, or doing a full readline.  */
-  if (len == (size_t)(f_imap->nl - f_imap->buffer))
-    {
-      len = 0;
-      status = imap_readline (f_imap);
-    }
-  *ptr = f_imap->buffer + len;
-  return status;
-}
-
-/* A quoted string is a sequence of zero or more 7-bit characters,
-   excluding CR and LF, with double quote (<">) characters at each end.
-   Same thing as the literal, diferent format the result is put in the
-   string buffer for the mailbox/callee.  */
-static int
-imap_quoted_string (f_imap_t f_imap, char **ptr)
-{
-  char *bquote;
-  int escaped = 0;
-  int len;
-
-  (*ptr)++;
-  bquote = *ptr;
-  while (**ptr && (**ptr != '"' || escaped))
-    {
-      escaped = (**ptr == '\\') ? 1 : 0;
-      (*ptr)++;
-    }
-
-  len = *ptr - bquote;
-  mu_stream_write (f_imap->string.stream, bquote, len,
-		f_imap->string.offset, NULL);
-  f_imap->string.offset += len;
-  if (**ptr == '"')
-    (*ptr)++;
-  if (DEBUG_SHOW_DATA)
-    fprintf (stderr, "%.*s", len, bquote);
-  return 0;
-}
-
-/* A number consists of one or more digit characters, and represents a
-   numeric value. */
-      
-static int
-imap_digits (f_imap_t f_imap, char **ptr)
-{
-  char *start = *ptr;
-  int len;
-  
-  for (++*ptr; **ptr && mu_isdigit(**ptr); ++*ptr)
-    ;
-  len = *ptr - start;
-  mu_stream_write (f_imap->string.stream, start, len,
-		f_imap->string.offset, NULL);
-  f_imap->string.offset += len;
-  return 0;
-}
-
-/* Find which type of string the response is: literal or quoted and let the
-   function fill the string buffer.  */
-static int
-imap_string (f_imap_t f_imap, char **ptr)
-{
-  int status = 0;
-
-  /* Skip whites.  */
-  while (**ptr == ' ')
-    (*ptr)++;
-  switch (**ptr)
-    {
-    case '{':
-      f_imap->string.nleft = strtol ((*ptr) + 1, ptr, 10);
-      if (**ptr == '}')
-	{
-	  (*ptr)++;
-	  /* Reset the buffer to the beginning.  */
-	  f_imap->ptr = f_imap->buffer;
-	  status = imap_literal_string (f_imap, ptr);
-	}
-      break;
-      
-    case '"':
-      status = imap_quoted_string (f_imap, ptr);
-      break;
-      
-      /* NIL */
-    case 'N':
-    case 'n':
-      (*ptr)++; /* N|n  */
-      (*ptr)++; /* I|i  */
-      (*ptr)++; /* L|l  */
-      break;
-      
-    default:
-      if (mu_isdigit (**ptr))
-	status = imap_digits (f_imap, ptr);
-      else
-	/* Problem. FIXME: Return a more appropriate error code */
-	status = MU_ERR_FAILURE;
-      break;
-    }
-  return status;
-}
-
-/* FIXME: does not work for nonblocking.  */
-static int
-imap_list (f_imap_t f_imap)
-{
-  char *tok;
-  char *sp = NULL;
-  size_t len = f_imap->nl - f_imap->buffer - 1;
-  char *buffer;
-  struct mu_list_response *lr;
-  int status = 0;
-  int argc;
-  char **argv;
-
-  if (f_imap->enum_stop)
-    return 0;
-      
-  buffer = malloc (len + 1);
-  if (!buffer)
-    return ENOMEM;
-  memcpy (buffer, f_imap->buffer, len);
-  buffer[len] = '\0';
-
-  lr = calloc (1, sizeof (*lr));
-  if (!lr)
-    return ENOMEM;
-      
-  if (!f_imap->flist)
-    {
-      mu_list_create (&f_imap->flist);
-      mu_list_set_destroy_item (f_imap->flist, mu_list_response_free);
-    }
-
-  /* Glob untag.  */
-  tok = strtok_r (buffer, " ", &sp);
-  /* Glob LIST.  */
-  tok = strtok_r (NULL, " ", &sp);
-  /* Get the attibutes.  */
-  tok = strtok_r (NULL, ")", &sp);
-  if (tok) 
-    {
-      char *s = NULL;
-      char *p = tok;
-      while ((tok = strtok_r (p, " ()", &s)) != NULL)
-	{
-	  if (mu_c_strcasecmp (tok, "\\Noselect") == 0)
-	    lr->type |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
-	  else if (mu_c_strcasecmp (tok, "\\Noinferiors") == 0)
-	    lr->type |= MU_FOLDER_ATTRIBUTE_FILE;
-	  else if (mu_c_strcasecmp (tok, "\\Marked") == 0
-		   || mu_c_strcasecmp (tok, "\\Unmarked") == 0)
-	    /* nothing */;
-	  else
-	    lr->type |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
-	  p = NULL;
-	}
-    }
-
-  status = mu_argcv_get (sp, "", NULL, &argc, &argv);
-  if (status == 0)
-    {
-      char *s;
-      
-      /* Hiearchy delimeter.  */
-      tok = argv[0];
-      if (tok && tok[1] == 0 && mu_c_strcasecmp (tok, "NIL"))
-	lr->separator = tok[0];
-      /* The path.  */
-      tok = argv[1];
-      s = strchr (tok, '{');
-      if (s)
-	{
-	  size_t n = strtoul (s + 1, NULL, 10);
-	  lr->name = calloc (n + 1, 1);
-	  if (!lr->name)
-	    status = ENOMEM;
-	  else
-	    {
-	      f_imap->ptr = f_imap->buffer;
-	      imap_readline (f_imap);
-	      memcpy (lr->name, f_imap->buffer, n);
-	    }
-	}
-      else if ((status = imap_string (f_imap, &tok)) == 0)
-	{
-	  mu_off_t sz = 0;
-	  
-	  mu_stream_size (f_imap->string.stream, &sz);
-	  lr->name = calloc (sz + 1, 1);
-	  if (!lr->name)
-		status = ENOMEM;
-	  else
-	    mu_stream_read (f_imap->string.stream, lr->name, sz, 0, NULL);
-	  mu_stream_truncate (f_imap->string.stream, 0);
-	  f_imap->string.offset = 0;
-	  f_imap->string.nleft = 0;
-	}
-      else
-	{
-	  lr->name = strdup (tok);
-	  if (!lr->name)
-	    status = ENOMEM;
-	}
-      if (lr->separator)
-	{
-	  size_t off;
-	  char delim[2];
-	  size_t n = 0;
-	  
-	  delim[0] = lr->separator;
-	  delim[1] = 0;
-	  s = lr->name;
-	  while (off = strcspn (s, delim), s[off])
-	    {
-	      n++;
-	      off++;
-	      s += off;
-	    }
-	  lr->level = n;
-	}
-    }
-  mu_argcv_free (argc, argv);
-  free (buffer);
-
-  if (f_imap->enum_fun)
-    f_imap->enum_stop = f_imap->enum_fun (f_imap->folder, lr,
-					  f_imap->enum_data);
-  mu_list_append (f_imap->flist, lr);
-  
-  return status;
-}
-
-/* Helping function to figure out the section name of the message: for example
-   a 2 part message with the first part being sub in two will be:
-   {1}, {1,1} {1,2}  The first subpart of the message and its sub parts
-   {2}  The second subpar of the message.  */
-char *
-section_name (msg_imap_t msg_imap)
-{
-  size_t sectionlen = 0;
-  char *section = strdup ("");
-
-  /* Build the section name, but it is in reverse.  */
-  for (; msg_imap; msg_imap = msg_imap->parent)
-    {
-      if (msg_imap->part != 0)
-	{
-	  char *tmp;
-	  char part[64];
-	  size_t partlen;
-	  snprintf (part, sizeof part, "%lu", (unsigned long) msg_imap->part);
-	  partlen = strlen (part);
-	  tmp = realloc (section, sectionlen + partlen + 2);
-	  if (tmp == NULL)
-	    break;
-	  section = tmp;
-	  memset (section + sectionlen, '\0', partlen + 2);
-	  if (sectionlen != 0)
-	    strcat (section, ".");
-	  strcat (section, part);
-	  sectionlen = strlen (section);
-	}
-    }
-
-  /* Reverse the string.  */
-  if (section)
-    {
-      char *begin, *last;
-      char c;
-      for (begin = section, last = section + sectionlen - 1; begin < last;
-	   begin++, last--)
-	{
-	  c = *begin;
-	  *begin = *last;
-	  *last = c;
-	}
-    }
-  return section;
-}
-
-/* We do not pay particular attention to the content of the bodystructure
-   but rather to the paremetized list layout to discover how many messages
-   the originial message is composed of.  The information is later retrieve
-   when needed via the body[header.fields] command.  Note that this function
-   is recursive. */
-static int
-imap_bodystructure0 (msg_imap_t msg_imap, char **ptr)
-{
-  int paren = 0;
-  int no_arg = 0;
-  int status = 0;
-  int have_size = 0;
-
-  /* Skip space.  */
-  while (**ptr == ' ')
-    (*ptr)++;
-  /* Pass lparen.  */
-  if (**ptr == '(')
-    {
-      ++(*ptr);
-      paren++;
-      no_arg++;
-    }
-  /* NOTE : this loop has side effects in strtol() and imap_string(), the
-     order of the if's are important.  */
-  while (**ptr)
-    {
-      /* Skip the string argument.  */
-      if (**ptr != '(' && **ptr != ')')
-	{
-	  char *start = *ptr;
-	  /* FIXME: set the command callback if EAGAIN to resume.  */
-          status = imap_string (msg_imap->m_imap->f_imap, ptr);
-	  if (status != 0)
-	    return status;
-	  if (start != *ptr)
-	    no_arg = 0;
-	}
-
-      if (mu_isdigit ((unsigned)**ptr))
-	{
-	  char *start = *ptr;
-	  size_t size = strtoul (*ptr, ptr, 10);
-	  if (start != *ptr)
-	    {
-	      if (!have_size && msg_imap && msg_imap->parent)
-		msg_imap->mu_message_size = size;
-	      have_size = 1;
-	      no_arg = 0;
-	    }
-	}
-
-      if (**ptr == '(')
-	{
-	  if (no_arg)
-	    {
-	      msg_imap_t new_part;
-	      msg_imap_t *tmp;
-	      tmp = realloc (msg_imap->parts,
-			     ((msg_imap->num_parts + 1) * sizeof (*tmp)));
-	      if (tmp)
-		{
-		  new_part = calloc (1, sizeof (*new_part));
-		  if (new_part)
-		    {
-		      msg_imap->parts = tmp;
-		      msg_imap->parts[msg_imap->num_parts] = new_part;
-		      new_part->part = ++(msg_imap->num_parts);
-		      new_part->parent = msg_imap;
-		      new_part->num = msg_imap->num;
-		      new_part->m_imap = msg_imap->m_imap;
-		      new_part->flags = msg_imap->flags;
-		      status = imap_bodystructure0 (new_part, ptr);
-		      /* Jump up, the rparen been swallen already.  */
-		      continue;
-		    }
-		  else
-		    {
-		      status = ENOMEM;
-		      free (tmp);
-		      break;
-		    }
-		}
-	      else
-		{
-		  status = ENOMEM;
-		  break;
-		}
-	    }
-	  paren++;
-        }
-
-      if (**ptr == ')')
-        {
-          no_arg = 1;
-          paren--;
-          /* Did we reach the same number of close paren ?  */
-          if (paren <= 0)
-	    {
-	      /* Swallow the rparen.  */
-	      (*ptr)++;
-	      break;
-	    }
-        }
-
-      if (**ptr == '\0')
-	break;
-
-      (*ptr)++;
-    }
-  return status;
-}
-
-static int
-imap_bodystructure (f_imap_t f_imap, char **ptr)
-{
-  return imap_bodystructure0 (f_imap->string.msg_imap, ptr);
-}
-
-/* The Format for a FLAG response is :
-   mailbox_data    ::=  "FLAGS" SPACE flag_list
-   flag_list       ::= "(" #flag ")"
-   flag            ::= "\Answered" / "\Flagged" / "\Deleted" /
-   "\Seen" / "\Draft" / flag_keyword / flag_extension
-   flag_extension  ::= "\" atom
-   ;; Future expansion.  Client implementations
-   ;; MUST accept flag_extension flags.  Server
-   ;; implementations MUST NOT generate
-   ;; flag_extension flags except as defined by
-   ;; future standard or standards-track
-   ;; revisions of this specification.
-   flag_keyword    ::= atom
-
-   S: * 14 FETCH (FLAGS (\Seen \Deleted))
-   S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
-
-   We assume that the '*' or the FLAGS keyword are strip.
-
-   FIXME:  User flags are not take to account. */
-static int
-imap_fetch_flags (f_imap_t f_imap, char **ptr)
-{
-  msg_imap_t msg_imap = f_imap->string.msg_imap;
-  if (msg_imap)
-    imap_flags (ptr, &msg_imap->flags);
-  return 0;
-}
-
-static int
-imap_permanentflags (f_imap_t f_imap, char **ptr)
-{
-  imap_flags (ptr, &f_imap->flags);
-  return 0;
-}
-
-static int
-imap_flags (char **ptr, int *pflags)
-{
-  char *start;
-  char *end;
-  int flags = 0;
-
-  /* Skip space.  */
-  while (**ptr == ' ')
-    (*ptr)++;
-
-  /* Skip LPAREN.  */
-  if (**ptr == '(')
-    (*ptr)++;
-
-  /* Go through the list and break on ')'  */
-  do
-    {
-      /* Skip space before next word.  */
-      while (**ptr == ' ')
-        (*ptr)++;
-
-      /* Save the beginning of the word.  */
-      start = *ptr;
-       /* Get the next word boundary.  */
-      while (**ptr && **ptr != ' ' && **ptr != ')')
-        ++(*ptr);
-
-      /* Save the end for the mu_c_strcasecmp.  */
-      end = *ptr;
-
-      /* Bail out.  */
-      if (*start == '\0')
-        break;
-
-      /* Guess the flag.  */
-      if (end == start)
-	flags |= MU_ATTRIBUTE_SEEN;
-      else
-	{
-	  if (mu_c_strncasecmp (start, "\\Seen", end - start) == 0)
-	    {
-	      flags |= MU_ATTRIBUTE_READ;
-	    }
-	  else if (mu_c_strncasecmp (start, "\\Answered", end - start) == 0)
-	    {
-	      flags |= MU_ATTRIBUTE_ANSWERED;
-	    }
-	  else if (mu_c_strncasecmp (start, "\\Flagged", end - start) == 0)
-	    {
-	      flags |= MU_ATTRIBUTE_FLAGGED;
-	    }
-	  else if (mu_c_strncasecmp (start, "\\Deleted", end - start) == 0)
-	    {
-	      flags |= MU_ATTRIBUTE_DELETED;
-	    }
-	  else if (mu_c_strncasecmp (start, "\\Draft", end - start) == 0)
-	    {
-	      flags |= MU_ATTRIBUTE_DRAFT;
-	    }
-	  else if (mu_c_strncasecmp (start, "\\Recent", end - start))
-	    flags |= MU_ATTRIBUTE_SEEN;
-	}
-    }
-  while (**ptr && **ptr != ')'); /* do {} */
-
-  /* Skip the last rparen.  */
-  if (**ptr == ')')
-    (*ptr)++;
-
-  if (pflags)
-    *pflags = flags;
-  return 0;
-}
-
-static int
-imap_body (f_imap_t f_imap, char **ptr)
-{
-  int status;
-
-  /* Skip leading spaces.  */
-  while (**ptr && **ptr == ' ')
-    (*ptr)++;
-
-  if (**ptr == '[')
-    {
-      char *sep = strchr (*ptr, ']');
-      (*ptr)++; /* Move past the '[' */
-      if (sep)
-	{
-	  size_t len = sep - *ptr;
-	  char *section = malloc (len + 1);
-	  
-	  if (!section)
-	    return ENOMEM;
-	  
-	  strncpy (section, *ptr, len);
-	  section[len] = '\0';
-	  /* strupper.  */
-	  mu_strupper (section);
-	  
-	  /* Set the string type to update the correct line count.  */
-	  /*if (!strstr (section, "FIELD"))*/
-	    {
-              if (strstr (section, "MIME") || (strstr (section, "HEADER")))
-                {
-                  f_imap->string.type = IMAP_HEADER;
-                }
-              else if (strstr (section, "TEXT") || len > 0)
-                {
-                  f_imap->string.type = IMAP_BODY;
-                }
-              else if (len == 0) /* body[]  */
-                {
-                  f_imap->string.type = IMAP_MESSAGE;
-                }
-	    }
-	  free (section);
-	  sep++; /* Move past the ']'  */
-	  *ptr = sep;
-	}
-    }
-  while (**ptr && **ptr == ' ')
-    (*ptr)++;
-  if (**ptr == '<')
-    {
-      char *sep = strchr (*ptr, '>');
-      if (sep)
-	{
-	  sep++;
-	  *ptr = sep;
-	}
-    }
-  status = imap_string (f_imap, ptr);
-
-  /* If the state scan.  Catch it here.  */
-  if (f_imap->state == IMAP_SCAN_ACK)
-    {
-      char *buffer;
-      mu_off_t total = 0;
-      if (f_imap->string.msg_imap && f_imap->string.msg_imap->fheader)
-	mu_header_destroy (&f_imap->string.msg_imap->fheader, NULL);
-      mu_stream_size (f_imap->string.stream, &total);
-      buffer = malloc (total + 1);
-      mu_stream_read (f_imap->string.stream, buffer, total, 0, NULL);
-      status = mu_header_create (&f_imap->string.msg_imap->fheader,
-			      buffer, total, NULL);
-      free (buffer);
-      mu_stream_truncate (f_imap->string.stream, 0);
-      f_imap->string.offset = 0;
-      f_imap->string.nleft = 0;
-    }
-  return status;
-}
-
-static int
-imap_internaldate (f_imap_t f_imap, char **ptr)
-{
-  return imap_string (f_imap, ptr);
-}
-
-static int
-imap_uid (f_imap_t f_imap, char **ptr)
-{
-  char token[128];
-  imap_token (token, sizeof token, ptr);
-  if (f_imap->string.msg_imap)
-    f_imap->string.msg_imap->uid = strtoul (token, NULL, 10);
-  return 0;
-}
-
-static int
-imap_rfc822 (f_imap_t f_imap, char **ptr)
-{
-  int status;
-  f_imap->string.type = IMAP_MESSAGE;
-  status = imap_body (f_imap, ptr);
-  f_imap->string.type = 0;
-  return status;
-}
-
-static int
-imap_rfc822_size (f_imap_t f_imap, char **ptr)
-{
-  char token[128];
-  imap_token (token, sizeof token, ptr);
-  if (f_imap->string.msg_imap)
-    f_imap->string.msg_imap->mu_message_size = strtoul (token, NULL, 10);
-  return 0;
-}
-
-static int
-imap_rfc822_header (f_imap_t f_imap, char **ptr)
-{
-  int status;
-  f_imap->string.type = IMAP_HEADER;
-  status = imap_string (f_imap, ptr);
-  f_imap->string.type = 0;
-  return status;
-}
-
-static int
-imap_rfc822_text (f_imap_t f_imap, char **ptr)
-{
-  int status;
-  f_imap->string.type = IMAP_HEADER;
-  status = imap_string (f_imap, ptr);
-  f_imap->string.type = 0;
-  return status;
-}
-
-/* Parse imap unfortunately FETCH is use as response for many commands.
-   We just use a small set an ignore the other ones :
-   not use  : ALL
-   use      : BODY
-   not use  : BODY[<section>]<<partial>>
-   use      : BODY.PEEK[<section>]<<partial>>
-   not use  : BODYSTRUCTURE
-   not use  : ENVELOPE
-   not use  : FAST
-   use      : FLAGS
-   not use  : FULL
-   use      : INTERNALDATE
-   not use  : RFC822
-   not use  : RFC822.HEADER
-   use      : RFC822.SIZE
-   not use  : RFC822.TEXT
-   not use  : UID
- */
-static int
-imap_fetch (f_imap_t f_imap)
-{
-  char token[128];
-  size_t msgno = 0;
-  m_imap_t m_imap = f_imap->selected;
-  int status = 0;
-  char *sp = NULL;
-
-  /* We should have a mailbox selected.  */
-  assert (m_imap != NULL);
-
-  /* Parse the FETCH respones to extract the pieces.  */
-  sp = f_imap->buffer;
-
-  /* Skip untag '*'.  */
-  imap_token (token, sizeof token, &sp);
-  /* Get msgno.  */
-  imap_token (token, sizeof token, &sp);
-  msgno = strtol (token, NULL,  10);
-  /* Skip FETCH .  */
-  imap_token (token, sizeof token, &sp);
-
-  /* It is actually possible, but higly unlikely that we do not have the
-     message yet, for example a "FETCH (FLAGS (\Recent))" notification
-     for a newly messsage.  */
-  if (f_imap->string.msg_imap == NULL
-      || f_imap->string.msg_imap->num != msgno)
-    {
-      /* Find the imap mesg struct.  */
-      size_t i;
-      mu_message_t msg = NULL;
-      mu_mailbox_get_message (m_imap->mailbox, msgno, &msg);
-      for (i = 0; i < m_imap->imessages_count; i++)
-	{
-	  if (m_imap->imessages[i] && m_imap->imessages[i]->num == msgno)
-	    {
-	      f_imap->string.msg_imap = m_imap->imessages[i];
-	      break;
-	    }
-	}
-      /* mu_message_destroy (&msg);  */
-    }
-
-  while (*sp && *sp != ')')
-    {
-      /* Get the token.  */
-      imap_token (token, sizeof token, &sp);
-
-      if (strncmp (token, "FLAGS", 5) == 0)
-	{
-	  status = imap_fetch_flags (f_imap, &sp);
-	}
-      else if (mu_c_strcasecmp (token, "BODY") == 0)
-	{
-	  if (*sp == '[')
-	    status = imap_body (f_imap, &sp);
-	  else
-	    status = imap_bodystructure (f_imap, &sp);
-	}
-      else if (mu_c_strcasecmp (token, "BODYSTRUCTURE") == 0)
-	{
-	  status = imap_bodystructure (f_imap, &sp);
-	}
-      else if (strncmp (token, "INTERNALDATE", 12) == 0)
-	{
-	  status = imap_internaldate (f_imap, &sp);
-	}
-      else if (strncmp (token, "RFC822", 10) == 0)
-	{
-	  if (*sp == '.')
-	    {
-	      sp++;
-	      imap_token (token, sizeof token, &sp);
-	      if (mu_c_strcasecmp (token, "SIZE") == 0)
-		{
-		  status = imap_rfc822_size (f_imap, &sp);
-		}
-	      else if (mu_c_strcasecmp (token, "TEXT") == 0)
-		{
-		  status = imap_rfc822_text (f_imap, &sp);
-		}
-	      else if (mu_c_strcasecmp (token, "HEADER") == 0)
-		{
-		  status = imap_rfc822_header (f_imap, &sp);
-		}
-	      /* else mu_error (_("not supported RFC822 option"));  */
-	    }
-	  else
-	    {
-	      status = imap_rfc822 (f_imap, &sp);
-	    }
-	}
-      else if (strncmp (token, "UID", 3) == 0)
-	{
-	  status = imap_uid (f_imap, &sp);
-	}
-      /* else mu_error (_("not supported FETCH command"));  */
-    }
-  return status;
-}
-
-static int
-imap_search (f_imap_t f_imap MU_ARG_UNUSED)
-{
-  /* Not implemented.  No provision for this in the API, yet.  */
-  return 0;
-}
-
-static int
-imap_status (f_imap_t f_imap MU_ARG_UNUSED)
-{
-  /* Not implemented.  No provision for this in the API, yet.  */
-  return 0;
-}
-
-static int
-imap_expunge (f_imap_t f_imap MU_ARG_UNUSED, unsigned msgno MU_ARG_UNUSED)
-{
-  /* We should not have this, since do not send the expunge, but rather
-     use SELECT/CLOSE.  */
-  return 0;
-}
-
-
-/* This function will advance ptr to the next character that IMAP
-   recognise as special: " .()[]<>" and put the result in buf which
-   is of size len.  */
-static int
-imap_token (char *buf, size_t len, char **ptr)
-{
-  char *start = *ptr;
-  size_t i;
-  /* Skip leading space.  */
-  while (**ptr && **ptr == ' ')
-    (*ptr)++;
-  /* Break the string by token, when we recognise Atoms we stop.  */
-  for (i = 1; **ptr && i < len; (*ptr)++, buf++, i++)
-    {
-      if (**ptr == ' ' || **ptr == '.'
-	  || **ptr == '(' || **ptr == ')'
-	  || **ptr == '[' || **ptr == ']'
-	  || **ptr == '<' || **ptr  == '>')
-	{
-	  /* Advance.  */
-	  if (start == (*ptr))
-	    (*ptr)++;
-	  break;
-	}
-      *buf = **ptr;
-  }
-  *buf = '\0';
-  /* Skip trailing space.  */
-  while (**ptr && **ptr == ' ')
-    (*ptr)++;
-  return  *ptr - start;;
-}
-
-/* Checks to see if a mailbox name matches a pattern, treating
-   INBOX case insensitively, as required (INBOX is a special
-   name no matter what the case is).
-   */
-static int
-imap_mailbox_name_match (const char* pattern, const char* mailbox)
-{
-  if (mu_c_strcasecmp (pattern, "inbox") == 0)
-    {
-      return mu_c_strcasecmp (pattern, mailbox);
-    }
-  return fnmatch (pattern, mailbox, 0);
-}
-
-/* C99 says that a conforming implementations of snprintf () should return the
-   number of char that would have been call but many GNU/Linux && BSD
-   implementations return -1 on error.  Worse QnX/Neutrino actually does not
-   put the terminal null char.  So let's try to cope.  */
-int
-imap_writeline (f_imap_t f_imap, const char *format, ...)
-{
-  int len;
-  va_list ap;
-  int done = 1;
-
-  va_start(ap, format);
-  do
-    {
-      va_list aq;
-      va_copy(aq, ap);
-      len = vsnprintf (f_imap->buffer, f_imap->buflen, format, aq);
-      va_end(aq);
-      if (len < 0 || len >= (int)f_imap->buflen
-          || !memchr (f_imap->buffer, '\0', len + 1))
-        {
-          f_imap->buflen *= 2;
-          f_imap->buffer = realloc (f_imap->buffer, f_imap->buflen);
-          if (f_imap->buffer == NULL)
-            return ENOMEM;
-          done = 0;
-        }
-      else
-        done = 1;
-    }
-  while (!done);
-  va_end(ap);
-  f_imap->ptr = f_imap->buffer + len;
-
-  if (DEBUG_SHOW_COMMAND)
-    fprintf (stderr, "%s", f_imap->buffer);
-  return 0;
-}
-
-/* Cover to send requests.  */
-int
-imap_send (f_imap_t f_imap)
-{
-  int status = 0;
-  if (f_imap->ptr > f_imap->buffer)
-    {
-      size_t len;
-      size_t n = 0;
-      len = f_imap->ptr - f_imap->buffer;
-      status = mu_stream_write (f_imap->folder->stream, f_imap->buffer, len,
-			     0, &n);
-      if (status == 0)
-        {
-          memmove (f_imap->buffer, f_imap->buffer + n, len - n);
-          f_imap->ptr -= n;
-        }
-    }
-  else
-    f_imap->ptr = f_imap->buffer;
-  return status;
-}
-
-/* Read a complete line form the imap server. Transform CRLF to LF, put a null
-   in the buffer when done.  Note f_imap->offset is not really of any use
-   but rather to keep the stream internal buffer scheme happy, so we have to
-   be in sync with the stream.  */
-int
-imap_readline (f_imap_t f_imap)
-{
-  size_t n = 0;
-  size_t total = f_imap->ptr - f_imap->buffer;
-  int status;
-
-  /* Must get a full line before bailing out.  */
-  do
-    {
-      status = mu_stream_readline (f_imap->folder->stream, f_imap->buffer + total,
-				f_imap->buflen - total,  f_imap->offset, &n);
-      if (status != 0)
-        return status;
-
-      /* The server went away:  It maybe a timeout and some imap server
-	 does not send the BYE.  Consider like an error.  */
-      if (n == 0)
-	return EIO;
-
-      total += n;
-      f_imap->offset += n;
-      f_imap->nl = memchr (f_imap->buffer, '\n', total);
-      if (f_imap->nl == NULL)  /* Do we have a full line.  */
-        {
-          /* Allocate a bigger buffer ?  */
-          if (total >= f_imap->buflen -1)
-            {
-              f_imap->buflen *= 2;
-              f_imap->buffer = realloc (f_imap->buffer, f_imap->buflen + 1);
-              if (f_imap->buffer == NULL)
-                return ENOMEM;
-            }
-        }
-      f_imap->ptr = f_imap->buffer + total;
-    }
-  while (f_imap->nl == NULL);
-
-  /* Conversion \r\n --> \n\0  */
-  /* FIXME: This should be done transparently by the TCP stream */
-  if (f_imap->nl > f_imap->buffer && f_imap->nl[-1] == '\r')
-    {
-      *(f_imap->nl - 1) = '\n';
-      *(f_imap->nl) = '\0';
-      f_imap->ptr = f_imap->nl;
-    }
-  return 0;
-}
-
-/*
-  The parsing was inspired by this article form the BeNews channel: "BE
-  ENGINEERING INSIGHTS: IMAP for the Masses." By Adam Haberlach adam@be.com
-
-  The server responses are in three forms: status responses, server data,
-  and command continuation request, ...
-  An untagged response is indicated by the token "*" instead of a tag.
-  Untagged status responses indicate server greeting, or server status
-  that does not indicate the completion of a command (for example, an
-  impending system shutdown alert).
-  ....
-  The client MUST be prepared to accept any response at all times.
-
-  Status responses are OK, NO, BAD, PREAUTH and BYE.  OK, NO, and BAD
-  may be tagged or untagged.  PREAUTH and BYE are always untagged.
-
-  Server Responses - Status Responses
-  BAD *|tag
-  BYE *
-  NO *|tag
-  OK *|tag
-  PREAUTH *
-
-  The code for status responses are
-  ALERT
-  BADCHARSET(IMAPV)
-  CAPABILITY(IMAPV)
-  NEWNAME
-  PARSE
-  PERMANENTFLAGS
-  READ-ONLY
-  READ-WRITE
-  TRYCREATE
-  UIDNEXT
-  UIDVALIDITY
-  UNSEEN
-
-  Server Responses - Server and Mailbox Status.
-  These responses are always untagged.
-  CAPABILITY *
-  EXISTS *
-  EXPUNGE *
-  FLAGS *
-  FETCH *
-  LIST *
-  LSUB *
-  RECENT *
-  SEARCH *
-  STATUS *
-
-  Server Responses - Command Continuation Request
-  +
-
-*/
-int
-imap_parse (f_imap_t f_imap)
+#ifdef WITH_TLS
+static struct _mu_record _imaps_record =
 {
-  int done = 0;
-  int status = 0;
-  char empty[2];
-  char *buffer = NULL;
-  mu_folder_t folder = f_imap->folder;
-
-  /* We use that moronic hack to not check null for the tockenize strings.  */
-  empty[0] = '\0';
-  empty[1] = '\0';
-  while (! done)
-    {
-      char *tag, *response, *remainder;
-
-      status = imap_readline (f_imap);
-      if (status != 0)
-	{
-	  break;
-	}
-      /* Comment out to see all reading traffic.  */
-      if (DEBUG_SHOW_RESPONSE)
-	mu_error ("\t\t%s", f_imap->buffer);
-
-      /* We do not want to step over f_imap->buffer since it can be use
-	 further down the chain.  */
-      if (buffer)
-	{
-	  free (buffer);
-	  buffer = NULL;
-	}
-      buffer = calloc ((f_imap->ptr - f_imap->buffer) + 1, 1);
-      memcpy (buffer, f_imap->buffer, (f_imap->ptr - f_imap->buffer));
-
-      /* Tokenize the string.  */
-      {
-	char *sp = NULL;
-	tag = strtok_r (buffer, " ", &sp);
-	response = strtok_r (NULL, " ", &sp);
-	if (!response)
-	  response = empty;
-	remainder = strtok_r (NULL, "\n", &sp);
-	if (!remainder)
-	  remainder = empty;
-      }
-
-      if (!tag)
-	{
-	  /* Just in case */
-	  mu_error (_("no tag in response: %s %s"), response, remainder);
-	  status = MU_ERR_FAILURE;
-	}
-      /* Is the response untagged ?  */
-      else if (tag[0] == '*')
-	{
-	  MU_DEBUG2 (folder->debug, MU_DEBUG_PROT, "* %s %s\n",
-	             response, remainder);
-	  /* Is it a Status Response.  */
-	  if (mu_c_strcasecmp (response, "OK") == 0)
-	    {
-	      /* Check for status response [code].  */
-	      if (*remainder == '[')
-		{
-		  char *cruft, *subtag;
-		  char *sp = NULL, *sp1;
-		  remainder++;
-		  cruft = strtok_r (remainder, "]", &sp);
-		  if (!cruft) cruft = empty;
-		  subtag = strtok_r (cruft, " ", &sp1);
-		  if (!subtag) subtag = empty;
-
-		  if (mu_c_strcasecmp (subtag, "ALERT") == 0)
-		    {
-		      /* The human-readable text contains a special alert that
-			 MUST be presented to the user in a fashion that calls
-			 the user's attention to the message.  */
-		      mu_error (_("ALERT: %s"), (sp) ? sp : "");
-		    }
-		  else if (mu_c_strcasecmp (subtag, "BADCHARSET") == 0)
-		    {
-		      /* Optionally followed by a parenthesized list of
-			 charsets.  A SEARCH failed because the given charset
-			 is not supported by this implementation.  If the
-			 optional list of charsets is given, this lists the
-			 charsets that are supported by this implementation. */
-		      mu_error (_("BAD CHARSET: %s"), (sp) ? sp : "");
-		    }
-		  else if (mu_c_strcasecmp (subtag, "CAPABILITY") == 0)
-		    {
-		      /* Followed by a list of capabilities.  This can appear
-			 in the initial OK or PREAUTH response to transmit an
-			 initial capabilities list.  This makes it unnecessary
-			 for a client to send a separate CAPABILITY command if
-			 it recognizes this response.  */
-		      parse_capa (f_imap, cruft);
-		    }
-		  else if (mu_c_strcasecmp (subtag, "NEWNAME") == 0)
-		    {
-		      /* Followed by a mailbox name and a new mailbox name.  A
-			 SELECT or EXAMINE failed because the target mailbox
-			 name (which once existed) was renamed to the new
-			 mailbox name.  This is a hint to the client that the
-			 operation can succeed if the SELECT or EXAMINE is
-			 reissued with the new mailbox name. */
-		      mu_error ("NEWNAME: %s", (sp) ? sp : "");
-		    }
-		  else if (mu_c_strcasecmp (subtag, "PARSE") == 0)
-		    {
-		      /* The human-readable text represents an error in
-			 parsing the [RFC-822] header or [MIME-IMB] headers
-			 of a message in the mailbox.  */
-		      mu_error ("PARSE: %s", (sp) ? sp : "");
-		    }
-		  else if (mu_c_strcasecmp (subtag, "PERMANENTFLAGS") == 0)
-		    {
-		      /* Followed by a parenthesized list of flags, indicates
-			 which of the known flags that the client can change
-			 permanently.  Any flags that are in the FLAGS
-			 untagged response, but not the PERMANENTFLAGS list,
-			 can not be set permanently.  If the client attempts
-			 to STORE a flag that is not in the PERMANENTFLAGS
-			 list, the server will either ignore the change or
-			 store the state change for the remainder of the
-			 current session only. The PERMANENTFLAGS list can
-			 also include the special flag \*, which indicates
-			 that it is possible to create new keywords by
-			 attempting to store those flags in the mailbox.  */
-		    }
-		  else if (mu_c_strcasecmp (subtag, "READ-ONLY") == 0)
-		    {
-		      /* The mailbox is selected read-only, or its access
-			 while selected has changed from read-write to
-			 read-only.  */
-		    }
-		  else if (mu_c_strcasecmp (subtag, "READ-WRITE") == 0)
-		    {
-		      /* The mailbox is selected read-write, or its access
-			 while selected has changed from read-only to
-			 read-write.  */
-		    }
-		  else if (mu_c_strcasecmp (subtag, "TRYCREATE") == 0)
-		    {
-		      /* An APPEND or COPY attempt is failing because the
-			 target mailbox does not exist (as opposed to some
-			 other reason).  This is a hint to the client that
-			 the operation can succeed if the mailbox is first
-			 created by the CREATE command.  */
-		      mu_error ("TRYCREATE: %s", (sp) ? sp : "");
-		    }
-		  else if (mu_c_strcasecmp (subtag, "UIDNEXT") == 0)
-		    {
-		      /* Followed by a decimal number, indicates the next
-			 unique identifier value.  Refer to section 2.3.1.1
-			 for more information.  */
-		      char *value = strtok_r (NULL, " ", &sp1);
-		      if (value)
-			f_imap->selected->uidnext = strtol (value, NULL, 10);
-		    }
-		  else if (mu_c_strcasecmp (subtag, "UIDVALIDITY") == 0)
-		    {
-		      /* Followed by a decimal number, indicates the unique
-			 identifier validity value.  Refer to section 2.3.1.1
-			 for more information.  */
-		      char *value = strtok_r (NULL, " ", &sp1);
-		      if (value)
-			f_imap->selected->uidvalidity = strtol (value,
-								NULL, 10);
-		    }
-		  else if (mu_c_strcasecmp (subtag, "UNSEEN") == 0)
-		    {
-		      /* Followed by a decimal number, indicates the number of
-			 the first message without the \Seen flag set.  */
-		      char *value = strtok_r (NULL, " ", &sp1);
-		      if (value)
-			f_imap->selected->unseen = strtol (value, NULL, 10);
-		    }
-		  else
-		    {
-		      /* Additional response codes defined by particular
-			 client or server implementations SHOULD be prefixed
-			 with an "X" until they are added to a revision of
-			 this protocol.  Client implementations SHOULD ignore
-			 response codes that they do not recognize.  */
-		    }
-		} /* End of code.  */
-	      else
-		{
-		  /* Not sure why we would get an untagged ok...but we do... */
-		  /* Still should we be verbose about is ? */
-		  mu_error (_("untagged OK response: %s"), remainder);
-		}
-	    }
-	  else if (mu_c_strcasecmp (response, "NO") == 0)
-	    {
-	      /* This does not mean failure but rather a strong warning.  */
-	      mu_error (_("untagged NO response: %s"), remainder);
-	    }
-	  else if (mu_c_strcasecmp (response, "BAD") == 0)
-	    {
-	      /* We're dead, protocol/syntax error.  */
-	      mu_error (_("untagged BAD response: %s"), remainder);
-	    }
-	  else if (mu_c_strcasecmp (response, "PREAUTH") == 0)
-	    {
-	      /* Should we be dealing with this?  */
-	    }
-	  else if (mu_c_strcasecmp (response, "BYE") == 0)
-	    {
-	      /* We should close the stream. This is not recoverable.  */
-	      done = 1;
-	      mu_monitor_wrlock (f_imap->folder->monitor);
-	      f_imap->isopen = 0;
-	      f_imap->selected = NULL;
-	      mu_monitor_unlock (f_imap->folder->monitor);
-	      mu_stream_close (f_imap->folder->stream);
-	    }
-	  else if (mu_c_strcasecmp (response, "CAPABILITY") == 0)
-	    {
-	      parse_capa (f_imap, remainder);
-	    }
-	  else if (mu_c_strcasecmp (remainder, "EXISTS") == 0)
-	    {
-	      f_imap->selected->messages_count = strtol (response, NULL, 10);
-	    }
-	  else if (mu_c_strcasecmp (remainder, "EXPUNGE") == 0)
-	    {
-	      unsigned int msgno = strtol (response, NULL, 10);
-	      status = imap_expunge (f_imap, msgno);
-	    }
-	  else if (mu_c_strncasecmp (remainder, "FETCH", 5) == 0)
-	    {
-	      status = imap_fetch (f_imap);
-	      if (status != 0)
-		break;
-	    }
-	  else if (mu_c_strcasecmp (response, "FLAGS") == 0)
-	    {
-	      /* Flags define on the mailbox not a message flags.  */
-	      status = imap_permanentflags (f_imap, &remainder);
-	    }
-	  else if (mu_c_strcasecmp (response, "LIST") == 0)
-	    {
-	      status = imap_list (f_imap);
-	    }
-	  else if (mu_c_strcasecmp (response, "LSUB") == 0)
-	    {
-	      status = imap_list (f_imap);
-	    }
-	  else if (mu_c_strcasecmp (remainder, "RECENT") == 0)
-	    {
-	      f_imap->selected->recent = strtol (response, NULL, 10);
-	    }
-	  else if (mu_c_strcasecmp (response, "SEARCH") == 0)
-	    {
-	      status = imap_search (f_imap);
-	    }
-	  else if (mu_c_strcasecmp (response, "STATUS") == 0)
-	    {
-	      status = imap_status (f_imap);
-	    }
-	  else
-	    {
-	      /* Once again, check for something strange.  */
-	      mu_error (_("unknown untagged response: \"%s\"  %s"),
-			response, remainder);
-	    }
-	}
-      /* Continuation token ???.  */
-      else if (tag[0] == '+')
-	{
-	  done = 1;
-	}
-      else
-	{
-	  /* Every transaction ends with a tagged response.  */
-	  done = 1;
-	  if (mu_c_strcasecmp (response, "OK") == 0)
-	    {
-	      /* Cool we are doing ok.  */
-	    }
-	  else if (mu_c_strcasecmp (response, "NO") == 0)
-	    {
-	      if (mu_c_strncasecmp (remainder, "LOGIN", 5) == 0)
-		{
-		  mu_observable_t observable = NULL;
-		  mu_folder_get_observable (f_imap->folder, &observable);
-		  mu_observable_notify (observable, MU_EVT_FOLDER_AUTHORITY_FAILED,
-					NULL);
-		  status = MU_ERR_AUTH_FAILURE;
-		}
-	      else if (mu_c_strncasecmp (remainder, "LIST", 4) == 0)
-		status = MU_ERR_NOENT;
-	      else
-		status = MU_ERR_FAILURE;
-	    }
-	  else /* NO and BAD */
-	    {
-	      status = EINVAL;
-	      mu_error (_("NO or BAD tagged response: %s %s %s"),
-			tag, response, remainder);
-	    }
-	}
-      f_imap->ptr = f_imap->buffer;
-    }
-
-  if (buffer)
-    free (buffer);
-  return status;
-}
-
+  MU_IMAP_PRIO,
+  MU_IMAPS_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
+  MU_URL_HOST,
+  _mu_imaps_url_init,     /* url entry.  */
+  _mu_imaps_mailbox_init, /* Mailbox entry.  */
+  NULL,                /* Mailer entry.  */
+  _mu_imap_folder_init,   /* Folder entry.  */
+  NULL, /* No need for a back pointer.  */
+  NULL, /* _is_scheme method.  */
+  NULL, /* _get_url method.  */
+  NULL, /* _get_mailbox method.  */
+  NULL, /* _get_mailer method.  */
+  NULL  /* _get_folder method.  */
+};
+mu_record_t mu_imaps_record = &_imaps_record;
 #else
-#include <stdio.h>
-#include <mailutils/sys/registrar.h>
-mu_record_t mu_imap_record = NULL;
 mu_record_t mu_imaps_record = NULL;
-#endif /* ENABLE_IMAP */
+#endif /* WITH_TLS */
diff --git a/libproto/imap/genlist.c b/libproto/imap/genlist.c
index 5931963..4feeb1c 100644
--- a/libproto/imap/genlist.c
+++ b/libproto/imap/genlist.c
@@ -104,13 +104,15 @@ list_untagged_handler (mu_imap_t imap, mu_list_t resp, void *data)
 	}
 
       elt = _mu_imap_list_at (resp, 2);
-      if (!(elt && elt->type == imap_eltype_string))
+      if (!elt)
 	return;
-      if (mu_c_strcasecmp (elt->v.string, "NIL") == 0)
+      if (_mu_imap_list_element_is_nil (elt))
 	{
 	  rp->separator = 0;
 	  rp->level = 0;
 	}
+      else if (elt->type != imap_eltype_string)
+	return;
       else
 	{
 	  rp->separator = elt->v.string[0];
@@ -131,6 +133,9 @@ mu_imap_genlist (mu_imap_t imap, int lsub,
   struct list_closure clos;
   int rc;
 
+  if (!refname || !mboxname)
+    return EINVAL;
+  
   argv[0] = lsub ? "LSUB" : "LIST";
   argv[1] = refname;
   argv[2] = mboxname;
diff --git a/libproto/imap/login.c b/libproto/imap/login.c
index 807d7b4..efdfb4b 100644
--- a/libproto/imap/login.c
+++ b/libproto/imap/login.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <mailutils/errno.h>
 #include <mailutils/stream.h>
+#include <mailutils/secret.h>
 #include <mailutils/sys/imap.h>
 
 int
@@ -77,3 +78,12 @@ mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
   return status;
 }
 
+int
+mu_imap_login_secret (mu_imap_t imap, const char *user, mu_secret_t secret)
+{
+  int rc = mu_imap_login (imap, user, mu_secret_password (secret));
+  mu_secret_password_unref (secret);
+  return rc;
+}
+  
+  
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
deleted file mode 100644
index 87cc10c..0000000
--- a/libproto/imap/mbox.c
+++ /dev/null
@@ -1,2142 +0,0 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2009,
-   2010, 2011 Free Software Foundation, Inc.
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see
-   <http://www.gnu.org/licenses/>. */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#ifdef ENABLE_IMAP
-
-#include <errno.h>
-#include <string.h>
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#include <stdlib.h>
-#include <assert.h>
-#include <time.h>
-
-#include <mailutils/address.h>
-#include <mailutils/attribute.h>
-#include <mailutils/body.h>
-#include <mailutils/debug.h>
-#include <mailutils/envelope.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
-#include <mailutils/header.h>
-#include <mailutils/message.h>
-#include <mailutils/util.h>
-#include <mailutils/observer.h>
-#include <mailutils/property.h>
-#include <mailutils/stream.h>
-#include <mailutils/io.h>
-
-#include <mailutils/sys/imap.h>
-#include <mailutils/sys/mailbox.h>
-#include <mailutils/sys/registrar.h>
-#include <mailutils/sys/url.h>
-
-#undef min
-#define min(a,b) ((a) < (b) ? (a) : (b))
-
-#define MU_IMAP_CACHE_HEADERS "Bcc Cc Content-Language Content-Transfer-Encoding Content-Type Date From In-Reply-To Message-ID Reference Reply-To Sender Subject To X-UIDL"
-
-/* mu_mailbox_t API.  */
-static void mailbox_imap_destroy  (mu_mailbox_t);
-static int  mailbox_imap_open     (mu_mailbox_t, int);
-static int  mailbox_imap_close    (mu_mailbox_t);
-static int  imap_uidvalidity      (mu_mailbox_t, unsigned long *);
-static int  imap_uidnext          (mu_mailbox_t, size_t *);
-static int  imap_expunge          (mu_mailbox_t);
-static int  imap_get_message      (mu_mailbox_t, size_t, mu_message_t *);
-static int  imap_messages_count   (mu_mailbox_t, size_t *);
-static int  imap_messages_recent  (mu_mailbox_t, size_t *);
-static int  imap_message_unseen   (mu_mailbox_t, size_t *);
-static int  imap_scan             (mu_mailbox_t, size_t, size_t *);
-static int  imap_scan0            (mu_mailbox_t, size_t, size_t *, int);
-static int  imap_is_updated       (mu_mailbox_t);
-static int  imap_append_message   (mu_mailbox_t, mu_message_t);
-static int  imap_append_message0  (mu_mailbox_t, mu_message_t);
-static int  imap_copy_message     (mu_mailbox_t, mu_message_t);
-
-/* mu_message_t API.  */
-static int  imap_submessage_size  (msg_imap_t, size_t *);
-static int  imap_message_size     (mu_message_t, size_t *);
-static int  imap_message_lines    (mu_message_t, size_t *);
-static int  imap_message_get_transport2  (mu_stream_t, mu_transport_t *pin, 
-                                          mu_transport_t *pout);
-static int  imap_message_read     (mu_stream_t , char *, size_t, mu_off_t, size_t *);
-static int  imap_message_uid      (mu_message_t, size_t *);
-
-/* mu_mime_t API.  */
-static int  imap_is_multipart     (mu_message_t, int *);
-static int  imap_get_num_parts    (mu_message_t, size_t *);
-static int  imap_get_part         (mu_message_t, size_t, mu_message_t *);
-
-/* mu_envelope_t API  */
-static int  imap_envelope_sender  (mu_envelope_t, char *, size_t, size_t *);
-static int  imap_envelope_date    (mu_envelope_t, char *, size_t, size_t *);
-
-/* mu_attribute_t API  */
-static int  imap_attr_get_flags   (mu_attribute_t, int *);
-static int  imap_attr_set_flags   (mu_attribute_t, int);
-static int  imap_attr_unset_flags (mu_attribute_t, int);
-
-/* mu_header_t API.  */
-static int  imap_header_read      (mu_header_t, char*, size_t, mu_off_t, size_t *);
-
-/* mu_body_t API.  */
-static int  imap_body_read        (mu_stream_t, char *, size_t, mu_off_t, size_t *);
-static int  imap_body_size        (mu_body_t, size_t *);
-static int  imap_body_lines       (mu_body_t, size_t *);
-static int  imap_body_get_transport2 (mu_stream_t, mu_transport_t *pin, mu_transport_t *pout);
-
-/* Helpers.  */
-static int  imap_get_transport2    (msg_imap_t msg_imap, 
-                                    mu_transport_t *pin,
-                                    mu_transport_t *pout);
-static int  imap_get_message0     (msg_imap_t, mu_message_t *);
-static int  fetch_operation       (f_imap_t, msg_imap_t, char *, size_t, size_t *);
-static void free_subparts         (msg_imap_t);
-static int  flags_to_string       (char **, int);
-static int  delete_to_string      (m_imap_t, char **);
-static int  is_same_folder        (mu_mailbox_t, mu_message_t);
-
-#define MBX_WRITABLE(mbx) ((mbx)->flags & (MU_STREAM_WRITE|MU_STREAM_RDWR|MU_STREAM_CREAT))
-
-/* Initialize the concrete object mu_mailbox_t by overloading the function of the
-   structure.  */
-int
-_mailbox_imap_and_imaps_init (mu_mailbox_t mailbox, int imaps)
-{
-  int status;
-  m_imap_t m_imap;
-
-  if (!mailbox)
-    return EINVAL;
-  if (mailbox->folder == NULL)
-    return EINVAL;
-  
-  m_imap = mailbox->data = calloc (1, sizeof (*m_imap));
-  if (m_imap == NULL)
-    return ENOMEM;
-
-  /* Retrieve the name of the mailbox from the URL.  */
-  status = mu_url_aget_path (mailbox->url, &m_imap->name);
-  if (status == MU_ERR_NOENT)
-    {
-      m_imap->name = strdup ("INBOX");
-      if (!m_imap->name)
-        return ENOMEM;
-    }
-  else if (status)
-    return status;
-
-  /* Overload the functions.  */
-  mailbox->_destroy = mailbox_imap_destroy;
-
-  mailbox->_open = mailbox_imap_open;
-  mailbox->_close = mailbox_imap_close;
-
-  /* Messages.  */
-  mailbox->_get_message = imap_get_message;
-  mailbox->_append_message = imap_append_message;
-  mailbox->_messages_count = imap_messages_count;
-  mailbox->_messages_recent = imap_messages_recent;
-  mailbox->_message_unseen = imap_message_unseen;
-  mailbox->_expunge = imap_expunge;
-  mailbox->_uidvalidity = imap_uidvalidity;
-  mailbox->_uidnext = imap_uidnext;
-
-  mailbox->_scan = imap_scan;
-  mailbox->_is_updated = imap_is_updated;
-
-  /* Get the back pointer of the concrete folder. */
-  m_imap->f_imap = mailbox->folder->data;
-  m_imap->f_imap->imaps = imaps;
-  
-  /* maibox back pointer.  */
-  m_imap->mailbox = mailbox;
-
-  /* Set our properties.  */
-  {
-    mu_property_t property = NULL;
-    mu_mailbox_get_property (mailbox, &property);
-    mu_property_set_value (property, "TYPE", "IMAP4", 1);
-  }
-
-  return 0;
-}
-
-int
-_mailbox_imap_init (mu_mailbox_t mailbox)
-{
-  return _mailbox_imap_and_imaps_init (mailbox, 0);
-}
-
-int
-_mailbox_imaps_init (mu_mailbox_t mailbox)
-{
-  return _mailbox_imap_and_imaps_init (mailbox, 1);
-}
-
-
-/* Recursive call to free all the subparts of a message.  */
-static void
-free_subparts (msg_imap_t msg_imap)
-{
-  size_t i;
-  for (i = 0; i < msg_imap->num_parts; i++)
-    {
-      if (msg_imap->parts[i])
-	free_subparts (msg_imap->parts[i]);
-    }
-
-  if (msg_imap->message)
-    mu_message_destroy (&(msg_imap->message), msg_imap);
-  if (msg_imap->parts)
-    free (msg_imap->parts);
-  if (msg_imap->fheader)
-    mu_header_destroy (&msg_imap->fheader, NULL);
-  if (msg_imap->internal_date)
-    free (msg_imap->internal_date);
-  free(msg_imap);
-}
-
-/* Give back all the resources. But it does not mean to shutdown the channel
-   this is done on the folder.  */
-static void
-mailbox_imap_destroy (mu_mailbox_t mailbox)
-{
-  if (mailbox->data)
-    {
-      m_imap_t m_imap = mailbox->data;
-      f_imap_t f_imap = m_imap->f_imap;
-      size_t i;
-
-      /* Deselect.  */
-      if (m_imap != f_imap->selected)
-	f_imap->selected = NULL;
-
-      mu_monitor_wrlock (mailbox->monitor);
-      /* Destroy the imap messages and ressources associated to them.  */
-      for (i = 0; i < m_imap->imessages_count; i++)
-	{
-	  if (m_imap->imessages[i])
-	    free_subparts (m_imap->imessages[i]);
-	}
-      if (m_imap->imessages)
-	free (m_imap->imessages);
-      if (m_imap->name)
-	free (m_imap->name);
-      free (m_imap);
-      mailbox->data = NULL;
-      mu_monitor_unlock (mailbox->monitor);
-    }
-}
-
-/* If the connection was not up it is open by the folder since the stream
-   socket is actually created by the folder.  It is not necessary
-   to set select the mailbox right away, there are maybe on going operations.
-   But on any operation by a particular mailbox, it will be selected first.  */
-static int
-mailbox_imap_open (mu_mailbox_t mailbox, int flags)
-{
-  int status = 0;
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-  mu_folder_t folder = f_imap->folder;
-  mu_list_t folders = NULL;
-  size_t count;
-  
-  /* m_imap must have been created during mailbox initialization. */
-  assert (mailbox->data);
-  assert (m_imap->name);
-
-  mailbox->flags = flags;
-
-  if ((status = mu_folder_open (mailbox->folder, flags)))
-    return status;
-
-  /* We might not have to SELECT the mailbox, but we need to know it
-     exists, and CREATE it if it doesn't, and CREATE is specified in
-     the flags.
-   */
-
-  switch (m_imap->state)
-    {
-    case IMAP_NO_STATE:
-      m_imap->state = IMAP_LIST;
-
-    case IMAP_LIST:
-      status = mu_folder_list (folder, NULL, m_imap->name, 0, &folders);
-      if (status != 0)
-	{
-	  if (status != EAGAIN && status != EINPROGRESS && status != EINTR)
-	    m_imap->state = IMAP_NO_STATE;
-
-	  return status;
-	}
-      m_imap->state = IMAP_NO_STATE;
-      status = mu_list_count (folders, &count);
-      mu_list_destroy (&folders);
-      if (status || count)
-	return 0;
-
-      if ((flags & MU_STREAM_CREAT) == 0)
-	return ENOENT;
-
-      m_imap->state = IMAP_CREATE;
-
-    case IMAP_CREATE:
-      switch (f_imap->state)
-	{
-	case IMAP_NO_STATE:
-	  {
-	    const char *path;
-	    status = mu_url_sget_path (folder->url, &path);
-	    if (status == MU_ERR_NOENT)
-	      return 0;
-	    else if (status)
-	      return status;
-	    status = imap_writeline (f_imap, "g%lu CREATE %s\r\n",
-				     (unsigned long) f_imap->seq, path);
-	    MU_DEBUG2 (folder->debug, MU_DEBUG_PROT, "g%lu CREATE %s\n",
-		       (unsigned long) f_imap->seq, path);
-	    f_imap->seq++;
-	    if (status != 0)
-	      {
-		m_imap->state = f_imap->state = IMAP_NO_STATE;
-		return status;
-	      }
-	    f_imap->state = IMAP_CREATE;
-	  }
-
-	case IMAP_CREATE:
-	  status = imap_send (f_imap);
-	  if (status != 0)
-	    {
-	      if (status != EAGAIN && status != EINPROGRESS
-		  && status != EINTR)
-		m_imap->state = f_imap->state = IMAP_NO_STATE;
-
-	      return status;
-	    }
-	  f_imap->state = IMAP_CREATE_ACK;
-
-	case IMAP_CREATE_ACK:
-	  status = imap_parse (f_imap);
-	  if (status != 0)
-	    {
-	      if (status == EINVAL)
-		status = EACCES;
-
-	      if (status != EAGAIN && status != EINPROGRESS
-		  && status != EINTR)
-		m_imap->state = f_imap->state = IMAP_NO_STATE;
-
-	      return status;
-	    }
-	  f_imap->state = IMAP_NO_STATE;
-	  break;
-
-	default:
-	  status = EINVAL;
-	  break;
-	}
-      m_imap->state = IMAP_NO_STATE;
-      break;
-
-    default:
-      status = EINVAL;
-      break;
-    }
-
-  return status;
-}
-
-/* We can not close the folder in term of shuting down the connection but if
-   we were the selected mailbox we send the close and deselect ourself.
-   The CLOSE is also use to expunge instead of sending expunge.  */
-static int
-mailbox_imap_close (mu_mailbox_t mailbox)
-{
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;
-  
-  /* If we are not the selected mailbox, just close the stream.  */
-  if (m_imap != f_imap->selected)
-    return mu_folder_close (mailbox->folder);
-
-  /* Select first.  */
-  status = imap_messages_count (mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu CLOSE\r\n",
-			       (unsigned long) f_imap->seq++);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_CLOSE;
-
-    case IMAP_CLOSE:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_CLOSE_ACK;
-
-    case IMAP_CLOSE_ACK:
-      {
-	size_t i;
-	status = imap_parse (f_imap);
-	CHECK_EAGAIN (f_imap, status);
-	MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-	mu_monitor_wrlock (mailbox->monitor);
-	/* Destroy the imap messages and ressources associated to them.  */
-	for (i = 0; i < m_imap->imessages_count; i++)
-	  {
-	    if (m_imap->imessages[i])
-	      free_subparts (m_imap->imessages[i]);
-	  }
-	if (m_imap->imessages)
-	  free (m_imap->imessages);
-	m_imap->imessages = NULL;
-	m_imap->imessages_count = 0;
-	m_imap->messages_count = 0;
-	m_imap->recent = 0;
-	m_imap->unseen = 0;
-	/* Clear the callback string structure.  */
-	mu_stream_truncate (f_imap->string.stream, 0);
-	f_imap->string.offset = 0;
-	f_imap->string.nleft = 0;
-	f_imap->string.type = IMAP_NO_STATE;
-	f_imap->string.msg_imap = NULL;
-	mu_monitor_unlock (mailbox->monitor);
-      }
-      break;
-
-    default:
-      /* mu_error ("imap_close unknown state: reconnect\n");*/
-      break;
-    }
-
-  /* Deselect.  */
-  f_imap->selected = NULL;
-
-  f_imap->state = IMAP_NO_STATE;
-  return mu_folder_close (mailbox->folder);
-}
-
-/* Construction of the mu_message_t, nothing else is done then this setup.  To
-   clarify this is different from say mu_message_get_part().  This call is for the
-   mailbox and we are setting up the mu_message_t structure.  */
-static int
-imap_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
-{
-  m_imap_t m_imap = mailbox->data;
-  msg_imap_t msg_imap;
-  int status = 0;
-
-  if (pmsg == NULL)
-    return MU_ERR_OUT_PTR_NULL;
-  if (msgno == 0 || msgno > m_imap->messages_count)
-    return EINVAL;
-
-  /* Check to see if we have already this message.  */
-  mu_monitor_rdlock (mailbox->monitor);
-  {
-    size_t i;
-    for (i = 0; i < m_imap->imessages_count; i++)
-      {
-	if (m_imap->imessages[i])
-	  {
-	    if (m_imap->imessages[i]->num == msgno)
-	      {
-		*pmsg = m_imap->imessages[i]->message;
-		mu_monitor_unlock (mailbox->monitor);
-		return 0;
-	      }
-	  }
-      }
-  }
-  mu_monitor_unlock (mailbox->monitor);
-
-  /* Allocate a concrete imap message.  */
-  msg_imap = calloc (1, sizeof *msg_imap);
-  if (msg_imap == NULL)
-    return ENOMEM;
-  /* Back pointer.  */
-  msg_imap->m_imap = m_imap;
-  msg_imap->num = msgno;
-  status = imap_get_message0 (msg_imap, pmsg);
-  if (status == 0)
-    {
-      /* Add it to the list.  */
-      mu_monitor_wrlock (mailbox->monitor);
-      {
-	msg_imap_t *m ;
-	m = realloc (m_imap->imessages,
-		     (m_imap->imessages_count + 1) * sizeof *m);
-	if (m == NULL)
-	  {
-	    mu_message_destroy (pmsg, msg_imap);
-	    mu_monitor_unlock (mailbox->monitor);
-	    return ENOMEM;
-	  }
-	m_imap->imessages = m;
-	m_imap->imessages[m_imap->imessages_count] = msg_imap;
-	m_imap->imessages_count++;
-      }
-      mu_monitor_unlock (mailbox->monitor);
-
-      msg_imap->message = *pmsg;
-    }
-  else
-    free (msg_imap);
-  return status;
-}
-
-/* Set all the mu_message_t functions and parts.  */
-static int
-imap_get_message0 (msg_imap_t msg_imap, mu_message_t *pmsg)
-{
-  int status = 0;
-  mu_message_t msg = NULL;
-  mu_mailbox_t mailbox = msg_imap->m_imap->mailbox;
-
-  /* Create the message and its stream.  */
-  {
-    mu_stream_t stream = NULL;
-    if ((status = mu_message_create (&msg, msg_imap)) != 0
-        || (status = mu_stream_create (&stream, mailbox->flags, msg)) != 0)
-      {
-        mu_stream_destroy (&stream, msg);
-        mu_message_destroy (&msg, msg_imap);
-        return status;
-      }
-    mu_stream_setbufsiz (stream, 128);
-    mu_stream_set_read (stream, imap_message_read, msg);
-    mu_stream_set_get_transport2 (stream, imap_message_get_transport2, msg);
-    mu_message_set_stream (msg, stream, msg_imap);
-    mu_message_set_size (msg, imap_message_size, msg_imap);
-    mu_message_set_lines (msg, imap_message_lines, msg_imap);
-  }
-
-  /* Create the header.  */
-  {
-    mu_header_t header = NULL;
-    if ((status = mu_header_create (&header, NULL, 0,  msg)) != 0)
-      {
-        mu_message_destroy (&msg, msg_imap);
-        return status;
-      }
-    mu_header_set_fill (header, imap_header_read, msg);
-    mu_message_set_header (msg, header, msg_imap);
-  }
-
-  /* Create the attribute.  */
-  {
-    mu_attribute_t attribute;
-    status = mu_attribute_create (&attribute, msg);
-    if (status != 0)
-      {
-        mu_message_destroy (&msg, msg_imap);
-        return status;
-      }
-    mu_attribute_set_get_flags (attribute, imap_attr_get_flags, msg);
-    mu_attribute_set_set_flags (attribute, imap_attr_set_flags, msg);
-    mu_attribute_set_unset_flags (attribute, imap_attr_unset_flags, msg);
-    mu_message_set_attribute (msg, attribute, msg_imap);
-  }
-
-  /* Create the body and its stream.  */
-  {
-    mu_body_t body = NULL;
-    mu_stream_t stream = NULL;
-    if ((status = mu_body_create (&body, msg)) != 0
-        || (status = mu_stream_create (&stream, mailbox->flags, body)) != 0)
-      {
-        mu_body_destroy (&body, msg);
-        mu_stream_destroy (&stream, body);
-        mu_message_destroy (&msg, msg_imap);
-        return status;
-      }
-    mu_stream_setbufsiz (stream, 128);
-    mu_stream_set_read (stream, imap_body_read, body);
-    mu_stream_set_get_transport2 (stream, imap_body_get_transport2, body);
-    mu_body_set_size (body, imap_body_size, msg);
-    mu_body_set_lines (body, imap_body_lines, msg);
-    mu_body_set_stream (body, stream, msg);
-    mu_message_set_body (msg, body, msg_imap);
-  }
-
-  /* Set the envelope.  */
-  {
-    mu_envelope_t envelope= NULL;
-    status = mu_envelope_create (&envelope, msg);
-    if (status != 0)
-      {
-        mu_message_destroy (&msg, msg_imap);
-        return status;
-      }
-    mu_envelope_set_sender (envelope, imap_envelope_sender, msg);
-    mu_envelope_set_date (envelope, imap_envelope_date, msg);
-    mu_message_set_envelope (msg, envelope, msg_imap);
-  }
-
-  /* Set the mime handling.  */
-  mu_message_set_is_multipart (msg, imap_is_multipart, msg_imap);
-  mu_message_set_get_num_parts (msg, imap_get_num_parts, msg_imap);
-  mu_message_set_get_part (msg, imap_get_part, msg_imap);
-
-  /* Set the UID on the message. */
-  mu_message_set_uid (msg, imap_message_uid, msg_imap);
-  mu_message_set_mailbox (msg, mailbox, msg_imap);
-
-  /* We are done here.  */
-  *pmsg = msg;
-  return 0;
-}
-
-static int
-imap_message_unseen (mu_mailbox_t mailbox, size_t *punseen)
-{
-  m_imap_t m_imap = mailbox->data;
-  *punseen = m_imap->unseen;
-  return 0;
-}
-
-static int
-imap_messages_recent (mu_mailbox_t mailbox, size_t *precent)
-{
-  m_imap_t m_imap = mailbox->data;
-  *precent = m_imap->recent;
-  return 0;
-}
-
-static int
-imap_uidvalidity (mu_mailbox_t mailbox, unsigned long *puidvalidity)
-{
-  m_imap_t m_imap = mailbox->data;
-  *puidvalidity = m_imap->uidvalidity;
-  return 0;
-}
-
-static int
-imap_uidnext (mu_mailbox_t mailbox, size_t *puidnext)
-{
-  m_imap_t m_imap = mailbox->data;
-  *puidnext = m_imap->uidnext;
-  return 0;
-}
-
-/* There is no explicit call to get the message count.  The count is send on
-   a SELECT/EXAMINE command it is also sent async, meaning it will be piggy
-   back on other server response as an untag "EXIST" response.  The
-   function is also use as a way to select mailbox by other functions.  */
-static int
-imap_messages_count (mu_mailbox_t mailbox, size_t *pnum)
-{
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;
-
-  /* FIXME: It is debatable if we should reconnect when the connection
-     timeout or die.  Probably for timeout client should ping i.e. send
-     a NOOP via imap_is_updated() function to keep the connection alive.  */
-  status = mu_folder_open (mailbox->folder, mailbox->flags);
-  if (status != 0)
-    return status;
-
-  /* Are we already selected ? */
-  if (m_imap == (f_imap->selected))
-    {
-      if (pnum)
-	*pnum = m_imap->messages_count;
-      return 0;
-    }
-
-  /*  Put the mailbox as selected.  */
-  f_imap->selected = m_imap;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu %s %s\r\n",
-			       (unsigned long) f_imap->seq++, 
-                               MBX_WRITABLE(mailbox) ? "SELECT" : "EXAMINE",
-                               m_imap->name);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_SELECT;
-
-    case IMAP_SELECT:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_SELECT_ACK;
-
-    case IMAP_SELECT_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      break;
-
-    default:
-      /*mu_error ("imap_message_count unknown state: reconnect\n");*/
-      break;
-    }
-
-  if (pnum)
-    *pnum = m_imap->messages_count;
-
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-static int
-imap_scan (mu_mailbox_t mailbox, size_t msgno, size_t *pcount)
-{
-  return imap_scan0 (mailbox, msgno, pcount , 1);
-}
-
-/* Normally this function is called when an observer is trying to build
-   some sort of list/tree header as the scanning progresses.  But doing
-   this for each message can be time consuming and inefficient.  So we
-   bundle all requests into one and ask the server for everything:
-   "FETCH 1:*".  The good side is that everything will be faster and we
-   do not do lots of small transcations, but rather a big one.  The bad
-   side is that everything will be cached in the structure using a lot of
-   memory.  */
-static int
-imap_scan0 (mu_mailbox_t mailbox, size_t msgno, size_t *pcount, int notif)
-{
-  int status;
-  size_t i;
-  size_t count = 0;
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-
-  /* Selected.  */
-  status = imap_messages_count (mailbox, &count);
-  if (pcount)
-    *pcount = count;
-  if (status != 0)
-    return status;
-
-  /* No need to scan, there is no messages. */
-  if (count == 0)
-    return 0;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap,
-			       "g%lu FETCH 1:* (FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (%s)])\r\n",
-			       (unsigned long) f_imap->seq++,
-			       MU_IMAP_CACHE_HEADERS);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_SCAN;
-
-    case IMAP_SCAN:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_SCAN_ACK;
-      /* Clear the callback string structure.  */
-      mu_stream_truncate (f_imap->string.stream, 0);
-      f_imap->string.offset = 0;
-      f_imap->string.nleft = 0;
-      f_imap->string.type = IMAP_NO_STATE;
-      f_imap->string.msg_imap = NULL;
-
-    case IMAP_SCAN_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      /* Clear the callback string structure.  */
-      mu_stream_truncate (f_imap->string.stream, 0);
-      f_imap->string.offset = 0;
-      f_imap->string.nleft = 0;
-      f_imap->string.type = IMAP_NO_STATE;
-      f_imap->string.msg_imap = NULL;
-      break;
-
-    default:
-      /*mu_error ("imap_scan unknown state: reconnect\n");*/
-      return EINVAL;
-    }
-
-  f_imap->state = IMAP_NO_STATE;
-
-  /* Do not send notifications.  */
-  if (!notif)
-    return 0;
-
-  /* If no callbacks bail out early.  */
-  if (mailbox->observable == NULL)
-    return 0;
-
-  for (i = msgno; i <= count; i++)
-    {
-      size_t tmp = i;
-      if (mu_observable_notify (mailbox->observable, MU_EVT_MESSAGE_ADD,
-                                &tmp) != 0)
-	break;
-      if ((i + 1) % 100 == 0)
-	mu_observable_notify (mailbox->observable, MU_EVT_MAILBOX_PROGRESS, 
-                              NULL);
-    }
-  return 0;
-}
-
-/* Send a NOOP and see if the count has changed.  */
-static int
-imap_is_updated (mu_mailbox_t mailbox)
-{
-  m_imap_t m_imap = mailbox->data;
-  size_t oldcount = m_imap->messages_count;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;
-
-  /* Selected.  */
-  status = imap_messages_count (mailbox, &oldcount);
-  if (status != 0)
-    return status;
-
-  /* Send a noop, and let imap piggy pack the information.  */
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      status = imap_writeline (f_imap, "g%lu NOOP\r\n",
-			       (unsigned long) f_imap->seq++);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_NOOP;
-
-    case IMAP_NOOP:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_NOOP_ACK;
-
-    case IMAP_NOOP_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      break;
-
-    default:
-      /*mu_error ("imap_noop unknown state: reconnect\n"); */
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return (oldcount == m_imap->messages_count);
-}
-
-
-static int
-imap_expunge (mu_mailbox_t mailbox)
-{
-  int status;
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-
-  if (!MBX_WRITABLE(mailbox))
-    return EACCES;
-       
-  /* Select first.  */
-  status = imap_messages_count (mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      {
-	char *set = NULL;
-	status = delete_to_string (m_imap, &set);
-	CHECK_ERROR (f_imap, status);
-	if (set == NULL || *set == '\0')
-	  {
-	    if (set)
-	      free (set);
-	    return 0;
-	  }
-	status = imap_writeline (f_imap,
-				 "g%lu STORE %s +FLAGS.SILENT (\\Deleted)\r\n",
-				 (unsigned long) f_imap->seq++,
-				 set);
-	free (set);
-	CHECK_ERROR (f_imap, status);
-	MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-	f_imap->state = IMAP_STORE;
-      }
-
-      /* Send DELETE.  */
-    case IMAP_STORE:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_STORE_ACK;
-
-    case IMAP_STORE_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_NO_STATE;
-
-    case IMAP_EXPUNGE:
-    case IMAP_EXPUNGE_ACK:
-      status = imap_writeline (f_imap, "g%lu EXPUNGE\r\n",
-			       (unsigned long) f_imap->seq++);
-      CHECK_ERROR (f_imap, status);
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-
-      /* Rescan after expunging but do not trigger the observers.  */
-    case IMAP_SCAN:
-    case IMAP_SCAN_ACK:
-      status = imap_scan0 (mailbox, 1, NULL, 0);
-      CHECK_EAGAIN (f_imap, status);
-
-    default:
-      /* mu_error ("imap_expunge: unknown state\n"); */
-      break;
-    }
-
-  return status;
-}
-
-/* FIXME: Not ___Nonblocking___ safe.  */
-/* DANGER:  The mu_message_t object makes no guaranty about the size and the lines
-   that it returns, if its pointing to non-local file messages, so we
-   make a local copy.  */
-static int
-imap_append_message (mu_mailbox_t mailbox, mu_message_t msg)
-{
-  int status = 0;
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-
-  /* FIXME: It is debatable if we should reconnect when the connection
-   timeout or die.  For timeout client should ping i.e. send
-   a NOOP via imap_is_updated() function to keep the connection alive.  */
-  status = mu_folder_open (mailbox->folder, mailbox->flags);
-  if (status != 0)
-    return status;
-
-  /* FIXME: Can we append to self.  */
-
-  /* Check to see if we are selected. If the message was not modified
-     and came from the same imap folder. use COPY.*/
-  if (f_imap->selected != m_imap && !mu_message_is_modified (msg)
-      && is_same_folder (mailbox, msg))
-    return imap_copy_message (mailbox, msg);
-
-  /* copy the message to local disk by createing a floating message.  */
-  {
-    mu_message_t message = NULL;
-
-    status = mu_message_create_copy(&message, msg);
-
-    if (status == 0)
-      status = imap_append_message0 (mailbox, message);
-    mu_message_destroy (&message, NULL);
-  }
-  return status;
-}
-
-/* Ok this mean that the message is coming from somewhere else.  IMAP
-   is very susceptible on the size, example:
-   A003 APPEND saved-messages (\Seen) {310}
-   if the server does not get the right size advertise in the string literal
-   it will misbehave.  Sine we are assuming that the message will be
-   in native file system format meaning ending with NEWLINE, we will have
-   to do the calculation.  But what is worse; the value return
-   by mu_message_size () and mu_message_lines () are no mean exact but rather
-   a gross approximation for certain type of mailbox.  So the sane
-   thing to do is to save the message in temporary file, this we say
-   we guarantee the size of the message.  */
-static int
-imap_append_message0 (mu_mailbox_t mailbox, mu_message_t msg)
-{
-  size_t total;
-  int status = 0;
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      {
-	size_t lines, size;
-	const char *path;
-	char *abuf = malloc (1);
-	/* Get the desired flags attribute.  */
-	if (abuf == NULL)
-	  return ENOMEM;
-	*abuf = '\0';
-	{
-	  mu_attribute_t attribute = NULL;
-	  int flags = 0;
-	  mu_message_get_attribute (msg, &attribute);
-	  mu_attribute_get_flags (attribute, &flags);
-	  status = flags_to_string (&abuf, flags);
-	  if (status != 0)
-	    return status;
-	  /* Put the surrounding parenthesis, wu-IMAP is sensible to this.  */
-	  {
-	    char *tmp = calloc (strlen (abuf) + 3, 1);
-	    if (tmp == NULL)
-	      {
-		free (abuf);
-		return ENOMEM;
-	      }
-	    sprintf (tmp, "(%s)", abuf);
-	    free (abuf);
-	    abuf = tmp;
-	  }
-	}
-
-	/* Get the mailbox filepath.  */
-        status = mu_url_sget_path (mailbox->url, &path);
-        if (status == MU_ERR_NOENT)
-          path = "INBOX";
-
-	/* FIXME: we need to get the mu_envelope_date and use it.
-	   currently it is ignored.  */
-
-	/* Get the total size, assuming that it is in UNIX format.  */
-	lines = size = 0;
-	mu_message_size (msg, &size);
-	mu_message_lines (msg, &lines);
-	total = size + lines;
-	status = imap_writeline (f_imap, "g%lu APPEND %s %s {%lu}\r\n",
-				 (unsigned long) f_imap->seq++,
-				 path,
-				 abuf,
-				 (unsigned long) (size + lines));
-	free (abuf);
-	CHECK_ERROR (f_imap, status);
-	MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-	f_imap->state = IMAP_APPEND;
-      }
-
-    case IMAP_APPEND:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_APPEND_CONT;
-
-    case IMAP_APPEND_CONT:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      /* If we did not receive the continuation token, it is an error
-         bail out.  */
-      if (f_imap->buffer[0] != '+')
-	{
-	  status = EACCES;
-	  break;
-	}
-      f_imap->state = IMAP_APPEND_SEND;
-
-    case IMAP_APPEND_SEND:
-      {
-	mu_stream_t stream = NULL;
-	mu_off_t off = 0;
-	size_t n = 0;
-	char buffer[255];
-	mu_message_get_stream (msg, &stream);
-	while (mu_stream_readline (stream, buffer, sizeof buffer, off, &n) == 0
-	       && n > 0)
-	  {
-	    if (buffer[n - 1] == '\n')
-	      {
-		buffer[n - 1] = '\0';
-		status = imap_writeline (f_imap, "%s\r\n", buffer);
-	      }
-	    else
-	      imap_writeline (f_imap, "%s", buffer);
-	    off += n;
-	    status = imap_send (f_imap);
-	    CHECK_EAGAIN (f_imap, status);
-	  }
-	f_imap->state = IMAP_APPEND_ACK;
-      }
-      /* !@#%$ UW-IMAP and Gimap server hack: both insist on the last line.  */
-      imap_writeline (f_imap, "\r\n");
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-
-    case IMAP_APPEND_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      /* mu_error ("imap_append: unknown state\n"); */
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-/* If the message is on the same server.  Use the COPY command much more
-   efficient.  */
-static int
-imap_copy_message (mu_mailbox_t mailbox, mu_message_t msg)
-{
-  m_imap_t m_imap = mailbox->data;
-  f_imap_t f_imap = m_imap->f_imap;
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  int status = 0;
-
-  /* FIXME: It is debatable if we should reconnect when the connection
-   timeout or die.  For timeout client should ping i.e. send
-   a NOOP via imap_is_updated() function to keep the connection alive.  */
-  status = mu_folder_open (mailbox->folder, mailbox->flags);
-  if (status != 0)
-    return status;
-
-  switch (f_imap->state)
-    {
-    case IMAP_NO_STATE:
-      {
-	const char *path;
-	/* Check for a valid mailbox name.  */
-	status = mu_url_sget_path (mailbox->url, &path);
-	if (status == 0)
-  	  status = imap_writeline (f_imap, "g%lu COPY %lu %s\r\n",
-	 			   (unsigned long) f_imap->seq++,
-				   (unsigned long) msg_imap->num,
-				   path);
-	CHECK_ERROR (f_imap, status);
-	MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-	f_imap->state = IMAP_COPY;
-      }
-
-    case IMAP_COPY:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      f_imap->state = IMAP_COPY_ACK;
-
-    case IMAP_COPY_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      MU_DEBUG (mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-
-    default:
-      break;
-    }
-  f_imap->state = IMAP_NO_STATE;
-  return status;
-}
-
-/* Message read overload  */
-static int
-imap_message_read (mu_stream_t stream, char *buffer, size_t buflen,
-		   mu_off_t offset, size_t *plen)
-{
-  mu_message_t msg = mu_stream_get_owner (stream);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  char *oldbuf = NULL;
-  char newbuf[2];
-  int status;
-
-  /* This is so annoying, a buffer len of 1 is a killer. If you have for
-     example "\n" to retrieve from the server, IMAP will transform this to
-     "\r\n" and since you ask for only 1, the server will send '\r' only.
-     And ... '\r' will be stripped by (imap_readline()) the number of char
-     read will be 0 which means we're done .... sigh ...  So we guard by at
-     least ask for 2 chars.  */
-  if (buflen == 1)
-    {
-      oldbuf = buffer;
-      buffer = newbuf;
-      buflen = 2;
-    }
-
-  /* Start over.  */
-  if (offset == 0)
-    msg_imap->mu_message_lines = 0;
-
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  /* Select first.  */
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      char *section = NULL;
-
-      if (msg_imap->part)
-	section = section_name (msg_imap);
-
-      /* We have strip the \r, but the offset on the imap server is with that
-	 octet(CFLF) so add it in the offset, it's the number of lines.  */
-      status = imap_writeline (f_imap,
-			       "g%lu FETCH %lu BODY.PEEK[%s]<%lu.%lu>\r\n",
-			       (unsigned long) f_imap->seq++,
-			       (unsigned long) msg_imap->num,
-			       (section) ? section : "",
-			       (unsigned long) (offset +
-						msg_imap->mu_message_lines),
-			       (unsigned long) buflen);
-      if (section)
-	free (section);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_FETCH;
-    }
-  status = fetch_operation (f_imap, msg_imap, buffer, buflen, plen);
-
-  if (oldbuf)
-    oldbuf[0] = buffer[0];
-  return status;
-}
-
-static int
-imap_message_lines (mu_message_t msg, size_t *plines)
-{
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  if (plines && msg_imap)
-    {
-      if (msg_imap->mu_message_lines == 0)
-	*plines = msg_imap->body_lines + msg_imap->header_lines;
-      else
-	*plines = msg_imap->mu_message_lines;
-    }
-  return 0;
-}
-
-/* Sometimes a message is just a place container for other sub parts.
-   In those cases imap bodystructure does not set the mu_message_size aka
-   the mu_body_size.  But we can calculate it since the mu_message_size
-   is the sum of its subparts.  */
-static int
-imap_submessage_size (msg_imap_t msg_imap, size_t *psize)
-{
-  if (psize)
-    {
-      *psize = 0;
-      if (msg_imap->mu_message_size == 0)
-	{
-	  size_t i, size;
-	  for (size = i = 0; i < msg_imap->num_parts; i++, size = 0)
-	    {
-	      if (msg_imap->parts[i])
-		imap_submessage_size (msg_imap->parts[i], &size);
-	      *psize += size;
-	    }
-	}
-      else
-	*psize = (msg_imap->mu_message_size + msg_imap->header_size)
-	  - msg_imap->mu_message_lines;
-    }
-  return 0;
-}
-
-static int
-imap_message_size (mu_message_t msg, size_t *psize)
-{
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;;
-
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  /* If there is a parent it means it is a sub message, IMAP does not give
-     the full size of mime messages, so the mu_message_size retrieved from
-     doing a bodystructure represents rather the mu_body_size.  */
-  if (msg_imap->parent)
-    return imap_submessage_size (msg_imap, psize);
-
-  if (msg_imap->mu_message_size == 0)
-    {
-      /* Select first.  */
-      if (f_imap->state == IMAP_NO_STATE)
-	{
-	  /* We strip the \r, but the offset/size on the imap server is with
-	     that octet so add it in the offset, since it's the number of
-	     lines.  */
-	  status = imap_writeline (f_imap,
-				   "g%lu FETCH %lu RFC822.SIZE\r\n",
-				   (unsigned long) f_imap->seq++,
-				   (unsigned long) msg_imap->num);
-	  CHECK_ERROR (f_imap, status);
-	  MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-	  f_imap->state = IMAP_FETCH;
-	}
-      status = fetch_operation (f_imap, msg_imap, 0, 0, 0);
-    }
-
-  if (status == 0)
-    {
-      if (psize)
-	*psize = msg_imap->mu_message_size - msg_imap->mu_message_lines;
-    }
-  return status;
-}
-
-static int
-imap_message_uid (mu_message_t msg, size_t *puid)
-{
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status;
-
-  if (puid)
-    return 0;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      if (msg_imap->uid)
-	{
-	  *puid = msg_imap->uid;
-	  return 0;
-	}
-      status = imap_writeline (f_imap, "g%lu FETCH %lu UID\r\n",
-			       (unsigned long) f_imap->seq++,
-			       (unsigned long) msg_imap->num);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_FETCH;
-    }
-  status = fetch_operation (f_imap, msg_imap, 0, 0, 0);
-  if (status != 0)
-    return status;
-  *puid = msg_imap->uid;
-  return 0;
-}
-
-static int
-imap_message_get_transport2 (mu_stream_t stream, mu_transport_t *pin, mu_transport_t *pout)
-{
-  mu_message_t msg = mu_stream_get_owner (stream);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  return imap_get_transport2 (msg_imap, pin, pout);
-}
-
-/* Mime.  */
-static int
-imap_is_multipart (mu_message_t msg, int *ismulti)
-{
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      if (msg_imap->num_parts || msg_imap->part)
-	{
-	  if (ismulti)
-	    *ismulti = (msg_imap->num_parts > 1);
-	  return 0;
-	}
-      status = imap_writeline (f_imap,
-			       "g%lu FETCH %lu BODYSTRUCTURE\r\n",
-			       (unsigned long) f_imap->seq++,
-			       (unsigned long) msg_imap->num);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_FETCH;
-    }
-  status = fetch_operation (f_imap, msg_imap, 0, 0, 0);
-  if (status != 0)
-    return status;
-  if (ismulti)
-    *ismulti = (msg_imap->num_parts > 1);
-  return 0;
-}
-
-static int
-imap_get_num_parts (mu_message_t msg, size_t *nparts)
-{
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  if (msg_imap)
-    {
-      if (msg_imap->num_parts == 0)
-	{
-	  int status = imap_is_multipart (msg, NULL);
-	  if (status != 0)
-	    return status;
-	}
-      if (nparts)
-	*nparts = (msg_imap->num_parts == 0) ? 1 : msg_imap->num_parts;
-    }
-  return 0;
-}
-
-static int
-imap_get_part (mu_message_t msg, size_t partno, mu_message_t *pmsg)
-{
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  int status = 0;
-
-  if (msg_imap->num_parts == 0)
-    {
-      status = imap_get_num_parts (msg, NULL);
-      if (status != 0)
-	return status;
-    }
-
-  if (partno <= msg_imap->num_parts)
-    {
-      if (msg_imap->parts[partno - 1]->message)
-	{
-	  if (pmsg)
-	    *pmsg = msg_imap->parts[partno - 1]->message;
-	}
-      else
-	{
-	  mu_message_t message;
-	  status = imap_get_message0 (msg_imap->parts[partno - 1], &message);
-	  if (status == 0)
-	    {
-	      mu_header_t header;
-	      mu_message_get_header (message, &header);
-	      mu_message_set_stream (message, NULL, msg_imap->parts[partno - 1]);
-	      /* mu_message_set_size (message, NULL, msg_imap->parts[partno - 1]); */
-	      msg_imap->parts[partno - 1]->message = message;
-	      if (pmsg)
-		*pmsg = message;
-	    }
-	}
-    }
-  else
-    {
-      if (pmsg)
-	*pmsg = msg_imap->message;
-    }
-  return status;
-}
-
-/* Envelope.  */
-static int
-imap_envelope_sender (mu_envelope_t envelope, char *buffer, size_t buflen,
-		      size_t *plen)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  mu_header_t header;
-  const char *sender;
-  int status;
-
-  mu_message_get_header (msg, &header);
-  status = mu_header_sget_value (header, MU_HEADER_SENDER, &sender);
-  if (status == EAGAIN)
-    return status;
-  else if (status != 0)
-    status = mu_header_sget_value (header, MU_HEADER_FROM, &sender);
-  if (status == 0)
-    {
-      const char *email = NULL;
-      size_t len;
-      mu_address_t address;
-      if (mu_address_create (&address, sender) == 0)
-	{
-	  if (mu_address_sget_email (address, 1, &email) == 0)
-	    len = mu_cpystr (buffer, email, buflen);
-	  mu_address_destroy (&address);
-	}
-
-      if (!email)
-	return MU_ERR_NOENT;
-      
-      if (plen)
-	*plen = len;
-    }
-  return status;
-}
-
-static int
-imap_envelope_date (mu_envelope_t envelope, char *buffer, size_t buflen,
-		    size_t *plen)
-{
-  mu_message_t msg = mu_envelope_get_owner (envelope);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  struct tm tm;
-  mu_timezone tz;
-  time_t now;
-  char datebuf[] = "mm-dd-yyyy hh:mm:ss +0000";
-  const char* date = datebuf;
-  const char** datep = &date;
-  /* reserve as much space as we need for internal-date */
-  int status;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-  if (msg_imap->internal_date == NULL)
-    {
-      if (f_imap->state == IMAP_NO_STATE)
-	{
-	  status = imap_writeline (f_imap,
-				   "g%lu FETCH %lu INTERNALDATE\r\n",
-				   (unsigned long) f_imap->seq++,
-				   (unsigned long) msg_imap->num);
-	  CHECK_ERROR (f_imap, status);
-	  MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-	  f_imap->state = IMAP_FETCH;
-	}
-      status = fetch_operation (f_imap, msg_imap, datebuf,
-				sizeof datebuf, NULL);
-      if (status != 0)
-	return status;
-      msg_imap->internal_date = strdup (datebuf);
-    }
-  else
-    {
-      date = msg_imap->internal_date;
-      datep = &date;
-    }
-
-  if (mu_parse_imap_date_time(datep, &tm, &tz) != 0)
-    now = (time_t)-1;
-  else
-    now = mu_datetime_to_utc (&tm, &tz);
-
-  /* if the time was unparseable, or mktime() didn't like what we
-     parsed, use the calendar time. */
-  if (now == (time_t)-1)
-    {
-      time (&now);
-      tm = *gmtime (&now);
-    }
-
-  {
-    char tmpbuf[MU_ENVELOPE_DATE_LENGTH+1];
-    size_t n = mu_strftime (tmpbuf, sizeof tmpbuf,
-                            MU_ENVELOPE_DATE_FORMAT, &tm);
-    n = mu_cpystr (buffer, tmpbuf, buflen);
-    if (plen)
-      *plen = n;
-  }
-  return 0;
-}
-
-/* Attributes.  */
-static int
-imap_attr_get_flags (mu_attribute_t attribute, int *pflags)
-{
-  mu_message_t msg = mu_attribute_get_owner (attribute);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  /* Did we retrieve it alread ?  */
-  if (msg_imap->flags != 0)
-    {
-      if (pflags)
-	*pflags = msg_imap->flags;
-      return 0;
-    }
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      status = imap_writeline (f_imap, "g%lu FETCH %lu FLAGS\r\n",
-			       (unsigned long) f_imap->seq++,
-			       (unsigned long) msg_imap->num);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_FETCH;
-    }
-  status = fetch_operation (f_imap, msg_imap, NULL, 0, NULL);
-  if (status == 0)
-    {
-      if (pflags)
-	*pflags = msg_imap->flags;
-    }
-  return status;
-}
-
-static int
-imap_attr_set_flags (mu_attribute_t attribute, int flag)
-{
-  mu_message_t msg = mu_attribute_get_owner (attribute);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  /* If already set don't bother.  */
-  if (msg_imap->flags & flag)
-    return 0;
-
-  /* The delete FLAG is not pass yet but only on the expunge.  */
-  if (flag & MU_ATTRIBUTE_DELETED)
-    {
-      msg_imap->flags |= MU_ATTRIBUTE_DELETED;
-      flag &= ~MU_ATTRIBUTE_DELETED;
-    }
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      char *abuf = malloc (1);
-      if (abuf == NULL)
-	return ENOMEM;
-      *abuf = '\0';
-      status = flags_to_string (&abuf, flag);
-      if (status != 0)
-	return status;
-      /* No flags to send??  */
-      if (*abuf == '\0')
-	{
-	  free (abuf);
-	  return 0;
-	}
-      status = imap_writeline (f_imap, "g%lu STORE %lu +FLAGS.SILENT (%s)\r\n",
-			       (unsigned long) f_imap->seq++,
-			       (unsigned long) msg_imap->num,
-			       abuf);
-      free (abuf);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      msg_imap->flags |= flag;
-      f_imap->state = IMAP_FETCH;
-    }
-  return fetch_operation (f_imap, msg_imap, NULL, 0, NULL);
-}
-
-static int
-imap_attr_unset_flags (mu_attribute_t attribute, int flag)
-{
-  mu_message_t msg = mu_attribute_get_owner (attribute);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  int status = 0;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  /* The delete FLAG is not pass yet but only on the expunge.  */
-  if (flag & MU_ATTRIBUTE_DELETED)
-    {
-      msg_imap->flags &= ~MU_ATTRIBUTE_DELETED;
-      flag &= ~MU_ATTRIBUTE_DELETED;
-    }
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      char *abuf = malloc (1);
-      if (abuf == NULL)
-	return ENOMEM;
-      *abuf = '\0';
-      status = flags_to_string (&abuf, flag);
-      if (status != 0)
-	return status;
-      /* No flags to send??  */
-      if (*abuf == '\0')
-	{
-	  free (abuf);
-	  return 0;
-	}
-      status = imap_writeline (f_imap, "g%lu STORE %lu -FLAGS.SILENT (%s)\r\n",
-			       (unsigned long) f_imap->seq++,
-			       (unsigned long) msg_imap->num,
-			       abuf);
-      free (abuf);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      msg_imap->flags &= ~flag;
-      f_imap->state = IMAP_FETCH;
-    }
-  return fetch_operation (f_imap, msg_imap, NULL, 0, NULL);
-}
-
-/* Header.  */
-static int
-imap_header_read (mu_header_t header, char *buffer,
-		  size_t buflen, mu_off_t offset,
-		  size_t *plen)
-{
-  mu_message_t msg = mu_header_get_owner (header);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  char *oldbuf = NULL;
-  char newbuf[2];
-  int status;
-
-  /* This is so annoying, a buffer len of 1 is a killer. If you have for
-     example "\n" to retrieve from the server, IMAP will transform this to
-     "\r\n" and since you ask for only 1, the server will send '\r' only.
-     And ... '\r' will be stripped by (imap_readline()) the number of char
-     read will be 0 which means we're done .... sigh ...  So we guard by at
-     least ask for 2 chars.  */
-  if (buflen == 1)
-    {
-      oldbuf = buffer;
-      buffer = newbuf;
-      buflen = 2;
-    }
-
-  /* Start over.  */
-  if (offset == 0)
-    msg_imap->header_lines = 0;
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      /* We strip the \r, but the offset/size on the imap server is with that
-         octet so add it in the offset, since it's the number of lines.  */
-      if (msg_imap->part)
-        {
-          char *section = section_name (msg_imap);
-          status = imap_writeline (f_imap,
-                                   "g%lu FETCH %lu BODY.PEEK[%s.MIME]<%lu.%lu>\r\n",
-                                   (unsigned long) f_imap->seq++,
-				   (unsigned long) msg_imap->num,
-                                   (section) ? section : "",
-                                   (unsigned long) (offset +
-						    msg_imap->header_lines),
-				   (unsigned long) buflen);
-          if (section)
-            free (section);
-        }
-      else
-        status = imap_writeline (f_imap,
-                                 "g%lu FETCH %lu BODY.PEEK[HEADER]<%lu.%lu>\r\n",
-                                 (unsigned long) f_imap->seq++,
-				 (unsigned long) msg_imap->num,
-                                 (unsigned long) (offset +
-						  msg_imap->header_lines),
-				 (unsigned long) buflen);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_FETCH;
-
-    }
-  status = fetch_operation (f_imap, msg_imap, buffer, buflen, plen);
-  if (oldbuf)
-    oldbuf[0] = buffer[0];
-  return status;
-}
-
-/* Body.  */
-static int
-imap_body_size (mu_body_t body, size_t *psize)
-{
-  mu_message_t msg = mu_body_get_owner (body);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  if (psize && msg_imap)
-    {
-      /* If there is a parent it means it is a sub message, IMAP does not give
-	 the full size of mime messages, so the mu_message_size was retrieve from
-	 doing a bodystructure and represents rather the mu_body_size.  */
-      if (msg_imap->parent)
-	{
-	  *psize = msg_imap->mu_message_size - msg_imap->mu_message_lines;
-	}
-      else
-	{
-	  if (msg_imap->body_size)
-	    *psize = msg_imap->body_size;
-	  else if (msg_imap->mu_message_size)
-	    *psize = msg_imap->mu_message_size
-	      - (msg_imap->header_size + msg_imap->header_lines);
-	  else
-	    *psize = 0;
-	}
-    }
-  return 0;
-}
-
-static int
-imap_body_lines (mu_body_t body, size_t *plines)
-{
-  mu_message_t msg = mu_body_get_owner (body);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  if (plines && msg_imap)
-    *plines = msg_imap->body_lines;
-  return 0;
-}
-
-/* FIXME: Send EISPIPE if trying to seek back.  */
-static int
-imap_body_read (mu_stream_t stream, char *buffer, size_t buflen,
-		mu_off_t offset, size_t *plen)
-{
-  mu_body_t body = mu_stream_get_owner (stream);
-  mu_message_t msg = mu_body_get_owner (body);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  m_imap_t m_imap = msg_imap->m_imap;
-  f_imap_t f_imap = m_imap->f_imap;
-  char *oldbuf = NULL;
-  char newbuf[2];
-  int status;
-
-  /* This is so annoying, a buffer len of 1 is a killer. If you have for
-     example "\n" to retrieve from the server, IMAP will transform this to
-     "\r\n" and since you ask for only 1, the server will send '\r' only.
-     And ... '\r' will be stripped by (imap_readline()) the number of char
-     read will be 0 which means we're done .... sigh ...  So we guard by at
-     least ask for 2 chars.  */
-  if (buflen == 1)
-    {
-      oldbuf = buffer;
-      buffer = newbuf;
-      buflen = 2;
-    }
-
-  /* Start over.  */
-  if (offset == 0)
-    {
-      msg_imap->body_lines = 0;
-      msg_imap->body_size = 0;
-    }
-
-  /* Select first.  */
-  status = imap_messages_count (m_imap->mailbox, NULL);
-  if (status != 0)
-    return status;
-
-  if (f_imap->state == IMAP_NO_STATE)
-    {
-      /* We strip the \r, but the offset/size on the imap server is with the
-         octet, so add it since it's the number of lines.  */
-      if (msg_imap->part)
-        {
-          char *section = section_name (msg_imap);
-          status = imap_writeline (f_imap,
-                                   "g%lu FETCH %lu BODY.PEEK[%s]<%lu.%lu>\r\n",
-                                   (unsigned long) f_imap->seq++,
-				   (unsigned long) msg_imap->num,
-                                   (section) ? section: "",
-                                   (unsigned long) (offset +
-						    msg_imap->body_lines),
-				   (unsigned long) buflen);
-          if (section)
-            free (section);
-        }
-      else
-        status = imap_writeline (f_imap,
-                                 "g%lu FETCH %lu BODY.PEEK[TEXT]<%lu.%lu>\r\n",
-                                 (unsigned long) f_imap->seq++,
-				 (unsigned long) msg_imap->num,
-                                 (unsigned long) (offset +
-						  msg_imap->body_lines),
-				 (unsigned long) buflen);
-      CHECK_ERROR (f_imap, status);
-      MU_DEBUG (m_imap->mailbox->debug, MU_DEBUG_PROT, f_imap->buffer);
-      f_imap->state = IMAP_FETCH;
-
-    }
-  status = fetch_operation (f_imap, msg_imap, buffer, buflen, plen);
-  if (oldbuf)
-    oldbuf[0] = buffer[0];
-  return status;
-}
-
-static int
-imap_body_get_transport2 (mu_stream_t stream, mu_transport_t *pin, 
-                         mu_transport_t *pout)
-{
-  mu_body_t body = mu_stream_get_owner (stream);
-  mu_message_t msg = mu_body_get_owner (body);
-  msg_imap_t msg_imap = mu_message_get_owner (msg);
-  return imap_get_transport2 (msg_imap, pin, pout);
-}
-
-
-static int
-imap_get_transport2 (msg_imap_t msg_imap, mu_transport_t *pin, mu_transport_t *pout)
-{
-  if (   msg_imap
-      && msg_imap->m_imap
-      && msg_imap->m_imap->f_imap
-      && msg_imap->m_imap->f_imap->folder)
-    return mu_stream_get_transport2 (msg_imap->m_imap->f_imap->folder->stream,
-			         pin, pout);
-  return EINVAL;
-}
-
-/* Since so many operations are fetch, we regoup this into one function.  */
-static int
-fetch_operation (f_imap_t f_imap, msg_imap_t msg_imap, char *buffer,
-		 size_t buflen, size_t *plen)
-{
-  int status = 0;
-
-  switch (f_imap->state)
-    {
-    case IMAP_FETCH:
-      status = imap_send (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      mu_stream_truncate (f_imap->string.stream, 0);
-      f_imap->string.offset = 0;
-      f_imap->string.nleft = 0;
-      f_imap->string.type = IMAP_NO_STATE;
-      f_imap->string.msg_imap = msg_imap;
-      f_imap->state = IMAP_FETCH_ACK;
-
-    case IMAP_FETCH_ACK:
-      status = imap_parse (f_imap);
-      CHECK_EAGAIN (f_imap, status);
-      if (f_imap->selected)
-	MU_DEBUG (f_imap->selected->mailbox->debug, MU_DEBUG_PROT,
-	   	  f_imap->buffer);
-
-    default:
-      break;
-    }
-
-  f_imap->state = IMAP_NO_STATE;
-
-  /* The server may have timeout any case connection is gone away.  */
-  if (status == 0 && f_imap->isopen == 0 && f_imap->string.offset == 0)
-    status = MU_ERR_CONN_CLOSED;
-
-  if (buffer)
-    mu_stream_read (f_imap->string.stream, buffer, buflen, 0, plen);
-  else if (plen)
-    *plen = 0;
-  mu_stream_truncate (f_imap->string.stream, 0);
-  f_imap->string.offset = 0;
-  f_imap->string.nleft = 0;
-  f_imap->string.type = IMAP_NO_STATE;
-  f_imap->string.msg_imap = NULL;
-  return status;
-}
-
-/* Decide whether the message came from the same folder as the mailbox.  */
-static int
-is_same_folder (mu_mailbox_t mailbox, mu_message_t msg)
-{
-  mu_mailbox_t mbox = NULL;
-  mu_message_get_mailbox (msg, &mbox);
-  return (mbox != NULL && mbox->url != NULL
-          && mu_url_is_same_scheme (mbox->url, mailbox->url)
-          && mu_url_is_same_host (mbox->url, mailbox->url)
-          && mu_url_is_same_port (mbox->url, mailbox->url));
-}
-
-/* Convert flag attribute to IMAP String attributes.  */
-static int
-flags_to_string (char **pbuf, int flag)
-{
-  char *abuf = *pbuf;
-  if (flag & MU_ATTRIBUTE_DELETED)
-    {
-      char *tmp = realloc (abuf, strlen (abuf) + strlen ("\\Deleted") + 2);
-      if (tmp == NULL)
-        {
-          free (abuf);
-          return ENOMEM;
-        }
-      abuf = tmp;
-      if (*abuf)
-        strcat (abuf, " ");
-      strcat (abuf, "\\Deleted");
-    }
-  if (flag & MU_ATTRIBUTE_READ)
-    {
-      char *tmp = realloc (abuf, strlen (abuf) + strlen ("\\Seen") + 2);
-      if (tmp == NULL)
-        {
-          free (abuf);
-          return ENOMEM;
-        }
-      abuf = tmp;
-      if (*abuf)
-        strcat (abuf, " ");
-      strcat (abuf, "\\Seen");
-    }
-  if (flag & MU_ATTRIBUTE_ANSWERED)
-    {
-      char *tmp = realloc (abuf, strlen (abuf) + strlen ("\\Answered") + 2);
-      if (tmp == NULL)
-        {
-          free (abuf);
-          return ENOMEM;
-        }
-      abuf = tmp;
-      if (*abuf)
-        strcat (abuf, " ");
-      strcat (abuf, "\\Answered");
-    }
-  if (flag & MU_ATTRIBUTE_DRAFT)
-    {
-      char *tmp = realloc (abuf, strlen (abuf) + strlen ("\\Draft") + 2);
-      if (tmp == NULL)
-        {
-          free (abuf);
-          return ENOMEM;
-        }
-      abuf = tmp;
-      if (*abuf)
-        strcat (abuf, " ");
-      strcat (abuf, "\\Draft");
-    }
-  if (flag & MU_ATTRIBUTE_FLAGGED)
-    {
-      char *tmp = realloc (abuf, strlen (abuf) + strlen ("\\Flagged") + 2);
-      if (tmp == NULL)
-        {
-          free (abuf);
-          return ENOMEM;
-        }
-      abuf = tmp;
-      if (*abuf)
-        strcat (abuf, " ");
-      strcat (abuf, "\\Flagged");
-    }
-  *pbuf = abuf;
-  return 0;
-}
-
-/* Convert a suite of number to IMAP message number.  */
-static int
-add_number (char **pset, size_t start, size_t end)
-{
-  char *buf = NULL;
-  char *set;
-  char *tmp;
-  size_t set_len = 0;
-
-  if (pset == NULL)
-    return 0;
-
-  set = *pset;
-
-  if (set)
-    set_len = strlen (set);
-
-  /* We had a previous seqence.  */
-  if (start == 0)
-    /* nothing */;
-  else if (start != end)
-    mu_asprintf (&buf, "%lu:%lu",
-		  (unsigned long) start, (unsigned long) end);
-  else
-    mu_asprintf (&buf, "%lu", (unsigned long) start);
-
-  if (set_len)
-    tmp = realloc (set, set_len + strlen (buf) + 2 /* null and comma */);
-  else
-    tmp = calloc (strlen (buf) + 1, 1);
-
-  if (tmp == NULL)
-    {
-      free (set);
-      free (buf);
-      return ENOMEM;
-    }
-  set = tmp;
-
-  /* If we had something add a comma separator.  */
-  if (set_len)
-    strcat (set, ",");
-  strcat (set, buf);
-  free (buf);
-  
-  *pset = set;
-  return 0;
-}
-
-static int
-delete_to_string (m_imap_t m_imap, char **pset)
-{
-  int status;
-  size_t i, prev = 0, is_range = 0;
-  size_t start = 0, cur = 0;
-  char *set = NULL;
-
-  /* Reformat the number for IMAP.  */
-  for (i = 0; i < m_imap->imessages_count; ++i)
-    {
-      if (m_imap->imessages[i]
-	  && (m_imap->imessages[i]->flags & MU_ATTRIBUTE_DELETED))
-	{
-	  cur = m_imap->imessages[i]->num;
-	  /* The first number.  */
-	  if (start == 0)
-	    {
-	      start = prev = cur;
-	    }
-	  /* Is it a sequence?  */
-	  else if ((prev + 1) == cur)
-	    {
-	      prev = cur;
-	      is_range = 1;
-	    }
-	  continue;
-	}
-
-      if (start)
-	{
-	  status = add_number (&set, start, cur);
-	  if (status != 0)
-	    return status;
-	  start = 0;
-	  prev = 0;
-	  cur = 0;
-	  is_range = 0;
-	}
-    } /* for () */
-
-  if (start)
-    {
-      status = add_number (&set, start, cur);
-      if (status != 0)
-	return status;
-    }
-  *pset = set;
-  return 0;
-}
-
-#endif
diff --git a/libproto/imap/rename.c b/libproto/imap/rename.c
index 334df7d..3348380 100644
--- a/libproto/imap/rename.c
+++ b/libproto/imap/rename.c
@@ -29,6 +29,9 @@ mu_imap_rename (mu_imap_t imap, const char *mailbox, const char *new_mailbox)
   char const *argv[3];
   static struct imap_command com;
 
+  if (!mailbox || !new_mailbox)
+    return EINVAL;
+
   argv[0] = "RENAME";
   argv[1] = mailbox;
   argv[2] = new_mailbox;
diff --git a/libproto/imap/resplist.c b/libproto/imap/resplist.c
index cca7f33..65d78dc 100644
--- a/libproto/imap/resplist.c
+++ b/libproto/imap/resplist.c
@@ -57,6 +57,7 @@ _mu_imap_response_list_create (mu_imap_t imap, mu_list_t *plist)
 
 #define IS_LBRACE(p) ((p)[0] == '(')
 #define IS_RBRACE(p) ((p)[0] == ')')
+#define IS_NIL(p) (strcmp (p, "NIL") == 0)
 
 static struct imap_list_element *
 _new_imap_list_element (mu_imap_t imap, enum imap_eltype type)
@@ -181,6 +182,7 @@ _parse_element (struct parsebuf *pb)
       
       if (IS_RBRACE (tok))
 	{
+	  parsebuf_gettok (pb);
 	  elt = _new_imap_list_element (pb->pb_imap, imap_eltype_list);
 	  if (!elt)
 	    {
@@ -203,6 +205,16 @@ _parse_element (struct parsebuf *pb)
 	parsebuf_seterr (pb, MU_ERR_PARSE);
       return NULL;
     }
+  else if (IS_NIL (tok))
+    {
+      elt = _new_imap_list_element (pb->pb_imap, imap_eltype_list);
+      if (!elt)
+	{
+	  parsebuf_seterr (pb, ENOMEM);
+	  return NULL;
+	}
+      elt->v.list = NULL;
+    }
   else
     {
       char *s;
@@ -255,6 +267,12 @@ _mu_imap_list_element_is_string (struct imap_list_element *elt,
   return strcmp (elt->v.string, str) == 0;
 }
 
+int
+_mu_imap_list_element_is_nil (struct imap_list_element *elt)
+{
+  return elt->type == imap_eltype_list && mu_list_is_empty (elt->v.list);
+}
+
 struct imap_list_element *
 _mu_imap_list_at (mu_list_t list, int idx)
 {
diff --git a/libproto/imap/subscribe.c b/libproto/imap/subscribe.c
index 3cf71a6..f370f3a 100644
--- a/libproto/imap/subscribe.c
+++ b/libproto/imap/subscribe.c
@@ -19,6 +19,7 @@
 # include <config.h>
 #endif
 
+#include <errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
@@ -28,6 +29,9 @@ mu_imap_subscribe (mu_imap_t imap, const char *mailbox)
   char const *argv[2];
   static struct imap_command com;
 
+  if (!mailbox)
+    return EINVAL;
+  
   argv[0] = "SUBSCRIBE";
   argv[1] = mailbox;
 
diff --git a/libproto/imap/tests/.gitignore b/libproto/imap/tests/.gitignore
new file mode 100644
index 0000000..0e29c74
--- /dev/null
+++ b/libproto/imap/tests/.gitignore
@@ -0,0 +1 @@
+imapfolder
diff --git a/libproto/mbox/Makefile.am b/libproto/imap/tests/Makefile.am
similarity index 77%
copy from libproto/mbox/Makefile.am
copy to libproto/imap/tests/Makefile.am
index c0fcaf2..7294934 100644
--- a/libproto/mbox/Makefile.am
+++ b/libproto/imap/tests/Makefile.am
@@ -13,16 +13,14 @@
 ## General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
+## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
-
-lib_LTLIBRARIES = libmu_mbox.la
-libmu_mbox_la_LDFLAGS=-version-info @VI_CURRENT@:@VI_REVISION@:@VI_AGE@
-libmu_mbox_la_LIBADD = ${MU_LIB_MAILUTILS}
-libmu_mbox_la_SOURCES = \
- folder.c\
- mbox.c\
- mboxscan.c\
- mbox0.h
+noinst_PROGRAMS = \
+ imapfolder
 
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
+LDADD = \
+ @MU_LIB_IMAP@\
+ @MU_LIB_AUTH@\
+ @MU_AUTHLIBS@\
+ @MU_LIB_MAILUTILS@
diff --git a/libproto/imap/tests/imapfolder.c b/libproto/imap/tests/imapfolder.c
new file mode 100644
index 0000000..cb3a08b
--- /dev/null
+++ b/libproto/imap/tests/imapfolder.c
@@ -0,0 +1,242 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <mailutils/mailutils.h>
+
+struct command
+{
+  char *verb;
+  int nargs;
+  char *args;
+  void (*handler) (mu_folder_t folder, char **argv);
+};
+
+static int
+_print_list_entry (void *item, void *data)
+{
+  struct mu_list_response *resp = item;
+  mu_printf ("%c%c %c %4d %s\n",
+	     (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY) ? 'd' : '-',
+	     (resp->type & MU_FOLDER_ATTRIBUTE_FILE) ? 'f' : '-',
+	     resp->separator ? resp->separator : ' ',
+	     resp->level,
+	     resp->name);
+  return 0;
+}
+
+static void
+com_list (mu_folder_t folder, char **argv)
+{
+  int rc;
+  mu_list_t list;
+  
+  mu_printf ("listing %s %s\n", argv[0], argv[1]);
+  rc = mu_folder_list (folder, argv[0], argv[1], 0, &list);
+  if (rc)
+    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_list", argv[0], rc);
+  else
+    {
+      mu_list_foreach (list, _print_list_entry, NULL);
+      mu_list_destroy (&list);
+    }
+}
+
+static void
+com_lsub (mu_folder_t folder, char **argv)
+{
+  int rc;
+  mu_list_t list;
+  
+  mu_printf ("listing subscriptions for '%s' '%s'\n", argv[0], argv[1]);
+  rc = mu_folder_lsub (folder, argv[0], argv[1], &list);
+  if (rc)
+    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_lsub", argv[0], rc);
+  else
+    {
+      mu_list_foreach (list, _print_list_entry, NULL);
+      mu_list_destroy (&list);
+    }
+}
+
+static void
+com_delete (mu_folder_t folder, char **argv)
+{
+  int rc;
+  
+  mu_printf ("deleting %s\n", argv[0]);
+  rc = mu_folder_delete (folder, argv[0]);
+  if (rc)
+    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_lsub", argv[0], rc);
+  else
+    mu_printf ("delete successful\n");
+}
+
+static void
+com_rename (mu_folder_t folder, char **argv)
+{
+  int rc;
+
+  mu_printf ("renaming %s to %s\n", argv[0], argv[1]);
+  rc = mu_folder_rename (folder, argv[0], argv[1]);
+  if (rc)
+    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_rename", argv[0], rc);
+  else
+    mu_printf ("rename successful\n");
+}
+
+static void
+com_subscribe (mu_folder_t folder, char **argv)
+{
+  int rc;
+
+  mu_printf ("subscribing %s\n", argv[0]);
+  rc = mu_folder_subscribe (folder, argv[0]);
+  if (rc)
+    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_subscribe", argv[0], rc);
+  else
+    mu_printf ("subscribe successful\n");
+}
+
+static void
+com_unsubscribe (mu_folder_t folder, char **argv)
+{
+  int rc;
+
+  mu_printf ("unsubscribing %s\n", argv[0]);
+  rc = mu_folder_unsubscribe (folder, argv[0]);
+  if (rc)
+    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_unsubscribe", argv[0], rc);
+  else
+    mu_printf ("unsubscribe successful\n");
+}
+
+static struct command comtab[] = {
+  { "list", 2, "REF MBX", com_list },
+  { "lsub", 2, "REF MBX", com_lsub },
+  { "delete", 1, "MBX", com_delete },
+  { "rename", 2, "OLD NEW", com_rename },
+  { "delete", 1, "MBOX", com_delete },
+  { "subscribe", 1, "MBX", com_subscribe },
+  { "unsubscribe", 1, "MBX", com_unsubscribe },
+  { NULL }
+};
+
+static struct command *
+find_command (const char *name)
+{
+  struct command *cp;
+  
+  for (cp = comtab; cp->verb; cp++)
+    if (strcmp (cp->verb, name) == 0)
+      return cp;
+  return NULL;
+}
+
+static void
+usage ()
+{
+  struct command *cp;
+  
+  mu_printf (
+    "usage: %s [debug=SPEC] url=URL OP ARG [ARG...] [OP ARG [ARG...]...]\n",
+    mu_program_name);
+  mu_printf ("OPerations and corresponding ARGuments are:\n");
+  for (cp = comtab; cp->verb; cp++)
+    mu_printf (" %s %s\n", cp->verb, cp->args);
+}
+
+int
+main (int argc, char **argv)
+{
+  int i;
+  int rc;
+  mu_folder_t folder;
+  char *fname = NULL;
+  
+  mu_set_program_name (argv[0]);
+  mu_registrar_record (mu_imap_record);
+  mu_registrar_record (mu_imaps_record);
+
+  if (argc == 1)
+    {
+      usage ();
+      exit (0);
+    }
+
+  for (i = 1; i < argc; i++)
+    {
+      if (strncmp (argv[i], "debug=", 6) == 0)
+	mu_debug_parse_spec (argv[i] + 6);
+      else if (strncmp (argv[i], "url=", 4) == 0)
+	fname = argv[i] + 4;
+      else
+	break;
+    }
+
+  if (!fname)
+    {
+      mu_error ("URL not specified");
+      exit (1);
+    }
+      
+  rc = mu_folder_create (&folder, fname);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_create", fname, rc);
+      return 1;
+    }
+  
+  rc = mu_folder_open (folder, MU_STREAM_READ);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_open", fname, rc);
+      return 1;
+    }
+  
+  while (i < argc)
+    {
+      char *comargs[2];
+      struct command *cmd;
+      
+      cmd = find_command (argv[i]);
+      if (!cmd)
+	{
+	  mu_error ("unknown command %s\n", argv[i]);
+	  break;
+	}
+
+      i++;
+      if (i + cmd->nargs > argc)
+	{
+	  mu_error ("not enough arguments for %s", cmd->verb);
+	  break;
+	}
+      memcpy (comargs, argv + i, cmd->nargs * sizeof (comargs[0]));
+      i += cmd->nargs;
+
+      cmd->handler (folder, comargs);
+    }
+
+  mu_folder_close (folder);
+  mu_folder_destroy (&folder);
+
+  return 0;
+}
diff --git a/libproto/imap/unsubscribe.c b/libproto/imap/unsubscribe.c
index 4831829..159b442 100644
--- a/libproto/imap/unsubscribe.c
+++ b/libproto/imap/unsubscribe.c
@@ -19,6 +19,7 @@
 # include <config.h>
 #endif
 
+#include <errno.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
@@ -28,6 +29,9 @@ mu_imap_unsubscribe (mu_imap_t imap, const char *mailbox)
   char const *argv[2];
   static struct imap_command com;
 
+  if (!mailbox)
+    return EINVAL;
+  
   argv[0] = "UNSUBSCRIBE";
   argv[1] = mailbox;
 
diff --git a/libproto/imap/url.c b/libproto/imap/url.c
index e9ddd2f..27ccea6 100644
--- a/libproto/imap/url.c
+++ b/libproto/imap/url.c
@@ -20,18 +20,15 @@
 # include <config.h>
 #endif
 
-#ifdef ENABLE_IMAP
-
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
+#include <string.h>
 
 #include <mailutils/sys/registrar.h>
 #include <mailutils/sys/url.h>
+#include <mailutils/sys/imap.h>
 
 static void url_imap_destroy (mu_url_t url);
 
@@ -47,7 +44,7 @@ url_imap_destroy (mu_url_t url MU_ARG_UNUSED)
 */
 
 int
-_url_imap_init (mu_url_t url)
+_mu_imap_url_init (mu_url_t url)
 {
   if (url->port == 0)
     url->port = MU_IMAP_PORT;
@@ -78,7 +75,7 @@ _url_imap_init (mu_url_t url)
 */
 
 int
-_url_imaps_init (mu_url_t url)
+_mu_imaps_url_init (mu_url_t url)
 {
   if (url->port == 0)
     url->port = MU_IMAPS_PORT;
@@ -102,4 +99,3 @@ _url_imaps_init (mu_url_t url)
   return 0;
 }
 
-#endif /* ENABLE_IMAP */
diff --git a/mu/imap.c b/mu/imap.c
index f615de9..2bebae4 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -160,7 +160,7 @@ imap_prompt_env ()
 
 /* Callbacks */
 static void
-imap_popauth_callback (void *data, int code, size_t sdat, void *pdat)
+imap_preauth_callback (void *data, int code, size_t sdat, void *pdat)
 {
   const char *text = pdat;
   if (text)
@@ -425,7 +425,7 @@ com_connect (int argc, char **argv)
 
 	  /* Set callbacks */
 	  mu_imap_register_callback_function (imap, MU_IMAP_CB_PREAUTH,
-					      imap_popauth_callback,
+					      imap_preauth_callback,
 					      NULL);
 	  mu_imap_register_callback_function (imap, MU_IMAP_CB_BYE,
 					      imap_bye_callback,


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 16 04:40:38 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RbUHO-0005aE-1J
	for mharc-commit-mailutils@gnu.org; Fri, 16 Dec 2011 04:40:38 -0500
Received: from eggs.gnu.org ([140.186.70.92]:39392)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbUHH-0005Zk-37
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 04:40:36 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbUHC-0003pf-JT
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 04:40:31 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:41956)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbUHC-0003pX-EZ
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 04:40:26 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbUHB-0006Ms-Of
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 09:40:26 +0000
Date: Fri, 16 Dec 2011 09:40:25 +0000
Message-Id: <E1RbUHB-0006Ms-Of@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-533-ga837453
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: fa42589cef090379bda4ea4e0838474520f9f4f6
X-Git-Newrev: a837453677a2888155aa565666acc956cdd3f896
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 16 Dec 2011 09:40:37 -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=a837453677a2888155aa565666acc956cdd3f896

The branch, master has been updated
       via  a837453677a2888155aa565666acc956cdd3f896 (commit)
      from  fa42589cef090379bda4ea4e0838474520f9f4f6 (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 a837453677a2888155aa565666acc956cdd3f896
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 16 11:39:30 2011 +0200

    Minor cleanup in imap client.
    
    * include/mailutils/imap.h (mu_imap_session_state): Remove.
    (mu_imap_state): Rename to mu_imap_session_state.
    (mu_imap_state_str): Rename to mu_imap_session_state_str.
    * libproto/imap/create.c (mu_imap_session_state)
    (mu_imap_iserror,mu_imap_clearerr): Move to state.c
    * mu/imap.c: Update accordingly.

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

Summary of changes:
 include/mailutils/imap.h |    5 ++---
 libproto/imap/create.c   |   26 --------------------------
 libproto/imap/state.c    |   30 ++++++++++++++++++++++++------
 mu/imap.c                |    5 +++--
 4 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index ab20716..63a5eae 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -47,7 +47,6 @@ void mu_imap_destroy (mu_imap_t *pimap);
 int mu_imap_connect (mu_imap_t imap);
 int mu_imap_disconnect (mu_imap_t imap);
 
-int mu_imap_session_state (mu_imap_t imap);
 int mu_imap_iserror (mu_imap_t imap);
 void mu_imap_clearerr (mu_imap_t imap);
   
@@ -126,8 +125,8 @@ int mu_imap_trace_mask (mu_imap_t imap, int op, int lev);
 
 int mu_imap_strerror (mu_imap_t imap, const char **pstr);
 
-int mu_imap_state (mu_imap_t imap, int *pstate);
-int mu_imap_state_str (int state, const char **pstr);
+int mu_imap_session_state (mu_imap_t imap);
+int mu_imap_session_state_str (int state, const char **pstr);
 
 int mu_imap_tag (mu_imap_t imap, const char **pseq);
 
diff --git a/libproto/imap/create.c b/libproto/imap/create.c
index 5aebcae..8ddc4cd 100644
--- a/libproto/imap/create.c
+++ b/libproto/imap/create.c
@@ -65,29 +65,3 @@ _mu_imap_init (mu_imap_t imap)
   return 0;
 }
 
-int
-mu_imap_session_state (mu_imap_t imap)
-{
-  if (!imap)
-    return -1;
-  return imap->session_state;
-}
-
-int
-mu_imap_iserror (mu_imap_t imap)
-{
-  if (!imap)
-    return -1;
-  return imap->client_state == MU_IMAP_CLIENT_ERROR;
-}
-
-void
-mu_imap_clearerr (mu_imap_t imap)
-{
-  if (imap)
-    {
-      imap->client_state = MU_IMAP_CLIENT_READY;
-      if (imap->io)
-	mu_imapio_clearerr (imap->io);
-    }
-}
diff --git a/libproto/imap/state.c b/libproto/imap/state.c
index a957aa1..b7d2028 100644
--- a/libproto/imap/state.c
+++ b/libproto/imap/state.c
@@ -32,19 +32,37 @@ const char *_mu_imap_state_name[] = {
 int _mu_imap_state_count = MU_ARRAY_SIZE (_mu_imap_state_name);
 
 int
-mu_imap_state (mu_imap_t imap, int *pstate)
+mu_imap_session_state (mu_imap_t imap)
 {
-  if (imap == NULL || pstate == NULL)
-    return EINVAL;
-  *pstate = imap->session_state;
-  return 0;
+  if (!imap)
+    return -1;
+  return imap->session_state;
 }
 
 int
-mu_imap_state_str (int state, const char **pstr)
+mu_imap_session_state_str (int state, const char **pstr)
 {
   if (state < 0 || state >= _mu_imap_state_count)
     return EINVAL;
   *pstr = gettext (_mu_imap_state_name[state]);
   return 0;
 }
+
+int
+mu_imap_iserror (mu_imap_t imap)
+{
+  if (!imap)
+    return -1;
+  return imap->client_state == MU_IMAP_CLIENT_ERROR;
+}
+
+void
+mu_imap_clearerr (mu_imap_t imap)
+{
+  if (imap)
+    {
+      imap->client_state = MU_IMAP_CLIENT_READY;
+      if (imap->io)
+	mu_imapio_clearerr (imap->io);
+    }
+}
diff --git a/mu/imap.c b/mu/imap.c
index 2bebae4..92e9c42 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -70,7 +70,7 @@ current_imap_state ()
   if (imap == NULL)
     state = MU_IMAP_SESSION_INIT;
   else
-    mu_imap_state (imap, &state);
+    state = mu_imap_session_state (imap);
   return state;
 }
 
@@ -152,7 +152,8 @@ imap_prompt_env ()
   mutool_prompt_env[11] = PACKAGE_VERSION;
 
   mutool_prompt_env[12] = "status";
-  if (mu_imap_state_str (state, (const char **) &mutool_prompt_env[13]))
+  if (mu_imap_session_state_str (state,
+				 (const char **) &mutool_prompt_env[13]))
     mutool_prompt_env[12] = NULL;
 
   mutool_prompt_env[14] = NULL;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 16 19:12:00 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rbhse-0002VB-8x
	for mharc-commit-mailutils@gnu.org; Fri, 16 Dec 2011 19:12:00 -0500
Received: from eggs.gnu.org ([140.186.70.92]:55221)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbhsT-0002U3-Rb
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 19:11:57 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbhsL-0002V5-Lu
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 19:11:49 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:48561)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbhsL-0002V1-Cg
	for commit-mailutils@gnu.org; Fri, 16 Dec 2011 19:11:41 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RbhsJ-0003bK-Iz
	for commit-mailutils@gnu.org; Sat, 17 Dec 2011 00:11:40 +0000
Date: Sat, 17 Dec 2011 00:11:39 +0000
Message-Id: <E1RbhsJ-0003bK-Iz@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-539-gc22d07a
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: a837453677a2888155aa565666acc956cdd3f896
X-Git-Newrev: c22d07a3d7bc380541bfb2603c6f5a2967ad63c9
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 17 Dec 2011 00:11:57 -0000

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

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

The branch, master has been updated
       via  c22d07a3d7bc380541bfb2603c6f5a2967ad63c9 (commit)
       via  7f325d5c04a280e0711a09fef9eab86e128ace86 (commit)
       via  5c16cf454ffc05a340cbf8136c36e4cc1ed72904 (commit)
       via  b71b1bb14085900051070cb82ad334ce67f14d96 (commit)
       via  dd08262c4c55b984e9b38a3dc4a330960d01e26f (commit)
       via  33a5d85239028bc3e34eec59909253d12d5434f4 (commit)
      from  a837453677a2888155aa565666acc956cdd3f896 (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 c22d07a3d7bc380541bfb2603c6f5a2967ad63c9
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 17 01:38:06 2011 +0200

    Move Python support from experimental to stable features.

commit 7f325d5c04a280e0711a09fef9eab86e128ace86
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 17 01:25:53 2011 +0200

    Fix the use of the hierarchy delimiter.
    
    * include/mailutils/util.h (MU_HIERARCHY_DELIMITER): New define.
    (mu_tilde_expansion): Change signature.  Delimiter is an int.
    * libmailutils/base/tilde.c: Likewise. All uses update.
    
    * imap4d/namespace.c (namespace_checkfullpath)
    (namespace_getfullpath): Remove delim argument. All uses updated.
    * imap4d/util.c (util_getfullpath): Remove delim argument. All uses updated.

commit 5c16cf454ffc05a340cbf8136c36e4cc1ed72904
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 17 00:44:05 2011 +0200

    Use IMAP-stile wildcards in folder matchers by default.
    
    * imap4d/imap4d.h: Include mailutils/imaputil.h
    * imap4d/list.c: Use mu_folder_imap_match/mu_imap_wildmatch.
    * imap4d/lsub.c: Likewise.
    * imap4d/util.c (util_wcard_match): Move as to libmailutils
    as mu_imap_wildmatch.
    * include/mailutils/imaputil.h: New file.
    * include/mailutils/Makefile.am (pkginclude_HEADERS): Add imaputil.h
    * include/mailutils/folder.h (mu_folder_imap_match)
    (mu_folder_glob_match): New protos.
    * include/mailutils/imapio.h (mu_imap_flag_to_attribute)
    (mu_imap_format_flags): Move to imaputil.h
    * libmailutils/imapio/wildmatch.c: New file.
    * libmailutils/imapio/sendflg.c: New file.
    * libmailutils/imapio/Makefile.am (libimapio_la_SOURCES): Add
    sendflg.c and wildmatch.c.
    * libmailutils/imapio/flags.c (mu_imapio_send_flags): Move to
    sendflg.c
    * libmailutils/mailbox/folder.c (mu_folder_match): Rename to
    mu_folder_glob_match.
    (mu_folder_imap_match): New function.
    (mu_folder_create_from_record): Use mu_folder_imap_match as the default
    matcher.
    * libmailutils/tests/fsfolder.c: Start command line options with a dash.
    New option "-glob".  All uses changed.
    * libproto/imap/select.c: Include imaputil.h
    * mu/imap.c: Likewise.

commit b71b1bb14085900051070cb82ad334ce67f14d96
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 15 02:49:48 2011 +0200

    Provisions for loading python modules from the source tree.
    
    * python/Makefile.am (EXTRA_DIST): Add usercustomize.py
    * python/usercustomize.py: New file.
    * python/libmu_py/Makefile.am (pythonexec_LTLIBRARIES): Remove.
    * python/mailutils/Makefile.am (pythonexec_LTLIBRARIES): New
    variable (moved from ../libmu_py/Makefile.am)
    * python/libmu_py/c_api.c: Move ...
    * python/mailutils/c_api.c: ... there

commit dd08262c4c55b984e9b38a3dc4a330960d01e26f
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 16 22:21:44 2011 +0200

    Add a test suite for folder delete method.
    
    * testsuite/Makefile.am: Add new test cases.
    * testsuite/testsuite.at: Inclide fldel.at, add banners.
    * testsuite/atlocal.in: Define MH_SUPPORT and MAILDIR_SUPPORT
    variables depending on whether the corresponding mailbox format
    is enabled.
    * testsuite/fldel.at: New test case.
    * testsuite/mbdel.at: Skip tests which rely on disabled mailbox
    formats.

commit 33a5d85239028bc3e34eec59909253d12d5434f4
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 16 16:36:29 2011 +0200

    Share folder implementation between mbox, mh and maildir.  Fix mu_folder_delete.
    
    * include/mailutils/folder.h (_mu_fsfolder_init): New proto.
    * include/mailutils/mailbox.h (mu_mailbox_create_at): New proto.
    * libmailutils/mailbox/mailbox.c (mu_mailbox_create_at): New function.
    * libmailutils/mailbox/Makefile.am (libmailbox_la_SOURCES): Add fsfolder.c
    * libmailutils/mailbox/folder.c (mu_folder_delete): If folder does
    not provide its own method for deletion, use mu_mailbox_remove.
    (mu_folder_open, mu_folder_close, mu_folder_enumerate)
    (mu_folder_lsub, mu_folder_subscribe, mu_folder_unsubscribe)
    (mu_folder_rename): Return EINVAL if folder is NULL.
    (mu_folder_match): Bugfix: don't pass folder flags to fnmatch.
    * libmailutils/mailbox/fsfolder.c: New file. Implementation of
    file-system based folders.
    * libmailutils/mailbox/mailbox (_create_mailbox0): Propagate error
    return from mu_registrar_lookup_url.
    * libmailutils/tests/fsfolder00.at: New test case.
    * libmailutils/tests/fsfolder01.at: New test case.
    * libmailutils/tests/fsfolder02.at: New test case.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add fsfolder.
    (TESTSUITE_AT): Add fsfolder tests.
    * libmailutils/tests/testsuite.at: Include fsfolder tests.
    
    * libproto/mbox/folder.c: Use fsfolder.
    (_mh_is_scheme): Check file even if scheme matches.
    * libproto/maildir/folder.c: Likewise.
    (_maildir_is_scheme): Check file even if scheme matches.
    * libproto/mh/folder.c: Likewise.
    
    * testsuite/fldel.c: New file.
    * testsuite/Makefile.am (noinst_PROGRAMS): Buld fldel.

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

Summary of changes:
 NEWS                                               |    5 +-
 configure.ac                                       |    2 +-
 imap4d/append.c                                    |    2 +-
 imap4d/copy.c                                      |    3 +-
 imap4d/create.c                                    |    7 +-
 imap4d/delete.c                                    |    3 +-
 imap4d/imap4d.c                                    |    3 +-
 imap4d/imap4d.h                                    |   12 +-
 imap4d/list.c                                      |   29 +-
 imap4d/lsub.c                                      |    7 +-
 imap4d/namespace.c                                 |    8 +-
 imap4d/quota.c                                     |    2 +-
 imap4d/rename.c                                    |    7 +-
 imap4d/select.c                                    |    2 +-
 imap4d/status.c                                    |    3 +-
 imap4d/util.c                                      |   76 +---
 include/mailutils/Makefile.am                      |    1 +
 include/mailutils/folder.h                         |    8 +
 include/mailutils/imapio.h                         |    3 -
 .../tzlocal.c => include/mailutils/imaputil.h      |   24 +-
 include/mailutils/mailbox.h                        |    2 +
 include/mailutils/util.h                           |    5 +-
 libmailutils/base/tilde.c                          |   10 +-
 libmailutils/cfg/driver.c                          |    2 +-
 libmailutils/cfg/lexer.l                           |    2 +-
 libmailutils/imapio/Makefile.am                    |    2 +
 libmailutils/imapio/flags.c                        |   15 +-
 libmailutils/imapio/{printf.c => sendflg.c}        |   21 +-
 libmailutils/imapio/wildmatch.c                    |   78 +++
 libmailutils/mailbox/Makefile.am                   |    1 +
 libmailutils/mailbox/folder.c                      |   77 +++-
 .../folder.c => libmailutils/mailbox/fsfolder.c    |  454 +++++++----------
 libmailutils/mailbox/mailbox.c                     |  116 ++++-
 libmailutils/mailbox/mbx_default.c                 |    5 +-
 libmailutils/tests/.gitignore                      |    1 +
 libmailutils/tests/Makefile.am                     |    4 +
 .../imapfolder.c => libmailutils/tests/fsfolder.c  |  135 ++++--
 .../tests/fsfolder00.at                            |   43 ++-
 libmailutils/tests/fsfolder01.at                   |   74 +++
 libmailutils/tests/{strerr.at => fsfolder02.at}    |   28 +-
 libmailutils/tests/testsuite.at                    |    5 +
 libmu_sieve/extensions/vacation.c                  |    2 +-
 libproto/imap/select.c                             |    1 +
 libproto/imap/tests/imapfolder.c                   |    1 -
 libproto/maildir/folder.c                          |   45 +-
 libproto/mailer/smtp_auth.c                        |    3 +-
 libproto/mbox/folder.c                             |  517 +-------------------
 libproto/mh/folder.c                               |   37 +-
 mail/util.c                                        |    2 +-
 mh/install-mh.c                                    |    2 +-
 mh/mh_global.c                                     |    2 +-
 mh/mh_init.c                                       |    8 +-
 mh/mhn.c                                           |    3 +-
 mh/send.c                                          |    2 +-
 mu/imap.c                                          |    1 +
 mu/shell.c                                         |    2 +-
 mu/wicket.c                                        |    2 +-
 pop3d/bulletin.c                                   |    6 +-
 python/Makefile.am                                 |    1 +
 python/libmu_py/Makefile.am                        |    5 -
 python/mailutils/Makefile.am                       |    8 +
 python/{libmu_py => mailutils}/c_api.c             |    0
 python/usercustomize.py                            |   63 +++
 sieve/sieve.c                                      |    2 +-
 testsuite/.gitignore                               |    1 +
 testsuite/Makefile.am                              |    2 +
 testsuite/atlocal.in                               |    2 +
 testsuite/fldel.at                                 |   81 +++
 testsuite/{mbdel.c => fldel.c}                     |   12 +-
 testsuite/mbdel.at                                 |   16 +-
 testsuite/testsuite.at                             |   10 +
 71 files changed, 1021 insertions(+), 1105 deletions(-)
 copy libmailutils/datetime/tzlocal.c => include/mailutils/imaputil.h (66%)
 copy libmailutils/imapio/{printf.c => sendflg.c} (73%)
 create mode 100644 libmailutils/imapio/wildmatch.c
 copy libproto/mbox/folder.c => libmailutils/mailbox/fsfolder.c (55%)
 copy libproto/imap/tests/imapfolder.c => libmailutils/tests/fsfolder.c (59%)
 copy imap4d/tests/create02.at => libmailutils/tests/fsfolder00.at (55%)
 create mode 100644 libmailutils/tests/fsfolder01.at
 copy libmailutils/tests/{strerr.at => fsfolder02.at} (69%)
 rename python/{libmu_py => mailutils}/c_api.c (100%)
 create mode 100644 python/usercustomize.py
 create mode 100644 testsuite/fldel.at
 copy testsuite/{mbdel.c => fldel.c} (85%)

diff --git a/NEWS b/NEWS
index a50433b..25cf5d1 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-GNU mailutils NEWS -- history of user-visible changes. 2011-11-23
+GNU mailutils NEWS -- history of user-visible changes. 2011-12-17
 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 Free Software Foundation, Inc.
 See the end of file for copying conditions.
@@ -29,7 +29,6 @@ experimental.  Not currently implemented are:
 Experimental features are:
 
  - C++ bindings
- - Python bindings
 
 The discussion below lists the changes in this release.  It is divided
 in three major sections, each addressing a particular audience.  The
@@ -158,7 +157,7 @@ This can be disabled using the --without-gsasl option.
 
 ** Experimental features
 
-The C++ and Python bindings are considered experimental and unstable.
+The C++ bindings are considered experimental and unstable.
 
 * Changes to the library
 
diff --git a/configure.ac b/configure.ac
index 4edf484..9472fb7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1167,7 +1167,7 @@ case "${enableval}" in
   yes) status_python=yes ;;
   no)  status_python=no ;;
   *)   AC_MSG_ERROR(bad value ${enableval} for --enable-python) ;;
-esac],[status_python=$mu_build_experimental])
+esac],[status_python=yes])
 
 AC_SUBST(PYTHON_LIBS)
 AC_SUBST(PYTHON_INCLUDES)
diff --git a/imap4d/append.c b/imap4d/append.c
index 74aebef..df5abab 100644
--- a/imap4d/append.c
+++ b/imap4d/append.c
@@ -252,7 +252,7 @@ imap4d_append (struct imap4d_command *command, imap4d_tokbuf_t tok)
 
   msg_text = imap4d_tokbuf_getarg (tok, i);
   
-  mboxname = namespace_getfullpath (mboxname, "/", NULL);
+  mboxname = namespace_getfullpath (mboxname, NULL);
   if (!mboxname)
     return io_completion_response (command, RESP_NO, "Couldn't open mailbox"); 
 
diff --git a/imap4d/copy.c b/imap4d/copy.c
index 607c278..5e60487 100644
--- a/imap4d/copy.c
+++ b/imap4d/copy.c
@@ -232,7 +232,6 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
   mu_list_t msglist;
   char *name;
   char *mailbox_name;
-  const char *delim = "/";
   char *end;
   mu_mailbox_t cmbox = NULL;
   int arg = IMAP4_ARG_1 + !!isuid;
@@ -256,7 +255,7 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
       return RESP_BAD;
     }
 
-  mailbox_name = namespace_getfullpath (name, delim, &ns);
+  mailbox_name = namespace_getfullpath (name, &ns);
 
   if (!mailbox_name)
     {
diff --git a/imap4d/create.c b/imap4d/create.c
index b65f419..ff70619 100644
--- a/imap4d/create.c
+++ b/imap4d/create.c
@@ -37,7 +37,6 @@ int
 imap4d_create (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
   char *name;
-  const char *delim = "/";
   int isdir = 0;
   int ns;
   int rc = RESP_OK;
@@ -63,11 +62,11 @@ imap4d_create (struct imap4d_command *command, imap4d_tokbuf_t tok)
      The trailing delimiter will be removed by namespace normalizer, so
      test for it now.
   */
-  if (name[strlen (name) - 1] == delim[0])
+  if (name[strlen (name) - 1] == MU_HIERARCHY_DELIMITER)
     isdir = 1;
   
   /* Allocates memory.  */
-  name = namespace_getfullpath (name, delim, &ns);
+  name = namespace_getfullpath (name, &ns);
 
   if (!name)
     return io_completion_response (command, RESP_NO, "Cannot create mailbox");
@@ -75,7 +74,7 @@ imap4d_create (struct imap4d_command *command, imap4d_tokbuf_t tok)
   /* It will fail if the mailbox already exists.  */
   if (access (name, F_OK) != 0)
     {
-      if (make_interdir (name, delim[0], MKDIR_PERMISSIONS))
+      if (make_interdir (name, MU_HIERARCHY_DELIMITER, MKDIR_PERMISSIONS))
 	{
 	  rc = RESP_NO;
 	  msg = "Cannot create mailbox";
diff --git a/imap4d/delete.c b/imap4d/delete.c
index aed74ee..9fe8b3d 100644
--- a/imap4d/delete.c
+++ b/imap4d/delete.c
@@ -33,7 +33,6 @@ imap4d_delete (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
   int rc = RESP_OK;
   const char *msg = "Completed";
-  const char *delim = "/";
   char *name;
   mu_mailbox_t tmpbox;
   
@@ -49,7 +48,7 @@ imap4d_delete (struct imap4d_command *command, imap4d_tokbuf_t tok)
     return io_completion_response (command, RESP_NO, "Already exist");
 
  /* Allocates memory.  */
-  name = namespace_getfullpath (name, delim, NULL);
+  name = namespace_getfullpath (name, NULL);
   if (!name)
     return io_completion_response (command, RESP_NO, "Cannot remove");
 
diff --git a/imap4d/imap4d.c b/imap4d/imap4d.c
index 3fbbf8f..0d93abb 100644
--- a/imap4d/imap4d.c
+++ b/imap4d/imap4d.c
@@ -325,7 +325,8 @@ imap4d_session_setup0 ()
 
   if (modify_homedir)
     {
-      char *expr = mu_tilde_expansion (modify_homedir, "/", real_homedir);
+      char *expr = mu_tilde_expansion (modify_homedir, MU_HIERARCHY_DELIMITER,
+				       real_homedir);
       struct mu_wordsplit ws;
       const char *env[5];
 
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 09e7f64..f652c1a 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -106,6 +106,7 @@
 #include <mailutils/io.h>
 #include <mailutils/prog.h>
 #include <mailutils/imapio.h>
+#include <mailutils/imaputil.h>
 
 #include <mu_umaxtostr.h>
 #include <muaux.h>
@@ -349,10 +350,9 @@ extern mu_list_t namespace[NS_MAX];
   
 extern int namespace_init_session (char *path);
 extern void namespace_init (void);
-extern char *namespace_getfullpath (const char *name, const char *delim,
-				    int *pns);
+extern char *namespace_getfullpath (const char *name, int *pns);
 extern char *namespace_checkfullpath (const char *name, const char *pattern,
-				       const char *delim, int *pns);
+				      int *pns);
 int imap4d_session_setup (char *username);
 int imap4d_session_setup0 (void);
 void imap4d_child_signal_setup (RETSIGTYPE (*handler) (int signo));
@@ -367,8 +367,7 @@ extern void imap4d_capability_init (void);
 extern int  util_start (char *);
 extern int  util_getstate (void);
 extern int  util_do_command (imap4d_tokbuf_t);
-extern char *util_tilde_expansion (const char *, const char *);
-extern char *util_getfullpath (const char *, const char *);
+extern char *util_getfullpath (const char *);
 extern struct imap4d_command *util_getcommand (char *, 
                                                struct imap4d_command []);
 
@@ -396,9 +395,6 @@ extern int util_parse_ctime_date (const char *date, time_t *timep,
 extern char *util_strcasestr (const char *haystack, const char *needle);
 extern char *util_localname (void);
 
-extern int util_wcard_match (const char *string, const char *pattern,
-			     const char *delim);
-
 void util_print_flags (mu_attribute_t attr);
 int util_attribute_matches_flag (mu_attribute_t attr, const char *item);
 int util_uidvalidity (mu_mailbox_t smbox, unsigned long *uidvp);
diff --git a/imap4d/list.c b/imap4d/list.c
index 82299d6..89b0f1f 100644
--- a/imap4d/list.c
+++ b/imap4d/list.c
@@ -19,12 +19,6 @@
 #include <dirent.h>
 #include <pwd.h>
 
-static int
-imap4d_match (const char *name, void *pat, int flags)
-{
-  return util_wcard_match (name, pat, "/");
-}
-
 struct refinfo
 {
   char *refptr;   /* Original reference */
@@ -141,7 +135,6 @@ imap4d_list (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
   char *ref;
   char *wcard;
-  const char *delim = "/";
 
   if (imap4d_tokbuf_argc (tok) != 4)
     return io_completion_response (command, RESP_BAD, "Invalid arguments");
@@ -153,9 +146,15 @@ imap4d_list (struct imap4d_command *command, imap4d_tokbuf_t tok)
      return the hierarchy.  */
   if (*wcard == '\0')
     {
-      io_untagged_response (RESP_NONE,
-                               "LIST (\\NoSelect) \"%s\" \"%s\"", delim,
-		               (*ref) ? delim : "");
+      if (*ref)
+	io_untagged_response (RESP_NONE,
+			      "LIST (\\NoSelect) \"%c\" \"%c\"",
+			      MU_HIERARCHY_DELIMITER,
+			      MU_HIERARCHY_DELIMITER);
+      else
+	io_untagged_response (RESP_NONE,
+			      "LIST (\\NoSelect) \"%c\" \"\"",
+			      MU_HIERARCHY_DELIMITER);
     }
   /* There is only one mailbox in the "INBOX" hierarchy ... INBOX.  */
   else if (mu_c_strcasecmp (ref, "INBOX") == 0
@@ -228,7 +227,7 @@ imap4d_list (struct imap4d_command *command, imap4d_tokbuf_t tok)
 	}
 
       /* Allocates.  */
-      cwd = namespace_checkfullpath (ref, wcard, delim, NULL);
+      cwd = namespace_checkfullpath (ref, wcard, NULL);
       if (!cwd)
 	{
 	  free (ref);
@@ -243,7 +242,8 @@ imap4d_list (struct imap4d_command *command, imap4d_tokbuf_t tok)
 	  return io_completion_response (command, RESP_NO,
 			              "The requested item could not be found.");
 	}
-      mu_folder_set_match (folder, imap4d_match);
+      /* Force the right matcher */
+      mu_folder_set_match (folder, mu_folder_imap_match);
 
       memset (&refinfo, 0, sizeof refinfo);
 
@@ -260,8 +260,9 @@ imap4d_list (struct imap4d_command *command, imap4d_tokbuf_t tok)
 	 failure; it is not relevant whether the user's real INBOX resides
 	 on this or some other server. */
 
-      if (!*ref && (imap4d_match ("INBOX", wcard, 0) == 0
-		    || imap4d_match ("inbox", wcard, 0) == 0))
+      if (!*ref &&
+	  (mu_imap_wildmatch (wcard, "INBOX", MU_HIERARCHY_DELIMITER) == 0
+	   || mu_imap_wildmatch (wcard, "inbox", MU_HIERARCHY_DELIMITER) == 0))
 	io_untagged_response (RESP_NONE, "LIST (\\NoInferiors) NIL INBOX");
 
       mu_folder_enumerate (folder, NULL, wcard, 0, 0, NULL,
diff --git a/imap4d/lsub.c b/imap4d/lsub.c
index 1ee5114..1b7f90d 100644
--- a/imap4d/lsub.c
+++ b/imap4d/lsub.c
@@ -37,7 +37,6 @@ imap4d_lsub (struct imap4d_command *command, imap4d_tokbuf_t tok)
   char *ref;
   char *wcard;
   char *pattern;
-  const char *delim = "/";
   mu_property_t prop;
   mu_iterator_t itr;
   
@@ -64,9 +63,9 @@ imap4d_lsub (struct imap4d_command *command, imap4d_tokbuf_t tok)
 	  
 	  mu_iterator_current_kv (itr, (const void **)&name, (void**)&val);
 
-	  if (util_wcard_match (name, pattern, delim) == 0)
-	    io_untagged_response (RESP_NONE, "LSUB () \"%s\" \"%s\"",
-				  delim, name);
+	  if (mu_imap_wildmatch (pattern, name, MU_HIERARCHY_DELIMITER) == 0)
+	    io_untagged_response (RESP_NONE, "LSUB () \"%c\" \"%s\"",
+				  MU_HIERARCHY_DELIMITER, name);
 	}
     }
   else
diff --git a/imap4d/namespace.c b/imap4d/namespace.c
index bee550a..9ffbd3b 100644
--- a/imap4d/namespace.c
+++ b/imap4d/namespace.c
@@ -166,7 +166,7 @@ risky_pattern (const char *pattern, int delim)
 
 char *
 namespace_checkfullpath (const char *name, const char *pattern, 
-			 const char *delim, int *nspace)
+			 int *nspace)
 {
   struct namespace_info info;
   const char *p;
@@ -185,7 +185,7 @@ namespace_checkfullpath (const char *name, const char *pattern,
       name = p + 1;
     }
 
-  path = util_getfullpath (name, delim);
+  path = util_getfullpath (name);
   if (!path)
     {
       free (scheme);
@@ -226,7 +226,7 @@ namespace_checkfullpath (const char *name, const char *pattern,
 }
 
 char *
-namespace_getfullpath (const char *name, const char *delim, int *nspace)
+namespace_getfullpath (const char *name, int *nspace)
 {
   char *ret;
   if (mu_c_strcasecmp (name, "INBOX") == 0 && auth_data->change_uid)
@@ -236,7 +236,7 @@ namespace_getfullpath (const char *name, const char *delim, int *nspace)
 	*nspace = NS_PRIVATE;
     }
   else
-    ret = namespace_checkfullpath (name, NULL, delim, nspace);
+    ret = namespace_checkfullpath (name, NULL, nspace);
   return ret;
 }
 
diff --git a/imap4d/quota.c b/imap4d/quota.c
index 2b1e9ae..878dec7 100644
--- a/imap4d/quota.c
+++ b/imap4d/quota.c
@@ -138,7 +138,7 @@ quota_check (mu_off_t size)
 
   total = used_size;
 
-  mailbox_name = namespace_getfullpath ("INBOX", "/", NULL);
+  mailbox_name = namespace_getfullpath ("INBOX", NULL);
   rc = mu_mailbox_create (&mbox, mailbox_name);
   if (rc)
     {
diff --git a/imap4d/rename.c b/imap4d/rename.c
index 24fa65e..440a498 100644
--- a/imap4d/rename.c
+++ b/imap4d/rename.c
@@ -110,7 +110,6 @@ imap4d_rename (struct imap4d_command *command, imap4d_tokbuf_t tok)
   int rc = RESP_OK;
   const char *msg = "Completed";
   struct stat newst;
-  const char *delim = "/";
   int ns;
   
   if (imap4d_tokbuf_argc (tok) != 4)
@@ -123,7 +122,7 @@ imap4d_rename (struct imap4d_command *command, imap4d_tokbuf_t tok)
     return io_completion_response (command, RESP_NO, "Name Inbox is reservered");
 
   /* Allocates memory.  */
-  newname = namespace_getfullpath (newname, delim, &ns);
+  newname = namespace_getfullpath (newname, &ns);
   if (!newname)
     return io_completion_response (command, RESP_NO, "Permission denied");
 
@@ -140,7 +139,7 @@ imap4d_rename (struct imap4d_command *command, imap4d_tokbuf_t tok)
 	}
     }
 
-  if (make_interdir (newname, delim[0], MKDIR_PERMISSIONS))
+  if (make_interdir (newname, MU_HIERARCHY_DELIMITER, MKDIR_PERMISSIONS))
     {
       free (newname);
       return io_completion_response (command, RESP_NO, "Cannot rename");
@@ -202,7 +201,7 @@ imap4d_rename (struct imap4d_command *command, imap4d_tokbuf_t tok)
       return io_completion_response (command, RESP_OK, "Rename successful");
     }
 
-  oldname = namespace_getfullpath (oldname, delim, NULL);
+  oldname = namespace_getfullpath (oldname, NULL);
 
   /* It must exist.  */
   /* FIXME: 1. What if odlname or newname is a remote mailbox?
diff --git a/imap4d/select.c b/imap4d/select.c
index 8a61ad5..42cdc8c 100644
--- a/imap4d/select.c
+++ b/imap4d/select.c
@@ -58,7 +58,7 @@ imap4d_select0 (struct imap4d_command *command, const char *mboxname,
 
   if (strcmp (mboxname, "INBOX") == 0)
     flags |= MU_STREAM_CREAT;
-  mailbox_name = namespace_getfullpath (mboxname, "/", NULL);
+  mailbox_name = namespace_getfullpath (mboxname, NULL);
 
   if (!mailbox_name)
     return io_completion_response (command, RESP_NO, "Couldn't open mailbox");
diff --git a/imap4d/status.c b/imap4d/status.c
index d0bd445..549a2fc 100644
--- a/imap4d/status.c
+++ b/imap4d/status.c
@@ -69,7 +69,6 @@ imap4d_status (struct imap4d_command *command, imap4d_tokbuf_t tok)
 {
   char *name;
   char *mailbox_name;
-  const char *delim = "/";
   mu_mailbox_t smbox = NULL;
   int status;
   int count = 0;
@@ -81,7 +80,7 @@ imap4d_status (struct imap4d_command *command, imap4d_tokbuf_t tok)
   
   name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1);
 
-  mailbox_name = namespace_getfullpath (name, delim, NULL);
+  mailbox_name = namespace_getfullpath (name, NULL);
 
   if (!mailbox_name)
     return io_completion_response (command, RESP_NO, "Error opening mailbox");
diff --git a/imap4d/util.c b/imap4d/util.c
index e520735..5ade76b 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -17,28 +17,21 @@
 
 #include "imap4d.h"
 
-/* NOTE: Allocates Memory.  */
-/* Expand: ~ --> /home/user, and ~guest --> /home/guest.  */
-char *
-util_tilde_expansion (const char *ref, const char *delim)
-{
-  return mu_tilde_expansion (ref, delim, imap4d_homedir);
-}
-
 /* Get the absolute path.  */
 /* NOTE: Path is allocated and must be free()d by the caller.  */
 char *
-util_getfullpath (const char *name, const char *delim)
+util_getfullpath (const char *name)
 {
-  char *exp = util_tilde_expansion (name, delim);
-  if (*exp != delim[0])
+  char *exp = mu_tilde_expansion (name, MU_HIERARCHY_DELIMITER,
+				  imap4d_homedir);
+  if (*exp != MU_HIERARCHY_DELIMITER)
     {
       char *p, *s =
-	malloc (strlen (imap4d_homedir) + strlen (delim) + strlen (exp) + 1);
+	malloc (strlen (imap4d_homedir) + 1 + strlen (exp) + 1);
       if (!s)
 	imap4d_bye (ERR_NO_MEM);
       p = mu_stpcpy (s, imap4d_homedir);
-      p = mu_stpcpy (p, (char*) delim);
+      *p++ = MU_HIERARCHY_DELIMITER;
       strcpy (p, exp);
       free (exp);
       exp = s;
@@ -566,63 +559,6 @@ util_localname ()
   return localname;
 }
 
-/* Match STRING against the IMAP4 wildcard pattern PATTERN. */
-
-#define WILD_FALSE 0
-#define WILD_TRUE  1
-#define WILD_ABORT 2
-
-int
-_wild_match (const char *expr, const char *name, char delim)
-{
-  while (expr && *expr)
-    {
-      if (*name == 0 && *expr != '*')
-	return WILD_ABORT;
-      switch (*expr)
-	{
-	case '*':
-	  while (*++expr == '*')
-	    ;
-	  if (*expr == 0)
-	    return WILD_TRUE;
-	  while (*name)
-	    {
-	      int res = _wild_match (expr, name++, delim);
-	      if (res != WILD_FALSE)
-		return res;
-	    }
-	  return WILD_ABORT;
-
-	case '%':
-	  while (*++expr == '%')
-	    ;
-	  if (*expr == 0)
-	    return strchr (name, delim) ? WILD_FALSE : WILD_TRUE;
-	  while (*name && *name != delim)
-	    {
-	      int res = _wild_match (expr, name++, delim);
-	      if (res != WILD_FALSE)
-		return res;
-	    }
-	  return _wild_match (expr, name, delim);
-	  
-	default:
-	  if (*expr != *name)
-	    return WILD_FALSE;
-	  expr++;
-	  name++;
-	}
-    }
-  return *name == 0;
-}
-
-int
-util_wcard_match (const char *name, const char *expr, const char *delim)
-{
-  return _wild_match (expr, name, delim[0]) != WILD_TRUE;
-}
-
 /* Return the uindvalidity of a mailbox.
    When a mailbox is selected, whose first message does not keep X-UIDVALIDITY
    value, the uidvalidity is computed basing on the return of time(). Now,
diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am
index a7b116d..53ea5e4 100644
--- a/include/mailutils/Makefile.am
+++ b/include/mailutils/Makefile.am
@@ -54,6 +54,7 @@ pkginclude_HEADERS = \
  header.h\
  imap.h\
  imapio.h\
+ imaputil.h\
  io.h\
  iterator.h\
  kwd.h\
diff --git a/include/mailutils/folder.h b/include/mailutils/folder.h
index e8f6cb9..c3da0a3 100644
--- a/include/mailutils/folder.h
+++ b/include/mailutils/folder.h
@@ -72,6 +72,12 @@ extern int  mu_folder_set_stream     (mu_folder_t, mu_stream_t);
 extern int mu_folder_set_match (mu_folder_t folder, mu_folder_match_fp pmatch);
 extern int mu_folder_get_match (mu_folder_t folder,
 				mu_folder_match_fp *pmatch);
+
+  /* Two often used matchers: */
+  /* 1. The default: IMAP-style wildcards: */
+extern int mu_folder_imap_match (const char *name, void *pattern, int flags);
+  /* 2. UNIX-style glob(7) wildcards: */
+extern int mu_folder_glob_match (const char *name, void *pattern, int flags);
   
   /* Notifications.  */
 extern int  mu_folder_get_observable (mu_folder_t, mu_observable_t *);
@@ -92,6 +98,8 @@ extern int  mu_folder_get_property   (mu_folder_t, mu_property_t *);
 extern int  mu_folder_decrement      (mu_folder_t);
 
 extern void mu_list_response_free    (void *data);
+
+int _mu_fsfolder_init (mu_folder_t folder);
   
 #ifdef __cplusplus
 }
diff --git a/include/mailutils/imapio.h b/include/mailutils/imapio.h
index 87f55ce..d038744 100644
--- a/include/mailutils/imapio.h
+++ b/include/mailutils/imapio.h
@@ -72,9 +72,6 @@ int mu_imapio_reply_string (mu_imapio_t io, size_t start, char **pbuf);
 int mu_imapio_last_error (mu_imapio_t io);
 void mu_imapio_clearerr (mu_imapio_t io);
   
-int mu_imap_flag_to_attribute (const char *item, int *attr);
-int mu_imap_format_flags (mu_stream_t str, int flags);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/libmailutils/datetime/tzlocal.c b/include/mailutils/imaputil.h
similarity index 66%
copy from libmailutils/datetime/tzlocal.c
copy to include/mailutils/imaputil.h
index cc561f5..09ddce4 100644
--- a/libmailutils/datetime/tzlocal.c
+++ b/include/mailutils/imaputil.h
@@ -14,14 +14,22 @@
    You should have received a copy of the GNU General Public License
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
+#ifndef _MAILUTILS_IMAPUTIL_H
+# define _MAILUTILS_IMAPUTIL_H
+
+#ifdef __cplusplus
+extern "C" {
 #endif
-#include <mailutils/datetime.h>
 
-void
-mu_datetime_tz_local (struct mu_timezone *tz)
-{
-  tz->utc_offset = mu_utc_offset ();
-  tz->tz_name = NULL;
+# include <mailutils/types.h>
+
+int mu_imap_wildmatch (const char *pattern, const char *name, int delim);
+
+int mu_imap_flag_to_attribute (const char *item, int *attr);
+int mu_imap_format_flags (mu_stream_t str, int flags);
+
+#ifdef __cplusplus
 }
+#endif
+
+#endif /* _MAILUTILS_IMAPUTIL_H */
diff --git a/include/mailutils/mailbox.h b/include/mailutils/mailbox.h
index 8969859..f21dc89 100644
--- a/include/mailutils/mailbox.h
+++ b/include/mailutils/mailbox.h
@@ -43,6 +43,8 @@ extern int  mu_mailbox_create_from_record (mu_mailbox_t *pmbox,
 					   mu_record_t record,
 					   const char *name);
 extern int  mu_mailbox_create_default  (mu_mailbox_t *, const char *);
+extern int mu_mailbox_create_at (mu_mailbox_t *pmbox, mu_folder_t folder,
+				 const char *name);
 
 extern void mu_mailbox_destroy         (mu_mailbox_t *);
 
diff --git a/include/mailutils/util.h b/include/mailutils/util.h
index 237ff3f..b28d8ea 100644
--- a/include/mailutils/util.h
+++ b/include/mailutils/util.h
@@ -47,11 +47,14 @@ int mu_str_url_decode (char **ptr, const char *s);
   /* ----------------------- */
   /* File & path names.      */
   /* ----------------------- */
+  
+#define MU_HIERARCHY_DELIMITER '/'
+  
 char *mu_get_homedir (void);
 char *mu_get_full_path (const char *file);
 char *mu_normalize_path (char *path);
 char *mu_expand_path_pattern (const char *pattern, const char *username);
-char *mu_tilde_expansion (const char *ref, const char *delim,
+char *mu_tilde_expansion (const char *ref, int delim,
 			  const char *homedir);
 int mu_readlink (const char *name, char **pbuf, size_t *psize, size_t *plen);
 int mu_unroll_symlink (const char *name, char **pout);
diff --git a/libmailutils/base/tilde.c b/libmailutils/base/tilde.c
index 900ffc2..6a4a7cc 100644
--- a/libmailutils/base/tilde.c
+++ b/libmailutils/base/tilde.c
@@ -31,7 +31,7 @@
 /* NOTE: Allocates Memory.  */
 /* Expand: ~ --> /home/user and to ~guest --> /home/guest.  */
 char *
-mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
+mu_tilde_expansion (const char *ref, int delim, const char *homedir)
 {
   char *base = strdup (ref);
   char *home = NULL;
@@ -52,9 +52,9 @@ mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
       memcpy (proto, base, proto_len);
       proto[proto_len] = 0;
       /* Allow for extra pair of slashes after the protocol specifier */
-      if (*p == delim[0])
+      if (*p == delim)
 	p++;
-      if (*p == delim[0])
+      if (*p == delim)
 	p++;
     }
   else
@@ -63,7 +63,7 @@ mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
   if (*p == '~')
     {
       p++;
-      if (*p == delim[0] || *p == '\0')
+      if (*p == delim || *p == '\0')
         {
 	  char *s;
 	  if (!homedir)
@@ -88,7 +88,7 @@ mu_tilde_expansion (const char *ref, const char *delim, const char *homedir)
           struct mu_auth_data *auth;
           char *s = p;
           char *name;
-          while (*s && *s != delim[0])
+          while (*s && *s != delim)
             s++;
           name = calloc (s - p + 1, 1);
           memcpy (name, p, s - p);
diff --git a/libmailutils/cfg/driver.c b/libmailutils/cfg/driver.c
index 47ca794..25ed2ab 100644
--- a/libmailutils/cfg/driver.c
+++ b/libmailutils/cfg/driver.c
@@ -503,7 +503,7 @@ mu_parse_config (const char *file, const char *progname,
 		 void *target_ptr)
 {
   int rc;
-  char *full_name = mu_tilde_expansion (file, "/", NULL);
+  char *full_name = mu_tilde_expansion (file, MU_HIERARCHY_DELIMITER, NULL);
   if (full_name)
     {
       if (access (full_name, R_OK) == 0)
diff --git a/libmailutils/cfg/lexer.l b/libmailutils/cfg/lexer.l
index fcf6fdd..ded4d70 100644
--- a/libmailutils/cfg/lexer.l
+++ b/libmailutils/cfg/lexer.l
@@ -326,7 +326,7 @@ mu_cfg_parse_file (mu_cfg_tree_t **return_tree, const char *file, int flags)
   struct stat st;
   FILE *fp;
   int rc;
-  char *full_name = mu_tilde_expansion (file, "/", NULL);
+  char *full_name = mu_tilde_expansion (file, MU_HIERARCHY_DELIMITER, NULL);
 
   if (stat (full_name, &st))
     {
diff --git a/libmailutils/imapio/Makefile.am b/libmailutils/imapio/Makefile.am
index d196b50..beb590b 100644
--- a/libmailutils/imapio/Makefile.am
+++ b/libmailutils/imapio/Makefile.am
@@ -31,9 +31,11 @@ libimapio_la_SOURCES = \
  sendcmd.c\
  sendcmdv.c\
  sendcmde.c\
+ sendflg.c\
  time.c\
  trace.c\
  transport.c\
+ wildmatch.c\
  words.c\
  xscript.c
 
diff --git a/libmailutils/imapio/flags.c b/libmailutils/imapio/flags.c
index ed20726..8bee66d 100644
--- a/libmailutils/imapio/flags.c
+++ b/libmailutils/imapio/flags.c
@@ -21,7 +21,7 @@
 #include <mailutils/stream.h>
 #include <mailutils/errno.h>
 #include <mailutils/cstr.h>
-#include <mailutils/sys/imapio.h>
+#include <mailutils/imaputil.h>
 
 static struct
 {
@@ -84,16 +84,3 @@ mu_imap_format_flags (mu_stream_t str, int flags)
   return 0;
 }
 
-int
-mu_imapio_send_flags (struct _mu_imapio *io, int flags)
-{
-  int rc;
-
-  rc = mu_stream_write (io->_imap_stream, "(", 1, NULL);
-  if (rc)
-    return rc;
-  rc = mu_imap_format_flags (io->_imap_stream, flags);
-  if (rc == 0)
-    rc = mu_stream_write (io->_imap_stream, ")", 1, NULL);
-  return rc;
-}
diff --git a/libmailutils/imapio/printf.c b/libmailutils/imapio/sendflg.c
similarity index 73%
copy from libmailutils/imapio/printf.c
copy to libmailutils/imapio/sendflg.c
index 801e82a..b4e0d3b 100644
--- a/libmailutils/imapio/printf.c
+++ b/libmailutils/imapio/sendflg.c
@@ -15,21 +15,22 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdarg.h>
 #include <mailutils/types.h>
 #include <mailutils/imapio.h>
 #include <mailutils/stream.h>
+#include <mailutils/imaputil.h>
 #include <mailutils/sys/imapio.h>
 
 int
-mu_imapio_printf (mu_imapio_t io, const char *fmt, ...)
+mu_imapio_send_flags (struct _mu_imapio *io, int flags)
 {
-  va_list ap;
-  int status;
-  
-  va_start (ap, fmt);
-  status = mu_stream_vprintf (io->_imap_stream, fmt, ap);
-  va_end (ap);
-  return status;
+  int rc;
+
+  rc = mu_stream_write (io->_imap_stream, "(", 1, NULL);
+  if (rc)
+    return rc;
+  rc = mu_imap_format_flags (io->_imap_stream, flags);
+  if (rc == 0)
+    rc = mu_stream_write (io->_imap_stream, ")", 1, NULL);
+  return rc;
 }
-    
diff --git a/libmailutils/imapio/wildmatch.c b/libmailutils/imapio/wildmatch.c
new file mode 100644
index 0000000..ee4e720
--- /dev/null
+++ b/libmailutils/imapio/wildmatch.c
@@ -0,0 +1,78 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <string.h>
+#include <mailutils/types.h>
+#include <mailutils/imaputil.h>
+
+/* Match STRING against the IMAP4 wildcard pattern PATTERN. */
+
+#define WILD_FALSE 0
+#define WILD_TRUE  1
+#define WILD_ABORT 2
+
+int
+_wild_match (const char *pat, const char *name, char delim)
+{
+  while (pat && *pat)
+    {
+      if (*name == 0 && *pat != '*')
+	return WILD_ABORT;
+      switch (*pat)
+	{
+	case '*':
+	  while (*++pat == '*')
+	    ;
+	  if (*pat == 0)
+	    return WILD_TRUE;
+	  while (*name)
+	    {
+	      int res = _wild_match (pat, name++, delim);
+	      if (res != WILD_FALSE)
+		return res;
+	    }
+	  return WILD_ABORT;
+
+	case '%':
+	  while (*++pat == '%')
+	    ;
+	  if (*pat == 0)
+	    return strchr (name, delim) ? WILD_FALSE : WILD_TRUE;
+	  while (*name && *name != delim)
+	    {
+	      int res = _wild_match (pat, name++, delim);
+	      if (res != WILD_FALSE)
+		return res;
+	    }
+	  return _wild_match (pat, name, delim);
+	  
+	default:
+	  if (*pat != *name)
+	    return WILD_FALSE;
+	  pat++;
+	  name++;
+	}
+    }
+  return *name == 0;
+}
+
+int
+mu_imap_wildmatch (const char *pattern, const char *name, int delim)
+{
+  return _wild_match (pattern, name, delim) != WILD_TRUE;
+}
+
diff --git a/libmailutils/mailbox/Makefile.am b/libmailutils/mailbox/Makefile.am
index 27e6c04..cb9e775 100644
--- a/libmailutils/mailbox/Makefile.am
+++ b/libmailutils/mailbox/Makefile.am
@@ -25,6 +25,7 @@ libmailbox_la_SOURCES =  \
  body.c\
  envelope.c\
  folder.c\
+ fsfolder.c\
  hdrfirst.c\
  hdritr.c\
  header.c\
diff --git a/libmailutils/mailbox/folder.c b/libmailutils/mailbox/folder.c
index 3314fcd..c602b19 100644
--- a/libmailutils/mailbox/folder.c
+++ b/libmailutils/mailbox/folder.c
@@ -36,6 +36,8 @@
 #include <mailutils/url.h>
 #include <mailutils/errno.h>
 #include <mailutils/property.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/imaputil.h>
 
 #include <mailutils/sys/folder.h>
 
@@ -47,9 +49,15 @@ static int is_known_folder (mu_url_t, mu_folder_t *);
 static struct mu_monitor folder_lock = MU_MONITOR_INITIALIZER;
 
 int
-mu_folder_match (const char *name, void *pattern, int flags)
+mu_folder_glob_match (const char *name, void *pattern, int flags)
 {
-  return fnmatch (pattern, name[0] == '/' ? name + 1 : name, flags);
+  return fnmatch (pattern, name[0] == '/' ? name + 1 : name, 0);
+}
+
+int
+mu_folder_imap_match (const char *name, void *pattern, int flags)
+{
+  return mu_imap_wildmatch (pattern, name, '/');
 }
 
 /* A folder could be remote (IMAP), or local(a spool directory) like $HOME/Mail
@@ -125,7 +133,7 @@ mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
 		  if (status == 0)
 		    {
 		      if (!folder->_match)
-			folder->_match = mu_folder_match;
+			folder->_match = mu_folder_imap_match;
 		      *pfolder = folder;
 		      folder->ref++;
 		      /* Put on the internal list of known folders.  */
@@ -146,7 +154,7 @@ mu_folder_create_from_record (mu_folder_t *pfolder, mu_url_t url,
 	}
     }
 
-    return MU_ERR_NOENT;
+  return MU_ERR_NOENT;
 }
 
 int
@@ -249,7 +257,9 @@ mu_folder_get_property (mu_folder_t folder, mu_property_t *prop)
 int
 mu_folder_open (mu_folder_t folder, int flags)
 {
-  if (folder == NULL || folder->_open == NULL)
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->_open == NULL)
     return ENOSYS;
   return folder->_open (folder, flags);
 }
@@ -257,7 +267,9 @@ mu_folder_open (mu_folder_t folder, int flags)
 int
 mu_folder_close (mu_folder_t folder)
 {
-  if (folder == NULL || folder->_close == NULL)
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->_close == NULL)
     return ENOSYS;
   return folder->_close (folder);
 }
@@ -381,8 +393,10 @@ mu_folder_enumerate (mu_folder_t folder, const char *name,
 		     mu_folder_enumerate_fp enumfun, void *enumdata)
 {
   int status;
-  if (folder == NULL || folder->_list == NULL)
+  if (folder == NULL)
     return EINVAL;
+  else if (folder->_list == NULL)
+    return ENOSYS;
   else
     {
       mu_list_t list = NULL;
@@ -412,7 +426,9 @@ mu_folder_lsub (mu_folder_t folder, const char *dirname, const char *basename,
 {
   int status;
   
-  if (folder == NULL || folder->_lsub == NULL)
+  if (folder == NULL)
+    return EINVAL;
+  else if (folder->_lsub == NULL)
     return ENOSYS;
   else
     {
@@ -428,31 +444,64 @@ mu_folder_lsub (mu_folder_t folder, const char *dirname, const char *basename,
 int
 mu_folder_subscribe (mu_folder_t folder, const char *name)
 {
-  if (folder == NULL || folder->_subscribe == NULL)
+  if (folder == NULL)
     return EINVAL;
+  if (folder->_subscribe == NULL)
+    return ENOSYS;
   return folder->_subscribe (folder, name);
 }
 
 int
 mu_folder_unsubscribe (mu_folder_t folder, const char *name)
 {
-  if (folder == NULL || folder->_unsubscribe == NULL)
+  if (folder == NULL)
     return EINVAL;
+  if (folder->_unsubscribe == NULL)
+    return ENOSYS;
   return folder->_unsubscribe (folder, name);
 }
 
 int
 mu_folder_delete (mu_folder_t folder, const char *name)
 {
-  if (folder == NULL || folder->_delete == NULL)
-    return ENOSYS;
-  return folder->_delete (folder, name);
+  int rc;
+  
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->_delete)
+    rc = folder->_delete (folder, name);
+  else
+    {
+      /* If there is no folder-specific _delete method, then try to create the
+	 mailbox and call mailbox delete (remove) method.  This is necessary
+	 because certain types of mailboxes share a common folder (e.g. mbox,
+	 maildir and mh all use filesystem folder), but have a different
+	 internal structure.  Supplying mu_folder_t with a knowledge of mailbox
+	 internals will harm separation of concerns.  On the other hand,
+	 removing something without looking into it may well yield undesired
+	 results.  For example, a MH mailbox can hold another mailboxes, i.e.
+	 be a folder itself.  Removing it blindly would result in removing
+	 these mailboxes as well, which is clearly not indended.
+
+	 To solve this folder and mailbox delete methods are tightly paired,
+	 but without looking into each-others internal mechanisms. */
+      mu_mailbox_t mbox;
+      rc = mu_mailbox_create_at (&mbox, folder, name);
+      if (rc == 0)
+	{
+	  rc = mu_mailbox_remove (mbox);
+	  mu_mailbox_destroy (&mbox);
+	}
+    }
+  return rc;
 }
 
 int
 mu_folder_rename (mu_folder_t folder, const char *oldname, const char *newname)
 {
-  if (folder == NULL || folder->_rename == NULL)
+  if (folder == NULL)
+    return EINVAL;
+  if (folder->_rename == NULL)
     return ENOSYS;
   return folder->_rename (folder, oldname, newname);
 }
diff --git a/libproto/mbox/folder.c b/libmailutils/mailbox/fsfolder.c
similarity index 55%
copy from libproto/mbox/folder.c
copy to libmailutils/mailbox/fsfolder.c
index 2cc23bb..fb43e33 100644
--- a/libproto/mbox/folder.c
+++ b/libmailutils/mailbox/fsfolder.c
@@ -1,4 +1,4 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
+/* Implementation of file-system folder for GNU Mailutils
    Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008,
    2010, 2011 Free Software Foundation, Inc.
 
@@ -41,169 +41,81 @@
 #include <mailutils/util.h>
 #include <mailutils/errno.h>
 #include <mailutils/debug.h>
+#include <mailutils/property.h>
+#include <mailutils/iterator.h>
 
-/* We export url parsing and the initialisation of
-   the mailbox, via the register entry/record.  */
+/* File-system folder is shared between UNIX mbox, maildir and MH
+   mailboxes.  It implements all usual folder methods, excepting
+   for _delete, which is implemented on the mailbox level.  See
+   comment to mu_folder_delete in folder.c */
+
+struct _mu_fsfolder
+{
+  char *dirname;
+  mu_property_t subscription;
+};
 
 static int
-_mbox_is_scheme (mu_record_t record, mu_url_t url, int flags)
+open_subscription (struct _mu_fsfolder *folder)
 {
-  int rc = 0;
-  
-  if (mu_url_is_scheme (url, record->scheme))
-    return MU_FOLDER_ATTRIBUTE_FILE & flags;
+  int rc;
+  mu_property_t prop;
+  mu_stream_t str;
+  char *filename = mu_make_file_name (folder->dirname, ".mu-subscr");
   
-  if (mu_scheme_autodetect_p (url))
-    {
-      struct stat st;
-      const char *path;
-
-      mu_url_sget_path (url, &path);
-      if (stat (path, &st) < 0)
-	return 0;
-
-      if (S_ISREG (st.st_mode) || S_ISCHR (st.st_mode))
-	{
-	  if (st.st_size == 0)
-	    {
-	      rc |= MU_FOLDER_ATTRIBUTE_FILE;
-	    }
-	  else if (flags & MU_FOLDER_ATTRIBUTE_FILE)
-	    {
-#if 0
-	      /* This effectively sieves out all non-mailbox files,
-		 but it makes mu_folder_enumerate crawl, which is
-		 intolerable for imap4d LIST command. */
-	      int fd = open (path, O_RDONLY);
-	      if (fd != -1)
-		{
-		  char buf[5];
-		  if (read (fd, buf, 5) == 5)
-		    if (memcmp (buf, "From ", 5) == 0)
-		      rc |= MU_FOLDER_ATTRIBUTE_FILE;
-		  close (fd);
-		}
-#else
-	      rc |= MU_FOLDER_ATTRIBUTE_FILE;
-#endif
-	    }
-	}
-	  
-      if ((flags & MU_FOLDER_ATTRIBUTE_DIRECTORY)
-	  && S_ISDIR (st.st_mode))
-	rc |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
-    }
+  rc = mu_file_stream_create (&str, filename, MU_STREAM_RDWR|MU_STREAM_CREAT);
+  if (rc)
+    return rc;
+  rc = mu_property_create_init (&prop, mu_assoc_property_init, str);
+  free (filename);
+  if (rc == 0)
+    folder->subscription = prop;
   return rc;
 }
 
-static struct _mu_record _mbox_record =
-{
-  MU_MBOX_PRIO,
-  MU_MBOX_SCHEME,
-  MU_RECORD_LOCAL,
-  MU_URL_SCHEME | MU_URL_PATH | MU_URL_PARAM,
-  MU_URL_PATH,
-  mu_url_expand_path, /* URL init.  */
-  _mailbox_mbox_init, /* Mailbox init.  */
-  NULL, /* Mailer init.  */
-  _folder_mbox_init, /* Folder init.  */
-  NULL, /* No need for an back pointer.  */
-  _mbox_is_scheme, /* _is_scheme method.  */
-  NULL, /* _get_url method.  */
-  NULL, /* _get_mailbox method.  */
-  NULL, /* _get_mailer method.  */
-  NULL  /* _get_folder method.  */
-};
-mu_record_t mu_mbox_record = &_mbox_record;
-
-/* lsub/subscribe/unsubscribe are not needed.  */
-static void folder_mbox_destroy    (mu_folder_t);
-static int folder_mbox_open        (mu_folder_t, int);
-static int folder_mbox_close       (mu_folder_t);
-static int folder_mbox_delete      (mu_folder_t, const char *);
-static int folder_mbox_rename      (mu_folder_t , const char *, const char *);
-static int folder_mbox_list        (mu_folder_t, const char *, void *, int,
-				    size_t, mu_list_t, mu_folder_enumerate_fp,
-				    void *);
-static int folder_mbox_subscribe   (mu_folder_t, const char *);
-static int folder_mbox_unsubscribe (mu_folder_t, const char *);
-static int folder_mbox_lsub        (mu_folder_t, const char *, const char *,
-				    mu_list_t);
-
 
-static char *get_pathname       (const char *, const char *);
-
-static int folder_mbox_get_authority (mu_folder_t folder, mu_authority_t * pauth);
-
-struct _fmbox
-{
-  char *dirname;
-  char **subscribe;
-  size_t sublen;
-};
-typedef struct _fmbox *fmbox_t;
-
-
-int
-_folder_mbox_init (mu_folder_t folder)
+static char *
+get_pathname (const char *dirname, const char *basename)
 {
-  fmbox_t dfolder;
-  int status = 0;
-
-  /* We create an authority so the API is uniform across the mailbox
-     types. */
-  status = folder_mbox_get_authority (folder, NULL);
-  if (status != 0)
-    return status;
-
-  dfolder = folder->data = calloc (1, sizeof (*dfolder));
-  if (dfolder == NULL)
-    return ENOMEM;
+  char *pathname = NULL, *p;
 
-  status = mu_url_aget_path (folder->url, &dfolder->dirname);
-  if (status == MU_ERR_NOENT)
+  /* Skip eventual protocol designator. */
+  p = strchr (dirname, ':');
+  if (p && p[1] == '/' && p[2] == '/')
+    dirname = p + 3;
+  
+  /* null basename gives dirname.  */
+  if (basename == NULL)
+    pathname = (dirname) ? strdup (dirname) : strdup (".");
+  /* Absolute.  */
+  else if (basename[0] == '/')
+    pathname = strdup (basename);
+  /* Relative.  */
+  else
     {
-      dfolder->dirname = malloc (2);
-      if (dfolder->dirname == NULL)
-	status = ENOMEM;
-      else
+      size_t baselen = strlen (basename);
+      size_t dirlen = strlen (dirname);
+      while (dirlen > 0 && dirname[dirlen-1] == '/')
+	dirlen--;
+      pathname = calloc (dirlen + baselen + 2, sizeof (char));
+      if (pathname)
 	{
-	  strcpy (dfolder->dirname, ".");
-	  status = 0;
+	  memcpy (pathname, dirname, dirlen);
+	  pathname[dirlen] = '/';
+	  strcpy (pathname + dirlen + 1, basename);
 	}
     }
-  
-  if (status)  
-    {
-      free (dfolder);
-      folder->data = NULL;
-      return status;
-    }
-
-  folder->_destroy = folder_mbox_destroy;
-
-  folder->_open = folder_mbox_open;
-  folder->_close = folder_mbox_close;
-
-  folder->_list = folder_mbox_list;
-  folder->_lsub = folder_mbox_lsub;
-  folder->_subscribe = folder_mbox_subscribe;
-  folder->_unsubscribe = folder_mbox_unsubscribe;
-  folder->_delete = folder_mbox_delete;
-  folder->_rename = folder_mbox_rename;
-  return 0;
+  return pathname;
 }
 
-void
-folder_mbox_destroy (mu_folder_t folder)
+static void
+_fsfolder_destroy (mu_folder_t folder)
 {
   if (folder->data)
     {
-      fmbox_t fmbox = folder->data;
-      if (fmbox->dirname)
-	free (fmbox->dirname);
-      if (fmbox->subscribe)
-	free (fmbox->subscribe);
+      struct _mu_fsfolder *fsfolder = folder->data;
+      free (fsfolder->dirname);
+      mu_property_destroy (&fsfolder->subscription);
       free (folder->data);
       folder->data = NULL;
     }
@@ -211,59 +123,45 @@ folder_mbox_destroy (mu_folder_t folder)
 
 /* Noop. */
 static int
-folder_mbox_open (mu_folder_t folder, int flags MU_ARG_UNUSED)
+_fsfolder_open (mu_folder_t folder, int flags MU_ARG_UNUSED)
 {
-  fmbox_t fmbox = folder->data;
+  struct _mu_fsfolder *fsfolder = folder->data;
   if (flags & MU_STREAM_CREAT)
     {
-      return (mkdir (fmbox->dirname, S_IRWXU) == 0) ? 0 : errno;
+      return (mkdir (fsfolder->dirname, S_IRWXU) == 0) ? 0 : errno;
     }
   return 0;
 }
 
 /*  Noop.  */
 static int
-folder_mbox_close (mu_folder_t folder MU_ARG_UNUSED)
-{
-  return 0;
-}
-
-static int
-folder_mbox_delete (mu_folder_t folder, const char *filename)
+_fsfolder_close (mu_folder_t folder MU_ARG_UNUSED)
 {
-  fmbox_t fmbox = folder->data;
-  if (filename)
-    {
-      int status = 0;
-      char *pathname = get_pathname (fmbox->dirname, filename);
-      if (pathname)
-	{
-	  if (remove (pathname) != 0)
-	    status = errno;
-	  free (pathname);
-	}
-      else
-	status = ENOMEM;
-      return status;
-    }
-  return EINVAL;
+  int rc = 0;
+  struct _mu_fsfolder *fsfolder = folder->data;
+  
+  if (fsfolder->subscription)
+    rc = mu_property_save (fsfolder->subscription);
+  return rc;
 }
 
 static int
-folder_mbox_rename (mu_folder_t folder, const char *oldpath,
+_fsfolder_rename (mu_folder_t folder, const char *oldpath,
 		    const char *newpath)
 {
-  fmbox_t fmbox = folder->data;
+  struct _mu_fsfolder *fsfolder = folder->data;
   if (oldpath && newpath)
     {
       int status = 0;
-      char *pathold = get_pathname (fmbox->dirname, oldpath);
+      char *pathold = get_pathname (fsfolder->dirname, oldpath);
       if (pathold)
 	{
-	  char *pathnew = get_pathname (fmbox->dirname, newpath);
+	  char *pathnew = get_pathname (fsfolder->dirname, newpath);
 	  if (pathnew)
 	    {
-	      if (rename (pathold, pathnew) != 0)
+	      if (access (pathnew, F_OK) == 0)
+		status = EEXIST;
+	      else if (rename (pathold, pathnew) != 0)
 		status = errno;
 	      free (pathnew);
 	    }
@@ -346,6 +244,8 @@ list_helper (struct search_data *data, mu_record_t record,
       
       if (ename[ename[0] != '.' ? 0 : ename[1] != '.' ? 1 : 2] == 0)
 	continue;
+      if (strncmp (ename, ".mu-", 4) == 0)
+	continue;
       fname = get_pathname (dirname, ename);
       if (lstat (fname, &st) == 0)
 	{
@@ -454,19 +354,19 @@ list_helper (struct search_data *data, mu_record_t record,
 }
 
 static int
-folder_mbox_list (mu_folder_t folder, const char *ref,
-		  void *pattern,
-		  int flags,
-		  size_t max_level,
-		  mu_list_t flist,
-		  mu_folder_enumerate_fp enumfun, void *enumdata)
+_fsfolder_list (mu_folder_t folder, const char *ref,
+		void *pattern,
+		int flags,
+		size_t max_level,
+		mu_list_t flist,
+		mu_folder_enumerate_fp enumfun, void *enumdata)
 {
-  fmbox_t fmbox = folder->data;
+  struct _mu_fsfolder *fsfolder = folder->data;
   struct inode_list iroot;
   struct search_data sdata;
   
   memset (&iroot, 0, sizeof iroot);
-  sdata.dirname = get_pathname (fmbox->dirname, ref);
+  sdata.dirname = get_pathname (fsfolder->dirname, ref);
   sdata.dirlen = strlen (sdata.dirname);
   sdata.result = flist;
   sdata.enumfun = enumfun;
@@ -483,132 +383,146 @@ folder_mbox_list (mu_folder_t folder, const char *ref,
 }
 
 static int
-folder_mbox_lsub (mu_folder_t folder, const char *ref MU_ARG_UNUSED,
-		  const char *name,
-		  mu_list_t flist)
+_fsfolder_lsub (mu_folder_t folder, const char *ref, const char *name,
+		mu_list_t flist)
 {
-  fmbox_t fmbox = folder->data;
-  int status;
+  struct _mu_fsfolder *fsfolder = folder->data;
+  int rc;
+  char *pattern;
+  mu_iterator_t itr;
   
   if (name == NULL || *name == '\0')
     name = "*";
 
-  if (fmbox->sublen > 0)
-    {      
-      size_t i;
-
-      for (i = 0; i < fmbox->sublen; i++)
+  if (!fsfolder->subscription && (rc = open_subscription (fsfolder)))
+    return rc;
+    
+  pattern = mu_make_file_name (ref, name);
+  
+  rc = mu_property_get_iterator (fsfolder->subscription, &itr);
+  if (rc == 0)
+    {
+      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+	   mu_iterator_next (itr))
 	{
-	  if (fmbox->subscribe[i]
-	      && fnmatch (name, fmbox->subscribe[i], 0) == 0)
+	  const char *key, *val;
+	  
+	  mu_iterator_current_kv (itr, (const void **)&key, (void**)&val);
+
+	  if (fnmatch (pattern, key, 0) == 0)
 	    {
 	      struct mu_list_response *resp;
 	      resp = malloc (sizeof (*resp));
 	      if (resp == NULL)
 		{
-		  status = ENOMEM;
+		  rc = ENOMEM;
 		  break;
 		}
-	      else if ((resp->name = strdup (fmbox->subscribe[i])) == NULL)
+	      else if ((resp->name = strdup (key)) == NULL)
 		{
 		  free (resp);
-		  status = ENOMEM;
+		  rc = ENOMEM;
 		  break;
 		}
 	      resp->type = MU_FOLDER_ATTRIBUTE_FILE;
 	      resp->level = 0;
 	      resp->separator = '/';
+	      rc = mu_list_append (flist, resp);
+	      if (rc)
+		{
+		  free (resp);
+		  break;
+		}
 	    }
 	}
+      mu_iterator_destroy (&itr);
     }
-  return status;
+  free (pattern);
+  return rc;
 }
 
 static int
-folder_mbox_subscribe (mu_folder_t folder, const char *name)
+_fsfolder_subscribe (mu_folder_t folder, const char *name)
 {
-  fmbox_t fmbox = folder->data;
-  char **tmp;
-  size_t i;
-  for (i = 0; i < fmbox->sublen; i++)
-    {
-      if (fmbox->subscribe[i] && strcmp (fmbox->subscribe[i], name) == 0)
-	return 0;
-    }
-  tmp = realloc (fmbox->subscribe, (fmbox->sublen + 1) * sizeof (*tmp));
-  if (tmp == NULL)
-    return ENOMEM;
-  fmbox->subscribe = tmp;
-  fmbox->subscribe[fmbox->sublen] = strdup (name);
-  if (fmbox->subscribe[fmbox->sublen] == NULL)
-    return ENOMEM;
-  fmbox->sublen++;
-  return 0;
-}
+  struct _mu_fsfolder *fsfolder = folder->data;
+  int rc;
+  
+  if (!fsfolder->subscription && (rc = open_subscription (fsfolder)))
+    return rc;
+
+  return mu_property_set_value (fsfolder->subscription, name, "", 1);
+}  
 
 static int
-folder_mbox_unsubscribe (mu_folder_t folder, const char *name)
+_fsfolder_unsubscribe (mu_folder_t folder, const char *name)
 {
-  fmbox_t fmbox = folder->data;
-  size_t i;
-  for (i = 0; i < fmbox->sublen; i++)
-    {
-      if (fmbox->subscribe[i] && strcmp (fmbox->subscribe[i], name) == 0)
-	{
-	  free (fmbox->subscribe[i]);
-	  fmbox->subscribe[i] = NULL;
-	  return 0;
-	}
-    }
-  return MU_ERR_NOENT;
-}
+  struct _mu_fsfolder *fsfolder = folder->data;
+  int rc;
 
-static char *
-get_pathname (const char *dirname, const char *basename)
-{
-  char *pathname = NULL;
-
-  /* Skip eventual protocol designator.
-     FIXME: Actually, any valid URL spec should be allowed as dirname ... */
-  if (strncmp (dirname, MU_MBOX_SCHEME, MU_MBOX_SCHEME_LEN) == 0)
-    dirname += MU_MBOX_SCHEME_LEN;
-  else if (strncmp (dirname, MU_FILE_SCHEME, MU_FILE_SCHEME_LEN) == 0)
-    dirname += MU_FILE_SCHEME_LEN;
-  
-  /* null basename gives dirname.  */
-  if (basename == NULL)
-    pathname = (dirname) ? strdup (dirname) : strdup (".");
-  /* Absolute.  */
-  else if (basename[0] == '/')
-    pathname = strdup (basename);
-  /* Relative.  */
-  else
-    {
-      size_t baselen = strlen (basename);
-      size_t dirlen = strlen (dirname);
-      while (dirlen > 0 && dirname[dirlen-1] == '/')
-	dirlen--;
-      pathname = calloc (dirlen + baselen + 2, sizeof (char));
-      if (pathname)
-	{
-	  memcpy (pathname, dirname, dirlen);
-	  pathname[dirlen] = '/';
-	  strcpy (pathname + dirlen + 1, basename);
-	}
-    }
-  return pathname;
+  if (!fsfolder->subscription && (rc = open_subscription (fsfolder)))
+    return rc;
+
+  return mu_property_unset (fsfolder->subscription, name);
 }
 
 static int
-folder_mbox_get_authority (mu_folder_t folder, mu_authority_t *pauth)
+_fsfolder_get_authority (mu_folder_t folder, mu_authority_t *pauth)
 {
   int status = 0;
   if (folder->authority == NULL)
-    {
-	status = mu_authority_create_null (&folder->authority, folder);
-    }
+    status = mu_authority_create_null (&folder->authority, folder);
   if (!status && pauth)
     *pauth = folder->authority;
   return status;
 }
 
+int
+_mu_fsfolder_init (mu_folder_t folder)
+{
+  struct _mu_fsfolder *dfolder;
+  int status = 0;
+
+  /* We create an authority so the API is uniform across the mailbox
+     types. */
+  status = _fsfolder_get_authority (folder, NULL);
+  if (status != 0)
+    return status;
+
+  dfolder = folder->data = calloc (1, sizeof (*dfolder));
+  if (dfolder == NULL)
+    return ENOMEM;
+
+  status = mu_url_aget_path (folder->url, &dfolder->dirname);
+  if (status == MU_ERR_NOENT)
+    {
+      dfolder->dirname = malloc (2);
+      if (dfolder->dirname == NULL)
+	status = ENOMEM;
+      else
+	{
+	  strcpy (dfolder->dirname, ".");
+	  status = 0;
+	}
+    }
+  
+  if (status)  
+    {
+      free (dfolder);
+      folder->data = NULL;
+      return status;
+    }
+
+  folder->_destroy = _fsfolder_destroy;
+
+  folder->_open = _fsfolder_open;
+  folder->_close = _fsfolder_close;
+
+  folder->_list = _fsfolder_list;
+  folder->_lsub = _fsfolder_lsub;
+  folder->_subscribe = _fsfolder_subscribe;
+  folder->_unsubscribe = _fsfolder_unsubscribe;
+  folder->_delete = NULL;
+  folder->_rename = _fsfolder_rename;
+  return 0;
+}
+
diff --git a/libmailutils/mailbox/mailbox.c b/libmailutils/mailbox/mailbox.c
index a31682a..990ae6a 100644
--- a/libmailutils/mailbox/mailbox.c
+++ b/libmailutils/mailbox/mailbox.c
@@ -41,6 +41,7 @@
 #include <mailutils/util.h>
 
 #include <mailutils/sys/mailbox.h>
+#include <mailutils/sys/folder.h>
 #include <mailutils/sys/url.h>
 
 /* Mailbox-specific flags */
@@ -76,7 +77,8 @@ mailbox_folder_create (mu_mailbox_t mbox, const char *name,
 int
 _mailbox_create_from_record (mu_mailbox_t *pmbox,
 			     mu_record_t record,
-			     mu_url_t url, 
+			     mu_url_t url,
+			     mu_folder_t folder,
 			     const char *name)
 {
   int (*m_init) (mu_mailbox_t) = NULL;
@@ -127,10 +129,16 @@ _mailbox_create_from_record (mu_mailbox_t *pmbox,
 	}
       
       mbox->url = url;
-      
-      /* Create the folder before initializing the concrete mailbox.
-	 The mailbox needs it's back pointer. */
-      status = mailbox_folder_create (mbox, name, record);
+
+      if (folder)
+	{
+	  folder->ref++; /* FIXME: No ref/unref function for folders */
+	  mbox->folder = folder;
+	}
+      else
+	/* Create the folder before initializing the concrete mailbox.
+	   The mailbox needs it's back pointer. */
+	status = mailbox_folder_create (mbox, name, record);
       
       if (status == 0)
 	status = m_init (mbox);   /* Create the concrete mailbox type.  */
@@ -152,11 +160,12 @@ static int
 _create_mailbox0 (mu_mailbox_t *pmbox, mu_url_t url, const char *name)
 {
   mu_record_t record = NULL;
-
-  if (mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL)
-      == 0)
-    return _mailbox_create_from_record (pmbox, record, url, name);
-  return ENOSYS;
+  int rc;
+  
+  rc = mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE, &record, NULL);
+  if (rc == 0)
+    rc = _mailbox_create_from_record (pmbox, record, url, NULL, name);
+  return rc;
 }
 
 static int
@@ -203,7 +212,71 @@ mu_mailbox_create_from_record (mu_mailbox_t *pmbox, mu_record_t record,
   rc = mu_url_create (&url, name);
   if (rc)
     return rc;
-  rc = _mailbox_create_from_record (pmbox, record, url, name);
+  rc = _mailbox_create_from_record (pmbox, record, url, NULL, name);
+  if (rc)
+    mu_url_destroy (&url);
+  return rc;
+}
+
+int
+mu_mailbox_create_at (mu_mailbox_t *pmbox, mu_folder_t folder,
+		      const char *name)
+{
+  int rc;
+  mu_url_t url;
+  const char *oldpath;
+  
+  rc = mu_url_dup (folder->url, &url);
+  if (rc)
+    return rc;
+  do
+    {
+      char *path;
+      size_t oldlen, len;
+      mu_record_t record;
+      
+      rc = mu_url_sget_path (url, &oldpath);
+      if (rc)
+	break;
+
+      oldlen = strlen (oldpath);
+      if (oldlen == 0)
+	{
+	  path = strdup (name);
+	  if (!path)
+	    {
+	      rc = ENOMEM;
+	      break;
+	    }
+	}
+      else
+	{
+	  if (oldpath[oldlen-1] == '/')
+	    oldlen--;
+	  len = oldlen + 1 + strlen (name) + 1;
+	  path = malloc (len);
+	  if (!path)
+	    {
+	      rc = ENOMEM;
+	      break;
+	    }
+	  memcpy (path, oldpath, oldlen);
+	  path[oldlen++] = '/';
+	  strcpy (path + oldlen, name);
+	}
+      rc = mu_url_set_path (url, path);
+      free (path);
+      if (rc)
+	break;
+
+      rc = mu_registrar_lookup_url (url, MU_FOLDER_ATTRIBUTE_FILE,
+				    &record, NULL);
+      if (rc)
+	break;
+      rc = _mailbox_create_from_record (pmbox, record, url, folder, name);
+    }
+  while (0);
+
   if (rc)
     mu_url_destroy (&url);
   return rc;
@@ -314,7 +387,24 @@ mu_mailbox_remove (mu_mailbox_t mbox)
   if (mbox->flags & _MU_MAILBOX_REMOVED)
     return MU_ERR_MBX_REMOVED;
   if (!mbox->_remove)
-    return MU_ERR_EMPTY_VFN;
+    {
+      /* Try the owning folder delete method.  See comment to mu_folder_delete
+	 in folder.c.  This may result in a recursive call to mu_mailbox_remove
+	 which is blocked by setting the _MU_MAILBOX_REMOVED flag. */
+
+      int rc;
+      const char *path;
+      
+      rc = mu_url_sget_path (mbox->url, &path);
+      if (rc == 0)
+	{
+	  mbox->flags |= _MU_MAILBOX_REMOVED;
+	  rc = mu_folder_delete (mbox->folder, path);
+	  if (rc)
+	    mbox->flags &= ~_MU_MAILBOX_REMOVED;
+	}
+      return rc;
+    }
   return mbox->_remove (mbox);
 }
 
@@ -689,7 +779,7 @@ mu_mailbox_set_folder (mu_mailbox_t mbox, mu_folder_t folder)
 {
   if (mbox == NULL)
     return EINVAL;
-   mbox->folder = folder;
+  mbox->folder = folder;
   return 0;
 }
 
diff --git a/libmailutils/mailbox/mbx_default.c b/libmailutils/mailbox/mbx_default.c
index 0556304..ff0a19b 100644
--- a/libmailutils/mailbox/mbx_default.c
+++ b/libmailutils/mailbox/mbx_default.c
@@ -345,7 +345,8 @@ attach_auth_ticket (mu_mailbox_t mbox)
       && mu_folder_get_authority (folder, &auth) == 0
       && auth)
     {
-      char *filename = mu_tilde_expansion (mu_ticket_file, "/", NULL);
+      char *filename = mu_tilde_expansion (mu_ticket_file,
+					   MU_HIERARCHY_DELIMITER, NULL);
       mu_wicket_t wicket;
       int rc;
   
@@ -420,7 +421,7 @@ mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail)
 	}
     }
 
-  p = mu_tilde_expansion (mail, "/", NULL);
+  p = mu_tilde_expansion (mail, MU_HIERARCHY_DELIMITER, NULL);
   if (tmp_mbox)
     free (tmp_mbox);
   tmp_mbox = p;
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index d67519f..6a1188d 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -12,6 +12,7 @@ decode2047
 encode2047
 fltst
 fsaf
+fsfolder
 imapio
 listop
 mailcap
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 1cb2968..aca151a 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -47,6 +47,7 @@ noinst_PROGRAMS = \
  encode2047\
  fltst\
  fsaf\
+ fsfolder\
  imapio\
  listop\
  mailcap\
@@ -78,6 +79,9 @@ TESTSUITE_AT = \
  encode2047.at\
  fromflt.at\
  fsaf.at\
+ fsfolder00.at\
+ fsfolder01.at\
+ fsfolder02.at\
  hdrflt.at\
  imapio.at\
  inline-comment.at\
diff --git a/libproto/imap/tests/imapfolder.c b/libmailutils/tests/fsfolder.c
similarity index 59%
copy from libproto/imap/tests/imapfolder.c
copy to libmailutils/tests/fsfolder.c
index cb3a08b..c2ae555 100644
--- a/libproto/imap/tests/imapfolder.c
+++ b/libmailutils/tests/fsfolder.c
@@ -1,25 +1,38 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 2011 Free Software Foundation, Inc.
 
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
 
-   This library is distributed in the hope that it will be useful,
+   GNU Mailutils is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-   You should have received a copy of the GNU Lesser General
-   Public License along with this library.  If not, see
-   <http://www.gnu.org/licenses/>. */
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+#include <mailutils/folder.h>
+#include <mailutils/stream.h>
+#include <mailutils/stdstream.h>
+#include <mailutils/list.h>
+#include <mailutils/url.h>
+#include <mailutils/util.h>
+#include <mailutils/registrar.h>
+#include <mailutils/sys/folder.h>
+#include <mailutils/sys/registrar.h>
 
-#include <mailutils/mailutils.h>
+int sort_option;
+int prefix_len;
 
 struct command
 {
@@ -30,15 +43,29 @@ struct command
 };
 
 static int
+compare_response (void const *a, void const *b)
+{
+  struct mu_list_response const *ra = a;
+  struct mu_list_response const *rb = b;
+
+  if (ra->level < rb->level)
+    return -1;
+  if (ra->level > rb->level)
+    return 1;
+  return strcmp (ra->name, rb->name);
+}
+
+static int
 _print_list_entry (void *item, void *data)
 {
   struct mu_list_response *resp = item;
+  int len = data ? *(int*) data : 0;
   mu_printf ("%c%c %c %4d %s\n",
 	     (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY) ? 'd' : '-',
 	     (resp->type & MU_FOLDER_ATTRIBUTE_FILE) ? 'f' : '-',
 	     resp->separator ? resp->separator : ' ',
 	     resp->level,
-	     resp->name);
+	     resp->name + len);
   return 0;
 }
 
@@ -48,13 +75,15 @@ com_list (mu_folder_t folder, char **argv)
   int rc;
   mu_list_t list;
   
-  mu_printf ("listing %s %s\n", argv[0], argv[1]);
+  mu_printf ("listing '%s' '%s'\n", argv[0], argv[1]);
   rc = mu_folder_list (folder, argv[0], argv[1], 0, &list);
   if (rc)
     mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_list", argv[0], rc);
   else
     {
-      mu_list_foreach (list, _print_list_entry, NULL);
+      if (sort_option)
+	mu_list_sort (list, compare_response);
+      mu_list_foreach (list, _print_list_entry, &prefix_len);
       mu_list_destroy (&list);
     }
 }
@@ -71,25 +100,14 @@ com_lsub (mu_folder_t folder, char **argv)
     mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_lsub", argv[0], rc);
   else
     {
+      if (sort_option)
+	mu_list_sort (list, compare_response);
       mu_list_foreach (list, _print_list_entry, NULL);
       mu_list_destroy (&list);
     }
 }
 
 static void
-com_delete (mu_folder_t folder, char **argv)
-{
-  int rc;
-  
-  mu_printf ("deleting %s\n", argv[0]);
-  rc = mu_folder_delete (folder, argv[0]);
-  if (rc)
-    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_lsub", argv[0], rc);
-  else
-    mu_printf ("delete successful\n");
-}
-
-static void
 com_rename (mu_folder_t folder, char **argv)
 {
   int rc;
@@ -131,9 +149,7 @@ com_unsubscribe (mu_folder_t folder, char **argv)
 static struct command comtab[] = {
   { "list", 2, "REF MBX", com_list },
   { "lsub", 2, "REF MBX", com_lsub },
-  { "delete", 1, "MBX", com_delete },
   { "rename", 2, "OLD NEW", com_rename },
-  { "delete", 1, "MBOX", com_delete },
   { "subscribe", 1, "MBX", com_subscribe },
   { "unsubscribe", 1, "MBX", com_unsubscribe },
   { NULL }
@@ -156,13 +172,38 @@ usage ()
   struct command *cp;
   
   mu_printf (
-    "usage: %s [debug=SPEC] url=URL OP ARG [ARG...] [OP ARG [ARG...]...]\n",
+    "usage: %s [-debug=SPEC] -name=URL [-sort] [-glob] OP ARG [ARG...] [OP ARG [ARG...]...]\n",
     mu_program_name);
   mu_printf ("OPerations and corresponding ARGuments are:\n");
   for (cp = comtab; cp->verb; cp++)
     mu_printf (" %s %s\n", cp->verb, cp->args);
 }
 
+static int
+_always_is_scheme (mu_record_t record, mu_url_t url, int flags)
+{
+  return 1;
+}
+
+static struct _mu_record test_record =
+{
+  0,
+  "file",
+  MU_RECORD_LOCAL,
+  MU_URL_SCHEME | MU_URL_PATH,
+  MU_URL_PATH,
+  mu_url_expand_path, /* URL init.  */
+  NULL, /* Mailbox init.  */
+  NULL, /* Mailer init.  */
+  _mu_fsfolder_init, /* Folder init.  */
+  NULL, /* No need for an back pointer.  */
+  _always_is_scheme, /* _is_scheme method.  */
+  NULL, /* _get_url method.  */
+  NULL, /* _get_mailbox method.  */
+  NULL, /* _get_mailer method.  */
+  NULL  /* _get_folder method.  */
+};
+
 int
 main (int argc, char **argv)
 {
@@ -170,10 +211,10 @@ main (int argc, char **argv)
   int rc;
   mu_folder_t folder;
   char *fname = NULL;
+  int glob_option = 0;
   
   mu_set_program_name (argv[0]);
-  mu_registrar_record (mu_imap_record);
-  mu_registrar_record (mu_imaps_record);
+  mu_registrar_record (&test_record);
 
   if (argc == 1)
     {
@@ -183,33 +224,49 @@ main (int argc, char **argv)
 
   for (i = 1; i < argc; i++)
     {
-      if (strncmp (argv[i], "debug=", 6) == 0)
-	mu_debug_parse_spec (argv[i] + 6);
-      else if (strncmp (argv[i], "url=", 4) == 0)
-	fname = argv[i] + 4;
+      if (strncmp (argv[i], "-debug=", 7) == 0)
+	mu_debug_parse_spec (argv[i] + 7);
+      else if (strncmp (argv[i], "-name=", 6) == 0)
+	fname = argv[i] + 6;
+      else if (strcmp (argv[i], "-sort") == 0)
+	sort_option = 1;
+      else if (strcmp (argv[i], "-glob") == 0)
+	glob_option = 1;
       else
 	break;
     }
 
   if (!fname)
     {
-      mu_error ("URL not specified");
+      mu_error ("name not specified");
       exit (1);
     }
-      
+  
+  if (fname[0] != '/')
+    {
+      char *cwd = mu_getcwd ();
+      prefix_len = strlen (cwd);
+      if (cwd[prefix_len-1] != '/')
+	prefix_len++;
+      fname = mu_make_file_name (cwd, fname);
+      free (cwd);
+    }
+  
   rc = mu_folder_create (&folder, fname);
   if (rc)
     {
       mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_create", fname, rc);
       return 1;
     }
-  
   rc = mu_folder_open (folder, MU_STREAM_READ);
   if (rc)
     {
       mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_open", fname, rc);
       return 1;
     }
+
+  if (glob_option)
+    mu_folder_set_match (folder, mu_folder_glob_match);
   
   while (i < argc)
     {
diff --git a/imap4d/tests/create02.at b/libmailutils/tests/fsfolder00.at
similarity index 55%
copy from imap4d/tests/create02.at
copy to libmailutils/tests/fsfolder00.at
index 552e8d3..a2b75db 100644
--- a/imap4d/tests/create02.at
+++ b/libmailutils/tests/fsfolder00.at
@@ -14,25 +14,40 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([create nested])
-AT_KEYWORDS([create create01])
+AT_SETUP([List])
+AT_KEYWORDS([fsfolder folder-list])
 
 AT_CHECK([
-AT_DATA([input],[dnl
-1 create en/to/tre
-X LOGOUT
-])
+mkdir dir
+> dir/foo
+> dir/bar
+> dir/foo.mbox
+mkdir dir/subdir
+> dir/subdir/file
+> dir/subdir/baz.mbox
 
-imap4d IMAP4D_OPTIONS < input | tr -d '\r'
-find . -name tre
+fsfolder -name=dir -sort dnl
+ list "" "*" dnl
+ list subdir "*" dnl
+ list "" "*.mbox" dnl
+ list "subdir" "*.mbox"
 ],
 [0],
-[* PREAUTH IMAP4rev1 Test mode
-1 OK CREATE Completed
-* BYE Session terminating.
-X OK LOGOUT Completed
-./en/to/tre
+[listing '' '*'
+d- /    0 dir/bar
+d- /    0 dir/foo
+d- /    0 dir/foo.mbox
+d- /    0 dir/subdir
+d- /    1 dir/subdir/baz.mbox
+d- /    1 dir/subdir/file
+listing 'subdir' '*'
+d- /    0 dir/subdir/baz.mbox
+d- /    0 dir/subdir/file
+listing '' '*.mbox'
+d- /    0 dir/foo.mbox
+d- /    1 dir/subdir/baz.mbox
+listing 'subdir' '*.mbox'
+d- /    0 dir/subdir/baz.mbox
 ])
 
 AT_CLEANUP
-
diff --git a/libmailutils/tests/fsfolder01.at b/libmailutils/tests/fsfolder01.at
new file mode 100644
index 0000000..b6c2f18
--- /dev/null
+++ b/libmailutils/tests/fsfolder01.at
@@ -0,0 +1,74 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([Subscriptions])
+AT_KEYWORDS([fsfolder folder-subscribe])
+
+AT_CHECK([
+mkdir dir
+fsfolder -name=dir -sort lsub "" "*"
+fsfolder -name=dir subscribe foo
+fsfolder -name=dir -sort lsub "" "*"
+fsfolder -name=dir subscribe baz subscribe foo/baz subscribe foo/bar
+fsfolder -name=dir -sort lsub "" "*"
+fsfolder -name=dir -sort lsub foo "*"
+fsfolder -name=dir -sort lsub "" 'foo*'
+fsfolder -name=dir unsubscribe baz
+fsfolder -name=dir -sort lsub "" "*"
+fsfolder -name=dir unsubscribe foo
+fsfolder -name=dir -sort lsub "" "*"
+],
+[0],
+[listing subscriptions for '' '*'
+subscribing foo
+subscribe successful
+listing subscriptions for '' '*'
+-f /    0 foo
+subscribing baz
+subscribe successful
+subscribing foo/baz
+subscribe successful
+subscribing foo/bar
+subscribe successful
+listing subscriptions for '' '*'
+-f /    0 baz
+-f /    0 foo
+-f /    0 foo/bar
+-f /    0 foo/baz
+listing subscriptions for 'foo' '*'
+-f /    0 foo/bar
+-f /    0 foo/baz
+listing subscriptions for '' 'foo*'
+-f /    0 foo
+-f /    0 foo/bar
+-f /    0 foo/baz
+unsubscribing baz
+unsubscribe successful
+listing subscriptions for '' '*'
+-f /    0 foo
+-f /    0 foo/bar
+-f /    0 foo/baz
+unsubscribing foo
+unsubscribe successful
+listing subscriptions for '' '*'
+-f /    0 foo/bar
+-f /    0 foo/baz
+])
+
+AT_CLEANUP
+
+
+
diff --git a/libmailutils/tests/strerr.at b/libmailutils/tests/fsfolder02.at
similarity index 69%
copy from libmailutils/tests/strerr.at
copy to libmailutils/tests/fsfolder02.at
index 1bdbe6f..09711f8 100644
--- a/libmailutils/tests/strerr.at
+++ b/libmailutils/tests/fsfolder02.at
@@ -14,24 +14,26 @@
 # You should have received a copy of the GNU General Public License
 # along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
 
-AT_SETUP([strerr])
-AT_KEYWORDS([stdstream])
+AT_SETUP([Rename])
+AT_KEYWORDS([fsfolder folder-rename])
 
 AT_CHECK([
-strout -err now is the time for all good men
+mkdir dir
+> dir/foo
+fsfolder -name=dir rename foo bar
+fsfolder -name=dir list "" "*"
+> dir/baz
+fsfolder -name=dir rename bar baz
 ],
 [0],
-[],
-[strout: now
-strout: is
-strout: the
-strout: time
-strout: for
-strout: all
-strout: good
-strout: men
+[renaming foo to bar
+rename successful
+listing '' '*'
+d- /    0 dir/bar
+renaming bar to baz
+],
+[fsfolder: mu_folder_rename(bar) failed: File exists
 ])
 
 AT_CLEANUP
 
-
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 888c25a..4c1b427 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -68,6 +68,11 @@ m4_include([mailcap.at])
 m4_include([wicket.at])
 m4_include([prop.at])
 
+AT_BANNER(File-system folder)
+m4_include([fsfolder00.at])
+m4_include([fsfolder01.at])
+m4_include([fsfolder02.at])
+
 AT_BANNER(Base64)
 m4_include([base64e.at])
 m4_include([base64d.at])
diff --git a/libmu_sieve/extensions/vacation.c b/libmu_sieve/extensions/vacation.c
index 160930f..299309f 100644
--- a/libmu_sieve/extensions/vacation.c
+++ b/libmu_sieve/extensions/vacation.c
@@ -348,7 +348,7 @@ check_db (mu_sieve_machine_t mach, mu_list_t tags, char *from)
   else
     days = DAYS_DEFAULT;
 
-  file = mu_tilde_expansion ("~/.vacation", "/", NULL);
+  file = mu_tilde_expansion ("~/.vacation", MU_HIERARCHY_DELIMITER, NULL);
   if (!file)
     {
       mu_sieve_error (mach, _("%lu: cannot build db file name"),
diff --git a/libproto/imap/select.c b/libproto/imap/select.c
index 05dd550..31c2b72 100644
--- a/libproto/imap/select.c
+++ b/libproto/imap/select.c
@@ -25,6 +25,7 @@
 #include <mailutils/assoc.h>
 #include <mailutils/stream.h>
 #include <mailutils/imap.h>
+#include <mailutils/imaputil.h>
 #include <mailutils/sys/imap.h>
 
 static int
diff --git a/libproto/imap/tests/imapfolder.c b/libproto/imap/tests/imapfolder.c
index cb3a08b..d19b052 100644
--- a/libproto/imap/tests/imapfolder.c
+++ b/libproto/imap/tests/imapfolder.c
@@ -133,7 +133,6 @@ static struct command comtab[] = {
   { "lsub", 2, "REF MBX", com_lsub },
   { "delete", 1, "MBX", com_delete },
   { "rename", 2, "OLD NEW", com_rename },
-  { "delete", 1, "MBOX", com_delete },
   { "subscribe", 1, "MBX", com_subscribe },
   { "unsubscribe", 1, "MBX", com_unsubscribe },
   { NULL }
diff --git a/libproto/maildir/folder.c b/libproto/maildir/folder.c
index bba6848..df25e53 100644
--- a/libproto/maildir/folder.c
+++ b/libproto/maildir/folder.c
@@ -37,12 +37,6 @@
 #include <mailutils/sys/amd.h>
 
 static int
-_maildir_folder_init (mu_folder_t folder MU_ARG_UNUSED)
-{
-  return 0;
-}
-
-static int
 dir_exists (const char *name, const char *suf)
 {
   struct stat st;
@@ -59,31 +53,42 @@ dir_exists (const char *name, const char *suf)
 static int
 _maildir_is_scheme (mu_record_t record, mu_url_t url, int flags)
 {
-  if (mu_url_is_scheme (url, record->scheme))
-    return MU_FOLDER_ATTRIBUTE_FILE & flags; 
-
-  if (mu_scheme_autodetect_p (url))
+  int scheme_matched = mu_url_is_scheme (url, record->scheme);
+  int rc = 0;
+  
+  if (scheme_matched || mu_scheme_autodetect_p (url))
     {
       /* Attemp auto-detection */
       const char *path;
       struct stat st;
-      
+
       if (mu_url_sget_path (url, &path))
         return 0;
 
       if (stat (path, &st) < 0)
-	return 0; 
-
+	{
+	  if (errno == ENOENT && scheme_matched)
+	    return MU_FOLDER_ATTRIBUTE_ALL & flags; 
+	  return 0; 
+	}
+      
       if (!S_ISDIR (st.st_mode))
 	return 0;
 
-      if ((flags & MU_FOLDER_ATTRIBUTE_FILE)
-	  && dir_exists (path, TMPSUF)
-	     && dir_exists (path, CURSUF)
- 	     && dir_exists (path, NEWSUF))
-        return MU_FOLDER_ATTRIBUTE_FILE|MU_FOLDER_ATTRIBUTE_DIRECTORY;
+      if (scheme_matched)
+	rc = MU_FOLDER_ATTRIBUTE_ALL;
+      else
+	{
+	  rc |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
+      
+	  if ((flags & MU_FOLDER_ATTRIBUTE_FILE)
+	      && dir_exists (path, TMPSUF)
+	      && dir_exists (path, CURSUF)
+	      && dir_exists (path, NEWSUF))
+	    rc |= MU_FOLDER_ATTRIBUTE_FILE;
+	}
     }
-  return 0;
+  return rc & flags;
 }
 
 static int
@@ -106,7 +111,7 @@ static struct _mu_record _maildir_record =
   mu_url_expand_path, /* Url init.  */
   _mailbox_maildir_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
-  _maildir_folder_init, /* Folder init.  */
+  _mu_fsfolder_init, /* Folder init.  */
   NULL, /* back pointer.  */
   _maildir_is_scheme, /* _is_scheme method.  */
   NULL, /* _get_url method.  */
diff --git a/libproto/mailer/smtp_auth.c b/libproto/mailer/smtp_auth.c
index 6929e18..e789fb8 100644
--- a/libproto/mailer/smtp_auth.c
+++ b/libproto/mailer/smtp_auth.c
@@ -32,7 +32,8 @@
 static int
 get_ticket (mu_ticket_t *pticket)
 {
-  char *filename = mu_tilde_expansion (mu_ticket_file, "/", NULL);
+  char *filename = mu_tilde_expansion (mu_ticket_file,
+				       MU_HIERARCHY_DELIMITER, NULL);
   mu_wicket_t wicket;
   int rc;
 
diff --git a/libproto/mbox/folder.c b/libproto/mbox/folder.c
index 2cc23bb..9e2c3f1 100644
--- a/libproto/mbox/folder.c
+++ b/libproto/mbox/folder.c
@@ -49,19 +49,23 @@ static int
 _mbox_is_scheme (mu_record_t record, mu_url_t url, int flags)
 {
   int rc = 0;
-  
-  if (mu_url_is_scheme (url, record->scheme))
-    return MU_FOLDER_ATTRIBUTE_FILE & flags;
-  
-  if (mu_scheme_autodetect_p (url))
+  int scheme_matched = mu_url_is_scheme (url, record->scheme);
+  if (scheme_matched || mu_scheme_autodetect_p (url))
     {
       struct stat st;
       const char *path;
 
       mu_url_sget_path (url, &path);
       if (stat (path, &st) < 0)
-	return 0;
-
+	{
+	  if (errno == ENOENT)
+	    {
+	      if (scheme_matched)
+		return MU_FOLDER_ATTRIBUTE_FILE & flags;
+	    }
+	  return 0;
+	}
+      
       if (S_ISREG (st.st_mode) || S_ISCHR (st.st_mode))
 	{
 	  if (st.st_size == 0)
@@ -71,7 +75,7 @@ _mbox_is_scheme (mu_record_t record, mu_url_t url, int flags)
 	  else if (flags & MU_FOLDER_ATTRIBUTE_FILE)
 	    {
 #if 0
-	      /* This effectively sieves out all non-mailbox files,
+	      /* FIXME: This effectively sieves out all non-mailbox files,
 		 but it makes mu_folder_enumerate crawl, which is
 		 intolerable for imap4d LIST command. */
 	      int fd = open (path, O_RDONLY);
@@ -106,7 +110,7 @@ static struct _mu_record _mbox_record =
   mu_url_expand_path, /* URL init.  */
   _mailbox_mbox_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
-  _folder_mbox_init, /* Folder init.  */
+  _mu_fsfolder_init, /* Folder init.  */
   NULL, /* No need for an back pointer.  */
   _mbox_is_scheme, /* _is_scheme method.  */
   NULL, /* _get_url method.  */
@@ -116,499 +120,4 @@ static struct _mu_record _mbox_record =
 };
 mu_record_t mu_mbox_record = &_mbox_record;
 
-/* lsub/subscribe/unsubscribe are not needed.  */
-static void folder_mbox_destroy    (mu_folder_t);
-static int folder_mbox_open        (mu_folder_t, int);
-static int folder_mbox_close       (mu_folder_t);
-static int folder_mbox_delete      (mu_folder_t, const char *);
-static int folder_mbox_rename      (mu_folder_t , const char *, const char *);
-static int folder_mbox_list        (mu_folder_t, const char *, void *, int,
-				    size_t, mu_list_t, mu_folder_enumerate_fp,
-				    void *);
-static int folder_mbox_subscribe   (mu_folder_t, const char *);
-static int folder_mbox_unsubscribe (mu_folder_t, const char *);
-static int folder_mbox_lsub        (mu_folder_t, const char *, const char *,
-				    mu_list_t);
-
-
-static char *get_pathname       (const char *, const char *);
-
-static int folder_mbox_get_authority (mu_folder_t folder, mu_authority_t * pauth);
-
-struct _fmbox
-{
-  char *dirname;
-  char **subscribe;
-  size_t sublen;
-};
-typedef struct _fmbox *fmbox_t;
-
-
-int
-_folder_mbox_init (mu_folder_t folder)
-{
-  fmbox_t dfolder;
-  int status = 0;
-
-  /* We create an authority so the API is uniform across the mailbox
-     types. */
-  status = folder_mbox_get_authority (folder, NULL);
-  if (status != 0)
-    return status;
-
-  dfolder = folder->data = calloc (1, sizeof (*dfolder));
-  if (dfolder == NULL)
-    return ENOMEM;
-
-  status = mu_url_aget_path (folder->url, &dfolder->dirname);
-  if (status == MU_ERR_NOENT)
-    {
-      dfolder->dirname = malloc (2);
-      if (dfolder->dirname == NULL)
-	status = ENOMEM;
-      else
-	{
-	  strcpy (dfolder->dirname, ".");
-	  status = 0;
-	}
-    }
-  
-  if (status)  
-    {
-      free (dfolder);
-      folder->data = NULL;
-      return status;
-    }
-
-  folder->_destroy = folder_mbox_destroy;
-
-  folder->_open = folder_mbox_open;
-  folder->_close = folder_mbox_close;
-
-  folder->_list = folder_mbox_list;
-  folder->_lsub = folder_mbox_lsub;
-  folder->_subscribe = folder_mbox_subscribe;
-  folder->_unsubscribe = folder_mbox_unsubscribe;
-  folder->_delete = folder_mbox_delete;
-  folder->_rename = folder_mbox_rename;
-  return 0;
-}
-
-void
-folder_mbox_destroy (mu_folder_t folder)
-{
-  if (folder->data)
-    {
-      fmbox_t fmbox = folder->data;
-      if (fmbox->dirname)
-	free (fmbox->dirname);
-      if (fmbox->subscribe)
-	free (fmbox->subscribe);
-      free (folder->data);
-      folder->data = NULL;
-    }
-}
-
-/* Noop. */
-static int
-folder_mbox_open (mu_folder_t folder, int flags MU_ARG_UNUSED)
-{
-  fmbox_t fmbox = folder->data;
-  if (flags & MU_STREAM_CREAT)
-    {
-      return (mkdir (fmbox->dirname, S_IRWXU) == 0) ? 0 : errno;
-    }
-  return 0;
-}
-
-/*  Noop.  */
-static int
-folder_mbox_close (mu_folder_t folder MU_ARG_UNUSED)
-{
-  return 0;
-}
-
-static int
-folder_mbox_delete (mu_folder_t folder, const char *filename)
-{
-  fmbox_t fmbox = folder->data;
-  if (filename)
-    {
-      int status = 0;
-      char *pathname = get_pathname (fmbox->dirname, filename);
-      if (pathname)
-	{
-	  if (remove (pathname) != 0)
-	    status = errno;
-	  free (pathname);
-	}
-      else
-	status = ENOMEM;
-      return status;
-    }
-  return EINVAL;
-}
-
-static int
-folder_mbox_rename (mu_folder_t folder, const char *oldpath,
-		    const char *newpath)
-{
-  fmbox_t fmbox = folder->data;
-  if (oldpath && newpath)
-    {
-      int status = 0;
-      char *pathold = get_pathname (fmbox->dirname, oldpath);
-      if (pathold)
-	{
-	  char *pathnew = get_pathname (fmbox->dirname, newpath);
-	  if (pathnew)
-	    {
-	      if (rename (pathold, pathnew) != 0)
-		status = errno;
-	      free (pathnew);
-	    }
-	  else
-	    status = ENOMEM;
-	  free (pathold);
-	}
-      else
-	status = ENOMEM;
-      return status;
-    }
-  return EINVAL;
-}
-
-struct inode_list           /* Inode/dev number list used to cut off
-			       recursion */
-{
-  struct inode_list *next;
-  ino_t inode;
-  dev_t dev;
-};
-
-struct search_data
-{
-  mu_list_t result;
-  mu_folder_enumerate_fp enumfun;
-  void *enumdata;
-  char *dirname;
-  size_t dirlen;
-  void *pattern;
-  int flags;
-  size_t max_level;
-  size_t errcnt;
-  mu_folder_t folder;
-};
-
-static int
-inode_list_lookup (struct inode_list *list, struct stat *st)
-{
-  for (; list; list = list->next)
-    if (list->inode == st->st_ino && list->dev == st->st_dev)
-      return 1;
-  return 0;
-}
-
-static int
-list_helper (struct search_data *data, mu_record_t record,
-	     const char *dirname, size_t level,
-	     struct inode_list *ilist)
-{
-  DIR *dirp;
-  struct dirent *dp;
-  int stop = 0;
-    
-  if (data->max_level && level > data->max_level)
-    return 0;
-
-  dirp = opendir (dirname);
-  if (dirp == NULL)
-    {
-      mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
-		("list_helper cannot open directory %s: %s",
-		 dirname, mu_strerror (errno)));
-      data->errcnt++;
-      return 1;
-    }
-
-  if (!record)
-    {
-      int type;
-      mu_registrar_lookup (dirname, MU_FOLDER_ATTRIBUTE_ALL,
-			   &record, &type);
-    }
-  
-  while ((dp = readdir (dirp)))
-    {
-      char const *ename = dp->d_name;
-      char *fname;
-      struct stat st;
-      
-      if (ename[ename[0] != '.' ? 0 : ename[1] != '.' ? 1 : 2] == 0)
-	continue;
-      fname = get_pathname (dirname, ename);
-      if (lstat (fname, &st) == 0)
-	{
-	  int f;
-	  if (S_ISDIR (st.st_mode))
-	    f = MU_FOLDER_ATTRIBUTE_DIRECTORY;
-	  else if (S_ISREG (st.st_mode))
-	    f = MU_FOLDER_ATTRIBUTE_FILE;
-	  else if (S_ISLNK (st.st_mode))
-	    f = MU_FOLDER_ATTRIBUTE_LINK;
-	  else
-	    f = 0;
-	  if (mu_record_list_p (record, ename, f))
-	    {
-	      if (data->folder->_match == NULL
-		  || data->folder->_match (fname + data->dirlen +
-					   ((data->dirlen > 1
-					     && data->dirname[data->dirlen-1] != '/') ?
-					    1 : 0),
-					   data->pattern,
-					   data->flags) == 0)
-		{
-		  char *refname = fname;
-		  int type = 0;
-		  struct mu_list_response *resp;
-		  mu_record_t rec = NULL;
-		  
-		  resp = malloc (sizeof (*resp));
-		  if (resp == NULL)
-		    {
-		      mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
-				("list_helper: %s", mu_strerror (ENOMEM)));
-		      data->errcnt++;
-		      free (fname);
-		      continue;
-		    }
-		  
-		  mu_registrar_lookup (refname, MU_FOLDER_ATTRIBUTE_ALL,
-				       &rec, &type);
-		  
-		  resp->name = fname;
-		  resp->level = level;
-		  resp->separator = '/';
-		  resp->type = type;
-		  resp->format = rec;
-		  
-		  if (resp->type == 0)
-		    {
-		      free (resp->name);
-		      free (resp);
-		      continue;
-		    }
-		  
-		  if (data->enumfun)
-		    {
-		      if (data->enumfun (data->folder, resp, data->enumdata))
-			{
-			  free (resp->name);
-			  free (resp);
-			  stop = 1;
-			  break;
-			}
-		    }
-		  
-		  if (data->result)
-		    {
-		      fname = NULL;
-		      mu_list_append (data->result, resp);
-		    }
-		  else
-		    free (resp);
-		  
-		  if ((type & MU_FOLDER_ATTRIBUTE_DIRECTORY)
-		      && !inode_list_lookup (ilist, &st))
-		    {
-		      struct inode_list idata;
-		      
-		      idata.inode = st.st_ino;
-		      idata.dev   = st.st_dev;
-		      idata.next  = ilist;
-		      stop = list_helper (data, rec, refname, level + 1,
-					  &idata);
-		    }
-		}
-	      else if (S_ISDIR (st.st_mode))
-		{
-		  struct inode_list idata;
-		  
-		  idata.inode = st.st_ino;
-		  idata.dev   = st.st_dev;
-		  idata.next  = ilist;
-		  stop = list_helper (data, NULL, fname, level + 1, &idata);
-		}
-	    }
-	}
-      else
-	{
-	  mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
-		    ("list_helper cannot stat %s: %s",
-		     fname, mu_strerror (errno)));
-	}
-      free (fname);
-    }
-  closedir (dirp);
-  return stop;
-}
-
-static int
-folder_mbox_list (mu_folder_t folder, const char *ref,
-		  void *pattern,
-		  int flags,
-		  size_t max_level,
-		  mu_list_t flist,
-		  mu_folder_enumerate_fp enumfun, void *enumdata)
-{
-  fmbox_t fmbox = folder->data;
-  struct inode_list iroot;
-  struct search_data sdata;
-  
-  memset (&iroot, 0, sizeof iroot);
-  sdata.dirname = get_pathname (fmbox->dirname, ref);
-  sdata.dirlen = strlen (sdata.dirname);
-  sdata.result = flist;
-  sdata.enumfun = enumfun;
-  sdata.enumdata = enumdata;
-  sdata.pattern = pattern;
-  sdata.flags = flags;
-  sdata.max_level = max_level;
-  sdata.folder = folder;
-  sdata.errcnt = 0;
-  list_helper (&sdata, NULL, sdata.dirname, 0, &iroot);
-  free (sdata.dirname);
-  /* FIXME: error code */
-  return 0;
-}
-
-static int
-folder_mbox_lsub (mu_folder_t folder, const char *ref MU_ARG_UNUSED,
-		  const char *name,
-		  mu_list_t flist)
-{
-  fmbox_t fmbox = folder->data;
-  int status;
-  
-  if (name == NULL || *name == '\0')
-    name = "*";
-
-  if (fmbox->sublen > 0)
-    {      
-      size_t i;
-
-      for (i = 0; i < fmbox->sublen; i++)
-	{
-	  if (fmbox->subscribe[i]
-	      && fnmatch (name, fmbox->subscribe[i], 0) == 0)
-	    {
-	      struct mu_list_response *resp;
-	      resp = malloc (sizeof (*resp));
-	      if (resp == NULL)
-		{
-		  status = ENOMEM;
-		  break;
-		}
-	      else if ((resp->name = strdup (fmbox->subscribe[i])) == NULL)
-		{
-		  free (resp);
-		  status = ENOMEM;
-		  break;
-		}
-	      resp->type = MU_FOLDER_ATTRIBUTE_FILE;
-	      resp->level = 0;
-	      resp->separator = '/';
-	    }
-	}
-    }
-  return status;
-}
-
-static int
-folder_mbox_subscribe (mu_folder_t folder, const char *name)
-{
-  fmbox_t fmbox = folder->data;
-  char **tmp;
-  size_t i;
-  for (i = 0; i < fmbox->sublen; i++)
-    {
-      if (fmbox->subscribe[i] && strcmp (fmbox->subscribe[i], name) == 0)
-	return 0;
-    }
-  tmp = realloc (fmbox->subscribe, (fmbox->sublen + 1) * sizeof (*tmp));
-  if (tmp == NULL)
-    return ENOMEM;
-  fmbox->subscribe = tmp;
-  fmbox->subscribe[fmbox->sublen] = strdup (name);
-  if (fmbox->subscribe[fmbox->sublen] == NULL)
-    return ENOMEM;
-  fmbox->sublen++;
-  return 0;
-}
-
-static int
-folder_mbox_unsubscribe (mu_folder_t folder, const char *name)
-{
-  fmbox_t fmbox = folder->data;
-  size_t i;
-  for (i = 0; i < fmbox->sublen; i++)
-    {
-      if (fmbox->subscribe[i] && strcmp (fmbox->subscribe[i], name) == 0)
-	{
-	  free (fmbox->subscribe[i]);
-	  fmbox->subscribe[i] = NULL;
-	  return 0;
-	}
-    }
-  return MU_ERR_NOENT;
-}
-
-static char *
-get_pathname (const char *dirname, const char *basename)
-{
-  char *pathname = NULL;
-
-  /* Skip eventual protocol designator.
-     FIXME: Actually, any valid URL spec should be allowed as dirname ... */
-  if (strncmp (dirname, MU_MBOX_SCHEME, MU_MBOX_SCHEME_LEN) == 0)
-    dirname += MU_MBOX_SCHEME_LEN;
-  else if (strncmp (dirname, MU_FILE_SCHEME, MU_FILE_SCHEME_LEN) == 0)
-    dirname += MU_FILE_SCHEME_LEN;
-  
-  /* null basename gives dirname.  */
-  if (basename == NULL)
-    pathname = (dirname) ? strdup (dirname) : strdup (".");
-  /* Absolute.  */
-  else if (basename[0] == '/')
-    pathname = strdup (basename);
-  /* Relative.  */
-  else
-    {
-      size_t baselen = strlen (basename);
-      size_t dirlen = strlen (dirname);
-      while (dirlen > 0 && dirname[dirlen-1] == '/')
-	dirlen--;
-      pathname = calloc (dirlen + baselen + 2, sizeof (char));
-      if (pathname)
-	{
-	  memcpy (pathname, dirname, dirlen);
-	  pathname[dirlen] = '/';
-	  strcpy (pathname + dirlen + 1, basename);
-	}
-    }
-  return pathname;
-}
-
-static int
-folder_mbox_get_authority (mu_folder_t folder, mu_authority_t *pauth)
-{
-  int status = 0;
-  if (folder->authority == NULL)
-    {
-	status = mu_authority_create_null (&folder->authority, folder);
-    }
-  if (!status && pauth)
-    *pauth = folder->authority;
-  return status;
-}
 
diff --git a/libproto/mh/folder.c b/libproto/mh/folder.c
index 66e02cf..4d9b43f 100644
--- a/libproto/mh/folder.c
+++ b/libproto/mh/folder.c
@@ -37,12 +37,6 @@
 #include <mailutils/util.h>
 #include <mailutils/cctype.h>
 
-static int
-_mh_folder_init (mu_folder_t folder MU_ARG_UNUSED)
-{
-  return 0;
-}
-
 /* Check if NAME is a valid MH message name */
 static int
 mh_message_name_p (const char *name)
@@ -90,13 +84,11 @@ static int
 _mh_is_scheme (mu_record_t record, mu_url_t url, int flags)
 {
   int rc = 0;
+  int scheme_matched = mu_url_is_scheme (url, record->scheme);
   
-  if (mu_url_is_scheme (url, record->scheme))
-    return MU_FOLDER_ATTRIBUTE_ALL & flags;
-
-  if (mu_scheme_autodetect_p (url))
+  if (scheme_matched || mu_scheme_autodetect_p (url))
     {
-      /* Attemp auto-detection */
+      /* Attempt auto-detection */
       const char *path;
       struct stat st;
       
@@ -104,18 +96,27 @@ _mh_is_scheme (mu_record_t record, mu_url_t url, int flags)
         return 0;
 
       if (stat (path, &st) < 0)
-	return 0; /* mu_mailbox_open will complain*/
-
+	{
+	  if (errno == ENOENT && scheme_matched)
+	    return MU_FOLDER_ATTRIBUTE_ALL & flags; 
+	  return 0; /* mu_mailbox_open will complain*/
+	}
+      
       if (!S_ISDIR (st.st_mode))
 	return 0;
 
-      rc |= (MU_FOLDER_ATTRIBUTE_DIRECTORY & flags);
+      if (scheme_matched)
+	rc = MU_FOLDER_ATTRIBUTE_ALL;
+      else
+	{
+	  rc |= MU_FOLDER_ATTRIBUTE_DIRECTORY;
       
-      if ((flags & MU_FOLDER_ATTRIBUTE_FILE) && mh_dir_p (path))
-        return rc | MU_FOLDER_ATTRIBUTE_FILE;
+	  if ((flags & MU_FOLDER_ATTRIBUTE_FILE) && mh_dir_p (path))
+	    rc |= MU_FOLDER_ATTRIBUTE_FILE;
+	}
     }
 
-  return 0;
+  return rc & flags;
 }
 
 static int
@@ -142,7 +143,7 @@ static struct _mu_record _mh_record =
   mu_url_expand_path, /* Url init.  */
   _mailbox_mh_init, /* Mailbox init.  */
   NULL, /* Mailer init.  */
-  _mh_folder_init, /* Folder init.  */
+  _mu_fsfolder_init, /* Folder init.  */
   NULL, /* back pointer.  */
   _mh_is_scheme, /* _is_scheme method.  */
   NULL, /* _get_url method.  */
diff --git a/mail/util.c b/mail/util.c
index b008048..747723c 100644
--- a/mail/util.c
+++ b/mail/util.c
@@ -475,7 +475,7 @@ util_get_homedir ()
 char *
 util_fullpath (const char *inpath)
 {
-  return mu_tilde_expansion (inpath, "/", NULL);
+  return mu_tilde_expansion (inpath, MU_HIERARCHY_DELIMITER, NULL);
 }
 
 char *
diff --git a/mh/install-mh.c b/mh/install-mh.c
index 8ccc5b8..da810c3 100644
--- a/mh/install-mh.c
+++ b/mh/install-mh.c
@@ -69,7 +69,7 @@ main (int argc, char **argv)
 
   name = getenv ("MH");
   if (name)
-    name = mu_tilde_expansion (name, "/", NULL);
+    name = mu_tilde_expansion (name, MU_HIERARCHY_DELIMITER, NULL);
   else
     {
       char *home = mu_get_homedir ();
diff --git a/mh/mh_global.c b/mh/mh_global.c
index f4ee5d1..2dbce1d 100644
--- a/mh/mh_global.c
+++ b/mh/mh_global.c
@@ -94,7 +94,7 @@ mh_read_profile ()
   
   p = getenv ("MH");
   if (p)
-    p = mu_tilde_expansion (p, "/", NULL);
+    p = mu_tilde_expansion (p, MU_HIERARCHY_DELIMITER, NULL);
   else
     {
       char *home = mu_get_homedir ();
diff --git a/mh/mh_init.c b/mh/mh_init.c
index d89a05d..75976f2 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -390,8 +390,8 @@ mh_audit_open (char *name, mu_mailbox_t mbox)
   mu_url_t url;
   char *namep;
   
-  namep = mu_tilde_expansion (name, "/", NULL);
-  if (strchr (namep, '/') == NULL)
+  namep = mu_tilde_expansion (name, MU_HIERARCHY_DELIMITER, NULL);
+  if (strchr (namep, MU_HIERARCHY_DELIMITER) == NULL)
     {
       char *p = mh_safe_make_file_name (mu_folder_directory (), namep);
       free (namep);
@@ -487,7 +487,7 @@ mh_expand_name (const char *base, const char *name, int is_folder)
   char *p = NULL;
   char *namep = NULL;
   
-  namep = mu_tilde_expansion (name, "/", NULL);
+  namep = mu_tilde_expansion (name, MU_HIERARCHY_DELIMITER, NULL);
   if (namep[0] == '+')
     memmove (namep, namep + 1, strlen (namep)); /* copy null byte as well */
   else if (strncmp (namep, "../", 3) == 0 || strncmp (namep, "./", 2) == 0)
@@ -536,7 +536,7 @@ mh_find_file (const char *name, char **resolved_name)
 
   if (name[0] == '~')
     {
-      s = mu_tilde_expansion (name, "/", NULL);
+      s = mu_tilde_expansion (name, MU_HIERARCHY_DELIMITER, NULL);
       *resolved_name = s;
       if (access (s, R_OK) == 0)
 	return 0;
diff --git a/mh/mhn.c b/mh/mhn.c
index ead6c83..57d7418 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -2961,7 +2961,8 @@ main (int argc, char **argv)
 	}
       message = mh_file_to_message (NULL,
 				    mu_tilde_expansion (input_file,
-							"/", NULL));
+							MU_HIERARCHY_DELIMITER,
+							NULL));
       if (!message)
 	return 1;
     }
diff --git a/mh/send.c b/mh/send.c
index b654f10..1f433b9 100644
--- a/mh/send.c
+++ b/mh/send.c
@@ -327,7 +327,7 @@ read_mts_profile ()
       name = mh_expand_name (MHLIBDIR, "mtstailor", 0);
       mts_profile = mh_read_property_file (name, 1);
 
-      name = mu_tilde_expansion ("~/.mtstailor", "/", NULL);
+      name = mu_tilde_expansion ("~/.mtstailor", MU_HIERARCHY_DELIMITER, NULL);
       local_profile = mh_read_property_file (name, 1);
 
       mh_property_merge (mts_profile, local_profile);
diff --git a/mu/imap.c b/mu/imap.c
index 92e9c42..85b0f60 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -26,6 +26,7 @@
 #include <mailutils/mailutils.h>
 #include <mailutils/imap.h>
 #include <mailutils/imapio.h>
+#include <mailutils/imaputil.h>
 #include "mu.h"
 #include "argp.h"
 #include "xalloc.h"
diff --git a/mu/shell.c b/mu/shell.c
index 891b08b..78ac94c 100644
--- a/mu/shell.c
+++ b/mu/shell.c
@@ -296,7 +296,7 @@ get_history_file_name ()
       strcpy (hname, "~/.mu_");
       strcat (hname, rl_readline_name);
       strcat (hname, HISTFILE_SUFFIX);
-      filename = mu_tilde_expansion (hname, "/", NULL);
+      filename = mu_tilde_expansion (hname, MU_HIERARCHY_DELIMITER, NULL);
       free(hname);
     }
   return filename;
diff --git a/mu/wicket.c b/mu/wicket.c
index f01b148..81a3f3e 100644
--- a/mu/wicket.c
+++ b/mu/wicket.c
@@ -141,7 +141,7 @@ mutool_wicket (int argc, char **argv)
       return 1;
     }
 
-  wicket_file = mu_tilde_expansion (wicket_file, "/", NULL);
+  wicket_file = mu_tilde_expansion (wicket_file, MU_HIERARCHY_DELIMITER, NULL);
   if (!wicket_file)
     {
       mu_diag_funcall (MU_DIAG_ERROR, "mu_tilde_expansion", wicket_file,
diff --git a/pop3d/bulletin.c b/pop3d/bulletin.c
index 9f4105d..c42229c 100644
--- a/pop3d/bulletin.c
+++ b/pop3d/bulletin.c
@@ -75,7 +75,8 @@ read_popbull_file (size_t *pnum)
 {
   int rc = 1;
   FILE *fp;
-  char *filename = mu_tilde_expansion ("~/.popbull", "/", auth_data->dir);
+  char *filename = mu_tilde_expansion ("~/.popbull", MU_HIERARCHY_DELIMITER,
+				       auth_data->dir);
   
   if (!filename)
     return 1;
@@ -99,7 +100,8 @@ write_popbull_file (size_t num)
 {
   int rc = 1;
   FILE *fp;
-  char *filename = mu_tilde_expansion ("~/.popbull", "/", auth_data->dir);
+  char *filename = mu_tilde_expansion ("~/.popbull", MU_HIERARCHY_DELIMITER,
+				       auth_data->dir);
   
   if (!filename)
     return 1;
diff --git a/python/Makefile.am b/python/Makefile.am
index bdf55e6..08be9af 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -15,4 +15,5 @@
 ## along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
 
 SUBDIRS = libmu_py mailutils
+EXTRA_DIST=usercustomize.py
 
diff --git a/python/libmu_py/Makefile.am b/python/libmu_py/Makefile.am
index d3d198d..84350c7 100644
--- a/python/libmu_py/Makefile.am
+++ b/python/libmu_py/Makefile.am
@@ -60,8 +60,3 @@ libmu_py_la_LIBADD = $(PYTHON_LIBS) @MU_COMMON_LIBRARIES@ \
  @MU_AUTHLIBS@\
  ${MU_LIB_MAILUTILS}
 
-pythonexecdir=@PYTHON_EXEC_DIR@
-pythonexec_LTLIBRARIES = c_api.la
-c_api_la_LDFLAGS = -avoid-version -module -rpath $(pythonexecdir)
-c_api_la_LIBADD = $(PYTHON_LIBS) ${MU_LIB_PY}
-c_api_la_SOURCES = c_api.c
diff --git a/python/mailutils/Makefile.am b/python/mailutils/Makefile.am
index 1208361..e3bb74a 100644
--- a/python/mailutils/Makefile.am
+++ b/python/mailutils/Makefile.am
@@ -39,3 +39,11 @@ pythonsite_PYTHON=\
  url.py \
  util.py
 
+pythonexecdir=@PYTHON_EXEC_DIR@
+pythonexec_LTLIBRARIES = c_api.la
+c_api_la_LDFLAGS = -avoid-version -module -rpath $(pythonexecdir)
+c_api_la_LIBADD = $(PYTHON_LIBS) ${MU_LIB_PY}
+c_api_la_SOURCES = c_api.c
+
+INCLUDES = @MU_LIB_COMMON_INCLUDES@ @PYTHON_INCLUDES@\
+ -I$(top_srcdir)/python/libmu_py
diff --git a/python/libmu_py/c_api.c b/python/mailutils/c_api.c
similarity index 100%
rename from python/libmu_py/c_api.c
rename to python/mailutils/c_api.c
diff --git a/python/usercustomize.py b/python/usercustomize.py
new file mode 100644
index 0000000..585377c
--- /dev/null
+++ b/python/usercustomize.py
@@ -0,0 +1,63 @@
+# Libtool library loader for Python.
+# This file is part of GNU Mailutils.
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. 
+
+# This file modifies the normal import routine by looking additionally
+# into module.la file and trying to load corresponding so library (as
+# determined by the dlname variable in it) from the .libs subdirectory.
+#
+# It is used to load Mailutils Python modules from the source tree.
+#
+# Example usage:
+#
+#  PYTHONPATH=/path/to/mailutils-3.0/python python test.py
+
+import sys,os,imp
+
+class laloader(object):
+    def __init__(self, filename, libdir):
+        self.filename = filename
+        self.libdir = libdir
+
+    def load_module(self, name):
+        with open(self.filename) as f:
+            for line in f:
+                s = line.strip()
+                if s.startswith('dlname='):
+                    s = s[7:].strip("'\"")
+                    libname = self.libdir + '/' + s
+                    if os.path.exists(libname):
+                        return imp.load_dynamic(name, libname)
+        return None
+    
+class lafinder(object):
+    def __init__(self):
+        pass
+    def find_module(self, fullname, path=None):
+        ml = fullname.split(".")
+        s = '/'.join(ml)+'.la'
+        libs = '/'.join(ml[:-1]) + '/.libs'
+        for dir in sys.path:
+            name = dir + '/' + s
+            libdir = dir + '/' + libs
+            if os.path.exists(name) and os.path.exists(libdir):
+                return laloader(name, libdir)
+        return None
+
+sys.meta_path.append(lafinder())
+
+# End of file
+
diff --git a/sieve/sieve.c b/sieve/sieve.c
index 07c41f5..a1107ca 100644
--- a/sieve/sieve.c
+++ b/sieve/sieve.c
@@ -231,7 +231,7 @@ parser (int key, char *arg, struct argp_state *state)
     case ARGP_KEY_ARG:
       if (script)
 	argp_error (state, _("Only one SCRIPT can be specified"));
-      script = mu_tilde_expansion (arg, "/", NULL);
+      script = mu_tilde_expansion (arg, MU_HIERARCHY_DELIMITER, NULL);
       break;
 
     case ARGP_KEY_INIT:
diff --git a/testsuite/.gitignore b/testsuite/.gitignore
index 9db984b..29a9221 100644
--- a/testsuite/.gitignore
+++ b/testsuite/.gitignore
@@ -1,5 +1,6 @@
 atconfig
 atlocal
+fldel
 package.m4
 testsuite
 testsuite.dir
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 1707985..219731d 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -50,6 +50,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 noinst_PROGRAMS = \
+ fldel\
  lstuid\
  mbdel\
  mimetest\
@@ -78,6 +79,7 @@ smtpsend_LDADD = \
 ## ------------ ##
 
 TESTSUITE_AT = \
+ fldel.at\
  lstuid00.at\
  lstuid01.at\
  lstuid02.at\
diff --git a/testsuite/atlocal.in b/testsuite/atlocal.in
index 9cd80d1..2a16c4d 100644
--- a/testsuite/atlocal.in
+++ b/testsuite/atlocal.in
@@ -3,3 +3,5 @@
 # Copyright (C) 2004, 2010, 2011 Free Software Foundation, Inc.
 
 PATH=@abs_builddir@:@abs_top_builddir@/libtests:$top_srcdir:$srcdir:$PATH
+@MU_COND_SUPPORT_MH_TRUE@MH_SUPPORT=yes
+@MU_COND_SUPPORT_MAILDIR_TRUE@MAILDIR_SUPPORT=yes
diff --git a/testsuite/fldel.at b/testsuite/fldel.at
new file mode 100644
index 0000000..06ca9d8
--- /dev/null
+++ b/testsuite/fldel.at
@@ -0,0 +1,81 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+dnl ------------------------------------------------------------
+dnl FLDEL_TEST([FORMAT], [PROG],
+dnl             [CODE], [STDOUT = `'], [STDERR = `'],
+dnl             [RUN-IF-FAIL], [RUN-IF-PASS])
+m4_pushdef([FLDEL_TEST],[
+AT_SETUP([Folder deletion: $1])
+AT_KEYWORDS([folder fldel fldel-$1])
+AT_CHECK([$2],[$3],[$4],[$5],[$6],[$7],[$8])
+AT_CLEANUP
+])
+
+dnl ------------------------------------------------------------
+FLDEL_TEST([mbox],
+[mkdir dir
+MUT_MBCOPY($abs_top_srcdir/testsuite/spool/mbox1, dir/mbox1)
+fldel mbox:dir mbox1 || exit 2
+test -f dir/mbox1
+],
+[1])
+
+FLDEL_TEST([mh],
+[test "$MH_SUPPORT" = yes || AT_SKIP_TEST
+mkdir dir
+MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1, dir/mbox1)
+fldel mh:dir mbox1 || exit 2
+test -d dir/mbox1
+],
+[1])
+
+FLDEL_TEST([maildir],
+[test "$MAILDIR_SUPPORT" = yes || AT_SKIP_TEST
+mkdir dir
+MUT_MBCOPY($abs_top_srcdir/testsuite/maildir/mbox1, dir/mbox1)
+fldel maildir:dir mbox1 || exit 2
+test -d dir/mbox1
+],
+[1])
+
+FLDEL_TEST([mh with subdirs],
+[test "$MH_SUPPORT" = yes || AT_SKIP_TEST
+mkdir dir
+MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1, dir/mbox1)
+mkdir dir/mbox1/subfolder
+fldel mh:dir mbox1 || exit 2
+test -d dir/mbox1 && find dir/mbox1
+],
+[0],
+[mailbox removed, but has subfolders
+dir/mbox1
+dir/mbox1/subfolder
+])
+
+FLDEL_TEST([maildir with subdirs],
+[test "$MAILDIR_SUPPORT" = yes || AT_SKIP_TEST
+mkdir dir
+MUT_MBCOPY($abs_top_srcdir/testsuite/maildir/mbox1, dir/mbox1)
+mkdir dir/mbox1/subfolder
+fldel maildir:dir mbox1 || exit 2
+test -d dir/mbox1 && find dir/mbox1
+],
+[0],
+[mailbox removed, but has subfolders
+dir/mbox1
+dir/mbox1/subfolder
+])
diff --git a/testsuite/mbdel.c b/testsuite/fldel.c
similarity index 85%
copy from testsuite/mbdel.c
copy to testsuite/fldel.c
index 8451df5..206f6b6 100644
--- a/testsuite/mbdel.c
+++ b/testsuite/fldel.c
@@ -26,18 +26,18 @@ int
 main (int argc, char **argv)
 {
   int rc;
-  mu_mailbox_t mbox;
+  mu_folder_t f;
   
-  if (argc != 2)
+  if (argc != 3)
     {
-      fprintf (stderr, "usage: %s URL\n", argv[0]);
+      fprintf (stderr, "usage: %s URL NAME\n", argv[0]);
       return 1;
     }
 
   mu_register_all_mbox_formats ();
   
-  MU_ASSERT (mu_mailbox_create (&mbox, argv[1]));
-  rc = mu_mailbox_remove (mbox);
+  MU_ASSERT (mu_folder_create (&f, argv[1]));
+  rc = mu_folder_delete (f, argv[2]);
   if (rc)
     {
       if (rc == ENOTEMPTY)
@@ -48,7 +48,7 @@ main (int argc, char **argv)
       else
 	fprintf (stderr, "%s\n", mu_strerror (rc));
     }
-  mu_mailbox_destroy (&mbox);
+  mu_folder_destroy (&f);
   
   return rc != 0;
 }
diff --git a/testsuite/mbdel.at b/testsuite/mbdel.at
index 4e84332..c3693eb 100644
--- a/testsuite/mbdel.at
+++ b/testsuite/mbdel.at
@@ -18,7 +18,7 @@ dnl ------------------------------------------------------------
 dnl MBDEL_TEST([FORMAT], [PROG],
 dnl             [CODE], [STDOUT = `'], [STDERR = `'],
 dnl             [RUN-IF-FAIL], [RUN-IF-PASS])
-m4_define([MBDEL_TEST],[
+m4_pushdef([MBDEL_TEST],[
 AT_SETUP([Mailbox deletion: $1])
 AT_KEYWORDS([mailbox mbdel mbdel-$1])
 AT_CHECK([$2],[$3],[$4],[$5],[$6],[$7],[$8])
@@ -34,21 +34,24 @@ test -f mbox1
 [1])
 
 MBDEL_TEST([mh],
-[MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1)
+[test "$MH_SUPPORT" = yes || AT_SKIP_TEST
+MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1)
 mbdel mh:mbox1 || exit 2
 test -d mbox1
 ],
 [1])
 
 MBDEL_TEST([maildir],
-[MUT_MBCOPY($abs_top_srcdir/testsuite/maildir/mbox1)
+[test "$MAILDIR_SUPPORT" = yes || AT_SKIP_TEST
+MUT_MBCOPY($abs_top_srcdir/testsuite/maildir/mbox1)
 mbdel maildir:mbox1 || exit 2
 test -d mbox1
 ],
 [1])
 
 MBDEL_TEST([mh with subdirs],
-[MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1)
+[test "$MH_SUPPORT" = yes || AT_SKIP_TEST
+MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1)
 mkdir mbox1/subfolder
 mbdel mh:mbox1 || exit 2
 test -d mbox1 && find mbox1
@@ -60,7 +63,8 @@ mbox1/subfolder
 ])
 
 MBDEL_TEST([maildir with subdirs],
-[MUT_MBCOPY($abs_top_srcdir/testsuite/maildir/mbox1)
+[test "$MAILDIR_SUPPORT" = yes || AT_SKIP_TEST
+MUT_MBCOPY($abs_top_srcdir/testsuite/maildir/mbox1)
 mkdir mbox1/subfolder
 mbdel maildir:mbox1 || exit 2
 test -d mbox1 && find mbox1
@@ -70,3 +74,5 @@ test -d mbox1 && find mbox1
 mbox1
 mbox1/subfolder
 ])
+
+m4_popdef([MBDEL_TEST])
diff --git a/testsuite/testsuite.at b/testsuite/testsuite.at
index 48bbe1a..87f80dc 100644
--- a/testsuite/testsuite.at
+++ b/testsuite/testsuite.at
@@ -18,9 +18,19 @@ m4_include([testsuite.inc])
 
 AT_INIT
 
+AT_BANNER(UIDs)
 m4_include([lstuid00.at])
 m4_include([lstuid01.at])
 m4_include([lstuid02.at])
+
+AT_BANNER(mimetest)
 m4_include([mime.at])
+
+AT_BANNER(Mailbox removal)
 m4_include([mbdel.at])
+
+AT_BANNER(Folder mailbox removal)
+m4_include([fldel.at])
+
+AT_BANNER(Various)
 m4_include([ufms.at])


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 20 18:26:55 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rd95D-00028c-5i
	for mharc-commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:26:55 -0500
Received: from eggs.gnu.org ([140.186.70.92]:52525)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd956-00028M-EW
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:26:53 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd951-0003YH-HO
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:26:48 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:60857)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd951-0003Y9-Ai
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:26:43 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd94z-0000Vo-M8
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 23:26:43 +0000
Date: Tue, 20 Dec 2011 23:26:41 +0000
Message-Id: <E1Rd94z-0000Vo-M8@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-540-g3c5aed3
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: c22d07a3d7bc380541bfb2603c6f5a2967ad63c9
X-Git-Newrev: 3c5aed3965c70e6e1a894005b1adc7a407d0aae9
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 20 Dec 2011 23:26: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=3c5aed3965c70e6e1a894005b1adc7a407d0aae9

The branch, master has been updated
       via  3c5aed3965c70e6e1a894005b1adc7a407d0aae9 (commit)
      from  c22d07a3d7bc380541bfb2603c6f5a2967ad63c9 (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 3c5aed3965c70e6e1a894005b1adc7a407d0aae9
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 21 00:42:00 2011 +0200

    Re-implement parser for RFC-2231 header fields.
    
    The new implementation is able to return all parameters at once,
    in an associative array. A subset of parameters can be requested.
    The data are automatically converted to the output charset.
    
    In addition, RFC-2047 parser is extended to support language
    specifications (RFC-2231, chapter 5).
    
    * include/mailutils/message.h (MU_MIMEHDR_CSINFO)
    (MU_MIMEHDR_MULTILINE): Remove, not public anymore.
    (mu_mimehdr_get_param,mu_mimehdr_aget_param)
    (mu_message_aget_attachment_name)
    (mu_message_get_attachment_name): Remove pflags agrument.
    * include/mailutils/mime.h (mu_mime_param): New struct.
    (mu_rfc2047_decode_param)
    (mu_mime_header_parse,mu_mime_header_parse_subset): New proto.
    * libmailutils/base/rfc2047.c (_rfc2047_decode_param): New
    auxiliary function.  Use memory stream to collect data.
    (mu_rfc2047_decode): Rewrite as a wrapper around the above.
    (mu_rfc2047_decode_param): New function.
    * libmailutils/filter/decode.c (mu_decode_filter_args): Pass actual
    (instead of maximal) number of arguments to mu_filter_chain_create.
    * libmailutils/mime/mimehdr.c: Rewrite from scratch.
    * libmailutils/tests/.gitignore: Add mimehdr.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add mimehdr.
    (TESTSUITE_AT): Add mimehdr.at.
    * libmailutils/tests/mimehdr.at: New test.
    * libmailutils/tests/mimehdr.c: New test program.
    * libmailutils/tests/testsuite.at: Include.

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

Summary of changes:
 include/mailutils/message.h     |   19 +-
 include/mailutils/mime.h        |   18 +-
 libmailutils/base/rfc2047.c     |  198 ++++---
 libmailutils/filter/decode.c    |    2 +-
 libmailutils/mime/mimehdr.c     | 1244 ++++++++++++++++++++++++---------------
 libmailutils/tests/.gitignore   |    1 +
 libmailutils/tests/Makefile.am  |    2 +
 libmailutils/tests/mimehdr.at   |  131 ++++
 libmailutils/tests/mimehdr.c    |  135 +++++
 libmailutils/tests/testsuite.at |    4 +-
 10 files changed, 1165 insertions(+), 589 deletions(-)
 create mode 100644 libmailutils/tests/mimehdr.at
 create mode 100644 libmailutils/tests/mimehdr.c

diff --git a/include/mailutils/message.h b/include/mailutils/message.h
index b448dda..ced7901 100644
--- a/include/mailutils/message.h
+++ b/include/mailutils/message.h
@@ -162,31 +162,20 @@ extern int mu_mime_io_buffer_aget_charset (mu_mime_io_buffer_t info,
 					   const char **charset);
 
   
-/* Bit values for *pflags in functions below */
-#define MU_MIMEHDR_MULTILINE 0x01  /* Parameter was multiline */
-#define MU_MIMEHDR_CSINFO    0x02  /* Parameter contains charset/language
-				      info */
-
 extern int mu_mimehdr_get_disp (const char *str, char *buf, size_t bufsz,
 				size_t *retsz);
 extern int mu_mimehdr_aget_disp (const char *str, char **pvalue);
 extern int mu_mimehdr_get_param (const char *str, const char *param,
-				 char *buf, size_t bufsz, size_t *retsz,
-				 int *pflags);
+				 char *buf, size_t bufsz, size_t *retsz);
 extern int mu_mimehdr_aget_param (const char *str, const char *param,
-				  char **pval, int *pflags);
-extern int mu_mimehdr_decode_param (const char *value, int csinfo,
-				    const char *charset,
-				    char **pval, char **plang);
+				  char **pval);
 extern int mu_mimehdr_aget_decoded_param (const char *str, const char *param,
 					  const char *charset, 
 					  char **pval, char **plang);
   
 extern int mu_message_get_attachment_name (mu_message_t, char *name,
-					   size_t bufsz, size_t* sz,
-					   int *pflags);
-extern int mu_message_aget_attachment_name (mu_message_t, char **name,
-					    int *pflags);
+					   size_t bufsz, size_t* sz);
+extern int mu_message_aget_attachment_name (mu_message_t, char **name);
 extern int mu_message_aget_decoded_attachment_name (mu_message_t msg,
 						    const char *charset,
 						    char **name,
diff --git a/include/mailutils/mime.h b/include/mailutils/mime.h
index d41ed38..9c3d473 100644
--- a/include/mailutils/mime.h
+++ b/include/mailutils/mime.h
@@ -29,6 +29,13 @@
 extern "C" {
 #endif
 
+struct mu_mime_param
+{
+  char *lang;
+  char *cset;
+  char *value;
+};
+
 int mu_mime_create	(mu_mime_t *pmime, mu_message_t msg, int flags);
 void mu_mime_destroy	(mu_mime_t *pmime);
 void mu_mime_ref        (mu_mime_t mime);
@@ -49,13 +56,22 @@ int mu_rfc2047_decode   (const char *tocode, const char *fromstr,
 
 int mu_rfc2047_encode   (const char *charset, const char *encoding, 
 			 const char *text, char **result);
+int mu_rfc2047_decode_param (const char *tocode, const char *input,
+			     struct mu_mime_param *param);
 
 int mu_base64_encode    (const unsigned char *input, size_t input_len,
 			 unsigned char **output, size_t * output_len);
 
 int mu_base64_decode    (const unsigned char *input, size_t input_len,
 			 unsigned char **output, size_t * output_len);
-
+
+
+int mu_mime_header_parse (const char *text, char *charset, char **pvalue,
+			  mu_assoc_t *paramtab);
+int mu_mime_header_parse_subset (const char *text, const char *charset,
+				 char **pvalue,
+				 mu_assoc_t assoc);
+  
 #ifdef __cplusplus
 }
 #endif
diff --git a/libmailutils/base/rfc2047.c b/libmailutils/base/rfc2047.c
index 33f7fda..b5b1ee7 100644
--- a/libmailutils/base/rfc2047.c
+++ b/libmailutils/base/rfc2047.c
@@ -28,20 +28,9 @@
 #include <mailutils/stream.h>
 #include <mailutils/filter.h>
 #include <mailutils/errno.h>
+#include <mailutils/mime.h>
 #include <mailutils/util.h>
 
-static int
-realloc_buffer (char **bufp, size_t *bufsizep, size_t incr)
-{
-  size_t newsize = *bufsizep + incr;
-  char *newp = realloc (*bufp, newsize);
-  if (newp == NULL)
-    return 1;
-  *bufp = newp;
-  *bufsizep = newsize;
-  return 0;
-}
-
 int
 getword (char **pret, const char **pstr, int delim)
 {
@@ -65,52 +54,32 @@ getword (char **pret, const char **pstr, int delim)
   return 0;
 }
     
-int
-mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
+static int
+_rfc2047_decode_param (const char *tocode, const char *input,
+		       struct mu_mime_param *param)
 {
   int status = 0;
   const char *fromstr;
-  char *buffer;
-  size_t bufsize;
-  size_t bufpos;
   size_t run_count = 0;
   char *fromcode = NULL;
   char *encoding_type = NULL;
   char *encoded_text = NULL;
+  char *tocodetmp = NULL;
+  mu_stream_t str;
 
-#define BUFINC 128  
-#define CHKBUF(count) do {                       \
-  if (bufpos+count >= bufsize)                   \
-    {                                            \
-      size_t s = bufpos + count - bufsize;       \
-      if (s < BUFINC)                            \
-        s = BUFINC;                              \
-      if (realloc_buffer (&buffer, &bufsize, s)) \
-	{                                        \
-	  free (buffer);                         \
-          free (fromcode);                       \
-          free (encoding_type);                  \
-          free (encoded_text);                   \
-	  return ENOMEM;                         \
-	}                                        \
-     }                                           \
- } while (0) 
-  
-  if (!input)
-    return EINVAL;
-  if (!ptostr)
-    return MU_ERR_OUT_PTR_NULL;
+  memset (param, 0, sizeof (*param));
 
-  fromstr = input;
+  status = mu_memory_stream_create (&str, MU_STREAM_RDWR);
+  if (status)
+    return status;
 
-  /* Allocate the buffer. It is assumed that encoded string is always
-     longer than it's decoded variant, so it's safe to use its length
-     as the first estimate */
-  bufsize = strlen (fromstr) + 1;
-  buffer = malloc (bufsize);
-  if (buffer == NULL)
-    return ENOMEM;
-  bufpos = 0;
+  if (tocode && (param->cset = strdup (tocode)) == NULL)
+    {
+      mu_stream_destroy (&str);
+      return ENOMEM;
+    }
+  
+  fromstr = input;
   
   while (*fromstr)
     {
@@ -119,13 +88,39 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
 	  mu_stream_t filter = NULL;
 	  mu_stream_t in_stream = NULL;
 	  const char *filter_type = NULL;
-	  size_t nbytes = 0, size;
+	  size_t size;
 	  const char *sp = fromstr + 2;
-	  char tmp[128];
+	  char *lang;
 	  
 	  status = getword (&fromcode, &sp, '?');
 	  if (status)
 	    break;
+	  lang = strchr (fromcode, '*');
+	  if (lang)
+	    *lang++ = 0;
+	  if (!param->cset)
+	    {
+	      param->cset = strdup (fromcode);
+	      if (!param->cset)
+		{
+		  status = ENOMEM;
+		  break;
+		}
+	    }
+	  if (lang && !param->lang && (param->lang = strdup (lang)) == NULL)
+	    {
+	      status = ENOMEM;
+	      break;
+	    }
+	  if (!tocode)
+	    {
+	      if ((tocodetmp = strdup (fromcode)) == NULL)
+		{
+		  status = ENOMEM;
+		  break;
+		}
+	      tocode = tocodetmp;
+	    }
 	  status = getword (&encoding_type, &sp, '?');
 	  if (status)
 	    break;
@@ -162,22 +157,12 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
 
 	  mu_static_memory_stream_create (&in_stream, encoded_text, size);
 	  mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL);
-	  status = mu_decode_filter (&filter, in_stream, filter_type, fromcode,
-				     tocode);
+	  status = mu_decode_filter (&filter, in_stream, filter_type,
+				     fromcode, tocode);
 	  mu_stream_unref (in_stream);
 	  if (status != 0)
 	    break;
-
-	  while ((status =
-		  mu_stream_read (filter, tmp, sizeof (tmp), &nbytes)) == 0
-		 && nbytes)
-	    {
-	      CHKBUF (nbytes);
-	      memcpy (buffer + bufpos, tmp, nbytes);
-	      bufpos += nbytes;
-	    }
-
-	  mu_stream_close (filter);
+	  status = mu_stream_copy (str, filter, 0, NULL);
 	  mu_stream_destroy (&filter);
 
 	  if (status)
@@ -198,44 +183,89 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
 	    {
 	      if (--run_count)
 		{
-		  CHKBUF (run_count);
-		  memcpy (buffer + bufpos, fromstr - run_count, run_count);
-		  bufpos += run_count;
+		  status = mu_stream_write (str, fromstr - run_count,
+					    run_count, NULL);
+		  if (status)
+		    break;
 		  run_count = 0;
 		}
-	      CHKBUF (1);
-	      buffer[bufpos++] = *fromstr++;
+	      status = mu_stream_write (str, fromstr, 1, NULL);
+	      if (status)
+		break;
+	      fromstr++;
 	    }
 	}
       else
 	{
-	  CHKBUF (1);
-	  buffer[bufpos++] = *fromstr++;
+	  status = mu_stream_write (str, fromstr, 1, NULL);
+	  if (status)
+	    break;
+	  fromstr++;
 	}
     }
   
-  if (*fromstr)
-    {
-      size_t len = strlen (fromstr);
-      CHKBUF (len);
-      memcpy (buffer + bufpos, fromstr, len);
-      bufpos += len;
-    }
+  if (status == 0 && *fromstr)
+    status = mu_stream_write (str, fromstr, strlen (fromstr), NULL);
 
-  CHKBUF (1);
-  buffer[bufpos++] = 0;
-  
   free (fromcode);
   free (encoding_type);
   free (encoded_text);
+  free (tocodetmp);
+  
+  if (status == 0)
+    {
+      mu_off_t size;
 
-  if (status)
-    free (buffer);
-  else
-    *ptostr = realloc (buffer, bufpos);
+      mu_stream_size (str, &size);
+      param->value = malloc (size + 1);
+      if (!param->value)
+	status = ENOMEM;
+      else
+	{
+	  mu_stream_seek (str, 0, MU_SEEK_SET, NULL);
+	  status = mu_stream_read (str, param->value, size, NULL);
+	  param->value[size] = 0;
+	}
+    }
+  
+  mu_stream_destroy (&str);
   return status;
 }
 
+int
+mu_rfc2047_decode_param (const char *tocode, const char *input,
+			 struct mu_mime_param *param)
+{
+  int rc;
+  struct mu_mime_param tmp;
+
+  if (!input)
+    return EINVAL;
+  if (!param)
+    return MU_ERR_OUT_PTR_NULL;
+  rc = _rfc2047_decode_param (tocode, input, &tmp);
+  if (rc == 0)
+    *param = tmp;
+  return rc;
+}
+
+int
+mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
+{
+  int rc;
+  struct mu_mime_param param;
+  
+  if (!input)
+    return EINVAL;
+  if (!ptostr)
+    return MU_ERR_OUT_PTR_NULL;
+  rc = _rfc2047_decode_param (tocode, input, &param);
+  free (param.cset);
+  free (param.lang);
+  if (rc == 0)
+    *ptostr = param.value;
+  return rc;
+}
 
 /**
    Encode a header according to RFC 2047
diff --git a/libmailutils/filter/decode.c b/libmailutils/filter/decode.c
index 6944474..3910d7d 100644
--- a/libmailutils/filter/decode.c
+++ b/libmailutils/filter/decode.c
@@ -73,7 +73,7 @@ mu_decode_filter_args (mu_stream_t *pfilter, mu_stream_t input,
 
   rc = mu_filter_chain_create (pfilter, input,
 			       MU_FILTER_DECODE, MU_STREAM_READ,
-			       xargc, xargv);
+			       i, xargv);
   free (xargv);
   return rc;
 }
diff --git a/libmailutils/mime/mimehdr.c b/libmailutils/mime/mimehdr.c
index 7460221..aedfac6 100644
--- a/libmailutils/mime/mimehdr.c
+++ b/libmailutils/mime/mimehdr.c
@@ -1,4 +1,5 @@
-/* GNU Mailutils -- a suite of utilities for electronic mail
+/* Operations on RFC-2231-compliant mail headers fields.
+   GNU Mailutils -- a suite of utilities for electronic mail
    Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
@@ -15,6 +16,7 @@
    You should have received a copy of the GNU Lesser General
    Public License along with this library.  If not,
    see <http://www.gnu.org/licenses/>. */
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -32,386 +34,693 @@
 #include <mailutils/mime.h>
 #include <mailutils/filter.h>
 #include <mailutils/util.h>
+#include <mailutils/wordsplit.h>
+#include <mailutils/assoc.h>
+#include <mailutils/iterator.h>
+#include <mailutils/diag.h>
+#include <mailutils/nls.h>
+
+#define MU_MIMEHDR_MULTILINE 0x01  /* Parameter was multiline */
+#define MU_MIMEHDR_CSINFO    0x02  /* Parameter contains charset/language
+				      info */
+
+/* Free members of struct mu_mime_param, but do not free it itself. */
+static void
+_mu_mime_param_free (struct mu_mime_param *p)
+{
+  free (p->lang);
+  free (p->cset);
+  free (p->value);
+}
 
-/* See RFC 2045, 5.1.  Syntax of the Content-Type Header Field */
-#define _ISSPECIAL(c) !!strchr ("()<>@,;:\\\"/[]?=", c)
+/* Treat ITEM as a pointer to struct mu_mime_param and reclaim all
+   memory associated with it.
 
-/* _header_get_param - an auxiliary function to extract values from
-   Content-Type, Content-Disposition and similar headers.
+   This is intended for use as a destroy_item method of assoc tables and
+   lists. */
+static void
+_mu_mime_param_free_item (void *item)
+{
+  _mu_mime_param_free (item);
+  free (item);
+}
 
-   Arguments:
+/* Recode a string between two charsets.
    
-   FIELD_BODY    Header value, complying to RFCs 2045, 2183, 2231.3;
-   DISP          Disposition.  Unless it is NULL, the disposition part
-                 of FIELD_BODY is compared with it.  If they differ,
-		 the function returns MU_ERR_NOENT.
-   PARAM         Name of the parameter to extract from FIELD_BODY;
-   BUF           Where to extract the value to;
-   BUFSZ         Size of BUF;
-   PRET          Pointer to the memory location for the return buffer (see
-                 below).
-   PLEN          Pointer to the return size.
-   PFLAGS        On return, flags describing the parameter are stored there.
-                 The MU_MIMEHDR_MULTILINE bit is set if the parameter value
-		 was multiline (RFC 2231.3).  The MU_MIMEHDR_CSINFO bit is set
-		 if the parameter value includes charset/language
-		 information (RFC 2231.4).
-
-   The function parses FIELD_BODY and extracts the value of the parameter
-   PARAM.
-
-   If BUF is not NULL and BUFSZ is not 0, the extracted value is stored into
-   BUF.  At most BUFSZ-1 bytes are copied.
-
-   Otherwise, if PRET is not NULL, the function allocates enough memory to
-   hold the extracted value, copies there the result, and stores the
-   pointer to the allocated memory into the location pointed to by PRET.
-
-   If PLEN is not NULL, the size of the extracted value (without terminating
-   NUL character) is stored there.
-
-   If BUF==NULL *and* PRET==NULL, no memory is allocated, but PLEN is
-   honored anyway, i.e. unless it is NULL it receives size of the result.
-   This can be used to estimate the needed buffer size.
-
-   Return values:
-     0             on success.
-     MU_ERR_NOENT, requested parameter not found, or disposition does
-                   not match DISP.
-     MU_ERR_PARSE, if FIELD_BODY does not comply to any of the abovemntioned
-                   RFCs.
-     ENOMEM      , if unable to allocate memory.
+   Input:
+     TEXT  - A string.
+     ICS   - Charset of TEXT.
+     OCS   - Charset to convert TEXT to.
+   Output:
+     PRESULT - On success, the pointer to the resulting string is stored here.
 */
+static int
+_recode_string (char *text, const char *ics, const char *ocs, char **presult)
+{
+  mu_stream_t istr, ostr, cvt;
+  mu_off_t size;
+  char *decoded;
+  int rc;
+  
+  rc = mu_static_memory_stream_create (&istr, text, strlen (text));
+  if (rc)
+    return rc;
+  rc = mu_memory_stream_create (&ostr, 0);
+  if (rc)
+    return rc;
+  rc = mu_decode_filter (&cvt, istr, NULL, ics, ocs);
+  mu_stream_unref (istr);
+  if (rc)
+    {
+      mu_stream_unref (ostr);
+      return rc;
+    }
+  rc = mu_stream_copy (ostr, cvt, 0, &size);
+  mu_stream_unref (cvt);
+  if (rc)
+    {
+      mu_stream_unref (ostr);
+      return rc;
+    }
 
-/* Internal flag used by _header_get_param to delay increasing
-   estimated continuation index. */
-#define _MU_MIMEHDR_INCR_CIND 0x8000
+  decoded = malloc (size + 1);
+  if (!decoded)
+    {
+      mu_stream_unref (ostr);
+      return ENOMEM;
+    }
 
-int
-_header_get_param (const char *field_body,
-		   const char *disp,
-		   const char *param,
-		   char *buf, size_t bufsz,
-		   char **pret, size_t *plen,
-		   int *pflags)
+  mu_stream_seek (ostr, 0, MU_SEEK_SET, NULL);
+  rc = mu_stream_read (ostr, decoded, size, NULL);
+  mu_stream_unref (ostr);
+  if (rc)
+    free (decoded);
+  else
+    {
+      decoded[size] = 0;
+      *presult = decoded;
+    }
+  return rc;
+}
+
+/* Structure for composing continued parameters.
+   See RFC 2231, Section 3, "Parameter Value Continuations" */
+struct param_continuation
 {
-  int res = MU_ERR_NOENT;            /* Return value, pessimistic default */
-  size_t param_len = strlen (param);
-  char *p;
-  size_t size;
-  char *mem = NULL;                  /* Allocated memory storage */
-  size_t retlen = 0;                 /* Total number of bytes copied */
-  unsigned long cind = 0;            /* Expected continued parameter index.
-					See RFC 2231, Section 3,
-					"Parameter Value Continuations" */
-  int flags = 0;
-  
-  if (field_body == NULL)
-    return EINVAL;
+  char *param_name;    /* Parameter name */
+  size_t param_length;        /* Length of param_name */
+  mu_stream_t param_value;    /* Its value (memory stream) */ 
+  int param_cind;             /* Expected continued parameter index. */
+  /* Language/character set information */
+  const char *param_lang;
+  const char *param_cset;
+};
 
-  if (bufsz == 0) /* Make sure buf value is meaningful */
-    buf = NULL;
+static int
+free_param_continuation (struct param_continuation *p)
+{
+  free (p->param_name);
+  mu_stream_destroy (&p->param_value);
+  /* param_lang and param_cset are handled separately */
+  memset (p, 0, sizeof (*p));
+}
+
+/* Auxiliary function to store the data collected in CONT into ASSOC.
+   If SUBSET is True, ASSOC is populated with empty mu_mime_param
+   structures. In this case data will be stored only if CONT->param_name
+   is already in ASSOC. If OUTCHARSET is not NULL, the value from
+   CONT->param_value will be recoded to that charset before storing it. */
+static int
+flush_param (struct param_continuation *cont, mu_assoc_t assoc, int subset,
+	     const char *outcharset)
+{
+  int rc;
+  struct mu_mime_param param, *param_slot = NULL;
+  mu_off_t size;
   
-  p = strchr (field_body, ';');
-  if (!p)
-    return MU_ERR_NOENT;
-  /* Allow for possible whitespace before the semicolon */
-  for (size = p - field_body;
-       size > 0 && mu_isblank (field_body[size-1]); size--)
-    ;
-  /* Remove surrounding quotes.
-     FIXME: unescape the quoted contents. */
-  if (field_body[0] == '"' && field_body[size-1] == '"')
+  if (subset)
     {
-      field_body++;
-      size -= 2;
+      param_slot = mu_assoc_ref (assoc, cont->param_name);
+      if (!param_slot)
+	return 0;
     }
-  if (disp && mu_c_strncasecmp (field_body, disp, size))
-    return MU_ERR_NOENT;
-      
-  while (p && *p)
+  
+  if (cont->param_lang)
     {
-      char *v, *e, *ep, *cp;
-      size_t len, escaped_chars = 0;
-      
-      if (*p != ';')
+      param.lang = strdup (cont->param_lang);
+      if (!param.lang)
+	return ENOMEM;
+    }
+  else
+    param.lang = NULL;
+
+  if (outcharset || cont->param_cset)
+    {
+      param.cset = strdup (outcharset ? outcharset : cont->param_cset);
+      if (!param.cset)
 	{
-	  res = MU_ERR_PARSE;
-	  break;
+	  free (param.lang);
+	  return ENOMEM;
 	}
-      
-      /* walk upto start of param */      
-      p = mu_str_skip_class (p + 1, MU_CTYPE_SPACE);
+    }
+  
+  rc = mu_stream_size (cont->param_value, &size);
+  if (rc == 0)
+    {
+      param.value = malloc (size + 1);
+      if (!param.value)
+	rc = ENOMEM;
+    }
 
-      /* Reportedly, some MUAs insert several semicolons */
-      if (*p == ';')
-	continue;
+  if (rc == 0)
+    {
+      rc = mu_stream_seek (cont->param_value, 0, MU_SEEK_SET, NULL);
+      if (rc == 0)
+	rc = mu_stream_read (cont->param_value, param.value, size, NULL);
+      param.value[size] = 0;
+    }
+  
+  if (rc)
+    {
+      free (param.lang);
+      free (param.cset);
+      return rc;
+    }
 
-      /* Ignore stray characters */
-      if (_ISSPECIAL (*p))
+  if (cont->param_cset && outcharset &&
+      mu_c_strcasecmp (cont->param_cset, outcharset))
+    {
+      char *tmp;
+      rc = _recode_string (param.value, cont->param_cset, outcharset, &tmp);
+      free (param.value);
+      if (rc)
 	{
-	  p = strchr (p, ';');
-	  continue;
+	  free (param.lang);
+	  free (param.cset);
+	  return rc;
 	}
+      param.value = tmp;
+    }
 	
-      if ((ep = strchr (p, '=')) == NULL)
-	break;
-      /* Allow for optional whitespace after '=' */
-      v = mu_str_skip_class (ep + 1, MU_CTYPE_SPACE);
-      /* Find end of the parameter */
-      if (*v == '"')
+  if (param_slot)
+    {
+      *param_slot = param;
+    }
+  else
+    {
+      rc = mu_assoc_install (assoc, cont->param_name, &param);
+      if (rc)
+	_mu_mime_param_free (&param);
+    }
+  
+  return rc;
+}
+
+/* Create and initialize an empty associative array for parameters. */
+int
+mu_mime_param_assoc_create (mu_assoc_t *paramtab)
+{
+  mu_assoc_t assoc;
+  int rc = mu_assoc_create (&assoc, sizeof (struct mu_mime_param),
+			    MU_ASSOC_ICASE);
+  if (rc == 0)
+    mu_assoc_set_free (assoc, _mu_mime_param_free_item);
+  *paramtab = assoc;
+  return rc;
+}
+
+/* Add an empty structure for the slot NAME in ASSOC. */
+int
+mu_mime_param_assoc_add (mu_assoc_t assoc, const char *name)
+{
+  struct mu_mime_param param;
+
+  memset (&param, 0, sizeof param);
+  return mu_assoc_install (assoc, name, &param);
+}
+
+/* See FIXME near the end of _mime_header_parse, below. */
+static int
+_remove_entry (void *item, void *data)
+{
+  struct mu_mime_param *p = item;
+  mu_assoc_t assoc = data;
+  mu_assoc_remove_ref (assoc, p);
+  return 0;
+}
+
+/* A working horse of this module.  Parses input string, which should
+   be a header field value complying to RFCs 2045, 2183, 2231.3.
+
+   Input:
+    TEXT   - The string.
+    ASSOC  - Associative array of parameters indexed by their names.
+    SUBSET - If true, store only those parameters that are already
+             in ASSOC.
+   Output:
+    PVALUE - Unless NULL, a pointer to the field value is stored here on
+             success.
+    ASSOC  - Unless NULL, parameters are stored here.	     
+
+   Either PVALUE or ASSOC (but not both) can be NULL, meaning that the
+   corresponding data are of no interest to the caller.
+*/
+static int
+_mime_header_parse (const char *text, char **pvalue,
+		    mu_assoc_t assoc, const char *outcharset, int subset)
+{
+  int rc;
+  struct mu_wordsplit ws;
+  struct param_continuation cont;
+  size_t i;
+
+  ws.ws_delim = " \t\r\n;";
+  ws.ws_escape = "\\\"";
+  if (mu_wordsplit (text, &ws,
+		    MU_WRDSF_DELIM | MU_WRDSF_ESCAPE |
+		    MU_WRDSF_NOVAR | MU_WRDSF_NOCMD |
+		    MU_WRDSF_DQUOTE | MU_WRDSF_SQUEEZE_DELIMS |
+		    MU_WRDSF_RETURN_DELIMS | MU_WRDSF_WS))
+    {
+      mu_debug (MU_DEBCAT_MIME, MU_DEBUG_ERROR,
+		(_("wordsplit: %s"), mu_wordsplit_strerror (&ws)));
+      return MU_ERR_PARSE;
+    }
+
+  if (!assoc)
+    {
+      if (!pvalue)
+	return MU_ERR_OUT_PTR_NULL;
+      *pvalue = strdup (ws.ws_wordv[i]);
+      mu_wordsplit_free (&ws);
+      if (!*pvalue)
+	return ENOMEM;
+      return 0;
+    }
+    
+  memset (&cont, 0, sizeof (cont));
+  for (i = 1; i < ws.ws_wordc; i++)
+    {
+      size_t klen;
+      char *key;
+      char *val;
+      const char *lang = NULL;
+      const char *cset = NULL;
+      char *langp = NULL;
+      char *csetp = NULL;
+      char *p;
+      char *decoded;
+      int flags = 0;
+      struct mu_mime_param param;
+
+      key = ws.ws_wordv[i];
+      if (key[0] == ';')
+	/* Reportedly, some MUAs insert several semicolons */
+	continue;
+      p = strchr (key, '=');
+      if (!p)
+	val = "";
+      else
 	{
-	  /* Quoted string */
-	  for (e = ++v; *e != '"'; e++)
+	  *p++ = 0;
+	  val = p;
+	}
+
+      klen = strlen (key);
+      if (klen == 0)
+	continue;
+
+      p = strchr (key, '*');
+      if (p)
+	{
+	  /* It is a parameter value continuation (RFC 2231, Section 3)
+	     or parameter value character set and language information
+	     (ibid., Section 4). */
+	  klen = p - key;
+	  if (p[1])
 	    {
-	      if (*e == 0) /* Malformed header */
+	      if (mu_isdigit (p[1]))
 		{
-		  res = MU_ERR_PARSE;
-		  break;
-		}
-	      if (*e == '\\')
-		{
-		  if (*++e == 0)
+		  char *q;
+		  unsigned long n = strtoul (p + 1, &q, 10);
+
+		  if (*q && *q != '*')
 		    {
-		      res = MU_ERR_PARSE;
-		      break;
+		      mu_debug (MU_DEBCAT_MIME, MU_DEBUG_TRACE0,
+				(_("malformed parameter name %s: skipping"),
+				 key));
+		      continue;
 		    }
-		  escaped_chars++;
+
+		  if (n != cont.param_cind)
+		    {
+		      mu_debug (MU_DEBCAT_MIME, MU_DEBUG_TRACE0,
+				(_("continuation index out of sequence in %s: "
+				   "skipping"),
+				 key));
+		      /* Ignore this parameter. Another possibility would be
+			 to drop the continuation assembled so far. That makes
+			 little difference, because the string is malformed
+			 anyway.
+
+			 We try to continue just to gather as many information
+			 as possible from this mess.
+		      */
+		      continue;
+		    }
+
+		  if (n == 0)
+		    {
+		      cont.param_name = malloc (klen + 1);
+		      if (!cont.param_name)
+			{
+			  rc = ENOMEM;
+			  break;
+			}
+		      cont.param_length = klen;
+		      memcpy (cont.param_name, key, klen);
+		      cont.param_name[klen] = 0;
+
+		      rc = mu_memory_stream_create (&cont.param_value,
+						    MU_STREAM_RDWR);
+		      if (rc)
+			break;
+		    }
+		  else if (cont.param_length != klen ||
+			   memcmp (cont.param_name, key, klen))
+		    {
+		      mu_debug (MU_DEBCAT_MIME, MU_DEBUG_TRACE0,
+				(_("continuation name mismatch: %s: "
+				   "skipping"),
+				 key));
+		      continue;
+		    }
+		      
+		  if (*q == '*')
+		    flags |= MU_MIMEHDR_CSINFO;
+		      
+		  cont.param_cind++;
+		  flags |= MU_MIMEHDR_MULTILINE;
 		}
 	    }
-	  if (res == MU_ERR_PARSE)
-	    break;
-	  len = e - v;
-	  e++;
+	  else
+	    {
+	      flags |= MU_MIMEHDR_CSINFO;
+	      *p = 0;
+	    }
 	}
-      else
+      else if (cont.param_name)
 	{
-	  for (e = v + 1; *e && !(*e == ';' || mu_isspace (*e)); e++)
-	    ;
-	  len = e - v;
+	  rc = flush_param (&cont, assoc, subset, outcharset);
+	  free_param_continuation (&cont);
+	  if (rc)
+	    break;
 	}
+      
+      if (flags & MU_MIMEHDR_CSINFO)
+	{
+	  p = strchr (val, '\'');
+	  if (p)
+	    {
+	      char *q = strchr (p + 1, '\'');
+	      if (q)
+		{
+		  cset = val;
+		  *p++ = 0;
+		  lang = p;
+		  *q++ = 0;
+		  val = q;
+		}
+	    }
 
-      /* Is it our parameter? */
-      if (mu_c_strncasecmp (p, param, param_len))
-	{			/* nope, jump to next */
-	  p = strchr (e, ';');
-	  continue;
+	  if ((flags & MU_MIMEHDR_MULTILINE) && cont.param_cind == 1)
+	    {
+	      cont.param_lang = lang;
+	      cont.param_cset = cset;
+	    }
 	}
 
-      cp = p + param_len;
-
-      if (*cp == '*')
+      if (flags & MU_MIMEHDR_CSINFO)
 	{
-	  cp++;
-	  /* It is a parameter value continuation (RFC 2231, Section 3)
-	     or parameter value character set and language information
-	     (ibid., Section 4). */
-	  if (mu_isdigit (*cp))
-	    {
-	      /* See if the index is OK */
+	  char *tmp;
 	  
-	      char *end;
-	      unsigned long n = strtoul (cp, &end, 10);
-
-	      if (*end == '*')
-		{
-		  flags |= MU_MIMEHDR_CSINFO;
-		  end++;
-		}
-	      if (n != cind)
+	  rc = mu_str_url_decode (&tmp, val);
+	  if (rc)
+	    break;
+	  if (!(flags & MU_MIMEHDR_MULTILINE))
+	    {
+	      if (!outcharset || mu_c_strcasecmp (cset, outcharset) == 0)
+		decoded = tmp;
+	      else
 		{
-		  res = MU_ERR_PARSE;
-		  break;
+		  rc = _recode_string (tmp, cset, outcharset, &decoded);
+		  free (tmp);
 		}
-	      /* Everything OK, mark this as a multiline (continued)
-		 parameter. We also need to increment the estimation,
-		 but it cannot be done right now because its value is
-		 used below to decide whether to do flag cleanup
-		 on error. So we set _MU_MIMEHDR_INCR_CIND flag instead
-		 and increment cind later. */
-	      flags |= (MU_MIMEHDR_MULTILINE|_MU_MIMEHDR_INCR_CIND);
-	      /* And point cp to the last character: there are more
-		 checks ahead. */
-	      cp = end;
+	      if (rc)
+		break;
 	    }
 	  else
-	    flags |= MU_MIMEHDR_CSINFO;
+	    decoded = tmp;
 	}
-      /* Allow for optional whitespace before '=' */
-      cp = mu_str_skip_class (cp, MU_CTYPE_SPACE);
-      /* cp must now point to the equals sign */
-      if (cp != ep)
+      else
 	{
-	  /* Clean up everything, unless we're in the middle of a
-	     parameter continuation. */
-	  if (cind == 0)
-	    flags = 0;
-
-	  /* Try next parameter */
-	  p = strchr (e, ';');
-	  continue;
+	  rc = mu_rfc2047_decode_param (outcharset, val, &param);
+	  if (rc)
+	    return rc;
+	  cset = csetp = param.cset;
+	  lang = langp = param.lang;
+	  decoded = param.value;
 	}
-
-      if (flags & _MU_MIMEHDR_INCR_CIND)
+      val = decoded;
+      
+      if (flags & MU_MIMEHDR_MULTILINE)
 	{
-	  /* Increase the estimation. */
-	  flags &= ~_MU_MIMEHDR_INCR_CIND;
-	  cind++;
+	  rc = mu_stream_write (cont.param_value, val, strlen (val),
+				NULL);
+	  free (decoded);
+	  free (csetp);
+	  free (langp);
+	  if (rc)
+	    break;
+	  continue;
 	}
-      
-      res = 0; /* Indicate success */
-      
-      /* Prepare P for the next iteration */
-      p = e;
 
-      /* Escape characters that appear in quoted-pairs are
-	 semantically "invisible" (RFC 2822, Section 3.2.2,
-	 "Quoted characters") */
-      len -= escaped_chars;
-
-      /* Adjust len if nearing end of the buffer */
-      if (bufsz && len >= bufsz)
-	len = bufsz - 1;
-
-      if (pret)
+      memset (&param, 0, sizeof (param));
+      if (lang)
 	{
-	  /* The caller wants us to allocate the memory */
-	  if (!buf && !mem)
-	    {
-	      mem = malloc (len + 1);
-	      if (!mem)
-		{
-		  res = ENOMEM;
-		  break;
-		}
-	      buf = mem;
-	    }
-	  else if (mem)
+	  param.lang = strdup (lang);
+	  if (!param.lang)
+	    rc = ENOMEM;
+	  else
 	    {
-	      /* If we got here, it means we are iterating over
-		 a parameter value continuation, and cind=0 has
-		 already been passed.  Reallocate the memory to
-		 accomodate next chunk of data. */
-	      char *newmem = realloc (mem, retlen + len + 1);
-	      if (!newmem)
+	      param.cset = strdup (cset);
+	      if (!param.cset)
 		{
-		  res = ENOMEM;
-		  break;
+		  free (param.lang);
+		  rc = ENOMEM;
 		}
-	      buf = mem = newmem;
 	    }
 	}
 
-      if (buf)
+      free (csetp);
+      free (langp);
+      
+      if (rc)
+	{
+	  free (decoded);
+	  break;
+	}
+      
+      param.value = strdup (val);
+      free (decoded);
+      if (!param.value)
 	{
-	  /* Actually copy the data.  Buf is not NULL either because
-	     the user passed it as an argument, or because we allocated
-	     memory for it. */
-	  if (escaped_chars)
+	  _mu_mime_param_free (&param);
+	  rc = ENOMEM;
+	  break;
+	}
+
+      if (subset)
+	{
+	  struct mu_mime_param *p = mu_assoc_ref (assoc, key);
+	  if (p)
+	    *p = param;
+	}
+      else
+	{
+	  rc = mu_assoc_install (assoc, key, &param);
+	  if (rc)
 	    {
-	      int i;
-	      for (i = 0; i < len; i++)
-		{
-		  if (*v == '\\')
-		    ++v;
-		  buf[retlen + i] = *v++;
-		}
+	      _mu_mime_param_free (&param);
+	      break;
 	    }
-	  else
-	    memcpy (buf + retlen, v, len);
 	}
-      /* Adjust total result size ... */
-      retlen += len;
-      /* ... and remaining buffer size, if necessary */
-      if (bufsz)
+    }
+
+  if (rc == 0 && cont.param_name)
+    rc = flush_param (&cont, assoc, subset, outcharset);
+  free_param_continuation (&cont);
+  if (rc == 0)
+    {
+      if (pvalue)
 	{
-	  bufsz -= len;
-	  if (bufsz == 0)
-	    break;
+	  *pvalue = strdup (ws.ws_wordv[0]);
+	  if (!*pvalue)
+	    rc = ENOMEM;
 	}
     }
 
-  if (res == 0)
+  mu_wordsplit_free (&ws);
+
+  if (subset)
     {
-      /* Everything OK, prepare the returned data. */
-      if (buf)
-	buf[retlen] = 0;
-      if (plen)
-	*plen = retlen;
-      if (pret)
-	*pret = mem;
-      if (pflags)
-	*pflags = flags;
+      /* Eliminate empty elements.
+	 
+	 FIXME: What I wanted to do initially is commented out, because
+	 unfortunately iterator_ctl is not defined for assoc tables...
+      */
+#if 0
+      mu_iterator_t itr;
+
+      rc = mu_assoc_get_iterator (assoc, &itr);
+      if (rc == 0)
+	{
+	  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+	       mu_iterator_next (itr))
+	    {
+	      const char *name;
+	      struct mu_mime_param *p;
+	      
+	      mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
+	      if (!p->value)
+		mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	    }
+	  mu_iterator_destroy (&itr);
+	}
+#else
+  /* ... Instead, the following kludgy approach is taken: */
+      mu_iterator_t itr;
+      mu_list_t elist;
+
+      rc = mu_list_create (&elist);
+      if (rc)
+	return rc;
+      rc = mu_assoc_get_iterator (assoc, &itr);
+      if (rc == 0)
+	{
+	  for (mu_iterator_first (itr); rc == 0 && !mu_iterator_is_done (itr);
+	       mu_iterator_next (itr))
+	    {
+	      const char *name;
+	      struct mu_mime_param *p;
+	      
+	      mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
+	      if (!p->value)
+		rc = mu_list_append (elist, p);
+	    }
+	  mu_iterator_destroy (&itr);
+	}
+      if (rc == 0)
+	mu_list_foreach (elist, _remove_entry, assoc);
+      mu_list_destroy (&elist);
+#endif
     }
-  else if (mem)
-    free (mem);
-  return res;
+  
+  return rc;
 }
 
-static size_t
-disp_segment_len (const char *str)
+/* Parse header value from TEXT and return its value and a subset of
+   parameters.
+
+   Input:
+     TEXT   - Header value.
+     CSET   - Output charset.  Can be NULL, in which case no conversions
+              take place.
+     ASSOC  - Parameter array initialized with empty slots for those
+              parameters, which are wanted on output.  It should be
+	      created using mu_mime_param_assoc_create and populated
+	      using mu_mime_param_assoc_add.
+   Output:
+     PVALUE - A pointer to the field value is stored here on success.
+     ASSOC  - Receives available parameters matching the input subset.
+
+   Either PVALUE or ASSOC (but not both) can be NULL, meaning that the
+   corresponding data are of no interest to the caller.
+*/
+int
+mu_mime_header_parse_subset (const char *text, const char *cset,
+			     char **pvalue, mu_assoc_t assoc)
 {
-  char *p = strchr (str, ';');
-  size_t size;
-  
-  if (!p)
-    size = strlen (str);
-  else
-    size = p - str;
-  while (size > 0 && mu_isblank (str[size-1]))
-    size--;
-  return size;
+  return _mime_header_parse (text, pvalue, assoc, cset, 1);
 }
 
-/* STR is a value of a structured MIME header, e.g. Content-Type.
+/* Parse header value from TEXT and return its value and parameters.
+
+   Input:
+     TEXT   - Header value.
+     CSET   - Output charset.  Can be NULL, in which case no conversions
+              take place.
+   Output:
+     PVALUE - A pointer to the field value is stored here on success.
+     PASSOC - Receives an associative array of parameters.
+
+   Either PVALUE or PASSOC (but not both) can be NULL, meaning that the
+   corresponding data are of no interest to the caller.
+*/
+int
+mu_mime_header_parse (const char *text, char *cset, char **pvalue,
+		      mu_assoc_t *passoc)
+{
+  int rc;
+  mu_assoc_t assoc;
+  
+  rc = mu_mime_param_assoc_create (&assoc);
+  if (rc == 0)
+    {
+      rc = _mime_header_parse (text, pvalue, assoc, cset, 0);
+      if (rc || !passoc)
+	mu_assoc_destroy (&assoc);
+      else
+	*passoc = assoc;
+    }
+  
+  return rc;
+}  
+
+/* TEXT is a value of a structured MIME header, e.g. Content-Type.
    This function returns the `disposition part' of it.  In other
-   words, it returns disposition, if STR is a Content-Disposition
+   words, it returns disposition, if TEXT is a Content-Disposition
    value, and `type/subtype' part, if it is a Content-Type value.
 */
 int
-mu_mimehdr_get_disp (const char *str, char *buf, size_t bufsz, size_t *retsz)
+mu_mimehdr_get_disp (const char *text, char *buf, size_t bufsz, size_t *retsz)
 {
-  size_t size;
-
-  str = mu_str_skip_class (str, MU_CTYPE_BLANK);
-  size = disp_segment_len (str);
-  if (size > 2 && str[0] == '"' && str[size-1] == '"')
+  int rc;
+  char *value;
+  
+  rc = mu_mime_header_parse (text, NULL, &value, NULL);
+  if (rc == 0)
     {
-      str++;
-      size -= 2;
+      size_t size = strlen (value);
+      if (size > bufsz)
+	size = bufsz;
+      if (buf)
+	size = mu_cpystr (buf, value, size);
+      if (retsz)
+	*retsz = size;
     }
-  if (buf)
-    size = mu_cpystr (buf, str, size);
-  if (retsz)
-    *retsz = size;
+  free (value);
   return 0;
 }
 
 /* Same as mu_mimehdr_get_disp, but allocates memory */
 int
-mu_mimehdr_aget_disp (const char *str, char **pvalue)
+mu_mimehdr_aget_disp (const char *text, char **pvalue)
 {
-  char *p;
-  size_t size;
-  
-  str = mu_str_skip_class (str, MU_CTYPE_BLANK);
-  size = disp_segment_len (str);
-  if (size > 2 && str[0] == '"' && str[size-1] == '"')
-    {
-      str++;
-      size -= 2;
-    }
-
-  p = malloc (size + 1);
-  if (!p)
-    return ENOMEM;
-  memcpy (p, str, size);
-  p[size] = 0;
-  *pvalue = p;
-  return 0;
+  return mu_mime_header_parse (text, NULL, pvalue, NULL);
 }
 
-/* Get the value of the parameter PARAM from STR, which must be
+/* Get the value of parameter NAME from STR, which must be
    a value of a structured MIME header.
    At most BUFSZ-1 of data are stored in BUF.  A terminating NUL
    character is appended to it.
@@ -419,202 +728,100 @@ mu_mimehdr_aget_disp (const char *str, char **pvalue)
    Unless NULL, RETSZ is filled with the actual length of the
    returned data (not including the NUL terminator).
 
-   Unless PFLAGS is null it will contain, on return, the flags describing
-   the parameter.  The MU_MIMEHDR_MULTILINE bit is set if the parameter value
-   was multiline (RFC 2231.3).  The MU_MIMEHDR_CSINFO bit is set if the
-   parameter value includes charset/language information (RFC 2231.4).
-   
    BUF may be NULL, in which case the function will only fill
-   RETSZ and PFLAGS, as described above. */
+   RETSZ, as described above. */
 int
-mu_mimehdr_get_param (const char *str, const char *param,
-		     char *buf, size_t bufsz, size_t *retsz,
-		     int *pflags)
+mu_mimehdr_get_param (const char *str, const char *name,
+		      char *buf, size_t bufsz, size_t *retsz)
 {
-  return _header_get_param (str, NULL, param, buf, bufsz, NULL, retsz,
-			    pflags);
+  int rc;
+  char *value;
+  
+  rc = mu_mimehdr_aget_param (str, name, &value);
+  if (rc == 0)
+    {
+      size_t size = strlen (value);
+      if (size > bufsz)
+	size = bufsz;
+      if (buf)
+	size = mu_cpystr (buf, value, size);
+      if (retsz)
+	*retsz = size;
+    }
+  free (value);
+  return rc;
 }
 
 /* Same as mu_mimehdr_get_param, but allocates memory. */
 int
-mu_mimehdr_aget_param (const char *str, const char *param,
-		      char **pval, int *pflags)
+mu_mimehdr_aget_param (const char *str, const char *name, char **pval)
 {
-  return _header_get_param (str, NULL, param, NULL, 0, pval, NULL, pflags);
+  return mu_mimehdr_aget_decoded_param (str, name, NULL, pval, NULL);
 }
 
-/* Decode a parameter value.  Arguments:
-
-   Input:
-   VALUE        Parameter value.
-   FLAGS        Flags obtained from a previous call to one of the functions
-                above.
-   CHARSET      Output charset.
-
-   Output:
-   PVAL         A pointer to the decoded value is stored there.
-                The memory is allocated using malloc.
-   PLANG        If language information was present in VALUE, its
-                malloc'ed copy is stored in the memory location pointed
-		to by this variable.  If there was no language information,
-		*PLANG is set to NULL. 
-
-   Both PVAL and PLANG may be NULL if that particular piece of information
-   is not needed. */
+
+/* Similar to mu_mimehdr_aget_param, but the returned value is decoded
+   according to the CHARSET.  Unless PLANG is NULL, it receives malloc'ed
+   language name from STR.  If there was no language name, *PLANG is set
+   to NULL. 
+*/
 int
-mu_mimehdr_decode_param (const char *value, int flags,
-			 const char *charset, char **pval, char **plang)
+mu_mimehdr_aget_decoded_param (const char *str, const char *name,
+			       const char *charset, 
+			       char **pval, char **plang)
 {
-  char *decoded;
+  mu_assoc_t assoc;
   int rc;
-  char *lang = NULL;
-  char *data;
 
-  if (flags == 0)
-    {
-      rc = mu_rfc2047_decode (charset, value, &decoded);
-      if (rc)
-	return rc;
-    }
-  else
+  rc = mu_mime_param_assoc_create (&assoc);
+  if (rc == 0)
     {
-      rc = mu_str_url_decode (&decoded, value);
-      if (rc)
-	return rc;
-  
-      if ((flags & MU_MIMEHDR_CSINFO)
-	  && (lang = strchr (decoded, '\''))
-	  && (data = strchr (lang + 1, '\'')))
+      rc = mu_mime_param_assoc_add (assoc, name);
+      if (rc == 0)
 	{
-	  char *source_cs = decoded;
-
-	  *lang++ = 0;
-	  *data++ = 0;
-
-	  lang = lang[0] ? strdup (lang) : NULL;
-      
-	  if (source_cs[0] && charset && mu_c_strcasecmp (source_cs, charset))
+	  rc = mu_mime_header_parse_subset (str, charset, NULL, assoc);
+	  if (rc == 0)
 	    {
-	      char *outval = NULL;
-	      mu_stream_t instr = NULL;
-	      mu_stream_t outstr = NULL;
-	      mu_stream_t cvt = NULL;
-	      char iobuf[512];
-	  
-	      do
+	      struct mu_mime_param *param = mu_assoc_ref (assoc, name);
+	      if (!param)
+		rc = MU_ERR_NOENT;
+	      else
 		{
-		  size_t total = 0, pos;
-		  size_t nbytes;
-
-		  rc = mu_static_memory_stream_create (&instr, data,
-						       strlen (data));
-		  if (rc)
-		    break;
-
-		  rc = mu_memory_stream_create (&outstr, 0);
-		  if (rc)
-		    break;
-
-		  rc = mu_decode_filter (&cvt, instr, NULL,
-					 source_cs, charset);
-		  if (rc)
-		    break;
-		  
-		  while (mu_stream_read (cvt, iobuf, sizeof (iobuf),
-					 &nbytes) == 0
-			 && nbytes)
-		    {
-		      rc = mu_stream_write (outstr, iobuf, nbytes, NULL);
-		      if (rc)
-			break;
-		      total += nbytes;
-		    }
-		  
-		  if (rc)
-		    break;
-		  
-		  outval = malloc (total + 1);
-		  if (!outval)
+		  *pval = param->value;
+		  if (plang)
 		    {
-		      rc = ENOMEM;
-		      break;
+		      *plang = param->lang;
+		      param->lang = NULL;
 		    }
-
-		  mu_stream_seek (outstr, 0, MU_SEEK_SET, NULL);
-		  pos = 0;
-		  while (mu_stream_read (outstr, outval + pos,
-					 total - pos, &nbytes) == 0
-			 && nbytes)
-		    pos += nbytes;
-		  outval[pos] = 0;
+		  param->value = NULL;
 		}
-	      while (0);
-	      
-	      mu_stream_close (cvt);
-	      mu_stream_destroy (&cvt);
-	      mu_stream_close (instr);
-	      mu_stream_destroy (&instr);
-	      mu_stream_close (outstr);
-	      mu_stream_destroy (&outstr);
-	      
-	      free (decoded);
-	  
-	      if (rc)
-		{
-		  /* Cleanup after an error. */
-		  free (lang);
-		  free (outval);
-		  return rc;
-		}
-	      decoded = outval;
 	    }
-	  else
-	    memmove (decoded, data, strlen (data) + 1);
 	}
-    }
-  
-  if (pval)
-    *pval = decoded;
-  else
-    free (decoded);
-  
-  if (plang)
-    *plang = lang;
-  return 0;
-}
-
-/* Similar to mu_mimehdr_aget_param, but the returned value is decoded
-   according to the CHARSET.  Unless PLANG is NULL, it receives malloc'ed
-   language name from STR.  If there was no language name, *PLANG is set
-   to NULL. 
-*/
-int
-mu_mimehdr_aget_decoded_param (const char *str, const char *param,
-			       const char *charset, 
-			       char **pval, char **plang)
-{
-  char *value;
-  int rc;
-  int flags;
-  
-  rc = mu_mimehdr_aget_param (str, param, &value, &flags);
-  if (rc == 0)
-    {
-      rc = mu_mimehdr_decode_param (value, flags, charset, pval, plang);
-      free (value);
+      mu_assoc_destroy (&assoc);
     }
   return rc;
 }
   
-/* Get the attachment name from MSG.  See _header_get_param, for a
-   description of the rest of arguments. */
+/* Get the attachment name from a message.
+
+   Input:
+     MSG     - The input message.
+     CHARSET - Character set to recode output values to.  Can be NULL.
+   Output:
+     PBUF    - Output value.
+     PSZ     - Its size in bytes, not counting the terminating zero.
+     PLANG   - Language the name is written in, if provided in the header.
+
+   Either PSZ or PLAN (or both) can be NULL.
+*/
 static int
-_get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
-		      char **pbuf, size_t *sz, int *pflags)
+_get_attachment_name (mu_message_t msg, const char *charset,
+		      char **pbuf, size_t *psz, char **plang)
 {
   int ret = EINVAL;
   mu_header_t hdr;
   char *value = NULL;
+  mu_assoc_t assoc;
 
   if (!msg)
     return ret;
@@ -622,7 +829,8 @@ _get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
   if ((ret = mu_message_get_header (msg, &hdr)) != 0)
     return ret;
 
-  ret = mu_header_aget_value_unfold (hdr, "Content-Disposition", &value);
+  ret = mu_header_aget_value_unfold (hdr, MU_HEADER_CONTENT_DISPOSITION,
+				     &value);
 
   /* If the header wasn't there, we'll fall back to Content-Type, but
      other errors are fatal. */
@@ -631,33 +839,88 @@ _get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
 
   if (ret == 0 && value != NULL)
     {
-      ret = _header_get_param (value, "attachment",
-			       "filename", buf, bufsz, pbuf, sz, pflags);
-      free (value);
-      value = NULL;
-      if (ret == 0 || ret != MU_ERR_NOENT)
+      ret = mu_mime_param_assoc_create (&assoc);
+      if (ret)
 	return ret;
+      ret = mu_mime_param_assoc_add (assoc, "filename");
+      if (ret == 0)
+	{
+	  char *disp;
+	  
+	  ret = mu_mime_header_parse_subset (value, charset, &disp, assoc);
+	  if (ret == 0)
+	    {
+	      struct mu_mime_param *param;
+	      if (mu_c_strcasecmp (disp, "attachment") == 0 &&
+		  (param = mu_assoc_ref (assoc, "filename")))
+		{
+		  *pbuf = param->value;
+		  if (psz)
+		    *psz = strlen (*pbuf);
+		  param->value = NULL;
+		  if (plang)
+		    {
+		      *plang = param->lang;
+		      param->lang = NULL;
+		    }
+		}
+	      else
+		ret = MU_ERR_NOENT;
+	      free (disp);
+	      mu_assoc_destroy (&assoc);
+	    }
+	}
     }
 
+  free (value);
+
+  if (ret == 0)
+    return ret;
+
   /* If we didn't get the name, we fall back on the Content-Type name
      parameter. */
 
-  free (value);
-  ret = mu_header_aget_value_unfold (hdr, "Content-Type", &value);
+  ret = mu_header_aget_value_unfold (hdr, MU_HEADER_CONTENT_TYPE, &value);
   if (ret == 0)
-    ret = _header_get_param (value, NULL, "name", buf, bufsz, pbuf, sz,
-			     pflags);
-  free (value);
+    {
+      ret = mu_mime_param_assoc_create (&assoc);
+      if (ret)
+	return ret;
+      ret = mu_mime_param_assoc_add (assoc, "name");
+      if (ret == 0)
+	{
+	  ret = mu_mime_header_parse_subset (value, charset, NULL, assoc);
+	  if (ret == 0)
+	    {
+	      struct mu_mime_param *param;
+	      if ((param = mu_assoc_ref (assoc, "name")))
+		{
+		  *pbuf = param->value;
+		  if (psz)
+		    *psz = strlen (*pbuf);
+		  param->value = NULL;
+		  if (plang)
+		    {
+		      *plang = param->lang;
+		      param->lang = NULL;
+		    }
+		}
+	      else
+		ret = MU_ERR_NOENT;
+	    }
+	}
+      free (value);
+    }
 
   return ret;
 }
 
 int
-mu_message_aget_attachment_name (mu_message_t msg, char **name, int *pflags)
+mu_message_aget_attachment_name (mu_message_t msg, char **name)
 {
   if (name == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  return _get_attachment_name (msg, NULL, 0, name, NULL, pflags);
+  return _get_attachment_name (msg, NULL, name, NULL, NULL);
 }
 
 int
@@ -666,21 +929,28 @@ mu_message_aget_decoded_attachment_name (mu_message_t msg,
 					 char **pval,
 					 char **plang)
 {
-  char *value;
-  int flags;
-  int rc = mu_message_aget_attachment_name (msg, &value, &flags);
-  if (rc == 0)
-    {
-      rc = mu_mimehdr_decode_param (value, flags, charset, pval, plang);
-      free (value);
-    }
-  return rc;
+  if (pval == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  return _get_attachment_name (msg, charset, pval, NULL, plang);
 }
 
 int
 mu_message_get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
-				size_t *sz, int *pflags)
+				size_t *sz)
 {
-  return _get_attachment_name (msg, buf, bufsz, NULL, sz, pflags);
+  char *tmp;
+  size_t size;
+  int rc = _get_attachment_name (msg, NULL, &tmp, &size, NULL);
+  if (rc == 0)
+    {
+      if (size > bufsz)
+	size = bufsz;
+      if (buf)
+	size = mu_cpystr (buf, tmp, size);
+      if (sz)
+	*sz = size;
+    }
+  free (tmp);
+  return rc;
 }
 
diff --git a/libmailutils/tests/.gitignore b/libmailutils/tests/.gitignore
index 6a1188d..955a4e3 100644
--- a/libmailutils/tests/.gitignore
+++ b/libmailutils/tests/.gitignore
@@ -16,6 +16,7 @@ fsfolder
 imapio
 listop
 mailcap
+mimehdr
 prop
 scantime
 strftime
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index aca151a..8b97c86 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -51,6 +51,7 @@ noinst_PROGRAMS = \
  imapio\
  listop\
  mailcap\
+ mimehdr\
  prop\
  scantime\
  strftime\
@@ -88,6 +89,7 @@ TESTSUITE_AT = \
  linecon.at\
  list.at\
  mailcap.at\
+ mimehdr.at\
  prop.at\
  scantime.at\
  strftime.at\
diff --git a/libmailutils/tests/mimehdr.at b/libmailutils/tests/mimehdr.at
new file mode 100644
index 0000000..dbe8f9d
--- /dev/null
+++ b/libmailutils/tests/mimehdr.at
@@ -0,0 +1,131 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+# Warning: This text contains 8-bit UTF-8
+
+AT_BANNER(RFC 2231 header fields)
+
+dnl ---------------------------------------------------------------------
+dnl MIMEHDR([NAME], [KW], [OPT], [INPUT], [STDOUT = `'], [STDERR = `'])
+dnl
+m4_pushdef([MIMEHDR],[
+m4_pushdef([MU_TEST_GROUP],[mimehdr])
+m4_pushdef([MU_TEST_KEYWORDS],[mimehdr rfc2231])
+m4_pushdef([MU_TEST_COMMAND],[mimehdr $3])
+MU_GENERIC_TEST([$1],[$2],[$4],[],[$5],[$6])
+m4_popdef([MU_TEST_COMMAND])
+m4_popdef([MU_TEST_KEYWORDS])
+m4_popdef([MU_TEST_GROUP])
+])
+dnl ---------------------------------------------------------------------
+
+MIMEHDR([simple],[mimehdr00 mimehdr-simple],
+[],
+[message/external-body; access-type=URL;
+    URL="ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
+],
+[message/external-body
+access-type=URL
+URL=ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar
+])
+
+MIMEHDR([continuation],[mimehdr01 mimehdr-cont mimehdr-cont-00],
+[],
+[message/external-body; access-type=URL;
+         URL*0="ftp://";
+         URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
+],
+[message/external-body
+access-type=URL
+URL=ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar
+])
+
+MIMEHDR([charset (2047)],[mimehdr02 mimehdr-charset-rfc2047 mimehdr-charset-00],
+[],
+[attachment; charset=utf-8;
+  filename==?UTF-8?B?zrHPgc+HzrXOr86/IM6zzrnOsSDPhM63zr0gzrTOv866zrnOvM6xz4POr86x==?=
+],
+[attachment
+charset=utf-8
+filename=αρχείο για την δοκιμασία
+])
+
+MIMEHDR([charset with language (2047)],[mimehdr03 mimehdr-charset-rfc2047 mimehdr-charset-01],
+[],
+[attachment; charset=utf-8;
+  filename==?UTF-8*el?B?zrHPgc+HzrXOr86/IM6zzrnOsSDPhM63zr0gzrTOv866zrnOvM6xz4POr86x==?=
+],
+[attachment
+charset=utf-8
+filename(lang:el/UTF-8)=αρχείο για την δοκιμασία
+])
+
+MIMEHDR([no charset (2231)],[mimehdr04 mimehdr-no-charset-rfc2231 mimehdr-nocharset-00],
+[],
+[attachment; charset=utf-8;
+  filename*=%CE%B1%CF%81%CF%87%CE%B5%CE%AF%CE%BF%20%CE%B3%CE%B9%CE%B1%20%CF%84%CE%B7%CE%BD%20%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
+],
+[attachment
+charset=utf-8
+filename=αρχείο για την δοκιμασία
+])
+
+MIMEHDR([charset (2231)],[mimehdr05 mimehdr-charset-rfc2231 mimehdr-charset-rfc2231-00 mimehdr-charset-03],
+[],
+[attachment; charset=utf-8;
+  filename*=UTF-8''%CE%B1%CF%81%CF%87%CE%B5%CE%AF%CE%BF%20%CE%B3%CE%B9%CE%B1%20%CF%84%CE%B7%CE%BD%20%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
+],
+[attachment
+charset=utf-8
+filename(lang:/UTF-8)=αρχείο για την δοκιμασία
+])
+
+MIMEHDR([charset with language (2231)],[mimehdr06 mimehdr-charset-rfc2231 mimehdr-charset-rfc2231-01 mimehdr-charset-04],
+[],
+[attachment; charset=utf-8;
+  filename*=UTF-8'el_GR'%CE%B1%CF%81%CF%87%CE%B5%CE%AF%CE%BF%20%CE%B3%CE%B9%CE%B1%20%CF%84%CE%B7%CE%BD%20%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
+],
+[attachment
+charset=utf-8
+filename(lang:el_GR/UTF-8)=αρχείο για την δοκιμασία
+])
+
+MIMEHDR([charset with language and continuation (2231)],[mimehdr07 mimehdr-charset-rfc2231 mimehdr-charset-rfc2231-02 mimehdr-charset-05],
+[],
+[attachment; charset=utf-8;
+  filename*00*=UTF-8'el_GR'%CE%B1%CF%81%CF%87%CE%B5;
+  filename*01*=%CE%AF%CE%BF%20%CE%B3%CE;
+  filename*02*=%B9%CE%B1%20%CF%84%CE%B7;
+  filename*03*=%CE%BD%20%CE%B4%CE%BF%CE;
+  filename*04*=%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
+],
+[attachment
+charset=utf-8
+filename(lang:el_GR/UTF-8)=αρχείο για την δοκιμασία
+])
+
+MIMEHDR([combined charset, lang and cset],[mimehdr08 mimehdr-comb mimehdr-charset-rfc2231],
+[],
+[application/x-stuff
+    title*0*=us-ascii'en'This%20is%20even%20more%20
+    title*1*=%2A%2A%2Afun%2A%2A%2A%20
+    title*2="isn't it!"
+],
+[application/x-stuff
+title(lang:en/us-ascii)=This is even more ***fun*** isn't it!
+])
+
+m4_popdef([MIMEHDR])
diff --git a/libmailutils/tests/mimehdr.c b/libmailutils/tests/mimehdr.c
new file mode 100644
index 0000000..f02a252
--- /dev/null
+++ b/libmailutils/tests/mimehdr.c
@@ -0,0 +1,135 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2005, 2007, 2009, 2010, 2011 Free Software Foundation,
+   Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/assoc.h>
+#include <mailutils/header.h>
+#include <mailutils/message.h>
+#include <mailutils/mime.h>
+#include <mailutils/iterator.h>
+#include <mailutils/stream.h>
+#include <mailutils/stdstream.h>
+#include <mailutils/util.h>
+#include <mailutils/cstr.h>
+#include <mailutils/cctype.h>
+#include <mailutils/error.h>
+#include <mailutils/errno.h>
+
+struct named_param
+{
+  const char *name;
+  struct mu_mime_param const *param;
+};
+
+static int
+sort_names (void const *a, void const *b)
+{
+  struct named_param const *pa = a;
+  struct named_param const *pb = b;
+  return mu_c_strcasecmp (pa->name, pb->name);
+}
+
+static int
+print_named_param (void *item, void *data)
+{
+  struct named_param const *p = item;
+  struct mu_mime_param const *param = p->param;
+  
+  mu_printf ("%s", p->name);
+  if (param->lang)
+    mu_printf ("(lang:%s/%s)", param->lang, param->cset);
+  mu_printf ("=%s\n", param->value);
+  return 0;
+}
+
+int
+main (int argc, char **argv)
+{
+  int i;
+  mu_stream_t tmp;
+  mu_transport_t trans[2];
+  char *value;
+  mu_assoc_t assoc;
+  mu_iterator_t itr;
+  mu_list_t list;
+  char *charset = NULL;
+  
+  mu_set_program_name (argv[0]);
+  for (i = 1; i < argc; i++)
+    {
+      char *opt = argv[i];
+
+      if (strncmp (opt, "-debug=", 7) == 0)
+	mu_debug_parse_spec (opt + 7);
+      else if (strncmp (opt, "-charset=", 9) == 0)
+	charset = opt + 9;
+      else if (strcmp (opt, "-h") == 0 || strcmp (opt, "-help") == 0)
+	{
+	  mu_printf ("usage: %s [-charset=cs] [-debug=SPEC]", mu_program_name);
+	  return 0;
+	}
+      else
+	{
+	  mu_error ("unknown option %s", opt);
+	  return 1;
+	}
+    }
+
+  if (i != argc)
+    {
+      mu_error ("too many arguments");
+      return 1;
+    }
+	    
+  MU_ASSERT (mu_memory_stream_create (&tmp, MU_STREAM_RDWR));
+  MU_ASSERT (mu_stream_copy (tmp, mu_strin, 0, NULL));
+  MU_ASSERT (mu_stream_write (tmp, "", 1, NULL));
+  MU_ASSERT (mu_stream_ioctl (tmp, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET,
+			      trans));
+      
+  MU_ASSERT (mu_mime_header_parse ((char*)trans[0], charset, &value, &assoc));
+
+  mu_printf ("%s\n", value);
+  MU_ASSERT (mu_list_create (&list));
+  MU_ASSERT (mu_assoc_get_iterator (assoc, &itr));
+  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      const char *name;
+      struct mu_mime_param *p;
+      struct named_param *np;
+      
+      mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
+      np = malloc (sizeof (*np));
+      if (!np)
+	abort ();
+      np->name = name;
+      np->param = p;
+      MU_ASSERT (mu_list_append (list, np));
+    }
+  mu_iterator_destroy (&itr);
+
+  mu_list_sort (list, sort_names);
+  mu_list_foreach (list, print_named_param, NULL);
+  
+  return 0;
+}
+  
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 4c1b427..b9b889c 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -96,4 +96,6 @@ m4_include([imapio.at])
 m4_include([scantime.at])
 m4_include([strftime.at])
 
-m4_include([fsaf.at])
\ No newline at end of file
+m4_include([fsaf.at])
+
+m4_include([mimehdr.at])


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 20 18:32:48 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rd9Au-0003gK-6c
	for mharc-commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:32:48 -0500
Received: from eggs.gnu.org ([140.186.70.92]:60015)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd9Ar-0003gA-Ra
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:32:46 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd9Aq-0004iW-Mo
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:32:45 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:57554)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd9Aq-0004iS-K2
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 18:32:44 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rd9Aq-0001AY-8j
	for commit-mailutils@gnu.org; Tue, 20 Dec 2011 23:32:44 +0000
Date: Tue, 20 Dec 2011 23:32:44 +0000
Message-Id: <E1Rd9Aq-0001AY-8j@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-541-g7919e2d
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 3c5aed3965c70e6e1a894005b1adc7a407d0aae9
X-Git-Newrev: 7919e2d260bd7971d75126d544b352b749c3fa74
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 20 Dec 2011 23:32:46 -0000

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

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

The branch, master has been updated
       via  7919e2d260bd7971d75126d544b352b749c3fa74 (commit)
      from  3c5aed3965c70e6e1a894005b1adc7a407d0aae9 (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 7919e2d260bd7971d75126d544b352b749c3fa74
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 21 01:32:20 2011 +0200

    Bugfix.

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

Summary of changes:
 libmailutils/mime/mimehdr.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)

diff --git a/libmailutils/mime/mimehdr.c b/libmailutils/mime/mimehdr.c
index aedfac6..94389b1 100644
--- a/libmailutils/mime/mimehdr.c
+++ b/libmailutils/mime/mimehdr.c
@@ -56,13 +56,11 @@ _mu_mime_param_free (struct mu_mime_param *p)
 /* Treat ITEM as a pointer to struct mu_mime_param and reclaim all
    memory associated with it.
 
-   This is intended for use as a destroy_item method of assoc tables and
-   lists. */
+   This is intended for use as a destroy_item method of assoc tables. */
 static void
 _mu_mime_param_free_item (void *item)
 {
   _mu_mime_param_free (item);
-  free (item);
 }
 
 /* Recode a string between two charsets.


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Wed Dec 21 17:14:21 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RdUQX-0007QR-Gl
	for mharc-commit-mailutils@gnu.org; Wed, 21 Dec 2011 17:14:21 -0500
Received: from eggs.gnu.org ([140.186.70.92]:55094)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdUQT-0007PX-JZ
	for commit-mailutils@gnu.org; Wed, 21 Dec 2011 17:14:19 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdUQQ-00019p-Gw
	for commit-mailutils@gnu.org; Wed, 21 Dec 2011 17:14:17 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:44669)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdUQQ-00019l-Cz
	for commit-mailutils@gnu.org; Wed, 21 Dec 2011 17:14:14 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdUQO-0005Mv-7o
	for commit-mailutils@gnu.org; Wed, 21 Dec 2011 22:14:13 +0000
Date: Wed, 21 Dec 2011 22:14:12 +0000
Message-Id: <E1RdUQO-0005Mv-7o@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-543-g690b1bf
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 7919e2d260bd7971d75126d544b352b749c3fa74
X-Git-Newrev: 690b1bf8506e50cb85d2634ba2db2fddbff99dfe
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Wed, 21 Dec 2011 22:14:19 -0000

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

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

The branch, master has been updated
       via  690b1bf8506e50cb85d2634ba2db2fddbff99dfe (commit)
       via  924bc7276b9626615d7030fa565d0876bbfd9409 (commit)
      from  7919e2d260bd7971d75126d544b352b749c3fa74 (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 690b1bf8506e50cb85d2634ba2db2fddbff99dfe
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 21 17:40:17 2011 +0200

    Fix API for obtaining full email address.
    
    Old API relied on passing a pointer to the buffer string and its size,
    which is defective.  The new API, in addtion to that, provides functions
    for obtaining a pointer to the statically allocated original value, a
    pointer to dynamically allocated copy, and a function for formatting the
    address directly to a MU stream.
    
    Two functions are marked as deprecated: mu_address_to_string and
    mu_address_format_string.  Both assume a pointer to an allocated string
    of a fixed size, which is far from being convenient nor reliable enough.
    
    * include/mailutils/address.h (MU_ADDR_HINT_ADDR): Rename to
    MU_ADDR_HINT_PRINTABLE
    (mu_address)<addr>: Rename to printable.
    (mu_address_sget_printable,mu_address_aget_printable)
    (mu_address_get_printable): New protos.
    (mu_stream_format_address): New proto.
    (mu_address_to_string)
    (mu_address_format_string): Mark as deprecated.
    (mu_validate_email): New proto.
    * examples/mta.c: Use mu_address_sget_printable instead
    of (mu_address_to_string)
    * libmailutils/address/addrstream.c: New file.
    * libmailutils/address/Makefile.am (libaddress_la_SOURCES): Add
    addrstream.c
    * libmailutils/address/address.c (mu_address_format_string): Rewrite using
    streams.
    (mu_address_to_string): Rewrite as a wrapper over
    mu_address_get_printable.
    (mu_address_sget_printable,mu_address_aget_printable)
    (mu_address_get_printable): New functions.
    * libmailutils/mime/mimehdr.c (_mime_header_parse): Initialize rc.
    * mail/util.c (util_merge_addresses): Use mu_address_aget_printable.
    * mh/mh.h (mh_annotate): Both string args are const.
    * mh/mh_init.c (mh_annotate): Likewise.
    * mh/mh_format.c (builtin_formataddr): Use mu_address_sget_printable.
    * mh/mh_whatnow.c (anno_data)<field,value>: Both are consts.
    (annotate): Use mu_address_sget_printable.
    * mh/send.c (set_address_header): Use mu_address_sget_printable.
    * mu/imap.c (format_email): Use mu_stream_format_address.
    * python/libmu_py/address.c (api_address_to_string): Likewise.

commit 924bc7276b9626615d7030fa565d0876bbfd9409
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 21 17:35:45 2011 +0200

    imap4d: Fix fetch in case of message/rfc822
    
    This case requires full recursion.
    
    * imap4d/fetch.c (bodystructure): Call fetch_bodystructure0.

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

Summary of changes:
 examples/addr.c                                    |    4 +-
 examples/mta.c                                     |   17 +--
 imap4d/fetch.c                                     |    4 +-
 include/mailutils/address.h                        |   33 +++-
 libmailutils/address/Makefile.am                   |    1 +
 libmailutils/address/address.c                     |  209 +++++++++++---------
 .../mu_scm.h => libmailutils/address/addrstream.c  |   70 ++++---
 libmailutils/address/parse822.c                    |    6 +-
 libmailutils/base/assoc.c                          |    4 +-
 libmailutils/mime/mimehdr.c                        |    2 +-
 mail/util.c                                        |   23 +--
 mh/mh.h                                            |    3 +-
 mh/mh_format.c                                     |    9 +-
 mh/mh_init.c                                       |    2 +-
 mh/mh_whatnow.c                                    |   12 +-
 mh/send.c                                          |    9 +-
 mu/imap.c                                          |    7 +-
 python/libmu_py/address.c                          |    9 +-
 18 files changed, 224 insertions(+), 200 deletions(-)
 copy libmu_scm/mu_scm.h => libmailutils/address/addrstream.c (51%)

diff --git a/examples/addr.c b/examples/addr.c
index 9373f64..2928eac 100644
--- a/examples/addr.c
+++ b/examples/addr.c
@@ -112,8 +112,8 @@ addr_fieldptr_by_mask (mu_address_t addr, int mask)
 {
   switch (mask)						
     {
-    case MU_ADDR_HINT_ADDR:
-      return &addr->addr;
+    case MU_ADDR_HINT_PRINTABLE:
+      return &addr->printable;
 	  
     case MU_ADDR_HINT_COMMENTS:				
       return &addr->comments;					
diff --git a/examples/mta.c b/examples/mta.c
index c9572b9..4008f78 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -437,26 +437,17 @@ message_finalize (mu_message_t msg, int warn)
 
   if (finalize_option && !have_to)
     {
-      size_t n;
+      char const *sptr;
       int c;
       
-      c = mu_address_to_string (recipients, NULL, 0, &n);
+      c = mu_address_sget_printable (recipients, &sptr);
       if (c)
 	{
-	  mu_error ("%s: mu_address_to_string failure: %s",
+	  mu_error ("%s: mu_address_sget_printable failure: %s",
 		    progname, mu_strerror (c));
 	  return 1;
 	}
-      value = malloc (n + 1);
-      if (!value)
-	{
-	  mu_error ("%s: not enough memory", progname);
-	  return 1;
-	}
-
-      mu_address_to_string (recipients, value, n + 1, &n);
-      mu_header_set_value (header, MU_HEADER_TO, value, 1);
-      free (value);
+      mu_header_set_value (header, MU_HEADER_TO, sptr, 1);
     }
   return 0;
 }
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index d1698b6..fed2e3f 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -312,6 +312,8 @@ fetch_envelope0 (mu_message_t msg)
   return RESP_OK;
 }
 
+static int fetch_bodystructure0 (mu_message_t message, int extension);
+
 /* The basic fields of a non-multipart body part are in the following order:
    body type:
    A string giving the content media type name as defined in [MIME-IMB].
@@ -490,7 +492,7 @@ bodystructure (mu_message_t msg, int extension)
       io_sendf (")");
       /* Add body structure of the encapsulated message.  */
       io_sendf ("(");
-      bodystructure (emsg, extension);
+      fetch_bodystructure0 (emsg, extension);
       io_sendf (")");
       /* Size in text lines of the encapsulated message.  */
       mu_message_lines (emsg, &lines);
diff --git a/include/mailutils/address.h b/include/mailutils/address.h
index 96640be..45742ef 100644
--- a/include/mailutils/address.h
+++ b/include/mailutils/address.h
@@ -25,7 +25,7 @@
 extern "C" {
 #endif
 
-#define MU_ADDR_HINT_ADDR       0x0001 /* Not used yet */
+#define MU_ADDR_HINT_PRINTABLE  0x0001 /* Not used yet */
 #define MU_ADDR_HINT_COMMENTS   0x0002
 #define MU_ADDR_HINT_PERSONAL   0x0004
 #define MU_ADDR_HINT_EMAIL      0x0008
@@ -42,9 +42,11 @@ extern "C" {
  */
 struct mu_address
 {
-  char *addr;
-  	/* the original string that this list of addresses was created
-	 * from, only present at the head of the list */
+  char *printable;
+  	/* Printable representation of this address.  Normally, it is
+	 * the original string that this list of addresses was created
+	 * from, only present at the head of the list
+	 */
 
   char *comments;
   	/* the collection of comments stripped during parsing this MAILBOX */
@@ -129,8 +131,12 @@ extern int mu_address_aget_route
 
 extern int mu_address_is_group
 	(mu_address_t, size_t, int*);
-
-extern int mu_address_to_string (mu_address_t, char *, size_t, size_t *);
+  
+extern int mu_address_sget_printable (mu_address_t addr, const char **sptr);
+extern int mu_address_aget_printable (mu_address_t addr, char **presult);
+extern int mu_address_get_printable (mu_address_t addr, char *buf, size_t len,
+				     size_t *n);
+  
 extern int mu_address_get_count (mu_address_t, size_t *);
 extern int mu_address_get_group_count (mu_address_t, size_t *);
 extern int mu_address_get_email_count (mu_address_t, size_t *);
@@ -139,8 +145,21 @@ extern int mu_address_get_unix_mailbox_count (mu_address_t, size_t *);
 extern int mu_address_contains_email (mu_address_t addr, const char *email);
 extern int mu_address_union (mu_address_t *a, mu_address_t b);
   
-extern size_t mu_address_format_string (mu_address_t addr, char *buf, size_t buflen);
+extern int mu_stream_format_address (mu_stream_t str, mu_address_t addr);
+
+  /* Deprecated calls */
+  
+extern int mu_address_to_string (mu_address_t, char *, size_t, size_t *)
+  MU_DEPRECATED; /* Use mu_address_get_printable, if you really have to */
+  
+extern size_t mu_address_format_string (mu_address_t addr, char *buf,
+					size_t buflen)
+  MU_DEPRECATED; /* Use mu_stream_format_address, or any of the
+		    _get_printable functions */
 
+extern int mu_validate_email (mu_address_t subaddr);
+
+  
 #ifdef __cplusplus
 }
 #endif
diff --git a/libmailutils/address/Makefile.am b/libmailutils/address/Makefile.am
index bafd881..6890875 100644
--- a/libmailutils/address/Makefile.am
+++ b/libmailutils/address/Makefile.am
@@ -19,6 +19,7 @@ noinst_LTLIBRARIES = libaddress.la
 
 libaddress_la_SOURCES = \
  address.c\
+ addrstream.c\
  parse822.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
diff --git a/libmailutils/address/address.c b/libmailutils/address/address.c
index 80ddb14..19c33fa 100644
--- a/libmailutils/address/address.c
+++ b/libmailutils/address/address.c
@@ -35,6 +35,7 @@
 #include <mailutils/parse822.h>
 #include <mailutils/address.h>
 #include <mailutils/cstr.h>
+#include <mailutils/stream.h>
 
 int
 mu_address_create_null (mu_address_t *pa)
@@ -70,8 +71,8 @@ mu_address_create_hint (mu_address_t *a, const char *s, mu_address_t hint,
       if (!*a)
 	return MU_ERR_EMPTY_ADDRESS;
 
-      (*a)->addr = strdup (s);
-      if (!(*a)->addr)
+      (*a)->printable = strdup (s);
+      if (!(*a)->printable)
 	{
 	  mu_address_destroy (a);
 	  return ENOMEM;
@@ -159,8 +160,8 @@ mu_address_destroy (mu_address_t *paddress)
       mu_address_t current;
       for (; address; address = current)
 	{
-	  if (address->addr)
-	    free (address->addr);
+	  if (address->printable)
+	    free (address->printable);
 	  if (address->comments)
 	    free (address->comments);
 	  if (address->personal)
@@ -195,19 +196,19 @@ mu_address_concatenate (mu_address_t to, mu_address_t *from)
   *from = NULL;
 
   /* discard the current string cache as it is now inaccurate */
-  if (to->addr)
+  if (to->printable)
     {
-      free (to->addr);
-      to->addr = NULL;
+      free (to->printable);
+      to->printable = NULL;
     }
 
   to = to->next;
 
   /* only the first address must have a cache */
-  if (to->addr)
+  if (to->printable)
     {
-      free (to->addr);
-      to->addr = NULL;
+      free (to->printable);
+      to->printable = NULL;
     }
 
   return 0;
@@ -432,8 +433,8 @@ mu_address_set_email (mu_address_t addr, size_t no, const char *buf)
   return 0;
 }
 
-static int
-validate_email (mu_address_t subaddr)
+int
+mu_validate_email (mu_address_t subaddr)
 {
   if (!subaddr->email)
     {
@@ -473,82 +474,45 @@ mu_address_sget_email (mu_address_t addr, size_t no, char const **sptr)
   if (!subaddr)
     return MU_ERR_NOENT;
 
-  validate_email (subaddr);
+  mu_validate_email (subaddr);
   *sptr = subaddr->email;
   return 0;
 }
 
 DECL_GET(email)
 DECL_AGET(email)
-
-
-
 
-#define format_char(c) do {\
- if (buflen) \
-   {\
-      *buf++ = c;\
-      buflen--;\
-   }\
- else\
-   rc++;\
-} while(0)
-
-#define format_string(str) do {\
- if (buflen) \
-   {\
-      int n = snprintf (buf, buflen, "%s", str);\
-      buf += n;\
-      buflen -= n;\
-   }\
- else\
-   rc += strlen (str);\
-} while (0)
-
 size_t
 mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
 {
-  int rc = 0;
-  int comma = 0;
-
-  for (;addr; addr = addr->next)
+  mu_stream_t str;
+  int rc;
+  
+  if (!buf)
+    rc = mu_nullstream_create (&str, MU_STREAM_WRITE);
+  else
+    rc = mu_fixed_memory_stream_create (&str, buf, buflen, MU_STREAM_WRITE);
+  if (rc == 0)
     {
-      validate_email (addr);
-      if (addr->email)
+      size_t size;
+      
+      mu_stream_stat_buffer statbuf;
+      mu_stream_set_stat (str, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT),
+			  statbuf);
+      rc = mu_stream_format_address (str, addr);
+      mu_stream_destroy (&str);
+      if (rc)
+	return 0;
+      size = statbuf[MU_STREAM_STAT_OUT];
+      if (buf)
 	{
-	  int space = 0;
-
-	  if (comma)
-	    format_char (',');
-
-	  if (addr->personal)
-	    {
-	      format_char ('"');
-	      format_string (addr->personal);
-	      format_char ('"');
-	      space++;
-	    }
-
-	  if (addr->comments)
-	    {
-	      if (space)
-		format_char (' ');
-	      format_char ('(');
-	      format_string (addr->comments);
-	      format_char (')');
-	      space++;
-	    }
-
-	  if (space)
-	    format_char (' ');
-	  format_char ('<');
-	  format_string (addr->email);
-	  format_char ('>');
-	  comma++;
+	  if (size + 1 >= buflen)
+	    size = buflen - 1;
+	  buf[size] = 0;
 	}
+      return size;
     }
-  format_char (0);
-  return rc;
+  return 0;
 }
 
 static int
@@ -593,27 +557,86 @@ mu_address_is_group (mu_address_t addr, size_t no, int *yes)
 }
 
 int
-mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t *n)
+mu_address_sget_printable (mu_address_t addr, const char **sptr)
 {
-  size_t i;
   if (addr == NULL)
     return EINVAL;
-  if (buf)
-    *buf = '\0';
+  if (!sptr)
+    return MU_ERR_OUT_PTR_NULL;
+  if (!addr->printable)
+    {
+      mu_stream_t str;
+      int rc;
+      
+      rc = mu_memory_stream_create (&str, MU_STREAM_RDWR);
+      if (rc)
+	return rc;
+      rc = mu_stream_format_address (str, addr);
+      if (rc == 0)
+	{
+	  mu_off_t size;
+	  mu_stream_size (str, &size);
+	  addr->printable = malloc (size + 1);
+	  if (!addr->printable)
+	    rc = ENOMEM;
+	  else
+	    {
+	      mu_stream_seek (str, 0, MU_SEEK_SET, NULL);
+	      rc = mu_stream_read (str, addr->printable, size, NULL);
+	      addr->printable[size] = 0;
+	    }
+	}
+      mu_stream_destroy (&str);
+      if (rc)
+	return rc;
+    }
+  *sptr = addr->printable;
+  return 0;
+}
 
-  if (!addr->addr)
+int
+mu_address_aget_printable (mu_address_t addr, char **presult)
+{
+  int rc;
+  const char *s;
+
+  if (addr == NULL)
+    return EINVAL;
+  if (!presult)
+    return MU_ERR_OUT_PTR_NULL;
+  rc = mu_address_sget_printable (addr, &s);
+  if (rc == 0)
     {
-      i = mu_address_format_string (addr, NULL, 0);
-      addr->addr = malloc (i + 1);
-      if (!addr->addr)
-	return ENOMEM;
-      mu_address_format_string (addr, addr->addr, i+1);
+      char *result = strdup (s);
+      if (result)
+	*presult = result;
+      else
+	rc = ENOMEM;
     }
+  return rc;
+}
 
-  i = mu_cpystr (buf, addr->addr, len);
-  if (n)
-    *n = i;
-  return 0;
+int
+mu_address_get_printable (mu_address_t addr, char *buf, size_t len, size_t *n)
+{
+  const char *s;
+  int rc;
+  
+  rc = mu_address_sget_printable (addr, &s);
+  if (rc == 0)
+    {
+      size_t i;
+      i = mu_cpystr (buf, addr->printable, len);
+      if (n)
+	*n = i;
+    }
+  return rc;
+}
+
+int
+mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t *n)
+{
+  return mu_address_get_printable (addr, buf, len, n);
 }
 
 int
@@ -687,8 +710,8 @@ mu_address_dup (mu_address_t src)
     return NULL;
 
   /* FIXME: How about:
-    if (src->addr)
-      dst->addr = strdup (src->addr);
+    if (src->printable)
+      dst->printable = strdup (src->printable);
     ?
   */
   if (src->comments)
@@ -725,10 +748,10 @@ mu_address_union (mu_address_t *a, mu_address_t b)
     }
   else
     {
-      if ((*a)->addr)
+      if ((*a)->printable)
 	{
-	  free ((*a)->addr);
-	  (*a)->addr = NULL;
+	  free ((*a)->printable);
+	  (*a)->printable = NULL;
 	}
       for (last = *a; last->next; last = last->next)
 	;
diff --git a/libmu_scm/mu_scm.h b/libmailutils/address/addrstream.c
similarity index 51%
copy from libmu_scm/mu_scm.h
copy to libmailutils/address/addrstream.c
index de946c4..44fe7c8 100644
--- a/libmu_scm/mu_scm.h
+++ b/libmailutils/address/addrstream.c
@@ -1,5 +1,5 @@
 /* GNU Mailutils -- a suite of utilities for electronic mail
-   Copyright (C) 1999, 2000, 2001, 2002, 2006, 2007, 2009, 2010, 2011
+   Copyright (C) 1999, 2000, 2001, 2005, 2006, 2007, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
    This library is free software; you can redistribute it and/or
@@ -16,39 +16,47 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#if defined(HAVE_CONFIG_H)
+#ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
-
 #include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <string.h>
-#include <confpaths.h>
-
-#include <mailutils/mailbox.h>
-#include <mailutils/message.h>
-#include <mailutils/header.h>
-#include <mailutils/body.h>
-#include <mailutils/registrar.h>
-#include <mailutils/error.h>
-#include <mailutils/errno.h>
 #include <mailutils/address.h>
-#include <mailutils/registrar.h>
-#include <mailutils/util.h>
-#include <mailutils/datetime.h>
 #include <mailutils/stream.h>
-#include <mailutils/debug.h>
-#include <mailutils/attribute.h>
-#include <mailutils/mailer.h>
-#include <mailutils/envelope.h>
-#include <mailutils/url.h>
-#include <mailutils/mime.h>
-#include <mailutils/registrar.h>
-#include <mailutils/mu_auth.h>
-#include <mailutils/cstr.h>
-
-#include <mailutils/guile.h>
 
+int
+mu_stream_format_address (mu_stream_t str, mu_address_t addr)
+{
+  int comma = 0;
+
+  for (;addr; addr = addr->next)
+    {
+      mu_validate_email (addr);
+      if (addr->email)
+	{
+	  int space = 0;
+
+	  if (comma)
+	    mu_stream_write (str, ",", 1, NULL);
+
+	  if (addr->personal)
+	    {
+	      mu_stream_printf (str, "\"%s\"", addr->personal);
+	      space++;
+	    }
+
+	  if (addr->comments)
+	    {
+	      if (space)
+		mu_stream_write (str, " ", 1, NULL);
+	      mu_stream_printf (str, "(%s)", addr->comments);
+	      space++;
+	    }
+
+	  if (space)
+	    mu_stream_write (str, " ", 1, NULL);
+	  mu_stream_printf (str, "<%s>", addr->email);
+	  comma++;
+	}
+    }
+  return mu_stream_err (str) ? mu_stream_last_error (str) : 0;
+}
diff --git a/libmailutils/address/parse822.c b/libmailutils/address/parse822.c
index 526afd1..bea2615 100644
--- a/libmailutils/address/parse822.c
+++ b/libmailutils/address/parse822.c
@@ -713,8 +713,8 @@ addr_field_by_mask (mu_address_t addr, int mask)
 {
   switch (mask)						
     {
-    case MU_ADDR_HINT_ADDR:
-      return addr->addr;
+    case MU_ADDR_HINT_PRINTABLE:
+      return addr->printable;
 	  
     case MU_ADDR_HINT_COMMENTS:				
       return addr->comments;					
@@ -758,7 +758,7 @@ addr_free_fields (mu_address_t a, int memflag)
 {
   char *p;
   
-  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_ADDR)))
+  if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_PRINTABLE)))
     free (p);
   if ((p = addr_field_by_mask (a, memflag & MU_ADDR_HINT_COMMENTS)))
     free (p);
diff --git a/libmailutils/base/assoc.c b/libmailutils/base/assoc.c
index 2d21fa8..519bb2d 100644
--- a/libmailutils/base/assoc.c
+++ b/libmailutils/base/assoc.c
@@ -172,8 +172,8 @@ assoc_remove (mu_assoc_t assoc, struct _mu_assoc_elem *elem)
       while ((j < r && r <= i) || (i < j && j < r) || (r <= i && i < j));
 
       if (j != i)
-	      memcpy (ASSOC_ELEM (assoc, j), ASSOC_ELEM (assoc, i),
-		      assoc->elsize);
+	memcpy (ASSOC_ELEM (assoc, j), ASSOC_ELEM (assoc, i),
+		assoc->elsize);
     }
   return 0;
 }
diff --git a/libmailutils/mime/mimehdr.c b/libmailutils/mime/mimehdr.c
index 94389b1..96dd109 100644
--- a/libmailutils/mime/mimehdr.c
+++ b/libmailutils/mime/mimehdr.c
@@ -287,7 +287,7 @@ static int
 _mime_header_parse (const char *text, char **pvalue,
 		    mu_assoc_t assoc, const char *outcharset, int subset)
 {
-  int rc;
+  int rc = 0;
   struct mu_wordsplit ws;
   struct param_continuation cont;
   size_t i;
diff --git a/mail/util.c b/mail/util.c
index 747723c..fb1cf12 100644
--- a/mail/util.c
+++ b/mail/util.c
@@ -861,17 +861,13 @@ util_merge_addresses (char **addr_str, const char *value)
   rc = mu_address_union (&addr, new_addr);
   if (rc == 0)
     {
-      size_t n;
-
-      rc = mu_address_to_string (addr, NULL, 0, &n);
+      char *val;
+      
+      rc = mu_address_aget_printable (addr, &val);
       if (rc == 0)
 	{
 	  free (*addr_str);
-	  *addr_str = malloc (n + 1);
-	  if (!*addr_str)
-	    rc = ENOMEM;
-	  else
-	    mu_address_to_string (addr, *addr_str, n + 1, &n);
+	  *addr_str = val;
 	}
     }
 
@@ -972,15 +968,12 @@ util_header_expand (mu_header_t *phdr)
 	  
 	  if (addr)
 	    {
-	      char *newvalue;
-	      size_t n = 0;
+	      const char *newvalue;
 	      
-	      mu_address_to_string (addr, NULL, 0, &n);
-	      newvalue = xmalloc (n + 1);
-	      mu_address_to_string (addr, newvalue, n + 1, NULL);
+	      rc = mu_address_sget_printable (addr, &newvalue);
+	      if (rc == 0)
+		mu_header_set_value (hdr, name, newvalue, 1);
 	      mu_address_destroy (&addr);
-	      mu_header_set_value (hdr, name, newvalue, 1);
-	      free (newvalue);
 	    }
 	}
       else
diff --git a/mh/mh.h b/mh/mh.h
index 0d883b9..2e0f812 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -358,7 +358,8 @@ typedef int (*mh_alias_enumerator_t) (char *alias, mu_list_t names, void *data);
 void mh_alias_enumerate (mh_alias_enumerator_t fun, void *data);
 
 
-void mh_annotate (mu_message_t msg, char *field, char *text, int date);
+void mh_annotate (mu_message_t msg, const char *field, const char *text,
+		  int date);
 
 #define MHL_DECODE       1
 #define MHL_CLEARSCREEN  2
diff --git a/mh/mh_format.c b/mh/mh_format.c
index 78bc3ed..1c5c77d 100644
--- a/mh/mh_format.c
+++ b/mh/mh_format.c
@@ -1801,7 +1801,6 @@ static void
 builtin_formataddr (struct mh_machine *mach)
 {
   mu_address_t addr, dest;
-  size_t size;
   int i;
   size_t num;
   const char *buf;
@@ -1837,12 +1836,12 @@ builtin_formataddr (struct mh_machine *mach)
 	}
     }
 
-  if (mu_address_to_string (dest, NULL, 0, &size) == 0)
+  if (mu_address_sget_printable (dest, &buf) == 0)
     {
-      strobj_realloc (&mach->reg_str, size + 1);
-      mu_address_to_string (dest, strobj_ptr (&mach->reg_str), size + 1, NULL);
-      mu_address_destroy (&dest);
+      strobj_realloc (&mach->reg_str, strlen (buf) + 1);
+      strcpy (strobj_ptr (&mach->reg_str), buf);
     }
+  mu_address_destroy (&dest);
 }
 
 /*      putaddr    literal        print str address list with
diff --git a/mh/mh_init.c b/mh/mh_init.c
index 75976f2..fc1934a 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -852,7 +852,7 @@ mh_install (char *name, int automode)
 }
         
 void
-mh_annotate (mu_message_t msg, char *field, char *text, int date)
+mh_annotate (mu_message_t msg, const char *field, const char *text, int date)
 {
   mu_header_t hdr;
   mu_attribute_t attr;
diff --git a/mh/mh_whatnow.c b/mh/mh_whatnow.c
index 4b4b210..08b66fc 100644
--- a/mh/mh_whatnow.c
+++ b/mh/mh_whatnow.c
@@ -264,8 +264,8 @@ invoke (const char *compname, const char *defval, int argc, char **argv,
 
 struct anno_data
 {
-  char *field;
-  char *value;
+  const char *field;
+  const char *value;
   int date;
 };
 
@@ -299,16 +299,12 @@ annotate (struct mh_whatnow_env *wh)
 
       if (mu_address_get_nth (addr, i, &subaddr) == 0)
 	{
-	  size_t size;
 	  struct anno_data d;
 
-	  mu_address_to_string (subaddr, NULL, 0, &size);
-	  d.value = xmalloc (size + 1);
 	  d.field = wh->anno_field;
 	  d.date = i == 1;
-	  mu_address_to_string (subaddr, d.value, size + 1, NULL);
-	  mu_list_foreach (wh->anno_list, anno, &d);
-	  free (d.value);
+	  if (mu_address_sget_printable (subaddr, &d.value) == 0)
+	    mu_list_foreach (wh->anno_list, anno, &d);
 	  mu_address_destroy (&subaddr);
 	}
     }
diff --git a/mh/send.c b/mh/send.c
index 1f433b9..479e9cc 100644
--- a/mh/send.c
+++ b/mh/send.c
@@ -475,11 +475,10 @@ get_sender_personal ()
 static void
 set_address_header (mu_header_t hdr, char *name, mu_address_t addr)
 {
-  size_t s = mu_address_format_string (addr, NULL, 0);
-  char *value = xmalloc (s + 1);
-  mu_address_format_string (addr, value, s);
-  mu_header_set_value (hdr, name, value, 1);
-  free (value);
+  const char *value;
+  if (mu_address_sget_printable (addr, &value) == 0)
+    mu_header_set_value (hdr, name, value, 1);
+  /* FIXME: Error reporting */
 }
 
 void
diff --git a/mu/imap.c b/mu/imap.c
index 85b0f60..cf49b4b 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -196,12 +196,7 @@ format_email (mu_stream_t str, const char *name, mu_address_t addr)
   if (!addr)
     mu_stream_printf (str, "NIL");
   else
-    {
-      size_t sz = mu_address_format_string (addr, NULL, 0);
-      char *buf = xmalloc (sz + 1);
-      mu_address_format_string (addr, buf, sz);
-      mu_stream_write (str, buf, strlen (buf), NULL);
-    }
+    mu_stream_format_address (str, addr);
   mu_stream_printf (str, "\n");
 }
 
diff --git a/python/libmu_py/address.c b/python/libmu_py/address.c
index 2d3d7d6..b943838 100644
--- a/python/libmu_py/address.c
+++ b/python/libmu_py/address.c
@@ -267,17 +267,14 @@ static PyObject *
 api_address_to_string (PyObject *self, PyObject *args)
 {
   int status;
-  size_t n;
-  char buf[256];
+  char const *sptr;
   PyAddress *py_addr;
 
-  memset (buf, 0, sizeof (buf));
-
   if (!PyArg_ParseTuple (args, "O!", &PyAddressType, &py_addr))
     return NULL;
 
-  status = mu_address_to_string (py_addr->addr, buf, sizeof (buf), &n);
-  return status_object (status, PyString_FromString (buf));
+  status = mu_address_sget_printable (py_addr->addr, &sptr);
+  return status_object (status, PyString_FromString (sptr));
 }
 
 static PyMethodDef methods[] = {


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 22 06:07:19 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RdgUZ-0003Rq-T5
	for mharc-commit-mailutils@gnu.org; Thu, 22 Dec 2011 06:07:19 -0500
Received: from eggs.gnu.org ([140.186.70.92]:34042)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdgUO-0003Pi-6G
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 06:07:16 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdgUG-0000ec-PW
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 06:07:08 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:33350)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdgUG-0000eQ-M7
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 06:07:00 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdgUF-0005fn-FE
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 11:07:00 +0000
Date: Thu, 22 Dec 2011 11:06:59 +0000
Message-Id: <E1RdgUF-0005fn-FE@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-544-ge218f1e
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 690b1bf8506e50cb85d2634ba2db2fddbff99dfe
X-Git-Newrev: e218f1ec00749701a00c074c4574a75a6f156d2b
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 22 Dec 2011 11:07:16 -0000

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

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

The branch, master has been updated
       via  e218f1ec00749701a00c074c4574a75a6f156d2b (commit)
      from  690b1bf8506e50cb85d2634ba2db2fddbff99dfe (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 e218f1ec00749701a00c074c4574a75a6f156d2b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 22 12:31:25 2011 +0200

    Implement imap (client) bodystructure and envelope.
    
    * libmailutils/string/wordsplit.c (alloc_space): Fix reallocation
    calculations.
    
    * imap4d/fetch.c (bodystructure): Output number of lines for any
    TEXT part, not only TEXT/PLAIN.
    
    * include/mailutils/header.h (MU_HEADER_CONTENT_LOCATION): New define.
    * include/mailutils/imap.h (mu_imap_fetch_bodystructure): Implement.
    (mu_imap_fetch_envelope): Replace data fields with a single pointer
    to struct mu_imapenvelope.
    * include/mailutils/message.h: Include datetime.h
    (mu_imapenvelope, mu_bodystructure): New structs.
    (mu_message_type): New type.
    (mu_message_get_imapenvelope,mu_message_imapenvelope_free)
    (mu_message_set_imapenvelope)
    (mu_bodystructure_free,mu_list_free_bodystructure)
    (mu_message_get_bodystructure)
    (mu_message_set_bodystructure): New protos.
    * include/mailutils/mime.h (mu_mime_param_assoc_create)
    (mu_mime_param_assoc_add): New protos.
    * include/mailutils/sys/message.h (_mu_message)<_imapenvelope>
    <_bodystructure>: New methods.
    * libmailutils/mailbox/bodystruct.c: New file.
    * libmailutils/mailbox/imapenv.c: New file.
    * libmailutils/mailbox/Makefile.am (libmailbox_la_SOURCES): Add new
    sources.
    
    * libproto/imap/fetch.c: Implement bodystructure.
    * mu/imap.c: Likewise.
    
    * testsuite/bs.c: New file.
    * testsuite/Makefile.am (noinst_PROGRAMS): Add bs.

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

Summary of changes:
 imap4d/fetch.c                    |   14 +-
 include/mailutils/header.h        |    1 +
 include/mailutils/imap.h          |   14 +-
 include/mailutils/message.h       |   75 ++++++-
 include/mailutils/mime.h          |    3 +
 include/mailutils/sys/message.h   |    2 +
 libmailutils/mailbox/Makefile.am  |    2 +
 libmailutils/mailbox/bodystruct.c |  325 ++++++++++++++++++++++++
 libmailutils/mailbox/imapenv.c    |  138 ++++++++++
 libmailutils/string/wordsplit.c   |    2 +-
 libproto/imap/fetch.c             |  498 ++++++++++++++++++++++++++++++++++---
 libproto/imap/resplist.c          |    9 +
 mu/imap.c                         |  168 ++++++++++++-
 testsuite/Makefile.am             |    1 +
 testsuite/bs.c                    |  169 +++++++++++++
 15 files changed, 1353 insertions(+), 68 deletions(-)
 create mode 100644 libmailutils/mailbox/bodystruct.c
 create mode 100644 libmailutils/mailbox/imapenv.c
 create mode 100644 testsuite/bs.c

diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index fed2e3f..ad42ff3 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -372,8 +372,9 @@ bodystructure (mu_message_t msg, int extension)
   if (mu_header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
     {
       struct mu_wordsplit ws;
-      char *s, *p;
-	  
+      char *p;
+      size_t len;
+      
       ws.ws_delim = " \t\r\n;=";
       ws.ws_alloc_die = imap4d_ws_alloc_die;
       if (mu_wordsplit (buffer, &ws, IMAP4D_WS_FLAGS))
@@ -383,18 +384,17 @@ bodystructure (mu_message_t msg, int extension)
 	  return RESP_BAD; /* FIXME: a better error handling, maybe? */
 	}
 
+      len = strcspn (ws.ws_wordv[0], "/");
       if (mu_c_strcasecmp (ws.ws_wordv[0], "MESSAGE/RFC822") == 0)
         message_rfc822 = 1;
-      else if (mu_c_strcasecmp (ws.ws_wordv[0], "TEXT/PLAIN") == 0)
+      else if (mu_c_strncasecmp (ws.ws_wordv[0], "TEXT", len) == 0)
         text_plain = 1;
 
-      s = strchr (ws.ws_wordv[0], '/');
-      if (s)
-	*s++ = 0;
+      ws.ws_wordv[0][len++] = 0;
       p = ws.ws_wordv[0];
       io_send_qstring (p);
       io_sendf (" ");
-      io_send_qstring (s);
+      io_send_qstring (ws.ws_wordv[0] + len);
 
       /* body parameter parenthesized list: Content-type attributes */
       if (ws.ws_wordc > 1)
diff --git a/include/mailutils/header.h b/include/mailutils/header.h
index ed13ab3..4117a29 100644
--- a/include/mailutils/header.h
+++ b/include/mailutils/header.h
@@ -59,6 +59,7 @@ extern "C" {
 #define MU_HEADER_CONTENT_DESCRIPTION       "Content-Description"
 #define MU_HEADER_CONTENT_DISPOSITION       "Content-Disposition"
 #define MU_HEADER_CONTENT_MD5               "Content-MD5"
+#define MU_HEADER_CONTENT_LOCATION          "Content-Location"   
 #define MU_HEADER_MIME_VERSION              "MIME-Version"
 #define MU_HEADER_X_MAILER                  "X-Mailer"
 #define MU_HEADER_X_UIDL                    "X-UIDL"
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 63a5eae..b6f9e57 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -236,23 +236,13 @@ struct mu_imap_fetch_body
 struct mu_imap_fetch_bodystructure
 {
   int type;
-  //FIXME?
+  struct mu_bodystructure *bs;
 };
   
 struct mu_imap_fetch_envelope
 {
   int type;
-  struct tm date;
-  struct mu_timezone tz;
-  char *subject;
-  mu_address_t from;
-  mu_address_t sender;
-  mu_address_t reply_to;
-  mu_address_t to;
-  mu_address_t cc;
-  mu_address_t bcc;
-  char *in_reply_to;
-  char *message_id;
+  struct mu_imapenvelope *imapenvelope;
 };
 
 struct mu_imap_fetch_flags
diff --git a/include/mailutils/message.h b/include/mailutils/message.h
index ced7901..d1a31ec 100644
--- a/include/mailutils/message.h
+++ b/include/mailutils/message.h
@@ -20,6 +20,7 @@
 #define _MAILUTILS_MESSAGE_H
 
 #include <mailutils/types.h>
+#include <mailutils/datetime.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -28,6 +29,64 @@ extern "C" {
 #define MU_SCAN_SEEK  0x01
 #define MU_SCAN_SIZE  0x02
 
+struct mu_imapenvelope
+{
+  struct tm date;
+  struct mu_timezone tz;
+  char *subject;
+  mu_address_t from;
+  mu_address_t sender;
+  mu_address_t reply_to;
+  mu_address_t to;
+  mu_address_t cc;
+  mu_address_t bcc;
+  char *in_reply_to;
+  char *message_id;
+};
+
+enum mu_message_type
+  {
+    mu_message_other,
+    mu_message_text,     /* text/plain */
+    mu_message_rfc822,   /* message/rfc822 */
+    mu_message_multipart /* multipart/mixed */
+  };
+  
+struct mu_bodystructure
+{
+  enum mu_message_type body_message_type;
+  char *body_type;
+  char *body_subtype;
+  mu_assoc_t body_param;
+  char *body_id;
+  char *body_descr;
+  char *body_encoding;
+  size_t body_size;
+  /* Optional */
+  char *body_md5;
+  char *body_disposition;
+  mu_assoc_t body_disp_param;
+  char *body_language;
+  char *body_location;
+  union
+  {
+    struct
+    {
+      size_t body_lines;
+    } text;
+    struct 
+    {
+      struct mu_imapenvelope *body_env;
+      struct mu_bodystructure *body_struct;
+      size_t body_lines;
+    } rfc822;
+    struct
+    {
+      mu_list_t body_parts;
+    } multipart;
+  } v;
+};
+
 struct mu_message_scan
 {
   int flags;
@@ -137,6 +196,21 @@ extern int mu_message_set_qid (mu_message_t,
 			       int (*_get_qid) (mu_message_t,
 						mu_message_qid_t *),
 			       void *owner);
+
+extern int mu_message_get_imapenvelope (mu_message_t, struct mu_imapenvelope **);
+extern void mu_message_imapenvelope_free (struct mu_imapenvelope *);
+extern int mu_message_set_imapenvelope (mu_message_t,
+   int (*_imapenvelope) (mu_message_t, struct mu_imapenvelope **),
+   void *owner);
+
+extern void mu_bodystructure_free (struct mu_bodystructure *);
+extern void mu_list_free_bodystructure (void *item);
+
+extern int mu_message_get_bodystructure (mu_message_t,
+					 struct mu_bodystructure **);
+extern int mu_message_set_bodystructure (mu_message_t msg,
+      int (*_bodystructure) (mu_message_t, struct mu_bodystructure **),
+      void *owner);
   
 /* misc functions */
 extern int mu_message_create_attachment (const char *content_type,
@@ -190,7 +264,6 @@ extern int mu_message_from_stream_with_envelope (mu_message_t *pmsg,
 						 mu_envelope_t env);
 extern int mu_stream_to_message (mu_stream_t instream, mu_message_t *pmsg);
 
-  
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/mailutils/mime.h b/include/mailutils/mime.h
index 9c3d473..48989f1 100644
--- a/include/mailutils/mime.h
+++ b/include/mailutils/mime.h
@@ -66,6 +66,9 @@ int mu_base64_decode    (const unsigned char *input, size_t input_len,
 			 unsigned char **output, size_t * output_len);
 
 
+int mu_mime_param_assoc_create (mu_assoc_t *passoc);
+int mu_mime_param_assoc_add (mu_assoc_t assoc, const char *name);
+  
 int mu_mime_header_parse (const char *text, char *charset, char **pvalue,
 			  mu_assoc_t *paramtab);
 int mu_mime_header_parse_subset (const char *text, const char *charset,
diff --git a/include/mailutils/sys/message.h b/include/mailutils/sys/message.h
index f538fc9..6259dbe 100644
--- a/include/mailutils/sys/message.h
+++ b/include/mailutils/sys/message.h
@@ -61,6 +61,8 @@ struct _mu_message
   int (*_get_qid)        (mu_message_t,	mu_message_qid_t *);
   int (*_get_num_parts)  (mu_message_t, size_t *);
   int (*_get_part)       (mu_message_t, size_t, mu_message_t *);
+  int (*_imapenvelope)   (mu_message_t, struct mu_imapenvelope **);
+  int (*_bodystructure)  (mu_message_t, struct mu_bodystructure **);
   int (*_is_multipart)   (mu_message_t, int *);
   int (*_lines)          (mu_message_t, size_t *, int);
   int (*_size)           (mu_message_t, size_t *);
diff --git a/libmailutils/mailbox/Makefile.am b/libmailutils/mailbox/Makefile.am
index cb9e775..661a922 100644
--- a/libmailutils/mailbox/Makefile.am
+++ b/libmailutils/mailbox/Makefile.am
@@ -23,12 +23,14 @@ libmailbox_la_SOURCES =  \
  mbxitr.c\
  attribute.c\
  body.c\
+ bodystruct.c\
  envelope.c\
  folder.c\
  fsfolder.c\
  hdrfirst.c\
  hdritr.c\
  header.c\
+ imapenv.c\
  msgcpy.c\
  msgattr.c\
  msgbody.c\
diff --git a/libmailutils/mailbox/bodystruct.c b/libmailutils/mailbox/bodystruct.c
new file mode 100644
index 0000000..c99cbbe
--- /dev/null
+++ b/libmailutils/mailbox/bodystruct.c
@@ -0,0 +1,325 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/assoc.h>
+#include <mailutils/list.h>
+#include <mailutils/message.h>
+#include <mailutils/mime.h>
+#include <mailutils/header.h>
+#include <mailutils/sys/message.h>
+#include <mailutils/errno.h>
+#include <mailutils/debug.h>
+#include <mailutils/nls.h>
+#include <mailutils/cstr.h>
+#include <mailutils/body.h>
+
+void
+mu_list_free_bodystructure (void *item)
+{
+  mu_bodystructure_free (item);
+}
+
+void
+mu_bodystructure_free (struct mu_bodystructure *bs)
+{
+  if (!bs)
+    return;
+  free (bs->body_type);
+  free (bs->body_subtype);
+  mu_assoc_destroy (&bs->body_param);
+  free (bs->body_id);
+  free (bs->body_descr);
+  free (bs->body_encoding);
+  free (bs->body_md5);
+  free (bs->body_disposition);
+  mu_assoc_destroy (&bs->body_disp_param);
+  free (bs->body_language);
+  free (bs->body_location);
+  switch (bs->body_message_type)
+    {
+    case mu_message_other:
+    case mu_message_text:
+      break;
+      
+    case mu_message_rfc822:
+      mu_message_imapenvelope_free (bs->v.rfc822.body_env);
+      mu_bodystructure_free (bs->v.rfc822.body_struct);
+      break;
+      
+    case mu_message_multipart:
+      mu_list_destroy (&bs->v.multipart.body_parts);
+    }
+
+  free (bs);
+}
+
+static int bodystructure_fill (mu_message_t msg,
+			       struct mu_bodystructure *bs);
+
+static int
+bodystructure_init (mu_message_t msg, struct mu_bodystructure **pbs)
+{
+  int rc;
+  struct mu_bodystructure *bs = calloc (1, sizeof (*bs));
+  if (!bs)
+    return ENOMEM;
+  rc = bodystructure_fill (msg, bs);
+  if (rc)
+    mu_bodystructure_free (bs);
+  else
+    *pbs = bs;
+  return rc;
+}
+
+static int
+bodystructure_fill (mu_message_t msg, struct mu_bodystructure *bs)
+{
+  mu_header_t header = NULL;
+  const char *buffer = NULL;
+  mu_body_t body = NULL;
+  int rc;
+  int is_multipart = 0;
+
+  rc = mu_message_get_header (msg, &header);
+  if (rc)
+    return rc;
+  
+  if (mu_header_sget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
+    {
+      char *value;
+      char *p;
+      size_t len;
+      
+      rc = mu_mime_header_parse (buffer, "UTF-8", &value, &bs->body_param);
+      if (rc)
+	return rc;
+
+      len = strcspn (value, "/");
+
+      if (mu_c_strcasecmp (value, "MESSAGE/RFC822") == 0)
+        bs->body_message_type = mu_message_rfc822;
+      else if (mu_c_strncasecmp (value, "TEXT", len) == 0)
+        bs->body_message_type = mu_message_text;
+
+      p = malloc (len + 1);
+      if (!p)
+	return ENOMEM;
+      memcpy (p, value, len);
+      p[len] = 0;
+      
+      bs->body_type = p;
+      mu_strupper (bs->body_type);
+      if (value[len])
+	{
+	  bs->body_subtype = strdup (value + len + 1);
+	  if (!bs->body_subtype)
+	    return ENOMEM;
+	  mu_strupper (bs->body_subtype);
+	}
+      
+      /* body parameter parenthesized list: Content-type attributes */
+
+      rc = mu_message_is_multipart (msg, &is_multipart);
+      if (rc)
+	return rc;
+      if (is_multipart)
+	bs->body_message_type = mu_message_multipart;
+    }
+  else
+    {
+      struct mu_mime_param param;
+      
+      /* Default? If Content-Type is not present consider as text/plain.  */
+      bs->body_type = strdup ("TEXT");
+      if (!bs->body_type)
+	return ENOMEM;
+      bs->body_subtype = strdup ("PLAIN");
+      if (!bs->body_subtype)
+	return ENOMEM;
+      rc = mu_mime_param_assoc_create (&bs->body_param);
+      if (rc)
+	return rc;
+      memset (&param, 0, sizeof (param));
+      param.value = strdup ("US-ASCII");
+      rc = mu_assoc_install (bs->body_param, "CHARSET", &param);
+      if (rc)
+	{
+	  free (param.value);
+	  return rc;
+	}
+      bs->body_message_type = mu_message_text;
+    }
+
+  if (is_multipart)
+    {
+      size_t i, nparts;
+
+      rc = mu_message_get_num_parts (msg, &nparts);
+      if (rc)
+	return rc;
+
+      rc = mu_list_create (&bs->v.multipart.body_parts);
+      if (rc)
+	return rc;
+
+      mu_list_set_destroy_item (bs->v.multipart.body_parts,
+				mu_list_free_bodystructure);
+      
+      for (i = 1; i <= nparts; i++)
+        {
+          mu_message_t partmsg;
+	  struct mu_bodystructure *partbs;
+
+	  rc = mu_message_get_part (msg, i, &partmsg);
+	  if (rc)
+	    return rc;
+
+	  rc = bodystructure_init (partmsg, &partbs);
+	  if (rc)
+	    return rc;
+
+	  rc = mu_list_append (bs->v.multipart.body_parts, partbs);
+	  if (rc)
+	    {
+	      mu_bodystructure_free (partbs);
+	      return rc;
+	    }
+	}
+    }
+  else
+    {
+      /* body id: Content-ID. */
+      rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_ID,
+					&bs->body_id);
+      if (rc && rc != MU_ERR_NOENT)
+	return rc;
+      /* body description: Content-Description. */
+      rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_DESCRIPTION,
+					&bs->body_descr);
+      if (rc && rc != MU_ERR_NOENT)
+	return rc;
+      
+      /* body encoding: Content-Transfer-Encoding. */
+      rc = mu_header_aget_value_unfold (header,
+					MU_HEADER_CONTENT_TRANSFER_ENCODING,
+					&bs->body_encoding);
+      if (rc == MU_ERR_NOENT)
+	{
+	  bs->body_encoding = strdup ("7BIT");
+	  if (!bs->body_encoding)
+	    return ENOMEM;
+	}
+      else if (rc)
+	return rc;
+
+      /* body size RFC822 format.  */
+      rc = mu_message_get_body (msg, &body);
+      if (rc)
+	return rc;
+      rc = mu_body_size (body, &bs->body_size);
+      if (rc)
+	return rc;
+      
+      /* If the mime type was text.  */
+      if (bs->body_message_type == mu_message_text)
+	{
+	  rc = mu_body_lines (body, &bs->v.text.body_lines);
+	  if (rc)
+	    return rc;
+	}
+      else if (bs->body_message_type == mu_message_rfc822)
+	{
+	  mu_message_t emsg = NULL;
+
+	  /* Add envelope structure of the encapsulated message.  */
+	  rc = mu_message_unencapsulate  (msg, &emsg, NULL);
+	  if (rc)
+	    return rc;
+	  rc = mu_message_get_imapenvelope (emsg, &bs->v.rfc822.body_env);
+	  if (rc)
+	    return rc;
+	  /* Add body structure of the encapsulated message.  */
+	  rc = bodystructure_init (emsg, &bs->v.rfc822.body_struct);
+	  if (rc)
+	    return rc;
+	  /* Size in text lines of the encapsulated message.  */
+	  rc = mu_message_lines (emsg, &bs->v.rfc822.body_lines);
+	  mu_message_destroy (&emsg, NULL);
+	}
+    }
+  
+  /* body MD5: Content-MD5.  */
+  rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_MD5,
+				    &bs->body_md5);
+  if (rc && rc != MU_ERR_NOENT)
+    return rc;
+  
+  /* body disposition: Content-Disposition.  */
+  rc = mu_header_sget_value (header, MU_HEADER_CONTENT_DISPOSITION,
+			     &buffer);
+  if (rc == 0)
+    {
+      rc = mu_mime_header_parse (buffer, "UTF-8", &bs->body_disposition,
+				 &bs->body_disp_param);
+      if (rc)
+	return rc;
+    }
+  else if (rc != MU_ERR_NOENT)
+    return rc;
+  /* body language: Content-Language.  */
+  rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_LANGUAGE,
+				    &bs->body_language);
+  if (rc && rc != MU_ERR_NOENT)
+    return rc;
+  rc = mu_header_aget_value_unfold (header, MU_HEADER_CONTENT_LOCATION,
+				    &bs->body_location);
+  if (rc && rc != MU_ERR_NOENT)
+    return rc;
+
+  return 0;
+}
+
+int
+mu_message_get_bodystructure (mu_message_t msg,
+			      struct mu_bodystructure **pbs)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (pbs == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (msg->_bodystructure)
+    return msg->_bodystructure (msg, pbs);
+  return bodystructure_init (msg, pbs);
+}
+
+int
+mu_message_set_bodystructure (mu_message_t msg,
+      int (*_bodystructure) (mu_message_t, struct mu_bodystructure **),
+      void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_bodystructure = _bodystructure;
+  return 0;
+}
diff --git a/libmailutils/mailbox/imapenv.c b/libmailutils/mailbox/imapenv.c
new file mode 100644
index 0000000..efd49b9
--- /dev/null
+++ b/libmailutils/mailbox/imapenv.c
@@ -0,0 +1,138 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/header.h>
+#include <mailutils/envelope.h>
+#include <mailutils/address.h>
+#include <mailutils/message.h>
+#include <mailutils/sys/message.h>
+#include <mailutils/errno.h>
+
+void
+mu_message_imapenvelope_free (struct mu_imapenvelope *env)
+{
+  if (!env)
+    return;
+  free (env->subject);
+  mu_address_destroy (&env->from);
+  mu_address_destroy (&env->sender);
+  mu_address_destroy (&env->reply_to);
+  mu_address_destroy (&env->to);
+  mu_address_destroy (&env->cc);
+  mu_address_destroy (&env->bcc);
+  free (env->in_reply_to);
+  free (env->message_id);
+  free (env);
+}
+
+int
+mu_message_get_imapenvelope (mu_message_t msg, struct mu_imapenvelope **pimapenvelope)
+{
+  struct mu_imapenvelope *imapenvelope;
+  int rc;
+  
+  if (msg == NULL)
+    return EINVAL;
+  if (imapenvelope == NULL)
+    return MU_ERR_OUT_PTR_NULL;
+  if (msg->_imapenvelope)
+    return msg->_imapenvelope (msg, pimapenvelope);
+
+  imapenvelope = calloc (1, sizeof (imapenvelope[0]));
+  if (!imapenvelope)
+    return ENOMEM;
+  do
+    {
+      mu_header_t hdr;
+      mu_envelope_t env;
+      const char *s;
+
+      if ((rc = mu_message_get_envelope (msg, &env)))
+	break;
+      if ((rc = mu_envelope_sget_date (env, &s)))
+	break;
+      if ((rc = mu_scan_datetime (s, MU_DATETIME_FROM,
+				  &imapenvelope->date, &imapenvelope->tz,
+				  NULL)))
+	break;
+
+      if ((rc = mu_message_get_header (msg, &hdr)))
+	break;
+
+      rc = mu_header_get_address (hdr, MU_HEADER_FROM, &imapenvelope->from);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+      
+      rc = mu_header_get_address (hdr, MU_HEADER_SENDER, &imapenvelope->sender);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+
+      rc = mu_header_get_address (hdr, MU_HEADER_REPLY_TO, &imapenvelope->reply_to);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+
+      rc = mu_header_get_address (hdr, MU_HEADER_TO, &imapenvelope->to);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+
+      rc = mu_header_get_address (hdr, MU_HEADER_CC, &imapenvelope->cc);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+
+      rc = mu_header_get_address (hdr, MU_HEADER_BCC, &imapenvelope->bcc);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+      
+      rc = mu_header_aget_value_unfold (hdr, MU_HEADER_SUBJECT,
+					&imapenvelope->subject);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+
+      rc = mu_header_aget_value_unfold (hdr, MU_HEADER_IN_REPLY_TO,
+					&imapenvelope->in_reply_to);
+      if (rc && rc != MU_ERR_NOENT)
+	break;
+
+      rc = mu_header_aget_value_unfold (hdr, MU_HEADER_MESSAGE_ID,
+					&imapenvelope->message_id);
+    }
+  while (0);
+  if (rc)
+    mu_message_imapenvelope_free (imapenvelope);
+  else
+    *pimapenvelope = imapenvelope;
+  return rc;
+}
+
+int
+mu_message_set_imapenvelope (mu_message_t msg,
+			int (*_imapenvelope) (mu_message_t,
+					 struct mu_imapenvelope **),
+			void *owner)
+{
+  if (msg == NULL)
+    return EINVAL;
+  if (msg->owner != owner)
+    return EACCES;
+  msg->_imapenvelope = _imapenvelope;
+  return 0;
+}
diff --git a/libmailutils/string/wordsplit.c b/libmailutils/string/wordsplit.c
index 12af725..0acf2a3 100644
--- a/libmailutils/string/wordsplit.c
+++ b/libmailutils/string/wordsplit.c
@@ -184,7 +184,7 @@ alloc_space (struct mu_wordsplit *wsp, size_t count)
   else if (wsp->ws_wordn < offs + wsp->ws_wordc + count)
     {
       newalloc = offs + wsp->ws_wordc +
-	count > ALLOC_INCR ? count : ALLOC_INCR;
+	(count > ALLOC_INCR ? count : ALLOC_INCR);
       ptr = realloc (wsp->ws_wordv, newalloc * sizeof (ptr[0]));
     }
   else
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index b1994ae..e448041 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -25,6 +25,9 @@
 #include <mailutils/address.h>
 #include <mailutils/cstr.h>
 #include <mailutils/cctype.h>
+#include <mailutils/message.h>
+#include <mailutils/mime.h>
+#include <mailutils/assoc.h>
 #include <mailutils/imap.h>
 #include <mailutils/sys/imap.h>
 
@@ -67,19 +70,11 @@ _free_fetch_response (void *ptr)
       break;
       
     case MU_IMAP_FETCH_BODYSTRUCTURE:
-      /* FIXME */
+      mu_bodystructure_free (resp->bodystructure.bs);
       break;
       
     case MU_IMAP_FETCH_ENVELOPE:
-      free (resp->envelope.subject);
-      mu_address_destroy (&resp->envelope.from);
-      mu_address_destroy (&resp->envelope.sender);
-      mu_address_destroy (&resp->envelope.reply_to);
-      mu_address_destroy (&resp->envelope.to);
-      mu_address_destroy (&resp->envelope.cc);
-      mu_address_destroy (&resp->envelope.bcc);
-      free (resp->envelope.in_reply_to);
-      free (resp->envelope.message_id);
+      mu_message_imapenvelope_free (resp->envelope.imapenvelope);
       break;
       
     case MU_IMAP_FETCH_FLAGS:
@@ -228,8 +223,9 @@ _body_mapper (union mu_imap_fetch_response *resp,
       partv = calloc (partc, sizeof (partv[0]));
       for (i = 0, p = section; i < partc; i++)
 	{
-	  partv[i] = strtoul (p, &p, 10);
-	  p++;
+	  char *q;
+	  partv[i] = strtoul (p, &q, 10);
+	  p = q + 1;
 	}
     }
 
@@ -327,12 +323,438 @@ _date_mapper (union mu_imap_fetch_response *resp,
   return 0;
 }
 
-/* FIXME */
-#define _bodystructure_mapper NULL
+static int parse_bodystructure (struct imap_list_element *elt,
+				struct mu_bodystructure **pbs);
+
+struct body_field_map
+{
+  size_t offset; /* Offset of the target member of mu_bodystructure */
+  int (*mapper) (struct imap_list_element *, void *);
+};
+
+static int
+parse_bs_list (struct imap_list_element *elt,
+	       struct mu_bodystructure *bs,
+	       struct body_field_map *map)
+{
+  int rc;
+  mu_iterator_t itr;
+
+  rc = mu_list_get_iterator (elt->v.list, &itr);
+  if (rc)
+    return rc;
+  for (mu_iterator_first (itr);
+       map->mapper && !mu_iterator_is_done (itr);
+       mu_iterator_next (itr), map++)
+    {
+      struct imap_list_element *tok;
+      mu_iterator_current (itr, (void**)&tok);
+      rc = map->mapper (tok, (char*)bs + map->offset);
+      if (rc)
+	break;
+    }
+  mu_iterator_destroy (&itr);
+  return rc;
+}
+
+static int
+_map_body_param (void **itmv, size_t itmc, void *call_data)
+{
+  mu_assoc_t assoc = call_data;
+  struct mu_mime_param param;
+  struct imap_list_element *key, *val;
+  int rc;
+
+  if (itmc != 2)
+    return MU_ERR_PARSE;
+
+  key = itmv[0];
+  val = itmv[1];
+  if (key->type != imap_eltype_string || val->type != imap_eltype_string)
+    return MU_ERR_PARSE;
+  
+  rc = mu_rfc2047_decode_param ("UTF-8", val->v.string, &param);
+  if (rc)
+    {
+      param.lang = param.cset = NULL;
+      param.value = strdup (val->v.string);
+      if (!param.value)
+	return ENOMEM;
+    }
+  return mu_assoc_install (assoc, key->v.string, &param);
+}
+
+static int
+_body_field_text_mapper (struct imap_list_element *tok, void *ptr)
+{
+  char *s;
+  
+  if (_mu_imap_list_element_is_nil (tok))
+    s = NULL;
+  else if (tok->type != imap_eltype_string)
+    return MU_ERR_PARSE;
+  else if (!(s = strdup (tok->v.string)))
+    return ENOMEM;
+  *(char**) ptr = s;
+  return 0;
+}
+
+static int
+_body_field_size_mapper (struct imap_list_element *tok, void *ptr)
+{
+  unsigned long n;
+  
+  if (_mu_imap_list_element_is_nil (tok))
+    n = 0;
+  else if (tok->type != imap_eltype_string)
+    return MU_ERR_PARSE;
+  else
+    {
+      char *s;
+
+      errno = 0;
+      n = strtoul (tok->v.string, &s, 10);
+      if (*s || errno)
+	return MU_ERR_PARSE;
+    }
+  *(size_t*) ptr = n;
+  return 0;
+}
+
+static int
+_body_field_param_mapper (struct imap_list_element *tok, void *ptr)
+{
+  mu_assoc_t param;
+  int rc = mu_mime_param_assoc_create (&param);
+  if (rc)
+    return rc;
+  *(mu_assoc_t*) ptr = param;
+  if (_mu_imap_list_element_is_nil (tok))
+    return 0;
+  if (tok->type != imap_eltype_list)
+    return MU_ERR_PARSE;
+  return mu_list_gmap (tok->v.list, _map_body_param, 2, param);
+}  
+
+static int
+_body_field_disposition_mapper (struct imap_list_element *tok, void *ptr)
+{
+  int rc;
+  struct mu_bodystructure *bs = ptr;
+  struct imap_list_element *elt;
+  
+  if (_mu_imap_list_element_is_nil (tok))
+    return 0;
+  if (tok->type != imap_eltype_list)
+    return MU_ERR_PARSE;
+  elt = _mu_imap_list_at (tok->v.list, 0);
+  if (_mu_imap_list_element_is_nil (elt))
+    bs->body_disposition = NULL;
+  else if (elt->type != imap_eltype_string)
+    return MU_ERR_PARSE;
+  else if ((bs->body_disposition = strdup (elt->v.string)) == NULL)
+    return ENOMEM;
+
+  rc = mu_mime_param_assoc_create (&bs->body_disp_param);
+  if (rc)
+    return rc;
+  
+  elt = _mu_imap_list_at (tok->v.list, 1);
+  if (_mu_imap_list_element_is_nil (elt))
+    return 0;
+  else if (elt->type != imap_eltype_list)
+    return MU_ERR_PARSE;
+  return mu_list_gmap (elt->v.list, _map_body_param, 2, bs->body_disp_param);
+}
+
+static int parse_envelope (struct imap_list_element *elt,
+			   struct mu_imapenvelope **penv);
+
+static int
+_body_field_imapenvelope_mapper (struct imap_list_element *tok, void *ptr)
+{
+  return parse_envelope (tok, ptr);
+}
+
+static int
+_body_field_bodystructure_mapper (struct imap_list_element *tok, void *ptr)
+{
+  return parse_bodystructure (tok, ptr);
+}
+
+/* Simple text or message/rfc822 body.
+
+   Sample TEXT body structure:
+
+   ("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff")
+    "<960723163407.20117h@cac.washington.edu>" "Compiler diff"
+    "BASE64" 4554 73)
+
+    Elements:
+
+    0        "TEXT"            body_type
+    1        "PLAIN"           body_subtype
+    2        (...)             body_param
+    3        "<9607...>"       body_id
+    4        "Compiler diff"   body_descr
+    5        "BASE64"          body_encoding
+    6        4554              body_size
+    7        73                v.text.body_lines
+    [Optional]
+    8                          body_md5
+    9                          body_disposition;
+    10                         body_language;
+    11                         body_location;
+*/
+
+struct body_field_map base_field_map[] = {
+  { mu_offsetof (struct mu_bodystructure, body_type),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_subtype),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_param),
+    _body_field_param_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_id),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_descr),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_encoding),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_size),
+    _body_field_size_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_md5),
+    _body_field_text_mapper },
+  { 0, _body_field_disposition_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_language),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_location),
+    _body_field_text_mapper },
+  { 0, NULL }
+};
+
+struct body_field_map text_field_map[] = {
+  { mu_offsetof (struct mu_bodystructure, body_type),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_subtype),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_param),
+    _body_field_param_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_id),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_descr),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_encoding),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_size),
+    _body_field_size_mapper },
+  { mu_offsetof (struct mu_bodystructure, v.text.body_lines),
+    _body_field_size_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_md5),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_disposition),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_language),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_location),
+    _body_field_text_mapper },
+  { 0, NULL }
+};
+
+struct body_field_map message_field_map[] = {
+  { mu_offsetof (struct mu_bodystructure, body_type),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_subtype),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_param),
+    _body_field_param_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_id),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_descr),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_encoding),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_size),
+    _body_field_size_mapper },
+  { mu_offsetof (struct mu_bodystructure, v.rfc822.body_env),
+    _body_field_imapenvelope_mapper },
+  { mu_offsetof (struct mu_bodystructure, v.rfc822.body_struct),
+    _body_field_bodystructure_mapper },
+  { mu_offsetof (struct mu_bodystructure, v.rfc822.body_lines),
+    _body_field_size_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_md5),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_disposition),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_language),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_location),
+    _body_field_text_mapper },
+  { 0, NULL }
+};
+
+struct body_field_map multipart_field_map[] = {
+  /* Body type is processed separately */
+  { mu_offsetof (struct mu_bodystructure, body_subtype),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_param),
+    _body_field_param_mapper },
+  { 0, _body_field_disposition_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_language),
+    _body_field_text_mapper },
+  { mu_offsetof (struct mu_bodystructure, body_location),
+    _body_field_text_mapper },
+  { 0, NULL }
+};
+
+#define BSTOK_BODY_TYPE    0
+#define BSTOK_BODY_SUBTYPE 1
+
+static int
+_parse_bodystructure_simple (struct imap_list_element *elt,
+			     struct mu_bodystructure *bs)
+{
+  size_t n;
+  struct imap_list_element *tok, *subtype;
+  struct body_field_map *map;
+  
+  mu_list_count (elt->v.list, &n);
+  if (n < 8)
+    return MU_ERR_PARSE;
+
+  tok = _mu_imap_list_at (elt->v.list, BSTOK_BODY_TYPE);
+  if (!tok || tok->type != imap_eltype_string)
+    return MU_ERR_PARSE;
+  subtype = _mu_imap_list_at (elt->v.list, BSTOK_BODY_SUBTYPE);
+  if (!subtype || subtype->type != imap_eltype_string)
+    return MU_ERR_PARSE;
+  
+  if (mu_c_strcasecmp (tok->v.string, "TEXT") == 0)
+    {
+      bs->body_message_type = mu_message_text;
+      map = text_field_map;
+    }
+  else if (mu_c_strcasecmp (tok->v.string, "MESSAGE") == 0 &&
+	   mu_c_strcasecmp (subtype->v.string, "RFC822") == 0)
+    {
+      bs->body_message_type = mu_message_rfc822;
+      map = message_field_map;
+    }
+  else
+    {
+      bs->body_message_type = mu_message_other;
+      map = base_field_map;
+    }
+  
+  return parse_bs_list (elt, bs, map);
+}
+
+/* Example multipart:
+        (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152
+         23)
+	 ("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff")
+         "<960723163407.20117h@cac.washington.edu>" "Compiler diff"
+         "BASE64" 4554 73)
+	 "MIXED")
+
+*/
+static int
+_parse_bodystructure_mixed (struct imap_list_element *elt,
+			    struct mu_bodystructure *bs)
+{
+  int rc;
+  struct imap_list_element *tok;
+  mu_iterator_t itr;
+  struct body_field_map *map = multipart_field_map;
+  
+  bs->body_message_type = mu_message_multipart;
+  if (!(bs->body_type = strdup ("MULTIPART")))
+    return ENOMEM;
+
+  rc = mu_list_create (&bs->v.multipart.body_parts);
+  if (rc)
+    return rc;
+
+  mu_list_set_destroy_item (bs->v.multipart.body_parts,
+			    mu_list_free_bodystructure);
+
+  rc = mu_list_get_iterator (elt->v.list, &itr);
+  if (rc)
+    return rc;
+  for (mu_iterator_first (itr);
+       !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      struct mu_bodystructure *bspart;
+
+      mu_iterator_current (itr, (void**) &tok);
+      if (!tok)
+	return MU_ERR_PARSE;
+      if (tok->type != imap_eltype_list)
+	break;
+      rc = parse_bodystructure (tok, &bspart);
+      if (rc)
+	return rc;
+      rc = mu_list_append (bs->v.multipart.body_parts, bspart);
+      if (rc)
+	{
+	  mu_bodystructure_free (bspart);
+	  return rc;
+	}
+    }
+
+  if (mu_iterator_is_done (itr))
+    return MU_ERR_PARSE;
+  
+  for (; map->mapper && !mu_iterator_is_done (itr);
+       mu_iterator_next (itr), map++)
+    {
+      struct imap_list_element *tok;
+      mu_iterator_current (itr, (void**)&tok);
+      rc = map->mapper (tok, (char*)bs + map->offset);
+      if (rc)
+	return rc;
+    }
+  mu_iterator_destroy (&itr);
+  return 0;
+}
+
+static int
+parse_bodystructure (struct imap_list_element *elt,
+		     struct mu_bodystructure **pbs)
+{
+  int rc;
+  struct mu_bodystructure *bs;
+  struct imap_list_element *tok;
+  
+  if (elt->type != imap_eltype_list)
+    return MU_ERR_FAILURE;
+  bs = calloc (1, sizeof (*bs));
+  if (!bs)
+    return ENOMEM;
+  tok = _mu_imap_list_at (elt->v.list, 0);
+  if (tok->type == imap_eltype_string)
+    rc = _parse_bodystructure_simple (elt, bs);
+  else
+    rc = _parse_bodystructure_mixed (elt, bs);
+
+  if (rc)
+    mu_bodystructure_free (bs);
+  else
+    *pbs = bs;
+  return rc;
+}
+
+static int
+_bodystructure_mapper (union mu_imap_fetch_response *resp,
+		       struct imap_list_element *elt,
+		       struct parse_response_env *parse_env)
+{
+  return parse_bodystructure (elt, &resp->bodystructure.bs);
+}
 
 struct fill_env
 {
-  struct mu_imap_fetch_envelope *envelope;
+  struct mu_imapenvelope *imapenvelope;
   size_t n;
 };
 
@@ -435,7 +857,8 @@ _fill_response (void *item, void *data)
   int rc;
   struct imap_list_element *elt = item;
   struct fill_env *env = data;
-
+  struct mu_imapenvelope *imapenvelope = env->imapenvelope;
+  
   switch (env->n++)
     {
     case env_date:
@@ -445,8 +868,8 @@ _fill_response (void *item, void *data)
 	{
 	  if (mu_scan_datetime (elt->v.string,
 				MU_DATETIME_SCAN_RFC822,
-				&env->envelope->date,
-				&env->envelope->tz, NULL))
+				&imapenvelope->date,
+				&imapenvelope->tz, NULL))
 	    rc = MU_ERR_FAILURE;
 	  else
 	    rc = 0;
@@ -454,58 +877,67 @@ _fill_response (void *item, void *data)
       break;
       
     case env_subject:
-      rc = elt_to_string (elt, &env->envelope->subject);
+      rc = elt_to_string (elt, &imapenvelope->subject);
       break;
 
     case env_from:
-      rc = elt_to_address (elt, &env->envelope->from);
+      rc = elt_to_address (elt, &imapenvelope->from);
       break;
 	
     case env_sender:
-      rc = elt_to_address (elt, &env->envelope->sender);
+      rc = elt_to_address (elt, &imapenvelope->sender);
       break;
       
     case env_reply_to:
-      rc = elt_to_address (elt, &env->envelope->reply_to);
+      rc = elt_to_address (elt, &imapenvelope->reply_to);
       break;
       
     case env_to:
-      rc = elt_to_address (elt, &env->envelope->to);
+      rc = elt_to_address (elt, &imapenvelope->to);
       break;
       
     case env_cc:
-      rc = elt_to_address (elt, &env->envelope->cc);
+      rc = elt_to_address (elt, &imapenvelope->cc);
       break;
       
     case env_bcc:
-      rc = elt_to_address (elt, &env->envelope->bcc);
+      rc = elt_to_address (elt, &imapenvelope->bcc);
       break;
       
     case env_in_reply_to:
-      rc = elt_to_string (elt, &env->envelope->in_reply_to);
+      rc = elt_to_string (elt, &imapenvelope->in_reply_to);
       break;
       
     case env_message_id:
-      rc = elt_to_string (elt, &env->envelope->message_id);
+      rc = elt_to_string (elt, &imapenvelope->message_id);
       break;
     }
   return rc;
 }
-  
+
 static int
-_envelope_mapper (union mu_imap_fetch_response *resp,
-		  struct imap_list_element *elt,
-		  struct parse_response_env *parse_env)
+parse_envelope (struct imap_list_element *elt, struct mu_imapenvelope **penv)
 {
   struct fill_env env;
   
   if (elt->type != imap_eltype_list)
     return MU_ERR_FAILURE;
-  env.envelope = &resp->envelope;
+  env.imapenvelope = calloc (1, sizeof (*env.imapenvelope));
+  if (!env.imapenvelope)
+    return ENOMEM;
   env.n = 0;
   mu_list_foreach (elt->v.list, _fill_response, &env);
+  *penv = env.imapenvelope;
   return 0;
 }
+
+static int
+_envelope_mapper (union mu_imap_fetch_response *resp,
+		  struct imap_list_element *elt,
+		  struct parse_response_env *parse_env)
+{
+  return parse_envelope (elt, &resp->envelope.imapenvelope);
+}
 
 struct mapper_tab
 {
@@ -517,7 +949,7 @@ struct mapper_tab
   
 static struct mapper_tab mapper_tab[] = {
 #define S(s) s, (sizeof (s) - 1)
-  { S("BODYSTRUCTURE"), },
+  { S("BODYSTRUCTURE"), MU_IMAP_FETCH_BODYSTRUCTURE, _bodystructure_mapper },
   { S("BODY"),          MU_IMAP_FETCH_BODY,        _body_mapper },
   { S("ENVELOPE"),      MU_IMAP_FETCH_ENVELOPE,    _envelope_mapper },
   { S("FLAGS"),         MU_IMAP_FETCH_FLAGS,       _flags_mapper },
diff --git a/libproto/imap/resplist.c b/libproto/imap/resplist.c
index 65d78dc..2f9dc80 100644
--- a/libproto/imap/resplist.c
+++ b/libproto/imap/resplist.c
@@ -297,4 +297,13 @@ _mu_imap_list_nth_element_is_string (mu_list_t list, size_t n,
 	 strcmp (elt->v.string, str) == 0;
 }
 
+int
+_mu_imap_list_nth_element_is_string_ci (mu_list_t list, size_t n,
+				     	const char *str)
+{
+  struct imap_list_element *elt = _mu_imap_list_at (list, n);
+  return elt && elt->type == imap_eltype_string &&
+	 mu_c_strcasecmp (elt->v.string, str) == 0;
+}
+
 
diff --git a/mu/imap.c b/mu/imap.c
index cf49b4b..9e6e387 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -225,7 +225,143 @@ format_date (mu_stream_t str, char *name,
     }
   mu_stream_printf (str, "\n");
 }
+
+#define S(str) ((str) ? (str) : "")
+
+static void
+print_param (mu_stream_t ostr, const char *prefix, mu_assoc_t assoc,
+	     int indent)
+{
+  mu_iterator_t itr;
+  int i;
+
+  mu_stream_printf (ostr, "%*s%s:\n", indent, "", prefix);
+  indent += 4;
+  if (mu_assoc_get_iterator (assoc, &itr))
+    return;
+  for (i = 0, mu_iterator_first (itr);
+       !mu_iterator_is_done (itr);
+       i++, mu_iterator_next (itr))
+    {
+      const char *name;
+      struct mu_mime_param *p;
+      
+      mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
+      mu_stream_printf (ostr, "%*s%d: %s=%s\n", indent, "", i, name, p->value);
+    }
+  mu_iterator_destroy (&itr);
+}
+
+struct print_data
+{
+  mu_stream_t ostr;
+  int num;
+  int level;
+};
+
+static void print_bs (mu_stream_t ostr,
+		      struct mu_bodystructure *bs, int level);
+
+static int
+print_item (void *item, void *data)
+{
+  struct mu_bodystructure *bs = item;
+  struct print_data *pd = data;
+  mu_stream_printf (pd->ostr, "%*sPart #%d\n", (pd->level-1) << 2, "",
+		    pd->num);
+  print_bs (pd->ostr, bs, pd->level);
+  ++pd->num;
+  return 0;
+}
+
+static void
+print_address (mu_stream_t ostr, const char *title, mu_address_t addr,
+	       int indent)
+{
+  mu_stream_printf (ostr, "%*s%s: ", indent, "", title);
+  mu_stream_format_address (mu_strout, addr);
+  mu_stream_printf (ostr, "\n");
+}
+
+static void
+print_imapenvelope (mu_stream_t ostr, struct mu_imapenvelope *env, int level)
+{
+  int indent = (level << 2);
+
+  mu_stream_printf (ostr, "%*sEnvelope:\n", indent, "");
+  indent += 4;
+  mu_stream_printf (ostr, "%*sTime: ", indent, "");
+  mu_c_streamftime (mu_strout, "%c%n", &env->date, &env->tz);
+  mu_stream_printf (ostr, "%*sSubject: %s\n", indent, "", S(env->subject));
+  print_address (ostr, "From", env->from, indent);
+  print_address (ostr, "Sender", env->sender, indent);
+  print_address (ostr, "Reply-to", env->reply_to, indent);
+  print_address (ostr, "To", env->to, indent);
+  print_address (ostr, "Cc", env->cc, indent);
+  print_address (ostr, "Bcc", env->bcc, indent);
+  mu_stream_printf (ostr, "%*sIn-Reply-To: %s\n", indent, "",
+		    S(env->in_reply_to));
+  mu_stream_printf (ostr, "%*sMessage-ID: %s\n", indent, "",
+		    S(env->message_id));
+}
 
+static void
+print_bs (mu_stream_t ostr, struct mu_bodystructure *bs, int level)
+{
+  int indent = level << 2;
+  mu_stream_printf (ostr, "%*sbody_type=%s\n", indent, "", S(bs->body_type));
+  mu_stream_printf (ostr, "%*sbody_subtype=%s\n", indent, "",
+		    S(bs->body_subtype));
+  print_param (ostr, "Parameters", bs->body_param, indent);
+  mu_stream_printf (ostr, "%*sbody_id=%s\n", indent, "", S(bs->body_id));
+  mu_stream_printf (ostr, "%*sbody_descr=%s\n", indent, "", S(bs->body_descr));
+  mu_stream_printf (ostr, "%*sbody_encoding=%s\n", indent, "",
+		    S(bs->body_encoding));
+  mu_stream_printf (ostr, "%*sbody_size=%lu\n", indent, "",
+		    (unsigned long) bs->body_size);
+  /* Optional */
+  mu_stream_printf (ostr, "%*sbody_md5=%s\n", indent, "", S(bs->body_md5));
+  mu_stream_printf (ostr, "%*sbody_disposition=%s\n", indent, "",
+		    S(bs->body_disposition));
+  print_param (ostr, "Disposition Parameters", bs->body_disp_param, indent);
+  mu_stream_printf (ostr, "%*sbody_language=%s\n", indent, "",
+		    S(bs->body_language));
+  mu_stream_printf (ostr, "%*sbody_location=%s\n", indent, "",
+		    S(bs->body_location));
+
+  mu_stream_printf (ostr, "%*sType ", indent, "");
+  switch (bs->body_message_type)
+    {
+    case mu_message_other:
+      mu_stream_printf (ostr, "mu_message_other\n");
+      break;
+      
+    case mu_message_text:
+      mu_stream_printf (ostr, "mu_message_text:\n%*sbody_lines=%lu\n",
+			indent + 4, "",
+			(unsigned long) bs->v.text.body_lines);
+      break;
+      
+    case mu_message_rfc822:
+      mu_stream_printf (ostr, "mu_message_rfc822:\n%*sbody_lines=%lu\n",
+			indent + 4, "",
+		 (unsigned long) bs->v.rfc822.body_lines);
+      print_imapenvelope (ostr, bs->v.rfc822.body_env, level + 1);
+      print_bs (ostr, bs->v.rfc822.body_struct, level + 1);
+      break;
+      
+    case mu_message_multipart:
+      {
+	struct print_data pd;
+	pd.ostr = ostr;
+	pd.num = 0;
+	pd.level = level + 1;
+	mu_stream_printf (ostr, "mu_message_multipart:\n");
+	mu_list_foreach (bs->v.multipart.body_parts, print_item, &pd);
+      }
+    }
+}
+
 static int
 fetch_response_printer (void *item, void *data)
 {
@@ -252,31 +388,35 @@ fetch_response_printer (void *item, void *data)
       
     case MU_IMAP_FETCH_BODYSTRUCTURE:
       /* FIXME */
-      mu_stream_printf (str, "BODYSTRUCTURE (not yet implemented)\n");
+      mu_stream_printf (str, "BODYSTRUCTURE:\nBEGIN\n");
+      print_bs (str, resp->bodystructure.bs, 0);
+      mu_stream_printf (str, "END\n");
       break;
       
     case MU_IMAP_FETCH_ENVELOPE:
       {
+	struct mu_imapenvelope *env = resp->envelope.imapenvelope;
+	
 	mu_stream_printf (str, "ENVELOPE:\n");
 	
-	format_date (str, "date", &resp->envelope.date, &resp->envelope.tz);
+	format_date (str, "date", &env->date, &env->tz);
 	mu_stream_printf (str, "  subject = %s\n",
-			  resp->envelope.subject ?
-			   resp->envelope.subject : "NIL");
+			  env->subject ?
+			   env->subject : "NIL");
 
-	format_email (str, "from", resp->envelope.from);
-	format_email (str, "sender", resp->envelope.sender);
-	format_email (str, "reply-to", resp->envelope.reply_to);
-	format_email (str, "to", resp->envelope.to);
-	format_email (str, "cc", resp->envelope.cc);
-	format_email (str, "bcc", resp->envelope.bcc);
+	format_email (str, "from", env->from);
+	format_email (str, "sender", env->sender);
+	format_email (str, "reply-to", env->reply_to);
+	format_email (str, "to", env->to);
+	format_email (str, "cc", env->cc);
+	format_email (str, "bcc", env->bcc);
 
 	mu_stream_printf (str, "  in-reply-to = %s\n",
-			  resp->envelope.in_reply_to ?
-			   resp->envelope.in_reply_to : "NIL");
+			  env->in_reply_to ?
+			   env->in_reply_to : "NIL");
 	mu_stream_printf (str, "  message-id = %s\n",
-			  resp->envelope.message_id ?
-			   resp->envelope.message_id : "NIL");
+			  env->message_id ?
+			   env->message_id : "NIL");
       }
       break;
       
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 219731d..2a9f4ea 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -50,6 +50,7 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ 
 noinst_PROGRAMS = \
+ bs\
  fldel\
  lstuid\
  mbdel\
diff --git a/testsuite/bs.c b/testsuite/bs.c
new file mode 100644
index 0000000..8f74b2d
--- /dev/null
+++ b/testsuite/bs.c
@@ -0,0 +1,169 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <mailutils/mailutils.h>
+
+#define S(str) ((str) ? (str) : "")
+
+static void
+print_param (const char *prefix, mu_assoc_t assoc, int indent)
+{
+  mu_iterator_t itr;
+  int i;
+
+  mu_printf ("%*s%s:\n", indent, "", prefix);
+  if (!assoc)
+    return;
+  indent += 4;
+  MU_ASSERT (mu_assoc_get_iterator (assoc, &itr));
+      
+  for (i = 0, mu_iterator_first (itr);
+       !mu_iterator_is_done (itr);
+       i++, mu_iterator_next (itr))
+    {
+      const char *name;
+      struct mu_mime_param *p;
+      
+      mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
+      mu_printf ("%*s%d: %s=%s\n", indent, "", i, name, p->value);
+    }
+  mu_iterator_destroy (&itr);
+}
+
+struct print_data
+{
+  int num;
+  int level;
+};
+
+static void print_bs (struct mu_bodystructure *bs, int level);
+
+static int
+print_item (void *item, void *data)
+{
+  struct mu_bodystructure *bs = item;
+  struct print_data *pd = data;
+  mu_printf ("%*sPart #%d\n", (pd->level-1) << 2, "", pd->num);
+  print_bs (bs, pd->level);
+  ++pd->num;
+  return 0;
+}
+
+static void
+print_address (const char *title, mu_address_t addr, int indent)
+{
+  mu_printf ("%*s%s: ", indent, "", title);
+  mu_stream_format_address (mu_strout, addr);
+  mu_printf ("\n");
+}
+
+static void
+print_imapenvelope (struct mu_imapenvelope *env, int level)
+{
+  int indent = (level << 2);
+
+  mu_printf ("%*sEnvelope:\n", indent, "");
+  indent += 4;
+  mu_printf ("%*sTime: ", indent, "");
+  mu_c_streamftime (mu_strout, "%c%n", &env->date, &env->tz);
+  mu_printf ("%*sSubject: %s\n", indent, "", S(env->subject));
+  print_address ("From", env->from, indent);
+  print_address ("Sender", env->sender, indent);
+  print_address ("Reply-to", env->reply_to, indent);
+  print_address ("To", env->to, indent);
+  print_address ("Cc", env->cc, indent);
+  print_address ("Bcc", env->bcc, indent);
+  mu_printf ("%*sIn-Reply-To: %s\n", indent, "", S(env->in_reply_to));
+  mu_printf ("%*sMessage-ID: %s\n", indent, "", S(env->message_id));
+}
+
+static void
+print_bs (struct mu_bodystructure *bs, int level)
+{
+  int indent = level << 2;
+  mu_printf ("%*sbody_type=%s\n", indent, "", S(bs->body_type));
+  mu_printf ("%*sbody_subtype=%s\n", indent, "", S(bs->body_subtype));
+  print_param ("Parameters", bs->body_param, indent);
+  mu_printf ("%*sbody_id=%s\n", indent, "", S(bs->body_id));
+  mu_printf ("%*sbody_descr=%s\n", indent, "", S(bs->body_descr));
+  mu_printf ("%*sbody_encoding=%s\n", indent, "", S(bs->body_encoding));
+  mu_printf ("%*sbody_size=%lu\n", indent, "", (unsigned long) bs->body_size);
+  /* Optional */
+  mu_printf ("%*sbody_md5=%s\n", indent, "", S(bs->body_md5));
+  mu_printf ("%*sbody_disposition=%s\n", indent, "", S(bs->body_disposition));
+  print_param ("Disposition Parameters", bs->body_disp_param, indent);
+  mu_printf ("%*sbody_language=%s\n", indent, "", S(bs->body_language));
+  mu_printf ("%*sbody_location=%s\n", indent, "", S(bs->body_location));
+
+  mu_printf ("%*sType ", indent, "");
+  switch (bs->body_message_type)
+    {
+    case mu_message_other:
+      mu_printf ("mu_message_other\n");
+      break;
+      
+    case mu_message_text:
+      mu_printf ("mu_message_text:\n%*sbody_lines=%lu\n", indent + 4, "",
+		 (unsigned long) bs->v.text.body_lines);
+      break;
+      
+    case mu_message_rfc822:
+      mu_printf ("mu_message_rfc822:\n%*sbody_lines=%lu\n", indent + 4, "",
+		 (unsigned long) bs->v.rfc822.body_lines);
+      print_imapenvelope (bs->v.rfc822.body_env, level + 1);
+      print_bs (bs->v.rfc822.body_struct, level + 1);
+      break;
+      
+    case mu_message_multipart:
+      {
+	struct print_data pd;
+	pd.num = 0;
+	pd.level = level + 1;
+	mu_printf ("mu_message_multipart:\n");
+	mu_list_foreach (bs->v.multipart.body_parts, print_item, &pd);
+      }
+    }
+}
+
+int
+main (int argc, char **argv)
+{
+  mu_mailbox_t mbox;
+  mu_message_t mesg;
+  struct mu_bodystructure *bs;
+  
+  if (argc != 3)
+    {
+      fprintf (stderr, "usage: %s URL NUM\n", argv[0]);
+      return 1;
+    }
+
+  mu_register_all_mbox_formats ();
+  MU_ASSERT (mu_mailbox_create (&mbox, argv[1]));
+  MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_READ));
+  MU_ASSERT (mu_mailbox_get_message (mbox, atoi (argv[2]), &mesg));
+  MU_ASSERT (mu_message_get_bodystructure (mesg, &bs));
+  print_bs (bs, 0);
+  mu_bodystructure_free (bs);
+  
+  return 0;
+}


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 22 11:45:43 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rdlm3-0004C8-6U
	for mharc-commit-mailutils@gnu.org; Thu, 22 Dec 2011 11:45:43 -0500
Received: from eggs.gnu.org ([140.186.70.92]:58697)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rdllw-0004Bt-V3
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 11:45:41 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rdllq-0007NW-Kp
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 11:45:36 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:38556)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rdllq-0007NM-J7
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 11:45:30 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rdllp-0001Rs-Qd
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 16:45:30 +0000
Date: Thu, 22 Dec 2011 16:45:29 +0000
Message-Id: <E1Rdllp-0001Rs-Qd@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-545-g09be781
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: e218f1ec00749701a00c074c4574a75a6f156d2b
X-Git-Newrev: 09be781f34bd43f47f65eee4ce757e8c9ab742e6
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 22 Dec 2011 16:45:41 -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=09be781f34bd43f47f65eee4ce757e8c9ab742e6

The branch, master has been updated
       via  09be781f34bd43f47f65eee4ce757e8c9ab742e6 (commit)
      from  e218f1ec00749701a00c074c4574a75a6f156d2b (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 09be781f34bd43f47f65eee4ce757e8c9ab742e6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Thu Dec 22 18:44:30 2011 +0200

    mu imap: implement copy command.
    
    * mu/imap.c (com_copy): New function.
    (print_list_item): Treat 0 separator as '/' to avoid preliminary
    truncating the output.

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

Summary of changes:
 mu/imap.c |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/mu/imap.c b/mu/imap.c
index 9e6e387..c280740 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -1074,6 +1074,15 @@ com_append (int argc, char **argv)
 }
 
 static int
+com_copy (int argc, char **argv)
+{
+  int status = mu_imap_copy (imap, uid_mode, argv[1], argv[2]);
+  if (status)
+    report_failure ("copy", status);
+  return 0;
+}
+
+static int
 print_list_item (void *item, void *data)
 {
   struct mu_list_response *resp = item;
@@ -1083,7 +1092,8 @@ print_list_item (void *item, void *data)
 		    "%c%c %c %4d %s\n",
 		    (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY) ? 'd' : '-',
 		    (resp->type & MU_FOLDER_ATTRIBUTE_FILE) ? 'f' : '-',
-		    resp->separator,
+		    resp->separator ?
+		        resp->separator : MU_HIERARCHY_DELIMITER,
 		    resp->level,
 		    resp->name);
   return 0;
@@ -1220,6 +1230,10 @@ struct mutool_command imap_comtab[] = {
     com_append,
     N_("[-time DATETIME] [-flag FLAG] MAILBOX FILE"),
     N_("append message text from FILE to MAILBOX") },
+  { "copy",         3, 3, 0,
+    com_copy,
+    N_("MSGSET MAILBOX"),
+    N_("Copy messages from MSGSET to MAILBOX") },
   { "list",         3, 3, 0,
     com_list,
     N_("REF MBOX"),


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Thu Dec 22 18:11:41 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RdrnZ-0005Dy-Hd
	for mharc-commit-mailutils@gnu.org; Thu, 22 Dec 2011 18:11:41 -0500
Received: from eggs.gnu.org ([140.186.70.92]:54191)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdrnX-0005DM-65
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 18:11:40 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdrnV-0003FO-Od
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 18:11:39 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:33048)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdrnV-0003FI-Mw
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 18:11:37 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RdrnV-00039Y-EX
	for commit-mailutils@gnu.org; Thu, 22 Dec 2011 23:11:37 +0000
Date: Thu, 22 Dec 2011 23:11:37 +0000
Message-Id: <E1RdrnV-00039Y-EX@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-546-g22ec491
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 09be781f34bd43f47f65eee4ce757e8c9ab742e6
X-Git-Newrev: 22ec4913c13137b497c6d6ee7f433d0b94e059cd
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 22 Dec 2011 23:11:40 -0000

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

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

The branch, master has been updated
       via  22ec4913c13137b497c6d6ee7f433d0b94e059cd (commit)
      from  09be781f34bd43f47f65eee4ce757e8c9ab742e6 (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 22ec4913c13137b497c6d6ee7f433d0b94e059cd
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 23 01:10:58 2011 +0200

    Bugfixes in imap client.
    
    * libproto/imap/fetch.c (_fetch_fold): Fix parsing of BODY[].
    (_mu_imap_parse_fetch_response): Fix return value.

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

Summary of changes:
 libproto/imap/fetch.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index e448041..aa5c090 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -1052,6 +1052,12 @@ _fetch_fold (void *item, void *data)
 	}
       else if (strncmp (elt->v.string, "HEADER.FIELDS", 13) == 0)
 	env->state = resp_body_hlist;
+      else if (strcmp (elt->v.string, "]") == 0)
+	{
+	  env->section = NULL;
+	  env->state = resp_val;
+	  break;
+	}
       else
 	env->state = resp_body_end;
       env->section = elt->v.string;
@@ -1109,5 +1115,5 @@ _mu_imap_parse_fetch_response (mu_list_t input, mu_list_t *result_list)
   else
     *result_list = result;
   mu_list_destroy (&env.hlist);
-  return status;
+  return env.status;
 }


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 23 05:42:33 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Re2a9-0003JR-QL
	for mharc-commit-mailutils@gnu.org; Fri, 23 Dec 2011 05:42:33 -0500
Received: from eggs.gnu.org ([140.186.70.92]:42490)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re2a1-0003Ft-SP
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 05:42:31 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re2Zy-0003n1-I6
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 05:42:25 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:55425)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re2Zy-0003mw-Dm
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 05:42:22 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re2Zw-0006Gh-IV
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 10:42:21 +0000
Date: Fri, 23 Dec 2011 10:42:20 +0000
Message-Id: <E1Re2Zw-0006Gh-IV@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-547-ge4dc718
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 22ec4913c13137b497c6d6ee7f433d0b94e059cd
X-Git-Newrev: e4dc7185c62a3042b9a05828be83c1f1ca308dcb
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 23 Dec 2011 10:42: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=e4dc7185c62a3042b9a05828be83c1f1ca308dcb

The branch, master has been updated
       via  e4dc7185c62a3042b9a05828be83c1f1ca308dcb (commit)
      from  22ec4913c13137b497c6d6ee7f433d0b94e059cd (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 e4dc7185c62a3042b9a05828be83c1f1ca308dcb
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 23 12:40:44 2011 +0200

    Implement imap mailbox (read-only, so far).
    
    * include/mailutils/sys/imap.h (_mu_imap_mailbox_init): New proto.
    (_mu_imap_message,_mu_imap_mailbox): New structures.
    * libproto/imap/mbox.c: New file.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Put back mbox.c
    * libproto/imap/fetch.c: Fix body/bodystructure parsing.
    * libproto/imap/folder.c (_mu_imap_folder_destroy): Close folder.
    (_mu_imap_folder_open): Implement STARTTLS.
    (_imap_record): Accept parameters in URL, use _mu_imap_mailbox_init
    to initialize mailbox.

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

Summary of changes:
 include/mailutils/sys/imap.h |   39 ++-
 libproto/imap/Makefile.am    |    3 +-
 libproto/imap/fetch.c        |   34 +-
 libproto/imap/folder.c       |   54 ++-
 libproto/imap/mbox.c         |  946 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1037 insertions(+), 39 deletions(-)
 create mode 100644 libproto/imap/mbox.c

diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index 68bdf06..a06e1b0 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -235,9 +235,46 @@ int _mu_imap_url_init (mu_url_t url);
 int _mu_imaps_url_init (mu_url_t url);
 
 /* ----------------------------- */
-/* Folder interface              */
+/* Mailbox interface             */
 /* ----------------------------- */
 
+int _mu_imap_mailbox_init (mu_mailbox_t mailbox);
+
+#define _MU_IMAP_MSG_SCANNED   0x01
+#define _MU_IMAP_MSG_CACHED    0x02
+#define _MU_IMAP_MSG_LINES     0x04
+
+struct _mu_imap_message
+{
+  int flags;
+  size_t uid;               /* Message UID */   
+  int attr_flags;           /* Attributes */
+  mu_off_t offset;          /* Offset in the message cache stream */
+  mu_off_t body_start;      /* Start of message, relative to offset */
+  mu_off_t body_end;        /* End of message, relative to offset */  
+  size_t header_lines;      /* Number of lines in the header */
+  size_t body_lines;        /* Number of lines in the body */
+  size_t message_size;      /* Message size */
+  size_t message_lines;     /* Number of lines in the message */
+  struct mu_imapenvelope *env; /* IMAP envelope */
+  mu_message_t message;     /* Pointer to the message structure */ 
+  struct _mu_imap_mailbox *imbx; /* Back pointer.  */
+};
+
+#define _MU_IMAP_MBX_UPTODATE  0x01
+
+struct _mu_imap_mailbox
+{
+  int flags;
+  mu_off_t total_size;        /* Total mailbox size. */
+  struct mu_imap_stat stats;     /* Mailbox statistics */ 
+  struct _mu_imap_message *msgs; /* Array of messages */
+  size_t msgs_cnt;               /* Number of used slots in msgs */
+  size_t msgs_max;               /* Number of slots in msgs */
+  mu_stream_t cache;          /* Message cache stream */
+  int last_error;             /* Last error code */
+  mu_mailbox_t mbox;
+};
 
 # ifdef __cplusplus
 }
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index 181ac35..e950890 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -23,8 +23,6 @@ libmu_imap_la_LIBADD = ${MU_LIB_AUTH} ${MU_LIB_MAILUTILS} @INTLLIBS@
 
 SUBDIRS = . tests
 
-# FIXME: Put this back when ready
-#  mbox.c
 libmu_imap_la_SOURCES = \
  appmsg.c\
  appstr.c\
@@ -68,4 +66,5 @@ libmu_imap_la_SOURCES = \
  unselect.c\
  unsubscribe.c\
  folder.c\
+ mbox.c\
  url.c
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index aa5c090..22937df 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -618,7 +618,7 @@ _parse_bodystructure_simple (struct imap_list_element *elt,
   struct body_field_map *map;
   
   mu_list_count (elt->v.list, &n);
-  if (n < 8)
+  if (n < 7)
     return MU_ERR_PARSE;
 
   tok = _mu_imap_list_at (elt->v.list, BSTOK_BODY_TYPE);
@@ -976,6 +976,7 @@ _extract_string (void **itmv, size_t itmc, void *call_data)
 static int
 _fetch_fold (void *item, void *data)
 {
+  int rc;
   struct imap_list_element *elt = item;
   struct parse_response_env *env = data;
 
@@ -983,7 +984,6 @@ _fetch_fold (void *item, void *data)
     {
     case resp_kw:
       {
-	int rc;
 	char *kw;
 	size_t kwlen;
 	struct mapper_tab *mt;
@@ -1020,6 +1020,26 @@ _fetch_fold (void *item, void *data)
 	break;
       }
       
+    case resp_body:
+      if (_mu_imap_list_element_is_string (elt, "["))
+	{
+	  env->state = resp_body_section;
+	  break;
+	}
+      else
+	{
+	  env->mapper = _bodystructure_mapper;
+	  _free_fetch_response (env->resp);
+	  rc = alloc_response (&env->resp, MU_IMAP_FETCH_BODYSTRUCTURE);
+	  if (rc)
+	    {
+	      env->status = rc;
+	      return MU_ERR_FAILURE;
+	    }
+	  env->state = resp_val;
+	}
+      /* fall through */
+      
     case resp_val:
       if (env->mapper)
 	{
@@ -1034,16 +1054,6 @@ _fetch_fold (void *item, void *data)
       env->state = resp_kw;
       break;
       
-    case resp_body:
-      if (_mu_imap_list_element_is_string (elt, "["))
-	env->state = resp_body_section;
-      else
-	{
-	  env->mapper = _bodystructure_mapper;
-	  env->state = resp_val;
-	}
-      break;
-      
     case resp_body_section:
       if (elt->type != imap_eltype_string)
 	{
diff --git a/libproto/imap/folder.c b/libproto/imap/folder.c
index 1bf637f..02712d2 100644
--- a/libproto/imap/folder.c
+++ b/libproto/imap/folder.c
@@ -39,21 +39,13 @@
 #include <mailutils/sys/imap.h>
 #include <mailutils/sys/folder.h>
 
-/* Placeholders. */
-#define _mu_imap_mailbox_init NULL
-#define _mu_imaps_mailbox_init NULL
-
-
 static void
 _mu_imap_folder_destroy (mu_folder_t folder)
 {
   mu_imap_t imap = folder->data;
   if (imap)
     {
-      /*
-	mu_imap_logout (imap);
-	mu_imap_disconnect (imap);
-      */
+      mu_folder_close (folder);
       mu_imap_destroy (&imap);
       folder->data = imap;
     }
@@ -90,13 +82,6 @@ _mu_folder_bad_callback (void *data, int code, size_t sdat, void *pdat)
   mu_error (_("This probably indicates a bug in Mailutils client code."));
   mu_error (_("Please, report that to <%s>."), PACKAGE_BUGREPORT);
 }
-#if 0
-static void
-_mu_folder_fetch_callback (void *data, int code, size_t sdat, void *pdat)
-{
-  mu_folder_t folder = data;
-}
-#endif
 
 /* Set up an IMAP(S) connection for this folder */
 static int
@@ -187,11 +172,6 @@ _mu_imap_folder_open (mu_folder_t folder, int flags)
   mu_imap_register_callback_function (imap, MU_IMAP_CB_BAD,
 				      _mu_folder_bad_callback,
 				      folder);
-#if 0
-  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
-				      _mu_folder_fetch_callback,
-				      folder);
-#endif
   rc = mu_imap_connect (imap);
   if (rc)
     {
@@ -206,6 +186,32 @@ _mu_imap_folder_open (mu_folder_t folder, int flags)
       return rc;
     }
 
+#ifdef WITH_TLS
+  if (!tls && mu_imap_capability_test (imap, "STARTTLS", NULL) == 0)
+    {
+      size_t parmc = 0;
+      char **parmv = NULL;
+
+      tls = 1;
+      if (mu_url_sget_fvpairs (folder->url, &parmc, &parmv) == 0)
+	{
+	  size_t i;
+      
+	  for (i = 0; i < parmc; i++)
+	    {
+	      if (strcmp (parmv[i], "notls") == 0)
+		tls = 0;
+	      /*FIXME:
+		else if (strncmp (parmv[i], "auth=", 5) == 0)
+	      */
+	      /* unrecognized arguments silently ignored */
+	    }
+	}
+
+      if (tls)
+	mu_imap_starttls (imap);
+    }
+#endif
   if (mu_imap_session_state (imap) == MU_IMAP_SESSION_NONAUTH)
     {
       rc = mu_authority_authenticate (folder->authority);
@@ -217,7 +223,7 @@ _mu_imap_folder_open (mu_folder_t folder, int flags)
 	  mu_folder_close (folder);
 	}
     }
-  
+    
   return rc;
 }
 
@@ -551,7 +557,7 @@ static struct _mu_record _imap_record =
   MU_IMAP_PRIO,
   MU_IMAP_SCHEME,
   MU_RECORD_DEFAULT,
-  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_HOST,
   _mu_imap_url_init,     /* url entry.  */
   _mu_imap_mailbox_init, /* Mailbox entry.  */
@@ -576,7 +582,7 @@ static struct _mu_record _imaps_record =
   MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PATH | MU_URL_PARAM,
   MU_URL_HOST,
   _mu_imaps_url_init,     /* url entry.  */
-  _mu_imaps_mailbox_init, /* Mailbox entry.  */
+  _mu_imap_mailbox_init,  /* Mailbox entry.  */
   NULL,                /* Mailer entry.  */
   _mu_imap_folder_init,   /* Folder entry.  */
   NULL, /* No need for a back pointer.  */
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
new file mode 100644
index 0000000..841942e
--- /dev/null
+++ b/libproto/imap/mbox.c
@@ -0,0 +1,946 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <mailutils/errno.h>
+#include <mailutils/stream.h>
+#include <mailutils/message.h>
+#include <mailutils/folder.h>
+#include <mailutils/assoc.h>
+#include <mailutils/url.h>
+#include <mailutils/io.h>
+#include <mailutils/nls.h>
+#include <mailutils/diag.h>
+#include <mailutils/filter.h>
+#include <mailutils/observer.h>
+#include <mailutils/envelope.h>
+#include <mailutils/address.h>
+#include <mailutils/attribute.h>
+#include <mailutils/header.h>
+#include <mailutils/body.h>
+#include <mailutils/sys/imap.h>
+
+#define _imap_mbx_clrerr(imbx) ((imbx)->last_error = 0)
+#define _imap_mbx_errno(imbx) ((imbx)->last_error)
+#define _imap_mbx_uptodate(imbx) ((imbx)->flags & _MU_IMAP_MBX_UPTODATE)
+
+static int _imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount);
+static int _imap_mbx_is_updated (mu_mailbox_t mbox);
+
+/* ------------------------------- */
+/* Auxiliary message functions     */
+/* ------------------------------- */
+
+static size_t
+_imap_msg_no (struct _mu_imap_message *imsg)
+{
+  return imsg - imsg->imbx->msgs + 1;
+}
+
+static int
+_imap_fetch_with_callback (mu_imap_t imap, char *msgset, char *items,
+			   mu_imap_callback_t cb, void *data)
+{
+  int rc;
+  
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH, cb, data);
+  rc = mu_imap_fetch (imap, 0, msgset, items);
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH, NULL, NULL);
+  return rc;
+}
+
+static void
+_imap_msg_free (struct _mu_imap_message *msg)
+{
+  mu_message_imapenvelope_free (msg->env);
+  mu_message_destroy (&msg->message, msg);
+}
+
+struct save_closure
+{
+  mu_stream_t save_stream;
+  size_t size;
+  struct _mu_imap_message *imsg;
+};
+
+static void
+_save_message (void *data, int code, size_t sdat, void *pdat)
+{
+  struct save_closure *clos = data;
+  struct _mu_imap_message *imsg = clos->imsg;
+  struct _mu_imap_mailbox *imbx = imsg->imbx;
+  mu_list_t list = pdat;
+  union mu_imap_fetch_response *resp;
+  int rc;
+  mu_stream_t istr, flt;
+  mu_off_t size;
+  
+  rc = mu_list_get (list, 0, (void**)&resp);
+  if (rc)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("cannot get response item: %s"),
+		 mu_strerror (rc)));
+      imbx->last_error = rc;
+      return;
+    }
+
+  if (resp->type != MU_IMAP_FETCH_BODY)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("fetch returned a not requested item %d"), resp->type));
+      imbx->last_error = MU_ERR_FAILURE;
+      return;
+    }
+  rc = mu_static_memory_stream_create (&istr, resp->body.text,
+				       strlen (resp->body.text));
+
+  if (rc)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("mu_static_memory_stream_create: %s"),
+		 mu_strerror (rc)));
+      imbx->last_error = rc;
+      return;
+    }
+
+  rc = mu_filter_create (&flt, istr, "CRLF", MU_FILTER_DECODE, MU_STREAM_READ);
+  mu_stream_unref (istr);
+  if (rc)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("mu_filter_create: %s"), mu_strerror (rc)));
+      imbx->last_error = rc;
+      return;
+    }
+      
+  rc = mu_stream_copy (clos->save_stream, flt, 0, &size);
+  mu_stream_destroy (&flt);
+  if (rc)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("copying to cache failed: %s"), mu_strerror (rc)));
+      imbx->last_error = rc;
+      return;
+    }
+  clos->size = size;
+}
+
+static int
+__imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
+		       mu_stream_t *pstr)
+{
+  int rc;
+  struct _mu_imap_mailbox *imbx = imsg->imbx;
+  mu_folder_t folder = imbx->mbox->folder;
+  mu_imap_t imap = folder->data;
+  
+  if (!(imsg->flags & _MU_IMAP_MSG_CACHED))
+    {
+      char *msgset;
+	  
+      if (!imbx->cache)
+	{
+	  rc = mu_temp_file_stream_create (&imbx->cache, NULL, 0);
+	  if (rc)
+	    /* FIXME: Try to recover first */
+	    return rc;
+
+	  mu_stream_set_buffer (imbx->cache, mu_buffer_full, 8192);
+	}
+
+      rc = mu_stream_size (imbx->cache, &imsg->offset);
+      if (rc)
+	return rc;
+
+      rc = mu_asprintf (&msgset, "%lu", msgno);
+      if (rc == 0)
+	{
+	  struct save_closure clos;
+	  clos.imsg = imsg;
+	  clos.save_stream = imbx->cache;
+	  _imap_mbx_clrerr (imbx);
+	  rc = _imap_fetch_with_callback (imap, msgset, "BODY[]",
+					  _save_message, &clos);
+	  free (msgset);
+	  if (rc == 0 && !_imap_mbx_errno (imbx))
+	    imsg->message_size = clos.size;
+	}
+
+      if (rc)
+	return rc;
+
+      imsg->flags |= _MU_IMAP_MSG_CACHED;
+    }
+  return mu_streamref_create_abridged (pstr, imbx->cache,
+				       imsg->offset,
+				       imsg->offset + imsg->message_size - 1);
+}
+
+static int
+_imap_msg_scan (struct _mu_imap_message *imsg)
+{
+  int rc;
+  mu_stream_t stream;
+  struct mu_message_scan scan;
+  size_t msgno = _imap_msg_no (imsg);
+  
+  if (imsg->flags & _MU_IMAP_MSG_SCANNED)
+    return 0;
+  
+  rc = __imap_msg_get_stream (imsg, msgno, &stream);
+  if (rc)
+    return rc;
+  
+  scan.flags = MU_SCAN_SEEK | MU_SCAN_SIZE;
+  scan.message_start = 0;
+  scan.message_size = imsg->message_size;
+  rc = mu_stream_scan_message (stream, &scan);
+  mu_stream_unref (stream);
+
+  if (rc == 0)
+    {
+      imsg->body_start = scan.body_start;
+      imsg->body_end = scan.body_end;
+      imsg->header_lines = scan.header_lines;
+      imsg->body_lines = scan.body_lines;
+      imsg->message_lines = imsg->header_lines + 1 + imsg->body_lines;
+      imsg->flags |= _MU_IMAP_MSG_SCANNED;
+    }
+  
+  return rc;
+}
+
+/* ------------------------------- */
+/* Message envelope                */
+/* ------------------------------- */
+
+static int
+_imap_env_date (mu_envelope_t env, char *buf, size_t len,
+		size_t *pnwrite)
+{
+  struct _mu_imap_message *imsg = mu_envelope_get_owner (env);
+  mu_stream_t str;
+  int rc;
+  
+  if (!buf)
+    rc = mu_nullstream_create (&str, MU_STREAM_WRITE);
+  else
+    rc = mu_fixed_memory_stream_create (&str, buf, len, MU_STREAM_WRITE);
+  if (rc == 0)
+    {
+      mu_stream_stat_buffer statbuf;
+      mu_stream_set_stat (str, MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT),
+			  statbuf);
+      rc = mu_c_streamftime (str, MU_DATETIME_FROM,
+			     &imsg->env->date, &imsg->env->tz);
+      mu_stream_destroy (&str);
+      if (pnwrite)
+	*pnwrite = statbuf[MU_STREAM_STAT_OUT];
+    }
+  return rc;
+}
+
+static int
+_imap_env_sender (mu_envelope_t env, char *buf, size_t len,
+		  size_t *pnwrite)
+{
+  struct _mu_imap_message *imsg = mu_envelope_get_owner (env);
+  mu_address_t addr = imsg->env->sender ? imsg->env->sender : imsg->env->from;
+
+  if (!addr)
+    return MU_ERR_NOENT;
+  return mu_address_get_email (addr, 1, buf, len, pnwrite);
+}
+  
+static int
+_imap_msg_env_setup (struct _mu_imap_message *imsg, mu_message_t message)
+{
+  mu_envelope_t env;
+  int rc = mu_envelope_create (&env, imsg);
+  if (rc == 0)
+    {
+      mu_envelope_set_sender (env, _imap_env_sender, imsg);
+      mu_envelope_set_date (env, _imap_env_date, imsg);
+      rc = mu_message_set_envelope (message, env, imsg);
+    }
+  return rc;
+}
+
+/* ------------------------------- */
+/* Message attributes              */
+/* ------------------------------- */
+static int
+_imap_attr_get_flags (mu_attribute_t attr, int *pflags)
+{
+  struct _mu_imap_message *imsg = mu_attribute_get_owner (attr);
+
+  if (!imsg)
+    return EINVAL;
+  if (pflags)
+    *pflags = imsg->attr_flags;
+  return 0;
+}
+
+static int
+_imap_attr_set_flags (mu_attribute_t attr, int flags)
+{
+  struct _mu_imap_message *imsg = mu_attribute_get_owner (attr);
+
+  if (!imsg)
+    return EINVAL;
+  imsg->attr_flags |= flags;
+  return 0;
+}
+
+static int
+_imap_attr_clr_flags (mu_attribute_t attr, int flags)
+{
+  struct _mu_imap_message *imsg = mu_attribute_get_owner (attr);
+
+  if (!imsg)
+    return EINVAL;
+  imsg->attr_flags |= flags;
+  return 0;
+}
+
+int
+_imap_msg_attr_setup (struct _mu_imap_message *imsg, mu_message_t message)
+{
+  mu_attribute_t attribute;
+  int rc = mu_attribute_create (&attribute, imsg);
+  if (rc == 0)
+    {
+      mu_attribute_set_get_flags (attribute, _imap_attr_get_flags, imsg);
+      mu_attribute_set_set_flags (attribute, _imap_attr_set_flags, imsg);
+      mu_attribute_set_unset_flags (attribute, _imap_attr_clr_flags, imsg);
+      rc = mu_message_set_attribute (message, attribute, imsg);
+    }
+  return rc;
+}
+
+/* ------------------------------- */
+/* Header functions                */
+/* ------------------------------- */
+static int
+_imap_hdr_fill (void *data, char **pbuf, size_t *plen)
+{
+  struct _mu_imap_message *imsg = data;
+  struct _mu_imap_mailbox *imbx = imsg->imbx;
+  mu_imap_t imap = imbx->mbox->folder->data;
+  struct save_closure clos;
+  char *msgset;
+  unsigned long msgno = _imap_msg_no (imsg);
+  int rc;
+  
+  clos.imsg = imsg;
+  rc = mu_memory_stream_create (&clos.save_stream, MU_STREAM_RDWR);
+  _imap_mbx_clrerr (imbx);
+  rc = mu_asprintf (&msgset, "%lu", msgno);
+  if (rc == 0)
+    {
+      rc = _imap_fetch_with_callback (imap, msgset, "BODY.PEEK[HEADER]",
+				      _save_message, &clos);
+      free (msgset);
+      if (rc == 0)
+	{
+	  char *buf;
+	  mu_off_t size;
+	  
+	  mu_stream_size (clos.save_stream, &size);
+	  buf = malloc (size + 1);
+	  if (!buf)
+	    rc = ENOMEM;
+	  else
+	    {
+	      mu_stream_seek (clos.save_stream, 0, MU_SEEK_SET, NULL);
+	      rc = mu_stream_read (clos.save_stream, buf, size, NULL);
+	      if (rc == 0)
+		{
+		  *pbuf = buf;
+		  *plen = size;
+		}
+	      else
+		free (buf);
+	    }
+	}
+    }
+  mu_stream_destroy (&clos.save_stream);
+  return rc;
+}
+
+static int
+_imap_msg_header_setup (struct _mu_imap_message *imsg, mu_message_t message)
+{
+  int rc;
+  mu_header_t header = NULL;
+
+  rc = mu_header_create (&header, NULL, 0);
+  if (rc)
+    return rc;
+  mu_header_set_fill (header, _imap_hdr_fill, imsg);
+  return mu_message_set_header (message, header, imsg);
+}
+
+/* ------------------------------- */
+/* Body functions                  */
+/* ------------------------------- */
+static int
+_imap_body_get_stream (mu_body_t body, mu_stream_t *pstr)
+{
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  struct _mu_imap_mailbox *imbx = imsg->imbx;
+  int rc;
+
+  rc = _imap_msg_scan (imsg);
+  if (rc)
+    return rc;
+  return mu_streamref_create_abridged (pstr, imbx->cache,
+				       imsg->offset + imsg->body_start,
+				       imsg->offset + imsg->body_end - 1);
+}
+
+static int
+_imap_body_size (mu_body_t body, size_t *psize)
+{
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  int rc;
+
+  rc = _imap_msg_scan (imsg);
+  if (rc)
+    return rc;
+  *psize = imsg->body_end - imsg->body_start;
+  return 0;
+}
+
+static int
+_imap_body_lines (mu_body_t body, size_t *psize)
+{
+  mu_message_t msg = mu_body_get_owner (body);
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  int rc;
+
+  rc = _imap_msg_scan (imsg);
+  if (rc)
+    return rc;
+  *psize = imsg->body_lines;
+  return 0;
+}
+
+static int
+_imap_mbx_body_setup (struct _mu_imap_message *imsg, mu_message_t message)
+{
+  int rc;
+  mu_body_t body;
+
+  /* FIXME: The owner of the body *must* be the message it belongs to. */
+  rc = mu_body_create (&body, message);
+  if (rc)
+    return rc;
+
+  mu_body_set_get_stream (body, _imap_body_get_stream, message);
+  mu_body_set_size (body, _imap_body_size, message);
+  mu_body_set_lines (body, _imap_body_lines, message);
+
+  return mu_message_set_body (message, body, imsg);
+}
+
+
+/* ------------------------------- */
+/* Message functions               */
+/* ------------------------------- */
+
+static int
+_imap_msg_get_stream (mu_message_t msg, mu_stream_t *pstr)
+{
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  return __imap_msg_get_stream (imsg, _imap_msg_no (imsg), pstr);
+}
+
+static int
+_imap_msg_size (mu_message_t msg, size_t *psize)
+{
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  *psize = imsg->message_size;
+  return 0;
+}
+
+static int
+_imap_msg_lines (mu_message_t msg, size_t *plines, int quick)
+{
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  struct _mu_imap_mailbox *imbx = imsg->imbx;
+  mu_mailbox_t mbox = imbx->mbox;
+  
+  if (!(imsg->flags & _MU_IMAP_MSG_LINES))
+    {
+      int rc;
+      
+      if (quick && !(imsg->flags & _MU_IMAP_MSG_CACHED))
+	return MU_ERR_INFO_UNAVAILABLE;
+      if (!_imap_mbx_uptodate (imbx))
+	_imap_mbx_scan (mbox, 1, NULL);
+      rc = _imap_msg_scan (imsg);
+      if (rc)
+	return rc;
+    }
+  *plines = imsg->message_lines;
+  return 0;
+}
+
+static int
+_imap_mbx_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
+{
+  struct _mu_imap_mailbox *imbx = mailbox->data;
+  struct _mu_imap_message *imsg;
+  int rc;
+  
+  /* If we did not start a scanning yet do it now.  */
+  if (!_imap_mbx_uptodate (imbx))
+    _imap_mbx_scan (mailbox, 1, NULL);
+
+  if (msgno > imbx->msgs_cnt)
+    return MU_ERR_NOENT;
+
+  imsg = imbx->msgs + msgno - 1;
+  if (!imsg->message)
+    {
+      mu_message_t msg;
+  
+      rc = mu_message_create (&msg, imsg);
+      if (rc)
+	return rc;
+      mu_message_set_get_stream (msg, _imap_msg_get_stream, imsg);
+      mu_message_set_size (msg, _imap_msg_size, imsg);
+      mu_message_set_lines (msg, _imap_msg_lines, imsg);
+
+      do
+	{
+	  rc = _imap_msg_env_setup (imsg, msg);
+	  if (rc)
+	    break;
+	  rc = _imap_msg_attr_setup (imsg, msg);
+	  if (rc)
+	    break;
+	  rc = _imap_msg_header_setup (imsg, msg);
+	  if (rc)
+	    break;
+	  rc = _imap_mbx_body_setup (imsg, msg);
+	}
+      while (0);
+      
+      if (rc)
+	{
+	  mu_message_destroy (&msg, imsg);
+	  return rc;
+	}
+      imsg->message = msg;
+
+    }
+  *pmsg = imsg->message;
+  return 0;
+}
+
+/* ------------------------------- */
+/* Mailbox functions               */
+/* ------------------------------- */
+static int
+_imap_realloc_messages (struct _mu_imap_mailbox *imbx, size_t count)
+{
+  if (count > imbx->msgs_max)
+    {
+      struct _mu_imap_message *newmsgs = realloc (imbx->msgs,
+						  count * sizeof (*newmsgs));
+      if (!newmsgs)
+	return ENOMEM;
+      memset (newmsgs + imbx->msgs_max, 0,
+	      sizeof (*newmsgs) * (count - imbx->msgs_max));
+      imbx->msgs = newmsgs;
+      imbx->msgs_max = count;
+    }
+  return 0;
+}
+
+static void
+_imap_mbx_destroy (mu_mailbox_t mailbox)
+{
+  size_t i;
+  struct _mu_imap_mailbox *imbx = mailbox->data;
+  
+  if (!imbx)
+    return;
+
+  if (imbx->msgs)
+    {
+      for (i = 0; i < imbx->msgs_cnt; i++)
+	_imap_msg_free (imbx->msgs + i);
+      free (imbx->msgs);
+    }
+  mu_stream_unref (imbx->cache);
+  free (imbx);
+  mailbox->data = NULL;
+}
+
+static void
+_imap_update_callback (void *data, int code, size_t sdat, void *pdat)
+{
+  struct _mu_imap_mailbox *imbx = data;
+  memcpy (&imbx->stats, pdat, sizeof (imbx->stats));
+  imbx->flags &= ~_MU_IMAP_MBX_UPTODATE;
+}
+
+static int
+_imap_mbx_open (mu_mailbox_t mbox, int flags)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  mu_folder_t folder = mbox->folder;
+  int rc;
+  const char *mbox_name;
+  mu_url_t url;
+  mu_imap_t imap;
+  
+  rc = mu_mailbox_get_url (mbox, &url);
+  if (rc)
+    return rc;
+  rc = mu_url_sget_path (url, &mbox_name);
+  if (rc == MU_ERR_NOENT)
+    mbox_name = "INBOX";
+  else if (rc)
+    return rc;
+      
+  rc = mu_folder_open (folder, flags);
+  if (rc)
+    return rc;
+
+  imap = folder->data;
+
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_RECENT_COUNT,
+				      _imap_update_callback,
+				      imbx);
+  mu_imap_register_callback_function (imap, MU_IMAP_STAT_MESSAGE_COUNT,
+				      _imap_update_callback,
+				      imbx);
+  
+  rc = mu_imap_select (imap, mbox_name,
+		       flags & (MU_STREAM_WRITE|MU_STREAM_APPEND),
+		       &imbx->stats);
+  if (rc)
+    return rc;
+
+  if (imbx->stats.flags & MU_IMAP_STAT_MESSAGE_COUNT)
+    rc = _imap_realloc_messages (imbx, imbx->stats.message_count);
+  
+  return rc;
+}
+
+static int
+_imap_mbx_close (mu_mailbox_t mbox)
+{
+  int rc;
+  mu_folder_t folder = mbox->folder;
+  mu_imap_t imap = folder->data;
+
+  if (mu_imap_capability_test (imap, "UNSELECT", NULL) == 0)
+    rc = mu_imap_unselect (imap);
+  else
+    rc = mu_imap_close (imap);
+  return rc;
+}
+
+static int
+_imap_messages_count (mu_mailbox_t mbox, size_t *pcount)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  if (imbx->stats.flags & MU_IMAP_STAT_MESSAGE_COUNT)
+    *pcount = imbx->stats.message_count;
+  else
+    return MU_ERR_INFO_UNAVAILABLE;
+  return 0;
+}
+  
+static int
+_imap_messages_recent (mu_mailbox_t mbox, size_t *pcount)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  if (imbx->stats.flags & MU_IMAP_STAT_RECENT_COUNT)
+    *pcount = imbx->stats.recent_count;
+  else
+    return MU_ERR_INFO_UNAVAILABLE;
+  return 0;
+}
+
+static int
+_imap_uidnext (mu_mailbox_t mbox, size_t *pn)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  if (imbx->stats.flags & MU_IMAP_STAT_UIDNEXT)
+    *pn = imbx->stats.uidnext;
+  else
+    return MU_ERR_INFO_UNAVAILABLE;
+  return 0;
+}
+
+static int
+_imap_message_unseen (mu_mailbox_t mbox, size_t *pn)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  if (imbx->stats.flags & MU_IMAP_STAT_FIRST_UNSEEN)
+    *pn = imbx->stats.uidnext;
+  else
+    return MU_ERR_INFO_UNAVAILABLE;
+  return 0;
+}
+
+static int
+_imap_uidvalidity (mu_mailbox_t mbox, unsigned long *pn)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  if (imbx->stats.flags & MU_IMAP_STAT_UIDVALIDITY)
+    *pn = imbx->stats.uidvalidity;
+  else
+    return MU_ERR_INFO_UNAVAILABLE;
+  return 0;
+}
+
+static int
+_imap_mbx_expunge (mu_mailbox_t mbox)
+{
+  mu_folder_t folder = mbox->folder;
+  mu_imap_t imap = folder->data;
+  return mu_imap_expunge (imap);
+}
+
+static int _compute_lines (struct mu_bodystructure *bs, size_t *pcount);
+
+static int
+sum_lines (void *item, void *data)
+{
+  struct mu_bodystructure *bs = item;
+  size_t *pn = data;
+  size_t n;
+
+  if (_compute_lines (bs, &n))
+    return 1;
+  *pn += n;
+  return 0;
+}
+
+static int
+_compute_lines (struct mu_bodystructure *bs, size_t *pcount)
+{
+  switch (bs->body_message_type)
+    {
+    case mu_message_other:
+      break;
+
+    case mu_message_text:
+      *pcount = bs->v.text.body_lines;
+      return 0;
+
+    case mu_message_rfc822:
+      *pcount = bs->v.rfc822.body_lines;
+      return 0;
+      
+    case mu_message_multipart:
+      *pcount = 0;
+      return mu_list_foreach (bs->v.multipart.body_parts, sum_lines, pcount);
+    }
+  return 1;
+}
+
+static int
+fetch_response_parser (void *item, void *data)
+{
+  union mu_imap_fetch_response *resp = item;
+  struct _mu_imap_message *imsg = data;
+
+  switch (resp->type)
+    {
+    case MU_IMAP_FETCH_UID:
+      imsg->uid = resp->uid.uid;
+      break;
+      
+    case MU_IMAP_FETCH_FLAGS:
+      imsg->attr_flags = resp->flags.flags;
+      break;
+      
+    case MU_IMAP_FETCH_ENVELOPE:
+      imsg->env = resp->envelope.imapenvelope;
+      resp->envelope.imapenvelope = NULL; /* Steal the envelope */
+      break;
+      
+    case MU_IMAP_FETCH_RFC822_SIZE:
+      imsg->message_size = resp->rfc822_size.size;
+      break;
+      
+    case MU_IMAP_FETCH_BODYSTRUCTURE:
+      {
+	size_t n;
+	if (_compute_lines (resp->bodystructure.bs, &n) == 0)
+	  {
+	    imsg->message_lines = n;
+	    imsg->flags |= _MU_IMAP_MSG_LINES;
+	  }
+      }
+      break;
+      
+    default:
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE0,
+		(_("fetch returned a not requested item %d"),
+		 resp->type));
+      break;
+    }
+  return 0;
+}
+  
+static void
+_imap_fetch_callback (void *data, int code, size_t sdat, void *pdat)
+{
+  struct _mu_imap_mailbox *imbx = data;
+  mu_mailbox_t mbox = imbx->mbox;
+  mu_list_t list = pdat;
+  int rc;
+  struct _mu_imap_message *imsg;
+  
+  rc = _imap_realloc_messages (imbx, sdat - 1);
+  if (rc)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("cannot reallocate array of messages: %s"),
+		 mu_strerror (rc)));
+      imbx->last_error = rc;
+      return;
+    }
+  if (imbx->msgs_cnt < sdat)
+    imbx->msgs_cnt = sdat;
+  imsg = imbx->msgs + sdat - 1;
+  imsg->imbx = imbx;
+  mu_list_foreach (list, fetch_response_parser, imsg);
+
+  if (mbox->observable)
+    {
+      if (((sdat + 1) % 10) == 0)
+	mu_observable_notify (imbx->mbox->observable,
+			      MU_EVT_MAILBOX_PROGRESS, NULL);
+    }
+}
+
+static int
+_imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  mu_folder_t folder = mbox->folder;
+  mu_imap_t imap = folder->data;
+  char *msgset;
+  int rc;
+  static char _imap_scan_items[] = "(UID FLAGS ENVELOPE RFC822.SIZE BODY)";
+  
+  rc = mu_asprintf (&msgset, "%lu:*", (unsigned long) msgno);
+  if (rc)
+    return rc;
+
+  _imap_mbx_clrerr (imbx);
+  rc = _imap_fetch_with_callback (imap, msgset, _imap_scan_items,
+				  _imap_fetch_callback, imbx);
+  free (msgset);
+  if (rc == 0)
+    rc = _imap_mbx_errno (imbx);
+  if (rc == 0)
+    {
+      size_t i;
+      mu_off_t total = 0;
+
+      imbx->flags |= _MU_IMAP_MBX_UPTODATE;
+
+      for (i = 1; i <= imbx->msgs_cnt; i++)
+	{
+	  total += imbx->msgs[i-1].message_size;
+	  /* MU_EVT_MESSAGE_ADD must be delivered only when it is already
+	     possible to retrieve the message in question.  It could not be
+	     done in the fetch handler for obvious reasons.  Hence the extra
+	     loop. */
+	  if (mbox->observable)
+	    mu_observable_notify (mbox->observable, MU_EVT_MESSAGE_ADD, &i);
+	}
+      imbx->total_size = total;
+      
+      if (pcount)
+	*pcount = imbx->msgs_cnt;
+    }
+  return rc;
+}
+
+static int
+_imap_mbx_is_updated (mu_mailbox_t mbox)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  mu_folder_t folder = mbox->folder;
+  mu_imap_t imap = folder->data;
+  int rc;
+  
+  rc = mu_imap_noop (imap);
+  if (rc)
+    {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		(_("mu_imap_noop: %s"), mu_strerror (rc)));
+      imbx->last_error = rc;
+    }
+  return imbx->flags & _MU_IMAP_MBX_UPTODATE;
+}
+
+int
+_mu_imap_mailbox_init (mu_mailbox_t mailbox)
+{
+  struct _mu_imap_mailbox *mbx = calloc (1, sizeof (*mbx));
+
+  if (!mbx)
+    return ENOMEM;
+  mbx->mbox = mailbox;
+  mailbox->data = mbx;
+
+  mailbox->_destroy = _imap_mbx_destroy;
+  mailbox->_open = _imap_mbx_open;
+  mailbox->_close = _imap_mbx_close;
+  mailbox->_expunge = _imap_mbx_expunge;
+
+  mailbox->_messages_count = _imap_messages_count;
+  mailbox->_messages_recent = _imap_messages_recent;
+  mailbox->_message_unseen = _imap_message_unseen;
+  mailbox->_uidvalidity = _imap_uidvalidity;
+  mailbox->_uidnext = _imap_uidnext;
+  
+  mailbox->_scan = _imap_mbx_scan;
+  mailbox->_is_updated = _imap_mbx_is_updated;
+  mailbox->_get_message = _imap_mbx_get_message;
+  
+  //FIXME
+#if 0
+
+  /* Messages.  */
+  mailbox->_append_message = _mu_imap_append_message;
+
+#endif
+  return 0;
+}


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 23 10:26:48 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Re71E-0005y3-51
	for mharc-commit-mailutils@gnu.org; Fri, 23 Dec 2011 10:26:48 -0500
Received: from eggs.gnu.org ([140.186.70.92]:49892)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re71A-0005xl-Fq
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 10:26:46 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re717-00069Y-My
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 10:26:44 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:36910)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re717-00069U-Jj
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 10:26:41 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Re715-0007MS-JX
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 15:26:41 +0000
Date: Fri, 23 Dec 2011 15:26:39 +0000
Message-Id: <E1Re715-0007MS-JX@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-548-g2b62203
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: e4dc7185c62a3042b9a05828be83c1f1ca308dcb
X-Git-Newrev: 2b62203b38aebd3cac815de74813a0bbacb086a6
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 23 Dec 2011 15:26:47 -0000

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

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

The branch, master has been updated
       via  2b62203b38aebd3cac815de74813a0bbacb086a6 (commit)
      from  e4dc7185c62a3042b9a05828be83c1f1ca308dcb (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 2b62203b38aebd3cac815de74813a0bbacb086a6
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 23 17:10:58 2011 +0200

    Finish imap mailbox implementation.
    
    Some features are missing (notably, quick access), but overall the
    implementation offers all necessary features and performs a lot
    better than 2.x.
    
    Additionally, this commit fixes a minor bug in the imap4d implementation:
    the STORE command did not accept empty flag list.
    
    * NEWS: Update.
    * imap4d/store.c (store_thunk): Accept empty list as "items" argument.
    
    * include/mailutils/imap.h (mu_imap_store_flags): New proto.
    * include/mailutils/imaputil.h (mu_imap_format_flags): Add new argument.
    * include/mailutils/sys/imap.h (_MU_IMAP_MSG_ATTRCHG): New flag.
    * libmailutils/imapio/flags.c: Accept three arguments, last one being
    a boolean flag specifying whether we want \Recent in the output.
    * libproto/imap/storeflg.c: New flag.
    * libproto/imap/Makefile.am (libmu_imap_la_SOURCES): Add storeflg.c.
    * libproto/imap/mbox.c: Finish the basic implementation.
    
    * imap4d/sync.c: Update calls to mu_imap_format_flags.
    * imap4d/util.c: Likewise.
    * mu/imap.c: Likewise.
    * libmailutils/imapio/sendflg.c (mu_imapio_send_flags): Likewise.

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

Summary of changes:
 NEWS                                  |    5 +-
 imap4d/store.c                        |   16 +-
 imap4d/sync.c                         |    2 +-
 imap4d/util.c                         |    2 +-
 include/mailutils/imap.h              |   10 +
 include/mailutils/imaputil.h          |    2 +-
 include/mailutils/sys/imap.h          |    7 +-
 libmailutils/imapio/flags.c           |    4 +-
 libmailutils/imapio/sendflg.c         |    2 +-
 libproto/imap/Makefile.am             |    1 +
 libproto/imap/mbox.c                  |  295 ++++++++++++++++++++++++++-------
 libproto/imap/{login.c => storeflg.c} |   58 ++++----
 mu/imap.c                             |    6 +-
 13 files changed, 295 insertions(+), 115 deletions(-)
 copy libproto/imap/{login.c => storeflg.c} (62%)

diff --git a/NEWS b/NEWS
index 25cf5d1..101f39a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-GNU mailutils NEWS -- history of user-visible changes. 2011-12-17
+GNU mailutils NEWS -- history of user-visible changes. 2011-12-23
 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 Free Software Foundation, Inc.
 See the end of file for copying conditions.
@@ -23,7 +23,6 @@ Due to the global nature of these changes, the rewrite of some parts
 of the package is not yet finished, while some others are considered
 experimental.  Not currently implemented are:
 
- - imap client
  - nntp client
  
 Experimental features are:
@@ -144,7 +143,7 @@ It is normally not needed to specify --with-gdbm, --with-berkeley-db
 or --with-ndbm explicitly.  Configuration will automatically pick up
 all available DBM libraries it can use.
 
-** Imap and nntp clients are not yet implemented
+** Nntp client is not yet implemented
 
 ** Link with GSASL by default
 
diff --git a/imap4d/store.c b/imap4d/store.c
index 17f853c..20318b5 100644
--- a/imap4d/store.c
+++ b/imap4d/store.c
@@ -75,14 +75,14 @@ store_thunk (imap4d_parsebuf_t p)
   if (status)
     imap4d_parsebuf_exit (p, "Failed to parse message set");
 
-  if (p->token[0] != '(')
-    imap4d_parsebuf_exit (p, "Syntax error");
-  imap4d_parsebuf_next (p, 1);
-  
-  do
-    if (mu_imap_flag_to_attribute (p->token, &pclos->type))
-      imap4d_parsebuf_exit (p, "Unrecognized flag");
-  while (imap4d_parsebuf_next (p, 1) && p->token[0] != ')');
+  if (strcmp (p->token, "NIL"))
+    {
+      if (p->token[0] != '(')
+	imap4d_parsebuf_exit (p, "Syntax error");
+      while (imap4d_parsebuf_next (p, 1) && p->token[0] != ')')
+	if (mu_imap_flag_to_attribute (p->token, &pclos->type))
+	  imap4d_parsebuf_exit (p, "Unrecognized flag");
+    }
   return RESP_OK;
 }
 
diff --git a/imap4d/sync.c b/imap4d/sync.c
index e14b015..d054f95 100644
--- a/imap4d/sync.c
+++ b/imap4d/sync.c
@@ -100,7 +100,7 @@ notify (void)
 	      if (nflags != attr_table[i-1])
 		{
 		  io_sendf ("* %lu FETCH FLAGS (",  (unsigned long) i);
-		  mu_imap_format_flags (iostream, nflags);
+		  mu_imap_format_flags (iostream, nflags, 1);
 		  io_sendf (")\n");
 		  attr_table[i-1] = nflags;
 		}
diff --git a/imap4d/util.c b/imap4d/util.c
index 5ade76b..643d962 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -499,7 +499,7 @@ util_print_flags (mu_attribute_t attr)
   int flags = 0;
 
   mu_attribute_get_flags (attr, &flags);
-  mu_imap_format_flags (iostream, flags);
+  mu_imap_format_flags (iostream, flags, 1);
 }
 
 int
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index b6f9e57..2e8e371 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -71,6 +71,16 @@ int mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset,
 int mu_imap_store (mu_imap_t imap, int uid, const char *msgset,
 		   const char *items);
 
+#define MU_IMAP_STORE_SET 0
+#define MU_IMAP_STORE_ADD 1
+#define MU_IMAP_STORE_CLR 2
+#define MU_IMAP_STORE_SILENT 0x10
+
+#define MU_IMAP_STORE_OPMASK 0xf
+
+int mu_imap_store_flags (mu_imap_t imap, int uid, const char *msgset,
+			 int op, int flags);
+  
 int mu_imap_delete (mu_imap_t imap, const char *mailbox);
 int mu_imap_rename (mu_imap_t imap, const char *mailbox,
 		    const char *new_mailbox);
diff --git a/include/mailutils/imaputil.h b/include/mailutils/imaputil.h
index 09ddce4..3f922dc 100644
--- a/include/mailutils/imaputil.h
+++ b/include/mailutils/imaputil.h
@@ -26,7 +26,7 @@ extern "C" {
 int mu_imap_wildmatch (const char *pattern, const char *name, int delim);
 
 int mu_imap_flag_to_attribute (const char *item, int *attr);
-int mu_imap_format_flags (mu_stream_t str, int flags);
+  int mu_imap_format_flags (mu_stream_t str, int flags, int include_recent);
 
 #ifdef __cplusplus
 }
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index a06e1b0..fd0f905 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -240,9 +240,10 @@ int _mu_imaps_url_init (mu_url_t url);
 
 int _mu_imap_mailbox_init (mu_mailbox_t mailbox);
 
-#define _MU_IMAP_MSG_SCANNED   0x01
-#define _MU_IMAP_MSG_CACHED    0x02
-#define _MU_IMAP_MSG_LINES     0x04
+#define _MU_IMAP_MSG_SCANNED   0x01 /* Message has already been scanned */
+#define _MU_IMAP_MSG_CACHED    0x02 /* Message is cached */
+#define _MU_IMAP_MSG_LINES     0x04 /* Number of lines is computed */
+#define _MU_IMAP_MSG_ATTRCHG   0x08 /* Message attributes has changed */
 
 struct _mu_imap_message
 {
diff --git a/libmailutils/imapio/flags.c b/libmailutils/imapio/flags.c
index 8bee66d..5d6188a 100644
--- a/libmailutils/imapio/flags.c
+++ b/libmailutils/imapio/flags.c
@@ -60,7 +60,7 @@ mu_imap_flag_to_attribute (const char *item, int *attr)
 }
 
 int
-mu_imap_format_flags (mu_stream_t str, int flags)
+mu_imap_format_flags (mu_stream_t str, int flags, int include_recent)
 {
   int i;
   int delim = 0;
@@ -74,7 +74,7 @@ mu_imap_format_flags (mu_stream_t str, int flags)
 	delim = 1;
       }
 
-  if (MU_ATTRIBUTE_IS_UNSEEN (flags))
+  if (include_recent && MU_ATTRIBUTE_IS_UNSEEN (flags))
     {
       if (delim)
 	mu_stream_printf (str, " ");
diff --git a/libmailutils/imapio/sendflg.c b/libmailutils/imapio/sendflg.c
index b4e0d3b..94cfe22 100644
--- a/libmailutils/imapio/sendflg.c
+++ b/libmailutils/imapio/sendflg.c
@@ -29,7 +29,7 @@ mu_imapio_send_flags (struct _mu_imapio *io, int flags)
   rc = mu_stream_write (io->_imap_stream, "(", 1, NULL);
   if (rc)
     return rc;
-  rc = mu_imap_format_flags (io->_imap_stream, flags);
+  rc = mu_imap_format_flags (io->_imap_stream, flags, io->_imap_server);
   if (rc == 0)
     rc = mu_stream_write (io->_imap_stream, ")", 1, NULL);
   return rc;
diff --git a/libproto/imap/Makefile.am b/libproto/imap/Makefile.am
index e950890..bc86ba5 100644
--- a/libproto/imap/Makefile.am
+++ b/libproto/imap/Makefile.am
@@ -60,6 +60,7 @@ libmu_imap_la_SOURCES = \
  status.c\
  starttls.c\
  store.c\
+ storeflg.c\
  subscribe.c\
  tag.c\
  trace.c\
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 841942e..d7a93a6 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -83,67 +83,66 @@ struct save_closure
   struct _mu_imap_message *imsg;
 };
 
-static void
-_save_message (void *data, int code, size_t sdat, void *pdat)
+static int
+_save_message_parser (void *item, void *data)
 {
+  union mu_imap_fetch_response *resp = item;
   struct save_closure *clos = data;
-  struct _mu_imap_message *imsg = clos->imsg;
-  struct _mu_imap_mailbox *imbx = imsg->imbx;
-  mu_list_t list = pdat;
-  union mu_imap_fetch_response *resp;
-  int rc;
-  mu_stream_t istr, flt;
-  mu_off_t size;
-  
-  rc = mu_list_get (list, 0, (void**)&resp);
-  if (rc)
-    {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-		(_("cannot get response item: %s"),
-		 mu_strerror (rc)));
-      imbx->last_error = rc;
-      return;
-    }
 
-  if (resp->type != MU_IMAP_FETCH_BODY)
+  if (resp->type == MU_IMAP_FETCH_BODY)
     {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-		(_("fetch returned a not requested item %d"), resp->type));
-      imbx->last_error = MU_ERR_FAILURE;
-      return;
-    }
-  rc = mu_static_memory_stream_create (&istr, resp->body.text,
-				       strlen (resp->body.text));
-
-  if (rc)
-    {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-		(_("mu_static_memory_stream_create: %s"),
-		 mu_strerror (rc)));
-      imbx->last_error = rc;
-      return;
-    }
+      struct _mu_imap_message *imsg = clos->imsg;
+      struct _mu_imap_mailbox *imbx = imsg->imbx;
+      int rc;
+      mu_stream_t istr, flt;
+      mu_off_t size;
+  
+      rc = mu_static_memory_stream_create (&istr, resp->body.text,
+					   strlen (resp->body.text));
+      
+      if (rc)
+	{
+	  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		    (_("mu_static_memory_stream_create: %s"),
+		     mu_strerror (rc)));
+	  imbx->last_error = rc;
+	  return 0;
+	}
 
-  rc = mu_filter_create (&flt, istr, "CRLF", MU_FILTER_DECODE, MU_STREAM_READ);
-  mu_stream_unref (istr);
-  if (rc)
-    {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-		(_("mu_filter_create: %s"), mu_strerror (rc)));
-      imbx->last_error = rc;
-      return;
-    }
+      rc = mu_filter_create (&flt, istr, "CRLF", MU_FILTER_DECODE,
+			     MU_STREAM_READ);
+      mu_stream_unref (istr);
+      if (rc)
+	{
+	  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		    (_("mu_filter_create: %s"), mu_strerror (rc)));
+	  imbx->last_error = rc;
+	  return 0;
+	}
       
-  rc = mu_stream_copy (clos->save_stream, flt, 0, &size);
-  mu_stream_destroy (&flt);
-  if (rc)
-    {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
-		(_("copying to cache failed: %s"), mu_strerror (rc)));
-      imbx->last_error = rc;
-      return;
+      rc = mu_stream_copy (clos->save_stream, flt, 0, &size);
+      mu_stream_destroy (&flt);
+      if (rc)
+	{
+	  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
+		    (_("copying to cache failed: %s"), mu_strerror (rc)));
+	  imbx->last_error = rc;
+	  return 0;
+	}
+      clos->size = size;
     }
-  clos->size = size;
+  else
+    mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE0,
+	      (_("fetch returned a not requested item %d"),
+	       resp->type));
+  return 0;
+}
+
+static void
+_save_message (void *data, int code, size_t sdat, void *pdat)
+{
+  mu_list_t list = pdat;
+  mu_list_foreach (list, _save_message_parser, data);
 }
 
 static int
@@ -310,6 +309,7 @@ _imap_attr_set_flags (mu_attribute_t attr, int flags)
   if (!imsg)
     return EINVAL;
   imsg->attr_flags |= flags;
+  imsg->flags |= _MU_IMAP_MSG_ATTRCHG;
   return 0;
 }
 
@@ -321,6 +321,7 @@ _imap_attr_clr_flags (mu_attribute_t attr, int flags)
   if (!imsg)
     return EINVAL;
   imsg->attr_flags |= flags;
+  imsg->flags |= _MU_IMAP_MSG_ATTRCHG;
   return 0;
 }
 
@@ -511,6 +512,97 @@ _imap_msg_lines (mu_message_t msg, size_t *plines, int quick)
 }
 
 static int
+_copy_imapenvelope (struct mu_imapenvelope *env,
+		    struct mu_imapenvelope const *src)
+{  
+  env->date = src->date;
+  env->tz   = src->tz;
+  
+  if (src->subject && (env->subject = strdup (src->subject)) == NULL)
+    return ENOMEM;
+  if (src->from && (env->from = mu_address_dup (src->from)) == NULL)
+    return ENOMEM;
+  if (src->sender && (env->sender = mu_address_dup (src->sender)) == NULL)
+    return ENOMEM;
+  if (src->reply_to && (env->reply_to = mu_address_dup (src->reply_to))
+      == NULL)
+    return ENOMEM;
+  if (src->to && (env->to = mu_address_dup (src->to)) == NULL)
+    return ENOMEM;
+  if (src->cc && (env->cc = mu_address_dup (src->cc)) == NULL)
+    return ENOMEM;
+  if (src->bcc && (env->bcc = mu_address_dup (src->bcc)) == NULL)
+    return ENOMEM;
+  if (src->in_reply_to && (env->in_reply_to = strdup (src->in_reply_to))
+      == NULL)
+    return ENOMEM;
+  if (src->message_id && (env->message_id = strdup (src->message_id)) == NULL)
+    return ENOMEM;
+  return 0;
+}
+		   
+static int
+_imap_msg_imapenvelope (mu_message_t msg, struct mu_imapenvelope **penv)
+{
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  int rc = 0;
+  struct mu_imapenvelope *env = calloc (1, sizeof (*env));
+
+  if (!env)
+    return ENOMEM;
+  rc = _copy_imapenvelope (env, imsg->env);
+  if (rc)
+    mu_message_imapenvelope_free (env);
+  else
+    *penv = env;
+  return rc;
+}
+
+static int
+fetch_bodystructure_parser (void *item, void *data)
+{
+  union mu_imap_fetch_response *resp = item;
+  struct mu_bodystructure **pbs = data;
+
+  if (resp->type == MU_IMAP_FETCH_BODYSTRUCTURE)
+    {
+      *pbs = resp->bodystructure.bs;
+      resp->bodystructure.bs = NULL;
+    }
+  else
+    mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE0,
+	      (_("fetch returned a not requested item %d"),
+	       resp->type));
+  return 0;
+}
+
+static void
+_imap_bodystructure_callback (void *data, int code, size_t sdat, void *pdat)
+{
+  mu_list_t list = pdat;
+  mu_list_foreach (list, fetch_bodystructure_parser, data);
+}
+  
+static int
+_imap_msg_bodystructure (mu_message_t msg, struct mu_bodystructure **pbs)
+{
+  struct _mu_imap_message *imsg = mu_message_get_owner (msg);
+  struct _mu_imap_mailbox *imbx = imsg->imbx;
+  mu_imap_t imap = imbx->mbox->folder->data;
+  int rc;
+  char *msgset;
+  
+  rc = mu_asprintf (&msgset, "%lu", _imap_msg_no (imsg));
+  if (rc)
+    return rc;
+
+  rc = _imap_fetch_with_callback (imap, msgset, "BODYSTRUCTURE",
+				  _imap_bodystructure_callback, pbs);
+  free (msgset);
+  return rc;
+}
+
+static int
 _imap_mbx_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
 {
   struct _mu_imap_mailbox *imbx = mailbox->data;
@@ -535,7 +627,9 @@ _imap_mbx_get_message (mu_mailbox_t mailbox, size_t msgno, mu_message_t *pmsg)
       mu_message_set_get_stream (msg, _imap_msg_get_stream, imsg);
       mu_message_set_size (msg, _imap_msg_size, imsg);
       mu_message_set_lines (msg, _imap_msg_lines, imsg);
-
+      mu_message_set_imapenvelope (msg, _imap_msg_imapenvelope, imsg);
+      mu_message_set_bodystructure (msg, _imap_msg_bodystructure, imsg);
+      
       do
 	{
 	  rc = _imap_msg_env_setup (imsg, msg);
@@ -621,6 +715,8 @@ _imap_mbx_open (mu_mailbox_t mbox, int flags)
   mu_url_t url;
   mu_imap_t imap;
   
+  mbox->flags = flags;
+  
   rc = mu_mailbox_get_url (mbox, &url);
   if (rc)
     return rc;
@@ -727,9 +823,86 @@ _imap_uidvalidity (mu_mailbox_t mbox, unsigned long *pn)
 static int
 _imap_mbx_expunge (mu_mailbox_t mbox)
 {
+  struct _mu_imap_mailbox *imbx = mbox->data;
   mu_folder_t folder = mbox->folder;
   mu_imap_t imap = folder->data;
-  return mu_imap_expunge (imap);
+  size_t i;
+  char *msgset;
+  int rc;
+  int delflg = 0;
+  
+  for (i = 0; i < imbx->msgs_cnt; i++)
+    {
+      if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
+	{
+	  rc = mu_asprintf (&msgset, "%lu", i + 1);
+	  if (rc)
+	    break;
+	  rc = mu_imap_store_flags (imap, 0, msgset,
+				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
+				    imbx->msgs[i].attr_flags);
+	  delflg |= imbx->msgs[i].attr_flags & MU_ATTRIBUTE_DELETED;
+	  free (msgset);
+	  if (rc)
+	    break;
+	}
+    }
+
+  if (rc)
+    return rc;
+
+  if (delflg)
+    rc = mu_imap_expunge (imap);
+  
+  return rc;
+}
+
+static int
+_imap_mbx_sync (mu_mailbox_t mbox)
+{
+  struct _mu_imap_mailbox *imbx = mbox->data;
+  mu_folder_t folder = mbox->folder;
+  mu_imap_t imap = folder->data;
+  size_t i;
+  char *msgset;
+  int rc;
+  
+  if (!imbx->msgs)
+    return 0; 
+
+  for (i = 0; i < imbx->msgs_cnt; i++)
+    {
+      if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
+	{
+	  rc = mu_asprintf (&msgset, "%lu", i + 1);
+	  if (rc)
+	    break;
+	  rc = mu_imap_store_flags (imap, 0, msgset,
+				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
+				    imbx->msgs[i].attr_flags);
+	  free (msgset);
+	  if (rc)
+	    break;
+	}
+    }
+  
+  return rc;
+}
+
+static int
+_imap_mbx_append_message (mu_mailbox_t mbox, mu_message_t msg)
+{
+  int rc;
+  mu_folder_t folder = mbox->folder;
+  mu_imap_t imap = folder->data;
+  mu_url_t url;
+  const char *mbox_name;
+  
+  rc = mu_mailbox_get_url (mbox, &url);
+  if (rc)
+    return rc;
+  rc = mu_url_sget_path (url, &mbox_name);
+  return mu_imap_append_message (imap, mbox_name, 0, NULL, NULL, msg);
 }
 
 static int _compute_lines (struct mu_bodystructure *bs, size_t *pcount);
@@ -934,13 +1107,9 @@ _mu_imap_mailbox_init (mu_mailbox_t mailbox)
   mailbox->_scan = _imap_mbx_scan;
   mailbox->_is_updated = _imap_mbx_is_updated;
   mailbox->_get_message = _imap_mbx_get_message;
+  mailbox->_sync = _imap_mbx_sync;
+  
+  mailbox->_append_message = _imap_mbx_append_message;
   
-  //FIXME
-#if 0
-
-  /* Messages.  */
-  mailbox->_append_message = _mu_imap_append_message;
-
-#endif
   return 0;
 }
diff --git a/libproto/imap/login.c b/libproto/imap/storeflg.c
similarity index 62%
copy from libproto/imap/login.c
copy to libproto/imap/storeflg.c
index efdfb4b..66394d5 100644
--- a/libproto/imap/login.c
+++ b/libproto/imap/storeflg.c
@@ -18,58 +18,68 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <mailutils/errno.h>
+#include <mailutils/assoc.h>
 #include <mailutils/stream.h>
-#include <mailutils/secret.h>
+#include <mailutils/imap.h>
+#include <mailutils/imaputil.h>
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
+mu_imap_store_flags (mu_imap_t imap, int uid, const char *msgset,
+		     int op, int flags)
 {
   int status;
+  static char *cmd[] = { "FLAGS", "+FLAGS", "-FLAGS" };
   
-  if (imap == NULL)
+  if (imap == NULL || (op & MU_IMAP_STORE_OPMASK) >= MU_ARRAY_SIZE (cmd))
     return EINVAL;
   if (!imap->io)
     return MU_ERR_NO_TRANSPORT;
-  if (imap->session_state != MU_IMAP_SESSION_NONAUTH)
+  if (imap->session_state < MU_IMAP_SESSION_SELECTED)
     return MU_ERR_SEQ;
-  
+
   switch (imap->client_state)
     {
     case MU_IMAP_CLIENT_READY:
-      if (mu_imap_trace_mask (imap, MU_IMAP_TRACE_QRY, MU_XSCRIPT_SECURE))
-	_mu_imap_xscript_level (imap, MU_XSCRIPT_SECURE);
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_send_command (imap->io, imap->tag_str,
-				       "LOGIN", user, pass, NULL);
-      _mu_imap_xscript_level (imap, MU_XSCRIPT_NORMAL);
-      /* FIXME: how to obscure the passwd in the stream buffer? */
-      MU_IMAP_CHECK_EAGAIN (imap, status);
+      mu_imapio_printf (imap->io, "%s ", imap->tag_str);
+      if (uid)
+	mu_imapio_printf (imap->io, "UID ");
+      mu_imapio_printf (imap->io, "STORE %s %s", msgset,
+			cmd[op & MU_IMAP_STORE_OPMASK]);
+      if (op & MU_IMAP_STORE_SILENT)
+	mu_imapio_printf (imap->io, ".SILENT");
+      mu_imapio_send_flags (imap->io, flags);
+      mu_imapio_printf (imap->io, "\r\n");
+      status = mu_imapio_last_error (imap->io);
+      MU_IMAP_CHECK_ERROR (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
-      imap->client_state = MU_IMAP_CLIENT_LOGIN_RX;
-
-    case MU_IMAP_CLIENT_LOGIN_RX:
+      imap->client_state = MU_IMAP_CLIENT_STORE_RX;
+      
+    case MU_IMAP_CLIENT_STORE_RX:
+      /* FIXME: Handle unsolicited responses */
       status = _mu_imap_response (imap, NULL, NULL);
-      imap->client_state = MU_IMAP_CLIENT_READY;
       MU_IMAP_CHECK_EAGAIN (imap, status);
       switch (imap->resp_code)
 	{
 	case MU_IMAP_OK:
-	  imap->session_state = MU_IMAP_SESSION_AUTH;
+	  status = 0;
 	  break;
-
+	  
 	case MU_IMAP_NO:
-	  status = EACCES;
+	  status = MU_ERR_FAILURE;
 	  break;
 
 	case MU_IMAP_BAD:
 	  status = MU_ERR_BADREPLY;
 	  break;
 	}
+      imap->client_state = MU_IMAP_CLIENT_READY;
       break;
 
     default:
@@ -77,13 +87,3 @@ mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
     }
   return status;
 }
-
-int
-mu_imap_login_secret (mu_imap_t imap, const char *user, mu_secret_t secret)
-{
-  int rc = mu_imap_login (imap, user, mu_secret_password (secret));
-  mu_secret_password_unref (secret);
-  return rc;
-}
-  
-  
diff --git a/mu/imap.c b/mu/imap.c
index c280740..0069ff5 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -422,7 +422,7 @@ fetch_response_printer (void *item, void *data)
       
     case MU_IMAP_FETCH_FLAGS:
       mu_stream_printf (str, "  flags = ");
-      mu_imap_format_flags (str, resp->flags.flags);
+      mu_imap_format_flags (str, resp->flags.flags, 1);
       mu_stream_printf (str, "\n");
       break;
       
@@ -796,13 +796,13 @@ print_imap_stats (struct mu_imap_stat *st)
   if (st->flags & MU_IMAP_STAT_DEFINED_FLAGS)
     {
       mu_printf (_("Flags defined: "));
-      mu_imap_format_flags (mu_strout, st->defined_flags);
+      mu_imap_format_flags (mu_strout, st->defined_flags, 0);
       mu_printf ("\n");
     }
   if (st->flags & MU_IMAP_STAT_PERMANENT_FLAGS)
     {
       mu_printf (_("Flags permanent: "));
-      mu_imap_format_flags (mu_strout, st->permanent_flags);
+      mu_imap_format_flags (mu_strout, st->permanent_flags, 0);
       mu_printf ("\n");
     }
 


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 23 14:12:00 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1ReAXA-0000KT-Up
	for mharc-commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:12:00 -0500
Received: from eggs.gnu.org ([140.186.70.92]:56685)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReAX7-0000Jw-Ej
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:11:58 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReAX6-0006GG-4N
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:11:57 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:36190)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReAX6-0006GB-1X
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:11:56 -0500
Received: from polak by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReAX5-0006xi-SU
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 19:11:56 +0000
Date: Fri, 23 Dec 2011 19:11:55 +0000
Message-Id: <E1ReAX5-0006xi-SU@vcs.savannah.gnu.org>
From: "Wojciech Polak" <polak@gnu.org>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-549-g29d57c1
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 2b62203b38aebd3cac815de74813a0bbacb086a6
X-Git-Newrev: 29d57c1d25bc045090acf4806ec3be700718ec47
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 23 Dec 2011 19:11:58 -0000

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

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

The branch, master has been updated
       via  29d57c1d25bc045090acf4806ec3be700718ec47 (commit)
      from  2b62203b38aebd3cac815de74813a0bbacb086a6 (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 29d57c1d25bc045090acf4806ec3be700718ec47
Author: Wojciech Polak <polak@gnu.org>
Date:   Fri Dec 23 20:10:49 2011 +0100

    Update the NEWS file.

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

Summary of changes:
 NEWS |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/NEWS b/NEWS
index 101f39a..4a55342 100644
--- a/NEWS
+++ b/NEWS
@@ -126,6 +126,12 @@ See <http://mailutils.org/wiki/mu> for more information.
 
 See <http://mailutils.org/wiki/Fetching_Mail_with_Movemail>.
 
+** Support for Kyoto Cabinet DBM.
+
+Kyoto Cabinet is a library of routines for managing a database. It is
+the successor of Tokyo Cabinet. Kyoto Cabinet is a free software
+licensed under the GNU General Public License.
+
 * Configuration changes
 
 These are of particular interest to installers.


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 23 14:58:45 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1ReBGP-00073N-6q
	for mharc-commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:58:45 -0500
Received: from eggs.gnu.org ([140.186.70.92]:39541)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReBGL-00072t-P6
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:58:43 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReBGJ-0004on-Re
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:58:41 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:59701)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReBGJ-0004oj-Q7
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 14:58:39 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1ReBGJ-0007rA-C1
	for commit-mailutils@gnu.org; Fri, 23 Dec 2011 19:58:39 +0000
Date: Fri, 23 Dec 2011 19:58:39 +0000
Message-Id: <E1ReBGJ-0007rA-C1@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-550-gbabd0be
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 29d57c1d25bc045090acf4806ec3be700718ec47
X-Git-Newrev: babd0be48657946dcf3589060b707dd3cfe0844c
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 23 Dec 2011 19:58:43 -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=babd0be48657946dcf3589060b707dd3cfe0844c

The branch, master has been updated
       via  babd0be48657946dcf3589060b707dd3cfe0844c (commit)
      from  29d57c1d25bc045090acf4806ec3be700718ec47 (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 babd0be48657946dcf3589060b707dd3cfe0844c
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 23 21:58:11 2011 +0200

    Minor fixes in imap.
    
    * include/mailutils/imap.h (MU_IMAP_RESPONSE_UNKNOWN)
    (MU_IMAP_RESPONSE_TAGGED): New defines.
    * libproto/imap/mbox.c (_imap_mbx_open): Fix callback ID.
    * libproto/imap/response.c (_mu_imap_response): Call MU_IMAP_CB_BAD
    callback on tagged BAD responses as well.
    * libproto/imap/storeflg.c (mu_imap_store_flags): Insert space after
    item name.

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

Summary of changes:
 include/mailutils/imap.h |   25 ++++++++++++++-----------
 libproto/imap/mbox.c     |    2 +-
 libproto/imap/response.c |    6 ++++++
 libproto/imap/storeflg.c |    1 +
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 2e8e371..09dad70 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -202,17 +202,20 @@ void mu_imap_register_callback_function (mu_imap_t imap, int code,
 					 mu_imap_callback_t callback,
 					 void *data);
 
-#define MU_IMAP_RESPONSE_ALERT            0
-#define MU_IMAP_RESPONSE_BADCHARSET       1
-#define MU_IMAP_RESPONSE_CAPABILITY       2
-#define MU_IMAP_RESPONSE_PARSE            3
-#define MU_IMAP_RESPONSE_PERMANENTFLAGS   4
-#define MU_IMAP_RESPONSE_READ_ONLY        5     
-#define MU_IMAP_RESPONSE_READ_WRITE       6
-#define MU_IMAP_RESPONSE_TRYCREATE        7
-#define MU_IMAP_RESPONSE_UIDNEXT          8
-#define MU_IMAP_RESPONSE_UIDVALIDITY      9
-#define MU_IMAP_RESPONSE_UNSEEN          10
+#define MU_IMAP_RESPONSE_TAGGED  	   0x10
+
+#define MU_IMAP_RESPONSE_UNKNOWN           0 
+#define MU_IMAP_RESPONSE_ALERT             1
+#define MU_IMAP_RESPONSE_BADCHARSET        2
+#define MU_IMAP_RESPONSE_CAPABILITY        3
+#define MU_IMAP_RESPONSE_PARSE             4
+#define MU_IMAP_RESPONSE_PERMANENTFLAGS    5
+#define MU_IMAP_RESPONSE_READ_ONLY         6   
+#define MU_IMAP_RESPONSE_READ_WRITE        7
+#define MU_IMAP_RESPONSE_TRYCREATE         8
+#define MU_IMAP_RESPONSE_UIDNEXT           9
+#define MU_IMAP_RESPONSE_UIDVALIDITY      10
+#define MU_IMAP_RESPONSE_UNSEEN           11
 
 extern struct mu_kwd mu_imap_response_codes[];  
 
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index d7a93a6..934551a 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -735,7 +735,7 @@ _imap_mbx_open (mu_mailbox_t mbox, int flags)
   mu_imap_register_callback_function (imap, MU_IMAP_CB_RECENT_COUNT,
 				      _imap_update_callback,
 				      imbx);
-  mu_imap_register_callback_function (imap, MU_IMAP_STAT_MESSAGE_COUNT,
+  mu_imap_register_callback_function (imap, MU_IMAP_CB_MESSAGE_COUNT,
 				      _imap_update_callback,
 				      imbx);
   
diff --git a/libproto/imap/response.c b/libproto/imap/response.c
index c74f656..fa9652d 100644
--- a/libproto/imap/response.c
+++ b/libproto/imap/response.c
@@ -97,6 +97,12 @@ _mu_imap_response (mu_imap_t imap, mu_imap_response_action_t fun,
 		{
 		  imap->resp_code = MU_IMAP_BAD;
 		  response_to_errstr (imap, wc, wv);
+		  /* This may be so important that CB_BAD callback is
+		   * overloaded to handle this case as well.
+		   */
+		  mu_imap_callback (imap, MU_IMAP_CB_BAD,
+				    MU_IMAP_RESPONSE_TAGGED,
+				    wc >= 1 ? wv[wc-1] : NULL);
 		}
 	      else
 		status = MU_ERR_BADREPLY;
diff --git a/libproto/imap/storeflg.c b/libproto/imap/storeflg.c
index 66394d5..eba59e4 100644
--- a/libproto/imap/storeflg.c
+++ b/libproto/imap/storeflg.c
@@ -54,6 +54,7 @@ mu_imap_store_flags (mu_imap_t imap, int uid, const char *msgset,
 			cmd[op & MU_IMAP_STORE_OPMASK]);
       if (op & MU_IMAP_STORE_SILENT)
 	mu_imapio_printf (imap->io, ".SILENT");
+      mu_imapio_printf (imap->io, " ");
       mu_imapio_send_flags (imap->io, flags);
       mu_imapio_printf (imap->io, "\r\n");
       status = mu_imapio_last_error (imap->io);


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 26 03:49:05 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rf6Ez-00063B-8x
	for mharc-commit-mailutils@gnu.org; Mon, 26 Dec 2011 03:49:05 -0500
Received: from eggs.gnu.org ([140.186.70.92]:39532)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rf6Ew-000635-Cy
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 03:49:03 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rf6Eu-0007T0-HU
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 03:49:02 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:56008)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rf6Eu-0007Sv-EU
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 03:49:00 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rf6Eu-0006B9-14
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 08:49:00 +0000
Date: Mon, 26 Dec 2011 08:49:00 +0000
Message-Id: <E1Rf6Eu-0006B9-14@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-551-gd16a3de
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: babd0be48657946dcf3589060b707dd3cfe0844c
X-Git-Newrev: d16a3de50d8c43f7b45f18db1704fa784ee4cb7b
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 26 Dec 2011 08:49: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=d16a3de50d8c43f7b45f18db1704fa784ee4cb7b

The branch, master has been updated
       via  d16a3de50d8c43f7b45f18db1704fa784ee4cb7b (commit)
      from  babd0be48657946dcf3589060b707dd3cfe0844c (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 d16a3de50d8c43f7b45f18db1704fa784ee4cb7b
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sun Dec 25 11:00:12 2011 +0200

    imap mbox: minor improvements.
    
    * libproto/imap/mbox.c: Add some debugging messages.
    (_imap_mbx_gensync): New function shared among _imap_mbx_sync and
    _imap_mbx_expunge. It aggregates message numbers with same flags
    to separate message sets, thereby reducing the number of STORE
    commands sent to the server.

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

Summary of changes:
 libproto/imap/mbox.c |  240 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 199 insertions(+), 41 deletions(-)

diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 934551a..609c86c 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -157,7 +157,9 @@ __imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
   if (!(imsg->flags & _MU_IMAP_MSG_CACHED))
     {
       char *msgset;
-	  
+      
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+		(_("caching message %lu"), (unsigned long) msgno));
       if (!imbx->cache)
 	{
 	  rc = mu_temp_file_stream_create (&imbx->cache, NULL, 0);
@@ -183,7 +185,14 @@ __imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
 					  _save_message, &clos);
 	  free (msgset);
 	  if (rc == 0 && !_imap_mbx_errno (imbx))
-	    imsg->message_size = clos.size;
+	    {
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+			(_("cached message %lu: offset=%lu, size=%lu"),
+			 (unsigned long) msgno,
+			 (unsigned long) imsg->offset,
+			 (unsigned long) clos.size));
+	      imsg->message_size = clos.size;
+	    }
 	}
 
       if (rc)
@@ -360,6 +369,9 @@ _imap_hdr_fill (void *data, char **pbuf, size_t *plen)
   rc = mu_asprintf (&msgset, "%lu", msgno);
   if (rc == 0)
     {
+      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+		(_("message %lu: reading headers"),
+		 (unsigned long) msgno));
       rc = _imap_fetch_with_callback (imap, msgset, "BODY.PEEK[HEADER]",
 				      _save_message, &clos);
       free (msgset);
@@ -720,6 +732,8 @@ _imap_mbx_open (mu_mailbox_t mbox, int flags)
   rc = mu_mailbox_get_url (mbox, &url);
   if (rc)
     return rc;
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+	    (_("opening mailbox %s"), mu_url_to_string (url)));
   rc = mu_url_sget_path (url, &mbox_name);
   if (rc == MU_ERR_NOENT)
     mbox_name = "INBOX";
@@ -758,6 +772,8 @@ _imap_mbx_close (mu_mailbox_t mbox)
   mu_folder_t folder = mbox->folder;
   mu_imap_t imap = folder->data;
 
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+	    (_("closing mailbox %s"), mu_url_to_string (mbox->url)));
   if (mu_imap_capability_test (imap, "UNSELECT", NULL) == 0)
     rc = mu_imap_unselect (imap);
   else
@@ -820,76 +836,216 @@ _imap_uidvalidity (mu_mailbox_t mbox, unsigned long *pn)
   return 0;
 }
 
+struct attr_tab
+{
+  size_t start;
+  size_t end;
+  int attr_flags;
+};
+
 static int
-_imap_mbx_expunge (mu_mailbox_t mbox)
+attr_tab_cmp (void const *a, void const *b)
 {
-  struct _mu_imap_mailbox *imbx = mbox->data;
-  mu_folder_t folder = mbox->folder;
-  mu_imap_t imap = folder->data;
-  size_t i;
-  char *msgset;
-  int rc;
-  int delflg = 0;
+  struct attr_tab const *ta = a;
+  struct attr_tab const *tb = b;
+
+  if (ta->attr_flags < tb->attr_flags)
+    return -1;
+  else if (ta->attr_flags > tb->attr_flags)
+    return 1;
+
+  if (ta->start < tb->start)
+    return -1;
+  else if (ta->start > tb->start)
+    return 1;
+  return 0;
+} 
+
+static int
+aggregate_attributes (struct _mu_imap_mailbox *imbx,
+		      struct attr_tab **ptab, size_t *pcnt)
+{
+  size_t i, j;
+  size_t count;
+  struct attr_tab *tab;
   
+  /* Pass 1: Count modified attributes */
+  count = 0;
   for (i = 0; i < imbx->msgs_cnt; i++)
     {
       if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
+	count++;
+    }
+
+  if (count == 0)
+    {
+      *ptab = NULL;
+      *pcnt = 0;
+      return 0;
+    }
+  
+  /* Pass 2: Create and populate expanded array */
+  tab = calloc (count, sizeof (*tab));
+  if (!tab)
+    return ENOMEM;
+  for (i = j = 0; i < imbx->msgs_cnt; i++)
+    {
+      if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
 	{
-	  rc = mu_asprintf (&msgset, "%lu", i + 1);
-	  if (rc)
-	    break;
-	  rc = mu_imap_store_flags (imap, 0, msgset,
-				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
-				    imbx->msgs[i].attr_flags);
-	  delflg |= imbx->msgs[i].attr_flags & MU_ATTRIBUTE_DELETED;
-	  free (msgset);
-	  if (rc)
-	    break;
+	  tab[j].start = tab[j].end = i;
+	  tab[j].attr_flags = imbx->msgs[i].attr_flags;
+	  j++;
 	}
     }
+  
+  /* Sort the array */
+  qsort (tab, count, sizeof (tab[0]), attr_tab_cmp);
 
-  if (rc)
-    return rc;
+  /* Pass 3: Coalesce message ranges */
+  for (i = j = 0; i < count; i++)
+    {
+      if (i == j)
+	continue;
+      else if ((tab[i].attr_flags == tab[j].attr_flags) &&
+	       (tab[i].start == tab[j].end + 1))
+	tab[j].end++;
+      else
+	tab[++j] = tab[i];
+    }
 
-  if (delflg)
-    rc = mu_imap_expunge (imap);
-  
+  *ptab = tab;
+  *pcnt = j + 1;
+  return 0;
+}
+
+static int
+flush_attributes (mu_imap_t imap, mu_stream_t str, int attr_flags)
+{
+  int rc;
+  mu_transport_t trans[2];
+		      
+  mu_stream_write (str, "", 1, NULL);
+  if (mu_stream_err (str))
+    return mu_stream_last_error (str);
+
+  rc = mu_stream_ioctl (str, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans);
+  if (rc == 0)
+    rc = mu_imap_store_flags (imap, 0, (char*)trans[0],
+			      MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
+			      attr_flags);
   return rc;
 }
 
 static int
-_imap_mbx_sync (mu_mailbox_t mbox)
+_imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
 {
   struct _mu_imap_mailbox *imbx = mbox->data;
   mu_folder_t folder = mbox->folder;
   mu_imap_t imap = folder->data;
-  size_t i;
+  size_t i, j;
   char *msgset;
   int rc;
+  int delflg = 0;
+  struct attr_tab *tab;
+  size_t count;
   
-  if (!imbx->msgs)
-    return 0; 
-
-  for (i = 0; i < imbx->msgs_cnt; i++)
+  rc = aggregate_attributes (imbx, &tab, &count);
+  if (rc)
     {
-      if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
+      /* Too bad, but try to use naive approach */
+      for (i = 0; i < imbx->msgs_cnt; i++)
 	{
-	  rc = mu_asprintf (&msgset, "%lu", i + 1);
-	  if (rc)
-	    break;
-	  rc = mu_imap_store_flags (imap, 0, msgset,
-				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
-				    imbx->msgs[i].attr_flags);
-	  free (msgset);
-	  if (rc)
-	    break;
+	  if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
+	    {
+	      rc = mu_asprintf (&msgset, "%lu", i + 1);
+	      if (rc)
+		break;
+	      rc = mu_imap_store_flags (imap, 0, msgset,
+					MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
+					imbx->msgs[i].attr_flags);
+	      delflg |= imbx->msgs[i].attr_flags & MU_ATTRIBUTE_DELETED;
+	      free (msgset);
+	      if (rc)
+		break;
+	    }
 	}
     }
+  else
+    {
+      mu_stream_t str;
+
+      rc = mu_memory_stream_create (&str, MU_STREAM_RDWR);
+      if (rc == 0)
+	{
+	  for (i = j = 0; i < count; i++)
+	    {
+	      if (j < i)
+		{
+		  if (tab[j].attr_flags != tab[i].attr_flags)
+		    {
+		      rc = flush_attributes (imap, str, tab[j].attr_flags);
+		      delflg |= tab[j].attr_flags & MU_ATTRIBUTE_DELETED;
+		      if (rc)
+			break;
+		      mu_stream_truncate (str, 0);
+		      j = i;
+		    }
+		  else
+		    mu_stream_printf (str, ",");
+		}
+	      if (tab[i].end == tab[i].start)
+		mu_stream_printf (str, "%lu", (unsigned long)tab[i].start+1);
+	      else
+		mu_stream_printf (str, "%lu:%lu",
+				  (unsigned long)tab[i].start+1,
+				  (unsigned long)tab[i].end+1);
+	    }
+
+	  if (j < i)
+	    {
+	      rc = flush_attributes (imap, str, tab[j].attr_flags);
+	      delflg |= tab[j].attr_flags & MU_ATTRIBUTE_DELETED;
+	    }
+	  mu_stream_unref (str);
+	}
+      free (tab);
+    }
+
+  if (rc)
+    return rc;
+
+  if (pdel)
+    *pdel = delflg;
+  
+  return 0;
+}
+
+static int
+_imap_mbx_expunge (mu_mailbox_t mbox)
+{
+  int rc, del = 0;
   
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+	    (_("expunging mailbox %s"), mu_url_to_string (mbox->url)));
+  rc = _imap_mbx_gensync (mbox, &del);
+  if (rc == 0 && del)
+    {
+      mu_folder_t folder = mbox->folder;
+      mu_imap_t imap = folder->data;
+      rc = mu_imap_expunge (imap);
+    }
   return rc;
 }
 
 static int
+_imap_mbx_sync (mu_mailbox_t mbox)
+{
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+	    (_("synchronizing mailbox %s"), mu_url_to_string (mbox->url)));
+  return _imap_mbx_gensync (mbox, NULL);
+}
+
+static int
 _imap_mbx_append_message (mu_mailbox_t mbox, mu_message_t msg)
 {
   int rc;
@@ -1030,6 +1186,8 @@ _imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
   int rc;
   static char _imap_scan_items[] = "(UID FLAGS ENVELOPE RFC822.SIZE BODY)";
   
+  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+	    (_("scanning mailbox %s"), mu_url_to_string (mbox->url)));
   rc = mu_asprintf (&msgset, "%lu:*", (unsigned long) msgno);
   if (rc)
     return rc;


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Mon Dec 26 16:17:19 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RfHv5-00070t-Lv
	for mharc-commit-mailutils@gnu.org; Mon, 26 Dec 2011 16:17:19 -0500
Received: from eggs.gnu.org ([140.186.70.92]:39550)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfHuv-00070J-9s
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 16:17:16 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfHum-0004Cc-Nv
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 16:17:09 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:37956)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfHum-0004CY-IV
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 16:17:00 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfHul-0005mz-6j
	for commit-mailutils@gnu.org; Mon, 26 Dec 2011 21:17:00 +0000
Date: Mon, 26 Dec 2011 21:16:59 +0000
Message-Id: <E1RfHul-0005mz-6j@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-555-g37fc6c3
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: d16a3de50d8c43f7b45f18db1704fa784ee4cb7b
X-Git-Newrev: 37fc6c3c41b16823e9b130a2a425ed40eebf09f8
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 26 Dec 2011 21:17:16 -0000

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

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

The branch, master has been updated
       via  37fc6c3c41b16823e9b130a2a425ed40eebf09f8 (commit)
       via  0ff01881ca8dd159c2802751901644daef2bcc19 (commit)
       via  1c7d50a62de57fbb0552bcde955db928908b2a98 (commit)
       via  e072a2734eaf1bffdacfc20fcb598d79a50d11e7 (commit)
      from  d16a3de50d8c43f7b45f18db1704fa784ee4cb7b (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 37fc6c3c41b16823e9b130a2a425ed40eebf09f8
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 26 23:11:00 2011 +0200

    Use mu_msgset_t in imap4d.
    
    * imap4d/util.c (util_parse_msgset): Remove. Use mu_msgset_parse_imap.
    All uses changed.
    (util_foreach_message): Change first argument to mu_msgset_t.
    All uses changed.
    * imap4d/imap4d.h: Likewise.
    
    * imap4d/copy.c: Use mu_msgset_t
    * imap4d/fetch.c: Likewise.
    * imap4d/search.c: Likewise.
    * imap4d/store.c: Likewise.

commit 0ff01881ca8dd159c2802751901644daef2bcc19
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 26 22:13:52 2011 +0200

    Use mu_msgset_t in imap client for FETCH, STORE and COPY.
    
    * include/mailutils/imap.h (mu_imap_fetch,mu_imap_store)
    (mu_imap_store_flags,mu_imap_copy): Take mu_msgset_t as the
    message set parameter.
    * include/mailutils/imapio.h (mu_imapio_send_msgset): New proto.
    (mu_imapio_send_command_v,mu_imapio_send_command)
    (mu_imapio_send_command_e): Take additional mu_msgset_t parameter.
    All uses changed.
    * include/mailutils/msgset.h (mu_msgset_parse_imap): Input string
    is const.
    * include/mailutils/sys/imap.h (imap_command) <msgset>: New member.
    * libmailutils/imapio/sendmsgset.c: New file.
    * libmailutils/imapio/Makefile.am (libimapio_la_SOURCES): Add sendmsgset.c.
    * libmailutils/imapio/sendcmd.c (mu_imapio_send_command): Take additional
    mu_msgset_t parameter.
    * libmailutils/imapio/sendcmde.c (mu_imapio_send_command_e): Likewise.
    * libmailutils/imapio/sendcmdv.c (mu_imapio_send_command_v): Likewise.
    * libmailutils/msgset/parse.c (mu_msgset_parse_imap): Input string
    is const.
    * libproto/imap/copy.c (mu_imap_copy): Take mu_msgset_t as the
    message set parameter.
    * libproto/imap/fetch.c (mu_imap_fetch): Likewise.
    * libproto/imap/store.c (mu_imap_store): Likewise.
    * libproto/imap/storeflg.c (mu_imap_store_flags): Likewise.
    * libproto/imap/gencom.c (mu_imap_gencom): Send cmd->msgset.

commit 1c7d50a62de57fbb0552bcde955db928908b2a98
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 26 20:34:43 2011 +0200

    Bugfix.
    
    * libmailutils/mailbox/mailbox.c (_MBOX_CHECK_FLAGS): Fix return code.

commit e072a2734eaf1bffdacfc20fcb598d79a50d11e7
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Mon Dec 26 14:06:57 2011 +0200

    Add functions for operations on message sets.
    
    * configure.ac: Build libmailutils/msgset/Makefile
    * include/mailutils/msgset.h: New file.
    * include/mailutils/Makefile.am (pkginclude_HEADERS): Add msgset.h
    * include/mailutils/mailutils.h: Include mailutils/msgset.h
    * include/mailutils/sys/msgset.h: New file.
    * include/mailutils/sys/Makefile.am (sysinclude_HEADERS): Add msgset.h
    * include/mailutils/types.hin (mu_msgset_t): New type.
    * libmailutils/Makefile.am (SUBDIRS): Add msgset.
    (libmailutils_la_LIBADD): Add libmsgset.la
    * libmailutils/msgset/Makefile.am: New file.
    * libmailutils/msgset/add.c: New file.
    * libmailutils/msgset/aggr.c: New file.
    * libmailutils/msgset/clear.c: New file.
    * libmailutils/msgset/create.c: New file.
    * libmailutils/msgset/free.c: New file.
    * libmailutils/msgset/getitr.c: New file.
    * libmailutils/msgset/getlist.c: New file.
    * libmailutils/msgset/locate.c: New file.
    * libmailutils/msgset/parse.c: New file.
    * libmailutils/msgset/print.c: New file.
    * libmailutils/msgset/sub.c: New file.
    * libmailutils/tests/msgset.at: New file.
    * libmailutils/tests/msgset.c: New file.
    * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add msgset.
    (TESTSUITE_AT): Add msgset.at.
    * libmailutils/tests/testsuite.at: Include msgset.at.
    * testsuite/msgset.c: New file.
    * testsuite/Makefile.am (noinst_PROGRAMS): Add msgset.

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

Summary of changes:
 configure.ac                                       |    1 +
 imap4d/copy.c                                      |   34 ++-
 imap4d/fetch.c                                     |   20 +-
 imap4d/imap4d.h                                    |    6 +-
 imap4d/search.c                                    |   98 +++++---
 imap4d/store.c                                     |   18 +-
 imap4d/util.c                                      |  280 +-------------------
 include/mailutils/Makefile.am                      |    1 +
 include/mailutils/datetime.h                       |    8 +
 include/mailutils/imap.h                           |    8 +-
 include/mailutils/imapio.h                         |    6 +-
 include/mailutils/mailutils.h                      |    1 +
 include/mailutils/msgset.h                         |   61 +++++
 include/mailutils/sys/Makefile.am                  |    1 +
 include/mailutils/sys/imap.h                       |    1 +
 .../close.c => include/mailutils/sys/msgset.h      |   24 +-
 include/mailutils/types.hin                        |    4 +-
 libmailutils/Makefile.am                           |    3 +-
 libmailutils/imapio/Makefile.am                    |    1 +
 libmailutils/imapio/sendcmd.c                      |   13 +-
 libmailutils/imapio/sendcmde.c                     |   12 +-
 libmailutils/imapio/sendcmdv.c                     |   12 +-
 libmailutils/imapio/{send.c => sendmsgset.c}       |    6 +-
 libmailutils/mailbox/mailbox.c                     |    2 +-
 libmailutils/{sockaddr => msgset}/Makefile.am      |   21 +-
 libmailutils/{imapio/sendflg.c => msgset/add.c}    |   29 ++-
 libmailutils/msgset/aggr.c                         |  169 ++++++++++++
 libmailutils/{imapio/send.c => msgset/clear.c}     |   17 +-
 libmailutils/msgset/create.c                       |   60 +++++
 libmailutils/{imapio/time.c => msgset/free.c}      |   32 ++-
 libmailutils/{imapio/send.c => msgset/getitr.c}    |   15 +-
 libmailutils/{imapio/send.c => msgset/getlist.c}   |   18 +-
 libmailutils/{imapio/time.c => msgset/locate.c}    |   18 +-
 libmailutils/msgset/parse.c                        |  179 +++++++++++++
 libmailutils/msgset/print.c                        |   74 +++++
 libmailutils/msgset/sub.c                          |  233 ++++++++++++++++
 libmailutils/tests/Makefile.am                     |    2 +
 libmailutils/tests/msgset.at                       |  175 ++++++++++++
 libmailutils/tests/msgset.c                        |  125 +++++++++
 libmailutils/tests/testsuite.at                    |    2 +
 libproto/imap/capability.c                         |    2 +-
 libproto/imap/close.c                              |    1 +
 libproto/imap/copy.c                               |    5 +-
 libproto/imap/delete.c                             |    1 +
 libproto/imap/fetch.c                              |    5 +-
 libproto/imap/gencom.c                             |    1 +
 libproto/imap/genlist.c                            |    1 +
 libproto/imap/login.c                              |    2 +-
 libproto/imap/mbcreate.c                           |    3 +-
 libproto/imap/mbox.c                               |  206 ++++++++-------
 libproto/imap/rename.c                             |    1 +
 libproto/imap/select.c                             |    2 +-
 libproto/imap/store.c                              |    5 +-
 libproto/imap/storeflg.c                           |    7 +-
 libproto/imap/subscribe.c                          |    1 +
 libproto/imap/unselect.c                           |    1 +
 libproto/imap/unsubscribe.c                        |    1 +
 mu/imap.c                                          |   76 ++++--
 testsuite/Makefile.am                              |    1 +
 testsuite/msgset.c                                 |  135 ++++++++++
 60 files changed, 1683 insertions(+), 564 deletions(-)
 create mode 100644 include/mailutils/msgset.h
 copy libmu_dbm/close.c => include/mailutils/sys/msgset.h (70%)
 copy libmailutils/imapio/{send.c => sendmsgset.c} (86%)
 copy libmailutils/{sockaddr => msgset}/Makefile.am (85%)
 copy libmailutils/{imapio/sendflg.c => msgset/add.c} (62%)
 create mode 100644 libmailutils/msgset/aggr.c
 copy libmailutils/{imapio/send.c => msgset/clear.c} (76%)
 create mode 100644 libmailutils/msgset/create.c
 copy libmailutils/{imapio/time.c => msgset/free.c} (70%)
 copy libmailutils/{imapio/send.c => msgset/getitr.c} (74%)
 copy libmailutils/{imapio/send.c => msgset/getlist.c} (73%)
 copy libmailutils/{imapio/time.c => msgset/locate.c} (72%)
 create mode 100644 libmailutils/msgset/parse.c
 create mode 100644 libmailutils/msgset/print.c
 create mode 100644 libmailutils/msgset/sub.c
 create mode 100644 libmailutils/tests/msgset.at
 create mode 100644 libmailutils/tests/msgset.c
 create mode 100644 testsuite/msgset.c

diff --git a/configure.ac b/configure.ac
index 9472fb7..8f1cbeb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1479,6 +1479,7 @@ AC_CONFIG_FILES([
  libmailutils/mailbox/Makefile
  libmailutils/mailer/Makefile
  libmailutils/mime/Makefile
+ libmailutils/msgset/Makefile
  libmailutils/property/Makefile
  libmailutils/server/Makefile
  libmailutils/string/Makefile
diff --git a/imap4d/copy.c b/imap4d/copy.c
index 5e60487..f2c2314 100644
--- a/imap4d/copy.c
+++ b/imap4d/copy.c
@@ -124,7 +124,7 @@ do_copy (size_t msgno, void *data)
 }
 
 static int
-try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
+try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
 	  char **err_text)
 {
   int rc;
@@ -139,7 +139,7 @@ try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
   *env.err_text = "Operation failed";
 
   /* Check size */
-  rc = util_foreach_message (msglist, size_sum, &env);
+  rc = util_foreach_message (msgset, size_sum, &env);
   if (rc)
     return RESP_NO;
   if (env.ret != RESP_OK)
@@ -151,7 +151,7 @@ try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
       return RESP_NO;
     }
   env.total = 0;
-  rc = util_foreach_message (msglist, do_copy, &env);
+  rc = util_foreach_message (msgset, do_copy, &env);
   quota_update (env.total);
   if (rc)
     return RESP_NO;
@@ -159,7 +159,7 @@ try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
 }
   
 static int
-safe_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
+safe_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
 	   char **err_text)
 {
   size_t nmesg;
@@ -174,7 +174,7 @@ safe_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_list_t msglist,
       return RESP_NO;
     }
 
-  status = try_copy (dst, src, msglist, err_text);
+  status = try_copy (dst, src, msgset, err_text);
   if (status != RESP_OK)
     {
       size_t maxmesg;
@@ -228,8 +228,8 @@ int
 imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 {
   int status;
-  char *msgset;
-  mu_list_t msglist;
+  char *msgset_str;
+  mu_msgset_t msgset;
   char *name;
   char *mailbox_name;
   char *end;
@@ -244,12 +244,19 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
       return 1;
     }
   
-  msgset = imap4d_tokbuf_getarg (tok, arg);
+  msgset_str = imap4d_tokbuf_getarg (tok, arg);
   name = imap4d_tokbuf_getarg (tok, arg + 1);
-  /* Get the message numbers in set[].  */
-  status = util_parse_msgset (msgset, isuid, mbox, &msglist, &end);
+  status = mu_msgset_create (&msgset, mbox, isuid ? MU_MSGSET_UID : 0);
+  if (!status)
+    {
+      *err_text = "Software error";
+      return RESP_BAD;
+    }
+    
+  status = mu_msgset_parse_imap (msgset, msgset_str, &end);
   if (status)
     {
+      mu_msgset_free (msgset);
       *err_text = "Error parsing message set";
       /* FIXME: print error location */
       return RESP_BAD;
@@ -259,6 +266,7 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 
   if (!mailbox_name)
     {
+      mu_msgset_free (msgset);
       *err_text = "Copy failed";
       return RESP_NO;
     }
@@ -272,13 +280,15 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
       status = mu_mailbox_open (cmbox, MU_STREAM_RDWR | mailbox_mode[ns]);
       if (status == 0)
 	{
+	  mu_list_t msglist;
+	  mu_msgset_get_list (msgset, &msglist);
 	  if (!mu_list_is_empty (msglist))
-	    status = safe_copy (cmbox, mbox, msglist, err_text);
+	    status = safe_copy (cmbox, mbox, msgset, err_text);
 	  mu_mailbox_close (cmbox);
 	}
       mu_mailbox_destroy (&cmbox);
     }
-  mu_list_destroy (&msglist);
+  mu_msgset_free (msgset);
 
   if (status == 0)
     {
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index ad42ff3..46cb835 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -64,7 +64,7 @@ struct fetch_parse_closure
 {
   int isuid;
   mu_list_t fnlist;
-  mu_list_t msgnumlist;
+  mu_msgset_t msgset;
 };
 
 
@@ -1753,18 +1753,22 @@ static int
 fetch_thunk (imap4d_parsebuf_t pb)
 {
   int status;
-  char *msgset;
+  char *mstr;
   char *end;
   struct fetch_parse_closure *pclos = imap4d_parsebuf_data (pb);
   
-  msgset = imap4d_parsebuf_next (pb, 1);
+  mstr = imap4d_parsebuf_next (pb, 1);
 
+  status = mu_msgset_create (&pclos->msgset, mbox,
+			     pclos->isuid ? MU_MSGSET_UID : 0);
+  if (status)
+    imap4d_parsebuf_exit (pb, "Software error");
+  
   /* Parse sequence numbers. */
-  status = util_parse_msgset (msgset, pclos->isuid, mbox,
-			      &pclos->msgnumlist, &end);
+  status = mu_msgset_parse_imap (pclos->msgset, mstr, &end);
   if (status)
     imap4d_parsebuf_exit (pb, "Failed to parse message set");
-
+  
   /* Compile the expression */
 
   /* Server implementations MUST implicitly
@@ -1829,12 +1833,12 @@ imap4d_fetch0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 	 loop below */
       frc.err_text = "Completed";
 
-      util_foreach_message (pclos.msgnumlist, _fetch_from_message, &frc);
+      util_foreach_message (pclos.msgset, _fetch_from_message, &frc);
       mu_list_destroy (&frc.msglist);
     }
   
   mu_list_destroy (&pclos.fnlist);
-  mu_list_destroy (&pclos.msgnumlist);
+  mu_msgset_free (pclos.msgset);
   return rc;
 }
 
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index f652c1a..22b3b28 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -107,6 +107,7 @@
 #include <mailutils/prog.h>
 #include <mailutils/imapio.h>
 #include <mailutils/imaputil.h>
+#include <mailutils/msgset.h>
 
 #include <mu_umaxtostr.h>
 #include <muaux.h>
@@ -371,12 +372,9 @@ extern char *util_getfullpath (const char *);
 extern struct imap4d_command *util_getcommand (char *, 
                                                struct imap4d_command []);
 
-extern int util_parse_msgset (char *s, int isuid, mu_mailbox_t mbx,
-			      mu_list_t *plist, char **end);
-
 typedef int (*imap4d_message_action_t) (size_t, void *);
 
-int util_foreach_message (mu_list_t list, imap4d_message_action_t action,
+int util_foreach_message (mu_msgset_t list, imap4d_message_action_t action,
 			  void *data);
 
 enum datetime_parse_mode     /* how to parse date/time strings */
diff --git a/imap4d/search.c b/imap4d/search.c
index 54c212e..2441810 100644
--- a/imap4d/search.c
+++ b/imap4d/search.c
@@ -62,7 +62,7 @@ struct value
     char *string;
     mu_off_t number;
     time_t date;
-    mu_list_t msgset;
+    mu_msgset_t msgset;
   } v;
 };
 
@@ -198,11 +198,12 @@ struct cond_equiv equiv_list[] =
 };
 
 /* A memory allocation chain used to keep track of objects allocated during
-   the recursive-descent parsing. */
+   the recursive-descend parsing. */
 struct mem_chain
 {
   struct mem_chain *next;
   void *mem;
+  void (*free_fun) (void *);
 };
 
 /* Code and stack sizes for execution of compiled search statement */
@@ -233,7 +234,7 @@ struct parsebuf
 };
 
 static void parse_free_mem (struct parsebuf *pb);
-static void *parse_regmem (struct parsebuf *pb, void *mem);
+static void *parse_regmem (struct parsebuf *pb, void *mem, void (*f)(void*));
 static char *parse_strdup (struct parsebuf *pb, char *s);
 static void *parse_alloc (struct parsebuf *pb, size_t size);
 static struct search_node *parse_search_key_list (struct parsebuf *pb);
@@ -385,7 +386,10 @@ parse_free_mem (struct parsebuf *pb)
   while (alloc)
     {
       next = alloc->next;
-      free (alloc->mem);
+      if (alloc->free_fun)
+	alloc->free_fun (alloc->mem);
+      else
+	free (alloc->mem);
       free (alloc);
       alloc = next;
     }
@@ -393,7 +397,7 @@ parse_free_mem (struct parsebuf *pb)
 
 /* Register a memory pointer mem with the parsebuf */
 void *
-parse_regmem (struct parsebuf *pb, void *mem)
+parse_regmem (struct parsebuf *pb, void *mem, void (*f) (void*))
 {
   struct mem_chain *mp;
 
@@ -403,6 +407,7 @@ parse_regmem (struct parsebuf *pb, void *mem)
   mp->next = pb->alloc;
   pb->alloc = mp;
   mp->mem = mem;
+  mp->free_fun = f;
   return mem;
 }
 
@@ -413,7 +418,7 @@ parse_alloc (struct parsebuf *pb, size_t size)
   void *p = malloc (size);
   if (!p)
     imap4d_bye (ERR_NO_MEM);
-  return parse_regmem (pb, p);
+  return parse_regmem (pb, p, NULL);
 }
 
 /* Create a copy of the string. */ 
@@ -423,18 +428,34 @@ parse_strdup (struct parsebuf *pb, char *s)
   s = strdup (s);
   if (!s)
     imap4d_bye (ERR_NO_MEM);
-  return parse_regmem (pb, s);
+  return parse_regmem (pb, s, NULL);
+}
+
+static void
+free_msgset (void *ptr)
+{
+  mu_msgset_free (ptr);
+}
+
+mu_msgset_t
+parse_msgset_create (struct parsebuf *pb, mu_mailbox_t mbox, int flags)
+{
+  mu_msgset_t msgset;
+
+  if (mu_msgset_create (&msgset, mbox, flags))
+    imap4d_bye (ERR_NO_MEM);
+  return parse_regmem (pb, msgset, free_msgset);
 }
 
 /* A recursive-descent parser for the following grammar:
    search_key_list : search_key
-                   | search_key_list search_key
-                   ;
+	           | search_key_list search_key
+		   ;
 
    search_key      : simple_key
-                   | NOT simple_key
-                   | OR simple_key simple_key
-                   | '(' search_key_list ')'
+	           | NOT simple_key
+		   | OR simple_key simple_key
+		   | '(' search_key_list ')'
 		   ;
 */
 
@@ -445,7 +466,7 @@ struct search_node *
 parse_search_key_list (struct parsebuf *pb)
 {
   struct search_node *leftarg = NULL;
-  
+
   while (pb->token && pb->token[0] != ')')
     {
       struct search_node *rightarg = parse_search_key (pb);
@@ -478,7 +499,7 @@ parse_search_key (struct parsebuf *pb)
       node = parse_search_key_list (pb);
       if (!node)
 	return NULL;
-	
+      
       if (strcmp (pb->token, ")"))
 	{
 	  pb->err_mesg = "Unbalanced parenthesis";
@@ -500,30 +521,30 @@ parse_search_key (struct parsebuf *pb)
   else if (mu_c_strcasecmp (pb->token, "NOT") == 0)
     {
       struct search_node *np;
-      
+
       if (parse_gettoken (pb, 1) == 0)
-	return NULL;
+	 return NULL;
 
       np = parse_search_key (pb);
       if (!np)
-	return NULL;
+	 return NULL;
 
       node = parse_alloc (pb, sizeof *node);
       node->type = node_not;
       node->v.arg[0] = np;
-      
+
       return node;
     }
   else if (mu_c_strcasecmp (pb->token, "OR") == 0)
     {
       struct search_node *leftarg, *rightarg;
-      
+
       if (parse_gettoken (pb, 1) == 0)
-	return NULL;
+	 return NULL;
 
       if ((leftarg = parse_search_key (pb)) == NULL
-	  || (rightarg = parse_search_key (pb)) == NULL)
-	return NULL;
+	   || (rightarg = parse_search_key (pb)) == NULL)
+	 return NULL;
 
       node = parse_alloc (pb, sizeof *node);
       node->type = node_or;
@@ -543,9 +564,9 @@ parse_equiv_key (struct parsebuf *pb)
   struct cond_equiv *condp;
   int save_arg;
   imap4d_tokbuf_t save_tok;
-  
+
   for (condp = equiv_list; condp->name && mu_c_strcasecmp (condp->name, pb->token);
-       condp++)
+	condp++)
     ;
 
   if (!condp->name)
@@ -563,11 +584,11 @@ parse_equiv_key (struct parsebuf *pb)
     {
       /* shouldn't happen? */
       mu_diag_output (MU_DIAG_CRIT, _("%s:%d: INTERNAL ERROR (please report)"),
-		      __FILE__, __LINE__);
+		       __FILE__, __LINE__);
       abort (); 
     }
   imap4d_tokbuf_destroy (&pb->tok);
-  
+
   pb->arg = save_arg;
   pb->tok = save_tok;
   parse_gettoken (pb, 0);
@@ -580,21 +601,22 @@ parse_simple_key (struct parsebuf *pb)
   struct search_node *node;
   struct cond *condp;
   time_t time;
-  
+
   for (condp = condlist; condp->name && mu_c_strcasecmp (condp->name, pb->token);
        condp++)
     ;
-
+  
   if (!condp->name)
     {
-      mu_list_t msglist;
+      mu_msgset_t msgset = parse_msgset_create (pb, mbox,
+						pb->isuid ? MU_MSGSET_UID : 0);
       
-      if (util_parse_msgset (pb->token, pb->isuid, mbox, &msglist, NULL) == 0) 
+      if (mu_msgset_parse_imap (msgset, pb->token, NULL) == 0) 
 	{
 	  struct search_node *np = parse_alloc (pb, sizeof *np);
 	  np->type = node_value;
 	  np->v.value.type = value_msgset;
-	  np->v.value.v.msgset = msglist;
+	  np->v.value.v.msgset = msgset;
 	  
 	  node = parse_alloc (pb, sizeof *node);
 	  node->type = node_call;
@@ -602,7 +624,7 @@ parse_simple_key (struct parsebuf *pb)
 	  node->v.key.narg = 1;
 	  node->v.key.arg[0] = np;
 	  node->v.key.fun = cond_msgset;
-
+	  
 	  parse_gettoken (pb, 0);
 	  
 	  return node;
@@ -613,7 +635,7 @@ parse_simple_key (struct parsebuf *pb)
 	  return NULL;
 	}
     }
-
+  
   node = parse_alloc (pb, sizeof *node);
   node->type = node_call;
   node->v.key.keyword = condp->name;
@@ -674,9 +696,11 @@ parse_simple_key (struct parsebuf *pb)
 	      break;
 	      
 	    case 'u': /* UID message set */
-	      if (util_parse_msgset (pb->token, 0, NULL,
-				     &arg->v.value.v.msgset, NULL)) 
+	      arg->v.value.v.msgset = parse_msgset_create (pb, NULL, 0);
+	      if (mu_msgset_parse_imap (arg->v.value.v.msgset, pb->token,
+					NULL)) 
 		{
+		  mu_msgset_free (arg->v.value.v.msgset);
 		  pb->err_mesg = "Bogus number set";
 		  return NULL;
 		}
@@ -853,7 +877,7 @@ static void
 cond_msgset (struct parsebuf *pb, struct search_node *node, struct value *arg,
 	     struct value *retval)
 {
-  int rc = mu_list_locate (arg[0].v.msgset, &pb->msgno, NULL);
+  int rc = mu_msgset_locate (arg[0].v.msgset, pb->msgno, NULL);
   retval->type = value_number;
   retval->v.number = rc == 0;
 }
@@ -1075,7 +1099,7 @@ cond_uid (struct parsebuf *pb, struct search_node *node, struct value *arg,
   size_t uid = 0;
   
   mu_message_get_uid (pb->msg, &uid);
-  rc = mu_list_locate (arg[0].v.msgset, &pb->msgno, NULL);
+  rc = mu_msgset_locate (arg[0].v.msgset, pb->msgno, NULL);
   retval->type = value_number;
   retval->v.number = rc == 0;
 }                      
diff --git a/imap4d/store.c b/imap4d/store.c
index 20318b5..0fe425d 100644
--- a/imap4d/store.c
+++ b/imap4d/store.c
@@ -25,19 +25,19 @@ struct store_parse_closure
   int ack;
   int type;
   int isuid;
-  mu_list_t msgnumlist;
+  mu_msgset_t msgset;
 };
   
 static int
 store_thunk (imap4d_parsebuf_t p)
 {
   struct store_parse_closure *pclos = imap4d_parsebuf_data (p);
-  char *msgset;
+  char *mstr;
   char *data;
   int status;
   char *end;
   
-  msgset = imap4d_parsebuf_next (p, 1);
+  mstr = imap4d_parsebuf_next (p, 1);
   data = imap4d_parsebuf_next (p, 1);
 
   if (*data == '+')
@@ -69,9 +69,13 @@ store_thunk (imap4d_parsebuf_t p)
 	imap4d_parsebuf_exit (p, "Bogus data suffix");
     }
 
+  status = mu_msgset_create (&pclos->msgset, mbox,
+			     pclos->isuid ? MU_MSGSET_UID : 0);
+  if (status)
+    imap4d_parsebuf_exit (p, "Software error");
+  
   /* Get the message numbers in set[].  */
-  status = util_parse_msgset (msgset, pclos->isuid, mbox,
-			      &pclos->msgnumlist, &end);
+  status = mu_msgset_parse_imap (pclos->msgset, mstr, &end);
   if (status)
     imap4d_parsebuf_exit (p, "Failed to parse message set");
 
@@ -150,12 +154,12 @@ imap4d_store0 (imap4d_tokbuf_t tok, int isuid, char **ptext)
 			     ptext);
   if (rc == RESP_OK)
     {
-      util_foreach_message (pclos.msgnumlist, _do_store, &pclos);
+      util_foreach_message (pclos.msgset, _do_store, &pclos);
     
       *ptext = "Completed";
     }
   
-  mu_list_destroy (&pclos.msgnumlist);
+  mu_msgset_free (pclos.msgset);
   
   return rc;
 }
diff --git a/imap4d/util.c b/imap4d/util.c
index 643d962..c5341de 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -39,279 +39,6 @@ util_getfullpath (const char *name)
   return mu_normalize_path (exp);
 }
 
-/* A message set is defined as:
-
-   set ::= sequence_num / (sequence_num ":" sequence_num) / (set "," set)
-   sequence_num    ::= nz_number / "*"
-   ;; * is the largest number in use.  For message
-   ;; sequence numbers, it is the number of messages
-   ;; in the mailbox.  For unique identifiers, it is
-   ;; the unique identifier of the last message in
-   ;; the mailbox.
-   nz_number       ::= digit_nz *digit
-
-   Non-existing sequence numbers are ignored, except when they form part
-   of a message range (sequence_num ":" sequence_num), in which case they
-   are treated as minimal or maximal sequence numbers (uids) available in
-   the mailbox depending on whether they appear to the left or to the right
-   of ":".
-*/
-
-/* Message set is a list of non-overlapping msgranges, in ascending
-   order.  No matter what command flavor is used, msg_beg and msg_end are
-   treated as sequence numbers (not UIDs). */
-struct msgrange
-{
-  size_t msg_beg;           /* beginning message number */
-  size_t msg_end;           /* ending message number */
-};
-
-/* Comparator function used to sort the message set list. */
-static int
-compare_msgrange (const void *a, const void *b)
-{
-  struct msgrange const *sa = a;
-  struct msgrange const *sb = b;
-  
-  if (sa->msg_beg < sb->msg_beg)
-    return -1;
-  if (sa->msg_beg > sb->msg_beg)
-    return 1;
-  if (sa->msg_end < sb->msg_end)
-    return -1;
-  if (sa->msg_end > sb->msg_end)
-    return 1;
-  return 0;
-}
-
-/* Comparator function used to locate a message in the list.  Second argument
-   (b) is a pointer to the message number. */
-static int
-compare_msgnum (const void *a, const void *b)
-{
-  struct msgrange const *range = a;
-  size_t msgno = *(size_t*)b;
-
-  if (range->msg_beg <= msgno && msgno <= range->msg_end)
-    return 0;
-  return 1;
-}
-
-/* This structure keeps parser state while parsing message set. */
-struct parse_msgnum_env
-{
-  char *s;               /* Current position in string */
-  size_t minval;         /* Min. sequence number or UID */
-  size_t maxval;         /* Max. sequence number or UID */
-  mu_list_t list;        /* List being built. */
-  int isuid:1;           /* True, if parsing an UID command. */
-  mu_mailbox_t mailbox;  /* Reference mailbox (can be NULL). */
-};
-
-/* Get a single message number/UID from env->s and store it into *PN.
-   Return 0 on success and error code on error.
-
-   Advance env->s to the point past the parsed message number.
- */
-static int
-get_msgnum (struct parse_msgnum_env *env, size_t *pn)
-{
-  size_t msgnum;
-  char *p;
-  
-  errno = 0;
-  msgnum = strtoul (env->s, &p, 10);
-  if (msgnum == ULONG_MAX && errno == ERANGE)
-    return MU_ERR_PARSE;
-  env->s = p;
-  if (msgnum > env->maxval)
-    msgnum = env->maxval;
-  *pn = msgnum;
-  return 0;
-}
-
-/* Parse a single message range (A:B). Treat '*' as the largest number/UID
-   in use. */
-static int
-parse_msgrange (struct parse_msgnum_env *env)
-{
-  int rc;
-  struct msgrange msgrange, *mp;
-  
-  if (*env->s == '*')
-    {
-      msgrange.msg_beg = env->maxval;
-      env->s++;
-    }
-  else if ((rc = get_msgnum (env, &msgrange.msg_beg)))
-    return rc;
-
-  if (*env->s == ':')
-    {
-      if (*++env->s == '*')
-	{
-	  msgrange.msg_end = env->maxval;
-	  ++env->s;
-	}
-      else if (*env->s == 0)
-	return MU_ERR_PARSE;
-      else if ((rc = get_msgnum (env, &msgrange.msg_end)))
-	return rc;
-    }
-  else
-    msgrange.msg_end = msgrange.msg_beg;
-
-  if (msgrange.msg_end < msgrange.msg_beg)
-    {
-      size_t tmp = msgrange.msg_end;
-      msgrange.msg_end = msgrange.msg_beg;
-      msgrange.msg_beg = tmp;
-    }
-
-  if (env->isuid && env->mailbox)
-    {
-      int rc;
-
-      rc = mu_mailbox_translate (env->mailbox,
-				 MU_MAILBOX_UID_TO_MSGNO,
-				 msgrange.msg_beg, &msgrange.msg_beg);
-      if (rc == MU_ERR_NOENT)
-	msgrange.msg_beg = env->minval;
-      else if (rc)
-	return rc;
-      
-      rc = mu_mailbox_translate (env->mailbox,
-				 MU_MAILBOX_UID_TO_MSGNO,
-				 msgrange.msg_end, &msgrange.msg_end);
-      if (rc == MU_ERR_NOENT)
-	msgrange.msg_end = env->maxval;
-      else if (rc)
-	return rc;
-    }      
-
-  mp = malloc (sizeof (*mp));
-  if (!mp)
-    return ENOMEM;
-
-  *mp = msgrange;
-
-  rc = mu_list_append (env->list, mp);
-  if (rc)
-    free (mp);
-  return rc;
-}
-
-/* Parse message set specification S. ISUID indicates if the set is supposed
-   to contain UIDs (they will be translated to message sequence numbers).
-   MBX is a reference mailbox or NULL.
-
-   On success, return 0 and place the resulting set into *PLIST. On error,
-   return error code and point END to the position in the input string where
-   the parsing failed. */
-int
-util_parse_msgset (char *s, int isuid, mu_mailbox_t mbx,
-		   mu_list_t *plist, char **end)
-{
-  int rc;
-  struct parse_msgnum_env env;
-  size_t n;
-  
-  if (!s)
-    return EINVAL;
-  if (!*s)
-    return MU_ERR_PARSE;
-
-  memset (&env, 0, sizeof (env));
-  env.s = s;
-  if (mbox)
-    {
-      size_t lastmsgno;      /* Max. sequence number. */
-
-      rc = mu_mailbox_messages_count (mbx, &lastmsgno);
-      if (rc == 0)
-	{
-	  if (isuid)
-	    {
-	      rc = mu_mailbox_translate (mbox, MU_MAILBOX_MSGNO_TO_UID,
-					 lastmsgno, &env.maxval);
-	      if (rc == 0)
-		rc = mu_mailbox_translate (mbox, MU_MAILBOX_MSGNO_TO_UID,
-					   1, &env.minval);
-	    }
-	  else
-	    env.maxval = lastmsgno;
-	}
-      if (rc)
-	return rc;
-    }
-  rc = mu_list_create (&env.list);
-  if (rc)
-    return rc;
-  mu_list_set_destroy_item (env.list, mu_list_free_item);
-  
-  env.isuid = isuid;
-  env.mailbox = mbx;
-  
-  while ((rc = parse_msgrange (&env)) == 0 && *env.s)
-    {
-      if (*env.s != ',' || *++env.s == 0)
-	{
-	  rc = MU_ERR_PARSE;
-	  break;
-	}
-    }
-
-  if (end)
-    *end = env.s;
-  if (rc)
-    {
-      mu_list_destroy (&env.list);
-      return rc;
-    }
-
-  mu_list_count (env.list, &n);
-  if (n > 1)
-    {
-      mu_iterator_t itr;
-      struct msgrange *last = NULL;
-
-      /* Sort the list and coalesce overlapping message ranges. */
-      mu_list_sort (env.list, compare_msgrange);
-
-      mu_list_get_iterator (env.list, &itr);
-      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-	   mu_iterator_next (itr))
-	{
-	  struct msgrange *msgrange;
-
-	  mu_iterator_current (itr, (void**)&msgrange);
-	  if (last)
-	    {
-	      if (last->msg_beg <= msgrange->msg_beg &&
-		  msgrange->msg_beg <= last->msg_end)
-		{
-		  if (msgrange->msg_end > last->msg_end)
-		    last->msg_end = msgrange->msg_end;
-		  mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
-		  continue;
-		}
-		  
-	    }
-	  last = msgrange;
-	}
-      mu_iterator_destroy (&itr);
-    }
-
-  if (rc == 0)
-    {
-      mu_list_set_comparator (env.list, compare_msgnum);
-      *plist = env.list;
-    }
-  else
-    mu_list_destroy (&env.list);
-  return rc;
-}
-
 struct action_closure
 {
   imap4d_message_action_t action;
@@ -321,7 +48,7 @@ struct action_closure
 static int
 procrange (void *item, void *data)
 {
-  struct msgrange *mp = item;
+  struct mu_msgrange *mp = item;
   struct action_closure *clos = data;
   size_t i;
 
@@ -336,12 +63,15 @@ procrange (void *item, void *data)
 
 /* Apply ACTION to each message number from LIST. */
 int
-util_foreach_message (mu_list_t list, imap4d_message_action_t action,
+util_foreach_message (mu_msgset_t msgset, imap4d_message_action_t action,
 		      void *data)
 {
+  mu_list_t list;
   struct action_closure clos;
+  
   clos.action = action;
   clos.data = data;
+  mu_msgset_get_list (msgset, &list);
   return mu_list_foreach (list, procrange, &clos);
 }
 
diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am
index 53ea5e4..d8e46ac 100644
--- a/include/mailutils/Makefile.am
+++ b/include/mailutils/Makefile.am
@@ -73,6 +73,7 @@ pkginclude_HEADERS = \
  mh.h\
  mime.h\
  monitor.h\
+ msgset.h\
  mu_auth.h\
  util.h\
  nls.h\
diff --git a/include/mailutils/datetime.h b/include/mailutils/datetime.h
index bc1aa98..dbf1447 100644
--- a/include/mailutils/datetime.h
+++ b/include/mailutils/datetime.h
@@ -18,6 +18,10 @@
 #ifndef _MAILUTILS_DATETIME_H
 #define _MAILUTILS_DATETIME_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #include <time.h>
 #include <mailutils/types.h>
 
@@ -83,4 +87,8 @@ int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm,
 #define MU_DATETIME_FORM_RFC822  "%a, %e %b %Y %H:%M:%S %z"
 #define MU_DATETIME_SCAN_RFC822  "%[%a, %]%e %b %Y %H:%M%[:%S%] %z"
 
+#ifdef __cplusplus
+}
+#endif
+  
 #endif
diff --git a/include/mailutils/imap.h b/include/mailutils/imap.h
index 09dad70..200656c 100644
--- a/include/mailutils/imap.h
+++ b/include/mailutils/imap.h
@@ -66,9 +66,9 @@ int mu_imap_id (mu_imap_t imap, char **idenv, mu_assoc_t *passoc);
 int mu_imap_noop (mu_imap_t imap);
 int mu_imap_check (mu_imap_t imap);
 
-int mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset,
+int mu_imap_fetch (mu_imap_t imap, int uid, mu_msgset_t msgset,
 		   const char *items);
-int mu_imap_store (mu_imap_t imap, int uid, const char *msgset,
+int mu_imap_store (mu_imap_t imap, int uid, mu_msgset_t msgset,
 		   const char *items);
 
 #define MU_IMAP_STORE_SET 0
@@ -78,13 +78,13 @@ int mu_imap_store (mu_imap_t imap, int uid, const char *msgset,
 
 #define MU_IMAP_STORE_OPMASK 0xf
 
-int mu_imap_store_flags (mu_imap_t imap, int uid, const char *msgset,
+int mu_imap_store_flags (mu_imap_t imap, int uid, mu_msgset_t msgset,
 			 int op, int flags);
   
 int mu_imap_delete (mu_imap_t imap, const char *mailbox);
 int mu_imap_rename (mu_imap_t imap, const char *mailbox,
 		    const char *new_mailbox);
-int mu_imap_copy (mu_imap_t imap, int uid, const char *msgset,
+int mu_imap_copy (mu_imap_t imap, int uid, mu_msgset_t msgset,
 		  const char *mailbox);
 
 int mu_imap_close (mu_imap_t imap);
diff --git a/include/mailutils/imapio.h b/include/mailutils/imapio.h
index d038744..dbd5e5d 100644
--- a/include/mailutils/imapio.h
+++ b/include/mailutils/imapio.h
@@ -44,13 +44,15 @@ int mu_imapio_send_literal_stream (mu_imapio_t io, mu_stream_t stream,
 int mu_imapio_send_qstring (mu_imapio_t io, const char *buffer);
 int mu_imapio_send_qstring_unfold (mu_imapio_t io, const char *buffer,
 				   int unfold);
+int mu_imapio_send_msgset (mu_imapio_t io, mu_msgset_t msgset);
 
 int mu_imapio_send_command_v (mu_imapio_t io, const char *tag,
+			      mu_msgset_t msgset,
 			      int argc, char const **argv, const char *extra);
 int mu_imapio_send_command (mu_imapio_t io, const char *tag,
-			char const *cmd, ...);
+			    mu_msgset_t msgset, char const *cmd, ...);
 int mu_imapio_send_command_e (mu_imapio_t io, const char *tag,
-			      char const *cmd, ...);
+			      mu_msgset_t msgset, char const *cmd, ...);
   
 int mu_imapio_send_flags (mu_imapio_t io, int flags);
 int mu_imapio_send_time (mu_imapio_t io, struct tm *tm,
diff --git a/include/mailutils/mailutils.h b/include/mailutils/mailutils.h
index eb2a616..b562136 100644
--- a/include/mailutils/mailutils.h
+++ b/include/mailutils/mailutils.h
@@ -70,5 +70,6 @@
 #include <mailutils/prog.h>
 #include <mailutils/sockaddr.h>
 #include <mailutils/cidr.h>
+#include <mailutils/msgset.h>
 
 /* EOF */
diff --git a/include/mailutils/msgset.h b/include/mailutils/msgset.h
new file mode 100644
index 0000000..f9ae5b2
--- /dev/null
+++ b/include/mailutils/msgset.h
@@ -0,0 +1,61 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General
+   Public License along with this library.  If not, see
+   <http://www.gnu.org/licenses/>. */
+
+#ifndef _MAILUTILS_MSGSET_H
+#define _MAILUTILS_MSGSET_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct mu_msgrange
+{
+  size_t msg_beg;
+  size_t msg_end;
+};
+
+/* Message numbers start with 1.  MU_MSGNO_LAST denotes the last
+   message. */
+#define MU_MSGNO_LAST   0
+  
+#define MU_MSGSET_UID   0x01   /* Message set operates on UIDs */
+  
+int mu_msgset_create (mu_msgset_t *pmsgset, mu_mailbox_t mbox, int flags);
+int mu_msgset_get_list (mu_msgset_t msgset, mu_list_t *plist);
+int mu_msgset_get_iterator (mu_msgset_t msgset, mu_iterator_t *pitr);
+
+int mu_msgset_add_range (mu_msgset_t list, size_t beg, size_t end);
+int mu_msgset_sub_range (mu_msgset_t list, size_t beg, size_t end);
+  /*int mu_msgset_add_set (mu_msgset_t a, mu_msgset_t b);*/
+  /*int mu_msgset_sub_set (mu_msgset_t a, mu_msgset_t b);*/
+int mu_msgset_aggregate (mu_msgset_t set);
+int mu_msgset_clear (mu_msgset_t set);
+void mu_msgset_free (mu_msgset_t set);
+void mu_msgset_destroy (mu_msgset_t *set);
+  
+int mu_msgset_parse_imap (mu_msgset_t set, const char *s, char **end);
+
+int mu_msgset_print (mu_stream_t str, mu_msgset_t msgset);
+  
+int mu_msgset_locate (mu_msgset_t msgset, size_t n,
+		      struct mu_msgrange const **prange);
+  
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/mailutils/sys/Makefile.am b/include/mailutils/sys/Makefile.am
index 86412d0..27e2fe4 100644
--- a/include/mailutils/sys/Makefile.am
+++ b/include/mailutils/sys/Makefile.am
@@ -44,6 +44,7 @@ sysinclude_HEADERS = \
  message.h\
  mime.h\
  monitor.h\
+ msgset.h\
  nntp.h\
  nullstream.h\
  observer.h\
diff --git a/include/mailutils/sys/imap.h b/include/mailutils/sys/imap.h
index fd0f905..995673a 100644
--- a/include/mailutils/sys/imap.h
+++ b/include/mailutils/sys/imap.h
@@ -151,6 +151,7 @@ struct imap_command
   int argc;
   char const **argv;
   char const *extra;
+  mu_msgset_t msgset;
   void (*tagged_handler) (mu_imap_t);
   mu_imap_response_action_t untagged_handler;
   void *untagged_handler_data;
diff --git a/libmu_dbm/close.c b/include/mailutils/sys/msgset.h
similarity index 70%
copy from libmu_dbm/close.c
copy to include/mailutils/sys/msgset.h
index a84574a..9ecb053 100644
--- a/libmu_dbm/close.c
+++ b/include/mailutils/sys/msgset.h
@@ -15,21 +15,17 @@
    Public License along with this library.  If not, see
    <http://www.gnu.org/licenses/>. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#ifndef _MAILUTILS_SYS_MSGSET_H
+# define _MAILUTILS_SYS_MSGSET_H
 
-#include <mailutils/types.h>
-#include <mailutils/dbm.h>
-#include <mailutils/errno.h>
-#include "mudbm.h"
+#define _MU_MSGSET_AGGREGATED 0x10
+#define _MU_MSGSET_USERFLAG_MASK 0x0f
 
-int
-mu_dbm_close (mu_dbm_file_t db)
+struct _mu_msgset
 {
-  DBMSYSCK (db, _dbm_close);
-  if (!db->db_descr)
-    return 0;
-  return db->db_sys->_dbm_close (db);
-}
+  mu_list_t list;           /* List of mu_msgrange structures */
+  mu_mailbox_t mbox;        /* Associated mailbox */
+  int flags;                /* Message set flags */
+};
 
+#endif
diff --git a/include/mailutils/types.hin b/include/mailutils/types.hin
index b98d617..bda2c37 100644
--- a/include/mailutils/types.hin
+++ b/include/mailutils/types.hin
@@ -77,7 +77,8 @@ struct _mu_server;
 struct _mu_tcp_server;
 struct _mu_dbm_file;
 struct _mu_imapio;
-  
+struct _mu_msgset;
+
 struct mu_sockaddr; /* defined in mailutils/sockaddr.h */  
 struct mu_cidr;     /* defined in mailutils/cidr.h */  
   
@@ -125,6 +126,7 @@ typedef struct _mu_secret *mu_secret_t;
 typedef struct _mu_mime_io_buffer *mu_mime_io_buffer_t;
 typedef struct _mu_dbm_file *mu_dbm_file_t;
 typedef struct _mu_imapio *mu_imapio_t;
+typedef struct _mu_msgset *mu_msgset_t;
 
 typedef void (*mu_onexit_t) (void*);
 typedef unsigned int mu_debug_handle_t;
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index c01b321..bbc8447 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -18,7 +18,7 @@
 
 SUBDIRS = \
  auth base address list sockaddr cidr cfg diag\
- filter mailbox mailer mime server string stream stdstream\
+ filter mailbox mailer mime msgset server string stream stdstream\
  property url imapio datetime . tests 
 
 lib_LTLIBRARIES = libmailutils.la
@@ -41,6 +41,7 @@ libmailutils_la_LIBADD = \
  mailbox/libmailbox.la\
  mailer/libmailer.la\
  mime/libmime.la\
+ msgset/libmsgset.la\
  property/libproperty.la\
  server/libserver.la\
  string/libstring.la\
diff --git a/libmailutils/imapio/Makefile.am b/libmailutils/imapio/Makefile.am
index beb590b..93644a9 100644
--- a/libmailutils/imapio/Makefile.am
+++ b/libmailutils/imapio/Makefile.am
@@ -32,6 +32,7 @@ libimapio_la_SOURCES = \
  sendcmdv.c\
  sendcmde.c\
  sendflg.c\
+ sendmsgset.c\
  time.c\
  trace.c\
  transport.c\
diff --git a/libmailutils/imapio/sendcmd.c b/libmailutils/imapio/sendcmd.c
index bf4c5f6..3d25a04 100644
--- a/libmailutils/imapio/sendcmd.c
+++ b/libmailutils/imapio/sendcmd.c
@@ -23,10 +23,14 @@
 
 /* Send a IMAP command to the server, quoting its arguments as necessary.
    TAG is the command tag, CMD is the command verb.  Command arguments are
-   given in variadic list terminated with NULL. */
+   given in variadic list terminated with NULL.
+
+   If MSGSET is supplied, it is sent when the function encounters the
+   "\\" (single backslash) in ARGV.
+*/
 int
 mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
-			char const *cmd, ...)
+			mu_msgset_t msgset, char const *cmd, ...)
 {
   va_list ap;
   
@@ -35,7 +39,10 @@ mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
   while ((cmd = va_arg (ap, char *)))
     {
       mu_imapio_send (io, " ", 1);
-      mu_imapio_send_qstring (io, cmd);
+      if (msgset && strcmp (cmd, "\\") == 0)
+	mu_imapio_send_msgset (io, msgset);
+      else
+	mu_imapio_send_qstring (io, cmd);
     }
   va_end (ap);
   mu_imapio_send (io, "\r\n", 2);
diff --git a/libmailutils/imapio/sendcmde.c b/libmailutils/imapio/sendcmde.c
index 1618c52..5069f6c 100644
--- a/libmailutils/imapio/sendcmde.c
+++ b/libmailutils/imapio/sendcmde.c
@@ -24,9 +24,12 @@
 /* Send a IMAP command to the server, quoting its arguments as necessary.
    TAG is the command tag, CMD is the command verb.  Command arguments are
    given in variadic list terminated with NULL. The last argument is sent
-   over the wire as is, without quoting. */
+   over the wire as is, without quoting. If MSGSET is supplied, it is sent
+   when the function encounters the "\\" (single backslash) in ARGV.
+*/
 int
 mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
+			  mu_msgset_t msgset,
 			  char const *cmd, ...)
 {
   va_list ap;
@@ -40,7 +43,12 @@ mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
       
       mu_imapio_send (io, " ", 1);
       if (next)
-	mu_imapio_send_qstring (io, cmd);
+	{
+	  if (msgset && strcmp (cmd, "\\") == 0)
+	    mu_imapio_send_msgset (io, msgset);
+	  else
+	    mu_imapio_send_qstring (io, cmd);
+	}
       else
 	mu_imapio_send (io, cmd, strlen (cmd));
       cmd = next;
diff --git a/libmailutils/imapio/sendcmdv.c b/libmailutils/imapio/sendcmdv.c
index af9fa8d..ef1bcc2 100644
--- a/libmailutils/imapio/sendcmdv.c
+++ b/libmailutils/imapio/sendcmdv.c
@@ -23,9 +23,14 @@
 /* Send a IMAP command to the server, quoting its arguments as necessary.
    TAG is the command tag, ARGV contains ARGC elements and supplies the
    command (in ARGV[0]) and its arguments. EXTRA (if not NULL) supplies
-   additional arguments that will be sent as is. */
+   additional arguments that will be sent as is.
+
+   If MSGSET is supplied, it is sent when the function encounters the
+   "\\" (single backslash) in ARGV.
+*/
 int
 mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
+			  mu_msgset_t msgset,
 			  int argc, char const **argv, const char *extra)
 {
   int i;
@@ -34,7 +39,10 @@ mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
   for (i = 1; i < argc; i++)
     {
       mu_imapio_send (io, " ", 1);
-      mu_imapio_send_qstring (io, argv[i]);
+      if (msgset && strcmp (argv[i], "\\") == 0)
+	mu_imapio_send_msgset (io, msgset);
+      else
+	mu_imapio_send_qstring (io, argv[i]);
     }
   if (extra)
     {
diff --git a/libmailutils/imapio/send.c b/libmailutils/imapio/sendmsgset.c
similarity index 86%
copy from libmailutils/imapio/send.c
copy to libmailutils/imapio/sendmsgset.c
index 11ee049..7f6cd78 100644
--- a/libmailutils/imapio/send.c
+++ b/libmailutils/imapio/sendmsgset.c
@@ -18,12 +18,12 @@
 #include <stdarg.h>
 #include <mailutils/types.h>
 #include <mailutils/imapio.h>
+#include <mailutils/msgset.h>
 #include <mailutils/stream.h>
 #include <mailutils/sys/imapio.h>
 
 int
-mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes)
+mu_imapio_send_msgset (mu_imapio_t io, mu_msgset_t msgset)
 {
-  return mu_stream_write (io->_imap_stream, buf, bytes, NULL);
+  return mu_msgset_print (io->_imap_stream, msgset);
 }
-    
diff --git a/libmailutils/mailbox/mailbox.c b/libmailutils/mailbox/mailbox.c
index 990ae6a..68a4e5a 100644
--- a/libmailutils/mailbox/mailbox.c
+++ b/libmailutils/mailbox/mailbox.c
@@ -448,7 +448,7 @@ mu_mailbox_flush (mu_mailbox_t mbox, int expunge)
   if (mbox->flags & _MU_MAILBOX_REMOVED)	\
     return MU_ERR_MBX_REMOVED;			\
   if (!(mbox->flags & _MU_MAILBOX_OPEN))	\
-    return _MU_MAILBOX_OPEN
+    return MU_ERR_NOT_OPEN
 
 #define _MBOX_CHECK(mbox,method)		\
   _MBOX_CHECK_FLAGS(mbox);			\
diff --git a/libmailutils/sockaddr/Makefile.am b/libmailutils/msgset/Makefile.am
similarity index 85%
copy from libmailutils/sockaddr/Makefile.am
copy to libmailutils/msgset/Makefile.am
index dcfb184..398806d 100644
--- a/libmailutils/sockaddr/Makefile.am
+++ b/libmailutils/msgset/Makefile.am
@@ -15,17 +15,20 @@
 # Public License along with this library.  If not, see
 # <http://www.gnu.org/licenses/>.
 
-noinst_LTLIBRARIES = libsockaddr.la
+noinst_LTLIBRARIES = libmsgset.la
 
-libsockaddr_la_SOURCES = \
- copy.c\
+libmsgset_la_SOURCES = \
+ add.c\
+ aggr.c\
+ clear.c\
  create.c\
+ getitr.c\
+ getlist.c\
  free.c\
- fromnode.c\
- insert.c\
- ipaddr.c\
- str.c\
- unlink.c\
- url.c
+ locate.c\
+ parse.c\
+ print.c\
+ sub.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
+
diff --git a/libmailutils/imapio/sendflg.c b/libmailutils/msgset/add.c
similarity index 62%
copy from libmailutils/imapio/sendflg.c
copy to libmailutils/msgset/add.c
index 94cfe22..9d97707 100644
--- a/libmailutils/imapio/sendflg.c
+++ b/libmailutils/msgset/add.c
@@ -15,22 +15,29 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
+#include <stdlib.h>
 #include <mailutils/types.h>
-#include <mailutils/imapio.h>
-#include <mailutils/stream.h>
-#include <mailutils/imaputil.h>
-#include <mailutils/sys/imapio.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
 
 int
-mu_imapio_send_flags (struct _mu_imapio *io, int flags)
+mu_msgset_add_range (mu_msgset_t mset, size_t beg, size_t end)
 {
   int rc;
-
-  rc = mu_stream_write (io->_imap_stream, "(", 1, NULL);
+  struct mu_msgrange *range;
+  
+  if (!mset || beg == 0)
+    return EINVAL;
+  range = calloc (1, sizeof (*range));
+  if (!range)
+    return ENOMEM;
+  range->msg_beg = beg;
+  range->msg_end = end;
+  rc = mu_list_append (mset->list, range);
   if (rc)
-    return rc;
-  rc = mu_imap_format_flags (io->_imap_stream, flags, io->_imap_server);
-  if (rc == 0)
-    rc = mu_stream_write (io->_imap_stream, ")", 1, NULL);
+    free (range);
+  mset->flags &= ~_MU_MSGSET_AGGREGATED;
   return rc;
 }
diff --git a/libmailutils/msgset/aggr.c b/libmailutils/msgset/aggr.c
new file mode 100644
index 0000000..a6fe7aa
--- /dev/null
+++ b/libmailutils/msgset/aggr.c
@@ -0,0 +1,169 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+/* Comparator function for sorting the message set list. */
+static int
+compare_msgrange (const void *a, const void *b)
+{
+  struct mu_msgrange const *sa = a;
+  struct mu_msgrange const *sb = b;
+  
+  if (sa->msg_end != sb->msg_end)
+    {
+      if (sa->msg_end == MU_MSGNO_LAST)
+	return 1;
+      if (sb->msg_end == MU_MSGNO_LAST)
+	return -1;
+    }
+  
+  if (sa->msg_beg < sb->msg_beg)
+    return -1;
+  if (sa->msg_beg > sb->msg_beg)
+    return 1;
+
+  if (sa->msg_end == sb->msg_end)
+    return 0;
+
+  if (sa->msg_end < sb->msg_end)
+    return -1;
+  return 1;
+}
+
+int
+mu_msgset_aggregate (mu_msgset_t mset)
+{
+  int rc;
+  mu_iterator_t itr;
+  size_t count;
+  struct mu_msgrange *prev = NULL, *mr;
+  int dir;
+  
+  if (!mset)
+    return EINVAL;
+
+  if (mset->flags & _MU_MSGSET_AGGREGATED)
+    return 0; /* nothing to do */
+  
+  rc = mu_list_count (mset->list, &count);
+  if (rc)
+    return rc;
+  if (count < 2)
+    return 0;
+  
+  mu_list_sort (mset->list, compare_msgrange);
+  
+  rc = mu_list_get_iterator (mset->list, &itr);
+  if (rc)
+    return rc;
+  /* Set backward direction */
+  dir = 1;
+  rc = mu_iterator_ctl (itr, mu_itrctl_set_direction, &dir);
+  if (rc)
+    {
+      mu_iterator_destroy (&itr);
+      return rc;
+    }
+
+  mu_iterator_first (itr);
+  mu_iterator_current (itr, (void **)&mr);
+  
+  if (mr->msg_end == MU_MSGNO_LAST)
+    {
+      struct mu_msgrange *last = mr;
+      
+      for (mu_iterator_next (itr);
+	   rc == 0 && !mu_iterator_is_done (itr); mu_iterator_next (itr))
+	{
+	  mu_iterator_current (itr, (void **)&mr);
+	  if (mr->msg_end == MU_MSGNO_LAST)
+	    {
+	      last->msg_beg = mr->msg_beg;
+	      rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	    }
+	  else if (mr->msg_beg >= last->msg_beg)
+	    rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	  else if (mr->msg_end + 1 >= last->msg_beg)
+	    {
+	      last->msg_beg = mr->msg_beg;
+	      rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	    }
+	  else
+	    break;
+	}
+    }
+
+  if (rc == 0)
+    {
+      dir = 0;
+      rc = mu_iterator_ctl (itr, mu_itrctl_set_direction, &dir);
+      if (rc)
+	{
+	  mu_iterator_destroy (&itr);
+	  return rc;
+	}
+
+      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+	   mu_iterator_next (itr))
+	{
+	  mu_iterator_current (itr, (void **)&mr);
+	  if (mr->msg_end == MU_MSGNO_LAST)
+	    break;
+	  if (prev)
+	    {
+	      if (prev->msg_beg <= mr->msg_beg && mr->msg_beg <= prev->msg_end)
+		{
+		  /* Possible cases:
+		     1. mr lies fully within prev:
+		     mr->msg_end <= prev->msg_end
+		     Action: mr deleted
+		     2. mr overlaps with prev:
+		     mr->msg_end > prev->msg_end
+		     Action: prev->msg_end is set to mr->msg_end; mr deleted
+		  */
+		  if (mr->msg_end > prev->msg_end)
+		    prev->msg_end = mr->msg_end;
+		  rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+		  if (rc)
+		    break;
+		  continue;
+		}
+	      else if (prev->msg_end + 1 == mr->msg_beg)
+		{
+		  prev->msg_end = mr->msg_end;
+		  rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+		  if (rc)
+		    break;
+		  continue;
+		}
+	    }
+	  prev = mr;
+	}
+    }
+  mu_iterator_destroy (&itr);
+
+  if (rc == 0)
+    mset->flags |= _MU_MSGSET_AGGREGATED;
+  return rc;
+}
diff --git a/libmailutils/imapio/send.c b/libmailutils/msgset/clear.c
similarity index 76%
copy from libmailutils/imapio/send.c
copy to libmailutils/msgset/clear.c
index 11ee049..f3f7ef8 100644
--- a/libmailutils/imapio/send.c
+++ b/libmailutils/msgset/clear.c
@@ -15,15 +15,18 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdarg.h>
 #include <mailutils/types.h>
-#include <mailutils/imapio.h>
-#include <mailutils/stream.h>
-#include <mailutils/sys/imapio.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
 
 int
-mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes)
+mu_msgset_clear (mu_msgset_t mset)
 {
-  return mu_stream_write (io->_imap_stream, buf, bytes, NULL);
+  if (!mset)
+    return EINVAL;
+  mu_list_clear (mset->list);
+  return 0;
 }
-    
+
diff --git a/libmailutils/msgset/create.c b/libmailutils/msgset/create.c
new file mode 100644
index 0000000..0f70ab2
--- /dev/null
+++ b/libmailutils/msgset/create.c
@@ -0,0 +1,60 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+/* Comparator function used to locate a message in the list.  Second argument
+   (b) is a pointer to the message number. */
+static int
+compare_msgnum (const void *a, const void *b)
+{
+  struct mu_msgrange const *range = a;
+  size_t msgno = *(size_t*)b;
+
+  if (range->msg_beg <= msgno && msgno <= range->msg_end)
+    return 0;
+  return 1;
+}
+
+int
+mu_msgset_create (mu_msgset_t *pres, mu_mailbox_t mbox, int flags)
+{
+  mu_msgset_t msgset;
+  int rc;
+
+  msgset = calloc (1, sizeof (*msgset));
+  if (!msgset)
+    return ENOMEM;
+  
+  rc = mu_list_create (&msgset->list);
+  if (rc)
+    {
+      free (msgset);
+      return rc;
+    }
+  mu_list_set_destroy_item (msgset->list, mu_list_free_item);
+  mu_list_set_comparator (msgset->list, compare_msgnum);
+  msgset->mbox = mbox;
+  msgset->flags = flags & _MU_MSGSET_USERFLAG_MASK;
+  *pres = msgset;
+  return 0;
+}
diff --git a/libmailutils/imapio/time.c b/libmailutils/msgset/free.c
similarity index 70%
copy from libmailutils/imapio/time.c
copy to libmailutils/msgset/free.c
index 089a7db..77a348e 100644
--- a/libmailutils/imapio/time.c
+++ b/libmailutils/msgset/free.c
@@ -16,15 +16,27 @@
 
 #include <config.h>
 #include <stdlib.h>
-#include <mailutils/stream.h>
-#include <mailutils/errno.h>
-#include <mailutils/sys/imapio.h>
-#include <mailutils/datetime.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
 
-int
-mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
-		     struct mu_timezone *tz)
+void
+mu_msgset_free (mu_msgset_t mset)
 {
-  return mu_c_streamftime (io->_imap_stream,
-			   "\"" MU_DATETIME_INTERNALDATE "\"", tm, tz);
-}  
+  if (mset)
+    {
+      mu_list_destroy (&mset->list);
+      free (mset);
+    }
+}
+
+void
+mu_msgset_destroy (mu_msgset_t *pset)
+{
+  if (pset)
+    {
+      mu_msgset_free (*pset);
+      *pset = NULL;
+    }
+}
+  
diff --git a/libmailutils/imapio/send.c b/libmailutils/msgset/getitr.c
similarity index 74%
copy from libmailutils/imapio/send.c
copy to libmailutils/msgset/getitr.c
index 11ee049..75cfb3d 100644
--- a/libmailutils/imapio/send.c
+++ b/libmailutils/msgset/getitr.c
@@ -15,15 +15,16 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdarg.h>
 #include <mailutils/types.h>
-#include <mailutils/imapio.h>
-#include <mailutils/stream.h>
-#include <mailutils/sys/imapio.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
 
 int
-mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes)
+mu_msgset_get_iterator (mu_msgset_t msgset, mu_iterator_t *pitr)
 {
-  return mu_stream_write (io->_imap_stream, buf, bytes, NULL);
+  if (!msgset)
+    return EINVAL;
+  return mu_list_get_iterator (msgset->list, pitr);
 }
-    
diff --git a/libmailutils/imapio/send.c b/libmailutils/msgset/getlist.c
similarity index 73%
copy from libmailutils/imapio/send.c
copy to libmailutils/msgset/getlist.c
index 11ee049..5f7370d 100644
--- a/libmailutils/imapio/send.c
+++ b/libmailutils/msgset/getlist.c
@@ -15,15 +15,19 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdarg.h>
 #include <mailutils/types.h>
-#include <mailutils/imapio.h>
-#include <mailutils/stream.h>
-#include <mailutils/sys/imapio.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
 
 int
-mu_imapio_send (mu_imapio_t io, const char *buf, size_t bytes)
+mu_msgset_get_list (mu_msgset_t msgset, mu_list_t *plist)
 {
-  return mu_stream_write (io->_imap_stream, buf, bytes, NULL);
+  if (!msgset)
+    return EINVAL;
+  if (!plist)
+    return MU_ERR_OUT_PTR_NULL;
+  *plist = msgset->list;
+  return 0;
 }
-    
diff --git a/libmailutils/imapio/time.c b/libmailutils/msgset/locate.c
similarity index 72%
copy from libmailutils/imapio/time.c
copy to libmailutils/msgset/locate.c
index 089a7db..7b94ece 100644
--- a/libmailutils/imapio/time.c
+++ b/libmailutils/msgset/locate.c
@@ -16,15 +16,17 @@
 
 #include <config.h>
 #include <stdlib.h>
-#include <mailutils/stream.h>
+#include <mailutils/types.h>
 #include <mailutils/errno.h>
-#include <mailutils/sys/imapio.h>
-#include <mailutils/datetime.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
 
 int
-mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
-		     struct mu_timezone *tz)
+mu_msgset_locate (mu_msgset_t msgset, size_t n,
+		  struct mu_msgrange const **prange)
 {
-  return mu_c_streamftime (io->_imap_stream,
-			   "\"" MU_DATETIME_INTERNALDATE "\"", tm, tz);
-}  
+  if (!msgset || n == 0)
+    return EINVAL;
+  return mu_list_locate (msgset->list, &n, (void**)prange);
+}
diff --git a/libmailutils/msgset/parse.c b/libmailutils/msgset/parse.c
new file mode 100644
index 0000000..c522478
--- /dev/null
+++ b/libmailutils/msgset/parse.c
@@ -0,0 +1,179 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+/* This structure keeps parser state while parsing message set. */
+struct parse_msgnum_env
+{
+  const char *s;         /* Current position in string */
+  size_t minval;         /* Min. sequence number or UID */
+  size_t maxval;         /* Max. sequence number or UID */
+  mu_msgset_t msgset;    /* Message set being built. */
+};
+
+/* Get a single message number/UID from env->s and store it into *PN.
+   Return 0 on success and error code on error.
+
+   Advance env->s to the point past the parsed message number.
+ */
+static int
+get_msgnum (struct parse_msgnum_env *env, size_t *pn)
+{
+  size_t msgnum;
+  char *p;
+  
+  errno = 0;
+  msgnum = strtoul (env->s, &p, 10);
+  if (msgnum == ULONG_MAX && errno == ERANGE)
+    return MU_ERR_PARSE;
+  env->s = p;
+  if (env->msgset->mbox && env->maxval && msgnum > env->maxval)
+    msgnum = env->maxval;
+  *pn = msgnum;
+  return 0;
+}
+
+/* Parse a single message range (A:B). Treat '*' as the largest number/UID
+   in use. */
+static int
+parse_msgrange (struct parse_msgnum_env *env)
+{
+  int rc;
+  struct mu_msgrange msgrange;
+  
+  if (*env->s == '*')
+    {
+      msgrange.msg_beg = env->maxval;
+      env->s++;
+    }
+  else if ((rc = get_msgnum (env, &msgrange.msg_beg)))
+    return rc;
+
+  if (*env->s == ':')
+    {
+      if (*++env->s == '*')
+	{
+	  msgrange.msg_end = env->maxval;
+	  ++env->s;
+	}
+      else if (*env->s == 0)
+	return MU_ERR_PARSE;
+      else if ((rc = get_msgnum (env, &msgrange.msg_end)))
+	return rc;
+    }
+  else
+    msgrange.msg_end = msgrange.msg_beg;
+
+  if (msgrange.msg_end && msgrange.msg_end < msgrange.msg_beg)
+    {
+      size_t tmp = msgrange.msg_end;
+      msgrange.msg_end = msgrange.msg_beg;
+      msgrange.msg_beg = tmp;
+    }
+
+  if ((env->msgset->flags & MU_MSGSET_UID) && env->msgset->mbox)
+    {
+      int rc;
+
+      rc = mu_mailbox_translate (env->msgset->mbox,
+				 MU_MAILBOX_UID_TO_MSGNO,
+				 msgrange.msg_beg, &msgrange.msg_beg);
+      if (rc == MU_ERR_NOENT)
+	msgrange.msg_beg = env->minval;
+      else if (rc)
+	return rc;
+      
+      rc = mu_mailbox_translate (env->msgset->mbox,
+				 MU_MAILBOX_UID_TO_MSGNO,
+				 msgrange.msg_end, &msgrange.msg_end);
+      if (rc == MU_ERR_NOENT)
+	msgrange.msg_end = env->maxval;
+      else if (rc)
+	return rc;
+    }      
+
+  return mu_msgset_add_range (env->msgset, msgrange.msg_beg, msgrange.msg_end);
+}
+
+/* Parse IMAP-style message set specification S.
+
+   On success, return 0 and populate MSET with the resulting message ranges.
+   On error, return error code and point END to the position in the input
+   string where parsing has failed. */
+int
+mu_msgset_parse_imap (mu_msgset_t mset, const char *s, char **end)
+{
+  int rc;
+  struct parse_msgnum_env env;
+  
+  if (!s || !mset)
+    return EINVAL;
+  if (!*s)
+    return MU_ERR_PARSE;
+
+  memset (&env, 0, sizeof (env));
+  env.s = s;
+  env.msgset = mset;
+  env.minval = 1;
+  
+  if (end)
+    *end = (char*) s;
+  if (mset->mbox)
+    {
+      size_t lastmsgno;      /* Max. sequence number. */
+
+      rc = mu_mailbox_messages_count (mset->mbox, &lastmsgno);
+      if (rc == 0)
+	{
+	  if (mset->flags & MU_MSGSET_UID)
+	    {
+	      rc = mu_mailbox_translate (mset->mbox, MU_MAILBOX_MSGNO_TO_UID,
+					 lastmsgno, &env.maxval);
+	      if (rc == 0)
+		rc = mu_mailbox_translate (mset->mbox, MU_MAILBOX_MSGNO_TO_UID,
+					   1, &env.minval);
+	    }
+	  else
+	    env.maxval = lastmsgno;
+	}
+      if (rc)
+	return rc;
+    }
+  
+  while ((rc = parse_msgrange (&env)) == 0 && *env.s)
+    {
+      if (*env.s != ',' || *++env.s == 0)
+	{
+	  rc = MU_ERR_PARSE;
+	  break;
+	}
+    }
+
+  if (end)
+    *end = (char*) env.s;
+  return rc;
+}
diff --git a/libmailutils/msgset/print.c b/libmailutils/msgset/print.c
new file mode 100644
index 0000000..e7f990e
--- /dev/null
+++ b/libmailutils/msgset/print.c
@@ -0,0 +1,74 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/stream.h>
+#include <mailutils/io.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+struct print_env
+{
+  mu_stream_t stream;
+  int cont;
+};
+
+static int
+_msgrange_printer (void *item, void *data)
+{
+  int rc;
+  struct mu_msgrange *range = item;
+  struct print_env *env = data;
+
+  if (env->cont)
+    {
+      rc = mu_stream_write (env->stream, ",", 1, NULL);
+      if (rc)
+	return rc;
+    }
+  else
+    env->cont = 1;
+  if (range->msg_beg == range->msg_end)
+    rc = mu_stream_printf (env->stream, "%lu", (unsigned long) range->msg_beg);
+  else if (range->msg_end == 0)
+    rc = mu_stream_printf (env->stream, "%lu:*",
+			   (unsigned long) range->msg_beg);
+  else
+    rc = mu_stream_printf (env->stream, "%lu:%lu",
+			   (unsigned long) range->msg_beg,
+			   (unsigned long) range->msg_end);
+  return rc;
+}
+
+int
+mu_msgset_print (mu_stream_t str, mu_msgset_t mset)
+{
+  struct print_env env;
+  int rc;
+  
+  if (mu_list_is_empty (mset->list))
+    return MU_ERR_NOENT;
+  rc = mu_msgset_aggregate (mset);
+  if (rc)
+    return rc;
+  env.stream = str;
+  env.cont = 0;
+  return mu_list_foreach (mset->list, _msgrange_printer, &env);
+}
diff --git a/libmailutils/msgset/sub.c b/libmailutils/msgset/sub.c
new file mode 100644
index 0000000..2840781
--- /dev/null
+++ b/libmailutils/msgset/sub.c
@@ -0,0 +1,233 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/iterator.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+/* Remove range R from the set S.
+
+   There are following basic cases:
+
+   1. S does not contain R.
+
+      Return imeediately, there is nothing to do here.
+
+   2. S contains a range X that matches exactly R.
+
+      Action: Remove X entirely and return.
+
+   3. There is a range X in S such that R falls entirely within it.
+
+      Action: Split X into two ranges:
+
+         X.msg_beg   : R.msg_beg-1
+	 R.msg_end+1 : X.msg_end
+
+      and return.
+      In border cases one of the new ranges may be empty.
+
+   4. There is a range X which contains an initial subrange of R.
+
+      Action: Remove the subrange from X. If the resulting range is
+      empty, remove it. Continue iteration with X.msg_end:R.msg_end
+      in place of R.
+
+   5. There is a range X such that its initial subrange is contained
+      in R.
+
+      Action: Remove X.msg_beg:R.msg_end and return.
+
+   6. There is a range X that is contained within R.
+
+      Action: Remove X and continue.
+
+There are two special cases.  First is when R.msg_end is 0, meaning the last
+available message number in the mailbox.  In this case the problem is reduced
+to either 2 or a combination of 4 and zero or more 6.  It is handled by the
+sub_msgno_last function below.
+
+Yet another special case is when the last element of S has msg_end == 0.  If
+so, the problem is reduced to either 2 or 4.
+*/
+
+static int
+sub_msgno_last (mu_msgset_t mset, size_t beg)
+{
+  int rc;
+  struct mu_msgrange *range;
+
+  if (beg == 1)
+    mu_list_clear (mset->list);
+  else
+    {
+      mu_iterator_t itr;
+      
+      rc = mu_list_get_iterator (mset->list, &itr);
+      if (rc)
+	return rc;
+      rc = 1;
+      rc = mu_iterator_ctl (itr, mu_itrctl_set_direction, &rc);
+      if (rc)
+	{
+	  mu_iterator_destroy (&itr);
+	  return rc;
+	}
+	  
+      for (mu_iterator_first (itr); rc == 0 && !mu_iterator_is_done (itr);
+	   mu_iterator_next (itr))
+	{
+	  mu_iterator_current (itr, (void **)&range);
+	  if (range->msg_beg > beg)
+	    rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	  else if (range->msg_beg == beg)
+	    {
+	      rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	      break;
+	    }
+	  else
+	    break;
+	}
+      mu_iterator_destroy (&itr);
+    }
+  return 0;
+}
+
+int
+mu_msgset_sub_range (mu_msgset_t mset, size_t beg, size_t end)
+{
+  int rc;
+  mu_iterator_t itr;
+  struct mu_msgrange *mr;
+
+  if (!mset)
+    return EINVAL;
+  if (mu_list_is_empty (mset->list))
+    return MU_ERR_NOENT;
+  rc = mu_msgset_aggregate (mset);
+  if (rc)
+    return rc;
+
+  if (end == MU_MSGNO_LAST)
+    return sub_msgno_last (mset, beg);
+  
+  /* Test border cases */
+  rc = mu_list_head (mset->list, (void**)&mr);
+  if (rc)
+    return rc;
+  if (end < mr->msg_beg)
+    return 0;
+  if (beg < mr->msg_beg)
+    beg = mr->msg_beg;
+  
+  if (rc)
+    return rc;
+  rc = mu_list_tail (mset->list, (void**) &mr);
+  if (mr->msg_end != MU_MSGNO_LAST)
+    {
+      if (beg > mr->msg_end)
+	return 0;
+      if (end > mr->msg_end)
+	end = mr->msg_end;
+    }
+  
+  rc = mu_list_get_iterator (mset->list, &itr);
+  if (rc)
+    return rc;
+  for (mu_iterator_first (itr); rc == 0 && !mu_iterator_is_done (itr);
+       mu_iterator_next (itr))
+    {
+      mu_iterator_current (itr, (void **)&mr);
+
+      if (mr->msg_end == MU_MSGNO_LAST)
+	{
+	  /* This is the last element in list. */
+	  if (mr->msg_beg == beg)
+	    rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	  else if (mr->msg_beg > beg)
+	    mr->msg_beg = end + 1;
+	  break;
+	}
+      
+      if (mr->msg_beg == beg && mr->msg_end == end) /* See case 2 above */
+	{
+	  rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	  break;
+	}
+      else if (mr->msg_beg <= beg && beg <= mr->msg_end) 
+	{ 
+	  if (mr->msg_beg <= end && end <= mr->msg_end) /* Case 3 */
+	    {
+	      /* Split the range */
+	      if (end != mr->msg_end)
+		{
+		  struct mu_msgrange *newrange = calloc (1,
+							 sizeof (*newrange));
+		  if (!newrange)
+		    {
+		      rc = ENOMEM;
+		      break;
+		    }
+		  newrange->msg_beg = end + 1;
+		  newrange->msg_end = mr->msg_end;
+		  rc = mu_iterator_ctl (itr, mu_itrctl_insert, newrange);
+		  if (rc)
+		    {
+		      free (newrange);
+		      break;
+		    }
+		}
+	      
+	      if (mr->msg_beg == beg)
+		rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	      else
+		mr->msg_end = beg - 1;
+	      break;
+	    }
+	  else if (mr->msg_beg == beg)      /* Case 4 */
+	    {
+	      beg = mr->msg_end;
+	      rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	    }
+	  else
+	    {
+	      size_t n = mr->msg_end;
+	      mr->msg_end = beg - 1;
+	      beg = n;
+	    }
+	}
+      else if (mr->msg_beg <= end && end <= mr->msg_end) /* Case 5 */
+	{
+	  mr->msg_beg = end + 1;
+	  if (mr->msg_beg >= mr->msg_end)
+	    rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	  break;
+	}
+      else if (beg <= mr->msg_beg && mr->msg_beg <= end)
+	{
+	  rc = mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
+	}
+    }
+  mu_iterator_destroy (&itr);
+  
+  return rc;
+}
+      
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 8b97c86..aa1661d 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -52,6 +52,7 @@ noinst_PROGRAMS = \
  listop\
  mailcap\
  mimehdr\
+ msgset\
  prop\
  scantime\
  strftime\
@@ -90,6 +91,7 @@ TESTSUITE_AT = \
  list.at\
  mailcap.at\
  mimehdr.at\
+ msgset.at\
  prop.at\
  scantime.at\
  strftime.at\
diff --git a/libmailutils/tests/msgset.at b/libmailutils/tests/msgset.at
new file mode 100644
index 0000000..a591afd
--- /dev/null
+++ b/libmailutils/tests/msgset.at
@@ -0,0 +1,175 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_BANNER([Message sets])
+
+dnl ------------------------------------------------------------------
+dnl MSGSET([NAME], [KW = `'], [OP], [STDOUT = `'], [STDERR = `'])
+dnl
+m4_pushdef([MSGSET],[
+AT_SETUP([$1])
+AT_KEYWORDS([msgset $2])
+AT_CHECK([msgset $3],[0],[$4],[$5])
+AT_CLEANUP
+])
+dnl ------------------------------------------------------------------
+
+MSGSET([Aggregation: simple],[msgset-aggr-simple msgset-aggr],
+[-msgset='1,5:11,20:24,80:90,22:30,50:60,4:12,41:50,70:81'],
+[1,4:12,20:30,41:60,70:90
+])
+
+MSGSET([Aggregation: open range (1)],
+[msgset-aggr-open-1 msgset-aggr-open msgset-aggr],
+[-msgset='12:*,1'],
+[1,12:*
+])
+
+MSGSET([Aggregation: coalescing open ranges],
+[msgset-aggr-open-2 msgset-aggr-open msgset-aggr],
+[-msgset='12:*,1,15:*,8:*'],
+[1,8:*
+])
+
+MSGSET([Aggregation: open range (3)],
+[msgset-aggr-open-3 msgset-aggr-open msgset-aggr],
+[-msgset='1,12:*,12:13'],
+[1,12:*
+])
+
+MSGSET([Aggregation: open range (4)],
+[msgset-aggr-open-4 msgset-aggr-open msgset-aggr],
+[-msgset='1,12:*,13:40'],
+[1,12:*
+])
+
+MSGSET([Aggregation: open range (5)],
+[msgset-aggr-open-5 msgset-aggr-open msgset-aggr],
+[-msgset='10:*,3,5:9'],
+[3,5:*
+])
+
+MSGSET([Create simple set],[msgset-simple msgset-add],
+[-add=1:10],
+[1:10
+])
+
+MSGSET([Create complex set],[msgset-complex msgset-add],
+[-add=1 -add=2 -add=3 -add=4 -add=10:20 -add=15:35 -add=36:40],
+[1:4,10:40
+])
+
+MSGSET([Subtract: no match],[msgset-sub msgset-sub-1],
+[-msgset=20:40 -sub=1:10],
+[20:40
+])
+
+MSGSET([Subtract: exact match],[msgset-sub msgset-sub-2],
+[-msgset=1,4:9,11 -sub=4:9],
+[1,11
+])
+
+MSGSET([Subtract: contained range],[msgset-sub msgset-sub-3],
+[-msgset=1:40 -sub=5:15
+],
+[1:4,16:40
+])
+
+MSGSET([Subtract: contained range (left border case)],
+[msgset-sub msgset-sub-3 msgset-sub-3-0],
+[-msgset=1:40 -sub=1:20],
+[21:40
+])
+
+MSGSET([Subtract: contained range (right border case)],
+[msgset-sub msgset-sub-3 msgset-sub-3-1],
+[-msgset=1:40 -sub=30:40],
+[1:29
+])
+
+MSGSET([Subtract: initial subrange],[msgset-sub msgset-sub-4],
+[-msgset=1:40,50:60,80:200 -sub=55:65],
+[1:40,50:54,80:200
+])
+
+MSGSET([Subtract: trailing subrange],[msgset-sub msgset-sub-5],
+[-msgset=1:40,50:60,80:200 -sub=45:55],
+[1:40,56:60,80:200
+])
+
+MSGSET([Subtract: overlapping subrange],[msgset-sub msgset-sub-6],
+[-msgset=1:40,50:60,80:200 -sub=41:70],
+[1:40,80:200
+])
+
+MSGSET([Subtract: 4, 5 and 6 combined],
+[msgset-sub msgset-sub-4 msgset-sub-5 msgset-sub-6 msgset-sub-456],
+[-msgset=1:40,50:60,80:200 -sub=30:100],
+[1:29,101:200
+])
+
+MSGSET([open range],[msgset-inf],
+[-msgset='1:*'],
+[1:*
+])
+
+MSGSET([add to open range],[msgset-inf-add msgset-add],
+[-msgset='10:*' -add=3 -add=5:9],
+[3,5:*
+])
+
+MSGSET([subtract from open range],[msgset-inf-sub msgset-sub],
+[-msgset='3,10:*' -sub=5:11],
+[3,12:*
+])
+
+MSGSET([subtract from open range an equal range],[msgset-inf-sub-1 msgset-sub],
+[-msgset='3,10:*' -sub=10:*],
+[3
+])
+
+MSGSET([subtract from open range a broader range],
+[msgset-inf-sub-2 msgset-sub],
+[-msgset='3,10:*' -sub=20:*],
+[3,10:*
+])
+
+MSGSET([subtract from open range a narrower range],
+[msgset-inf-sub-3 msgset-sub],
+[-msgset='3,10:*' -sub=5:*],
+[3
+])
+
+MSGSET([subtract an open range with matching left boundary],
+[msgset-inf-sub-4 msgset-sub],
+[-msgset='3,10:20' -sub=10:*],
+[3
+])
+
+MSGSET([subtract an open range with greater left boundary],
+[msgset-inf-sub-4 msgset-sub],
+[-msgset='3,10:20' -sub=11:*],
+[3,10:20
+])
+
+MSGSET([subtract an open range with smaller left boundary],
+[msgset-inf-sub-4 msgset-sub],
+[-msgset='3,10:20' -sub=8:*],
+[3
+])
+
+dnl ------------------------------------------------------------------
+m4_popdef([MSGSET])
\ No newline at end of file
diff --git a/libmailutils/tests/msgset.c b/libmailutils/tests/msgset.c
new file mode 100644
index 0000000..91db029
--- /dev/null
+++ b/libmailutils/tests/msgset.c
@@ -0,0 +1,125 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/mailutils.h>
+
+static void
+parse_msgset (char *arg, struct mu_msgrange *range)
+{
+  size_t msgnum;
+  char *p;
+  
+  errno = 0;
+  msgnum = strtoul (arg, &p, 10);
+  range->msg_beg = msgnum;
+  if (*p == ':')
+    {
+      if (*++p == '*')
+	msgnum = 0;
+      else
+	{
+	  msgnum = strtoul (p, &p, 10);
+	  if (*p)
+	    {
+	      mu_error ("error in message range near %s", p);
+	      exit (1);
+	    }
+	}
+    }
+  else if (*p == '*')
+    msgnum = 0;
+  else if (*p)
+    {
+      mu_error ("error in message range near %s", p);
+      exit (1);
+    }
+
+  range->msg_end = msgnum;
+}
+
+int
+main (int argc, char **argv)
+{
+  int i;
+  char *msgset_string = NULL;
+  mu_msgset_t msgset;
+  int rc;
+  
+  mu_set_program_name (argv[0]);
+  for (i = 1; i < argc; i++)
+    {
+      char *arg = argv[i];
+
+      if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
+	{
+	  mu_printf ("usage: %s [-msgset=SET] [-add X[:Y]] [-del X:[Y]]...\n",
+		     mu_program_name);
+	  return 0;
+	}
+      else if (strncmp (arg, "-msgset=", 8) == 0)
+	msgset_string = arg + 8;
+      else
+	break;
+    }
+
+  MU_ASSERT (mu_msgset_create (&msgset, NULL, 0));
+  if (msgset_string)
+    {
+      char *end;
+      rc = mu_msgset_parse_imap (msgset, msgset_string, &end);
+      if (rc)
+	{
+	  mu_error ("mu_msgset_parse_imap: %s near %s",
+		    mu_strerror (rc), end);
+	  return 1;
+	}
+    }
+  
+  for (; i < argc; i++)
+    {
+      char *arg = argv[i];
+      struct mu_msgrange range;
+      
+      if (strncmp (arg, "-add=", 5) == 0)
+	{
+	  parse_msgset (arg + 5, &range);
+	  MU_ASSERT (mu_msgset_add_range (msgset, range.msg_beg,
+					  range.msg_end));
+	}
+      else if (strncmp (arg, "-sub=", 5) == 0)
+	{
+	  parse_msgset (arg + 5, &range);
+	  MU_ASSERT (mu_msgset_sub_range (msgset, range.msg_beg,
+					  range.msg_end));
+	}
+      else
+      	{
+	  mu_error ("unknown option %s", arg);
+	  return 1;
+	}
+    }
+  MU_ASSERT (mu_msgset_print (mu_strout, msgset));
+  mu_printf ("\n");
+  mu_msgset_free (msgset);
+  
+  return 0;
+}
+  
+	     
+
+	
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index b9b889c..15226c6 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -99,3 +99,5 @@ m4_include([strftime.at])
 m4_include([fsaf.at])
 
 m4_include([mimehdr.at])
+
+m4_include([msgset.at])
diff --git a/libproto/imap/capability.c b/libproto/imap/capability.c
index bc3c8a3..ade9a61 100644
--- a/libproto/imap/capability.c
+++ b/libproto/imap/capability.c
@@ -102,7 +102,7 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
     case MU_IMAP_CLIENT_READY:
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_send_command (imap->io, imap->tag_str,
+      status = mu_imapio_send_command (imap->io, imap->tag_str, NULL,
 				       "CAPABILITY", NULL); 
       MU_IMAP_CHECK_EAGAIN (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
diff --git a/libproto/imap/close.c b/libproto/imap/close.c
index 50bf1c5..17cd8cc 100644
--- a/libproto/imap/close.c
+++ b/libproto/imap/close.c
@@ -40,6 +40,7 @@ mu_imap_close (mu_imap_t imap)
     1,
     &command,
     NULL,
+    NULL,
     _mu_close_handler
   };
   return mu_imap_gencom (imap, &com);
diff --git a/libproto/imap/copy.c b/libproto/imap/copy.c
index 5598c20..e97990d 100644
--- a/libproto/imap/copy.c
+++ b/libproto/imap/copy.c
@@ -24,7 +24,7 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
+mu_imap_copy (mu_imap_t imap, int uid, mu_msgset_t msgset, const char *mailbox)
 {
   char const *argv[4];
   int i;
@@ -34,7 +34,7 @@ mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
   if (uid)
     argv[i++] = "UID";
   argv[i++] = "COPY";
-  argv[i++] = msgset;
+  argv[i++] = "\\";
   argv[i++] = mailbox;
 
   com.session_state = MU_IMAP_SESSION_SELECTED;
@@ -43,6 +43,7 @@ mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
   com.argc = i;
   com.argv = argv;
   com.extra = NULL;
+  com.msgset = msgset;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/delete.c b/libproto/imap/delete.c
index 5a605d0..dd4a45c 100644
--- a/libproto/imap/delete.c
+++ b/libproto/imap/delete.c
@@ -43,6 +43,7 @@ mu_imap_delete (mu_imap_t imap, const char *mailbox)
   com.argc = 2;
   com.argv = argv;
   com.extra = NULL;
+  com.msgset = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/fetch.c b/libproto/imap/fetch.c
index 22937df..471ad86 100644
--- a/libproto/imap/fetch.c
+++ b/libproto/imap/fetch.c
@@ -32,7 +32,7 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
+mu_imap_fetch (mu_imap_t imap, int uid, mu_msgset_t msgset, const char *items)
 {
   char const *argv[3];
   int i;
@@ -42,7 +42,7 @@ mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
   if (uid)
     argv[i++] = "UID";
   argv[i++] = "FETCH";
-  argv[i++] = msgset;
+  argv[i++] = "\\";
   
   com.session_state = MU_IMAP_SESSION_SELECTED;
   com.capa = NULL;
@@ -50,6 +50,7 @@ mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
   com.argc = i;
   com.argv = argv;
   com.extra = items;
+  com.msgset = msgset;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/gencom.c b/libproto/imap/gencom.c
index 57e8643..d0d8914 100644
--- a/libproto/imap/gencom.c
+++ b/libproto/imap/gencom.c
@@ -57,6 +57,7 @@ mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
       status = mu_imapio_send_command_v (imap->io, imap->tag_str,
+					 cmd->msgset,
 					 cmd->argc, cmd->argv, cmd->extra);
       MU_IMAP_CHECK_ERROR (imap, status);
       MU_IMAP_FCLR (imap, MU_IMAP_RESP);
diff --git a/libproto/imap/genlist.c b/libproto/imap/genlist.c
index 4feeb1c..7659d41 100644
--- a/libproto/imap/genlist.c
+++ b/libproto/imap/genlist.c
@@ -150,6 +150,7 @@ mu_imap_genlist (mu_imap_t imap, int lsub,
   com.argc = 3;
   com.argv = argv;
   com.extra = NULL;
+  com.msgset = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = list_untagged_handler;
   com.untagged_handler_data = &clos;
diff --git a/libproto/imap/login.c b/libproto/imap/login.c
index efdfb4b..e7220e4 100644
--- a/libproto/imap/login.c
+++ b/libproto/imap/login.c
@@ -44,7 +44,7 @@ mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
 	_mu_imap_xscript_level (imap, MU_XSCRIPT_SECURE);
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_send_command (imap->io, imap->tag_str,
+      status = mu_imapio_send_command (imap->io, imap->tag_str, NULL,
 				       "LOGIN", user, pass, NULL);
       _mu_imap_xscript_level (imap, MU_XSCRIPT_NORMAL);
       /* FIXME: how to obscure the passwd in the stream buffer? */
diff --git a/libproto/imap/mbcreate.c b/libproto/imap/mbcreate.c
index a9b7edd..a0a3f95 100644
--- a/libproto/imap/mbcreate.c
+++ b/libproto/imap/mbcreate.c
@@ -39,7 +39,8 @@ mu_imap_mailbox_create (mu_imap_t imap, const char *mailbox)
   com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
   com.argc = 2;
   com.argv = argv;
-  com.extra = 0;
+  com.extra = NULL;
+  com.msgset = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 609c86c..1613ba3 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -38,6 +38,7 @@
 #include <mailutils/attribute.h>
 #include <mailutils/header.h>
 #include <mailutils/body.h>
+#include <mailutils/msgset.h>
 #include <mailutils/sys/imap.h>
 
 #define _imap_mbx_clrerr(imbx) ((imbx)->last_error = 0)
@@ -58,7 +59,7 @@ _imap_msg_no (struct _mu_imap_message *imsg)
 }
 
 static int
-_imap_fetch_with_callback (mu_imap_t imap, char *msgset, char *items,
+_imap_fetch_with_callback (mu_imap_t imap, mu_msgset_t msgset, char *items,
 			   mu_imap_callback_t cb, void *data)
 {
   int rc;
@@ -156,7 +157,7 @@ __imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
   
   if (!(imsg->flags & _MU_IMAP_MSG_CACHED))
     {
-      char *msgset;
+      mu_msgset_t msgset;
       
       mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
 		(_("caching message %lu"), (unsigned long) msgno));
@@ -174,16 +175,22 @@ __imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
       if (rc)
 	return rc;
 
-      rc = mu_asprintf (&msgset, "%lu", msgno);
+      rc = mu_msgset_create (&msgset, NULL, 0);
       if (rc == 0)
 	{
 	  struct save_closure clos;
+
 	  clos.imsg = imsg;
 	  clos.save_stream = imbx->cache;
-	  _imap_mbx_clrerr (imbx);
-	  rc = _imap_fetch_with_callback (imap, msgset, "BODY[]",
-					  _save_message, &clos);
-	  free (msgset);
+
+	  rc = mu_msgset_add_range (msgset, msgno, msgno);
+	  if (rc == 0)
+	    {
+	      _imap_mbx_clrerr (imbx);
+	      rc = _imap_fetch_with_callback (imap, msgset, "BODY[]",
+					      _save_message, &clos);
+	    }
+	  mu_msgset_free (msgset);
 	  if (rc == 0 && !_imap_mbx_errno (imbx))
 	    {
 	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
@@ -359,46 +366,54 @@ _imap_hdr_fill (void *data, char **pbuf, size_t *plen)
   struct _mu_imap_mailbox *imbx = imsg->imbx;
   mu_imap_t imap = imbx->mbox->folder->data;
   struct save_closure clos;
-  char *msgset;
+  mu_msgset_t msgset;
   unsigned long msgno = _imap_msg_no (imsg);
   int rc;
-  
-  clos.imsg = imsg;
-  rc = mu_memory_stream_create (&clos.save_stream, MU_STREAM_RDWR);
-  _imap_mbx_clrerr (imbx);
-  rc = mu_asprintf (&msgset, "%lu", msgno);
+
+  rc = mu_msgset_create (&msgset, NULL, 0);
   if (rc == 0)
     {
-      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
-		(_("message %lu: reading headers"),
-		 (unsigned long) msgno));
-      rc = _imap_fetch_with_callback (imap, msgset, "BODY.PEEK[HEADER]",
-				      _save_message, &clos);
-      free (msgset);
+      rc = mu_msgset_add_range (msgset, msgno, msgno);
       if (rc == 0)
 	{
-	  char *buf;
-	  mu_off_t size;
-	  
-	  mu_stream_size (clos.save_stream, &size);
-	  buf = malloc (size + 1);
-	  if (!buf)
-	    rc = ENOMEM;
-	  else
+	  clos.imsg = imsg;
+	  rc = mu_memory_stream_create (&clos.save_stream, MU_STREAM_RDWR);
+	  if (rc == 0)
 	    {
-	      mu_stream_seek (clos.save_stream, 0, MU_SEEK_SET, NULL);
-	      rc = mu_stream_read (clos.save_stream, buf, size, NULL);
+	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
+			(_("message %lu: reading headers"),
+			 (unsigned long) msgno));
+	      _imap_mbx_clrerr (imbx);
+	      rc = _imap_fetch_with_callback (imap, msgset,
+					      "BODY.PEEK[HEADER]",
+					      _save_message, &clos);
 	      if (rc == 0)
 		{
-		  *pbuf = buf;
-		  *plen = size;
+		  char *buf;
+		  mu_off_t size;
+	  
+		  mu_stream_size (clos.save_stream, &size);
+		  buf = malloc (size + 1);
+		  if (!buf)
+		    rc = ENOMEM;
+		  else
+		    {
+		      mu_stream_seek (clos.save_stream, 0, MU_SEEK_SET, NULL);
+		      rc = mu_stream_read (clos.save_stream, buf, size, NULL);
+		      if (rc == 0)
+			{
+			  *pbuf = buf;
+			  *plen = size;
+			}
+		      else
+			free (buf);
+		    }
 		}
-	      else
-		free (buf);
+	      mu_stream_destroy (&clos.save_stream);
 	    }
 	}
+      mu_msgset_free (msgset);
     }
-  mu_stream_destroy (&clos.save_stream);
   return rc;
 }
 
@@ -602,15 +617,18 @@ _imap_msg_bodystructure (mu_message_t msg, struct mu_bodystructure **pbs)
   struct _mu_imap_mailbox *imbx = imsg->imbx;
   mu_imap_t imap = imbx->mbox->folder->data;
   int rc;
-  char *msgset;
-  
-  rc = mu_asprintf (&msgset, "%lu", _imap_msg_no (imsg));
-  if (rc)
-    return rc;
+  mu_msgset_t msgset;
 
-  rc = _imap_fetch_with_callback (imap, msgset, "BODYSTRUCTURE",
-				  _imap_bodystructure_callback, pbs);
-  free (msgset);
+  rc = mu_msgset_create (&msgset, NULL, 0);
+  if (rc == 0)
+    {
+      size_t msgno = _imap_msg_no (imsg);
+      rc = mu_msgset_add_range (msgset, msgno, msgno);
+      if (rc == 0)
+	rc = _imap_fetch_with_callback (imap, msgset, "BODYSTRUCTURE",
+					_imap_bodystructure_callback, pbs);
+      mu_msgset_free (msgset);
+    }
   return rc;
 }
 
@@ -919,36 +937,22 @@ aggregate_attributes (struct _mu_imap_mailbox *imbx,
 }
 
 static int
-flush_attributes (mu_imap_t imap, mu_stream_t str, int attr_flags)
-{
-  int rc;
-  mu_transport_t trans[2];
-		      
-  mu_stream_write (str, "", 1, NULL);
-  if (mu_stream_err (str))
-    return mu_stream_last_error (str);
-
-  rc = mu_stream_ioctl (str, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans);
-  if (rc == 0)
-    rc = mu_imap_store_flags (imap, 0, (char*)trans[0],
-			      MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
-			      attr_flags);
-  return rc;
-}
-
-static int
 _imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
 {
   struct _mu_imap_mailbox *imbx = mbox->data;
   mu_folder_t folder = mbox->folder;
   mu_imap_t imap = folder->data;
   size_t i, j;
-  char *msgset;
+  mu_msgset_t msgset;
   int rc;
   int delflg = 0;
   struct attr_tab *tab;
   size_t count;
-  
+
+  rc = mu_msgset_create (&msgset, NULL, 0);
+  if (rc)
+    return rc;
+
   rc = aggregate_attributes (imbx, &tab, &count);
   if (rc)
     {
@@ -957,14 +961,14 @@ _imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
 	{
 	  if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
 	    {
-	      rc = mu_asprintf (&msgset, "%lu", i + 1);
+	      mu_msgset_clear (msgset);
+	      mu_msgset_add_range (msgset, i + 1, i + 1);
 	      if (rc)
 		break;
 	      rc = mu_imap_store_flags (imap, 0, msgset,
 					MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
 					imbx->msgs[i].attr_flags);
 	      delflg |= imbx->msgs[i].attr_flags & MU_ATTRIBUTE_DELETED;
-	      free (msgset);
 	      if (rc)
 		break;
 	    }
@@ -972,45 +976,45 @@ _imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
     }
   else
     {
-      mu_stream_t str;
-
-      rc = mu_memory_stream_create (&str, MU_STREAM_RDWR);
-      if (rc == 0)
+      for (i = j = 0; i < count; i++)
 	{
-	  for (i = j = 0; i < count; i++)
+	  if (j < i)
 	    {
-	      if (j < i)
+	      if (tab[j].attr_flags != tab[i].attr_flags)
 		{
-		  if (tab[j].attr_flags != tab[i].attr_flags)
-		    {
-		      rc = flush_attributes (imap, str, tab[j].attr_flags);
-		      delflg |= tab[j].attr_flags & MU_ATTRIBUTE_DELETED;
-		      if (rc)
-			break;
-		      mu_stream_truncate (str, 0);
-		      j = i;
-		    }
-		  else
-		    mu_stream_printf (str, ",");
+		  rc = mu_imap_store_flags (imap, 0, msgset,
+					    MU_IMAP_STORE_SET|
+					    MU_IMAP_STORE_SILENT,
+					    tab[j].attr_flags);
+		  delflg |= tab[j].attr_flags & MU_ATTRIBUTE_DELETED;
+		  if (rc)
+		    break;
+		  mu_msgset_clear (msgset);
+		  j = i;
 		}
-	      if (tab[i].end == tab[i].start)
-		mu_stream_printf (str, "%lu", (unsigned long)tab[i].start+1);
-	      else
-		mu_stream_printf (str, "%lu:%lu",
-				  (unsigned long)tab[i].start+1,
-				  (unsigned long)tab[i].end+1);
 	    }
+	  if (tab[i].end == tab[i].start)
+	    rc = mu_msgset_add_range (msgset, tab[i].start + 1,
+				      tab[i].start + 1);
+	  else
+	    rc = mu_msgset_add_range (msgset,
+				      tab[i].start + 1,
+				      tab[i].end + 1);
+	  if (rc)
+	    break;
+	}
 
-	  if (j < i)
-	    {
-	      rc = flush_attributes (imap, str, tab[j].attr_flags);
-	      delflg |= tab[j].attr_flags & MU_ATTRIBUTE_DELETED;
-	    }
-	  mu_stream_unref (str);
+      if (rc == 0 && j < i)
+	{
+	  rc = mu_imap_store_flags (imap, 0, msgset,
+				    MU_IMAP_STORE_SET|MU_IMAP_STORE_SILENT,
+				    tab[j].attr_flags);
+	  delflg |= tab[j].attr_flags & MU_ATTRIBUTE_DELETED;
 	}
       free (tab);
     }
-
+  mu_msgset_free (msgset);
+  
   if (rc)
     return rc;
 
@@ -1182,20 +1186,26 @@ _imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
   struct _mu_imap_mailbox *imbx = mbox->data;
   mu_folder_t folder = mbox->folder;
   mu_imap_t imap = folder->data;
-  char *msgset;
+  mu_msgset_t msgset;
   int rc;
   static char _imap_scan_items[] = "(UID FLAGS ENVELOPE RFC822.SIZE BODY)";
   
   mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
 	    (_("scanning mailbox %s"), mu_url_to_string (mbox->url)));
-  rc = mu_asprintf (&msgset, "%lu:*", (unsigned long) msgno);
+  rc = mu_msgset_create (&msgset, NULL, 0);
   if (rc)
     return rc;
-
+  rc = mu_msgset_add_range (msgset, msgno, MU_MSGNO_LAST);
+  if (rc)
+    {
+      mu_msgset_free (msgset);
+      return rc;
+    }
+  
   _imap_mbx_clrerr (imbx);
   rc = _imap_fetch_with_callback (imap, msgset, _imap_scan_items,
 				  _imap_fetch_callback, imbx);
-  free (msgset);
+  mu_msgset_free (msgset);
   if (rc == 0)
     rc = _imap_mbx_errno (imbx);
   if (rc == 0)
diff --git a/libproto/imap/rename.c b/libproto/imap/rename.c
index 3348380..dc5f0d5 100644
--- a/libproto/imap/rename.c
+++ b/libproto/imap/rename.c
@@ -42,6 +42,7 @@ mu_imap_rename (mu_imap_t imap, const char *mailbox, const char *new_mailbox)
   com.argc = 3;
   com.argv = argv;
   com.extra = NULL;
+  com.msgset = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/select.c b/libproto/imap/select.c
index 31c2b72..bd78218 100644
--- a/libproto/imap/select.c
+++ b/libproto/imap/select.c
@@ -102,7 +102,7 @@ mu_imap_select (mu_imap_t imap, const char *mbox, int writable,
     case MU_IMAP_CLIENT_READY:
       status = _mu_imap_tag_next (imap);
       MU_IMAP_CHECK_EAGAIN (imap, status);
-      status = mu_imapio_send_command (imap->io, imap->tag_str,
+      status = mu_imapio_send_command (imap->io, imap->tag_str, NULL,
 				       writable ? "SELECT" : "EXAMINE",
 				       mbox, NULL);
       MU_IMAP_CHECK_ERROR (imap, status);
diff --git a/libproto/imap/store.c b/libproto/imap/store.c
index be1349f..acec7a1 100644
--- a/libproto/imap/store.c
+++ b/libproto/imap/store.c
@@ -26,7 +26,7 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
+mu_imap_store (mu_imap_t imap, int uid, mu_msgset_t msgset, const char *items)
 {
   char const *argv[3];
   int i;
@@ -36,7 +36,7 @@ mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
   if (uid)
     argv[i++] = "UID";
   argv[i++] = "STORE";
-  argv[i++] = msgset;
+  argv[i++] = "\\";
   
   com.session_state = MU_IMAP_SESSION_SELECTED;
   com.capa = NULL;
@@ -44,6 +44,7 @@ mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
   com.argc = i;
   com.argv = argv;
   com.extra = items;
+  com.msgset = msgset;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/storeflg.c b/libproto/imap/storeflg.c
index eba59e4..8bc2e90 100644
--- a/libproto/imap/storeflg.c
+++ b/libproto/imap/storeflg.c
@@ -29,7 +29,7 @@
 #include <mailutils/sys/imap.h>
 
 int
-mu_imap_store_flags (mu_imap_t imap, int uid, const char *msgset,
+mu_imap_store_flags (mu_imap_t imap, int uid, mu_msgset_t msgset,
 		     int op, int flags)
 {
   int status;
@@ -50,8 +50,9 @@ mu_imap_store_flags (mu_imap_t imap, int uid, const char *msgset,
       mu_imapio_printf (imap->io, "%s ", imap->tag_str);
       if (uid)
 	mu_imapio_printf (imap->io, "UID ");
-      mu_imapio_printf (imap->io, "STORE %s %s", msgset,
-			cmd[op & MU_IMAP_STORE_OPMASK]);
+      mu_imapio_printf (imap->io, "STORE ");
+      mu_imapio_send_msgset (imap->io, msgset);
+      mu_imapio_printf (imap->io, " %s", cmd[op & MU_IMAP_STORE_OPMASK]);
       if (op & MU_IMAP_STORE_SILENT)
 	mu_imapio_printf (imap->io, ".SILENT");
       mu_imapio_printf (imap->io, " ");
diff --git a/libproto/imap/subscribe.c b/libproto/imap/subscribe.c
index f370f3a..3a71584 100644
--- a/libproto/imap/subscribe.c
+++ b/libproto/imap/subscribe.c
@@ -41,6 +41,7 @@ mu_imap_subscribe (mu_imap_t imap, const char *mailbox)
   com.argc = 2;
   com.argv = argv;
   com.extra = NULL;
+  com.msgset = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/libproto/imap/unselect.c b/libproto/imap/unselect.c
index c1ac499..3c23241 100644
--- a/libproto/imap/unselect.c
+++ b/libproto/imap/unselect.c
@@ -33,6 +33,7 @@ mu_imap_unselect (mu_imap_t imap)
     1,
     &command,
     NULL,
+    NULL,
     _mu_close_handler
   };
   return mu_imap_gencom (imap, &com);
diff --git a/libproto/imap/unsubscribe.c b/libproto/imap/unsubscribe.c
index 159b442..fc1084f 100644
--- a/libproto/imap/unsubscribe.c
+++ b/libproto/imap/unsubscribe.c
@@ -41,6 +41,7 @@ mu_imap_unsubscribe (mu_imap_t imap, const char *mailbox)
   com.argc = 2;
   com.argv = argv;
   com.extra = NULL;
+  com.msgset = NULL;
   com.tagged_handler = NULL;
   com.untagged_handler = NULL;
 
diff --git a/mu/imap.c b/mu/imap.c
index 0069ff5..8e3e2bf 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -27,6 +27,7 @@
 #include <mailutils/imap.h>
 #include <mailutils/imapio.h>
 #include <mailutils/imaputil.h>
+#include <mailutils/msgset.h>
 #include "mu.h"
 #include "argp.h"
 #include "xalloc.h"
@@ -110,6 +111,29 @@ com_verbose (int argc, char **argv)
   
 }
 
+static mu_msgset_t
+parse_msgset (const char *arg)
+{
+  int status;
+  mu_msgset_t msgset;
+  char *p;
+  
+  status = mu_msgset_create (&msgset, NULL, 0);
+  if (status)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, status);
+      return NULL;
+    }
+  status = mu_msgset_parse_imap (msgset, arg, &p);
+  if (status)
+    {
+      mu_error (_("failed to parse message set near \"%s\": %s"),
+		p, mu_strerror (status));
+      mu_msgset_destroy (&msgset);
+    }
+  return msgset;
+}
+
 static void
 report_failure (const char *what, int status)
 {
@@ -898,28 +922,39 @@ com_check (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
 static int
 com_fetch (int argc, char **argv)
 {
-  int status;
   mu_stream_t out = mutool_open_pager ();
-  
-  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
-				      imap_fetch_callback,
-				      out);
-  status = mu_imap_fetch (imap, uid_mode, argv[1], argv[2]);
-  mu_stream_destroy (&out);
-  mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
-				      imap_fetch_callback,
-				      mu_strout);
-  if (status)
-    report_failure ("fetch", status);
+  mu_msgset_t msgset = parse_msgset (argv[1]);
+
+  if (msgset)
+    {
+      int status;
+
+      mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
+					  imap_fetch_callback,
+					  out);
+      status = mu_imap_fetch (imap, uid_mode, msgset, argv[2]);
+      mu_stream_destroy (&out);
+      mu_imap_register_callback_function (imap, MU_IMAP_CB_FETCH,
+					  imap_fetch_callback,
+					  mu_strout);
+      mu_msgset_free (msgset);
+      if (status)
+	report_failure ("fetch", status);
+    }
   return 0;  
 }
 
 static int
 com_store (int argc, char **argv)
 {
-  int status = mu_imap_store (imap, uid_mode, argv[1], argv[2]);
-  if (status)
-    report_failure ("store", status);
+  mu_msgset_t msgset = parse_msgset (argv[1]);
+
+  if (msgset)
+    {
+      int status = mu_imap_store (imap, uid_mode, msgset, argv[2]);
+      if (status)
+	report_failure ("store", status);
+    }
   return 0;
 }
 
@@ -1076,9 +1111,14 @@ com_append (int argc, char **argv)
 static int
 com_copy (int argc, char **argv)
 {
-  int status = mu_imap_copy (imap, uid_mode, argv[1], argv[2]);
-  if (status)
-    report_failure ("copy", status);
+  mu_msgset_t msgset = parse_msgset (argv[1]);
+  if (msgset)
+    {
+      int status = mu_imap_copy (imap, uid_mode, msgset, argv[2]);
+      mu_msgset_free (msgset);
+      if (status)
+	report_failure ("copy", status);
+    }
   return 0;
 }
 
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 2a9f4ea..f115a86 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -54,6 +54,7 @@ noinst_PROGRAMS = \
  fldel\
  lstuid\
  mbdel\
+ msgset\
  mimetest\
  smtpsend\
  ufms
diff --git a/testsuite/msgset.c b/testsuite/msgset.c
new file mode 100644
index 0000000..ae2a099
--- /dev/null
+++ b/testsuite/msgset.c
@@ -0,0 +1,135 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/mailutils.h>
+
+static void
+parse_msgset (char *arg, struct mu_msgrange *range)
+{
+  size_t msgnum;
+  char *p;
+  
+  errno = 0;
+  msgnum = strtoul (arg, &p, 10);
+  range->msg_beg = msgnum;
+  if (*p == ':')
+    {
+      if (*++p == '*')
+	msgnum = 0;
+      else
+	{
+	  msgnum = strtoul (p, &p, 10);
+	  if (*p)
+	    {
+	      mu_error ("error in message range near %s", p);
+	      exit (1);
+	    }
+	}
+    }
+  else if (*p == '*')
+    msgnum = 0;
+  else if (*p)
+    {
+      mu_error ("error in message range near %s", p);
+      exit (1);
+    }
+
+  range->msg_end = msgnum;
+}
+
+int
+main (int argc, char **argv)
+{
+  int i;
+  char *msgset_string = NULL;
+  mu_msgset_t msgset;
+  int rc;
+  int flags = 0;
+  mu_mailbox_t mbox = NULL;
+  
+  mu_set_program_name (argv[0]);
+  mu_registrar_record (mu_mbox_record);
+  for (i = 1; i < argc; i++)
+    {
+      char *arg = argv[i];
+
+      if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
+	{
+	  mu_printf ("usage: %s [-msgset=SET] [-add X[:Y]] [-del X:[Y]]...\n",
+		     mu_program_name);
+	  return 0;
+	}
+      else if (strncmp (arg, "-msgset=", 8) == 0)
+	msgset_string = arg + 8;
+      else if (strcmp (arg, "-uid") == 0)
+	flags |= MU_MSGSET_UID;
+      else if (strncmp (arg, "-mailbox=", 9) == 0)
+	{
+	  MU_ASSERT (mu_mailbox_create (&mbox, arg + 9));
+	  MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_READ));
+	}
+      else
+	break;
+    }
+
+  MU_ASSERT (mu_msgset_create (&msgset, mbox, flags));
+  if (msgset_string)
+    {
+      char *end;
+      rc = mu_msgset_parse_imap (msgset, msgset_string, &end);
+      if (rc)
+	{
+	  mu_error ("mu_msgset_parse_imap: %s near %s",
+		    mu_strerror (rc), end);
+	  return 1;
+	}
+    }
+  
+  for (; i < argc; i++)
+    {
+      char *arg = argv[i];
+      struct mu_msgrange range;
+      
+      if (strncmp (arg, "-add=", 5) == 0)
+	{
+	  parse_msgset (arg + 5, &range);
+	  MU_ASSERT (mu_msgset_add_range (msgset, range.msg_beg,
+					  range.msg_end));
+	}
+      else if (strncmp (arg, "-sub=", 5) == 0)
+	{
+	  parse_msgset (arg + 5, &range);
+	  MU_ASSERT (mu_msgset_sub_range (msgset, range.msg_beg,
+					  range.msg_end));
+	}
+      else
+      	{
+	  mu_error ("unknown option %s", arg);
+	  return 1;
+	}
+    }
+  MU_ASSERT (mu_msgset_print (mu_strout, msgset));
+  mu_printf ("\n");
+  mu_msgset_free (msgset);
+  
+  return 0;
+}
+  
+	     
+
+	


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Tue Dec 27 04:02:03 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RfSv5-0002Zf-BH
	for mharc-commit-mailutils@gnu.org; Tue, 27 Dec 2011 04:02:03 -0500
Received: from eggs.gnu.org ([140.186.70.92]:54715)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfSv2-0002Z2-Ou
	for commit-mailutils@gnu.org; Tue, 27 Dec 2011 04:02:01 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfSuy-0002if-Hp
	for commit-mailutils@gnu.org; Tue, 27 Dec 2011 04:02:00 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:48435)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfSuy-0002ib-Ch
	for commit-mailutils@gnu.org; Tue, 27 Dec 2011 04:01:56 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RfSuw-0000j9-KZ
	for commit-mailutils@gnu.org; Tue, 27 Dec 2011 09:01:55 +0000
Date: Tue, 27 Dec 2011 09:01:55 +0000
Message-Id: <E1RfSuw-0000j9-KZ@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-556-ge7ab689
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 37fc6c3c41b16823e9b130a2a425ed40eebf09f8
X-Git-Newrev: e7ab6892fa88d513d83945121c9b6864db4b30f7
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 27 Dec 2011 09:02:02 -0000

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

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

The branch, master has been updated
       via  e7ab6892fa88d513d83945121c9b6864db4b30f7 (commit)
      from  37fc6c3c41b16823e9b130a2a425ed40eebf09f8 (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 e7ab6892fa88d513d83945121c9b6864db4b30f7
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Tue Dec 27 11:03:34 2011 +0200

    Update TODO.

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

Summary of changes:
 TODO |   14 --------------
 1 files changed, 0 insertions(+), 14 deletions(-)

diff --git a/TODO b/TODO
index 8c0c1f0..be576c8 100644
--- a/TODO
+++ b/TODO
@@ -16,18 +16,12 @@ See also mu_rfc2822_in_reply_to.
 
 * Check if _mu_smtp_trace_disable correctly dereferences streams.
 
-* re-implement imap4 client (in progress)
-
 * re-implement nntp client
 
 * properties:
 
-** implement libproperty - a library of various storage backends for mu_property_t.
-
 ** consider a possibility of typed properties (integer, unsigned, string, ...)
 
-* vacation: link with libproperty when it is ready
-
 * remove libmu_auth, rewrite mu_auth support via mu_property_t
 
 * locale-dependent variant of mu_rfc2047_decode
@@ -38,16 +32,8 @@ See rfc2047_decode_wrapper in frm/common.c for a prototype.
 
 See frm/common.c (near line 425).
 
-* sieve.scm is exempted from installcheck.
-
-See guimb/scm/Makefile.am for a discussion.
-
-* sieve: extension tests [in progress]
-
 * mu_address_createv: pass hints as in mu_address_create_hint?
 
-* fix Python support
-
 * fix C++ support
 
 * redo libraries as loadable modules (where possible)


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 30 13:36:01 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RghJB-00033f-4R
	for mharc-commit-mailutils@gnu.org; Fri, 30 Dec 2011 13:36:01 -0500
Received: from eggs.gnu.org ([140.186.70.92]:36477)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RghJ1-00033X-RJ
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 13:35:59 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RghIt-000169-4G
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 13:35:51 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:46020)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RghIs-000165-Qq
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 13:35:43 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RghIr-0000Jt-Qv
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 18:35:42 +0000
Date: Fri, 30 Dec 2011 18:35:41 +0000
Message-Id: <E1RghIr-0000Jt-Qv@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-558-ga01bb41
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: e7ab6892fa88d513d83945121c9b6864db4b30f7
X-Git-Newrev: a01bb419262e90164a558075e3607fb3b0514443
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 30 Dec 2011 18:35:59 -0000

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

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

The branch, master has been updated
       via  a01bb419262e90164a558075e3607fb3b0514443 (commit)
       via  26cfa2197d5a2866f1a08c48f94d65504fdef7b1 (commit)
      from  e7ab6892fa88d513d83945121c9b6864db4b30f7 (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 a01bb419262e90164a558075e3607fb3b0514443
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 30 19:52:49 2011 +0200

    Improve msgset interface.  Use it in MH.
    
    * libmailutils/msgset/Makefile.am (libmsgset_la_SOURCES): Add new files.
    * libmailutils/msgset/addset.c: New file.
    * libmailutils/msgset/count.c: New file.
    * libmailutils/msgset/foreachmsgno.c: New file.
    * libmailutils/msgset/foreachuid.c: New file.
    * libmailutils/msgset/isempty.c: New file.
    * libmailutils/msgset/subset.c: New file.
    * libmailutils/msgset/trans.c: New file.
    * libmailutils/msgset/add.c (mu_msgset_add_range): Take fourth argument
    specifying the conversion mode. Translate numbers as necessary.
    * libmailutils/msgset/foreachmsg.c: Translate numbers as necessary.
    * libmailutils/msgset/foreachnum.c: Rewrite.
    * libmailutils/msgset/negate.c (_invert_range)
    (mu_msgset_add_range): Update calls to mu_msgset_add_range.
    * libmailutils/msgset/parse.c (parse_msgnum_env)<mode>: New member.
    (parse_msgrange): Update calls to mu_msgset_add_range.
    (mu_msgset_parse_imap): Change signature. All callers updated.
    * libmailutils/msgset/sub.c (mu_msgset_sub_range): Take fourth argument
    specifying the conversion mode. Translate numbers as necessary.
    
    * imap4d/copy.c (imap4d_copy0): Update calls to mu_msgset_create
    and mu_msgset_parse_imap.
    * imap4d/fetch.c (fetch_thunk): Likewise.
    * imap4d/search.c (parse_simple_key): Likewise.
    * imap4d/store.c (store_thunk): Likewise.
    * include/mailutils/msgset.h (MU_MSGSET_NUM,MU_MSGSET_UID): New defines.
    (mu_msgset_add,mu_msgset_sub): New protos.
    (mu_msgset_add_range,mu_msgset_sub_range): Take 4 arguments. All callers
    changed.
    * include/mailutils/sys/msgset.h (_mu_msgset_translate_pair)
    (_mu_msgset_translate_range): New protos.
    
    * libmailutils/tests/msgset.c: Reflect the above changes.
    * testsuite/msgset.c: Likewise.
    * libproto/imap/mbox.c: Update calls to mu_msgset functions.
    
    * mh/mh.h (mh_msgset_t): Remove.
    (mh_iterator_fp): Remove typedef.
    (mh_msgset_parse): Change signature.
    (mh_msgset_member,mh_msgset_reverse,mh_msgset_negate)
    (mh_msgset_current,mh_msgset_free,mh_msgset_uids): Remove protos.
    (mh_msgset_parse_string): New proto.
    (mh_msgset_first_current,mh_msgset_first): New proto.
    (mh_iterate: Remove proto. Use mu_msgset_foreach* functions instead.
    (mh_seq_add,mh_seq_delete): Change signatures.
    * mh/mh_init.c (mh_iterate): Remove.
    * mh/mh_msgset.c: Rewrite using mu_msgset_t.
    * mh/mh_sequence.c: Likewise.
    
    * mh/anno.c: Use new msgset functions.
    * mh/burst.c: Likewise.
    * mh/comp.c: Likewise.
    * mh/folder.c: Likewise.
    * mh/forw.c: Likewise.
    * mh/mark.c: Likewise.
    * mh/mhn.c: Likewise.
    * mh/mhpath.c: Likewise.
    * mh/mhseq.c: Likewise.
    * mh/pick.c: Likewise.
    * mh/refile.c: Likewise.
    * mh/repl.c: Likewise.
    * mh/rmm.c: Likewise.
    * mh/scan.c: Likewise.
    * mh/send.c: Likewise.
    * mh/sortm.c: Likewise.
    * mh/whatnowenv.c: Likewise.
    * mh/tests/mark.at: Reflect changes in the format of the saved
    sequences, which may contain ranges now.
    
    * mu/imap.c: Update calls to mu_msgset functions.

commit 26cfa2197d5a2866f1a08c48f94d65504fdef7b1
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Wed Dec 28 12:04:30 2011 +0200

    msgset: implement "foreach" calls.
    
    * imap4d/copy.c (copy_env) <src>: Remove. Not needed now.
    (size_sum,do_copy): Change to a mu_msgset_message_action_t.
    (try_copy,safe_copy): Change signature; use mu_msgset_foreach_message.
    * imap4d/fetch.c (_fetch_from_message): Change to a
    mu_msgset_message_action_t.
    (imap4d_fetch0): Use mu_msgset_foreach_message.
    * imap4d/imap4d.h (imap4d_message_action_t): Remove typedef.
    (util_foreach_message): Remove. Use mu_msgset_foreach_message.
    * imap4d/store.c (_do_store): Change to a mu_msgset_message_action_t.
    (imap4d_store0): Use mu_msgset_foreach_message.
    * imap4d/util.c (util_foreach_message): Remove.
    * include/mailutils/list.h (mu_list_foreach_dir): New proto.
    * include/mailutils/msgset.h (mu_msgset_msgno_action_t)
    (mu_msgset_message_action_t): New typedefs.
    (mu_msgset_negate,mu_msgset_foreach_dir_msgno)
    (mu_msgset_foreach_msgno,mu_msgset_foreach_dir_message)
    (mu_msgset_foreach_message): New protos.
    * libmailutils/list/foreachdir.c: New file.
    * libmailutils/list/Makefile.am (liblist_la_SOURCES): Add foreachdir.c.
    * libmailutils/list/head.c (mu_list_head): Fix conditional.
    * libmailutils/list/tail.c (mu_list_tail): Likewise.
    * libmailutils/msgset/foreachmsg.c: New file.
    * libmailutils/msgset/foreachnum.c: New file.
    * libmailutils/msgset/negate.c: New file.
    * libmailutils/msgset/Makefile.am (libmsgset_la_SOURCES): Add
    new files.
    * libmailutils/msgset/getitr.c (mu_msgset_get_iterator): Call
    mu_msgset_aggregate before doing anything.
    * libmailutils/msgset/getlist.c (mu_msgset_get_list): Likewise.
    * libmailutils/msgset/locate.c (mu_msgset_locate): Likewise.
    * libmailutils/msgset/parse.c (parse_msgrange): Remove duplicated
    code.
    * testsuite/msgset.c: Implement -neg option.

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

Summary of changes:
 imap4d/copy.c                                  |   59 ++---
 imap4d/fetch.c                                 |   25 +-
 imap4d/imap4d.h                                |    5 -
 imap4d/search.c                                |   14 +-
 imap4d/store.c                                 |   13 +-
 imap4d/util.c                                  |   36 ---
 include/mailutils/list.h                       |    7 +-
 include/mailutils/msgset.h                     |   58 ++++-
 include/mailutils/sys/msgset.h                 |    5 +
 libmailutils/list/Makefile.am                  |    1 +
 libmailutils/list/{foreach.c => foreachdir.c}  |   31 +--
 libmailutils/list/head.c                       |    2 +-
 libmailutils/list/tail.c                       |    2 +-
 libmailutils/msgset/Makefile.am                |   12 +-
 libmailutils/msgset/add.c                      |   14 +-
 libmailutils/msgset/{locate.c => addset.c}     |   26 ++-
 libmailutils/msgset/{add.c => count.c}         |   31 ++-
 libmailutils/msgset/foreachmsg.c               |  105 ++++++++
 libmailutils/msgset/{free.c => foreachmsgno.c} |   32 +--
 libmailutils/msgset/foreachnum.c               |  106 ++++++++
 libmailutils/msgset/{getitr.c => foreachuid.c} |   23 +-
 libmailutils/msgset/getitr.c                   |    4 +
 libmailutils/msgset/getlist.c                  |    4 +
 libmailutils/msgset/{clear.c => isempty.c}     |   10 +-
 libmailutils/msgset/locate.c                   |    4 +
 libmailutils/msgset/negate.c                   |   97 +++++++
 libmailutils/msgset/parse.c                    |   34 +--
 libmailutils/msgset/sub.c                      |   19 +-
 libmailutils/msgset/{locate.c => subset.c}     |   26 ++-
 libmailutils/msgset/trans.c                    |   99 +++++++
 libmailutils/tests/msgset.c                    |   73 ++++--
 libproto/imap/mbox.c                           |   29 +-
 mh/anno.c                                      |   21 +-
 mh/burst.c                                     |   83 ++++---
 mh/comp.c                                      |   21 +-
 mh/folder.c                                    |   39 ++-
 mh/forw.c                                      |   48 ++--
 mh/mark.c                                      |   10 +-
 mh/mh.h                                        |   35 +--
 mh/mh_init.c                                   |   39 +---
 mh/mh_msgset.c                                 |  328 +++++++++++-------------
 mh/mh_sequence.c                               |  189 +++++++-------
 mh/mhn.c                                       |   30 ++-
 mh/mhpath.c                                    |   15 +-
 mh/mhseq.c                                     |   19 +-
 mh/pick.c                                      |   48 +---
 mh/refile.c                                    |   11 +-
 mh/repl.c                                      |   18 +-
 mh/rmm.c                                       |   11 +-
 mh/scan.c                                      |   19 +-
 mh/send.c                                      |   46 ++--
 mh/sortm.c                                     |   83 +++++--
 mh/tests/mark.at                               |    6 +-
 mh/whatnowenv.c                                |   38 +--
 mu/imap.c                                      |    4 +-
 testsuite/msgset.c                             |  155 ++++++++++--
 56 files changed, 1451 insertions(+), 871 deletions(-)
 copy libmailutils/list/{foreach.c => foreachdir.c} (68%)
 copy libmailutils/msgset/{locate.c => addset.c} (67%)
 copy libmailutils/msgset/{add.c => count.c} (71%)
 create mode 100644 libmailutils/msgset/foreachmsg.c
 copy libmailutils/msgset/{free.c => foreachmsgno.c} (61%)
 create mode 100644 libmailutils/msgset/foreachnum.c
 copy libmailutils/msgset/{getitr.c => foreachuid.c} (61%)
 copy libmailutils/msgset/{clear.c => isempty.c} (86%)
 create mode 100644 libmailutils/msgset/negate.c
 copy libmailutils/msgset/{locate.c => subset.c} (67%)
 create mode 100644 libmailutils/msgset/trans.c

diff --git a/imap4d/copy.c b/imap4d/copy.c
index f2c2314..63f262d 100644
--- a/imap4d/copy.c
+++ b/imap4d/copy.c
@@ -58,57 +58,35 @@ imap4d_copy (struct imap4d_command *command, imap4d_tokbuf_t tok)
 struct copy_env
 {
   mu_mailbox_t dst;
-  mu_mailbox_t src;
   mu_off_t total;
   int ret;
   char **err_text;
 };
 
 static int
-size_sum (size_t msgno, void *data)
+size_sum (size_t msgno, mu_message_t msg, void *data)
 {
   struct copy_env *env = data;
-  mu_message_t msg = NULL;
   int rc;
   
-  rc = mu_mailbox_get_message (env->src, msgno, &msg);
+  size_t size;
+  rc = mu_message_size (msg, &size);
   if (rc)
     {
-      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL, rc);
-      env->ret = RESP_NO;
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_message_size", NULL, rc);
+      env->ret = RESP_BAD;
       return MU_ERR_FAILURE;
     }
-  else 
-    {
-      size_t size;
-      rc = mu_message_size (msg, &size);
-      if (rc)
-	{
-	  mu_diag_funcall (MU_DIAG_ERROR, "mu_message_size", NULL, rc);
-	  env->ret = RESP_BAD;
-	  return MU_ERR_FAILURE;
-	}
-      env->total += size;
-    }
+  env->total += size;
   return 0;
 }
 
 static int
-do_copy (size_t msgno, void *data)
+do_copy (size_t msgno, mu_message_t msg, void *data)
 {
   struct copy_env *env = data;
-  mu_message_t msg = NULL;
   int status;
 
-  status = mu_mailbox_get_message (env->src, msgno, &msg);
-  if (status)
-    {
-      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL,
-		       status);
-      env->ret = RESP_BAD;
-      return MU_ERR_FAILURE;
-    }
-
   imap4d_enter_critical ();
   status = mu_mailbox_append_message (env->dst, msg);
   imap4d_leave_critical ();
@@ -124,14 +102,12 @@ do_copy (size_t msgno, void *data)
 }
 
 static int
-try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
-	  char **err_text)
+try_copy (mu_mailbox_t dst, mu_msgset_t msgset, char **err_text)
 {
   int rc;
   struct copy_env env;
 
   env.dst = dst;
-  env.src = src;
   env.total = 0;
   env.ret = RESP_OK;
   env.err_text = err_text;
@@ -139,7 +115,7 @@ try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
   *env.err_text = "Operation failed";
 
   /* Check size */
-  rc = util_foreach_message (msgset, size_sum, &env);
+  rc = mu_msgset_foreach_message (msgset, size_sum, &env);
   if (rc)
     return RESP_NO;
   if (env.ret != RESP_OK)
@@ -151,7 +127,7 @@ try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
       return RESP_NO;
     }
   env.total = 0;
-  rc = util_foreach_message (msgset, do_copy, &env);
+  rc = mu_msgset_foreach_message (msgset, do_copy, &env);
   quota_update (env.total);
   if (rc)
     return RESP_NO;
@@ -159,8 +135,7 @@ try_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
 }
   
 static int
-safe_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
-	   char **err_text)
+safe_copy (mu_mailbox_t dst, mu_msgset_t msgset, char **err_text)
 {
   size_t nmesg;
   int status;
@@ -174,7 +149,7 @@ safe_copy (mu_mailbox_t dst, mu_mailbox_t src, mu_msgset_t msgset,
       return RESP_NO;
     }
 
-  status = try_copy (dst, src, msgset, err_text);
+  status = try_copy (dst, msgset, err_text);
   if (status != RESP_OK)
     {
       size_t maxmesg;
@@ -236,7 +211,7 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
   mu_mailbox_t cmbox = NULL;
   int arg = IMAP4_ARG_1 + !!isuid;
   int ns;
-
+  
   *err_text = NULL;
   if (imap4d_tokbuf_argc (tok) != arg + 2)
     {
@@ -246,14 +221,16 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
   
   msgset_str = imap4d_tokbuf_getarg (tok, arg);
   name = imap4d_tokbuf_getarg (tok, arg + 1);
-  status = mu_msgset_create (&msgset, mbox, isuid ? MU_MSGSET_UID : 0);
+  status = mu_msgset_create (&msgset, mbox, MU_MSGSET_NUM);
   if (!status)
     {
       *err_text = "Software error";
       return RESP_BAD;
     }
     
-  status = mu_msgset_parse_imap (msgset, msgset_str, &end);
+  status = mu_msgset_parse_imap (msgset,
+				 isuid ? MU_MSGSET_UID : MU_MSGSET_NUM,
+				 msgset_str, &end);
   if (status)
     {
       mu_msgset_free (msgset);
@@ -283,7 +260,7 @@ imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 	  mu_list_t msglist;
 	  mu_msgset_get_list (msgset, &msglist);
 	  if (!mu_list_is_empty (msglist))
-	    status = safe_copy (cmbox, mbox, msgset, err_text);
+	    status = safe_copy (cmbox, msgset, err_text);
 	  mu_mailbox_close (cmbox);
 	}
       mu_mailbox_destroy (&cmbox);
diff --git a/imap4d/fetch.c b/imap4d/fetch.c
index 46cb835..a73d169 100644
--- a/imap4d/fetch.c
+++ b/imap4d/fetch.c
@@ -1759,13 +1759,14 @@ fetch_thunk (imap4d_parsebuf_t pb)
   
   mstr = imap4d_parsebuf_next (pb, 1);
 
-  status = mu_msgset_create (&pclos->msgset, mbox,
-			     pclos->isuid ? MU_MSGSET_UID : 0);
+  status = mu_msgset_create (&pclos->msgset, mbox, MU_MSGSET_NUM);
   if (status)
     imap4d_parsebuf_exit (pb, "Software error");
   
   /* Parse sequence numbers. */
-  status = mu_msgset_parse_imap (pclos->msgset, mstr, &end);
+  status = mu_msgset_parse_imap (pclos->msgset,
+				 pclos->isuid ? MU_MSGSET_UID : MU_MSGSET_NUM,
+				 mstr, &end);
   if (status)
     imap4d_parsebuf_exit (pb, "Failed to parse message set");
   
@@ -1783,19 +1784,19 @@ fetch_thunk (imap4d_parsebuf_t pb)
 }
 
 int
-_fetch_from_message (size_t msgno, void *data)
+_fetch_from_message (size_t msgno, mu_message_t msg, void *data)
 {
   int rc = 0;
   struct fetch_runtime_closure *frc = data;
 
   frc->msgno = msgno;
-  if (mu_mailbox_get_message (mbox, msgno, &frc->msg) == 0)
-    {
-      io_sendf ("* %lu FETCH (", (unsigned long) msgno);
-      frc->eltno = 0;
-      rc = mu_list_foreach (frc->fnlist, _do_fetch, frc);
-      io_sendf (")\n");
-    }
+  frc->msg = msg;
+
+  io_sendf ("* %lu FETCH (", (unsigned long) msgno);
+  frc->eltno = 0;
+  rc = mu_list_foreach (frc->fnlist, _do_fetch, frc);
+  io_sendf (")\n");
+
   return rc;
 }
 
@@ -1833,7 +1834,7 @@ imap4d_fetch0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
 	 loop below */
       frc.err_text = "Completed";
 
-      util_foreach_message (pclos.msgset, _fetch_from_message, &frc);
+      mu_msgset_foreach_message (pclos.msgset, _fetch_from_message, &frc);
       mu_list_destroy (&frc.msglist);
     }
   
diff --git a/imap4d/imap4d.h b/imap4d/imap4d.h
index 22b3b28..a737363 100644
--- a/imap4d/imap4d.h
+++ b/imap4d/imap4d.h
@@ -372,11 +372,6 @@ extern char *util_getfullpath (const char *);
 extern struct imap4d_command *util_getcommand (char *, 
                                                struct imap4d_command []);
 
-typedef int (*imap4d_message_action_t) (size_t, void *);
-
-int util_foreach_message (mu_msgset_t list, imap4d_message_action_t action,
-			  void *data);
-
 enum datetime_parse_mode     /* how to parse date/time strings */
   {
     datetime_default,        /* default mode */
diff --git a/imap4d/search.c b/imap4d/search.c
index 2441810..69f4bae 100644
--- a/imap4d/search.c
+++ b/imap4d/search.c
@@ -608,10 +608,11 @@ parse_simple_key (struct parsebuf *pb)
   
   if (!condp->name)
     {
-      mu_msgset_t msgset = parse_msgset_create (pb, mbox,
-						pb->isuid ? MU_MSGSET_UID : 0);
+      mu_msgset_t msgset = parse_msgset_create (pb, mbox, MU_MSGSET_NUM);
       
-      if (mu_msgset_parse_imap (msgset, pb->token, NULL) == 0) 
+      if (mu_msgset_parse_imap (msgset,
+				pb->isuid ? MU_MSGSET_UID : MU_MSGSET_NUM,
+				pb->token, NULL) == 0) 
 	{
 	  struct search_node *np = parse_alloc (pb, sizeof *np);
 	  np->type = node_value;
@@ -696,9 +697,10 @@ parse_simple_key (struct parsebuf *pb)
 	      break;
 	      
 	    case 'u': /* UID message set */
-	      arg->v.value.v.msgset = parse_msgset_create (pb, NULL, 0);
-	      if (mu_msgset_parse_imap (arg->v.value.v.msgset, pb->token,
-					NULL)) 
+	      arg->v.value.v.msgset = parse_msgset_create (pb, NULL,
+							   MU_MSGSET_NUM);
+	      if (mu_msgset_parse_imap (arg->v.value.v.msgset, MU_MSGSET_UID,
+					pb->token, NULL)) 
 		{
 		  mu_msgset_free (arg->v.value.v.msgset);
 		  pb->err_mesg = "Bogus number set";
diff --git a/imap4d/store.c b/imap4d/store.c
index 0fe425d..21bc86a 100644
--- a/imap4d/store.c
+++ b/imap4d/store.c
@@ -69,13 +69,14 @@ store_thunk (imap4d_parsebuf_t p)
 	imap4d_parsebuf_exit (p, "Bogus data suffix");
     }
 
-  status = mu_msgset_create (&pclos->msgset, mbox,
-			     pclos->isuid ? MU_MSGSET_UID : 0);
+  status = mu_msgset_create (&pclos->msgset, mbox, MU_MSGSET_NUM);
   if (status)
     imap4d_parsebuf_exit (p, "Software error");
   
   /* Get the message numbers in set[].  */
-  status = mu_msgset_parse_imap (pclos->msgset, mstr, &end);
+  status = mu_msgset_parse_imap (pclos->msgset,
+				 pclos->isuid ? MU_MSGSET_UID : MU_MSGSET_NUM,
+				 mstr, &end);
   if (status)
     imap4d_parsebuf_exit (p, "Failed to parse message set");
 
@@ -91,13 +92,11 @@ store_thunk (imap4d_parsebuf_t p)
 }
 
 static int
-_do_store (size_t msgno, void *data)
+_do_store (size_t msgno, mu_message_t msg, void *data)
 {
   struct store_parse_closure *pclos = data;
-  mu_message_t msg = NULL;
   mu_attribute_t attr = NULL;
       
-  mu_mailbox_get_message (mbox, msgno, &msg);
   mu_message_get_attribute (msg, &attr);
 	      
   switch (pclos->how)
@@ -154,7 +153,7 @@ imap4d_store0 (imap4d_tokbuf_t tok, int isuid, char **ptext)
 			     ptext);
   if (rc == RESP_OK)
     {
-      util_foreach_message (pclos.msgset, _do_store, &pclos);
+      mu_msgset_foreach_message (pclos.msgset, _do_store, &pclos);
     
       *ptext = "Completed";
     }
diff --git a/imap4d/util.c b/imap4d/util.c
index c5341de..1a09610 100644
--- a/imap4d/util.c
+++ b/imap4d/util.c
@@ -39,42 +39,6 @@ util_getfullpath (const char *name)
   return mu_normalize_path (exp);
 }
 
-struct action_closure
-{
-  imap4d_message_action_t action;
-  void *data;
-};
-
-static int
-procrange (void *item, void *data)
-{
-  struct mu_msgrange *mp = item;
-  struct action_closure *clos = data;
-  size_t i;
-
-  for (i = mp->msg_beg; i <= mp->msg_end; i++)
-    {
-      int rc = clos->action (i, clos->data);
-      if (rc)
-	return rc;
-    }
-  return 0;
-}
-
-/* Apply ACTION to each message number from LIST. */
-int
-util_foreach_message (mu_msgset_t msgset, imap4d_message_action_t action,
-		      void *data)
-{
-  mu_list_t list;
-  struct action_closure clos;
-  
-  clos.action = action;
-  clos.data = data;
-  mu_msgset_get_list (msgset, &list);
-  return mu_list_foreach (list, procrange, &clos);
-}
-
 int
 util_do_command (imap4d_tokbuf_t tok)
 {
diff --git a/include/mailutils/list.h b/include/mailutils/list.h
index 184c7ec..6194f10 100644
--- a/include/mailutils/list.h
+++ b/include/mailutils/list.h
@@ -144,10 +144,15 @@ int mu_list_get_iterator (mu_list_t _list, mu_iterator_t *_pitr);
 typedef int (*mu_list_action_t) (void *_item, void *_data);
 
   /* Execute _action for each element in _list.  Use _data as the call-specific
-     data. */
+     data.  If _dir is 0, traverse the list from head to tail.  If it is 1,
+     traverse it in the reverse direction */
+int mu_list_foreach_dir (mu_list_t _list, int _dir, mu_list_action_t _action,
+			 void *_cbdata);
+  /* Same as mu_list_foreach_dir with _dir==0. */
 int mu_list_foreach (mu_list_t _list, mu_list_action_t _action, void *_data);
   /* A historical alias to the above. */
 int mu_list_do (mu_list_t, mu_list_action_t, void *) MU_DEPRECATED;
+  
 
   /* ************************************************* */
   /* Functions for combining two lists.                */
diff --git a/include/mailutils/msgset.h b/include/mailutils/msgset.h
index f9ae5b2..b665143 100644
--- a/include/mailutils/msgset.h
+++ b/include/mailutils/msgset.h
@@ -18,6 +18,8 @@
 #ifndef _MAILUTILS_MSGSET_H
 #define _MAILUTILS_MSGSET_H
 
+# include <mailutils/types.h>
+  
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -31,28 +33,68 @@ struct mu_msgrange
 /* Message numbers start with 1.  MU_MSGNO_LAST denotes the last
    message. */
 #define MU_MSGNO_LAST   0
+
+#define MU_MSGSET_NUM   0      /* Message set operates on sequence numbers */   
+#define MU_MSGSET_UID   1      /* Message set operates on UIDs */
+
+#define MU_MSGSET_MODE_MASK 0x0f
   
-#define MU_MSGSET_UID   0x01   /* Message set operates on UIDs */
-  
-int mu_msgset_create (mu_msgset_t *pmsgset, mu_mailbox_t mbox, int flags);
+int mu_msgset_create (mu_msgset_t *pmsgset, mu_mailbox_t mbox, int mode);
 int mu_msgset_get_list (mu_msgset_t msgset, mu_list_t *plist);
 int mu_msgset_get_iterator (mu_msgset_t msgset, mu_iterator_t *pitr);
 
-int mu_msgset_add_range (mu_msgset_t list, size_t beg, size_t end);
-int mu_msgset_sub_range (mu_msgset_t list, size_t beg, size_t end);
-  /*int mu_msgset_add_set (mu_msgset_t a, mu_msgset_t b);*/
-  /*int mu_msgset_sub_set (mu_msgset_t a, mu_msgset_t b);*/
+int mu_msgset_add_range (mu_msgset_t set, size_t beg, size_t end, int mode);
+int mu_msgset_sub_range (mu_msgset_t set, size_t beg, size_t end, int mode);
+int mu_msgset_add (mu_msgset_t a, mu_msgset_t b);
+int mu_msgset_sub (mu_msgset_t a, mu_msgset_t b);
 int mu_msgset_aggregate (mu_msgset_t set);
 int mu_msgset_clear (mu_msgset_t set);
 void mu_msgset_free (mu_msgset_t set);
 void mu_msgset_destroy (mu_msgset_t *set);
   
-int mu_msgset_parse_imap (mu_msgset_t set, const char *s, char **end);
+int mu_msgset_parse_imap (mu_msgset_t set, int mode, const char *s, char **end);
 
 int mu_msgset_print (mu_stream_t str, mu_msgset_t msgset);
   
 int mu_msgset_locate (mu_msgset_t msgset, size_t n,
 		      struct mu_msgrange const **prange);
+
+int mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset);
+
+int mu_msgset_count (mu_msgset_t mset, size_t *pcount);
+int mu_msgset_is_empty (mu_msgset_t mset);
+  
+typedef int (*mu_msgset_msgno_action_t) (size_t _n, void *_call_data);
+typedef int (*mu_msgset_message_action_t) (size_t _n, mu_message_t _msg,
+					   void *_call_data);
+
+#define MU_MSGSET_FOREACH_FORWARD  0x00
+#define MU_MSGSET_FOREACH_BACKWARD 0x10
+
+int mu_msgset_foreach_num (mu_msgset_t _msgset, int _flags,
+			   mu_msgset_msgno_action_t _action,
+			   void *_call_data);
+  
+int mu_msgset_foreach_dir_msgno (mu_msgset_t _msgset, int _dir,
+				 mu_msgset_msgno_action_t _action,
+				 void *_data);
+int mu_msgset_foreach_msgno (mu_msgset_t _msgset,
+			     mu_msgset_msgno_action_t _action,
+			     void *_call_data);
+int mu_msgset_foreach_dir_msguid (mu_msgset_t _msgset, int _dir,
+				  mu_msgset_msgno_action_t _action,
+				  void *_data);
+int mu_msgset_foreach_msguid (mu_msgset_t _msgset,
+			      mu_msgset_msgno_action_t _action,
+			      void *_data);
+  
+int mu_msgset_foreach_dir_message (mu_msgset_t _msgset, int _dir,
+				   mu_msgset_message_action_t _action,
+				   void *_call_data);
+int mu_msgset_foreach_message (mu_msgset_t _msgset,
+			       mu_msgset_message_action_t _action,
+			       void *_call_data);
+
   
 #ifdef __cplusplus
 }
diff --git a/include/mailutils/sys/msgset.h b/include/mailutils/sys/msgset.h
index 9ecb053..7de6d61 100644
--- a/include/mailutils/sys/msgset.h
+++ b/include/mailutils/sys/msgset.h
@@ -28,4 +28,9 @@ struct _mu_msgset
   int flags;                /* Message set flags */
 };
 
+int _mu_msgset_translate_pair (mu_msgset_t mset, int mode,
+			       size_t *beg, size_t *end);
+int _mu_msgset_translate_range (mu_msgset_t mset, int mode,
+				struct mu_msgrange *r);
+
 #endif
diff --git a/libmailutils/list/Makefile.am b/libmailutils/list/Makefile.am
index 21a600b..e583fc7 100644
--- a/libmailutils/list/Makefile.am
+++ b/libmailutils/list/Makefile.am
@@ -29,6 +29,7 @@ liblist_la_SOURCES = \
  foreach.c\
  get.c\
  getcomp.c\
+ foreachdir.c\
  gmap.c\
  head.c\
  insert.c\
diff --git a/libmailutils/list/foreach.c b/libmailutils/list/foreachdir.c
similarity index 68%
copy from libmailutils/list/foreach.c
copy to libmailutils/list/foreachdir.c
index 8c0d2a3..7baa3b5 100644
--- a/libmailutils/list/foreach.c
+++ b/libmailutils/list/foreachdir.c
@@ -24,32 +24,29 @@
 #include <mailutils/errno.h>
 
 int
-mu_list_foreach (mu_list_t list, mu_list_action_t action, void *cbdata)
+mu_list_foreach_dir (mu_list_t list, int dir,
+		     mu_list_action_t action, void *cbdata)
 {
   mu_iterator_t itr;
   int status = 0;
-
+  
   if (list == NULL || action == NULL)
     return EINVAL;
   status = mu_list_get_iterator (list, &itr);
   if (status)
     return status;
-  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
-       mu_iterator_next (itr))
-    {
-      void *item;
-      mu_iterator_current (itr, &item);
-      if ((status = action (item, cbdata)))
-	break;
-    }
+
+  status = mu_iterator_ctl (itr, mu_itrctl_set_direction, &dir);
+  if (status == 0)
+    for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
+	 mu_iterator_next (itr))
+      {
+	void *item;
+	mu_iterator_current (itr, &item);
+	if ((status = action (item, cbdata)))
+	  break;
+      }
   mu_iterator_destroy (&itr);
   return status;
 }
 
-/* Retained for compatibility with previous versions.
-   In the future it will be removed, or changed to a define or weak alias. */
-int
-mu_list_do (mu_list_t list, mu_list_action_t action, void *cbdata)
-{
-  return mu_list_foreach (list, action, cbdata);
-}
diff --git a/libmailutils/list/head.c b/libmailutils/list/head.c
index 7c52b89..fd5790b 100644
--- a/libmailutils/list/head.c
+++ b/libmailutils/list/head.c
@@ -29,7 +29,7 @@ mu_list_head (mu_list_t list, void **pitem)
     return EINVAL;
   if (pitem == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  if (!list->head.next)
+  if (list->head.next == &list->head)
     return MU_ERR_NOENT;
   *pitem = list->head.next->item;
   return 0;
diff --git a/libmailutils/list/tail.c b/libmailutils/list/tail.c
index a8d1472..f44a2cb 100644
--- a/libmailutils/list/tail.c
+++ b/libmailutils/list/tail.c
@@ -29,7 +29,7 @@ mu_list_tail (mu_list_t list, void **pitem)
     return EINVAL;
   if (pitem == NULL)
     return MU_ERR_OUT_PTR_NULL;
-  if (!list->head.prev)
+  if (list->head.prev == &list->head)
     return MU_ERR_NOENT;
   *pitem = list->head.prev->item;
   return 0;
diff --git a/libmailutils/msgset/Makefile.am b/libmailutils/msgset/Makefile.am
index 398806d..56582e4 100644
--- a/libmailutils/msgset/Makefile.am
+++ b/libmailutils/msgset/Makefile.am
@@ -19,16 +19,26 @@ noinst_LTLIBRARIES = libmsgset.la
 
 libmsgset_la_SOURCES = \
  add.c\
+ addset.c\
  aggr.c\
  clear.c\
  create.c\
+ count.c\
  getitr.c\
  getlist.c\
+ foreachnum.c\
+ foreachmsgno.c\
+ foreachuid.c\
+ foreachmsg.c\
  free.c\
+ isempty.c\
  locate.c\
+ negate.c\
  parse.c\
  print.c\
- sub.c
+ sub.c\
+ subset.c\
+ trans.c
 
 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
 
diff --git a/libmailutils/msgset/add.c b/libmailutils/msgset/add.c
index 9d97707..3623768 100644
--- a/libmailutils/msgset/add.c
+++ b/libmailutils/msgset/add.c
@@ -23,18 +23,30 @@
 #include <mailutils/sys/msgset.h>
 
 int
-mu_msgset_add_range (mu_msgset_t mset, size_t beg, size_t end)
+mu_msgset_add_range (mu_msgset_t mset, size_t beg, size_t end, int mode)
 {
   int rc;
   struct mu_msgrange *range;
   
   if (!mset || beg == 0)
     return EINVAL;
+  if (end && beg > end)
+    {
+      size_t t = end;
+      end = beg;
+      beg = t;
+    }
   range = calloc (1, sizeof (*range));
   if (!range)
     return ENOMEM;
   range->msg_beg = beg;
   range->msg_end = end;
+  rc = _mu_msgset_translate_range (mset, mode, range);
+  if (rc)
+    {
+      free (range);
+      return rc;
+    }
   rc = mu_list_append (mset->list, range);
   if (rc)
     free (range);
diff --git a/libmailutils/msgset/locate.c b/libmailutils/msgset/addset.c
similarity index 67%
copy from libmailutils/msgset/locate.c
copy to libmailutils/msgset/addset.c
index 7b94ece..e897a7e 100644
--- a/libmailutils/msgset/locate.c
+++ b/libmailutils/msgset/addset.c
@@ -22,11 +22,29 @@
 #include <mailutils/msgset.h>
 #include <mailutils/sys/msgset.h>
 
+struct add_closure
+{
+  int mode;
+  mu_msgset_t dest;
+};
+
+static int
+add_range (void *item, void *data)
+{
+  struct mu_msgrange *r = item;
+  struct add_closure *clos = data;
+  return mu_msgset_add_range (clos->dest, r->msg_beg, r->msg_end, clos->mode);
+}
+
 int
-mu_msgset_locate (mu_msgset_t msgset, size_t n,
-		  struct mu_msgrange const **prange)
+mu_msgset_add (mu_msgset_t a, mu_msgset_t b)
 {
-  if (!msgset || n == 0)
+  struct add_closure closure;
+  if (!a)
     return EINVAL;
-  return mu_list_locate (msgset->list, &n, (void**)prange);
+  if (!b)
+    return 0;
+  closure.mode = b->flags;
+  closure.dest = a;
+  return mu_list_foreach (b->list, add_range, &closure);
 }
diff --git a/libmailutils/msgset/add.c b/libmailutils/msgset/count.c
similarity index 71%
copy from libmailutils/msgset/add.c
copy to libmailutils/msgset/count.c
index 9d97707..2eefaa4 100644
--- a/libmailutils/msgset/add.c
+++ b/libmailutils/msgset/count.c
@@ -22,22 +22,27 @@
 #include <mailutils/msgset.h>
 #include <mailutils/sys/msgset.h>
 
+static int
+count_messages (void *item, void *data)
+{
+  struct mu_msgrange *r = item;
+  size_t *count = data;
+  *count += r->msg_end - r->msg_beg + 1;
+  return 0;
+}
+  
 int
-mu_msgset_add_range (mu_msgset_t mset, size_t beg, size_t end)
+mu_msgset_count (mu_msgset_t mset, size_t *pcount)
 {
   int rc;
-  struct mu_msgrange *range;
-  
-  if (!mset || beg == 0)
+  size_t count = 0;
+
+  if (!mset)
     return EINVAL;
-  range = calloc (1, sizeof (*range));
-  if (!range)
-    return ENOMEM;
-  range->msg_beg = beg;
-  range->msg_end = end;
-  rc = mu_list_append (mset->list, range);
-  if (rc)
-    free (range);
-  mset->flags &= ~_MU_MSGSET_AGGREGATED;
+  if (!pcount)
+    return MU_ERR_OUT_PTR_NULL;
+  rc = mu_list_foreach (mset->list, count_messages, &count);
+  if (rc == 0)
+    *pcount = count;
   return rc;
 }
diff --git a/libmailutils/msgset/foreachmsg.c b/libmailutils/msgset/foreachmsg.c
new file mode 100644
index 0000000..f5fa981
--- /dev/null
+++ b/libmailutils/msgset/foreachmsg.c
@@ -0,0 +1,105 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/sys/msgset.h>
+
+struct action_closure
+{
+  mu_msgset_message_action_t action;
+  void *data;
+  mu_msgset_t msgset;
+  int dir;
+};
+
+static int
+call_action (struct action_closure *clos, size_t i)
+{
+  int rc;
+  mu_message_t msg = NULL;
+  size_t n;
+
+  if (clos->msgset->flags == MU_MSGSET_UID)
+    {
+      rc = mu_mailbox_translate (clos->msgset->mbox, MU_MAILBOX_UID_TO_MSGNO,
+				 i, &n);
+      if (rc == MU_ERR_NOENT)
+	return 0;
+      else if (rc)
+	return rc;
+    }
+  else
+    n = i;
+  
+  rc = mu_mailbox_get_message (clos->msgset->mbox, n, &msg);
+  if (rc == MU_ERR_NOENT)
+    return 0;
+  else if (rc == 0)
+    rc = clos->action (i, msg, clos->data);
+  return rc;
+}
+
+static int
+procrange (void *item, void *data)
+{
+  struct mu_msgrange *mp = item;
+  struct action_closure *clos = data;
+  size_t i;
+  int rc = 0;
+  
+  if (clos->dir)
+    for (i = mp->msg_end; rc == 0 && i >= mp->msg_beg; i--)
+      rc = call_action (clos, i);
+  else
+    for (i = mp->msg_beg; i <= mp->msg_end; i++)
+      rc = call_action (clos, i);
+  return rc;
+}
+
+/* Apply ACTION to each message number from MSGSET. */
+int
+mu_msgset_foreach_dir_message (mu_msgset_t msgset, int dir,
+			       mu_msgset_message_action_t action,
+			       void *data)
+{
+  int rc;
+  struct action_closure clos;
+
+  if (!msgset->mbox)
+    return MU_ERR_NOT_OPEN;
+  rc = mu_msgset_aggregate (msgset);
+  if (rc)
+    return rc;
+  clos.action = action;
+  clos.data = data;
+  clos.msgset = msgset;
+  clos.dir = dir;
+  return mu_list_foreach_dir (msgset->list, dir, procrange, &clos);
+}
+
+int
+mu_msgset_foreach_message (mu_msgset_t msgset,
+			   mu_msgset_message_action_t action,
+			   void *data)
+{
+  return mu_msgset_foreach_dir_message (msgset, 0, action, data);
+}
diff --git a/libmailutils/msgset/free.c b/libmailutils/msgset/foreachmsgno.c
similarity index 61%
copy from libmailutils/msgset/free.c
copy to libmailutils/msgset/foreachmsgno.c
index 77a348e..200a81f 100644
--- a/libmailutils/msgset/free.c
+++ b/libmailutils/msgset/foreachmsgno.c
@@ -15,28 +15,24 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <stdlib.h>
-#include <mailutils/list.h>
 #include <mailutils/msgset.h>
-#include <mailutils/sys/msgset.h>
 
-void
-mu_msgset_free (mu_msgset_t mset)
+/* Apply ACTION to each message number from MSGSET. */
+int
+mu_msgset_foreach_dir_msgno (mu_msgset_t msgset, int dir,
+			     mu_msgset_msgno_action_t action,
+			     void *data)
 {
-  if (mset)
-    {
-      mu_list_destroy (&mset->list);
-      free (mset);
-    }
+  return mu_msgset_foreach_num (msgset,
+	       (dir ? MU_MSGSET_FOREACH_BACKWARD : MU_MSGSET_FOREACH_FORWARD)|
+	       MU_MSGSET_NUM,
+	       action, data);
 }
 
-void
-mu_msgset_destroy (mu_msgset_t *pset)
+int
+mu_msgset_foreach_msgno (mu_msgset_t msgset,
+			 mu_msgset_msgno_action_t action,
+			 void *data)
 {
-  if (pset)
-    {
-      mu_msgset_free (*pset);
-      *pset = NULL;
-    }
+  return mu_msgset_foreach_dir_msgno (msgset, 0, action, data);
 }
-  
diff --git a/libmailutils/msgset/foreachnum.c b/libmailutils/msgset/foreachnum.c
new file mode 100644
index 0000000..3bb2314
--- /dev/null
+++ b/libmailutils/msgset/foreachnum.c
@@ -0,0 +1,106 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/sys/msgset.h>
+
+struct action_closure
+{
+  mu_msgset_msgno_action_t action;
+  void *data;
+  mu_msgset_t msgset;
+  int flags;
+};
+
+static int
+call_action (struct action_closure *clos, size_t i)
+{
+  size_t n;
+  int cmd;
+  
+  if (clos->msgset->flags != (clos->flags & MU_MSGSET_MODE_MASK))
+    {
+      int rc;
+
+      switch (clos->flags & MU_MSGSET_MODE_MASK)
+	{
+	case MU_MSGSET_NUM:
+	  cmd = MU_MAILBOX_UID_TO_MSGNO;
+	  break;
+	  
+	case MU_MSGSET_UID:
+	  cmd = MU_MAILBOX_MSGNO_TO_UID;
+	  break;
+	  
+	default:
+	  return EINVAL;
+	}
+      
+      rc = mu_mailbox_translate (clos->msgset->mbox, cmd, i, &n);
+      if (rc == MU_ERR_NOENT)
+	return 0;
+      if (rc)
+	return rc;
+    }
+  else
+    n = i;
+  return clos->action (n, clos->data);
+}
+    
+
+static int
+procrange (void *item, void *data)
+{
+  struct mu_msgrange *mp = item;
+  struct action_closure *clos = data;
+  size_t i;
+  int rc = 0;
+  
+  if (clos->flags & MU_MSGSET_FOREACH_BACKWARD)
+    for (i = mp->msg_end; rc == 0 && i >= mp->msg_beg; i--)
+      rc = call_action (clos, i);
+  else
+    for (i = mp->msg_beg; rc == 0 && i <= mp->msg_end; i++)
+      rc = call_action (clos, i);
+  return rc;
+}
+
+/* Apply ACTION to each message number or UID from MSGSET. */
+int
+mu_msgset_foreach_num (mu_msgset_t msgset, int flags,
+		       mu_msgset_msgno_action_t action,
+		       void *data)
+{
+  int rc;
+  struct action_closure clos;
+  
+  rc = mu_msgset_aggregate (msgset);
+  if (rc)
+    return rc;
+  clos.action = action;
+  clos.data = data;
+  clos.flags = flags;
+  clos.msgset = msgset;
+  return mu_list_foreach_dir (msgset->list,
+			      !!(flags & MU_MSGSET_FOREACH_BACKWARD),
+			      procrange, &clos);
+}
diff --git a/libmailutils/msgset/getitr.c b/libmailutils/msgset/foreachuid.c
similarity index 61%
copy from libmailutils/msgset/getitr.c
copy to libmailutils/msgset/foreachuid.c
index 75cfb3d..7fc36ed 100644
--- a/libmailutils/msgset/getitr.c
+++ b/libmailutils/msgset/foreachuid.c
@@ -15,16 +15,23 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <mailutils/types.h>
-#include <mailutils/errno.h>
-#include <mailutils/list.h>
 #include <mailutils/msgset.h>
-#include <mailutils/sys/msgset.h>
 
+/* Apply ACTION to each message number from MSGSET. */
 int
-mu_msgset_get_iterator (mu_msgset_t msgset, mu_iterator_t *pitr)
+mu_msgset_foreach_dir_msguid (mu_msgset_t msgset, int dir,
+			      mu_msgset_msgno_action_t action,
+			      void *data)
 {
-  if (!msgset)
-    return EINVAL;
-  return mu_list_get_iterator (msgset->list, pitr);
+  return mu_msgset_foreach_num (msgset,
+	    (dir ? MU_MSGSET_FOREACH_BACKWARD : MU_MSGSET_FOREACH_FORWARD) |
+	    MU_MSGSET_UID, action, data);
+}
+
+int
+mu_msgset_foreach_msguid (mu_msgset_t msgset,
+			  mu_msgset_msgno_action_t action,
+			  void *data)
+{
+  return mu_msgset_foreach_dir_msguid (msgset, 0, action, data);
 }
diff --git a/libmailutils/msgset/getitr.c b/libmailutils/msgset/getitr.c
index 75cfb3d..9d770f4 100644
--- a/libmailutils/msgset/getitr.c
+++ b/libmailutils/msgset/getitr.c
@@ -24,7 +24,11 @@
 int
 mu_msgset_get_iterator (mu_msgset_t msgset, mu_iterator_t *pitr)
 {
+  int rc;
   if (!msgset)
     return EINVAL;
+  rc = mu_msgset_aggregate (msgset);
+  if (rc)
+    return rc;
   return mu_list_get_iterator (msgset->list, pitr);
 }
diff --git a/libmailutils/msgset/getlist.c b/libmailutils/msgset/getlist.c
index 5f7370d..9f98c11 100644
--- a/libmailutils/msgset/getlist.c
+++ b/libmailutils/msgset/getlist.c
@@ -24,10 +24,14 @@
 int
 mu_msgset_get_list (mu_msgset_t msgset, mu_list_t *plist)
 {
+  int rc;
   if (!msgset)
     return EINVAL;
   if (!plist)
     return MU_ERR_OUT_PTR_NULL;
+  rc = mu_msgset_aggregate (msgset);
+  if (rc)
+    return rc;
   *plist = msgset->list;
   return 0;
 }
diff --git a/libmailutils/msgset/clear.c b/libmailutils/msgset/isempty.c
similarity index 86%
copy from libmailutils/msgset/clear.c
copy to libmailutils/msgset/isempty.c
index f3f7ef8..c1e4052 100644
--- a/libmailutils/msgset/clear.c
+++ b/libmailutils/msgset/isempty.c
@@ -15,18 +15,14 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
+#include <stdlib.h>
 #include <mailutils/types.h>
-#include <mailutils/errno.h>
 #include <mailutils/list.h>
 #include <mailutils/msgset.h>
 #include <mailutils/sys/msgset.h>
 
 int
-mu_msgset_clear (mu_msgset_t mset)
+mu_msgset_is_empty (mu_msgset_t mset)
 {
-  if (!mset)
-    return EINVAL;
-  mu_list_clear (mset->list);
-  return 0;
+  return mset == NULL || mu_list_is_empty (mset->list);
 }
-
diff --git a/libmailutils/msgset/locate.c b/libmailutils/msgset/locate.c
index 7b94ece..0d7155e 100644
--- a/libmailutils/msgset/locate.c
+++ b/libmailutils/msgset/locate.c
@@ -26,7 +26,11 @@ int
 mu_msgset_locate (mu_msgset_t msgset, size_t n,
 		  struct mu_msgrange const **prange)
 {
+  int rc;
   if (!msgset || n == 0)
     return EINVAL;
+  rc = mu_msgset_aggregate (msgset);
+  if (rc)
+    return rc;
   return mu_list_locate (msgset->list, &n, (void**)prange);
 }
diff --git a/libmailutils/msgset/negate.c b/libmailutils/msgset/negate.c
new file mode 100644
index 0000000..75a454d
--- /dev/null
+++ b/libmailutils/msgset/negate.c
@@ -0,0 +1,97 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+struct invert_closure
+{
+  mu_msgset_t nset;
+  size_t next_num;
+};
+
+static int
+_invert_range (void *item, void *data)
+{
+  struct mu_msgrange *range = item;
+  struct invert_closure *clos = data;
+  int rc;
+  
+  if (clos->next_num < range->msg_beg - 1)
+    {
+      rc = mu_msgset_add_range (clos->nset,
+				clos->next_num, range->msg_beg - 1,
+				clos->nset->flags);
+      if (rc)
+	return rc;
+    }
+  clos->next_num = range->msg_end + 1;
+  return 0;
+}
+
+/* Negate the message set: on return PNSET consists of the messages
+   _not contained_ in the input message set. */
+int
+mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset)
+{
+  int rc;
+  struct invert_closure clos;
+  size_t total;
+  
+  if (!msgset)
+    return EINVAL;
+  if (!msgset->mbox)
+    return MU_ERR_NOT_OPEN;
+
+  rc = mu_msgset_aggregate (msgset);
+  if (rc)
+    return rc;
+  rc = mu_mailbox_messages_count (msgset->mbox, &total);
+  if (rc)
+    return rc;
+  if (msgset->flags == MU_MSGSET_UID)
+    {
+      rc = mu_mailbox_translate (msgset->mbox,
+				 MU_MAILBOX_MSGNO_TO_UID,
+				 total, &total);
+      if (rc)
+	return rc;
+    }
+  rc = mu_msgset_create (&clos.nset, msgset->mbox, msgset->flags);
+  if (rc)
+    return rc;
+  clos.next_num = 1;
+  rc = mu_list_foreach (msgset->list, _invert_range, &clos);
+  if (rc == 0)
+    {
+      if (clos.next_num < total)
+	rc = mu_msgset_add_range (clos.nset, clos.next_num, total,
+				  clos.nset->flags);
+    }
+  
+  if (rc)
+    mu_msgset_free (clos.nset);
+  else
+    *pnset = clos.nset;
+
+  return rc;
+}
diff --git a/libmailutils/msgset/parse.c b/libmailutils/msgset/parse.c
index c522478..cc064d6 100644
--- a/libmailutils/msgset/parse.c
+++ b/libmailutils/msgset/parse.c
@@ -33,6 +33,7 @@ struct parse_msgnum_env
   size_t minval;         /* Min. sequence number or UID */
   size_t maxval;         /* Max. sequence number or UID */
   mu_msgset_t msgset;    /* Message set being built. */
+  int mode;              /* Operation mode (num/uid) */
 };
 
 /* Get a single message number/UID from env->s and store it into *PN.
@@ -95,28 +96,8 @@ parse_msgrange (struct parse_msgnum_env *env)
       msgrange.msg_beg = tmp;
     }
 
-  if ((env->msgset->flags & MU_MSGSET_UID) && env->msgset->mbox)
-    {
-      int rc;
-
-      rc = mu_mailbox_translate (env->msgset->mbox,
-				 MU_MAILBOX_UID_TO_MSGNO,
-				 msgrange.msg_beg, &msgrange.msg_beg);
-      if (rc == MU_ERR_NOENT)
-	msgrange.msg_beg = env->minval;
-      else if (rc)
-	return rc;
-      
-      rc = mu_mailbox_translate (env->msgset->mbox,
-				 MU_MAILBOX_UID_TO_MSGNO,
-				 msgrange.msg_end, &msgrange.msg_end);
-      if (rc == MU_ERR_NOENT)
-	msgrange.msg_end = env->maxval;
-      else if (rc)
-	return rc;
-    }      
-
-  return mu_msgset_add_range (env->msgset, msgrange.msg_beg, msgrange.msg_end);
+  return mu_msgset_add_range (env->msgset, msgrange.msg_beg, msgrange.msg_end,
+			      env->mode);
 }
 
 /* Parse IMAP-style message set specification S.
@@ -125,13 +106,15 @@ parse_msgrange (struct parse_msgnum_env *env)
    On error, return error code and point END to the position in the input
    string where parsing has failed. */
 int
-mu_msgset_parse_imap (mu_msgset_t mset, const char *s, char **end)
+mu_msgset_parse_imap (mu_msgset_t mset, int mode, const char *s, char **end)
 {
   int rc;
   struct parse_msgnum_env env;
   
   if (!s || !mset)
     return EINVAL;
+  if (end)
+    *end = (char*) s;
   if (!*s)
     return MU_ERR_PARSE;
 
@@ -139,9 +122,8 @@ mu_msgset_parse_imap (mu_msgset_t mset, const char *s, char **end)
   env.s = s;
   env.msgset = mset;
   env.minval = 1;
+  env.mode = mode;
   
-  if (end)
-    *end = (char*) s;
   if (mset->mbox)
     {
       size_t lastmsgno;      /* Max. sequence number. */
@@ -149,7 +131,7 @@ mu_msgset_parse_imap (mu_msgset_t mset, const char *s, char **end)
       rc = mu_mailbox_messages_count (mset->mbox, &lastmsgno);
       if (rc == 0)
 	{
-	  if (mset->flags & MU_MSGSET_UID)
+	  if (mode == MU_MSGSET_UID)
 	    {
 	      rc = mu_mailbox_translate (mset->mbox, MU_MAILBOX_MSGNO_TO_UID,
 					 lastmsgno, &env.maxval);
diff --git a/libmailutils/msgset/sub.c b/libmailutils/msgset/sub.c
index 2840781..d069568 100644
--- a/libmailutils/msgset/sub.c
+++ b/libmailutils/msgset/sub.c
@@ -112,7 +112,7 @@ sub_msgno_last (mu_msgset_t mset, size_t beg)
 }
 
 int
-mu_msgset_sub_range (mu_msgset_t mset, size_t beg, size_t end)
+mu_msgset_sub_range (mu_msgset_t mset, size_t beg, size_t end, int mode)
 {
   int rc;
   mu_iterator_t itr;
@@ -122,6 +122,19 @@ mu_msgset_sub_range (mu_msgset_t mset, size_t beg, size_t end)
     return EINVAL;
   if (mu_list_is_empty (mset->list))
     return MU_ERR_NOENT;
+  if (end && beg > end)
+    {
+      size_t t = end;
+      end = beg;
+      beg = t;
+    }
+
+  rc = _mu_msgset_translate_pair (mset, mode, &beg, &end);
+  if (rc == MU_ERR_NOENT)
+    return 0;
+  else if (rc)
+    return rc;
+  
   rc = mu_msgset_aggregate (mset);
   if (rc)
     return rc;
@@ -138,8 +151,6 @@ mu_msgset_sub_range (mu_msgset_t mset, size_t beg, size_t end)
   if (beg < mr->msg_beg)
     beg = mr->msg_beg;
   
-  if (rc)
-    return rc;
   rc = mu_list_tail (mset->list, (void**) &mr);
   if (mr->msg_end != MU_MSGNO_LAST)
     {
@@ -148,7 +159,7 @@ mu_msgset_sub_range (mu_msgset_t mset, size_t beg, size_t end)
       if (end > mr->msg_end)
 	end = mr->msg_end;
     }
-  
+
   rc = mu_list_get_iterator (mset->list, &itr);
   if (rc)
     return rc;
diff --git a/libmailutils/msgset/locate.c b/libmailutils/msgset/subset.c
similarity index 67%
copy from libmailutils/msgset/locate.c
copy to libmailutils/msgset/subset.c
index 7b94ece..87b7cc0 100644
--- a/libmailutils/msgset/locate.c
+++ b/libmailutils/msgset/subset.c
@@ -22,11 +22,29 @@
 #include <mailutils/msgset.h>
 #include <mailutils/sys/msgset.h>
 
+struct sub_closure
+{
+  int mode;
+  mu_msgset_t dest;
+};
+
+static int
+sub_range (void *item, void *data)
+{
+  struct mu_msgrange *r = item;
+  struct sub_closure *clos = data;
+  return mu_msgset_sub_range (clos->dest, r->msg_beg, r->msg_end, clos->mode);
+}
+
 int
-mu_msgset_locate (mu_msgset_t msgset, size_t n,
-		  struct mu_msgrange const **prange)
+mu_msgset_sub (mu_msgset_t a, mu_msgset_t b)
 {
-  if (!msgset || n == 0)
+  struct sub_closure closure;
+  if (!a)
     return EINVAL;
-  return mu_list_locate (msgset->list, &n, (void**)prange);
+  if (!b)
+    return 0;
+  closure.mode = b->flags;
+  closure.dest = a;
+  return mu_list_foreach (b->list, sub_range, &closure);
 }
diff --git a/libmailutils/msgset/trans.c b/libmailutils/msgset/trans.c
new file mode 100644
index 0000000..df7df42
--- /dev/null
+++ b/libmailutils/msgset/trans.c
@@ -0,0 +1,99 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <stdlib.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/mailbox.h>
+#include <mailutils/sys/msgset.h>
+
+int
+_mu_msgset_translate_pair (mu_msgset_t mset, int mode,
+			   size_t *pbeg, size_t *pend)
+{
+  if (mode != mset->flags && mset->mbox)
+    {
+      int cmd, rc;
+      size_t n;
+      size_t beg = *pbeg;
+      size_t end = *pend;
+      
+      switch (mset->flags)
+	{
+	case MU_MSGSET_NUM:
+	  cmd = MU_MAILBOX_UID_TO_MSGNO;
+	  break;
+
+	case MU_MSGSET_UID:
+	  cmd = MU_MAILBOX_MSGNO_TO_UID;
+	  break;
+
+	default:
+	  return EINVAL;
+	}
+
+      rc = mu_mailbox_translate (mset->mbox, cmd, beg, &n);
+      if (rc)
+	{
+	  if (rc == MU_ERR_NOENT && cmd == MU_MAILBOX_UID_TO_MSGNO)
+	    {
+	      size_t x;
+	      
+	      if (end == MU_MSGNO_LAST)
+		{
+		  rc = mu_mailbox_uidnext (mset->mbox, &x);
+		  if (rc)
+		    return rc;
+		}
+	      else
+		x = end;
+	      for (; rc == MU_ERR_NOENT && beg < x; beg++)
+		rc = mu_mailbox_translate (mset->mbox, cmd, beg, &n);
+	    }
+	  if (rc)
+	    return rc;
+	}
+
+      *pbeg = n;
+      
+      if (beg == end)
+	*pend = n;
+      else if (end != MU_MSGNO_LAST)
+	{
+	  rc = mu_mailbox_translate (mset->mbox, cmd, end, &n);
+	  if (rc == MU_ERR_NOENT && cmd == MU_MAILBOX_UID_TO_MSGNO)
+	    {
+	      for (; rc == MU_ERR_NOENT && beg < end; end--)
+		rc = mu_mailbox_translate (mset->mbox, cmd, end, &n);
+	      
+	    }
+	  if (rc)
+	    return rc;
+	  *pend = n;
+	}
+    }
+  return 0;
+}
+
+int
+_mu_msgset_translate_range (mu_msgset_t mset, int mode, struct mu_msgrange *r)
+{
+  return _mu_msgset_translate_pair (mset, mode, &r->msg_beg, &r->msg_end);
+}
+
diff --git a/libmailutils/tests/msgset.c b/libmailutils/tests/msgset.c
index 91db029..19549ac 100644
--- a/libmailutils/tests/msgset.c
+++ b/libmailutils/tests/msgset.c
@@ -19,7 +19,7 @@
 #include <mailutils/mailutils.h>
 
 static void
-parse_msgset (char *arg, struct mu_msgrange *range)
+parse_msgrange (char *arg, struct mu_msgrange *range)
 {
   size_t msgnum;
   char *p;
@@ -52,13 +52,33 @@ parse_msgset (char *arg, struct mu_msgrange *range)
   range->msg_end = msgnum;
 }
 
+mu_msgset_t
+parse_msgset (const char *arg)
+{
+  int rc;
+  mu_msgset_t msgset;
+  char *end;
+
+  MU_ASSERT (mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM));
+  if (arg)
+    {
+      rc = mu_msgset_parse_imap (msgset, MU_MSGSET_NUM, arg, &end);
+      if (rc)
+	{
+	  mu_error ("mu_msgset_parse_imap: %s near %s",
+		    mu_strerror (rc), end);
+	  exit (1);
+	}
+    }
+  return msgset;
+}
+
 int
 main (int argc, char **argv)
 {
   int i;
   char *msgset_string = NULL;
   mu_msgset_t msgset;
-  int rc;
   
   mu_set_program_name (argv[0]);
   for (i = 1; i < argc; i++)
@@ -67,7 +87,8 @@ main (int argc, char **argv)
 
       if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
 	{
-	  mu_printf ("usage: %s [-msgset=SET] [-add X[:Y]] [-del X:[Y]]...\n",
+	  mu_printf ("usage: %s [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] "
+		     "[-addset=SET] [-delset=SET] ...\n",
 		     mu_program_name);
 	  return 0;
 	}
@@ -77,18 +98,7 @@ main (int argc, char **argv)
 	break;
     }
 
-  MU_ASSERT (mu_msgset_create (&msgset, NULL, 0));
-  if (msgset_string)
-    {
-      char *end;
-      rc = mu_msgset_parse_imap (msgset, msgset_string, &end);
-      if (rc)
-	{
-	  mu_error ("mu_msgset_parse_imap: %s near %s",
-		    mu_strerror (rc), end);
-	  return 1;
-	}
-    }
+  msgset = parse_msgset (msgset_string);
   
   for (; i < argc; i++)
     {
@@ -97,15 +107,40 @@ main (int argc, char **argv)
       
       if (strncmp (arg, "-add=", 5) == 0)
 	{
-	  parse_msgset (arg + 5, &range);
+	  parse_msgrange (arg + 5, &range);
 	  MU_ASSERT (mu_msgset_add_range (msgset, range.msg_beg,
-					  range.msg_end));
+					  range.msg_end, MU_MSGSET_NUM));
 	}
       else if (strncmp (arg, "-sub=", 5) == 0)
 	{
-	  parse_msgset (arg + 5, &range);
+	  parse_msgrange (arg + 5, &range);
 	  MU_ASSERT (mu_msgset_sub_range (msgset, range.msg_beg,
-					  range.msg_end));
+					  range.msg_end, MU_MSGSET_NUM));
+	}
+      else if (strncmp (arg, "-addset=", 8) == 0)
+	{
+	  mu_msgset_t tset = parse_msgset (arg + 8);
+	  if (!msgset)
+	    msgset = tset;
+	  else
+	    {
+	      MU_ASSERT (mu_msgset_add (msgset, tset));
+	      mu_msgset_free (tset);
+	    }
+	}
+      else if (strncmp (arg, "-subset=", 8) == 0)
+	{
+	  mu_msgset_t tset = parse_msgset (arg + 8);
+	  if (!msgset)
+	    {
+	      mu_error ("no initial message set");
+	      exit (1);
+	    }
+	  else
+	    {
+	      MU_ASSERT (mu_msgset_sub (msgset, tset));
+	      mu_msgset_free (tset);
+	    }
 	}
       else
       	{
diff --git a/libproto/imap/mbox.c b/libproto/imap/mbox.c
index 1613ba3..b4582cb 100644
--- a/libproto/imap/mbox.c
+++ b/libproto/imap/mbox.c
@@ -175,7 +175,7 @@ __imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
       if (rc)
 	return rc;
 
-      rc = mu_msgset_create (&msgset, NULL, 0);
+      rc = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
       if (rc == 0)
 	{
 	  struct save_closure clos;
@@ -183,7 +183,7 @@ __imap_msg_get_stream (struct _mu_imap_message *imsg, size_t msgno,
 	  clos.imsg = imsg;
 	  clos.save_stream = imbx->cache;
 
-	  rc = mu_msgset_add_range (msgset, msgno, msgno);
+	  rc = mu_msgset_add_range (msgset, msgno, msgno, MU_MSGSET_NUM);
 	  if (rc == 0)
 	    {
 	      _imap_mbx_clrerr (imbx);
@@ -370,10 +370,10 @@ _imap_hdr_fill (void *data, char **pbuf, size_t *plen)
   unsigned long msgno = _imap_msg_no (imsg);
   int rc;
 
-  rc = mu_msgset_create (&msgset, NULL, 0);
+  rc = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
   if (rc == 0)
     {
-      rc = mu_msgset_add_range (msgset, msgno, msgno);
+      rc = mu_msgset_add_range (msgset, msgno, msgno, MU_MSGSET_NUM);
       if (rc == 0)
 	{
 	  clos.imsg = imsg;
@@ -619,11 +619,11 @@ _imap_msg_bodystructure (mu_message_t msg, struct mu_bodystructure **pbs)
   int rc;
   mu_msgset_t msgset;
 
-  rc = mu_msgset_create (&msgset, NULL, 0);
+  rc = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
   if (rc == 0)
     {
       size_t msgno = _imap_msg_no (imsg);
-      rc = mu_msgset_add_range (msgset, msgno, msgno);
+      rc = mu_msgset_add_range (msgset, msgno, msgno, MU_MSGSET_NUM);
       if (rc == 0)
 	rc = _imap_fetch_with_callback (imap, msgset, "BODYSTRUCTURE",
 					_imap_bodystructure_callback, pbs);
@@ -949,7 +949,7 @@ _imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
   struct attr_tab *tab;
   size_t count;
 
-  rc = mu_msgset_create (&msgset, NULL, 0);
+  rc = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
   if (rc)
     return rc;
 
@@ -962,7 +962,7 @@ _imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
 	  if (imbx->msgs[i].flags & _MU_IMAP_MSG_ATTRCHG)
 	    {
 	      mu_msgset_clear (msgset);
-	      mu_msgset_add_range (msgset, i + 1, i + 1);
+	      mu_msgset_add_range (msgset, i + 1, i + 1, MU_MSGSET_NUM);
 	      if (rc)
 		break;
 	      rc = mu_imap_store_flags (imap, 0, msgset,
@@ -994,12 +994,13 @@ _imap_mbx_gensync (mu_mailbox_t mbox, int *pdel)
 		}
 	    }
 	  if (tab[i].end == tab[i].start)
-	    rc = mu_msgset_add_range (msgset, tab[i].start + 1,
-				      tab[i].start + 1);
+	    rc = mu_msgset_add_range (msgset,
+				      tab[i].start + 1, tab[i].start + 1,
+				      MU_MSGSET_NUM);
 	  else
 	    rc = mu_msgset_add_range (msgset,
-				      tab[i].start + 1,
-				      tab[i].end + 1);
+				      tab[i].start + 1, tab[i].end + 1,
+				      MU_MSGSET_NUM);
 	  if (rc)
 	    break;
 	}
@@ -1192,10 +1193,10 @@ _imap_mbx_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
   
   mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_TRACE1,
 	    (_("scanning mailbox %s"), mu_url_to_string (mbox->url)));
-  rc = mu_msgset_create (&msgset, NULL, 0);
+  rc = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
   if (rc)
     return rc;
-  rc = mu_msgset_add_range (msgset, msgno, MU_MSGNO_LAST);
+  rc = mu_msgset_add_range (msgset, msgno, MU_MSGNO_LAST, MU_MSGSET_NUM);
   if (rc)
     {
       mu_msgset_free (msgset);
diff --git a/mh/anno.c b/mh/anno.c
index 1b113fb..0851f75 100644
--- a/mh/anno.c
+++ b/mh/anno.c
@@ -95,10 +95,11 @@ opt_handler (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
-void
-anno (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+int
+anno (size_t n, mu_message_t msg, void *call_data)
 {
   mh_annotate (msg, component, anno_text, anno_date);
+  return 0;
 }
 
 int
@@ -107,7 +108,7 @@ main (int argc, char **argv)
   int rc;
   int index;
   mu_mailbox_t mbox;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   size_t len;
 
   MU_APP_INIT_NLS ();
@@ -154,10 +155,16 @@ main (int argc, char **argv)
   argc -= index;
   argv += index;
   
-  mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
-  rc = mh_iterate (mbox, &msgset, anno, NULL);
-
-  mh_msgset_current (mbox, &msgset, 0);
+  mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
+  rc = mu_msgset_foreach_message (msgset, anno, NULL);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_foreach_message", NULL, rc);
+      exit (1);
+    }
+      
+  mh_msgset_first_current (mbox, msgset);
+  mu_msgset_free (msgset);
   mh_global_save_state ();
   mu_mailbox_sync (mbox);
   mu_mailbox_close (mbox);
diff --git a/mh/burst.c b/mh/burst.c
index d731bfa..98851b1 100644
--- a/mh/burst.c
+++ b/mh/burst.c
@@ -578,8 +578,8 @@ burst_or_copy (mu_message_t msg, int recursive, int copy)
   return 1;
 }
 
-void
-burst (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+int
+burst (size_t num, mu_message_t msg, void *data)
 {
   memset (&map, 0, sizeof (map));
   mh_message_number (msg, &map.msgno);
@@ -599,36 +599,38 @@ burst (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
     }
   else if (!quiet)
     mu_error (_("message %s not in digest format"), mu_umaxtostr (0, num));
+  return 0;
 }
 
 
 /* Inplace handling */
+struct rename_env
+{
+  size_t lastuid;
+  size_t idx;
+};
 
-void
-burst_rename (mh_msgset_t *ms, size_t lastuid)
+  
+static int
+_rename (size_t msgno, void *data)
 {
-  size_t i, j;
+  struct rename_env *rp = data;
+  
+  if (msgno == burst_map[rp->idx].msgno)
+    {
+      rp->lastuid -= burst_map[rp->idx].count;
+      burst_map[rp->idx].msgno = rp->lastuid;
+      rp->idx--;
+    }
 
-  VERBOSE ((_("Renaming messages")));
-  j = burst_count - 1;
-  for (i = ms->count; i > 0; i--)
+  if (msgno != rp->lastuid)
     {
       const char *from;
       const char *to;
 
-      if (ms->list[i-1] == burst_map[j].msgno)
-	{
-	  lastuid -= burst_map[j].count;
-	  burst_map[j].msgno = lastuid;
-	  j--;
-	}
-
-      if (ms->list[i-1] == lastuid)
-	continue;
-      
-      from = mu_umaxtostr (0, ms->list[i-1]);
-      to   = mu_umaxtostr (1, lastuid);
-      --lastuid;
+      from = mu_umaxtostr (0, msgno);
+      to   = mu_umaxtostr (1, rp->lastuid);
+      --rp->lastuid;
 
       VERBOSE((_("message %s becomes message %s"), from, to));
 	       
@@ -639,6 +641,18 @@ burst_rename (mh_msgset_t *ms, size_t lastuid)
 	  exit (1);
 	}
     }
+  return 0;
+}
+
+void
+burst_rename (mu_msgset_t ms, size_t lastuid)
+{
+  struct rename_env renv;
+
+  VERBOSE ((_("Renaming messages")));
+  renv.lastuid = lastuid;
+  renv.idx = burst_count - 1;
+  mu_msgset_foreach_dir_msguid (ms, 1, _rename, &renv);
 }  
 
 void
@@ -705,7 +719,7 @@ main (int argc, char **argv)
 {
   int index, rc;
   mu_mailbox_t mbox;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   const char *tempfolder = mh_global_profile_get ("Temp-Folder", ".temp");
   
   /* Native Language Support */
@@ -720,7 +734,7 @@ main (int argc, char **argv)
 
   VERBOSE ((_("Opening folder `%s'"), mh_current_folder ()));
   mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
-  mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
+  mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
 
   if (inplace)
     {
@@ -744,7 +758,7 @@ main (int argc, char **argv)
   else
     tmpbox = mbox;
 
-  rc = mh_iterate (mbox, &msgset, burst, NULL);
+  rc = mu_msgset_foreach_message (msgset, burst, NULL);
   if (rc)
     return rc;
 
@@ -752,8 +766,8 @@ main (int argc, char **argv)
     {
       mu_url_t dst_url = NULL;
       size_t i, next_uid, last_uid;
-      mh_msgset_t ms;
-      char *xargv[2];
+      mu_msgset_t ms;
+      size_t count;
       const char *dir;
       
       burst_map = obstack_finish (&stk);
@@ -763,11 +777,14 @@ main (int argc, char **argv)
 	last_uid += burst_map[i].count;
       VERBOSE ((_("Estimated last UID: %s"), mu_umaxtostr (0, last_uid)));
 
-      mu_asprintf (&xargv[0], "%s-last", mu_umaxtostr (0, burst_map[0].msgno));
-      xargv[1] = NULL;
-      mh_msgset_parse (mbox, &ms, 1, xargv, NULL);
-      free (xargv[0]);
-      mh_msgset_uids (mbox, &ms);
+      rc = mu_msgset_create (&ms, mbox, MU_MSGSET_NUM);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, rc);
+	  exit (1);
+	}
+      mu_mailbox_messages_count (mbox, &count);
+      mu_msgset_add_range (ms, burst_map[0].msgno, count, MU_MSGSET_NUM);
 	
       mu_mailbox_get_url (mbox, &dst_url);
       mu_url_sget_path (dst_url, &dir);
@@ -779,8 +796,8 @@ main (int argc, char **argv)
 	}
       mu_mailbox_close (mbox);
 
-      burst_rename (&ms, last_uid);
-      mh_msgset_free (&ms);
+      burst_rename (ms, last_uid);
+      mu_msgset_free (ms);
 
       finalize_inplace (last_uid);
 
diff --git a/mh/comp.c b/mh/comp.c
index 336b348..8adc2cd 100644
--- a/mh/comp.c
+++ b/mh/comp.c
@@ -246,22 +246,21 @@ main (int argc, char **argv)
 	     `-draftfolder +folder'  treat this arguments  as `msg'. */
 	  if (use_draft || index < argc)
 	    {
-	      mh_msgset_t msgset;
+	      mu_msgset_t msgset;
 	      mu_mailbox_t mbox;
 	      
 	      mbox = mh_open_folder (draftfolder, 
                                      MU_STREAM_RDWR|MU_STREAM_CREAT);
-	      mh_msgset_parse (mbox, &msgset, 
+	      mh_msgset_parse (&msgset, mbox, 
 	                       argc - index, argv + index,
 			       use_draft ? "cur" : "new");
-              mh_msgset_uids (mbox, &msgset);	
-	      if (msgset.count != 1)
+	      if (!mh_msgset_single_message (msgset))
 		{
 		  mu_error (_("only one message at a time!"));
 		  return 1;
 		}
-	      draftmessage = mu_umaxtostr (0, msgset.list[0]);
-	      mh_msgset_free (&msgset);
+	      draftmessage = mu_umaxtostr (0, mh_msgset_first_uid (msgset));
+	      mu_msgset_free (msgset);
 	      mu_mailbox_destroy (&mbox);
 	    }
 	  if (mh_draft_message (draftfolder, draftmessage,
@@ -273,20 +272,20 @@ main (int argc, char **argv)
 
   if (folder_set && index < argc)
     {
-      mh_msgset_t msgset;
+      mu_msgset_t msgset;
       mu_mailbox_t mbox;
       
       mbox = mh_open_folder (mh_current_folder (), MU_STREAM_READ);
-      mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
-      if (msgset.count != 1)
+      mh_msgset_parse (&msgset, mbox, argc - index, argv + index, "cur");
+      if (!mh_msgset_single_message (msgset))
 	{
 	  mu_error (_("only one message at a time!"));
 	  return 1;
 	}
       unlink (wh_env.file);
-      copy_message (mbox, msgset.list[0], wh_env.file);
+      copy_message (mbox, mh_msgset_first (msgset), wh_env.file);
       mu_mailbox_destroy (&mbox);
-      mh_msgset_free (&msgset);
+      mu_msgset_free (msgset);
     }
   else
     {
diff --git a/mh/folder.c b/mh/folder.c
index 756015f..bc952c1 100644
--- a/mh/folder.c
+++ b/mh/folder.c
@@ -773,9 +773,10 @@ pack_xlate (struct pack_tab *pack_tab, size_t count, size_t n)
 static int
 _fixup (const char *name, const char *value, struct fixup_data *fd, int flags)
 {
-  size_t i, j;
+  size_t i;
+  int rc;
   struct mu_wordsplit ws;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
 
   if (verbose)
     fprintf (stderr, "Sequence `%s'...\n", name);
@@ -787,20 +788,32 @@ _fixup (const char *name, const char *value, struct fixup_data *fd, int flags)
       return 0;
     }
 
-  msgset.list = xcalloc (ws.ws_wordc, sizeof msgset.list[0]);
-  for (i = j = 0; i < ws.ws_wordc; i++)
+  rc = mu_msgset_create (&msgset, NULL, MU_MSGSET_UID);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, rc);
+      exit (1);
+    }
+  
+  for (i = 0; i < ws.ws_wordc; i++)
     {
       size_t n = pack_xlate (fd->pack_tab, fd->count,
 			     strtoul (ws.ws_wordv[i], NULL, 0));
       if (n)
-	msgset.list[j++] = n;
+	{
+	  rc = mu_msgset_add_range (msgset, n, n, MU_MSGSET_UID);
+	  if (rc)
+	    {
+	      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_add_range", NULL, rc);
+	      exit (1);
+	    }
+	}
     }
-  msgset.count = j;
 
   mu_wordsplit_free (&ws);
   
-  mh_seq_add (fd->mbox, name, &msgset, flags | SEQ_ZERO);
-  free (msgset.list);
+  mh_seq_add (fd->mbox, name, msgset, flags | SEQ_ZERO);
+  mu_msgset_free (msgset);
 
   if (verbose)
     {
@@ -944,7 +957,7 @@ int
 main (int argc, char **argv)
 {
   int index = 0;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
 
   /* Native Language Support */
   MU_APP_INIT_NLS ();
@@ -971,9 +984,11 @@ main (int argc, char **argv)
     
   if (argc - index == 1)
     {
-      mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
-      mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
-      mh_msgset_current (mbox, &msgset, 0);
+      mu_mailbox_t mbox = mh_open_folder (mh_current_folder (),
+					  MU_STREAM_RDWR);
+      mh_msgset_parse (&msgset, mbox, argc - index, argv + index, "cur");
+      mh_msgset_first_current (mbox, msgset);
+      mu_msgset_free (msgset);
       mh_global_save_state ();
       mu_mailbox_close (mbox);
       mu_mailbox_destroy (&mbox);
diff --git a/mh/forw.c b/mh/forw.c
index 4f3726b..5824e8a 100644
--- a/mh/forw.c
+++ b/mh/forw.c
@@ -116,7 +116,7 @@ static char *draftmessage = "new";
 static const char *draftfolder = NULL;
 static char *input_file;        /* input file name (--file option) */
 
-static mh_msgset_t msgset;
+static mu_msgset_t msgset;
 static mu_mailbox_t mbox;
 
 static int
@@ -331,15 +331,27 @@ format_message (mu_stream_t outstr, mu_message_t msg, int num,
     }
 }
 
-void
-format_message_itr (mu_mailbox_t mbox MU_ARG_UNUSED,
-		    mu_message_t msg, size_t num, void *data)
+int
+format_message_itr (size_t num, mu_message_t msg, void *data)
 {
   struct format_data *fp = data;
 
   format_message (fp->stream, msg, fp->num, fp->format);
   if (fp->num)
     fp->num++;
+  return 0;
+}
+
+static int
+_proc_forwards (size_t n, mu_message_t msg, void *call_data)
+{
+  mu_stream_t stream = call_data;
+  size_t num;
+	      
+  if (annotate)
+    mu_list_append (wh_env.anno_list, msg);
+  mh_message_number (msg, &num);
+  return mu_stream_printf (stream, " %lu", (unsigned long) num);
 }
 
 void
@@ -411,36 +423,22 @@ finish_draft ()
 	{
 	  mu_url_t url;
 	  const char *mbox_path;
-	  const char *p;
-	  size_t i;
       
 	  mu_mailbox_get_url (mbox, &url);
 	  mu_url_sget_path (url, &mbox_path);
 	  mu_asprintf (&str, "#forw [] +%s", mbox_path);
 	  rc = mu_stream_write (stream, str, strlen (str), NULL);
 	  free (str);
-	  for (i = 0; rc == 0 && i < msgset.count; i++)
-	    {
-	      mu_message_t msg;
-	      size_t num;
-	      
-	      mu_mailbox_get_message (mbox, msgset.list[i], &msg);
-	      if (annotate)
-		mu_list_append (wh_env.anno_list, msg);
-	      mh_message_number (msg, &num);
-	      p = mu_umaxtostr (0, num);
-	      rc = mu_stream_write (stream, " ", 1, NULL);
-	      if (rc)
-		break;
-	      rc = mu_stream_write (stream, p, strlen (p), NULL);
-	    }
+	  mu_msgset_foreach_message (msgset, _proc_forwards, stream);
 	}
       else
 	{
+	  int single_message = mh_msgset_single_message (msgset);
+	  
 	  str = "\n------- ";
 	  rc = mu_stream_write (stream, str, strlen (str), NULL);
 	  
-	  if (msgset.count == 1)
+	  if (single_message)
 	    {
 	      fd.num = 0;
 	      str = (char*) _("Forwarded message\n");
@@ -454,12 +452,12 @@ finish_draft ()
 	  rc = mu_stream_write (stream, str, strlen (str), NULL);
 	  fd.stream = stream;
 	  fd.format = format;
-	  rc = mh_iterate (mbox, &msgset, format_message_itr, &fd);
+	  rc = mu_msgset_foreach_message (msgset, format_message_itr, &fd);
       
 	  str = "\n------- ";
 	  rc = mu_stream_write (stream, str, strlen (str), NULL);
 	  
-	  if (msgset.count == 1)
+	  if (single_message)
 	    str = (char*) _("End of Forwarded message");
 	  else
 	    str = (char*) _("End of Forwarded messages");
@@ -504,7 +502,7 @@ main (int argc, char **argv)
   else
     {
       mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
-      mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
+      mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
     }
   
   if (build_only || !draftfolder)
diff --git a/mh/mark.c b/mh/mark.c
index da9297e..b4224db 100644
--- a/mh/mark.c
+++ b/mh/mark.c
@@ -121,7 +121,7 @@ opt_handler (int key, char *arg, struct argp_state *state)
 struct mark_closure
 {
   mu_mailbox_t mbox;
-  mh_msgset_t *msgset;
+  mu_msgset_t msgset;
 };
 
 static int
@@ -191,7 +191,7 @@ int
 main (int argc, char **argv)
 {
   int index;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   mu_mailbox_t mbox;
   mu_url_t url;
   struct mark_closure clos;
@@ -209,11 +209,11 @@ main (int argc, char **argv)
 	
   argc -= index;
   argv += index;
-  mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
-  mh_msgset_uids (mbox, &msgset);
+  mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
   
   clos.mbox = mbox;
-  clos.msgset = &msgset;
+  clos.msgset = msgset;
+  //FIXME: msgset operates on UIDs but there's no way to inform it about that.
   switch (action)
     {
     case ARG_ADD:
diff --git a/mh/mh.h b/mh/mh.h
index 2e0f812..bca11a2 100644
--- a/mh/mh.h
+++ b/mh/mh.h
@@ -60,6 +60,7 @@
 #include <mailutils/mh.h>
 #include <mailutils/stdstream.h>
 #include <mailutils/datetime.h>
+#include <mailutils/msgset.h>
 
 #include <mu_umaxtostr.h>
 
@@ -194,19 +195,6 @@ typedef struct
   mu_header_t header;
 } mh_context_t;
 
-#define MH_MSGSET_UID   0x01
-
-typedef struct
-{
-  int flags;
-  size_t *list;
-  size_t count;
-  size_t size;
-} mh_msgset_t;
-
-typedef void (*mh_iterator_fp) (mu_mailbox_t mbox, mu_message_t msg,
-			        size_t num, void *data);
-
 /* Recipient masks */
 #define RCPT_NONE 0
 #define RCPT_TO   0x0001
@@ -303,14 +291,14 @@ int mh_message_number (mu_message_t msg, size_t *pnum);
 
 mu_mailbox_t mh_open_folder (const char *folder, int flags);
 
-void mh_msgset_parse (mu_mailbox_t mbox, mh_msgset_t *msgset,
+void mh_msgset_parse (mu_msgset_t *msgset, mu_mailbox_t mbox, 
 		      int argc, char **argv, char *def);
-int mh_msgset_member (mh_msgset_t *msgset, size_t num);
-void mh_msgset_reverse (mh_msgset_t *msgset);
-void mh_msgset_negate (mu_mailbox_t mbox, mh_msgset_t *msgset);
-void mh_msgset_current (mu_mailbox_t mbox, mh_msgset_t *msgset, int index);
-void mh_msgset_free (mh_msgset_t *msgset);
-void mh_msgset_uids (mu_mailbox_t mbox, mh_msgset_t *msgset);
+void mh_msgset_parse_string (mu_msgset_t *msgset, mu_mailbox_t mbox, 
+			     const char *string, char *def);
+void mh_msgset_first_current (mu_mailbox_t mbox, mu_msgset_t msgset);
+size_t mh_msgset_first (mu_msgset_t msgset);
+size_t mh_msgset_first_uid (mu_msgset_t msgset);
+int mh_msgset_single_message (mu_msgset_t msgset);
 
 char *mh_get_dir (void);
 int mh_find_file (const char *name, char **resolved_name);
@@ -323,9 +311,6 @@ void mh_expand_aliases (mu_message_t msg, mu_address_t *addr_to,
 int mh_is_my_name (const char *name);
 char * mh_my_email (void);
 
-int mh_iterate (mu_mailbox_t mbox, mh_msgset_t *msgset,
-	        mh_iterator_fp itr, void *data);
-
 size_t mh_get_message (mu_mailbox_t mbox, size_t seqno, mu_message_t *mesg);
 
 int mh_decode_rcpt_flag (const char *arg);
@@ -372,9 +357,9 @@ int mhl_format_run (mu_list_t fmt, int width, int length, int flags,
 		    mu_message_t msg, mu_stream_t output);
 void mhl_format_destroy (mu_list_t *fmt);
 
-void mh_seq_add (mu_mailbox_t mbox, const char *name, mh_msgset_t *mset,
+void mh_seq_add (mu_mailbox_t mbox, const char *name, mu_msgset_t mset,
 		 int flags);
-int mh_seq_delete (mu_mailbox_t mbox, const char *name, mh_msgset_t *mset,
+int mh_seq_delete (mu_mailbox_t mbox, const char *name, mu_msgset_t mset,
 		   int flags);
 const char *mh_seq_read (mu_mailbox_t mbox, const char *name, int flags);
 
diff --git a/mh/mh_init.c b/mh/mh_init.c
index fc1934a..7fef8f9 100644
--- a/mh/mh_init.c
+++ b/mh/mh_init.c
@@ -577,32 +577,6 @@ mh_find_file (const char *name, char **resolved_name)
 }
 
 int
-mh_iterate (mu_mailbox_t mbox, mh_msgset_t *msgset,
-	    mh_iterator_fp itr, void *data)
-{
-  int rc;
-  size_t i;
-
-  for (i = 0; i < msgset->count; i++)
-    {
-      mu_message_t msg;
-      size_t num;
-
-      num = msgset->list[i];
-      if ((rc = mu_mailbox_get_message (mbox, num, &msg)) != 0)
-	{
-	  mu_error (_("cannot get message %lu: %s"),
-		    (unsigned long) num, mu_strerror (rc));
-	  return 1;
-	}
-
-      itr (mbox, msg, num, data);
-    }
-
-  return 0;
-}
-
-int
 mh_spawnp (const char *prog, const char *file)
 {
   struct mu_wordsplit ws;
@@ -1039,19 +1013,16 @@ mh_draft_message (const char *name, const char *msgspec, char **pname)
   else
     {
       char *argv[2];
-      mh_msgset_t msgset;
+      mu_msgset_t msgset;
       
       argv[0] = (char*) msgspec;
       argv[1] = NULL;
-      mh_msgset_parse (mbox, &msgset, 1, argv, "cur");
-      if (msgset.count > 1)
+      mh_msgset_parse (&msgset, mbox, 1, argv, "cur");
+      if (!mh_msgset_single_message (msgset))
 	mu_error (_("only one message at a time!"));
       else
-	{
-	  mh_msgset_uids (mbox, &msgset);
-	  uid = msgset.list[0];
-	}
-      mh_msgset_free (&msgset);
+	uid = mh_msgset_first_uid (msgset);
+      mu_msgset_free (msgset);
     }
 
   mu_url_sget_path (url, &path);
diff --git a/mh/mh_msgset.c b/mh/mh_msgset.c
index 49ed35a..5392d5f 100644
--- a/mh/mh_msgset.c
+++ b/mh/mh_msgset.c
@@ -18,169 +18,91 @@
 /* MH message sets. */
 
 #include <mh.h>
+#include <mailutils/sys/msgset.h>
 
-void
-mh_msgset_init (mh_msgset_t *msgset)
-{
-  memset (msgset, 0, sizeof (*msgset));
-}
-
-void
-mh_msgset_expand (mh_msgset_t *msgset, size_t count)
+size_t
+mh_msgset_first (mu_msgset_t msgset)
 {
-  size_t rest = msgset->size - msgset->count;
-
-  if (rest < count)
+  mu_list_t list;
+  struct mu_msgrange *r;
+  int rc;
+  
+  rc = mu_msgset_get_list (msgset, &list);
+  if (rc)
     {
-      msgset->size += count;
-      msgset->list = xrealloc (msgset->list,
-			       msgset->size * sizeof (msgset->list[0]));
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_get_list", NULL, rc);
+      exit (1);
     }
-}
-
-void
-mh_msgset_add (mh_msgset_t *msgset, size_t n)
-{
-  mh_msgset_expand (msgset, 1);
-  msgset->list[msgset->count++] = n;
-}
-
-static int
-comp_mesg (const void *a, const void *b)
-{
-  size_t an = *(size_t*)a;
-  size_t bn = *(size_t*)b;
-  if (an > bn)
-    return 1;
-  else if (an < bn)
-    return -1;
-  return 0;
-}
-
-void
-mh_msgset_optimize (mh_msgset_t *msgset)
-{
-  size_t i, msgno;
-  size_t msgcnt = msgset->count;
-  size_t *msglist = msgset->list;
-      
-  /* Sort the resulting message set */
-  qsort (msglist, msgcnt, sizeof (*msgset->list), comp_mesg);
-
-  /* Remove duplicates. */
-  for (i = 0, msgno = 1; i < msgset->count; i++)
-    if (msglist[msgno-1] != msglist[i])
-      msglist[msgno++] = msglist[i];
-  msgset->count = msgno;
-}
-
-/* Check if message with ordinal number `num' is contained in the
-   message set. */
-int
-mh_msgset_member (mh_msgset_t *msgset, size_t num)
-{
-  size_t i;
-
-  for (i = 0; i < msgset->count; i++)
-    if (msgset->list[i] == num)
-      return i + 1;
-  return 0;
-}
-
-/* Reverse the order of messages in the message set */
-void
-mh_msgset_reverse (mh_msgset_t *msgset)
-{
-  int head, tail;
-
-  for (head = 0, tail = msgset->count-1; head < tail; head++, tail--)
+  rc = mu_list_head (list, (void**)&r);
+  if (rc)
     {
-      size_t val = msgset->list[head];
-      msgset->list[head] = msgset->list[tail];
-      msgset->list[tail] = val;
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_get", NULL, rc);
+      exit (1);
     }
+  return r->msg_beg;
 }
 
-/* Set the current message to that contained at position `index'
-   in the given message set */
-void
-mh_msgset_current (mu_mailbox_t mbox, mh_msgset_t *msgset, int index)
+size_t
+mh_msgset_first_uid (mu_msgset_t msgset)
 {
-  mu_message_t msg = NULL;
   int rc;
   size_t cur;
-  
-  rc = mu_mailbox_get_message (mbox, msgset->list[index], &msg);
+
+  cur = mh_msgset_first (msgset);
+  rc = mu_mailbox_translate (msgset->mbox, MU_MAILBOX_MSGNO_TO_UID, cur, &cur);
   if (rc)
     {
-      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL, rc);
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_translate", NULL, rc);
       exit (1);
     }
-  mh_message_number (msg, &cur);
-  mh_mailbox_set_cur (mbox, cur);
+  return cur;
 }
 
-/* Free memory allocated for the message set. Note, that the msgset
-   itself is supposed to reside in the statically allocated memory and
-   therefore is not freed */
+/* Set the current message to that contained at position 0
+   in the given message set.
+   FIXME: mbox is superfluous
+*/
 void
-mh_msgset_free (mh_msgset_t *msgset)
+mh_msgset_first_current (mu_mailbox_t mbox, mu_msgset_t msgset)
 {
-  if (msgset->count)
-    free (msgset->list);
+  mh_mailbox_set_cur (mbox, mh_msgset_first_uid (msgset));
 }
 
-/* Negate the message set: on return `msgset' consists of the messages
-   _not contained_ in the input message set. Any memory associated with
-   the input message set is freed */
-void
-mh_msgset_negate (mu_mailbox_t mbox, mh_msgset_t *msgset)
+int
+mh_msgset_single_message (mu_msgset_t msgset)
 {
-  size_t i, total = 0, msgno;
-  size_t *list;
-
-  mu_mailbox_messages_count (mbox, &total);
-  list = calloc (total, sizeof (list[0]));
-  if (!list)
-    mh_err_memory (1);
-  for (i = 1, msgno = 0; i <= total; i++)
+  int rc;
+  mu_list_t list;
+  struct mu_msgrange *r;
+  size_t count;
+  
+  rc = mu_msgset_get_list (msgset, &list);
+  if (rc)
     {
-      if (!mh_msgset_member (msgset, i))
-	list[msgno++] = i;
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_get_list", NULL, rc);
+      exit (1);
     }
-
-  list = realloc (list, sizeof (list[0]) * msgno);
-  if (!list)
+  rc = mu_list_count (list, &count);
+  if (rc)
     {
-      mu_error (_("not enough memory"));
-      abort ();
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_count", NULL, rc);
+      exit (1);
     }
-  mh_msgset_free (msgset);
-  msgset->count = msgno;
-  msgset->list = list;
-}
-
-void
-mh_msgset_uids (mu_mailbox_t mbox, mh_msgset_t *msgset)
-{
-  size_t i;
-
-  if (msgset->flags & MH_MSGSET_UID)
-    return;
-  for (i = 0; i < msgset->count; i++)
+  if (count != 1)
+    return 0;
+  rc = mu_list_head (list, (void**)&r);
+  if (rc)
     {
-      mu_message_t msg;
-      mu_mailbox_get_message (mbox, msgset->list[i], &msg);
-      mh_message_number (msg, &msgset->list[i]);
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_get", NULL, rc);
+      exit (1);
     }
-  msgset->flags |= MH_MSGSET_UID;
+  return r->msg_beg == r->msg_end;
 }
 
 
 struct msgset_parser
 {
-  mu_mailbox_t mbox;
-  mh_msgset_t *msgset;
+  mu_msgset_t msgset;
   char *curp;
   int argc;
   char **argv;
@@ -192,10 +114,16 @@ struct msgset_parser
 
 static void
 msgset_parser_init (struct msgset_parser *parser, mu_mailbox_t mbox,
-		    mh_msgset_t *msgset, int argc, char **argv)
+		    int argc, char **argv)
 {
-  parser->mbox = mbox;
-  parser->msgset = msgset;
+  int rc;
+  
+  rc = mu_msgset_create (&parser->msgset, mbox, MU_MSGSET_NUM);//FIXME: flags?
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, rc);
+      exit (1);
+    }
   parser->argc = argc;
   parser->argv = argv;
   parser->curp = "";
@@ -238,7 +166,7 @@ _expand_sequence (struct msgset_parser *parser, char *term)
   const char *listp;
   int negate = 0;
 
-  listp = mh_global_sequences_get (parser->mbox, term, NULL);
+  listp = mh_global_sequences_get (parser->msgset->mbox, term, NULL);
   if (!listp)
     {
       int len;
@@ -249,7 +177,7 @@ _expand_sequence (struct msgset_parser *parser, char *term)
       if (strncmp (term, neg, len))
 	return 1;
       negate = 1;
-      listp = mh_global_sequences_get (parser->mbox, term + len, NULL);
+      listp = mh_global_sequences_get (parser->msgset->mbox, term + len, NULL);
       if (!listp)
 	return 1;
     }
@@ -262,16 +190,35 @@ _expand_sequence (struct msgset_parser *parser, char *term)
     }
   else
     {
+      int rc;
       struct msgset_parser clone;
-      
-      msgset_parser_init (&clone, parser->mbox,  parser->msgset,
+
+      msgset_parser_init (&clone, parser->msgset->mbox,
 			  ws.ws_wordc, ws.ws_wordv);
       msgset_parser_run (&clone);
       mu_wordsplit_free (&ws);
+      if (negate)
+	{
+	  mu_msgset_t negset;
+	  
+	  rc = mu_msgset_negate (clone.msgset, &negset);
+	  if (rc)
+	    {
+	      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_negate", NULL, rc);
+	      exit (1);
+	    }
+	  mu_msgset_free (clone.msgset);
+	  clone.msgset = negset;
+	}
+      rc = mu_msgset_add (parser->msgset, clone.msgset);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_add", NULL, rc);
+	  exit (1);
+	}
+      mu_msgset_free (clone.msgset);
     }
   
-  if (negate)
-    mh_msgset_negate (parser->mbox, parser->msgset);
   return 0;
 }
 
@@ -310,7 +257,7 @@ static int
 msgset_last (mu_mailbox_t mbox, size_t *pnum)
 {
   int rc;
-
+  
   rc = mu_mailbox_messages_count (mbox, pnum);
   if (rc)
     {
@@ -406,8 +353,11 @@ parse_term (struct msgset_parser *parser, int seq)
       for (p = keywords; p->name; p++)
 	if (tlen == p->len && memcmp (p->name, term, tlen) == 0)
 	  {
-	    if (p->handler (parser->mbox, &parser->number))
+	    size_t num;
+	    
+	    if (p->handler (parser->msgset->mbox, &num))
 	      msgset_abort (term);
+	    parser->number = num;
 	    parser->sign = p->sign;
 	    parser->validuid = 1;
 	    return PARSE_MORE;
@@ -428,7 +378,8 @@ parse_term (struct msgset_parser *parser, int seq)
       if (endp != parser->curp)
 	msgset_abort (term);
       
-      if (mu_mailbox_translate (parser->mbox, MU_MAILBOX_UID_TO_MSGNO,
+      if (mu_mailbox_translate (parser->msgset->mbox,
+				MU_MAILBOX_UID_TO_MSGNO,
 				num, &parser->number))
 	{
 	  parser->validuid = 0;
@@ -447,48 +398,37 @@ static void
 add_messages (struct msgset_parser *parser, size_t start, size_t count,
 	      int sign)
 {
-  size_t i;
+  int rc;
 
   if (start == 0)
     start = 1;
-  mh_msgset_expand (parser->msgset, count);
   if (sign)
     {
       if (count > start)
 	count = start;
-      for (i = 0; i < count; i++, start--)
-	parser->msgset->list[parser->msgset->count++] = start;
+      rc = mu_msgset_add_range (parser->msgset, start, start - count + 1,
+				MU_MSGSET_NUM);
     }
   else
     {
       size_t total;
   
-      mu_mailbox_messages_count (parser->mbox, &total);
+      mu_mailbox_messages_count (parser->msgset->mbox, &total);
       if (start + count > total)
-	count = total - start + 1;
-      for (i = 0; i < count; i++, start++)
-	parser->msgset->list[parser->msgset->count++] = start;
+	{
+	  count = total - start + 1;
+	  if (count == 0)
+	    emptyrange_abort (parser->argv[-1]);
+	}
+      rc = mu_msgset_add_range (parser->msgset, start, start + count - 1,
+				MU_MSGSET_NUM);
     }
-  if (count == 0)
-    emptyrange_abort (parser->argv[-1]);
-}
 
-static void
-add_message_range (struct msgset_parser *parser, size_t start, size_t end)
-{
-  if (end == start)
-    emptyrange_abort (parser->argv[-1]);
-
-  if (end < start)
+  if (rc)
     {
-      size_t t = start;
-      start = end;
-      end = t;
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_add_range", NULL, rc);
+      exit (1);
     }
-  mh_msgset_expand (parser->msgset, end - start + 1);
-
-  for (; start <= end; start++)
-    parser->msgset->list[parser->msgset->count++] = start;
 }
 
 /* range: term '-' term
@@ -527,8 +467,9 @@ parse_range (struct msgset_parser *parser)
       if (!validuid)
 	{
 	  size_t total, lastuid;
-	  msgset_last (parser->mbox, &total);
-	  mu_mailbox_translate (parser->mbox, MU_MAILBOX_MSGNO_TO_UID,
+	  msgset_last (parser->msgset->mbox, &total);
+	  mu_mailbox_translate (parser->msgset->mbox,
+				MU_MAILBOX_MSGNO_TO_UID,
 				total, &lastuid);
 	  if (start > lastuid)
 	    {
@@ -550,6 +491,7 @@ parse_range (struct msgset_parser *parser)
     {
       size_t lastuid = 0;
       int validuid = parser->validuid;
+      int rc;
       
       parser->curp++;
       if (parse_term (parser, 0) == PARSE_EOF)
@@ -558,8 +500,8 @@ parse_range (struct msgset_parser *parser)
 	{
 	  size_t total;
 
-	  msgset_last (parser->mbox, &total);
-	  mu_mailbox_translate (parser->mbox, MU_MAILBOX_MSGNO_TO_UID,
+	  msgset_last (parser->msgset->mbox, &total);
+	  mu_mailbox_translate (parser->msgset->mbox, MU_MAILBOX_MSGNO_TO_UID,
 				total, &lastuid);
 
 	  if (parser->number > lastuid)
@@ -572,15 +514,22 @@ parse_range (struct msgset_parser *parser)
 	  if (!lastuid)
 	    {
 	      size_t total;
-	      msgset_last (parser->mbox, &total);
-	      mu_mailbox_translate (parser->mbox, MU_MAILBOX_MSGNO_TO_UID,
+	      msgset_last (parser->msgset->mbox, &total);
+	      mu_mailbox_translate (parser->msgset->mbox,
+				    MU_MAILBOX_MSGNO_TO_UID,
 				    total, &lastuid);
 	    }
 	  if (start > lastuid && !parser->validuid)
 	    emptyrange_abort (parser->argv[-1]);
 	  start = 1;
 	}
-      add_message_range (parser, start, parser->number);
+      rc = mu_msgset_add_range (parser->msgset, start, parser->number,
+				MU_MSGSET_NUM);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_add_range", NULL, rc);
+	  exit (1);
+	}
     }
   else if (!parser->validuid)
     {
@@ -588,13 +537,12 @@ parse_range (struct msgset_parser *parser)
       exit (1);
     }
   else
-    mh_msgset_add (parser->msgset, start);
+    mu_msgset_add_range (parser->msgset, start, start, MU_MSGSET_NUM);
   return 1;
 }
   
   
-/* Parse a message specification. The composed msgset is
-   not sorted nor optimised */
+/* Parse a message specification saved in a configured PARSER. */
 static void
 msgset_parser_run (struct msgset_parser *parser)
 {
@@ -602,11 +550,9 @@ msgset_parser_run (struct msgset_parser *parser)
     ;
 }
 
-/* Parse a message specification from (argc;argv). Returned msgset is
-   sorted and optimised (i.e. it does not contain duplicate message
-   numbers) */
+/* Parse a message specification from (argc;argv).  */
 void
-mh_msgset_parse (mu_mailbox_t mbox, mh_msgset_t *msgset, 
+mh_msgset_parse (mu_msgset_t *msgset, mu_mailbox_t mbox, 
 		 int argc, char **argv, char *def)
 {
   struct msgset_parser parser;
@@ -629,11 +575,25 @@ mh_msgset_parse (mu_mailbox_t mbox, mh_msgset_t *msgset,
       argv[1] = NULL;
     }
   
-  mh_msgset_init (msgset);
-  msgset_parser_init (&parser, mbox, msgset, argc, argv);
+  msgset_parser_init (&parser, mbox, argc, argv);
   msgset_parser_run (&parser);
+  *msgset = parser.msgset;
+}
 
-  mh_msgset_optimize (msgset);
+void
+mh_msgset_parse_string (mu_msgset_t *msgset, mu_mailbox_t mbox, 
+			const char *string, char *def)
+{
+  struct mu_wordsplit ws;
+  
+  if (mu_wordsplit (string, &ws, MU_WRDSF_DEFFLAGS))
+    {
+      mu_error (_("cannot split line `%s': %s"), string,
+		mu_wordsplit_strerror (&ws));
+      exit (1);
+    }
+  mh_msgset_parse (msgset, mbox, ws.ws_wordc, ws.ws_wordv, def);
+  mu_wordsplit_free (&ws);
 }
 
 
diff --git a/mh/mh_sequence.c b/mh/mh_sequence.c
index a254ed3..509033f 100644
--- a/mh/mh_sequence.c
+++ b/mh/mh_sequence.c
@@ -16,6 +16,7 @@
    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <mh.h>
+#include <mailutils/sys/msgset.h>
 
 static char *
 private_sequence_name (const char *name)
@@ -62,118 +63,124 @@ delete_sequence (mu_mailbox_t mbox, const char *name, int private)
   write_sequence (mbox, name, NULL, private);
 }
 
-void
-mh_seq_add (mu_mailbox_t mbox, const char *name, mh_msgset_t *mset, int flags)
+struct format_closure
 {
-  const char *value = mh_seq_read (mbox, name, flags);
-  char *new_value, *p;
-  const char *buf;
-  size_t i, len;
+  mu_stream_t stream;
+  mu_mailbox_t mailbox;
+};
 
-  delete_sequence (mbox, name, !(flags & SEQ_PRIVATE));
+static int
+format_sequence (void *item, void *data)
+{
+  struct mu_msgrange *r = item;
+  struct format_closure *clos = data;
+  int rc;
+  size_t beg, end;
 
-  if (flags & SEQ_ZERO)
-    value = NULL;
-  
-  if (value)
-    len = strlen (value);
+  if (clos->mailbox)
+    {
+      rc = mu_mailbox_translate (clos->mailbox,
+				 MU_MAILBOX_MSGNO_TO_UID,
+				 r->msg_beg, &beg);
+      if (rc)
+	return rc;
+    }
+  else
+    beg = r->msg_beg;
+  if (r->msg_beg == r->msg_end)
+    rc = mu_stream_printf (clos->stream, " %lu", (unsigned long) beg);
   else
-    len = 0;
-  len++;
-  for (i = 0; i < mset->count; i++)
     {
-      buf = mu_umaxtostr (0, mset->list[i]);
-      len += strlen (buf) + 1;
+      if (clos->mailbox)
+	{
+	  rc = mu_mailbox_translate (clos->mailbox,
+				     MU_MAILBOX_MSGNO_TO_UID,
+				     r->msg_end, &end);
+	  if (rc)
+	    return rc;
+	}
+      else
+	end = r->msg_end;
+      if (beg + 1 == end)
+	rc = mu_stream_printf (clos->stream, " %lu %lu",
+			       (unsigned long) beg,
+			       (unsigned long) end);
+      else
+	rc = mu_stream_printf (clos->stream, " %lu-%lu",
+			       (unsigned long) beg,
+			       (unsigned long) end);
     }
+  return rc;
+}
 
-  new_value = xmalloc (len + 1);
-  if (value)
-    strcpy (new_value, value);
+static void
+save_sequence (mu_mailbox_t mbox, const char *name, mu_msgset_t mset,
+	       int flags)
+{
+  mu_list_t list;
+  
+  mu_msgset_get_list (mset, &list);
+  if (mu_list_is_empty (list))
+    write_sequence (mset->mbox, name, NULL, flags & SEQ_PRIVATE);
   else
-    new_value[0] = 0;
-  p = new_value + strlen (new_value);
-  *p++ = ' ';
-  for (i = 0; i < mset->count; i++)
     {
-      p += sprintf (p, "%s", mu_umaxtostr (0, mset->list[i]));
-      *p++ = ' ';
+      struct format_closure clos;
+      int rc;
+      mu_transport_t trans[2];
+      
+      rc = mu_memory_stream_create (&clos.stream, MU_STREAM_RDWR);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_memory_stream_create", NULL, rc);
+	  exit (1);
+	}
+      
+      clos.mailbox = mset->mbox;
+      rc = mu_list_foreach (list, format_sequence, &clos);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_list_foreach", NULL, rc);
+	  exit (1);
+	}
+      mu_stream_write (clos.stream, "", 1, NULL);
+      mu_stream_ioctl (clos.stream, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET,
+		       trans);
+      write_sequence (mbox, name, (char*)trans[0], flags & SEQ_PRIVATE);
+      mu_stream_unref (clos.stream);
     }
-  *p = 0;
-  write_sequence (mbox, name, new_value, flags & SEQ_PRIVATE);
-  /* FIXME
-  if (mu_c_strcasecmp (name, "cur") == 0)
-    current_message = strtoul (new_value, NULL, 0);
-  */
-  free (new_value);
 }
 
-static int
-cmp_msgnum (const void *a, const void *b)
+void
+mh_seq_add (mu_mailbox_t mbox, const char *name, mu_msgset_t mset, int flags)
 {
-  const size_t *as = a;
-  const size_t *bs = b;
-
-  if (*as < *bs)
-    return -1;
-  if (*as > *bs)
-    return 1;
-  return 0;
+  const char *value = mh_seq_read (mbox, name, flags);
+  
+  delete_sequence (mbox, name, !(flags & SEQ_PRIVATE));
+  if (value && !(flags & SEQ_ZERO))
+    {
+      mu_msgset_t oldset;
+      mh_msgset_parse_string (&oldset, mbox, value, "cur");
+      mu_msgset_add (oldset, mset);
+      save_sequence (mbox, name, oldset, flags);
+      mu_msgset_free (oldset);
+    }
+  else
+    save_sequence (mbox, name, mset, flags);
 }
 
 int
 mh_seq_delete (mu_mailbox_t mbox, const char *name,
-	       mh_msgset_t *mset, int flags)
+	       mu_msgset_t mset, int flags)
 {
   const char *value = mh_seq_read (mbox, name, flags);
-  char *new_val;
-  char *p;
-  size_t i, count;
-  struct mu_wordsplit ws;
-  
+  mu_msgset_t oldset;
+
   if (!value)
     return 0;
-
-  if (mu_wordsplit (value, &ws, MU_WRDSF_DEFFLAGS))
-    {
-      mu_error (_("cannot split line `%s': %s"), value,
-		mu_wordsplit_strerror (&ws));
-      return 0;
-    }
-
-  for (i = 0; i < ws.ws_wordc; i++)
-    {
-      char *p;
-      size_t num = strtoul (ws.ws_wordv[i], &p, 10);
-
-      if (*p)
-	continue;
-
-      if (bsearch (&num, mset->list, mset->count, sizeof (mset->list[0]),
-		   cmp_msgnum))
-	{
-	  free (ws.ws_wordv[i]);
-	  ws.ws_wordv[i] = NULL;
-	}
-    }
-
-  new_val = xstrdup (value);
-  p = new_val;
-  count = 0;
-  for (i = 0; i < ws.ws_wordc; i++)
-    {
-      if (ws.ws_wordv[i])
-	{
-	  strcpy (p, ws.ws_wordv[i]);
-	  p += strlen (p);
-	  *p++ = ' ';
-	  count++;
-	}
-    }
-  *p = 0;
-  write_sequence (mbox, name, count > 0 ? new_val : NULL, flags & SEQ_PRIVATE);
-  mu_wordsplit_free (&ws);
-  free (new_val);
-  
+  mh_msgset_parse_string (&oldset, mbox, value, "cur");
+  mu_msgset_sub (oldset, mset);
+  save_sequence (mbox, name, oldset, flags);
+  mu_msgset_free (oldset);
   return 0;
 }
 
diff --git a/mh/mhn.c b/mh/mhn.c
index 57d7418..1b1d902 100644
--- a/mh/mhn.c
+++ b/mh/mhn.c
@@ -158,7 +158,7 @@ static int width = 80;
 static char *charset;  /* Charset for output file names.  NULL means
 			  no recoding is necessary. */
 
-static mh_msgset_t msgset;
+static mu_msgset_t msgset;
 static mu_mailbox_t mbox;
 static mu_message_t message;
 static msg_part_t req_part;
@@ -1369,7 +1369,7 @@ list_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
 }
 
 int
-list_message (mu_message_t msg, size_t num)
+list_message (mu_message_t msg)
 {
   size_t uid;
   msg_part_t part;
@@ -1381,10 +1381,10 @@ list_message (mu_message_t msg, size_t num)
   return 0;
 }
 
-void
-list_iterator (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+int
+list_iterator (size_t num, mu_message_t msg, void *data)
 {
-  list_message (msg, num);
+  return list_message (msg);
 }
 
 int
@@ -1396,9 +1396,9 @@ mhn_list ()
     printf (_(" msg part type/subtype              size  description\n"));
 
   if (message)
-    rc = list_message (message, 0);
+    rc = list_message (message);
   else
-    rc = mh_iterate (mbox, &msgset, list_iterator, NULL);
+    rc = mu_msgset_foreach_message (msgset, list_iterator, NULL);
   return rc;
 }
 
@@ -1609,8 +1609,8 @@ show_message (mu_message_t msg, size_t num, void *data)
   return 0;
 }
 
-void
-show_iterator (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+int
+show_iterator (size_t num, mu_message_t msg, void *data)
 {
   msg_part_t part;
   
@@ -1618,6 +1618,7 @@ show_iterator (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
   part = msg_part_create (num);
   show_message (msg, num, data);
   msg_part_destroy (part);
+  return 0;
 }
 
 int
@@ -1630,7 +1631,7 @@ mhn_show ()
   if (message)
     rc = show_message (message, 0, mu_strout);
   else
-    rc = mh_iterate (mbox, &msgset, show_iterator, mu_strout);
+    rc = mu_msgset_foreach_message (msgset, show_iterator, mu_strout);
   mu_stream_flush (mu_strout);
   return rc;
 }
@@ -1855,10 +1856,11 @@ store_message (mu_message_t msg, void *data)
   msg_part_destroy (part);
 }
 
-void
-store_iterator (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+int
+store_iterator (size_t num, mu_message_t msg, void *data)
 {
   store_message (msg, data);
+  return 0;
 }
 
 int
@@ -1876,7 +1878,7 @@ mhn_store ()
       store_message (message, p);
     }
   else
-    rc = mh_iterate (mbox, &msgset, store_iterator, NULL);
+    rc = mu_msgset_foreach_message (msgset, store_iterator, NULL);
   return rc;
 }
 
@@ -2982,7 +2984,7 @@ main (int argc, char **argv)
   else
     {
       mbox = mh_open_folder (mh_current_folder (), MU_STREAM_READ);
-      mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
+      mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
     }
   
   switch (mode)
diff --git a/mh/mhpath.c b/mh/mhpath.c
index 089041f..63cf3f3 100644
--- a/mh/mhpath.c
+++ b/mh/mhpath.c
@@ -50,13 +50,14 @@ opt_handler (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
-void
-mhpath (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+static int
+mhpath (size_t num, mu_message_t msg, void *data)
 {
   size_t uid;
-      
+  
   mh_message_number (msg, &uid);
   printf ("%s/%s\n", (char*) data, mu_umaxtostr (0, uid));
+  return 0;
 }
 
 int
@@ -67,7 +68,7 @@ main (int argc, char **argv)
   mu_url_t url = NULL;
   char *mhdir;
   size_t total;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   int status;
   const char *current_folder;
   
@@ -125,9 +126,9 @@ main (int argc, char **argv)
   /* Mhpath  expands  and  sorts  the  message  list `msgs' and
      writes the full pathnames of the messages to the  standard
      output separated by newlines. */
-  mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
-  status = mh_iterate (mbox, &msgset, mhpath, mhdir);
+  mh_msgset_parse (&msgset, mbox, argc - index, argv + index, "cur");
+  status = mu_msgset_foreach_message (msgset, mhpath, mhdir);
   mu_mailbox_close (mbox);
   mu_mailbox_destroy (&mbox);
-  return status;
+  return status != 0;
 }
diff --git a/mh/mhseq.c b/mh/mhseq.c
index 1059c4a..8685b2c 100644
--- a/mh/mhseq.c
+++ b/mh/mhseq.c
@@ -63,13 +63,19 @@ opt_handler (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
+static int
+_print_number (size_t n, void *data)
+{
+  printf ("%lu\n", (unsigned long) n);
+  return 0;
+}
+
 int
 main (int argc, char **argv)
 {
   int index;
   mu_mailbox_t mbox;
-  mh_msgset_t msgset;
-  size_t i;
+  mu_msgset_t msgset;
     
   /* Native Language Support */
   MU_APP_INIT_NLS ();
@@ -82,12 +88,11 @@ main (int argc, char **argv)
   argv += index;
   mbox = mh_open_folder (mh_current_folder (), MU_STREAM_READ);
 
-  mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
+  mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
   if (uid_option)
-    mh_msgset_uids (mbox, &msgset);
-
-  for (i = 0; i < msgset.count; i++)
-    printf ("%lu\n", (unsigned long) msgset.list[i]);
+    mu_msgset_foreach_msguid (msgset, _print_number, NULL);
+  else
+    mu_msgset_foreach_msgno (msgset, _print_number, NULL);
   return 0;
 }
 
diff --git a/mh/pick.c b/mh/pick.c
index c819a29..51e3c7d 100644
--- a/mh/pick.c
+++ b/mh/pick.c
@@ -21,9 +21,6 @@
 #include <regex.h>
 #include <pick.h>
 #include <pick-gram.h>
-#define obstack_chunk_alloc malloc
-#define obstack_chunk_free free
-#include <obstack.h>
 
 static char doc[] = N_("GNU MH pick")"\v"
 N_("Compatibility syntax for picking a matching component is:\n\
@@ -146,8 +143,7 @@ static mu_list_t seq_list;  /* List of sequence names to operate upon */
 
 static mu_list_t lexlist;   /* List of input tokens */
 
-static struct obstack msgno_stk; /* Stack of selected message numbers */
-static size_t msgno_count;       /* Number of items on the stack */
+static mu_msgset_t picked_message_uids;
 
 static void
 add_sequence (char *name)
@@ -282,34 +278,26 @@ opt_handler (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
-void
-pick_message (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+static int
+pick_message (size_t num, mu_message_t msg, void *data)
 {
   if (pick_eval (msg))
     {
       mh_message_number (msg, &num);
       if (list)
 	printf ("%s\n", mu_umaxtostr (0, num));
-      if (seq_list)
-	{
-	  obstack_grow (&msgno_stk, &num, sizeof (num));
-	  msgno_count++;
-	}
+      if (picked_message_uids)
+	mu_msgset_add_range (picked_message_uids, num, num, MU_MSGSET_UID);
     }
+  return 0;
 }
 
 
-struct pick_closure
-{
-  mu_mailbox_t mbox;
-  mh_msgset_t *msgset;
-};
-
 static int
 action_add (void *item, void *data)
 {
-  struct pick_closure *clos = data;
-  mh_seq_add (clos->mbox, (char *)item, clos->msgset, seq_flags);
+  mu_mailbox_t mbox = data;
+  mh_seq_add (mbox, (char *)item, picked_message_uids, seq_flags);
   return 0;
 }
 
@@ -356,7 +344,7 @@ main (int argc, char **argv)
   int status;
   int index;
   mu_mailbox_t mbox;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   int interactive = mh_interactive_mode_p ();
 
   MU_APP_INIT_NLS ();
@@ -397,21 +385,13 @@ main (int argc, char **argv)
   argv += index;
 
   if (seq_list)
-    obstack_init (&msgno_stk);
+    mu_msgset_create (&picked_message_uids, NULL, MU_MSGSET_UID);
   
-  mh_msgset_parse (mbox, &msgset, argc, argv, "all");
-  status = mh_iterate (mbox, &msgset, pick_message, NULL);
+  mh_msgset_parse (&msgset, mbox, argc, argv, "all");
+  status = mu_msgset_foreach_message (msgset, pick_message, NULL);
 
-  if (seq_list)
-    {
-      struct pick_closure clos;
-      mh_msgset_t msgset;
-      msgset.count = msgno_count;
-      msgset.list = obstack_finish (&msgno_stk);
-      clos.mbox = mbox;
-      clos.msgset = &msgset;
-      mu_list_foreach (seq_list, action_add, &clos);
-    }
+  if (picked_message_uids)
+    mu_list_foreach (seq_list, action_add, mbox);
 
   mh_global_save_state ();
   mu_mailbox_close (mbox);
diff --git a/mh/refile.c b/mh/refile.c
index ca929e8..343ec91 100644
--- a/mh/refile.c
+++ b/mh/refile.c
@@ -201,8 +201,8 @@ refile (mu_message_t msg)
   enumerate_folders (refile_folder, msg);
 }
 
-void
-refile_iterator (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+int
+refile_iterator (size_t num, mu_message_t msg, void *data)
 {
   enumerate_folders (refile_folder, msg);
   if (!link_flag)
@@ -211,13 +211,14 @@ refile_iterator (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
       mu_message_get_attribute (msg, &attr);
       mu_attribute_set_deleted (attr);
     }
+  return 0;
 }
 
 int
 main (int argc, char **argv)
 {
   int index;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   mu_mailbox_t mbox;
   int status, i, j;
 
@@ -262,9 +263,9 @@ main (int argc, char **argv)
   else
     {
       mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
-      mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
+      mh_msgset_parse (&msgset, mbox, argc, argv, "cur");
 
-      status = mh_iterate (mbox, &msgset, refile_iterator, NULL);
+      status = mu_msgset_foreach_message (msgset, refile_iterator, NULL);
  
       mu_mailbox_expunge (mbox);
       mu_mailbox_close (mbox);
diff --git a/mh/repl.c b/mh/repl.c
index c70081a..9b5157b 100644
--- a/mh/repl.c
+++ b/mh/repl.c
@@ -112,7 +112,7 @@ static int width = 80;
 struct mh_whatnow_env wh_env = { 0 };
 static int initial_edit = 1;
 static const char *whatnowproc;
-static mh_msgset_t msgset;
+static mu_msgset_t msgset;
 static mu_mailbox_t mbox;
 static int build_only = 0; /* --build flag */
 static int query_mode = 0; /* --query flag */
@@ -281,6 +281,7 @@ make_draft (mu_mailbox_t mbox, int disp, struct mh_whatnow_env *wh)
   int rc;
   mu_message_t msg;
   struct stat st;
+  size_t msgno;
   
   /* First check if the draft exists */
   if (!build_only && stat (wh->draftfile, &st) == 0)
@@ -309,12 +310,13 @@ make_draft (mu_mailbox_t mbox, int disp, struct mh_whatnow_env *wh)
       unlink (wh->draftfile);
       break;  
     }
-  
-  rc = mu_mailbox_get_message (mbox, msgset.list[0], &msg);
+
+  msgno = mh_msgset_first (msgset);
+  rc = mu_mailbox_get_message (mbox, msgno, &msg);
   if (rc)
     {
       mu_error (_("cannot read message %s: %s"),
-		mu_umaxtostr (0, msgset.list[0]),
+		mu_umaxtostr (0, msgno),
 		mu_strerror (rc));
       exit (1);
     }
@@ -349,11 +351,11 @@ make_draft (mu_mailbox_t mbox, int disp, struct mh_whatnow_env *wh)
 	  mu_message_get_header (tmp_msg, &hdr);
 	  text = obstack_finish (&fcc_stack);
 	  mu_header_set_value (hdr, MU_HEADER_FCC, text, 0);
-	  mh_format (&format, tmp_msg, msgset.list[0], width, &buf);
+	  mh_format (&format, tmp_msg, msgno, width, &buf);
 	  mu_message_destroy (&tmp_msg, NULL);
 	}
       else
-	mh_format (&format, msg, msgset.list[0], width, &buf);
+	mh_format (&format, msg, msgno, width, &buf);
       
       mu_stream_write (str, buf, strlen (buf), NULL);
 
@@ -409,8 +411,8 @@ main (int argc, char **argv)
     }
 
   mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
-  mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
-  if (msgset.count != 1)
+  mh_msgset_parse (&msgset, mbox, argc - index, argv + index, "cur");
+  if (!mh_msgset_single_message (msgset))
     {
       mu_error (_("only one message at a time!"));
       return 1;
diff --git a/mh/rmm.c b/mh/rmm.c
index 5bc2ab8..592e2a4 100644
--- a/mh/rmm.c
+++ b/mh/rmm.c
@@ -50,12 +50,13 @@ opt_handler (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
-void
-rmm (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+static int
+rmm (size_t num, mu_message_t msg, void *data)
 {
   mu_attribute_t attr;
   mu_message_get_attribute (msg, &attr);
   mu_attribute_set_deleted (attr);
+  return 0;
 }
 
 int
@@ -63,7 +64,7 @@ main (int argc, char **argv)
 {
   int index = 0;
   mu_mailbox_t mbox;
-  mh_msgset_t msgset;
+  mu_msgset_t msgset;
   int status;
 
   /* Native Language Support */
@@ -75,9 +76,9 @@ main (int argc, char **argv)
 
   mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
 
-  mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
+  mh_msgset_parse (&msgset, mbox, argc - index, argv + index, "cur");
 
-  status = mh_iterate (mbox, &msgset, rmm, NULL);
+  status = mu_msgset_foreach_message (msgset, rmm, NULL);
 
   mu_mailbox_expunge (mbox);
   mu_mailbox_close (mbox);
diff --git a/mh/scan.c b/mh/scan.c
index bee454d..c0b228e 100644
--- a/mh/scan.c
+++ b/mh/scan.c
@@ -75,9 +75,9 @@ static int header;
 
 static mh_format_t format;
 
-static mh_msgset_t msgset;
+static mu_msgset_t msgset;
 
-void list_message (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data);
+static int list_message (size_t num, mu_message_t msg, void *data);
 void print_header (mu_mailbox_t mbox);
 void clear_screen (void);
 
@@ -153,7 +153,7 @@ action (mu_observer_t o, size_t type, void *data, void *action_data)
       counter++;
       mu_mailbox_get_message (mbox, counter, &msg);
       mh_message_number (msg, &num);
-      list_message (mbox, msg, num, NULL);
+      list_message (num, msg, NULL);
     }
   return 0;
 }
@@ -201,13 +201,11 @@ main (int argc, char **argv)
   else
     {
       mu_mailbox_messages_count (mbox, &total);
-      mh_msgset_parse (mbox, &msgset, argc, argv, "all");
-
-      if (reverse)
-	mh_msgset_reverse (&msgset);
+      mh_msgset_parse (&msgset, mbox, argc, argv, "all");
 
       print_header (mbox);
-      status = mh_iterate (mbox, &msgset, list_message, NULL);
+      status = mu_msgset_foreach_dir_message (msgset, reverse,
+					      list_message, NULL);
     }
 
   if (total == 0)
@@ -282,8 +280,8 @@ clear_screen ()
     }
 }
 
-void
-list_message (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
+static int
+list_message (size_t num, mu_message_t msg, void *data)
 {
   char *buffer;
   int len;
@@ -294,4 +292,5 @@ list_message (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
   if (len > 0 && buffer[len-1] != '\n')
     printf("\n");
   free (buffer);
+  return 0;
 }
diff --git a/mh/send.c b/mh/send.c
index 479e9cc..4318520 100644
--- a/mh/send.c
+++ b/mh/send.c
@@ -737,6 +737,27 @@ _action_send (void *item, void *data)
   return 0;
 }
 
+static int
+_add_to_mesg_list (size_t num, mu_message_t msg, void *data)
+{
+  char const *path = data;
+  struct list_elt *elt;
+  size_t uid;
+  int rc;
+  
+  elt = xmalloc (sizeof *elt);
+  elt->msg = msg;
+  mu_message_get_uid (msg, &uid);
+  elt->file_name = mu_make_file_name (path, mu_umaxtostr (0, uid));
+  rc = mu_list_append (mesg_list, elt);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_append", NULL, rc);
+      exit (1);
+    }
+  return 0;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -758,13 +779,12 @@ main (int argc, char **argv)
 
   if (draftfolder)
     {
-      mh_msgset_t msgset;
+      mu_msgset_t msgset;
       mu_url_t url;
       const char *path;
-      size_t i;
       
       mbox = mh_open_folder (draftfolder, MU_STREAM_RDWR|MU_STREAM_CREAT);
-      mh_msgset_parse (mbox, &msgset, argc, argv, draftmessage);
+      mh_msgset_parse (&msgset, mbox, argc, argv, draftmessage);
       mu_mailbox_get_url (mbox, &url);
       mu_url_sget_path (url, &path);
       if ((rc = mu_list_create (&mesg_list)))
@@ -772,25 +792,9 @@ main (int argc, char **argv)
 	  mu_error (_("cannot create message list: %s"), mu_strerror (rc));
 	  exit (1);
 	}
-      for (i = 0; i < msgset.count; i++)
-	{
-	  struct list_elt *elt;
-	  size_t uid;
-	  
-	  elt = xmalloc (sizeof *elt);
-	  mu_mailbox_get_message (mbox, msgset.list[i], &elt->msg);
-	  mu_message_get_uid (elt->msg, &uid);
-	  elt->file_name =
-	    mu_make_file_name (path, mu_umaxtostr (0, uid));
-	  rc = mu_list_append (mesg_list, elt);
-	  if (rc)
-	    {
-	      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_append", NULL, rc);
-	      exit (1);
-	    }
-	}
+      mu_msgset_foreach_message (msgset, _add_to_mesg_list, (void*)path);
       
-      mh_msgset_free (&msgset);
+      mu_msgset_free (msgset);
     }
   else
     {
diff --git a/mh/sortm.c b/mh/sortm.c
index 5778793..b0de66b 100644
--- a/mh/sortm.c
+++ b/mh/sortm.c
@@ -92,7 +92,10 @@ static int limit;
 static int verbose;
 static mu_mailbox_t mbox;
 static const char *mbox_path;
-static mh_msgset_t msgset;
+
+static size_t *msgarr;
+static size_t msgcount;
+
 static size_t current_num;
 
 #define ACTION_REORDER   0
@@ -416,17 +419,17 @@ shell_sort ()
     int h, s, i, j;
     size_t hold;
 
-    for (h = startdst (msgset.count, &s); s > 0; s--, h = prevdst (h))
+    for (h = startdst (msgcount, &s); s > 0; s--, h = prevdst (h))
       {
 	if (verbose > 1)
 	  fprintf (stderr, _("distance %d\n"), h);
-        for (j = h; j < msgset.count; j++)
+        for (j = h; j < msgcount; j++)
 	  {
-            hold = msgset.list[j];
+            hold = msgarr[j];
             for (i = j - h;
-		 i >= 0 && comp0 (hold, msgset.list[i]) < 0; i -= h)
-	      msgset.list[i + h] = msgset.list[i];
-	    msgset.list[i + h] = hold;
+		 i >= 0 && comp0 (hold, msgarr[i]) < 0; i -= h)
+	      msgarr[i + h] = msgarr[i];
+	    msgarr[i + h] = hold;
 	  }
       }
 }
@@ -467,12 +470,12 @@ transpose(size_t i, size_t n)
 {
   size_t j;
 
-  for (j = i+1; j < msgset.count; j++)
-    if (msgset.list[j] == n)
+  for (j = i+1; j < msgcount; j++)
+    if (msgarr[j] == n)
       {
-	size_t t = msgset.list[i];
-	msgset.list[i] = msgset.list[j];
-	msgset.list[j] = t;
+	size_t t = msgarr[i];
+	msgarr[i] = msgarr[j];
+	msgarr[j] = t;
 	break;
       }
 }
@@ -489,26 +492,25 @@ void
 sort ()
 {
   size_t *oldlist, i;
-  oldlist = xmalloc (msgset.count * sizeof (*oldlist));
-  memcpy (oldlist, msgset.list, msgset.count * sizeof (*oldlist));
+  oldlist = xmalloc (msgcount * sizeof (*oldlist));
+  memcpy (oldlist, msgarr, msgcount * sizeof (*oldlist));
 
   switch (algorithm)
     {
     case ARG_QUICKSORT:
-      qsort(msgset.list, msgset.count, sizeof (msgset.list[0]),
-	    comp);
+      qsort (msgarr, msgcount, sizeof (msgarr[0]), comp);
       break;
 
     case ARG_SHELL:
-      shell_sort();
+      shell_sort ();
       break;
     }
 
   switch (action)
     {
     case ACTION_LIST:
-      for (i = 0; i < msgset.count; i++)
-	list_message (msgset.list[i]);
+      for (i = 0; i < msgcount; i++)
+	list_message (msgarr[i]);
       break;
 
     default:
@@ -519,16 +521,16 @@ sort ()
       
       if (verbose)
 	fprintf (stderr, _("Transpositions:\n"));
-      for (i = 0, got_signal = 0; !got_signal && i < msgset.count; i++)
+      for (i = 0, got_signal = 0; !got_signal && i < msgcount; i++)
 	{
-	  if (msgset.list[i] != oldlist[i])
+	  if (msgarr[i] != oldlist[i])
 	    {
 	      size_t old_num, new_num;
 	      mu_message_t msg;
 
 	      mu_mailbox_get_message (mbox, oldlist[i], &msg);
 	      mh_message_number (msg, &old_num);
-	      mu_mailbox_get_message (mbox, msgset.list[i], &msg);
+	      mu_mailbox_get_message (mbox, msgarr[i], &msg);
 	      mh_message_number (msg, &new_num);
 	      transpose (i, oldlist[i]);
 	      if (verbose)
@@ -563,6 +565,38 @@ sort ()
       mh_mailbox_set_cur (mbox, current_num);
     }
 }
+
+static int
+_add_msgno (size_t n, void *data)
+{
+  size_t *pidx = data;
+  msgarr[*pidx] = n;
+  ++*pidx;
+  return 0;
+}
+
+static void
+fill_msgarr (mu_msgset_t msgset)
+{
+  size_t i;
+  int rc;
+  
+  rc = mu_msgset_count (msgset, &msgcount);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_count", NULL, rc);
+      exit (1);
+    }
+      
+  msgarr = xcalloc (msgcount, sizeof (msgarr[0]));
+  i = 0;
+  rc = mu_msgset_foreach_msgno (msgset, _add_msgno, &i);
+  if (rc)
+    {
+      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_foreach_msgno", NULL, rc);
+      exit (1);
+    }
+}
 
 
 /* Main */
@@ -572,6 +606,7 @@ main (int argc, char **argv)
 {
   int index;
   mu_url_t url;
+  mu_msgset_t msgset;
   
   MU_APP_INIT_NLS ();
   mh_argp_init ();
@@ -597,7 +632,9 @@ main (int argc, char **argv)
 
   mh_mailbox_get_cur (mbox, &current_num);
 
-  mh_msgset_parse (mbox, &msgset, argc, argv, "all");
+  mh_msgset_parse (&msgset, mbox, argc, argv, "all");
+  fill_msgarr (msgset);
+  mu_msgset_free (msgset);
   sort ();
   mh_global_save_state ();
   mu_mailbox_destroy (&mbox);
diff --git a/mh/tests/mark.at b/mh/tests/mark.at
index 8072dc7..363fafc 100644
--- a/mh/tests/mark.at
+++ b/mh/tests/mark.at
@@ -25,7 +25,7 @@ mark -sequence andro -add 3-5
 sed -n '/^andro:/{s/  */ /g;s/ $//;p;}' Mail/inbox/.mh_sequences
 ],
 [0],
-[andro: 2 3 4 5
+[andro: 2-5
 ])
 
 MH_CHECK([mark -add -zero],[mark01 mark-add-zero],[
@@ -48,7 +48,7 @@ mark -nopublic -sequence andro -add 1 2 3
 sed -n '/^atr-andro/{s/  */ /g;s/ $//;s/^[[^:]]*:/atr-andro:/;p;}' Mail/context
 ],
 [0],
-[atr-andro: 1 2 3
+[atr-andro: 1-3
 ])
 
 MH_CHECK([mark -del],[mark03 mark-del],[
@@ -67,7 +67,7 @@ MUT_MBCOPY($abs_top_srcdir/testsuite/mh/mbox1,[Mail/inbox])
 echo 'Current-Folder: inbox' > Mail/context
 inbox=`pwd`/Mail/inbox
 cat > Mail/context <<EOT
-atr-andro-$inbox: 2 3 4 5
+atr-andro-$inbox: 2-5
 EOT
 mark -nopublic -sequence andro -del 3
 sed -n '/^atr-andro/{s/  */ /g;s/ $//;s/^[[^:]]*:/atr-andro:/;p;}' Mail/context
diff --git a/mh/whatnowenv.c b/mh/whatnowenv.c
index 8a69d58..78a6994 100644
--- a/mh/whatnowenv.c
+++ b/mh/whatnowenv.c
@@ -17,6 +17,13 @@
 
 #include <mh.h>
 
+static int
+_add_to_list (size_t num, mu_message_t msg, void *data)
+{
+  mu_list_t list = data;
+  return mu_list_append (list, msg);
+}
+
 void
 mh_whatnow_env_from_environ (struct mh_whatnow_env *wh)
 {
@@ -39,38 +46,15 @@ mh_whatnow_env_from_environ (struct mh_whatnow_env *wh)
 	    wh->anno_field = NULL;
 	  else
 	    {
-	      size_t i;
-	      struct mu_wordsplit ws;
-	      mh_msgset_t msgset;
+	      mu_msgset_t msgset;
 	      mu_mailbox_t mbox = mh_open_folder (folder, MU_STREAM_RDWR);
 	      
-	      if (mu_wordsplit (p, &ws,
-				MU_WRDSF_DEFFLAGS & ~MU_WRDSF_CESCAPES))
-		{
-		  mu_error (_("cannot parse mhmessages (%s): %s"), p,
-			    mu_wordsplit_strerror (&ws));
-		  exit (1);
-		}
-	      mh_msgset_parse (mbox, &msgset, ws.ws_wordc, ws.ws_wordv, "cur");
-	      mu_wordsplit_free (&ws);
+	      mh_msgset_parse_string (&msgset, mbox, p, "cur");
 
 	      wh->mbox = mbox;
 	      mu_list_create (&wh->anno_list);
-	      for (i = 0; i < msgset.count; i++)
-		{
-		  mu_message_t msg;
-		  int rc = mu_mailbox_get_message (mbox, msgset.list[i], &msg);
-		  if (rc)
-		    {
-		      mu_error (_("cannot get message %lu from %s: %s"),
-				(unsigned long) msgset.list[i],
-				folder,
-				mu_strerror (rc));
-		      continue;
-		    }
-		  mu_list_append (wh->anno_list, msg);
-		}
-	      mh_msgset_free (&msgset);
+	      mu_msgset_foreach_message (msgset, _add_to_list, wh->anno_list);
+	      mu_msgset_free (msgset);
 	      /* FIXME:
 		 wh->anno_inplace = getenv ("mhinplace");
 	      */
diff --git a/mu/imap.c b/mu/imap.c
index 8e3e2bf..d8628c8 100644
--- a/mu/imap.c
+++ b/mu/imap.c
@@ -118,13 +118,13 @@ parse_msgset (const char *arg)
   mu_msgset_t msgset;
   char *p;
   
-  status = mu_msgset_create (&msgset, NULL, 0);
+  status = mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM);
   if (status)
     {
       mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, status);
       return NULL;
     }
-  status = mu_msgset_parse_imap (msgset, arg, &p);
+  status = mu_msgset_parse_imap (msgset, MU_MSGSET_NUM, arg, &p);
   if (status)
     {
       mu_error (_("failed to parse message set near \"%s\": %s"),
diff --git a/testsuite/msgset.c b/testsuite/msgset.c
index ae2a099..8ddc2b1 100644
--- a/testsuite/msgset.c
+++ b/testsuite/msgset.c
@@ -19,7 +19,7 @@
 #include <mailutils/mailutils.h>
 
 static void
-parse_msgset (char *arg, struct mu_msgrange *range)
+parse_msgrange (char *arg, struct mu_msgrange *range)
 {
   size_t msgnum;
   char *p;
@@ -52,14 +52,36 @@ parse_msgset (char *arg, struct mu_msgrange *range)
   range->msg_end = msgnum;
 }
 
+mu_msgset_t
+parse_msgset (const char *arg, mu_mailbox_t mbox,
+	      int create_mode, int parse_mode)
+{
+  int rc;
+  mu_msgset_t msgset;
+  char *end;
+  
+  MU_ASSERT (mu_msgset_create (&msgset, mbox, create_mode));
+  if (arg)
+    {
+      rc = mu_msgset_parse_imap (msgset, parse_mode, arg, &end);
+      if (rc)
+        {
+          mu_error ("mu_msgset_parse_imap: %s near %s",
+	  	    mu_strerror (rc), end);
+          exit (1);
+        }
+    }
+  return msgset;
+}
+
 int
 main (int argc, char **argv)
 {
   int i;
   char *msgset_string = NULL;
   mu_msgset_t msgset;
-  int rc;
-  int flags = 0;
+  int create_mode = MU_MSGSET_NUM;
+  int parse_mode = MU_MSGSET_NUM;
   mu_mailbox_t mbox = NULL;
   
   mu_set_program_name (argv[0]);
@@ -70,14 +92,23 @@ main (int argc, char **argv)
 
       if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
 	{
-	  mu_printf ("usage: %s [-msgset=SET] [-add X[:Y]] [-del X:[Y]]...\n",
+	  mu_printf ("usage: %s [-msgset[uid]=SET] [-uid] [-add[uid]=X[:Y]] [-del[uid]=X[:Y]] "
+		     "[-addset[uid]=SET] [-delset[uid]=SET] ...\n",
 		     mu_program_name);
 	  return 0;
 	}
       else if (strncmp (arg, "-msgset=", 8) == 0)
-	msgset_string = arg + 8;
+	{
+	  parse_mode = MU_MSGSET_NUM;
+	  msgset_string = arg + 8;
+	}
+      else if (strncmp (arg, "-msgsetuid=", 11) == 0)
+	{
+	  parse_mode = MU_MSGSET_UID;
+	  msgset_string = arg + 11;
+	}      
       else if (strcmp (arg, "-uid") == 0)
-	flags |= MU_MSGSET_UID;
+	create_mode = MU_MSGSET_UID;
       else if (strncmp (arg, "-mailbox=", 9) == 0)
 	{
 	  MU_ASSERT (mu_mailbox_create (&mbox, arg + 9));
@@ -87,18 +118,7 @@ main (int argc, char **argv)
 	break;
     }
 
-  MU_ASSERT (mu_msgset_create (&msgset, mbox, flags));
-  if (msgset_string)
-    {
-      char *end;
-      rc = mu_msgset_parse_imap (msgset, msgset_string, &end);
-      if (rc)
-	{
-	  mu_error ("mu_msgset_parse_imap: %s near %s",
-		    mu_strerror (rc), end);
-	  return 1;
-	}
-    }
+  msgset = parse_msgset (msgset_string, mbox, create_mode, parse_mode);
   
   for (; i < argc; i++)
     {
@@ -107,15 +127,102 @@ main (int argc, char **argv)
       
       if (strncmp (arg, "-add=", 5) == 0)
 	{
-	  parse_msgset (arg + 5, &range);
-	  MU_ASSERT (mu_msgset_add_range (msgset, range.msg_beg,
-					  range.msg_end));
+	  parse_msgrange (arg + 5, &range);
+	  MU_ASSERT (mu_msgset_add_range (msgset,
+					  range.msg_beg, range.msg_end,
+					  MU_MSGSET_NUM));
 	}
       else if (strncmp (arg, "-sub=", 5) == 0)
 	{
-	  parse_msgset (arg + 5, &range);
-	  MU_ASSERT (mu_msgset_sub_range (msgset, range.msg_beg,
-					  range.msg_end));
+	  parse_msgrange (arg + 5, &range);
+	  MU_ASSERT (mu_msgset_sub_range (msgset,
+					  range.msg_beg, range.msg_end,
+					  MU_MSGSET_NUM));
+	}
+      else if (strncmp (arg, "-adduid=", 8) == 0)
+	{
+	  parse_msgrange (arg + 8, &range);
+	  MU_ASSERT (mu_msgset_add_range (msgset,
+					  range.msg_beg, range.msg_end,
+					  MU_MSGSET_UID));
+	}
+      else if (strncmp (arg, "-subuid=", 8) == 0)
+	{
+	  parse_msgrange (arg + 8, &range);
+	  MU_ASSERT (mu_msgset_sub_range (msgset,
+					  range.msg_beg, range.msg_end,
+					  MU_MSGSET_UID));
+	}
+      else if (strncmp (arg, "-addset", 7) == 0)
+	{
+	  mu_msgset_t tset;
+	  int m;
+
+	  arg += 7;
+	  if (strncmp (arg, "uid", 3) == 0)
+	    {
+	      m = MU_MSGSET_UID;
+	      arg += 3;
+	    }
+	  else
+	    m = MU_MSGSET_NUM;
+	  if (*arg == '=')
+	    arg++;
+	  else
+	    {
+	      mu_error ("unknown option %s", argv[i]);
+	      return 1;
+	    }
+	  
+	  tset = parse_msgset (arg, mbox, m, m);
+	  if (!msgset)
+	    msgset = tset;
+	  else
+	    {
+	      MU_ASSERT (mu_msgset_add (msgset, tset));
+	      mu_msgset_free (tset);
+	    }
+	}
+      else if (strncmp (arg, "-subset=", 8) == 0)
+	{
+	  mu_msgset_t tset;
+	  int m;
+
+	  arg += 7;
+	  if (strncmp (arg, "uid", 3) == 0)
+	    {
+	      m = MU_MSGSET_UID;
+	      arg += 3;
+	    }
+	  else
+	    m = MU_MSGSET_NUM;
+	  if (*arg == '=')
+	    arg++;
+	  else
+	    {
+	      mu_error ("unknown option %s", argv[i]);
+	      return 1;
+	    }
+	  
+	  tset = parse_msgset (arg, mbox, m, m);
+	  
+	  if (!msgset)
+	    {
+	      mu_error ("no initial message set");
+	      exit (1);
+	    }
+	  else
+	    {
+	      MU_ASSERT (mu_msgset_sub (msgset, tset));
+	      mu_msgset_free (tset);
+	    }
+	}
+      else if (strcmp (arg, "-neg") == 0)
+	{
+	  mu_msgset_t negated_set;
+	  MU_ASSERT (mu_msgset_negate (msgset, &negated_set));
+	  mu_msgset_free (msgset);
+	  msgset = negated_set;
 	}
       else
       	{


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 30 15:17:00 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rgisu-0007z9-E8
	for mharc-commit-mailutils@gnu.org; Fri, 30 Dec 2011 15:17:00 -0500
Received: from eggs.gnu.org ([140.186.70.92]:56644)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rgisr-0007z2-Mz
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 15:16:58 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rgisq-0007GM-Fh
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 15:16:57 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:41086)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rgisq-0007GG-Cv
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 15:16:56 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rgisp-00009Q-Rq
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 20:16:56 +0000
Date: Fri, 30 Dec 2011 20:16:55 +0000
Message-Id: <E1Rgisp-00009Q-Rq@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-559-gd4dd5a8
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: a01bb419262e90164a558075e3607fb3b0514443
X-Git-Newrev: d4dd5a8a5777be7ba655b87b483659206ea0cf54
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 30 Dec 2011 20:16:58 -0000

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

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

The branch, master has been updated
       via  d4dd5a8a5777be7ba655b87b483659206ea0cf54 (commit)
      from  a01bb419262e90164a558075e3607fb3b0514443 (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 d4dd5a8a5777be7ba655b87b483659206ea0cf54
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Fri Dec 30 22:18:45 2011 +0200

    Fix a bug in DOT decoder.

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

Summary of changes:
 libmailutils/filter/dot.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/libmailutils/filter/dot.c b/libmailutils/filter/dot.c
index b42d63d..90d61e4 100644
--- a/libmailutils/filter/dot.c
+++ b/libmailutils/filter/dot.c
@@ -74,7 +74,10 @@ new_decode_state (enum dot_decode_state state, int c)
 	{
 	case '.':
 	  return dot_decode_dot;
+	case '\n':
+	  return dot_decode_lf;
 	}
+      break;
       
     case dot_decode_dot:
       switch (c)


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Fri Dec 30 17:43:22 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1RglAY-0001dw-HF
	for mharc-commit-mailutils@gnu.org; Fri, 30 Dec 2011 17:43:22 -0500
Received: from eggs.gnu.org ([140.186.70.92]:37375)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RglAU-0001dR-W6
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 17:43:20 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RglAR-0003du-Sa
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 17:43:18 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:40617)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RglAR-0003dh-QO
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 17:43:15 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1RglAP-0007Uz-1Q
	for commit-mailutils@gnu.org; Fri, 30 Dec 2011 22:43:15 +0000
Date: Fri, 30 Dec 2011 22:43:13 +0000
Message-Id: <E1RglAP-0007Uz-1Q@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-560-g356c075
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: d4dd5a8a5777be7ba655b87b483659206ea0cf54
X-Git-Newrev: 356c07542f39f1ff159e2646bd97e1791f493aad
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Fri, 30 Dec 2011 22:43:20 -0000

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

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

The branch, master has been updated
       via  356c07542f39f1ff159e2646bd97e1791f493aad (commit)
      from  d4dd5a8a5777be7ba655b87b483659206ea0cf54 (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 356c07542f39f1ff159e2646bd97e1791f493aad
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 31 00:37:46 2011 +0200

    Add SMTP test.
    
    * examples/mta.c: Handle -bD option. Detach from the controlling
    terminal if -bd is given. Print pid and port number in this case.
    * testsuite/smtpsend.c: Fix handling of the trace= option.
    * testsuite/Makefile.am: Add new tests.
    * testsuite/smtp-msg.at: New test case.
    * testsuite/smtp-str.at: New test case.
    * testsuite/testsuite.at: Include new test cases.

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

Summary of changes:
 examples/mta.c         |  161 ++++++++++++++++++++++++++++++++++++-----------
 testsuite/Makefile.am  |    2 +
 testsuite/smtp-msg.at  |   61 ++++++++++++++++++
 testsuite/smtp-str.at  |   62 ++++++++++++++++++
 testsuite/smtpsend.c   |   14 +++-
 testsuite/testsuite.at |    4 +
 6 files changed, 262 insertions(+), 42 deletions(-)
 create mode 100644 testsuite/smtp-msg.at
 create mode 100644 testsuite/smtp-str.at

diff --git a/examples/mta.c b/examples/mta.c
index 4008f78..3cdd7f2 100644
--- a/examples/mta.c
+++ b/examples/mta.c
@@ -27,13 +27,21 @@
    recipients.
 
    2. mta -bd [-p port]
-
+      mta -bD [-p port]
+      
    Operates as daemon. If port is given, mta will listen on that port.
    Otherwise, it will use the first free port in the range 1024-65535.
-   In this case, mta prints the port number on the stdout, prior to
-   starting operation. Notice, that in this mode mta does not disconnect
-   itself from the controlling terminal, it always stays on the foreground.
+   If the -bd option is given, mta switches to the background and prints
+   its PID and port number on two separate lines on the stdout.  It then
+   listens for incoming connections for no more than 60 seconds.
+
+   If the -bD option is given, the utility prints the port number and
+   listens for incoming connections not detaching itself from the controlling
+   terminal.
 
+   In any case, at most one connection is served, after which the utility
+   terminates.
+   
    Environment variables:
 
    MTA_DOMAIN   Sets the default domain name for email addresses. Default
@@ -66,12 +74,13 @@
 FILE *diag = NULL;       /* diagnostic output */
 int smtp_mode;           /* Operate in smtp mode */
 int port = 0;            /* Port number (for smtp mode) */
+int foreground = 0;      /* Stay in the foreground (smtp mode) */
 char *from_person = NULL; /* Set the name of the `from' person */
 int read_recipients = 0; /* Read the message for recipients */
 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
@@ -95,9 +104,7 @@ main (int argc, char **argv)
   int c, status;
   char *domain;
 
-  progname = strrchr (argv[0], '/');
-  if (!progname)
-    progname = argv[0];
+  mu_set_program_name (argv[0]);
   
   while ((c = getopt (argc, argv, "b:f:p:to:")) != EOF)
     {
@@ -110,6 +117,11 @@ main (int argc, char **argv)
 	      smtp_mode = 1;
 	      break;
 
+	    case 'D':
+	      smtp_mode = 1;
+	      foreground = 1;
+	      break;
+	      
 	    default:
 	      /*FIXME*/;
 	    }
@@ -153,13 +165,13 @@ main (int argc, char **argv)
 	  diag = fopen (name, mode);
 	  if (!diag)
 	    {
-	      mu_error ("%s: can't open diagnostic output: %s",
-			progname, name);
+	      mu_error ("can't open diagnostic output %s: %s",
+			name, mu_strerror (errno));
 	      return 1;
 	    }
 	}
       else
-	diag = stdout;
+	diag = stderr;
     }
   
   register_handlers ();
@@ -226,7 +238,7 @@ make_tmp (mu_stream_t in)
       const char *from = from_address ();
       if (!from)
 	{
-	  mu_error ("%s: can't determine sender address", progname);
+	  mu_error ("can't determine sender address");
 	  exit (EX_NOUSER);
 	}
 	  
@@ -301,7 +313,7 @@ address_email_string (mu_address_t addr)
   value = malloc (length + 1);
   if (!value)
     {
-      mu_error ("%s: not enough memory", progname);
+      mu_error ("not enough memory");
       return NULL;
     }
   p = value;
@@ -390,7 +402,7 @@ message_finalize (mu_message_t msg, int warn)
 			   sizeof (SENDER_WARNING));
       if (warn == NULL)
 	{
-	  mu_error ("%s: not enough memory", progname);
+	  mu_error ("not enough memory");
 	  return 1;
 	}
       sprintf (warn, "%s %s", pwd->pw_name, SENDER_WARNING);
@@ -406,7 +418,7 @@ message_finalize (mu_message_t msg, int warn)
 	{
 	  if (add_recipient (value))
 	    {
-	      mu_error ("%s: bad address %s", progname, value);
+	      mu_error ("bad address %s", value);
 	      return 1;
 	    }
 	  free (value);
@@ -416,7 +428,7 @@ message_finalize (mu_message_t msg, int warn)
 	{
 	  if (add_recipient (value))
 	    {
-	      mu_error ("%s: bad address %s", progname, value);
+	      mu_error ("bad address %s", value);
 	      return 1;
 	    }
 	  free (value);
@@ -427,7 +439,7 @@ message_finalize (mu_message_t msg, int warn)
 	{
 	  if (add_recipient (value))
 	    {
-	      mu_error ("%s: bad address %s", progname, value);
+	      mu_error ("bad address %s", value);
 	      return 1;
 	    }
 	  free (value);
@@ -443,8 +455,8 @@ message_finalize (mu_message_t msg, int warn)
       c = mu_address_sget_printable (recipients, &sptr);
       if (c)
 	{
-	  mu_error ("%s: mu_address_sget_printable failure: %s",
-		    progname, mu_strerror (c));
+	  mu_error ("mu_address_sget_printable failure: %s",
+		    mu_strerror (c));
 	  return 1;
 	}
       mu_header_set_value (header, MU_HEADER_TO, sptr, 1);
@@ -463,7 +475,7 @@ mta_stdin (int argc, char **argv)
     {
       if (add_recipient (argv[c]))
 	{
-	  mu_error ("%s: bad address %s", progname, argv[c]);
+	  mu_error ("bad address %s", argv[c]);
 	  return 1;
 	}
     }
@@ -483,7 +495,7 @@ mta_stdin (int argc, char **argv)
   
   if (!recipients)
     {
-      mu_error ("%s: Recipient names must be specified", progname);
+      mu_error ("recipient names must be specified");
       return 1;
     }
 
@@ -501,7 +513,7 @@ smtp_reply (mu_stream_t str, int code, char *fmt, ...)
   va_start (ap, fmt);
   mu_stream_vprintf (str, fmt, ap);
   va_end (ap);
-  mu_stream_printf (str, "\r\n");
+  mu_stream_printf (str, "\n");
 }
 
 #define STATE_INIT   0
@@ -563,14 +575,22 @@ smtp (mu_stream_t str)
   char *rcpt_addr;
   int wsflags = MU_WRDSF_DEFFLAGS;
   struct mu_wordsplit ws;
-  
+
   smtp_reply (str, 220, "Ready");
   for (state = STATE_INIT; state != STATE_QUIT; )
     {
+      int rc;
       int kw;
       size_t len;
+
+      rc = mu_stream_getline (str, &buf, &size, &len);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_getline", NULL, rc);
+	  exit (EX_PROTOCOL);
+	}
       
-      if (mu_stream_getline (str, &buf, &size, &len) || len == 0)
+      if (len == 0)
 	exit (EX_PROTOCOL);
 
       if (mu_wordsplit (buf, &ws, wsflags))
@@ -702,8 +722,8 @@ smtp (mu_stream_t str)
 		mu_message_t msg;
 		int rc;
 		
-		rc = mu_filter_create (&flt, str, "CRLFDOT", MU_FILTER_DECODE,
-				       MU_STREAM_READ|MU_STREAM_WRTHRU);
+		rc = mu_filter_create (&flt, str, "DOT", MU_FILTER_DECODE,
+				       MU_STREAM_READ);
 		if (rc)
 		  {
 		    mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_create",
@@ -738,17 +758,22 @@ smtp (mu_stream_t str)
     }
 }
 
+#ifndef INADDR_LOOPBACK
+# define INADDR_LOOPBACK 0x7f000001
+#endif
+
 int
 mta_smtp (int argc, char **argv)
 {
   int on = 1;
   struct sockaddr_in address;
   int fd;
+  pid_t pid;
   
   fd = socket (PF_INET, SOCK_STREAM, 0);
   if (fd < 0)
     {
-      mu_error ("%s: socket: %s", progname, strerror (errno));
+      mu_error ("socket: %s", mu_strerror (errno));
       return 1;
     }
 
@@ -756,7 +781,7 @@ mta_smtp (int argc, char **argv)
 
   memset (&address, 0, sizeof (address));
   address.sin_family = AF_INET;
-  address.sin_addr.s_addr = INADDR_ANY;
+  address.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
 
   if (port)
     {
@@ -764,7 +789,7 @@ mta_smtp (int argc, char **argv)
       if (bind (fd, (struct sockaddr *) &address, sizeof (address)) < 0)
 	{
 	  close (fd);
-	  mu_error ("%s: bind: %s", progname, strerror(errno));
+	  mu_error ("bind: %s", mu_strerror (errno));
 	  return 1;
 	}
     }
@@ -777,8 +802,7 @@ mta_smtp (int argc, char **argv)
 	{
 	  if (++port >= 65535)
 	    {
-	      mu_error ("%s: can't bind socket: all ports in use?",
-			progname);
+	      mu_error ("can't bind socket: all ports in use?");
 	      return 1;
 	    }
 	  address.sin_port = htons (port);
@@ -788,8 +812,30 @@ mta_smtp (int argc, char **argv)
     }
 
   listen (fd, 5);
-  printf ("%d\n", port);
+  if (!foreground)
+    {
+      pid = fork ();
+      if (pid == -1)
+	{
+	  mu_error ("fork: %s", mu_strerror (errno));
+	  return 1;
+	}
+      if (pid)
+	{
+	  /* Master process. */
+	  printf ("%lu\n", (unsigned long) pid);
+	  printf ("%d\n", port);
+	  return 0;
+	}
+    }
+  else
+    printf ("%d\n", port);
+  
+  /* Child */
   fclose (stdout);
+  if (!foreground)
+    alarm (60);
+  
   while (1)
     {
       fd_set rfds;
@@ -797,7 +843,7 @@ mta_smtp (int argc, char **argv)
       int sfd, status;
       socklen_t len;
       int rc;
-      mu_stream_t str;
+      mu_stream_t str, istream, ostream, flt;
       
       FD_ZERO (&rfds);
       FD_SET (fd, &rfds);
@@ -807,22 +853,61 @@ mta_smtp (int argc, char **argv)
 	{
 	  if (errno == EINTR)
 	    continue;
-	  mu_error ("%s: select: %s", progname, strerror (errno));
+	  mu_error ("select: %s", mu_strerror (errno));
 	  return 1;
 	}
 
       len = sizeof (his_addr);
       if ((sfd = accept (fd, (struct sockaddr *)&his_addr, &len)) < 0)
 	{
-	  mu_error ("%s: accept: %s", progname, strerror (errno));
+	  mu_error ("accept: %s", mu_strerror (errno));
 	  return 1;
 	}
 
-      rc = mu_fd_stream_create (&str, NULL, sfd, MU_STREAM_RDWR);
+      rc = mu_stdio_stream_create (&istream, sfd, MU_STREAM_READ);
       if (rc)
 	{
-	  mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
-	  break;
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+			   "MU_STREAM_READ", rc);
+	  return 1;
+	}
+      mu_stream_set_buffer (istream, mu_buffer_line, 0);
+      rc = mu_filter_create (&flt, istream, "CRLF", MU_FILTER_DECODE,
+			     MU_STREAM_READ);
+      mu_stream_unref (istream);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_create",
+			   "CRLF,MU_FILTER_DECODE", rc);
+	  return 1;
+	}
+      mu_stream_set_buffer (flt, mu_buffer_line, 0);
+      istream = flt;
+
+      rc = mu_stdio_stream_create (&ostream, sfd, MU_STREAM_WRITE);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create",
+			   "MU_STREAM_WRITE", rc);
+	  return 1;
+	}
+      mu_stream_set_buffer (ostream, mu_buffer_line, 0);
+      rc = mu_filter_create (&flt, ostream, "CRLF", MU_FILTER_ENCODE,
+			     MU_STREAM_WRITE);
+      mu_stream_unref (ostream);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_filter_create",
+			   "CRLF,MU_FILTER_ENCODE", rc);
+	  return 1;
+	}
+      mu_stream_set_buffer (flt, mu_buffer_line, 0);
+      ostream = flt;
+      rc = mu_iostream_create (&str, istream, ostream);
+      if (rc)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_iostream_create", NULL, rc);
+	  return 1;
 	}
       mu_stream_set_buffer (str, mu_buffer_line, 0);
       smtp (str);
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index f115a86..377131d 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -87,6 +87,8 @@ TESTSUITE_AT = \
  lstuid02.at\
  mbdel.at\
  mime.at\
+ smtp-msg.at\
+ smtp-str.at\
  ufms.at\
  testsuite.at
 
diff --git a/testsuite/smtp-msg.at b/testsuite/smtp-msg.at
new file mode 100644
index 0000000..5e1a115
--- /dev/null
+++ b/testsuite/smtp-msg.at
@@ -0,0 +1,61 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([smtp send from message])
+AT_KEYWORDS([smtp-msg])
+
+AT_DATA([msg],[dnl
+From: mailutils@localhost
+To: gray@example.org
+Subject: SMTP test
+
+Omnis enim res, quae dando non deficit,
+dum habetur et non datur, nondum habetur,
+quomodo habenda est.
+])
+
+AT_CHECK([
+MTA_DIAG=`pwd`/mta.diag
+export MTA_DIAG
+p=`$abs_top_builddir/examples/mta -bd`
+test $? -eq 0 || AT_SKIP_TEST
+set -- $p
+# $1 - pid, $2 - port
+smtpsend localhost port=$2 \
+         from=mailutils@mailutils.org\
+	 rcpt=gray@example.org\
+	 domain=mailutils.org\
+	 input=msg
+kill $1
+cat mta.diag
+],
+[0],
+[ENVELOPE FROM: <mailutils@mailutils.org>
+ENVELOPE TO: <gray@example.org>
+   0: From: mailutils@localhost
+   1: To: gray@example.org
+   2: Subject: SMTP test
+   3:
+   4: Omnis enim res, quae dando non deficit,
+   5: dum habetur et non datur, nondum habetur,
+   6: quomodo habenda est.
+   7:
+END OF MESSAGE
+])
+
+AT_CLEANUP
+
+	 
\ No newline at end of file
diff --git a/testsuite/smtp-str.at b/testsuite/smtp-str.at
new file mode 100644
index 0000000..4568e5f
--- /dev/null
+++ b/testsuite/smtp-str.at
@@ -0,0 +1,62 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2011 Free Software Foundation, Inc.
+#
+# GNU Mailutils is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3, or (at
+# your option) any later version.
+#
+# GNU Mailutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.
+
+AT_SETUP([smtp send from stream])
+AT_KEYWORDS([smtp-stream smtp-str])
+
+AT_DATA([msg],[dnl
+From: mailutils@localhost
+To: gray@example.org
+Subject: SMTP test
+
+Omnis enim res, quae dando non deficit,
+dum habetur et non datur, nondum habetur,
+quomodo habenda est.
+])
+
+AT_CHECK([
+MTA_DIAG=`pwd`/mta.diag
+export MTA_DIAG
+p=`$abs_top_builddir/examples/mta -bd`
+test $? -eq 0 || AT_SKIP_TEST
+set -- $p
+# $1 - pid, $2 - port
+smtpsend localhost port=$2 \
+         from=mailutils@mailutils.org\
+	 rcpt=gray@example.org\
+	 domain=mailutils.org\
+	 raw=1\
+	 input=msg
+kill $1
+cat mta.diag
+],
+[0],
+[ENVELOPE FROM: <mailutils@mailutils.org>
+ENVELOPE TO: <gray@example.org>
+   0: From: mailutils@localhost
+   1: To: gray@example.org
+   2: Subject: SMTP test
+   3:
+   4: Omnis enim res, quae dando non deficit,
+   5: dum habetur et non datur, nondum habetur,
+   6: quomodo habenda est.
+   7:
+END OF MESSAGE
+])
+
+AT_CLEANUP
+
+	 
\ No newline at end of file
diff --git a/testsuite/smtpsend.c b/testsuite/smtpsend.c
index 9dcc4e7..7e721ea 100644
--- a/testsuite/smtpsend.c
+++ b/testsuite/smtpsend.c
@@ -120,10 +120,16 @@ main (int argc, char **argv)
 	  if (mu_isdigit (arg[0]))
 	    mu_smtp_trace (smtp, atoi (argv[i] + 6) ?
 			   MU_SMTP_TRACE_SET : MU_SMTP_TRACE_CLR);
-	  else if (strcmp (arg, "secure") == 0)
-	    mu_smtp_trace_mask (smtp, MU_SMTP_TRACE_SET, MU_XSCRIPT_SECURE);
-	  else if (strcmp (arg, "payload") == 0)
-	    mu_smtp_trace_mask (smtp, MU_SMTP_TRACE_SET, MU_XSCRIPT_PAYLOAD);
+	  else
+	    {
+	      mu_smtp_trace (smtp, MU_SMTP_TRACE_SET);
+	      if (strcmp (arg, "secure") == 0)
+		mu_smtp_trace_mask (smtp, MU_SMTP_TRACE_SET,
+				    MU_XSCRIPT_SECURE);
+	      else if (strcmp (arg, "payload") == 0)
+		mu_smtp_trace_mask (smtp, MU_SMTP_TRACE_SET,
+				    MU_XSCRIPT_PAYLOAD);
+	    }
 	}
       else if (strncmp (argv[i], "tls=", 4) == 0)
 	tls = atoi (argv[i] + 4);
diff --git a/testsuite/testsuite.at b/testsuite/testsuite.at
index 87f80dc..476751d 100644
--- a/testsuite/testsuite.at
+++ b/testsuite/testsuite.at
@@ -32,5 +32,9 @@ m4_include([mbdel.at])
 AT_BANNER(Folder mailbox removal)
 m4_include([fldel.at])
 
+AT_BANNER(SMTP)
+m4_include([smtp-msg.at])
+m4_include([smtp-str.at])
+
 AT_BANNER(Various)
 m4_include([ufms.at])


hooks/post-receive
-- 
GNU Mailutils


From MAILER-DAEMON Sat Dec 31 09:57:22 2011
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Rh0N8-0005ST-2c
	for mharc-commit-mailutils@gnu.org; Sat, 31 Dec 2011 09:57:22 -0500
Received: from eggs.gnu.org ([140.186.70.92]:36566)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rh0N4-0005SN-1o
	for commit-mailutils@gnu.org; Sat, 31 Dec 2011 09:57:20 -0500
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rh0N1-000841-MW
	for commit-mailutils@gnu.org; Sat, 31 Dec 2011 09:57:17 -0500
Received: from vcs.savannah.gnu.org ([140.186.70.72]:35278)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rh0N1-00083w-CQ
	for commit-mailutils@gnu.org; Sat, 31 Dec 2011 09:57:15 -0500
Received: from gray by vcs.savannah.gnu.org with local (Exim 4.72)
	(envelope-from <INVALID.NOREPLY@gnu.org>) id 1Rh0Mz-00079a-ON
	for commit-mailutils@gnu.org; Sat, 31 Dec 2011 14:57:14 +0000
Date: Sat, 31 Dec 2011 14:57:13 +0000
Message-Id: <E1Rh0Mz-00079a-ON@vcs.savannah.gnu.org>
From: "Sergey Poznyakoff" <gray@gnu.org.ua>
To: commit-mailutils@gnu.org
Subject: [SCM] GNU Mailutils branch, master, updated. release-2.2-561-gf8a9693
X-Git-Refname: refs/heads/master
X-Git-Reftype: branch
X-Git-Oldrev: 356c07542f39f1ff159e2646bd97e1791f493aad
X-Git-Newrev: f8a96930b6ec84c63b36e5789c9fdf74285f420c
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2)
X-Received-From: 140.186.70.72
X-BeenThere: commit-mailutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Commit notices for GNU mailutils <commit-mailutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/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: <https://lists.gnu.org/mailman/listinfo/commit-mailutils>,
	<mailto:commit-mailutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Sat, 31 Dec 2011 14:57:20 -0000

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

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

The branch, master has been updated
       via  f8a96930b6ec84c63b36e5789c9fdf74285f420c (commit)
      from  356c07542f39f1ff159e2646bd97e1791f493aad (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 f8a96930b6ec84c63b36e5789c9fdf74285f420c
Author: Sergey Poznyakoff <gray@gnu.org.ua>
Date:   Sat Dec 31 16:48:38 2011 +0200

    Implement SMTPS.
    
    * NEWS: Update.
    * include/mailutils/registrar.h (mu_smtps_record): New extern.
    (mu_register_all_mailer_formats): Register mu_smtps_record.
    * include/mailutils/sys/registrar.h (MU_SMTPS_SCHEME)
    (MU_SMTPS_SCHEME_LEN,MU_SMTPS_PORT): New defines.
    * libmailutils/address/address.c (mu_address_get_count): Don't
    count parts with NULL emails.
    (mu_address_contains_email): Validate email before comparison.
    (mu_list_copy): New static.
    (mu_address_union): Reuse last subaddress if its email is NULL.
    * libmailutils/base/nullrec.c (mu_smtps_record): New placeholder.
    * libproto/mailer/smtp.c (mu_smtps_record): New global.
    (smtp_open): Implement SMTPS
    
    * mu/send.c: New file.
    * mu/Makefile.am (MODULES): Add send.c

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

Summary of changes:
 NEWS                              |    4 +-
 include/mailutils/registrar.h     |    4 +-
 include/mailutils/sys/registrar.h |    4 +
 libmailutils/address/address.c    |  115 +++++++++++++--------
 libmailutils/base/nullrec.c       |    4 +
 libproto/mailer/smtp.c            |   65 ++++++++++--
 mu/Makefile.am                    |    1 +
 mu/send.c                         |  202 +++++++++++++++++++++++++++++++++++++
 8 files changed, 344 insertions(+), 55 deletions(-)
 create mode 100644 mu/send.c

diff --git a/NEWS b/NEWS
index 4a55342..29ce7cc 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-GNU mailutils NEWS -- history of user-visible changes. 2011-12-23
+GNU mailutils NEWS -- history of user-visible changes. 2011-12-31
 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
 Free Software Foundation, Inc.
 See the end of file for copying conditions.
@@ -91,6 +91,8 @@ This feature is useful for those system administrators who don't wish
 to permit simultaneous access to mailboxes, even when the nature of the
 mailbox allows that (e.g. maildir).
 
+** SMTPS support.
+
 ** Sieve: new extensions
 
 New extension action `pipe' invokes arbitrary external program and
diff --git a/include/mailutils/registrar.h b/include/mailutils/registrar.h
index f0fbd0a..ae17974 100644
--- a/include/mailutils/registrar.h
+++ b/include/mailutils/registrar.h
@@ -122,8 +122,9 @@ extern mu_record_t mu_maildir_record;
 #define MU_SENDMAIL_PRIO    10000
 #define MU_PROG_PRIO        10000
   
-/* SMTP mailer, "smtp://"  */
+/* SMTP mailer, "smtp://" and "smtps://"  */
 extern mu_record_t mu_smtp_record;
+extern mu_record_t mu_smtps_record;
 /* Sendmail, "sendmail:"  */
 extern mu_record_t mu_sendmail_record;
 /* Program mailer, "prog://", "|" */
@@ -158,6 +159,7 @@ extern mu_record_t mu_prog_record;
 #define mu_register_all_mailer_formats() do {\
   mu_registrar_record (mu_sendmail_record);\
   mu_registrar_record (mu_smtp_record);\
+  mu_registrar_record (mu_smtps_record);\
   mu_registrar_record (mu_prog_record);\
 } while (0)
 
diff --git a/include/mailutils/sys/registrar.h b/include/mailutils/sys/registrar.h
index 5d3992b..c725b14 100644
--- a/include/mailutils/sys/registrar.h
+++ b/include/mailutils/sys/registrar.h
@@ -74,6 +74,10 @@ extern int _folder_path_init      (mu_folder_t);
 # define MU_SMTP_SCHEME_LEN (sizeof (MU_SMTP_SCHEME) - 1)
 # define MU_SMTP_PORT 25
 
+# define MU_SMTPS_SCHEME "smtps"
+# define MU_SMTPS_SCHEME_LEN (sizeof (MU_SMTPS_SCHEME) - 1)
+# define MU_SMTPS_PORT 465
+  
 # define MU_SENDMAIL_SCHEME "sendmail"
 # define MU_SENDMAIL_SCHEME_LEN (sizeof (MU_SENDMAIL_SCHEME) - 1)
 extern int _mu_mailer_sendmail_init (mu_mailer_t mailer);
diff --git a/libmailutils/address/address.c b/libmailutils/address/address.c
index 19c33fa..f77230a 100644
--- a/libmailutils/address/address.c
+++ b/libmailutils/address/address.c
@@ -151,6 +151,18 @@ mu_address_createv (mu_address_t *a, const char *sv[], size_t len)
   return status;
 }
 
+static void
+_address_free (mu_address_t address)
+{
+  free (address->printable);
+  free (address->comments);
+  free (address->personal);
+  free (address->email);
+  free (address->local_part);
+  free (address->domain);
+  free (address->route);
+}
+
 void
 mu_address_destroy (mu_address_t *paddress)
 {
@@ -160,21 +172,8 @@ mu_address_destroy (mu_address_t *paddress)
       mu_address_t current;
       for (; address; address = current)
 	{
-	  if (address->printable)
-	    free (address->printable);
-	  if (address->comments)
-	    free (address->comments);
-	  if (address->personal)
-	    free (address->personal);
-	  if (address->email)
-	    free (address->email);
-	  if (address->local_part)
-	    free (address->local_part);
-	  if (address->domain)
-	    free (address->domain);
-	  if (address->route)
-	    free (address->route);
 	  current = address->next;
+	  _address_free (address);
 	  free (address);
 	}
       *paddress = NULL;
@@ -642,11 +641,15 @@ mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t *n)
 int
 mu_address_get_count (mu_address_t addr, size_t *pcount)
 {
-  size_t j;
-  for (j = 0; addr; addr = addr->next, j++)
-    ;
+  size_t i, count = 0;
+  for (i = 0; addr; addr = addr->next, i++)
+    {
+      mu_validate_email (addr);
+      if (addr->email)
+	++count;
+    }
   if (pcount)
-    *pcount = j;
+    *pcount = count;
   return 0;
 }
 
@@ -696,37 +699,48 @@ int
 mu_address_contains_email (mu_address_t addr, const char *email)
 {
   for (; addr; addr = addr->next)
-    if (mu_c_strcasecmp (addr->email, email) == 0)
-      return 1;
+    {
+      mu_validate_email (addr);
+      if (!addr->email)
+	break;
+      if (mu_c_strcasecmp (addr->email, email) == 0)
+	return 1;
+    }
   return 0;
 }
 
-mu_address_t
-mu_address_dup (mu_address_t src)
+static int
+mu_list_copy (mu_address_t dst, mu_address_t src)
 {
-  mu_address_t dst = calloc (1, sizeof (*dst));
-
-  if (!dst)
-    return NULL;
-
   /* FIXME: How about:
     if (src->printable)
       dst->printable = strdup (src->printable);
     ?
   */
-  if (src->comments)
-    dst->comments = strdup (src->comments);
-  if (src->personal)
-    dst->personal = strdup (src->personal);
-  if (src->email)
-    dst->email = strdup (src->email);
-  if (src->local_part)
-    dst->local_part = strdup (src->local_part);
-  if (src->domain)
-    dst->domain = strdup (src->domain);
-  if (src->route)
-    dst->route = strdup (src->route);
+  if (src->comments && !(dst->comments = strdup (src->comments)))
+    return ENOMEM;
+  if (src->personal && !(dst->personal = strdup (src->personal)))
+    return ENOMEM;
+  if (src->email && !(dst->email = strdup (src->email)))
+    return ENOMEM;
+  if (src->local_part && !(dst->local_part = strdup (src->local_part)))
+    return ENOMEM;
+  if (src->domain && !(dst->domain = strdup (src->domain)))
+    return ENOMEM;
+  if (src->route && !(dst->route = strdup (src->route)))
+    return ENOMEM;
+  return 0;
+}  
 
+mu_address_t
+mu_address_dup (mu_address_t src)
+{
+  mu_address_t dst = calloc (1, sizeof (*dst));
+
+  if (!dst)
+    return NULL;
+  if (mu_list_copy (dst, src))
+    mu_address_destroy (&dst);
   return dst;
 }
 
@@ -760,11 +774,24 @@ mu_address_union (mu_address_t *a, mu_address_t b)
   for (; b; b = b->next)
     if (!mu_address_contains_email (*a, b->email))
       {
-	mu_address_t next = mu_address_dup (b);
-	if (!next)
-	  return ENOMEM;
-	last->next = next;
-	last = next;
+	if (last->email)
+	  {
+	    mu_address_t next = mu_address_dup (b);
+	    if (!next)
+	      return ENOMEM;
+	    last->next = next;
+	    last = next;
+	  }
+	else
+	  {
+	    int rc = mu_list_copy (last, b);
+	    if (rc)
+	      {
+		_address_free (last);
+		memset (last, 0, sizeof (last));
+		return rc;
+	      }
+	  }
       }
   return 0;
 }
diff --git a/libmailutils/base/nullrec.c b/libmailutils/base/nullrec.c
index a507a24..80aadbf 100644
--- a/libmailutils/base/nullrec.c
+++ b/libmailutils/base/nullrec.c
@@ -47,3 +47,7 @@ mu_record_t mu_mh_record = NULL;
 mu_record_t mu_maildir_record = NULL;
 #endif
 
+#ifndef WITH_TLS
+mu_record_t mu_smtps_record = NULL;
+#endif
+
diff --git a/libproto/mailer/smtp.c b/libproto/mailer/smtp.c
index 7177f3b..1c80428 100644
--- a/libproto/mailer/smtp.c
+++ b/libproto/mailer/smtp.c
@@ -86,6 +86,27 @@ static struct _mu_record _smtp_record = {
    the mailbox, via the register entry/record.  */
 mu_record_t     mu_smtp_record = &_smtp_record;
 
+#ifdef WITH_TLS
+static struct _mu_record _smtps_record = {
+  MU_SMTP_PRIO,
+  MU_SMTPS_SCHEME,
+  MU_RECORD_DEFAULT,
+  MU_URL_SCHEME | MU_URL_CRED | MU_URL_INET | MU_URL_PARAM,
+  MU_URL_HOST,
+  _url_smtp_init,		/* url init.  */
+  _mu_mailer_mailbox_init,	/* Mailbox init.  */
+  _mailer_smtp_init,		/* Mailer init.  */
+  _mu_mailer_folder_init,	/* Folder init.  */
+  NULL,				/* No need for a back pointer.  */
+  NULL,				/* _is_scheme method.  */
+  NULL,				/* _get_url method.  */
+  NULL,				/* _get_mailbox method.  */
+  NULL,				/* _get_mailer method.  */
+  NULL				/* _get_folder method.  */
+};
+mu_record_t     mu_smtps_record = &_smtps_record;
+#endif
+
 struct _smtp_mailer
 {
   mu_mailer_t     mailer;
@@ -116,14 +137,19 @@ smtp_mailer_add_auth_mech (struct _smtp_mailer *smtp_mailer, const char *str)
 static int
 smtp_open (mu_mailer_t mailer, int flags)
 {
-  const char *auth;
+  const char *auth, *scheme;
   struct _smtp_mailer *smtp_mailer = mailer->data;
   int rc;
   size_t parmc = 0;
   char **parmv = NULL;
-  int notls = 0;
+  int tls = 0;
+  int nostarttls = 0;
   int noauth = 0;
-  
+
+  rc = mu_url_sget_scheme (mailer->url, &scheme);
+  if (rc == 0) 
+    tls = strcmp (scheme, "smtps") == 0;
+
   rc = mu_smtp_create (&smtp_mailer->smtp);
   if (rc)
     return rc;
@@ -150,7 +176,7 @@ smtp_open (mu_mailer_t mailer, int flags)
       for (i = 0; i < parmc; i++)
 	{
 	  if (strcmp (parmv[i], "notls") == 0)
-	    notls = 1;
+	    nostarttls = 1;
 	  else if (strcmp (parmv[i], "noauth") == 0)
 	    noauth = 1;
 	  else if (strncmp (parmv[i], "auth=", 5) == 0)
@@ -163,23 +189,44 @@ smtp_open (mu_mailer_t mailer, int flags)
     {
       struct mu_sockaddr *sa;
       struct mu_sockaddr_hints hints;
-
+      mu_stream_t transport;
+      
       memset (&hints, 0, sizeof (hints));
       hints.flags = MU_AH_DETECT_FAMILY;
-      hints.port = 25;
+      hints.port = tls ? MU_SMTPS_PORT : MU_SMTP_PORT;
       hints.protocol = IPPROTO_TCP;
       hints.socktype = SOCK_STREAM;
       rc = mu_sockaddr_from_url (&sa, mailer->url, &hints);
       if (rc)
 	return rc;
       
-      rc = mu_tcp_stream_create_from_sa (&mailer->stream, sa, NULL,
-					 mailer->flags);
+      rc = mu_tcp_stream_create_from_sa (&transport, sa, NULL, mailer->flags);
       if (rc)
         {
           mu_sockaddr_free (sa);
 	  return rc;
 	}
+#ifdef WITH_TLS
+      if (tls && mu_tls_enable)
+	{
+	  mu_stream_t tlsstream;
+	  
+	  rc = mu_tls_client_stream_create (&tlsstream, transport, transport,
+					    0);
+	  mu_stream_unref (transport);
+	  if (rc)
+	    {
+	      mu_debug (MU_DEBCAT_MAILER, MU_DEBUG_ERROR,
+			(_("cannot create TLS stream: %s"),
+			 mu_strerror (rc)));
+	      mu_sockaddr_free (sa);
+	      return rc;
+	    }
+	  transport = tlsstream;
+	  nostarttls = 1;
+	}
+#endif
+      mailer->stream = transport;
       mu_stream_set_buffer (mailer->stream, mu_buffer_line, 0);
     }
   mu_smtp_set_carrier (smtp_mailer->smtp, mailer->stream);
@@ -194,7 +241,7 @@ smtp_open (mu_mailer_t mailer, int flags)
     return rc;
 
 #ifdef WITH_TLS
-  if (!notls && mu_tls_enable &&
+  if (!nostarttls && mu_tls_enable &&
       mu_smtp_capa_test (smtp_mailer->smtp, "STARTTLS", NULL) == 0)
     {
       rc = mu_smtp_starttls (smtp_mailer->smtp);
diff --git a/mu/Makefile.am b/mu/Makefile.am
index e28b984..05408fa 100644
--- a/mu/Makefile.am
+++ b/mu/Makefile.am
@@ -51,6 +51,7 @@ MODULES = \
  ldflags.c\
  logger.c\
  $(POP_C)\
+ send.c\
  query.c\
  wicket.c
 
diff --git a/mu/send.c b/mu/send.c
new file mode 100644
index 0000000..0d4402d
--- /dev/null
+++ b/mu/send.c
@@ -0,0 +1,202 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+   Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+
+   GNU Mailutils is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GNU Mailutils is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <mailutils/cctype.h>
+#include <mailutils/mailutils.h>
+#include <argp.h>
+#include "mu.h"
+
+static int read_recipients;
+static mu_address_t rcpt_addr;
+static mu_address_t from_addr;
+
+static void
+send_address_add (mu_address_t *paddr, const char *value)
+{
+  mu_address_t addr = NULL;
+  int rc;
+
+  rc = mu_address_create (&addr, value);
+  if (rc)
+    {
+      mu_error (_("%s: %s"), value, mu_strerror (rc));
+      exit (1);
+    }
+  MU_ASSERT (mu_address_union (paddr, addr));
+  mu_address_destroy (&addr);
+}
+
+static char send_doc[] = N_("mu send - send a message.");
+char send_docstring[] = N_("send a message");
+static char send_args_doc[] = "URL-or-HOST [FILE]";
+
+static struct argp_option send_options[] = {
+  { "from",  'F', N_("ADDRESS"), 0,
+    N_("send mail from this ADDRESS") },
+  { "rcpt",  'T', N_("ADDRESS"), 0,
+    N_("send mail to this ADDRESS") },
+  { "read-recipients", 't', NULL, 0,
+    N_("read recipients from the message") },
+  { NULL }
+};
+
+static error_t
+send_parse_opt (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+    case 'F':
+      MU_ASSERT (mu_address_create_null (&from_addr));
+      send_address_add (&from_addr, arg);
+      break;
+
+    case 'T':
+      send_address_add (&rcpt_addr, arg);
+      break;
+
+    case 't':
+      read_recipients = 1;
+      break;
+
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp send_argp = {
+  send_options,
+  send_parse_opt,
+  send_args_doc,
+  send_doc,
+  NULL,
+  NULL,
+  NULL
+};
+
+int
+mutool_send (int argc, char **argv)
+{
+  int index;
+  char *infile;
+  mu_stream_t instr;
+  mu_message_t msg;
+  size_t count;
+  mu_url_t urlhint, url;
+  mu_mailer_t mailer;
+  
+  MU_ASSERT (mu_address_create_null (&rcpt_addr));
+  mu_register_all_mailer_formats ();
+  
+  if (argp_parse (&send_argp, argc, argv, 0, &index, NULL))
+    return 1;
+
+  argc -= index;
+  argv += index;
+
+  if (argc < 1)
+    {
+      mu_error (_("not enough arguments"));
+      return 1;
+    }
+
+  infile = argv[1];
+  if (infile)
+    MU_ASSERT (mu_file_stream_create (&instr, infile,
+				      MU_STREAM_READ|MU_STREAM_SEEK));
+  else
+    MU_ASSERT (mu_stdio_stream_create (&instr, MU_STDIN_FD,
+				       MU_STREAM_READ|MU_STREAM_SEEK));
+
+  MU_ASSERT (mu_stream_to_message (instr, &msg));
+  mu_stream_unref (instr);
+
+  mu_address_get_count (rcpt_addr, &count);
+  if (count == 0)
+    read_recipients = 1;
+
+  if (read_recipients)
+    {
+      int rc;
+      mu_header_t header;
+      const char *value;
+
+      MU_ASSERT (mu_message_get_header (msg, &header));
+	  
+      rc = mu_header_sget_value (header, MU_HEADER_TO, &value);
+      if (rc == 0)
+	send_address_add (&rcpt_addr, value);
+      else if (rc != MU_ERR_NOENT)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_header_sget_value",
+			   MU_HEADER_TO, rc);
+	  exit (1);
+	}
+      
+      rc = mu_header_sget_value (header, MU_HEADER_CC, &value);
+      if (rc == 0)
+	send_address_add (&rcpt_addr, value);
+      else if (rc != MU_ERR_NOENT)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_header_sget_value",
+			   MU_HEADER_CC, rc);
+	  exit (1);
+	}
+
+      rc = mu_header_sget_value (header, MU_HEADER_BCC, &value);
+      if (rc == 0)
+	send_address_add (&rcpt_addr, value);
+      else if (rc != MU_ERR_NOENT)
+	{
+	  mu_diag_funcall (MU_DIAG_ERROR, "mu_header_sget_value",
+			   MU_HEADER_BCC, rc);
+	  exit (1);
+	}
+    }
+
+  mu_address_get_count (rcpt_addr, &count);
+  if (count == 0)
+    {
+      mu_error (_("no recipients"));
+      exit (1);
+    }
+  
+  MU_ASSERT (mu_url_create (&urlhint, "smtp://"));
+  MU_ASSERT (mu_url_create_hint (&url, argv[0], MU_URL_PARSE_DEFAULT,
+				 urlhint));
+  mu_url_invalidate (url);
+  MU_ASSERT (mu_mailer_create_from_url (&mailer, url));
+  MU_ASSERT (mu_mailer_open (mailer, MU_STREAM_RDWR));
+  MU_ASSERT (mu_mailer_send_message (mailer, msg, from_addr, rcpt_addr));
+  mu_mailer_close (mailer);
+  mu_mailer_destroy (&mailer);
+  return 0;
+}
+
+/*
+  MU Setup: send
+  mu-handler: mutool_send
+  mu-docstring: send_docstring
+  End MU Setup:
+*/


hooks/post-receive
-- 
GNU Mailutils


