summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-managesieve
diff options
context:
space:
mode:
Diffstat (limited to 'pigeonhole/src/lib-managesieve')
-rw-r--r--pigeonhole/src/lib-managesieve/Makefile.am15
-rw-r--r--pigeonhole/src/lib-managesieve/Makefile.in691
-rw-r--r--pigeonhole/src/lib-managesieve/managesieve-arg.c207
-rw-r--r--pigeonhole/src/lib-managesieve/managesieve-arg.h117
-rw-r--r--pigeonhole/src/lib-managesieve/managesieve-parser.c757
-rw-r--r--pigeonhole/src/lib-managesieve/managesieve-parser.h67
-rw-r--r--pigeonhole/src/lib-managesieve/managesieve-quote.c121
-rw-r--r--pigeonhole/src/lib-managesieve/managesieve-quote.h17
8 files changed, 1992 insertions, 0 deletions
diff --git a/pigeonhole/src/lib-managesieve/Makefile.am b/pigeonhole/src/lib-managesieve/Makefile.am
new file mode 100644
index 0000000..b036680
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/Makefile.am
@@ -0,0 +1,15 @@
+noinst_LTLIBRARIES = libmanagesieve.la
+
+AM_CPPFLAGS = \
+ $(LIBDOVECOT_INCLUDE) \
+ -I$(top_srcdir)
+
+libmanagesieve_la_SOURCES = \
+ managesieve-arg.c \
+ managesieve-quote.c \
+ managesieve-parser.c
+
+noinst_HEADERS = \
+ managesieve-arg.h \
+ managesieve-quote.h \
+ managesieve-parser.h
diff --git a/pigeonhole/src/lib-managesieve/Makefile.in b/pigeonhole/src/lib-managesieve/Makefile.in
new file mode 100644
index 0000000..7186d55
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/Makefile.in
@@ -0,0 +1,691 @@
+# Makefile.in generated by automake 1.16.1 from Makefile.am.
+# @configure_input@
+
+# Copyright (C) 1994-2018 Free Software Foundation, Inc.
+
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+@SET_MAKE@
+
+
+VPATH = @srcdir@
+am__is_gnu_make = { \
+ if test -z '$(MAKELEVEL)'; then \
+ false; \
+ elif test -n '$(MAKE_HOST)'; then \
+ true; \
+ elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \
+ true; \
+ else \
+ false; \
+ fi; \
+}
+am__make_running_with_option = \
+ case $${target_option-} in \
+ ?) ;; \
+ *) echo "am__make_running_with_option: internal error: invalid" \
+ "target option '$${target_option-}' specified" >&2; \
+ exit 1;; \
+ esac; \
+ has_opt=no; \
+ sane_makeflags=$$MAKEFLAGS; \
+ if $(am__is_gnu_make); then \
+ sane_makeflags=$$MFLAGS; \
+ else \
+ case $$MAKEFLAGS in \
+ *\\[\ \ ]*) \
+ bs=\\; \
+ sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \
+ | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \
+ esac; \
+ fi; \
+ skip_next=no; \
+ strip_trailopt () \
+ { \
+ flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \
+ }; \
+ for flg in $$sane_makeflags; do \
+ test $$skip_next = yes && { skip_next=no; continue; }; \
+ case $$flg in \
+ *=*|--*) continue;; \
+ -*I) strip_trailopt 'I'; skip_next=yes;; \
+ -*I?*) strip_trailopt 'I';; \
+ -*O) strip_trailopt 'O'; skip_next=yes;; \
+ -*O?*) strip_trailopt 'O';; \
+ -*l) strip_trailopt 'l'; skip_next=yes;; \
+ -*l?*) strip_trailopt 'l';; \
+ -[dEDm]) skip_next=yes;; \
+ -[JT]) skip_next=yes;; \
+ esac; \
+ case $$flg in \
+ *$$target_option*) has_opt=yes; break;; \
+ esac; \
+ done; \
+ test $$has_opt = yes
+am__make_dryrun = (target_option=n; $(am__make_running_with_option))
+am__make_keepgoing = (target_option=k; $(am__make_running_with_option))
+pkgdatadir = $(datadir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkglibexecdir = $(libexecdir)/@PACKAGE@
+am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+install_sh_DATA = $(install_sh) -c -m 644
+install_sh_PROGRAM = $(install_sh) -c
+install_sh_SCRIPT = $(install_sh) -c
+INSTALL_HEADER = $(INSTALL_DATA)
+transform = $(program_transform_name)
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+build_triplet = @build@
+host_triplet = @host@
+subdir = src/lib-managesieve
+ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
+am__aclocal_m4_deps = $(top_srcdir)/m4/dovecot.m4 \
+ $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \
+ $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \
+ $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/configure.ac
+am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
+ $(ACLOCAL_M4)
+DIST_COMMON = $(srcdir)/Makefile.am $(noinst_HEADERS) \
+ $(am__DIST_COMMON)
+mkinstalldirs = $(install_sh) -d
+CONFIG_HEADER = $(top_builddir)/dummy-config.h \
+ $(top_builddir)/pigeonhole-config.h
+CONFIG_CLEAN_FILES =
+CONFIG_CLEAN_VPATH_FILES =
+LTLIBRARIES = $(noinst_LTLIBRARIES)
+libmanagesieve_la_LIBADD =
+am_libmanagesieve_la_OBJECTS = managesieve-arg.lo managesieve-quote.lo \
+ managesieve-parser.lo
+libmanagesieve_la_OBJECTS = $(am_libmanagesieve_la_OBJECTS)
+AM_V_lt = $(am__v_lt_@AM_V@)
+am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
+am__v_lt_0 = --silent
+am__v_lt_1 =
+AM_V_P = $(am__v_P_@AM_V@)
+am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
+am__v_P_0 = false
+am__v_P_1 = :
+AM_V_GEN = $(am__v_GEN_@AM_V@)
+am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@)
+am__v_GEN_0 = @echo " GEN " $@;
+am__v_GEN_1 =
+AM_V_at = $(am__v_at_@AM_V@)
+am__v_at_ = $(am__v_at_@AM_DEFAULT_V@)
+am__v_at_0 = @
+am__v_at_1 =
+DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
+depcomp = $(SHELL) $(top_srcdir)/depcomp
+am__maybe_remake_depfiles = depfiles
+am__depfiles_remade = ./$(DEPDIR)/managesieve-arg.Plo \
+ ./$(DEPDIR)/managesieve-parser.Plo \
+ ./$(DEPDIR)/managesieve-quote.Plo
+am__mv = mv -f
+COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \
+ $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \
+ $(AM_CFLAGS) $(CFLAGS)
+AM_V_CC = $(am__v_CC_@AM_V@)
+am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@)
+am__v_CC_0 = @echo " CC " $@;
+am__v_CC_1 =
+CCLD = $(CC)
+LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
+ $(AM_LDFLAGS) $(LDFLAGS) -o $@
+AM_V_CCLD = $(am__v_CCLD_@AM_V@)
+am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
+am__v_CCLD_0 = @echo " CCLD " $@;
+am__v_CCLD_1 =
+SOURCES = $(libmanagesieve_la_SOURCES)
+DIST_SOURCES = $(libmanagesieve_la_SOURCES)
+am__can_run_installinfo = \
+ case $$AM_UPDATE_INFO_DIR in \
+ n|no|NO) false;; \
+ *) (install-info --version) >/dev/null 2>&1;; \
+ esac
+HEADERS = $(noinst_HEADERS)
+am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP)
+# Read a list of newline-separated strings from the standard input,
+# and print each of them once, without duplicates. Input order is
+# *not* preserved.
+am__uniquify_input = $(AWK) '\
+ BEGIN { nonempty = 0; } \
+ { items[$$0] = 1; nonempty = 1; } \
+ END { if (nonempty) { for (i in items) print i; }; } \
+'
+# Make sure the list of sources is unique. This is necessary because,
+# e.g., the same source file might be shared among _SOURCES variables
+# for different programs/libraries.
+am__define_uniq_tagged_files = \
+ list='$(am__tagged_files)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | $(am__uniquify_input)`
+ETAGS = etags
+CTAGS = ctags
+am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp
+DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
+ACLOCAL = @ACLOCAL@
+AMTAR = @AMTAR@
+AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@
+AR = @AR@
+AUTOCONF = @AUTOCONF@
+AUTOHEADER = @AUTOHEADER@
+AUTOMAKE = @AUTOMAKE@
+AWK = @AWK@
+BINARY_CFLAGS = @BINARY_CFLAGS@
+BINARY_LDFLAGS = @BINARY_LDFLAGS@
+CC = @CC@
+CCDEPMODE = @CCDEPMODE@
+CFLAGS = @CFLAGS@
+CPP = @CPP@
+CPPFLAGS = @CPPFLAGS@
+CYGPATH_W = @CYGPATH_W@
+DEFS = @DEFS@
+DEPDIR = @DEPDIR@
+DISTCHECK_CONFIGURE_FLAGS = @DISTCHECK_CONFIGURE_FLAGS@
+DLLTOOL = @DLLTOOL@
+DOVECOT_BINARY_CFLAGS = @DOVECOT_BINARY_CFLAGS@
+DOVECOT_BINARY_LDFLAGS = @DOVECOT_BINARY_LDFLAGS@
+DOVECOT_CFLAGS = @DOVECOT_CFLAGS@
+DOVECOT_COMPRESS_LIBS = @DOVECOT_COMPRESS_LIBS@
+DOVECOT_INSTALLED = @DOVECOT_INSTALLED@
+DOVECOT_LIBS = @DOVECOT_LIBS@
+DOVECOT_LUA_CFLAGS = @DOVECOT_LUA_CFLAGS@
+DOVECOT_LUA_LIBS = @DOVECOT_LUA_LIBS@
+DOVECOT_SQL_LIBS = @DOVECOT_SQL_LIBS@
+DOVECOT_SSL_LIBS = @DOVECOT_SSL_LIBS@
+DSYMUTIL = @DSYMUTIL@
+DUMPBIN = @DUMPBIN@
+ECHO_C = @ECHO_C@
+ECHO_N = @ECHO_N@
+ECHO_T = @ECHO_T@
+EGREP = @EGREP@
+EXEEXT = @EXEEXT@
+FGREP = @FGREP@
+GREP = @GREP@
+INSTALL = @INSTALL@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
+LD = @LD@
+LDAP_LIBS = @LDAP_LIBS@
+LDFLAGS = @LDFLAGS@
+LIBDOVECOT = @LIBDOVECOT@
+LIBDOVECOT_ACL_INCLUDE = @LIBDOVECOT_ACL_INCLUDE@
+LIBDOVECOT_AUTH_INCLUDE = @LIBDOVECOT_AUTH_INCLUDE@
+LIBDOVECOT_COMPRESS = @LIBDOVECOT_COMPRESS@
+LIBDOVECOT_COMPRESS_DEPS = @LIBDOVECOT_COMPRESS_DEPS@
+LIBDOVECOT_CONFIG_INCLUDE = @LIBDOVECOT_CONFIG_INCLUDE@
+LIBDOVECOT_DEPS = @LIBDOVECOT_DEPS@
+LIBDOVECOT_DOVEADM_INCLUDE = @LIBDOVECOT_DOVEADM_INCLUDE@
+LIBDOVECOT_DSYNC = @LIBDOVECOT_DSYNC@
+LIBDOVECOT_DSYNC_DEPS = @LIBDOVECOT_DSYNC_DEPS@
+LIBDOVECOT_DSYNC_INCLUDE = @LIBDOVECOT_DSYNC_INCLUDE@
+LIBDOVECOT_FTS_INCLUDE = @LIBDOVECOT_FTS_INCLUDE@
+LIBDOVECOT_IMAPC_INCLUDE = @LIBDOVECOT_IMAPC_INCLUDE@
+LIBDOVECOT_IMAP_INCLUDE = @LIBDOVECOT_IMAP_INCLUDE@
+LIBDOVECOT_IMAP_LOGIN_INCLUDE = @LIBDOVECOT_IMAP_LOGIN_INCLUDE@
+LIBDOVECOT_INCLUDE = @LIBDOVECOT_INCLUDE@
+LIBDOVECOT_LDA = @LIBDOVECOT_LDA@
+LIBDOVECOT_LDA_DEPS = @LIBDOVECOT_LDA_DEPS@
+LIBDOVECOT_LDA_INCLUDE = @LIBDOVECOT_LDA_INCLUDE@
+LIBDOVECOT_LIBFTS = @LIBDOVECOT_LIBFTS@
+LIBDOVECOT_LIBFTS_DEPS = @LIBDOVECOT_LIBFTS_DEPS@
+LIBDOVECOT_LIBFTS_INCLUDE = @LIBDOVECOT_LIBFTS_INCLUDE@
+LIBDOVECOT_LMTP_INCLUDE = @LIBDOVECOT_LMTP_INCLUDE@
+LIBDOVECOT_LOGIN = @LIBDOVECOT_LOGIN@
+LIBDOVECOT_LOGIN_DEPS = @LIBDOVECOT_LOGIN_DEPS@
+LIBDOVECOT_LOGIN_INCLUDE = @LIBDOVECOT_LOGIN_INCLUDE@
+LIBDOVECOT_LUA = @LIBDOVECOT_LUA@
+LIBDOVECOT_LUA_DEPS = @LIBDOVECOT_LUA_DEPS@
+LIBDOVECOT_LUA_INCLUDE = @LIBDOVECOT_LUA_INCLUDE@
+LIBDOVECOT_NOTIFY_INCLUDE = @LIBDOVECOT_NOTIFY_INCLUDE@
+LIBDOVECOT_POP3_INCLUDE = @LIBDOVECOT_POP3_INCLUDE@
+LIBDOVECOT_PUSH_NOTIFICATION_INCLUDE = @LIBDOVECOT_PUSH_NOTIFICATION_INCLUDE@
+LIBDOVECOT_SERVICE_INCLUDE = @LIBDOVECOT_SERVICE_INCLUDE@
+LIBDOVECOT_SQL = @LIBDOVECOT_SQL@
+LIBDOVECOT_SQL_DEPS = @LIBDOVECOT_SQL_DEPS@
+LIBDOVECOT_SQL_INCLUDE = @LIBDOVECOT_SQL_INCLUDE@
+LIBDOVECOT_SSL = @LIBDOVECOT_SSL@
+LIBDOVECOT_SSL_DEPS = @LIBDOVECOT_SSL_DEPS@
+LIBDOVECOT_STORAGE = @LIBDOVECOT_STORAGE@
+LIBDOVECOT_STORAGE_DEPS = @LIBDOVECOT_STORAGE_DEPS@
+LIBDOVECOT_STORAGE_INCLUDE = @LIBDOVECOT_STORAGE_INCLUDE@
+LIBDOVECOT_SUBMISSION_INCLUDE = @LIBDOVECOT_SUBMISSION_INCLUDE@
+LIBOBJS = @LIBOBJS@
+LIBS = @LIBS@
+LIBTOOL = @LIBTOOL@
+LIPO = @LIPO@
+LN_S = @LN_S@
+LTLIBOBJS = @LTLIBOBJS@
+LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@
+MAINT = @MAINT@
+MAKEINFO = @MAKEINFO@
+MANIFEST_TOOL = @MANIFEST_TOOL@
+MKDIR_P = @MKDIR_P@
+NM = @NM@
+NMEDIT = @NMEDIT@
+OBJDUMP = @OBJDUMP@
+OBJEXT = @OBJEXT@
+OTOOL = @OTOOL@
+OTOOL64 = @OTOOL64@
+PACKAGE = @PACKAGE@
+PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
+PACKAGE_NAME = @PACKAGE_NAME@
+PACKAGE_STRING = @PACKAGE_STRING@
+PACKAGE_TARNAME = @PACKAGE_TARNAME@
+PACKAGE_URL = @PACKAGE_URL@
+PACKAGE_VERSION = @PACKAGE_VERSION@
+PATH_SEPARATOR = @PATH_SEPARATOR@
+PIE_CFLAGS = @PIE_CFLAGS@
+PIE_LDFLAGS = @PIE_LDFLAGS@
+RANLIB = @RANLIB@
+RELRO_LDFLAGS = @RELRO_LDFLAGS@
+RUN_TEST = @RUN_TEST@
+SED = @SED@
+SET_MAKE = @SET_MAKE@
+SHELL = @SHELL@
+STRIP = @STRIP@
+VALGRIND = @VALGRIND@
+VERSION = @VERSION@
+abs_builddir = @abs_builddir@
+abs_srcdir = @abs_srcdir@
+abs_top_builddir = @abs_top_builddir@
+abs_top_srcdir = @abs_top_srcdir@
+ac_ct_AR = @ac_ct_AR@
+ac_ct_CC = @ac_ct_CC@
+ac_ct_DUMPBIN = @ac_ct_DUMPBIN@
+am__include = @am__include@
+am__leading_dot = @am__leading_dot@
+am__quote = @am__quote@
+am__tar = @am__tar@
+am__untar = @am__untar@
+bindir = @bindir@
+build = @build@
+build_alias = @build_alias@
+build_cpu = @build_cpu@
+build_os = @build_os@
+build_vendor = @build_vendor@
+builddir = @builddir@
+datadir = @datadir@
+datarootdir = @datarootdir@
+docdir = @docdir@
+dovecot_docdir = @dovecot_docdir@
+dovecot_installed_moduledir = @dovecot_installed_moduledir@
+dovecot_moduledir = @dovecot_moduledir@
+dovecot_pkgincludedir = @dovecot_pkgincludedir@
+dovecot_pkglibdir = @dovecot_pkglibdir@
+dovecot_pkglibexecdir = @dovecot_pkglibexecdir@
+dovecot_statedir = @dovecot_statedir@
+dovecotdir = @dovecotdir@
+dvidir = @dvidir@
+exec_prefix = @exec_prefix@
+host = @host@
+host_alias = @host_alias@
+host_cpu = @host_cpu@
+host_os = @host_os@
+host_vendor = @host_vendor@
+htmldir = @htmldir@
+includedir = @includedir@
+infodir = @infodir@
+install_sh = @install_sh@
+libdir = @libdir@
+libexecdir = @libexecdir@
+localedir = @localedir@
+localstatedir = @localstatedir@
+mandir = @mandir@
+mkdir_p = @mkdir_p@
+moduledir = @moduledir@
+oldincludedir = @oldincludedir@
+pdfdir = @pdfdir@
+prefix = @prefix@
+program_transform_name = @program_transform_name@
+psdir = @psdir@
+runstatedir = @runstatedir@
+sbindir = @sbindir@
+sharedstatedir = @sharedstatedir@
+sieve_docdir = @sieve_docdir@
+srcdir = @srcdir@
+sysconfdir = @sysconfdir@
+target_alias = @target_alias@
+top_build_prefix = @top_build_prefix@
+top_builddir = @top_builddir@
+top_srcdir = @top_srcdir@
+noinst_LTLIBRARIES = libmanagesieve.la
+AM_CPPFLAGS = \
+ $(LIBDOVECOT_INCLUDE) \
+ -I$(top_srcdir)
+
+libmanagesieve_la_SOURCES = \
+ managesieve-arg.c \
+ managesieve-quote.c \
+ managesieve-parser.c
+
+noinst_HEADERS = \
+ managesieve-arg.h \
+ managesieve-quote.h \
+ managesieve-parser.h
+
+all: all-am
+
+.SUFFIXES:
+.SUFFIXES: .c .lo .o .obj
+$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps)
+ @for dep in $?; do \
+ case '$(am__configure_deps)' in \
+ *$$dep*) \
+ ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \
+ && { if test -f $@; then exit 0; else break; fi; }; \
+ exit 1;; \
+ esac; \
+ done; \
+ echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/lib-managesieve/Makefile'; \
+ $(am__cd) $(top_srcdir) && \
+ $(AUTOMAKE) --foreign src/lib-managesieve/Makefile
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+ @case '$?' in \
+ *config.status*) \
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \
+ *) \
+ echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \
+ cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \
+ esac;
+
+$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+
+$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(am__aclocal_m4_deps):
+
+clean-noinstLTLIBRARIES:
+ -test -z "$(noinst_LTLIBRARIES)" || rm -f $(noinst_LTLIBRARIES)
+ @list='$(noinst_LTLIBRARIES)'; \
+ locs=`for p in $$list; do echo $$p; done | \
+ sed 's|^[^/]*$$|.|; s|/[^/]*$$||; s|$$|/so_locations|' | \
+ sort -u`; \
+ test -z "$$locs" || { \
+ echo rm -f $${locs}; \
+ rm -f $${locs}; \
+ }
+
+libmanagesieve.la: $(libmanagesieve_la_OBJECTS) $(libmanagesieve_la_DEPENDENCIES) $(EXTRA_libmanagesieve_la_DEPENDENCIES)
+ $(AM_V_CCLD)$(LINK) $(libmanagesieve_la_OBJECTS) $(libmanagesieve_la_LIBADD) $(LIBS)
+
+mostlyclean-compile:
+ -rm -f *.$(OBJEXT)
+
+distclean-compile:
+ -rm -f *.tab.c
+
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/managesieve-arg.Plo@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/managesieve-parser.Plo@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/managesieve-quote.Plo@am__quote@ # am--include-marker
+
+$(am__depfiles_remade):
+ @$(MKDIR_P) $(@D)
+ @echo '# dummy' >$@-t && $(am__mv) $@-t $@
+
+am--depfiles: $(am__depfiles_remade)
+
+.c.o:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $<
+
+.c.obj:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
+
+.c.lo:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $<
+
+mostlyclean-libtool:
+ -rm -f *.lo
+
+clean-libtool:
+ -rm -rf .libs _libs
+
+ID: $(am__tagged_files)
+ $(am__define_uniq_tagged_files); mkid -fID $$unique
+tags: tags-am
+TAGS: tags
+
+tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files)
+ set x; \
+ here=`pwd`; \
+ $(am__define_uniq_tagged_files); \
+ shift; \
+ if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \
+ test -n "$$unique" || unique=$$empty_fix; \
+ if test $$# -gt 0; then \
+ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ "$$@" $$unique; \
+ else \
+ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ $$unique; \
+ fi; \
+ fi
+ctags: ctags-am
+
+CTAGS: ctags
+ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files)
+ $(am__define_uniq_tagged_files); \
+ test -z "$(CTAGS_ARGS)$$unique" \
+ || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
+ $$unique
+
+GTAGS:
+ here=`$(am__cd) $(top_builddir) && pwd` \
+ && $(am__cd) $(top_srcdir) \
+ && gtags -i $(GTAGS_ARGS) "$$here"
+cscopelist: cscopelist-am
+
+cscopelist-am: $(am__tagged_files)
+ list='$(am__tagged_files)'; \
+ case "$(srcdir)" in \
+ [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \
+ *) sdir=$(subdir)/$(srcdir) ;; \
+ esac; \
+ for i in $$list; do \
+ if test -f "$$i"; then \
+ echo "$(subdir)/$$i"; \
+ else \
+ echo "$$sdir/$$i"; \
+ fi; \
+ done >> $(top_builddir)/cscope.files
+
+distclean-tags:
+ -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
+
+distdir: $(BUILT_SOURCES)
+ $(MAKE) $(AM_MAKEFLAGS) distdir-am
+
+distdir-am: $(DISTFILES)
+ @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+ topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+ list='$(DISTFILES)'; \
+ dist_files=`for file in $$list; do echo $$file; done | \
+ sed -e "s|^$$srcdirstrip/||;t" \
+ -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
+ case $$dist_files in \
+ */*) $(MKDIR_P) `echo "$$dist_files" | \
+ sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
+ sort -u` ;; \
+ esac; \
+ for file in $$dist_files; do \
+ if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+ if test -d $$d/$$file; then \
+ dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
+ if test -d "$(distdir)/$$file"; then \
+ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+ fi; \
+ if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
+ cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \
+ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+ fi; \
+ cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \
+ else \
+ test -f "$(distdir)/$$file" \
+ || cp -p $$d/$$file "$(distdir)/$$file" \
+ || exit 1; \
+ fi; \
+ done
+check-am: all-am
+check: check-am
+all-am: Makefile $(LTLIBRARIES) $(HEADERS)
+installdirs:
+install: install-am
+install-exec: install-exec-am
+install-data: install-data-am
+uninstall: uninstall-am
+
+install-am: all-am
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+installcheck: installcheck-am
+install-strip:
+ if test -z '$(STRIP)'; then \
+ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+ install; \
+ else \
+ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+ "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \
+ fi
+mostlyclean-generic:
+
+clean-generic:
+
+distclean-generic:
+ -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
+ -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
+
+maintainer-clean-generic:
+ @echo "This command is intended for maintainers to use"
+ @echo "it deletes files that may require special tools to rebuild."
+clean: clean-am
+
+clean-am: clean-generic clean-libtool clean-noinstLTLIBRARIES \
+ mostlyclean-am
+
+distclean: distclean-am
+ -rm -f ./$(DEPDIR)/managesieve-arg.Plo
+ -rm -f ./$(DEPDIR)/managesieve-parser.Plo
+ -rm -f ./$(DEPDIR)/managesieve-quote.Plo
+ -rm -f Makefile
+distclean-am: clean-am distclean-compile distclean-generic \
+ distclean-tags
+
+dvi: dvi-am
+
+dvi-am:
+
+html: html-am
+
+html-am:
+
+info: info-am
+
+info-am:
+
+install-data-am:
+
+install-dvi: install-dvi-am
+
+install-dvi-am:
+
+install-exec-am:
+
+install-html: install-html-am
+
+install-html-am:
+
+install-info: install-info-am
+
+install-info-am:
+
+install-man:
+
+install-pdf: install-pdf-am
+
+install-pdf-am:
+
+install-ps: install-ps-am
+
+install-ps-am:
+
+installcheck-am:
+
+maintainer-clean: maintainer-clean-am
+ -rm -f ./$(DEPDIR)/managesieve-arg.Plo
+ -rm -f ./$(DEPDIR)/managesieve-parser.Plo
+ -rm -f ./$(DEPDIR)/managesieve-quote.Plo
+ -rm -f Makefile
+maintainer-clean-am: distclean-am maintainer-clean-generic
+
+mostlyclean: mostlyclean-am
+
+mostlyclean-am: mostlyclean-compile mostlyclean-generic \
+ mostlyclean-libtool
+
+pdf: pdf-am
+
+pdf-am:
+
+ps: ps-am
+
+ps-am:
+
+uninstall-am:
+
+.MAKE: install-am install-strip
+
+.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \
+ clean-generic clean-libtool clean-noinstLTLIBRARIES \
+ cscopelist-am ctags ctags-am distclean distclean-compile \
+ distclean-generic distclean-libtool distclean-tags distdir dvi \
+ dvi-am html html-am info info-am install install-am \
+ install-data install-data-am install-dvi install-dvi-am \
+ install-exec install-exec-am install-html install-html-am \
+ install-info install-info-am install-man install-pdf \
+ install-pdf-am install-ps install-ps-am install-strip \
+ installcheck installcheck-am installdirs maintainer-clean \
+ maintainer-clean-generic mostlyclean mostlyclean-compile \
+ mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \
+ tags tags-am uninstall uninstall-am
+
+.PRECIOUS: Makefile
+
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/pigeonhole/src/lib-managesieve/managesieve-arg.c b/pigeonhole/src/lib-managesieve/managesieve-arg.c
new file mode 100644
index 0000000..178bf45
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/managesieve-arg.c
@@ -0,0 +1,207 @@
+/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
+ */
+
+#include "lib.h"
+#include "str.h"
+#include "strescape.h"
+#include "managesieve-arg.h"
+
+bool managesieve_arg_get_atom(const struct managesieve_arg *arg,
+ const char **str_r)
+{
+ if (arg->type != MANAGESIEVE_ARG_ATOM)
+ return FALSE;
+
+ *str_r = arg->_data.str;
+ return TRUE;
+}
+
+bool managesieve_arg_get_number(const struct managesieve_arg *arg,
+ uoff_t *number_r)
+{
+ const char *data;
+ uoff_t num = 0;
+ size_t i;
+
+ if (arg->type != MANAGESIEVE_ARG_ATOM)
+ return FALSE;
+
+ data = arg->_data.str;
+ for (i = 0; i < arg->str_len; i++) {
+ uoff_t newnum;
+
+ if (data[i] < '0' || data[i] > '9')
+ return FALSE;
+
+ newnum = num*10 + (data[i] -'0');
+ if (newnum < num)
+ return FALSE;
+
+ num = newnum;
+ }
+
+ *number_r = num;
+ return TRUE;
+}
+
+bool managesieve_arg_get_quoted(const struct managesieve_arg *arg,
+ const char **str_r)
+{
+ if (arg->type != MANAGESIEVE_ARG_STRING)
+ return FALSE;
+
+ *str_r = arg->_data.str;
+ return TRUE;
+}
+
+bool managesieve_arg_get_string(const struct managesieve_arg *arg,
+ const char **str_r)
+{
+ if (arg->type != MANAGESIEVE_ARG_STRING &&
+ arg->type != MANAGESIEVE_ARG_LITERAL)
+ return FALSE;
+
+ *str_r = arg->_data.str;
+ return TRUE;
+}
+
+bool managesieve_arg_get_string_stream(const struct managesieve_arg *arg,
+ struct istream **stream_r)
+{
+ if (arg->type != MANAGESIEVE_ARG_STRING_STREAM)
+ return FALSE;
+
+ *stream_r = arg->_data.str_stream;
+ return TRUE;
+}
+
+bool managesieve_arg_get_list(const struct managesieve_arg *arg,
+ const struct managesieve_arg **list_r)
+{
+ unsigned int count;
+
+ return managesieve_arg_get_list_full(arg, list_r, &count);
+}
+
+bool managesieve_arg_get_list_full(const struct managesieve_arg *arg,
+ const struct managesieve_arg **list_r,
+ unsigned int *list_count_r)
+{
+ unsigned int count;
+
+ if (arg->type != MANAGESIEVE_ARG_LIST)
+ return FALSE;
+
+ *list_r = array_get(&arg->_data.list, &count);
+
+ /* drop MANAGESIEVE_ARG_EOL from list size */
+ i_assert(count > 0);
+ *list_count_r = count - 1;
+ return TRUE;
+}
+
+const char *managesieve_arg_as_atom(const struct managesieve_arg *arg)
+{
+ const char *str;
+
+ if (!managesieve_arg_get_atom(arg, &str))
+ i_unreached();
+ return str;
+}
+
+const char *managesieve_arg_as_string(const struct managesieve_arg *arg)
+{
+ const char *str;
+
+ if (!managesieve_arg_get_string(arg, &str))
+ i_unreached();
+ return str;
+}
+
+struct istream *
+managesieve_arg_as_string_stream(const struct managesieve_arg *arg)
+{
+ struct istream *stream;
+
+ if (!managesieve_arg_get_string_stream(arg, &stream))
+ i_unreached();
+ return stream;
+}
+
+const struct managesieve_arg *
+managesieve_arg_as_list(const struct managesieve_arg *arg)
+{
+ const struct managesieve_arg *ret;
+
+ if (!managesieve_arg_get_list(arg, &ret))
+ i_unreached();
+ return ret;
+}
+
+bool managesieve_arg_atom_equals(const struct managesieve_arg *arg,
+ const char *str)
+{
+ const char *value;
+
+ if (!managesieve_arg_get_atom(arg, &value))
+ return FALSE;
+ return strcasecmp(value, str) == 0;
+}
+
+void managesieve_write_arg(string_t *dest, const struct managesieve_arg *arg)
+{
+ const char *strval;
+
+ switch (arg->type) {
+ case MANAGESIEVE_ARG_ATOM:
+ str_append(dest, managesieve_arg_as_atom(arg));
+ break;
+ case MANAGESIEVE_ARG_STRING:
+ strval = managesieve_arg_as_string(arg);
+ str_append_c(dest, '"');
+ str_append_escaped(dest, strval, strlen(strval));
+ str_append_c(dest, '"');
+ break;
+ case MANAGESIEVE_ARG_STRING_STREAM:
+ str_append(dest, "\"<too large>\"");
+ break;
+ case MANAGESIEVE_ARG_LITERAL: {
+ const char *strarg = managesieve_arg_as_string(arg);
+ str_printfa(dest, "{%zu}\r\n",
+ strlen(strarg));
+ str_append(dest, strarg);
+ break;
+ }
+ case MANAGESIEVE_ARG_LIST:
+ str_append_c(dest, '(');
+ managesieve_write_args(dest, managesieve_arg_as_list(arg));
+ str_append_c(dest, ')');
+ break;
+ case MANAGESIEVE_ARG_NONE:
+ case MANAGESIEVE_ARG_EOL:
+ i_unreached();
+ }
+}
+
+void managesieve_write_args(string_t *dest, const struct managesieve_arg *args)
+{
+ bool first = TRUE;
+
+ for (; !MANAGESIEVE_ARG_IS_EOL(args); args++) {
+ if (first)
+ first = FALSE;
+ else
+ str_append_c(dest, ' ');
+ managesieve_write_arg(dest, args);
+ }
+}
+
+const char *managesieve_args_to_str(const struct managesieve_arg *args)
+{
+ string_t *str;
+
+ str = t_str_new(128);
+ managesieve_write_args(str, args);
+ return str_c(str);
+}
+
diff --git a/pigeonhole/src/lib-managesieve/managesieve-arg.h b/pigeonhole/src/lib-managesieve/managesieve-arg.h
new file mode 100644
index 0000000..23f7c05
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/managesieve-arg.h
@@ -0,0 +1,117 @@
+#ifndef MANAGESIEVE_ARG_H
+#define MANAGESIEVE_ARG_H
+
+#include "array.h"
+
+/*
+ * QUOTED-SPECIALS = <"> / "\"
+ */
+#define IS_QUOTED_SPECIAL(c) \
+ ((c) == '"' || (c) == '\\')
+
+/*
+ * ATOM-SPECIALS = "(" / ")" / "{" / SP / CTL / QUOTED-SPECIALS
+ */
+#define IS_ATOM_SPECIAL(c) \
+ ((c) == '(' || (c) == ')' || (c) == '{' || \
+ (c) <= 32 || (c) == 0x7f || \
+ IS_QUOTED_SPECIAL(c))
+
+/*
+ * CHAR = %x01-7F
+ */
+#define IS_CHAR(c) \
+ (((c) & 0x80) == 0)
+
+/*
+ * TEXT-CHAR = %x01-09 / %x0B-0C / %x0E-7F
+ * ;; any CHAR except CR and LF
+ */
+#define IS_TEXT_CHAR(c) \
+ (IS_CHAR(c) && (c) != '\r' && (c) != '\n')
+
+/*
+ * SAFE-CHAR = %x01-09 / %x0B-0C / %x0E-21 /
+ * %x23-5B / %x5D-7F
+ * ;; any TEXT-CHAR except QUOTED-SPECIALS
+ */
+#define IS_SAFE_CHAR(c) \
+ (IS_TEXT_CHAR(c) && !IS_QUOTED_SPECIAL(c))
+
+enum managesieve_arg_type {
+ MANAGESIEVE_ARG_NONE = 0,
+ MANAGESIEVE_ARG_ATOM,
+ MANAGESIEVE_ARG_STRING,
+ MANAGESIEVE_ARG_STRING_STREAM,
+
+ MANAGESIEVE_ARG_LIST,
+
+ /* literals are returned as MANAGESIEVE_ARG_STRING by default */
+ MANAGESIEVE_ARG_LITERAL,
+
+ MANAGESIEVE_ARG_EOL /* end of argument list */
+};
+
+ARRAY_DEFINE_TYPE(managesieve_arg_list, struct managesieve_arg);
+struct managesieve_arg {
+ enum managesieve_arg_type type;
+ /* parent argument; always of type MANAGESIEVE_ARG_LIST */
+ struct managesieve_arg *parent;
+
+ /* Set when _data.str is set */
+ size_t str_len;
+
+ union {
+ const char *str;
+ struct istream *str_stream;
+ ARRAY_TYPE(managesieve_arg_list) list;
+ } _data;
+};
+
+#define MANAGESIEVE_ARG_IS_EOL(arg) \
+ ((arg)->type == MANAGESIEVE_ARG_EOL)
+
+bool managesieve_arg_get_atom(const struct managesieve_arg *arg,
+ const char **str_r) ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_number(const struct managesieve_arg *arg,
+ uoff_t *number_r) ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_quoted(const struct managesieve_arg *arg,
+ const char **str_r) ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_string(const struct managesieve_arg *arg,
+ const char **str_r) ATTR_WARN_UNUSED_RESULT;
+
+bool managesieve_arg_get_string_stream(const struct managesieve_arg *arg,
+ struct istream **stream_r)
+ ATTR_WARN_UNUSED_RESULT;
+
+bool managesieve_arg_get_list(const struct managesieve_arg *arg,
+ const struct managesieve_arg **list_r)
+ ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_list_full(const struct managesieve_arg *arg,
+ const struct managesieve_arg **list_r,
+ unsigned int *list_count_r)
+ ATTR_WARN_UNUSED_RESULT;
+
+/* Similar to above, but assumes that arg is already of correct type. */
+const char *managesieve_arg_as_atom(const struct managesieve_arg *arg);
+const char *managesieve_arg_as_string(const struct managesieve_arg *arg);
+struct istream *
+managesieve_arg_as_string_stream(const struct managesieve_arg *arg);
+const struct managesieve_arg *
+managesieve_arg_as_list(const struct managesieve_arg *arg);
+
+/* Returns TRUE if arg is atom and case-insensitively matches str */
+bool managesieve_arg_atom_equals(const struct managesieve_arg *arg,
+ const char *str);
+
+/* Write ManageSieve arg to the given string. Because
+ MANAGESIVE_ARG_LITERAL_SIZE* have no content, they're written as
+ "{size}\r\n<too large>". */
+void managesieve_write_arg(string_t *dest, const struct managesieve_arg *arg);
+/* Same as managesieve_write_arg(), but write all the args until EOL. */
+void managesieve_write_args(string_t *dest, const struct managesieve_arg *args);
+/* Like managesieve_write_args(), but return the string allocated from data
+ stack. */
+const char *managesieve_args_to_str(const struct managesieve_arg *args);
+
+#endif
diff --git a/pigeonhole/src/lib-managesieve/managesieve-parser.c b/pigeonhole/src/lib-managesieve/managesieve-parser.c
new file mode 100644
index 0000000..546e431
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/managesieve-parser.c
@@ -0,0 +1,757 @@
+/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
+ */
+
+#include "lib.h"
+#include "unichar.h"
+#include "istream-private.h"
+#include "ostream.h"
+#include "strescape.h"
+#include "managesieve-parser.h"
+
+#define is_linebreak(c) \
+ ((c) == '\r' || (c) == '\n')
+
+#define LIST_INIT_COUNT 7
+
+enum arg_parse_type {
+ ARG_PARSE_NONE = 0,
+ ARG_PARSE_ATOM,
+ ARG_PARSE_STRING,
+ ARG_PARSE_LITERAL,
+ ARG_PARSE_LITERAL_DATA
+};
+
+struct managesieve_parser {
+ /* permanent */
+ pool_t pool;
+ struct istream *input;
+ size_t max_line_size;
+ enum managesieve_parser_flags flags;
+
+ /* reset by managesieve_parser_reset(): */
+ size_t line_size;
+ ARRAY_TYPE(managesieve_arg_list) root_list;
+ ARRAY_TYPE(managesieve_arg_list) *cur_list;
+ struct managesieve_arg *list_arg;
+
+ enum arg_parse_type cur_type;
+ size_t cur_pos; /* parser position in input buffer */
+
+ int str_first_escape; /* ARG_PARSE_STRING: index to first '\' */
+ uoff_t literal_size; /* ARG_PARSE_LITERAL: string size */
+
+ struct istream *str_stream;
+
+ const char *error;
+
+ bool literal_skip_crlf:1;
+ bool literal_nonsync:1;
+ bool eol:1;
+ bool fatal_error:1;
+};
+
+static struct istream *quoted_string_istream_create
+ (struct managesieve_parser *parser);
+
+struct managesieve_parser *
+managesieve_parser_create(struct istream *input, size_t max_line_size)
+{
+ struct managesieve_parser *parser;
+
+ parser = i_new(struct managesieve_parser, 1);
+ parser->pool = pool_alloconly_create("MANAGESIEVE parser", 8192);
+ parser->input = input;
+ parser->max_line_size = max_line_size;
+
+ p_array_init(&parser->root_list, parser->pool, LIST_INIT_COUNT);
+ parser->cur_list = &parser->root_list;
+ return parser;
+}
+
+void managesieve_parser_destroy(struct managesieve_parser **parser)
+{
+ i_stream_unref(&(*parser)->str_stream);
+
+ pool_unref(&(*parser)->pool);
+ i_free(*parser);
+ *parser = NULL;
+}
+
+void managesieve_parser_reset(struct managesieve_parser *parser)
+{
+ p_clear(parser->pool);
+
+ parser->line_size = 0;
+
+ p_array_init(&parser->root_list, parser->pool, LIST_INIT_COUNT);
+ parser->cur_list = &parser->root_list;
+ parser->list_arg = NULL;
+
+ parser->cur_type = ARG_PARSE_NONE;
+ parser->cur_pos = 0;
+
+ parser->str_first_escape = 0;
+ parser->literal_size = 0;
+
+ parser->error = NULL;
+
+ parser->literal_skip_crlf = FALSE;
+ parser->eol = FALSE;
+
+ i_stream_unref(&parser->str_stream);
+}
+
+const char *
+managesieve_parser_get_error(struct managesieve_parser *parser, bool *fatal)
+{
+ *fatal = parser->fatal_error;
+ return parser->error;
+}
+
+/* Skip over everything parsed so far, plus the following whitespace */
+static bool
+managesieve_parser_skip_to_next(struct managesieve_parser *parser,
+ const unsigned char **data, size_t *data_size)
+{
+ size_t i;
+
+ for (i = parser->cur_pos; i < *data_size; i++) {
+ if ((*data)[i] != ' ')
+ break;
+ }
+
+ parser->line_size += i;
+ i_stream_skip(parser->input, i);
+ parser->cur_pos = 0;
+
+ *data += i;
+ *data_size -= i;
+ return *data_size > 0;
+}
+
+static struct managesieve_arg *
+managesieve_arg_create(struct managesieve_parser *parser)
+{
+ struct managesieve_arg *arg;
+
+ arg = array_append_space(parser->cur_list);
+ arg->parent = parser->list_arg;
+
+ return arg;
+}
+
+static void
+managesieve_parser_save_arg(struct managesieve_parser *parser,
+ const unsigned char *data, size_t size)
+{
+ struct managesieve_arg *arg;
+ char *str;
+
+ arg = managesieve_arg_create(parser);
+
+ switch (parser->cur_type) {
+ case ARG_PARSE_ATOM:
+ /* Simply save the string */
+ arg->type = MANAGESIEVE_ARG_ATOM;
+ arg->_data.str = p_strndup(parser->pool, data, size);
+ arg->str_len = size;
+ break;
+ case ARG_PARSE_STRING:
+ /* Data is quoted and may contain escapes. */
+ if ((parser->flags &
+ MANAGESIEVE_PARSE_FLAG_STRING_STREAM) != 0) {
+ arg->type = MANAGESIEVE_ARG_STRING_STREAM;
+ arg->_data.str_stream = parser->str_stream;
+ } else {
+ i_assert(size > 0);
+
+ arg->type = MANAGESIEVE_ARG_STRING;
+ str = p_strndup(parser->pool, data+1, size-1);
+
+ /* remove the escapes */
+ if (parser->str_first_escape >= 0 &&
+ (parser->flags &
+ MANAGESIEVE_PARSE_FLAG_NO_UNESCAPE) == 0)
+ (void)str_unescape(str);
+
+ arg->_data.str = str;
+ arg->str_len = strlen(str);
+ }
+ break;
+ case ARG_PARSE_LITERAL_DATA:
+ if ((parser->flags &
+ MANAGESIEVE_PARSE_FLAG_STRING_STREAM) != 0) {
+ arg->type = MANAGESIEVE_ARG_STRING_STREAM;
+ arg->_data.str_stream = parser->str_stream;
+ } else if ((parser->flags &
+ MANAGESIEVE_PARSE_FLAG_LITERAL_TYPE) != 0) {
+ arg->type = MANAGESIEVE_ARG_LITERAL;
+ arg->_data.str = p_strndup(parser->pool, data, size);
+ arg->str_len = size;
+ } else {
+ arg->type = MANAGESIEVE_ARG_STRING;
+ arg->_data.str = p_strndup(parser->pool, data, size);
+ arg->str_len = size;
+ }
+ break;
+ default:
+ i_unreached();
+ }
+
+ parser->cur_type = ARG_PARSE_NONE;
+}
+
+static bool is_valid_atom_char(struct managesieve_parser *parser, char chr)
+{
+ if (IS_ATOM_SPECIAL((unsigned char)chr)) {
+ parser->error = "Invalid characters in atom";
+ return FALSE;
+ } else if ((((unsigned char)chr) & 0x80) != 0) {
+ parser->error = "8bit data in atom";
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static bool
+managesieve_parser_read_atom(struct managesieve_parser *parser,
+ const unsigned char *data, size_t data_size)
+{
+ size_t i;
+
+ /* Read until we've found space, CR or LF. */
+ for (i = parser->cur_pos; i < data_size; i++) {
+ if (data[i] == ' ' || data[i] == ')' ||
+ is_linebreak(data[i])) {
+ managesieve_parser_save_arg(parser, data, i);
+ break;
+ } else if (!is_valid_atom_char(parser, data[i]))
+ return FALSE;
+ }
+
+ parser->cur_pos = i;
+ return (parser->cur_type == ARG_PARSE_NONE);
+}
+
+static bool
+managesieve_parser_read_string(struct managesieve_parser *parser,
+ const unsigned char *data, size_t data_size)
+{
+ size_t i;
+
+ /* QUOTED-CHAR = SAFE-UTF8-CHAR / "\" QUOTED-SPECIALS
+ quoted = <"> *QUOTED-CHAR <">
+ ;; limited to 1024 octets between the <">s
+ */
+
+ /* Read until we've found non-escaped ", CR or LF */
+ for (i = parser->cur_pos; i < data_size; i++) {
+ if (data[i] == '"') {
+ if (!uni_utf8_data_is_valid(data+1, i-1)) {
+ parser->error =
+ "Invalid UTF-8 character in quoted-string.";
+ return FALSE;
+ }
+
+ managesieve_parser_save_arg(parser, data, i);
+ i++; /* skip the trailing '"' too */
+ break;
+ }
+
+ if (data[i] == '\0') {
+ parser->error = "NULs not allowed in strings";
+ return FALSE;
+ }
+
+ if (data[i] == '\\') {
+ if (i+1 == data_size) {
+ /* Known data ends with '\' - leave it to next
+ time as well if it happens to be \" */
+ break;
+ }
+
+ /* Save the first escaped char */
+ if (parser->str_first_escape < 0)
+ parser->str_first_escape = i;
+
+ /* Skip the escaped char */
+ i++;
+
+ if (!IS_QUOTED_SPECIAL(data[i])) {
+ parser->error =
+ "Escaped quoted-string character is not a QUOTED-SPECIAL.";
+ return FALSE;
+ }
+ continue;
+ }
+
+ if ((data[i] & 0x80) == 0 && !IS_SAFE_CHAR(data[i])) {
+ parser->error = "String contains invalid character.";
+ return FALSE;
+ }
+ }
+
+ parser->cur_pos = i;
+ return (parser->cur_type == ARG_PARSE_NONE);
+}
+
+static bool managesieve_parser_literal_end(struct managesieve_parser *parser)
+{
+ if ((parser->flags & MANAGESIEVE_PARSE_FLAG_STRING_STREAM) == 0) {
+ if (parser->line_size >= parser->max_line_size ||
+ (parser->literal_size >
+ (parser->max_line_size - parser->line_size))) {
+ /* Too long string, abort. */
+ parser->error = "Literal size too large";
+ parser->fatal_error = TRUE;
+ return FALSE;
+ }
+ }
+
+ parser->cur_type = ARG_PARSE_LITERAL_DATA;
+ parser->literal_skip_crlf = TRUE;
+
+ parser->cur_pos = 0;
+ return TRUE;
+}
+
+static bool
+managesieve_parser_read_literal(struct managesieve_parser *parser,
+ const unsigned char *data, size_t data_size)
+{
+ size_t i;
+
+ /* Expecting digits + "}" */
+ for (i = parser->cur_pos; i < data_size; i++) {
+ if (data[i] == '}') {
+ parser->line_size += i+1;
+ i_stream_skip(parser->input, i+1);
+
+ return managesieve_parser_literal_end(parser);
+ }
+
+ if (parser->literal_nonsync) {
+ parser->error = "Expecting '}' after '+'";
+ return FALSE;
+ }
+
+ if (data[i] == '+') {
+ parser->literal_nonsync = TRUE;
+ continue;
+ }
+
+ if (data[i] < '0' || data[i] > '9') {
+ parser->error = "Invalid literal size";
+ return FALSE;
+ }
+
+ if (parser->literal_size >= ((uoff_t)-1 / 10)) {
+ if (parser->literal_size > ((uoff_t)-1 / 10) ||
+ (uoff_t)(data[i] - '0') > ((uoff_t)-1 % 10)) {
+ parser->error = "Literal size too large";
+ return FALSE;
+ }
+ }
+ parser->literal_size = parser->literal_size * 10 +
+ (data[i] - '0');
+ }
+
+ parser->cur_pos = i;
+ return FALSE;
+}
+
+static bool
+managesieve_parser_read_literal_data(struct managesieve_parser *parser,
+ const unsigned char *data,
+ size_t data_size)
+{
+ if (parser->literal_skip_crlf) {
+ /* Skip \r\n or \n, anything else gives an error */
+ if (data_size == 0)
+ return FALSE;
+
+ if (*data == '\r') {
+ parser->line_size++;
+ data++; data_size--;
+ i_stream_skip(parser->input, 1);
+
+ if (data_size == 0)
+ return FALSE;
+ }
+
+ if (*data != '\n') {
+ parser->error = "Missing LF after literal size";
+ return FALSE;
+ }
+
+ parser->line_size++;
+ data++; data_size--;
+ i_stream_skip(parser->input, 1);
+
+ parser->literal_skip_crlf = FALSE;
+
+ i_assert(parser->cur_pos == 0);
+ }
+
+ if ((parser->flags & MANAGESIEVE_PARSE_FLAG_STRING_STREAM) == 0) {
+ /* Now we just wait until we've read enough data */
+ if (data_size < parser->literal_size) {
+ return FALSE;
+ } else {
+ if (!uni_utf8_data_is_valid(
+ data, (size_t)parser->literal_size)) {
+ parser->error =
+ "Invalid UTF-8 character in literal string.";
+ return FALSE;
+ }
+
+ managesieve_parser_save_arg(
+ parser, data, (size_t)parser->literal_size);
+ parser->cur_pos = (size_t)parser->literal_size;
+ return TRUE;
+ }
+ } else {
+ /* We don't read the data; we just create a stream for the
+ literal */
+ parser->eol = TRUE;
+ parser->str_stream = i_stream_create_limit(
+ parser->input, parser->literal_size);
+ managesieve_parser_save_arg(parser, NULL, 0);
+ return TRUE;
+ }
+}
+
+/* Returns TRUE if argument was fully processed. Also returns TRUE if an
+ argument inside a list was processed. */
+static bool managesieve_parser_read_arg(struct managesieve_parser *parser)
+{
+ const unsigned char *data;
+ size_t data_size;
+
+ data = i_stream_get_data(parser->input, &data_size);
+ if (data_size == 0)
+ return FALSE;
+
+ while (parser->cur_type == ARG_PARSE_NONE) {
+ /* We haven't started parsing yet */
+ if (!managesieve_parser_skip_to_next(parser, &data, &data_size))
+ return FALSE;
+ i_assert(parser->cur_pos == 0);
+
+ switch (data[0]) {
+ case '\r':
+ case '\n':
+ /* Unexpected end of line */
+ parser->eol = TRUE;
+ return FALSE;
+ case '"':
+ parser->cur_type = ARG_PARSE_STRING;
+ parser->str_first_escape = -1;
+ break;
+ case '{':
+ parser->cur_type = ARG_PARSE_LITERAL;
+ parser->literal_size = 0;
+ parser->literal_nonsync = FALSE;
+ break;
+ default:
+ if (!is_valid_atom_char(parser, data[0]))
+ return FALSE;
+ parser->cur_type = ARG_PARSE_ATOM;
+ break;
+ }
+ parser->cur_pos++;
+ }
+
+ i_assert(data_size > 0);
+
+ switch (parser->cur_type) {
+ case ARG_PARSE_ATOM:
+ if (!managesieve_parser_read_atom(parser, data, data_size))
+ return FALSE;
+ break;
+ case ARG_PARSE_STRING:
+ if ((parser->flags &
+ MANAGESIEVE_PARSE_FLAG_STRING_STREAM) != 0) {
+ parser->eol = TRUE;
+ parser->line_size += parser->cur_pos;
+ i_stream_skip(parser->input, parser->cur_pos);
+ parser->cur_pos = 0;
+ parser->str_stream =
+ quoted_string_istream_create(parser);
+ managesieve_parser_save_arg(parser, NULL, 0);
+
+ } else if (!managesieve_parser_read_string(parser,
+ data, data_size)) {
+ return FALSE;
+ }
+ break;
+ case ARG_PARSE_LITERAL:
+ if (!managesieve_parser_read_literal(parser, data, data_size))
+ return FALSE;
+
+ /* Pass through to parsing data. since input->skip was
+ modified, we need to get the data start position again. */
+ data = i_stream_get_data(parser->input, &data_size);
+
+ /* fall through */
+ case ARG_PARSE_LITERAL_DATA:
+ if (!managesieve_parser_read_literal_data(parser,
+ data, data_size))
+ return FALSE;
+ break;
+ default:
+ i_unreached();
+ }
+
+ i_assert(parser->cur_type == ARG_PARSE_NONE);
+ return TRUE;
+}
+
+/* ARG_PARSE_NONE checks that last argument isn't only partially parsed. */
+#define IS_UNFINISHED(parser) \
+ ((parser)->cur_type != ARG_PARSE_NONE || \
+ (parser)->cur_list != &parser->root_list)
+
+static int
+finish_line(struct managesieve_parser *parser, unsigned int count,
+ const struct managesieve_arg **args_r)
+{
+ struct managesieve_arg *arg;
+ int ret = array_count(&parser->root_list);
+
+ parser->line_size += parser->cur_pos;
+ i_stream_skip(parser->input, parser->cur_pos);
+ parser->cur_pos = 0;
+
+ /* Fill the missing parameters with NILs */
+ while (count > array_count(&parser->root_list)) {
+ arg = array_append_space(&parser->root_list);
+ arg->type = MANAGESIEVE_ARG_NONE;
+ }
+ arg = array_append_space(&parser->root_list);
+ arg->type = MANAGESIEVE_ARG_EOL;
+
+ *args_r = array_get(&parser->root_list, &count);
+ return ret;
+}
+
+int managesieve_parser_read_args(struct managesieve_parser *parser,
+ unsigned int count,
+ enum managesieve_parser_flags flags,
+ const struct managesieve_arg **args_r)
+{
+ parser->flags = flags;
+
+ while (!parser->eol && (count == 0 || IS_UNFINISHED(parser) ||
+ array_count(&parser->root_list) < count)) {
+ if (!managesieve_parser_read_arg(parser))
+ break;
+
+ if (parser->line_size > parser->max_line_size) {
+ parser->error = "MANAGESIEVE command line too large";
+ break;
+ }
+ }
+
+ if (parser->error != NULL) {
+ /* Error, abort */
+ parser->line_size += parser->cur_pos;
+ i_stream_skip(parser->input, parser->cur_pos);
+ parser->cur_pos = 0;
+ *args_r = NULL;
+ return -1;
+ } else if ((!IS_UNFINISHED(parser) && count > 0 &&
+ array_count(&parser->root_list) >= count) || parser->eol) {
+ /* All arguments read / end of line. */
+ return finish_line(parser, count, args_r);
+ } else {
+ /* Need more data */
+ *args_r = NULL;
+ return -2;
+ }
+}
+
+int managesieve_parser_finish_line(struct managesieve_parser *parser,
+ unsigned int count,
+ enum managesieve_parser_flags flags,
+ const struct managesieve_arg **args_r)
+{
+ const unsigned char *data;
+ size_t data_size;
+ int ret;
+
+ ret = managesieve_parser_read_args(parser, count, flags, args_r);
+ if (ret == -2) {
+ /* We should have noticed end of everything except atom */
+ if (parser->cur_type == ARG_PARSE_ATOM) {
+ data = i_stream_get_data(parser->input, &data_size);
+ managesieve_parser_save_arg(parser, data, data_size);
+ }
+ }
+ return finish_line(parser, count, args_r);
+}
+
+const char *managesieve_parser_read_word(struct managesieve_parser *parser)
+{
+ const unsigned char *data;
+ size_t i, data_size;
+
+ data = i_stream_get_data(parser->input, &data_size);
+
+ for (i = 0; i < data_size; i++) {
+ if (data[i] == ' ' || data[i] == '\r' || data[i] == '\n')
+ break;
+ }
+
+ if (i < data_size) {
+ data_size = i + (data[i] == ' ' ? 1 : 0);
+ parser->line_size += data_size;
+ i_stream_skip(parser->input, data_size);
+ return p_strndup(parser->pool, data, i);
+ } else {
+ return NULL;
+ }
+}
+
+/*
+ * Quoted string stream
+ */
+
+struct quoted_string_istream {
+ struct istream_private istream;
+
+ /* The end '"' was found */
+ bool str_end:1;
+};
+
+static int
+quoted_string_istream_read_parent(struct quoted_string_istream *qsstream,
+ unsigned int min_bytes)
+{
+ struct istream_private *stream = &qsstream->istream;
+ size_t size, avail;
+ ssize_t ret;
+
+ size = i_stream_get_data_size(stream->parent);
+ while (size < min_bytes) {
+ ret = i_stream_read_memarea(stream->parent);
+ if (ret <= 0) {
+ if (ret == -2) {
+ /* Tiny parent buffer size - shouldn't happen */
+ return -2;
+ }
+ stream->istream.stream_errno =
+ stream->parent->stream_errno;
+ stream->istream.eof = stream->parent->eof;
+ if (ret == -1 && stream->istream.stream_errno == 0) {
+ io_stream_set_error(&stream->iostream,
+ "Quoted string ends without closing quotes");
+ stream->istream.stream_errno = EPIPE;
+ }
+ return ret;
+ }
+ size = i_stream_get_data_size(stream->parent);
+ }
+
+ if (!i_stream_try_alloc(stream, size, &avail))
+ return -2;
+ return 1;
+}
+
+static ssize_t quoted_string_istream_read(struct istream_private *stream)
+{
+ struct quoted_string_istream *qsstream =
+ (struct quoted_string_istream *)stream;
+ const unsigned char *data;
+ unsigned int extra;
+ size_t i, dest, size;
+ ssize_t ret;
+
+ if (qsstream->str_end) {
+ stream->istream.eof = TRUE;
+ return -1;
+ }
+
+ ret = quoted_string_istream_read_parent(qsstream, 1);
+ if (ret <= 0)
+ return ret;
+
+ /* @UNSAFE */
+ dest = stream->pos;
+ extra = 0;
+
+ data = i_stream_get_data(stream->parent, &size);
+ for (i = 0; i < size && dest < stream->buffer_size; ) {
+ if (data[i] == '"') {
+ i++;
+ qsstream->str_end = TRUE;
+ if (dest == stream->pos) {
+ i_stream_skip(stream->parent, i);
+ stream->istream.eof = TRUE;
+ return -1;
+ }
+ break;
+ } else if (data[i] == '\\') {
+ if (i+1 == size) {
+ /* Not enough input for \x */
+ extra = 1;
+ break;
+ }
+ i++;
+
+ if (!IS_QUOTED_SPECIAL(data[i])) {
+ /* Invalid string */
+ io_stream_set_error(&stream->iostream,
+ "Escaped quoted-string character is not a QUOTED-SPECIAL");
+ stream->istream.stream_errno = EINVAL;
+ return -1;
+ }
+ stream->w_buffer[dest++] = data[i];
+ i++;
+ } else {
+ if (data[i] == '\r' || data[i] == '\n') {
+ io_stream_set_error(&stream->iostream,
+ "Quoted string contains an invalid character");
+ stream->istream.stream_errno = EINVAL;
+ return -1;
+ }
+
+ stream->w_buffer[dest++] = data[i];
+ i++;
+ }
+ i_assert(dest <= stream->buffer_size);
+ }
+ i_stream_skip(stream->parent, i);
+
+ ret = dest - stream->pos;
+ if (ret == 0) {
+ /* Not enough input */
+ i_assert(i == 0);
+ i_assert(extra > 0);
+ ret = quoted_string_istream_read_parent(qsstream, extra+1);
+ if (ret <= 0)
+ return ret;
+ return quoted_string_istream_read(stream);
+ }
+ i_assert(ret > 0);
+ stream->pos = dest;
+ return ret;
+}
+
+static struct
+istream *quoted_string_istream_create(struct managesieve_parser *parser)
+{
+ struct quoted_string_istream *qsstream;
+
+ qsstream = i_new(struct quoted_string_istream, 1);
+ qsstream->istream.max_buffer_size =
+ parser->input->real_stream->max_buffer_size;
+ qsstream->istream.read = quoted_string_istream_read;
+
+ qsstream->istream.istream.readable_fd = FALSE;
+ qsstream->istream.istream.blocking = parser->input->blocking;
+ qsstream->istream.istream.seekable = FALSE;
+ return i_stream_create(&qsstream->istream, parser->input,
+ i_stream_get_fd(parser->input), 0);
+}
diff --git a/pigeonhole/src/lib-managesieve/managesieve-parser.h b/pigeonhole/src/lib-managesieve/managesieve-parser.h
new file mode 100644
index 0000000..93e67ae
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/managesieve-parser.h
@@ -0,0 +1,67 @@
+#ifndef MANAGESIEVE_PARSER_H
+#define MANAGESIEVE_PARSER_H
+
+#include "managesieve-arg.h"
+
+enum managesieve_parser_flags {
+ /* Set this flag if you want to read a string argument as as stream.
+ Useful when you need to deal with large strings. The string must be
+ the last read argument. */
+ MANAGESIEVE_PARSE_FLAG_STRING_STREAM = 0x01,
+ /* Don't remove '\' chars from string arguments */
+ MANAGESIEVE_PARSE_FLAG_NO_UNESCAPE = 0x02,
+ /* Return literals as MANAGESIEVE_ARG_LITERAL instead of
+ MANAGESIEVE_ARG_STRING */
+ MANAGESIEVE_PARSE_FLAG_LITERAL_TYPE = 0x04
+};
+
+struct managesieve_parser;
+
+/* Create new MANAGESIEVE argument parser.
+
+ max_line_size can be used to approximately limit the maximum amount of memory
+ that gets allocated when parsing a line. Input buffer size limits the maximum
+ size of each parsed token.
+
+ Usually the largest lines are large only because they have a one huge message
+ set token, so you'll probably want to keep input buffer size the same as
+ max_line_size. That means the maximum memory usage is around
+ 2 * max_line_size. */
+struct managesieve_parser *
+managesieve_parser_create(struct istream *input, size_t max_line_size);
+void managesieve_parser_destroy(struct managesieve_parser **parser);
+
+/* Reset the parser to initial state. */
+void managesieve_parser_reset(struct managesieve_parser *parser);
+
+/* Return the last error in parser. fatal is set to TRUE if there's no way to
+ continue parsing, currently only if too large non-sync literal size was
+ given. */
+const char *
+managesieve_parser_get_error(struct managesieve_parser *parser, bool *fatal);
+
+/* Read a number of arguments. This function doesn't call i_stream_read(), you
+ need to do that. Returns number of arguments read (may be less than count
+ in case of EOL), -2 if more data is needed or -1 if error occurred.
+
+ count-sized array of arguments are stored into args when return value is 0 or
+ larger. If all arguments weren't read, they're set to NIL. count can be set
+ to 0 to read all arguments in the line. Last element in args is always of
+ type MANAGESIEVE_ARG_EOL. */
+int managesieve_parser_read_args(struct managesieve_parser *parser,
+ unsigned int count,
+ enum managesieve_parser_flags flags,
+ const struct managesieve_arg **args_r);
+
+/* Just like managesieve_parser_read_args(), but assume \n at end of data in
+ input stream. */
+int managesieve_parser_finish_line(struct managesieve_parser *parser,
+ unsigned int count,
+ enum managesieve_parser_flags flags,
+ const struct managesieve_arg **args_r);
+
+/* Read one word - used for reading command name. Returns NULL if more data is
+ needed. */
+const char *managesieve_parser_read_word(struct managesieve_parser *parser);
+
+#endif
diff --git a/pigeonhole/src/lib-managesieve/managesieve-quote.c b/pigeonhole/src/lib-managesieve/managesieve-quote.c
new file mode 100644
index 0000000..a86818c
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/managesieve-quote.c
@@ -0,0 +1,121 @@
+/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
+ */
+
+#include "lib.h"
+#include "str.h"
+#include "unichar.h"
+#include "managesieve-parser.h"
+#include "managesieve-quote.h"
+
+/* Turn the value string into a valid MANAGESIEVE string or literal, no matter
+ * what. QUOTED-SPECIALS are escaped, but any invalid (UTF-8) character
+ * is simply removed. Linebreak characters are not considered invalid, but
+ * they do force the generation of a string literal.
+ */
+void managesieve_quote_append(string_t *str, const unsigned char *value,
+ size_t value_len, bool compress_lwsp)
+{
+ size_t i, extra = 0, escape = 0;
+ string_t *tmp;
+ bool
+ last_lwsp = TRUE,
+ literal = FALSE,
+ modify = FALSE;
+
+ if (value == NULL) {
+ str_append(str, "\"\"");
+ return;
+ }
+
+ if (value_len == (size_t)-1)
+ value_len = strlen((const char *) value);
+
+ for (i = 0; i < value_len; i++) {
+ switch (value[i]) {
+ case ' ':
+ case '\t':
+ if (last_lwsp && compress_lwsp) {
+ modify = TRUE;
+ extra++;
+ }
+ last_lwsp = TRUE;
+ break;
+ case '"':
+ case '\\':
+ escape++;
+ last_lwsp = FALSE;
+ break;
+ case 13:
+ case 10:
+ literal = TRUE;
+ last_lwsp = TRUE;
+ break;
+ default:
+ last_lwsp = FALSE;
+ }
+ }
+
+ if (!literal) {
+ /* no linebreak chars, return as (escaped) "string" */
+ str_append_c(str, '"');
+ } else {
+ /* return as literal */
+ str_printfa(str, "{%zu}\r\n", value_len - extra);
+ }
+
+ tmp = t_str_new(value_len+escape+4);
+ if (!modify && (literal || escape == 0))
+ str_append_data(tmp, value, value_len);
+ else {
+ last_lwsp = TRUE;
+ for (i = 0; i < value_len; i++) {
+ switch (value[i]) {
+ case '"':
+ case '\\':
+ last_lwsp = FALSE;
+ if (!literal)
+ str_append_c(tmp, '\\');
+ str_append_c(tmp, value[i]);
+ break;
+ case ' ':
+ case '\t':
+ if (!last_lwsp || !compress_lwsp)
+ str_append_c(tmp, ' ');
+ last_lwsp = TRUE;
+ break;
+ case 13:
+ case 10:
+ last_lwsp = TRUE;
+ str_append_c(tmp, value[i]);
+ break;
+ default:
+ last_lwsp = FALSE;
+ str_append_c(tmp, value[i]);
+ break;
+ }
+ }
+ }
+
+ if ( uni_utf8_get_valid_data(str_data(tmp), str_len(tmp), str) )
+ str_append_str(str, tmp);
+
+ if (!literal)
+ str_append_c(str, '"');
+}
+
+char *managesieve_quote(pool_t pool, const unsigned char *value, size_t value_len)
+{
+ string_t *str;
+ char *ret;
+
+ if (value == NULL)
+ return "\"\"";
+
+ T_BEGIN {
+ str = t_str_new(value_len + MAX_INT_STRLEN + 5);
+ managesieve_quote_append(str, value, value_len, TRUE);
+ ret = p_strndup(pool, str_data(str), str_len(str));
+ } T_END;
+
+ return ret;
+}
diff --git a/pigeonhole/src/lib-managesieve/managesieve-quote.h b/pigeonhole/src/lib-managesieve/managesieve-quote.h
new file mode 100644
index 0000000..cae5491
--- /dev/null
+++ b/pigeonhole/src/lib-managesieve/managesieve-quote.h
@@ -0,0 +1,17 @@
+#ifndef IMAP_QUOTE_H
+#define IMAP_QUOTE_H
+
+/* Return value suitable for sending to client, either as quoted-string or
+ literal. Note that this also converts TABs into spaces, multiple spaces
+ into single space and NULs to #128. */
+char *managesieve_quote(pool_t pool, const unsigned char *value, size_t value_len);
+
+/* Append to existing string. */
+void managesieve_quote_append(string_t *str, const unsigned char *value,
+ size_t value_len, bool compress_lwsp);
+
+#define managesieve_quote_append_string(str, value, compress_lwsp) \
+ managesieve_quote_append(str, (const unsigned char *)(value), \
+ (size_t)-1, compress_lwsp)
+
+#endif