Commit 74b2971a 74b2971a50804a51f83bea8c49648fe9d2b344c4 by Sergey Poznyakoff

Gracefully handle empty mailboxes in mu_msgset_parse_imap

* libmailutils/msgset/parse.c (MU_MSGSET_EMPTY): New constant.
(parse_msgrange): Don't add any ranges if mode is set to
MU_MSGSET_EMPTY.
(mu_msgset_parse_imap): Force MU_MSGSET_EMPTY mode if the mailbox
is empty.
* libmailutils/msgset/print.c (mu_msgset_print): Handle empty message
sets.
* libmailutils/msgset/trans.c (_mu_msgset_translate_pair): Initialize
variable.
* libmailutils/tests/Makefile.am (msgset_LDADD): New variable.
* libmailutils/tests/msgset.c (parse_msgset): Take mailbox as first
argument.
(main): New option -mailbox
1 parent 561ce8ff
......@@ -26,6 +26,9 @@
#include <mailutils/msgset.h>
#include <mailutils/sys/msgset.h>
/* Special translation mode, indicating that the mailbox is empty */
#define MU_MSGSET_EMPTY -1
/* This structure keeps parser state while parsing message set. */
struct parse_msgnum_env
{
......@@ -33,7 +36,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) */
int mode; /* Operation mode (num/uid/dry_run) */
};
/* Get a single message number/UID from env->s and store it into *PN.
......@@ -96,6 +99,8 @@ parse_msgrange (struct parse_msgnum_env *env)
msgrange.msg_beg = tmp;
}
if (env->mode == MU_MSGSET_EMPTY)
return 0;
return mu_msgset_add_range (env->msgset, msgrange.msg_beg, msgrange.msg_end,
env->mode);
}
......@@ -131,7 +136,11 @@ mu_msgset_parse_imap (mu_msgset_t mset, int mode, const char *s, char **end)
rc = mu_mailbox_messages_count (mset->mbox, &lastmsgno);
if (rc == 0)
{
if (mode == MU_MSGSET_UID)
if (lastmsgno == 0)
{
env.mode = MU_MSGSET_EMPTY;
}
else if (mode == MU_MSGSET_UID)
{
rc = mu_mailbox_translate (mset->mbox, MU_MAILBOX_MSGNO_TO_UID,
lastmsgno, &env.maxval);
......
......@@ -64,7 +64,7 @@ mu_msgset_print (mu_stream_t str, mu_msgset_t mset)
int rc;
if (mu_list_is_empty (mset->list))
return MU_ERR_NOENT;
return mu_stream_printf (str, "%s", "nil");
rc = mu_msgset_aggregate (mset);
if (rc)
return rc;
......
......@@ -30,7 +30,7 @@ _mu_msgset_translate_pair (mu_msgset_t mset, int mode,
if (mode != _MU_MSGSET_MODE (mset->flags) && mset->mbox)
{
int cmd, rc;
size_t n;
size_t n = 1;
size_t beg = *pbeg;
size_t end = *pend;
......
......@@ -69,6 +69,12 @@ noinst_PROGRAMS = \
LDADD = ${MU_LIB_MAILUTILS}
msgset_LDADD = \
${MU_LIB_MBOX}\
${MU_LIB_MH}\
${MU_LIB_MAILDIR}\
${MU_LIB_MAILUTILS}
EXTRA_DIST += Encode Decode Wicketfile
## ------------ ##
......
......@@ -53,16 +53,16 @@ parse_msgrange (char *arg, struct mu_msgrange *range)
}
mu_msgset_t
parse_msgset (const char *arg)
parse_msgset (mu_mailbox_t mbox, const char *arg)
{
int rc;
mu_msgset_t msgset;
char *end;
MU_ASSERT (mu_msgset_create (&msgset, NULL, MU_MSGSET_NUM));
MU_ASSERT (mu_msgset_create (&msgset, mbox, MU_MSGSET_NUM));
if (arg)
{
rc = mu_msgset_parse_imap (msgset, MU_MSGSET_NUM, arg, &end);
rc = mu_msgset_parse_imap (msgset, MU_MSGSET_UID, arg, &end);
if (rc)
{
mu_error ("mu_msgset_parse_imap: %s near %s",
......@@ -79,26 +79,33 @@ main (int argc, char **argv)
int i;
char *msgset_string = NULL;
mu_msgset_t msgset;
mu_mailbox_t mbox = NULL;
mu_set_program_name (argv[0]);
mu_register_local_mbox_formats ();
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]] "
mu_printf ("usage: %s [-mailbox=PATH] [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] "
"[-addset=SET] [-delset=SET] ...\n",
mu_program_name);
return 0;
}
else if (strncmp (arg, "-msgset=", 8) == 0)
msgset_string = arg + 8;
else if (strncmp (arg, "-mailbox=", 9) == 0)
{
MU_ASSERT (mu_mailbox_create_default (&mbox, arg + 9));
MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_READ));
}
else
break;
}
msgset = parse_msgset (msgset_string);
msgset = parse_msgset (mbox, msgset_string);
for (; i < argc; i++)
{
......@@ -119,7 +126,7 @@ main (int argc, char **argv)
}
else if (strncmp (arg, "-addset=", 8) == 0)
{
mu_msgset_t tset = parse_msgset (arg + 8);
mu_msgset_t tset = parse_msgset (mbox, arg + 8);
if (!msgset)
msgset = tset;
else
......@@ -130,7 +137,7 @@ main (int argc, char **argv)
}
else if (strncmp (arg, "-subset=", 8) == 0)
{
mu_msgset_t tset = parse_msgset (arg + 8);
mu_msgset_t tset = parse_msgset (mbox, arg + 8);
if (!msgset)
{
mu_error ("no initial message set");
......