bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen#include "lib.h"
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen#include "imap-arg.h"
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_atom(const struct imap_arg *arg, const char **str_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
333b1f356e437f17d843fcbe529d6ac5915a64f5Timo Sirainen if (arg->type != IMAP_ARG_ATOM && arg->type != IMAP_ARG_NIL)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *str_r = arg->_data.str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_quoted(const struct imap_arg *arg, const char **str_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (arg->type != IMAP_ARG_STRING)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *str_r = arg->_data.str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_string(const struct imap_arg *arg, const char **str_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (arg->type != IMAP_ARG_STRING && arg->type != IMAP_ARG_LITERAL)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *str_r = arg->_data.str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_astring(const struct imap_arg *arg, const char **str_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
1dc33c503c3de306db4b7cba4e51ea2077b2805dTimo Sirainen /* RFC 3501 4.5. specifies that NIL is the same as "NIL" when
1dc33c503c3de306db4b7cba4e51ea2077b2805dTimo Sirainen reading astring. */
1dc33c503c3de306db4b7cba4e51ea2077b2805dTimo Sirainen if (!IMAP_ARG_IS_ASTRING(arg) && arg->type != IMAP_ARG_NIL)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *str_r = arg->_data.str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_nstring(const struct imap_arg *arg, const char **str_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (arg->type == IMAP_ARG_NIL) {
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *str_r = NULL;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen }
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return imap_arg_get_astring(arg, str_r);
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_literal_size(const struct imap_arg *arg, uoff_t *size_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (arg->type != IMAP_ARG_LITERAL_SIZE &&
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen arg->type != IMAP_ARG_LITERAL_SIZE_NONSYNC)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *size_r = arg->_data.literal_size;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_list(const struct imap_arg *arg,
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const struct imap_arg **list_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen unsigned int count;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return imap_arg_get_list_full(arg, list_r, &count);
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_get_list_full(const struct imap_arg *arg,
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const struct imap_arg **list_r,
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen unsigned int *list_count_r)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen unsigned int count;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (arg->type != IMAP_ARG_LIST)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *list_r = array_get(&arg->_data.list, &count);
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen /* drop IMAP_ARG_EOL from list size */
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen i_assert(count > 0);
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen *list_count_r = count - 1;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return TRUE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenconst char *imap_arg_as_astring(const struct imap_arg *arg)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const char *str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_astring(arg, &str))
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen i_unreached();
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenconst char *imap_arg_as_nstring(const struct imap_arg *arg)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const char *str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_nstring(arg, &str))
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen i_unreached();
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return str;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenuoff_t imap_arg_as_literal_size(const struct imap_arg *arg)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen uoff_t size;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_literal_size(arg, &size))
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen i_unreached();
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return size;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenconst struct imap_arg *
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenimap_arg_as_list(const struct imap_arg *arg)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const struct imap_arg *ret;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_list(arg, &ret))
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen i_unreached();
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return ret;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenbool imap_arg_atom_equals(const struct imap_arg *arg, const char *str)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen const char *value;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (!imap_arg_get_atom(arg, &value))
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return FALSE;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen return strcasecmp(value, str) == 0;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen}