Commit 81750c9d 81750c9d313c09c0af1b5087bf2b0f584256d416 by Sergey Poznyakoff

Add tests for sieve environment. Initialize environment in maidag and inc

* sieve/tests/environment.at: New testcase.
* sieve/tests/Makefile.am: Add new file.
* sieve/tests/testsuite.at: Include new file.

* lib/muscript.h (mu_script_init): Change signature.
* lib/muscript_priv.h (mu_script_fun) <script_init>: Likewise.
* lib/guile.c (scheme_init): Update.
* lib/python.c (python_init): Update.
* lib/script.c (mu_script_init): Pass environment to the script_init
method.
* lib/sieve.c (sieve_init): Set sieve environment.
* maidag/script.c (apply_script): Set environment.
* mh/inc.c: Likewise.
1 parent 57e0b979
gint @ 42f47120
Subproject commit fd86bf7d44b0c970771830692ae7491447ebe8b1
Subproject commit 42f4712085b40173eaea58e14b1a579291a6fe3a
......
......@@ -25,7 +25,7 @@
static int initialized;
static int
scheme_init (const char *prog, mu_script_descr_t *pdescr)
scheme_init (const char *prog, const char **env, mu_script_descr_t *pdescr)
{
if (!initialized)
{
......
......@@ -27,7 +27,8 @@ typedef struct mu_script_descr *mu_script_descr_t;
mu_script_t mu_script_lang_handler (const char *lang);
mu_script_t mu_script_suffix_handler (const char *name);
int mu_script_init (mu_script_t scr, const char *name, mu_script_descr_t *);
int mu_script_init (mu_script_t scr, const char *name, const char **env,
mu_script_descr_t *);
int mu_script_done (mu_script_t, mu_script_descr_t);
int mu_script_process_msg (mu_script_t, mu_script_descr_t, mu_message_t msg);
void mu_script_log_enable (mu_script_t scr, mu_script_descr_t descr,
......
......@@ -2,7 +2,7 @@ struct mu_script_fun
{
char *lang;
char *suf;
int (*script_init) (const char *, mu_script_descr_t *);
int (*script_init) (const char *, const char **, mu_script_descr_t *);
int (*script_done) (mu_script_descr_t);
int (*script_process) (mu_script_descr_t, mu_message_t);
int (*script_log_enable) (mu_script_descr_t descr, const char *name,
......
......@@ -23,7 +23,7 @@
#include <string.h>
static int
python_init (const char *prog, mu_script_descr_t *pdescr)
python_init (const char *prog, const char **env, mu_script_descr_t *pdescr)
{
*pdescr = (mu_script_descr_t) strdup (prog);
if (!*pdescr)
......
......@@ -108,9 +108,10 @@ mu_script_suffix_handler (const char *name)
}
int
mu_script_init (mu_script_t scr, const char *name, mu_script_descr_t *pdescr)
mu_script_init (mu_script_t scr, const char *name, const char **env,
mu_script_descr_t *pdescr)
{
return scr->script_init ? scr->script_init (name, pdescr) : 0;
return scr->script_init ? scr->script_init (name, env, pdescr) : 0;
}
int
......
......@@ -75,8 +75,36 @@ _sieve_action_log (mu_sieve_machine_t mach,
mu_stream_unref (stream);
}
static void
sieve_setenv (mu_sieve_machine_t mach, const char **env)
{
if (env)
{
char *buffer = NULL;
size_t buflen = 0;
size_t i;
char *p;
for (i = 0; env[i]; i++)
{
if (buflen < strlen (env[i]) + 1)
{
buflen = strlen (env[i]) + 1;
buffer = mu_realloc (buffer, buflen);
}
strcpy (buffer, env[i]);
p = strchr (buffer, '=');
if (!p)
continue;
*p++ = 0;
mu_sieve_set_environ (mach, buffer, p);
}
free (buffer);
}
}
static int
sieve_init (const char *prog, mu_script_descr_t *pdescr)
sieve_init (const char *prog, const char **env, mu_script_descr_t *pdescr)
{
int rc;
mu_sieve_machine_t mach;
......@@ -86,6 +114,7 @@ sieve_init (const char *prog, mu_script_descr_t *pdescr)
{
if (mu_script_sieve_log)
mu_sieve_set_logger (mach, _sieve_action_log);
sieve_setenv (mach, env);
rc = mu_sieve_compile (mach, prog);
}
*pdescr = (mu_script_descr_t) mach;
......
......@@ -58,6 +58,8 @@ struct apply_script_closure
mu_message_t msg;
};
static char const *script_env[] = { "location=MDA", "phase=during", NULL };
static int
apply_script (void *item, void *data)
{
......@@ -67,7 +69,7 @@ apply_script (void *item, void *data)
int rc;
struct stat st;
mu_script_descr_t sd;
progfile = mu_expand_path_pattern (scr->pat, clos->auth->name);
if (stat (progfile, &st))
{
......@@ -79,7 +81,7 @@ apply_script (void *item, void *data)
return 0;
}
rc = mu_script_init (scr->scr, progfile, &sd);
rc = mu_script_init (scr->scr, progfile, script_env, &sd);
if (rc)
mu_error (_("initialization of script %s failed: %s"),
progfile, mu_strerror (rc));
......
......@@ -40,6 +40,7 @@ static const char *append_folder;
static const char *move_to_mailbox;
static const char *script_file;
static const char *script_lang;
static char const *script_env[] = { "location=MUA", "phase=post", NULL };
static void
add_file (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
......@@ -390,7 +391,8 @@ main (int argc, char **argv)
exit (1);
}
}
rc = mu_script_init (incdat.handler, script_file, &incdat.descr);
rc = mu_script_init (incdat.handler, script_file, script_env,
&incdat.descr);
if (rc)
{
mu_error (_("script initialization failed: %s"),
......
......@@ -49,6 +49,7 @@ TESTSUITE_AT = \
compile.at\
enc-char.at\
envelope.at\
environment.at\
exists.at\
ext.at\
false.at\
......
# This file is part of GNU Mailutils. -*- Autotest -*-
# Copyright (C) 2016 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/>.
AT_BANNER([environment])
m4_pushdef([MUT_SIEVE_OPTIONS],[--environment=location=MUA])
MUT_TESTCASE([match],[environment match],[
require "environment";
if environment "location" "MUA" {
discard;
}
],
[],[0],[],
[DISCARD on msg uid 1: marking as deleted
DISCARD on msg uid 2: marking as deleted
DISCARD on msg uid 3: marking as deleted
])
MUT_TESTCASE([no match], [environment unmatch],[
require "environment";
if environment "location" "MTA" {
discard;
}
],
[],[0],[],
[IMPLICIT KEEP on msg uid 1
IMPLICIT KEEP on msg uid 2
IMPLICIT KEEP on msg uid 3
])
m4_popdef([MUT_SIEVE_OPTIONS])
......@@ -150,3 +150,4 @@ m4_include([delheader.at])
m4_include([vacation.at])
m4_include([variables.at])
m4_include([environment.at])
......