diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-12-26 14:06:57 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-12-26 20:34:13 +0200 |
commit | e072a2734eaf1bffdacfc20fcb598d79a50d11e7 (patch) | |
tree | 62b91a5c1e240aee855bc87431ad75b73ca32367 /libmailutils | |
parent | d16a3de50d8c43f7b45f18db1704fa784ee4cb7b (diff) | |
download | mailutils-e072a2734eaf1bffdacfc20fcb598d79a50d11e7.tar.gz mailutils-e072a2734eaf1bffdacfc20fcb598d79a50d11e7.tar.bz2 |
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.
Diffstat (limited to 'libmailutils')
-rw-r--r-- | libmailutils/Makefile.am | 3 | ||||
-rw-r--r-- | libmailutils/msgset/Makefile.am | 34 | ||||
-rw-r--r-- | libmailutils/msgset/add.c | 43 | ||||
-rw-r--r-- | libmailutils/msgset/aggr.c | 169 | ||||
-rw-r--r-- | libmailutils/msgset/clear.c | 32 | ||||
-rw-r--r-- | libmailutils/msgset/create.c | 60 | ||||
-rw-r--r-- | libmailutils/msgset/free.c | 42 | ||||
-rw-r--r-- | libmailutils/msgset/getitr.c | 30 | ||||
-rw-r--r-- | libmailutils/msgset/getlist.c | 33 | ||||
-rw-r--r-- | libmailutils/msgset/locate.c | 32 | ||||
-rw-r--r-- | libmailutils/msgset/parse.c | 179 | ||||
-rw-r--r-- | libmailutils/msgset/print.c | 74 | ||||
-rw-r--r-- | libmailutils/msgset/sub.c | 233 | ||||
-rw-r--r-- | libmailutils/tests/Makefile.am | 2 | ||||
-rw-r--r-- | libmailutils/tests/msgset.at | 175 | ||||
-rw-r--r-- | libmailutils/tests/msgset.c | 125 | ||||
-rw-r--r-- | libmailutils/tests/testsuite.at | 2 |
17 files changed, 1267 insertions, 1 deletions
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am index c01b32176..bbc844768 100644 --- a/libmailutils/Makefile.am +++ b/libmailutils/Makefile.am @@ -20,3 +20,3 @@ 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 @@ -43,2 +43,3 @@ libmailutils_la_LIBADD = \ mime/libmime.la\ + msgset/libmsgset.la\ property/libproperty.la\ diff --git a/libmailutils/msgset/Makefile.am b/libmailutils/msgset/Makefile.am new file mode 100644 index 000000000..398806d49 --- /dev/null +++ b/libmailutils/msgset/Makefile.am @@ -0,0 +1,34 @@ +# 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/>. + +noinst_LTLIBRARIES = libmsgset.la + +libmsgset_la_SOURCES = \ + add.c\ + aggr.c\ + clear.c\ + create.c\ + getitr.c\ + getlist.c\ + free.c\ + locate.c\ + parse.c\ + print.c\ + sub.c + +INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils + diff --git a/libmailutils/msgset/add.c b/libmailutils/msgset/add.c new file mode 100644 index 000000000..9d9770735 --- /dev/null +++ b/libmailutils/msgset/add.c @@ -0,0 +1,43 @@ +/* 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> + +int +mu_msgset_add_range (mu_msgset_t mset, size_t beg, size_t end) +{ + int rc; + 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) + 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 000000000..a6fe7aa19 --- /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/msgset/clear.c b/libmailutils/msgset/clear.c new file mode 100644 index 000000000..f3f7ef874 --- /dev/null +++ b/libmailutils/msgset/clear.c @@ -0,0 +1,32 @@ +/* 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 <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) +{ + 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 000000000..0f70ab265 --- /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/msgset/free.c b/libmailutils/msgset/free.c new file mode 100644 index 000000000..77a348e14 --- /dev/null +++ b/libmailutils/msgset/free.c @@ -0,0 +1,42 @@ +/* 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/list.h> +#include <mailutils/msgset.h> +#include <mailutils/sys/msgset.h> + +void +mu_msgset_free (mu_msgset_t mset) +{ + 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/msgset/getitr.c b/libmailutils/msgset/getitr.c new file mode 100644 index 000000000..75cfb3df0 --- /dev/null +++ b/libmailutils/msgset/getitr.c @@ -0,0 +1,30 @@ +/* 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 <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/list.h> +#include <mailutils/msgset.h> +#include <mailutils/sys/msgset.h> + +int +mu_msgset_get_iterator (mu_msgset_t msgset, mu_iterator_t *pitr) +{ + if (!msgset) + return EINVAL; + return mu_list_get_iterator (msgset->list, pitr); +} diff --git a/libmailutils/msgset/getlist.c b/libmailutils/msgset/getlist.c new file mode 100644 index 000000000..5f7370dc1 --- /dev/null +++ b/libmailutils/msgset/getlist.c @@ -0,0 +1,33 @@ +/* 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 <mailutils/types.h> +#include <mailutils/errno.h> +#include <mailutils/list.h> +#include <mailutils/msgset.h> +#include <mailutils/sys/msgset.h> + +int +mu_msgset_get_list (mu_msgset_t msgset, mu_list_t *plist) +{ + if (!msgset) + return EINVAL; + if (!plist) + return MU_ERR_OUT_PTR_NULL; + *plist = msgset->list; + return 0; +} diff --git a/libmailutils/msgset/locate.c b/libmailutils/msgset/locate.c new file mode 100644 index 000000000..7b94ece89 --- /dev/null +++ b/libmailutils/msgset/locate.c @@ -0,0 +1,32 @@ +/* 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> + +int +mu_msgset_locate (mu_msgset_t msgset, size_t n, + struct mu_msgrange const **prange) +{ + 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 000000000..6e139fc53 --- /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 +{ + 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, 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 = 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 = env.s; + return rc; +} diff --git a/libmailutils/msgset/print.c b/libmailutils/msgset/print.c new file mode 100644 index 000000000..e7f990e5b --- /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 000000000..2840781dc --- /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); |