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
1 Subproject commit fd86bf7d44b0c970771830692ae7491447ebe8b1 1 Subproject commit 42f4712085b40173eaea58e14b1a579291a6fe3a
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
25 static int initialized; 25 static int initialized;
26 26
27 static int 27 static int
28 scheme_init (const char *prog, mu_script_descr_t *pdescr) 28 scheme_init (const char *prog, const char **env, mu_script_descr_t *pdescr)
29 { 29 {
30 if (!initialized) 30 if (!initialized)
31 { 31 {
......
...@@ -27,7 +27,8 @@ typedef struct mu_script_descr *mu_script_descr_t; ...@@ -27,7 +27,8 @@ typedef struct mu_script_descr *mu_script_descr_t;
27 mu_script_t mu_script_lang_handler (const char *lang); 27 mu_script_t mu_script_lang_handler (const char *lang);
28 mu_script_t mu_script_suffix_handler (const char *name); 28 mu_script_t mu_script_suffix_handler (const char *name);
29 29
30 int mu_script_init (mu_script_t scr, const char *name, mu_script_descr_t *); 30 int mu_script_init (mu_script_t scr, const char *name, const char **env,
31 mu_script_descr_t *);
31 int mu_script_done (mu_script_t, mu_script_descr_t); 32 int mu_script_done (mu_script_t, mu_script_descr_t);
32 int mu_script_process_msg (mu_script_t, mu_script_descr_t, mu_message_t msg); 33 int mu_script_process_msg (mu_script_t, mu_script_descr_t, mu_message_t msg);
33 void mu_script_log_enable (mu_script_t scr, mu_script_descr_t descr, 34 void mu_script_log_enable (mu_script_t scr, mu_script_descr_t descr,
......
...@@ -2,7 +2,7 @@ struct mu_script_fun ...@@ -2,7 +2,7 @@ struct mu_script_fun
2 { 2 {
3 char *lang; 3 char *lang;
4 char *suf; 4 char *suf;
5 int (*script_init) (const char *, mu_script_descr_t *); 5 int (*script_init) (const char *, const char **, mu_script_descr_t *);
6 int (*script_done) (mu_script_descr_t); 6 int (*script_done) (mu_script_descr_t);
7 int (*script_process) (mu_script_descr_t, mu_message_t); 7 int (*script_process) (mu_script_descr_t, mu_message_t);
8 int (*script_log_enable) (mu_script_descr_t descr, const char *name, 8 int (*script_log_enable) (mu_script_descr_t descr, const char *name,
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static int 25 static int
26 python_init (const char *prog, mu_script_descr_t *pdescr) 26 python_init (const char *prog, const char **env, mu_script_descr_t *pdescr)
27 { 27 {
28 *pdescr = (mu_script_descr_t) strdup (prog); 28 *pdescr = (mu_script_descr_t) strdup (prog);
29 if (!*pdescr) 29 if (!*pdescr)
......
...@@ -108,9 +108,10 @@ mu_script_suffix_handler (const char *name) ...@@ -108,9 +108,10 @@ mu_script_suffix_handler (const char *name)
108 } 108 }
109 109
110 int 110 int
111 mu_script_init (mu_script_t scr, const char *name, mu_script_descr_t *pdescr) 111 mu_script_init (mu_script_t scr, const char *name, const char **env,
112 mu_script_descr_t *pdescr)
112 { 113 {
113 return scr->script_init ? scr->script_init (name, pdescr) : 0; 114 return scr->script_init ? scr->script_init (name, env, pdescr) : 0;
114 } 115 }
115 116
116 int 117 int
......
...@@ -75,8 +75,36 @@ _sieve_action_log (mu_sieve_machine_t mach, ...@@ -75,8 +75,36 @@ _sieve_action_log (mu_sieve_machine_t mach,
75 mu_stream_unref (stream); 75 mu_stream_unref (stream);
76 } 76 }
77 77
78 static void
79 sieve_setenv (mu_sieve_machine_t mach, const char **env)
80 {
81 if (env)
82 {
83 char *buffer = NULL;
84 size_t buflen = 0;
85 size_t i;
86 char *p;
87
88 for (i = 0; env[i]; i++)
89 {
90 if (buflen < strlen (env[i]) + 1)
91 {
92 buflen = strlen (env[i]) + 1;
93 buffer = mu_realloc (buffer, buflen);
94 }
95 strcpy (buffer, env[i]);
96 p = strchr (buffer, '=');
97 if (!p)
98 continue;
99 *p++ = 0;
100 mu_sieve_set_environ (mach, buffer, p);
101 }
102 free (buffer);
103 }
104 }
105
78 static int 106 static int
79 sieve_init (const char *prog, mu_script_descr_t *pdescr) 107 sieve_init (const char *prog, const char **env, mu_script_descr_t *pdescr)
80 { 108 {
81 int rc; 109 int rc;
82 mu_sieve_machine_t mach; 110 mu_sieve_machine_t mach;
...@@ -86,6 +114,7 @@ sieve_init (const char *prog, mu_script_descr_t *pdescr) ...@@ -86,6 +114,7 @@ sieve_init (const char *prog, mu_script_descr_t *pdescr)
86 { 114 {
87 if (mu_script_sieve_log) 115 if (mu_script_sieve_log)
88 mu_sieve_set_logger (mach, _sieve_action_log); 116 mu_sieve_set_logger (mach, _sieve_action_log);
117 sieve_setenv (mach, env);
89 rc = mu_sieve_compile (mach, prog); 118 rc = mu_sieve_compile (mach, prog);
90 } 119 }
91 *pdescr = (mu_script_descr_t) mach; 120 *pdescr = (mu_script_descr_t) mach;
......
...@@ -58,6 +58,8 @@ struct apply_script_closure ...@@ -58,6 +58,8 @@ struct apply_script_closure
58 mu_message_t msg; 58 mu_message_t msg;
59 }; 59 };
60 60
61 static char const *script_env[] = { "location=MDA", "phase=during", NULL };
62
61 static int 63 static int
62 apply_script (void *item, void *data) 64 apply_script (void *item, void *data)
63 { 65 {
...@@ -67,7 +69,7 @@ apply_script (void *item, void *data) ...@@ -67,7 +69,7 @@ apply_script (void *item, void *data)
67 int rc; 69 int rc;
68 struct stat st; 70 struct stat st;
69 mu_script_descr_t sd; 71 mu_script_descr_t sd;
70 72
71 progfile = mu_expand_path_pattern (scr->pat, clos->auth->name); 73 progfile = mu_expand_path_pattern (scr->pat, clos->auth->name);
72 if (stat (progfile, &st)) 74 if (stat (progfile, &st))
73 { 75 {
...@@ -79,7 +81,7 @@ apply_script (void *item, void *data) ...@@ -79,7 +81,7 @@ apply_script (void *item, void *data)
79 return 0; 81 return 0;
80 } 82 }
81 83
82 rc = mu_script_init (scr->scr, progfile, &sd); 84 rc = mu_script_init (scr->scr, progfile, script_env, &sd);
83 if (rc) 85 if (rc)
84 mu_error (_("initialization of script %s failed: %s"), 86 mu_error (_("initialization of script %s failed: %s"),
85 progfile, mu_strerror (rc)); 87 progfile, mu_strerror (rc));
......
...@@ -40,6 +40,7 @@ static const char *append_folder; ...@@ -40,6 +40,7 @@ static const char *append_folder;
40 static const char *move_to_mailbox; 40 static const char *move_to_mailbox;
41 static const char *script_file; 41 static const char *script_file;
42 static const char *script_lang; 42 static const char *script_lang;
43 static char const *script_env[] = { "location=MUA", "phase=post", NULL };
43 44
44 static void 45 static void
45 add_file (struct mu_parseopt *po, struct mu_option *opt, char const *arg) 46 add_file (struct mu_parseopt *po, struct mu_option *opt, char const *arg)
...@@ -390,7 +391,8 @@ main (int argc, char **argv) ...@@ -390,7 +391,8 @@ main (int argc, char **argv)
390 exit (1); 391 exit (1);
391 } 392 }
392 } 393 }
393 rc = mu_script_init (incdat.handler, script_file, &incdat.descr); 394 rc = mu_script_init (incdat.handler, script_file, script_env,
395 &incdat.descr);
394 if (rc) 396 if (rc)
395 { 397 {
396 mu_error (_("script initialization failed: %s"), 398 mu_error (_("script initialization failed: %s"),
......
...@@ -49,6 +49,7 @@ TESTSUITE_AT = \ ...@@ -49,6 +49,7 @@ TESTSUITE_AT = \
49 compile.at\ 49 compile.at\
50 enc-char.at\ 50 enc-char.at\
51 envelope.at\ 51 envelope.at\
52 environment.at\
52 exists.at\ 53 exists.at\
53 ext.at\ 54 ext.at\
54 false.at\ 55 false.at\
......
1 # This file is part of GNU Mailutils. -*- Autotest -*-
2 # Copyright (C) 2016 Free Software Foundation, Inc.
3 #
4 # GNU Mailutils is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3, or (at
7 # your option) any later version.
8 #
9 # GNU Mailutils is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
16
17 AT_BANNER([environment])
18
19 m4_pushdef([MUT_SIEVE_OPTIONS],[--environment=location=MUA])
20 MUT_TESTCASE([match],[environment match],[
21 require "environment";
22
23 if environment "location" "MUA" {
24 discard;
25 }
26 ],
27 [],[0],[],
28 [DISCARD on msg uid 1: marking as deleted
29 DISCARD on msg uid 2: marking as deleted
30 DISCARD on msg uid 3: marking as deleted
31 ])
32
33 MUT_TESTCASE([no match], [environment unmatch],[
34 require "environment";
35
36 if environment "location" "MTA" {
37 discard;
38 }
39 ],
40 [],[0],[],
41 [IMPLICIT KEEP on msg uid 1
42 IMPLICIT KEEP on msg uid 2
43 IMPLICIT KEEP on msg uid 3
44 ])
45 m4_popdef([MUT_SIEVE_OPTIONS])
46
...@@ -150,3 +150,4 @@ m4_include([delheader.at]) ...@@ -150,3 +150,4 @@ m4_include([delheader.at])
150 m4_include([vacation.at]) 150 m4_include([vacation.at])
151 151
152 m4_include([variables.at]) 152 m4_include([variables.at])
153 m4_include([environment.at])
......