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 | |
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.
27 files changed, 1510 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac index 9472fb732..8f1cbebe7 100644 --- a/configure.ac +++ b/configure.ac @@ -1481,2 +1481,3 @@ AC_CONFIG_FILES([ libmailutils/mime/Makefile + libmailutils/msgset/Makefile libmailutils/property/Makefile diff --git a/include/mailutils/Makefile.am b/include/mailutils/Makefile.am index 53ea5e44d..d8e46ac7d 100644 --- a/include/mailutils/Makefile.am +++ b/include/mailutils/Makefile.am @@ -75,2 +75,3 @@ pkginclude_HEADERS = \ monitor.h\ + msgset.h\ mu_auth.h\ diff --git a/include/mailutils/datetime.h b/include/mailutils/datetime.h index bc1aa989d..dbf144790 100644 --- a/include/mailutils/datetime.h +++ b/include/mailutils/datetime.h @@ -20,2 +20,6 @@ +#ifdef __cplusplus +extern "C" { +#endif + #include <time.h> @@ -85,2 +89,6 @@ int mu_scan_datetime (const char *input, const char *fmt, struct tm *tm, +#ifdef __cplusplus +} +#endif + #endif diff --git a/include/mailutils/mailutils.h b/include/mailutils/mailutils.h index eb2a61627..b5621365d 100644 --- a/include/mailutils/mailutils.h +++ b/include/mailutils/mailutils.h @@ -72,2 +72,3 @@ #include <mailutils/cidr.h> +#include <mailutils/msgset.h> diff --git a/include/mailutils/msgset.h b/include/mailutils/msgset.h new file mode 100644 index 000000000..901ea79c6 --- /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, 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 86412d0f3..27e2fe4b6 100644 --- a/include/mailutils/sys/Makefile.am +++ b/include/mailutils/sys/Makefile.am @@ -46,2 +46,3 @@ sysinclude_HEADERS = \ monitor.h\ + msgset.h\ nntp.h\ diff --git a/include/mailutils/sys/msgset.h b/include/mailutils/sys/msgset.h new file mode 100644 index 000000000..9ecb053a0 --- /dev/null +++ b/include/mailutils/sys/msgset.h @@ -0,0 +1,31 @@ +/* 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_SYS_MSGSET_H +# define _MAILUTILS_SYS_MSGSET_H + +#define _MU_MSGSET_AGGREGATED 0x10 +#define _MU_MSGSET_USERFLAG_MASK 0x0f + +struct _mu_msgset +{ + 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 b98d617fb..bda2c375f 100644 --- a/include/mailutils/types.hin +++ b/include/mailutils/types.hin @@ -79,3 +79,4 @@ struct _mu_dbm_file; struct _mu_imapio; - +struct _mu_msgset; + struct mu_sockaddr; /* defined in mailutils/sockaddr.h */ @@ -127,2 +128,3 @@ typedef struct _mu_dbm_file *mu_dbm_file_t; typedef struct _mu_imapio *mu_imapio_t; +typedef struct _mu_msgset *mu_msgset_t; 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->maxva |