svctx.c
3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "sv.h"
#include <mailutils/attribute.h>
#include <mailutils/errno.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
const char *
sv_strerror (int e)
{
switch (e)
{
case SV_EPARSE:
return "parse failure";
}
return mu_errstring (e);
}
int
sv_interp_alloc (sv_interp_t * ip,
sv_parse_error_t pe,
sv_execute_error_t ee, sv_action_log_t al)
{
int rc = 0;
sv_interp_t i = calloc (1, sizeof (struct sv_interp_ctx_t));
if (!i)
return ENOMEM;
i->parse_error = pe;
i->execute_error = ee;
i->action_log = al;
if (sieve_interp_alloc (&i->interp, i) != SIEVE_OK)
{
free (i);
return ENOMEM;
}
if ((rc = sv_register_callbacks (i->interp)))
{
sieve_interp_free (&i->interp);
free (i);
return rc;
}
*ip = i;
return 0;
}
void
sv_interp_free (sv_interp_t * ip)
{
sv_interp_t i = *ip;
if (i)
{
if (i->interp)
sieve_interp_free (&i->interp);
free (i);
}
*ip = 0;
}
int
sv_script_parse (sv_script_t * sp, sv_interp_t i, const char *script)
{
FILE *f = 0;
sv_script_t s = 0;
int rc = 0;
if (!i || !script)
return EINVAL;
if ((s = calloc (1, sizeof (struct sv_script_ctx_t))) == 0)
return ENOMEM;
s->ic = i;
if ((s->file = strdup (script)) == 0)
{
free (s);
return ENOMEM;
}
f = fopen (script, "r");
if (!f)
{
free (s->file);
free (s);
return errno;
}
rc = sieve_script_parse (i->interp, f, s, &s->script);
fclose (f);
switch (rc)
{
case SIEVE_NOT_FINALIZED:
rc = EINVAL;
break;
case SIEVE_PARSE_ERROR:
rc = ENOEXEC;
break;
case SIEVE_NOMEM:
rc = ENOMEM;
break;
}
if (rc)
{
free (s->file);
free (s);
}
else
*sp = s;
return rc;
}
void
sv_script_free (sv_script_t * sp)
{
sv_script_t s = *sp;
if (s)
{
if (s->script)
sieve_script_free (&s->script);
free (s->file);
free (s);
}
*sp = 0;
}
int
sv_script_execute (sv_script_t script, message_t msg, ticket_t ticket,
mu_debug_t debug, mailer_t mailer, int svflags)
{
sv_msg_ctx_t mc = { 0, };
int rc;
int isdeleted;
attribute_t attr = 0;
rc = message_get_attribute (msg, &attr);
if(rc)
return rc;
isdeleted = attribute_is_deleted(attr);
mc.sc = script;
mc.msg = msg;
mc.ticket = ticket;
mc.debug = debug;
mc.mailer = mailer;
mc.svflags = svflags;
message_get_uid (msg, &mc.uid);
rc = sieve_execute_script (script->script, &mc);
sv_field_cache_release (&mc.cache);
if(rc && mc.rc)
rc = mc.rc;
/* Reset the deleted flag if the script failed. */
if(rc)
sv_mu_mark_deleted (msg, isdeleted);
return rc;
}