fltchain.c
4.08 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2001, 2004-2005, 2007, 2009-2012, 2014-2015 Free
Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <mailutils/filter.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
static int
negate_filter_mode (int mode)
{
if (mode == MU_FILTER_DECODE)
return MU_FILTER_ENCODE;
else if (mode == MU_FILTER_ENCODE)
return MU_FILTER_DECODE;
abort ();
}
static int
_add_next_link (mu_stream_t *pret, mu_stream_t transport,
int defmode, int flags,
size_t argc, char **argv,
int (*pred) (void *, mu_stream_t, const char *),
void *closure)
{
int mode;
int qmark = 0;
char *fltname;
int status = 0;
fltname = argv[0];
if (fltname[0] == '?')
{
if (pred)
qmark = 1;
fltname++;
}
if (fltname[0] == '~')
{
mode = negate_filter_mode (defmode);
fltname++;
}
else
mode = defmode;
if (qmark == 0 || pred (closure, transport, fltname))
{
status = mu_filter_create_args (pret, transport, fltname,
argc, (const char **)argv,
mode, flags);
mu_stream_unref (transport);
}
return status;
}
int
_filter_chain_create (mu_stream_t *pret, mu_stream_t transport,
int defmode,
int flags,
size_t argc, char **argv,
int (*pred) (void *, mu_stream_t, const char *),
void *closure)
{
while (argc)
{
size_t i;
int status;
mu_stream_t stream;
for (i = 1; i < argc; i++)
if (strcmp (argv[i], "+") == 0)
break;
status = _add_next_link (&stream, transport,
defmode, flags,
i, argv,
pred, closure);
if (status)
return status;
transport = stream;
argc -= i;
argv += i;
if (argc)
{
argc--;
argv++;
}
}
*pret = transport;
return 0;
}
int
_filter_chain_create_rev (mu_stream_t *pret, mu_stream_t transport,
int defmode,
int flags,
size_t argc, char **argv,
int (*pred) (void *, mu_stream_t, const char *),
void *closure)
{
size_t pos;
for (pos = argc; pos > 0;)
{
size_t i;
int status;
mu_stream_t stream;
for (i = pos; i > 0; i--)
{
if (strcmp (argv[i - 1], "+") == 0)
break;
}
status = _add_next_link (&stream, transport,
defmode, flags,
pos - i, argv + i,
pred, closure);
if (status)
return status;
transport = stream;
if (i > 0)
i--;
pos = i;
}
*pret = transport;
return 0;
}
int
mu_filter_chain_create_pred (mu_stream_t *pret, mu_stream_t transport,
int defmode,
int flags,
size_t argc, char **argv,
int (*pred) (void *, mu_stream_t, const char *),
void *closure)
{
int rc;
mu_stream_ref (transport);
if (flags & MU_STREAM_WRITE)
rc = _filter_chain_create_rev (pret, transport,
defmode, flags,
argc, argv,
pred, closure);
else
rc = _filter_chain_create (pret, transport,
defmode, flags,
argc, argv, pred, closure);
if (rc)
mu_stream_unref (transport);
return rc;
}
int
mu_filter_chain_create (mu_stream_t *pret, mu_stream_t transport,
int defmode, int flags,
size_t argc, char **argv)
{
return mu_filter_chain_create_pred (pret, transport, defmode, flags,
argc, argv, NULL, NULL);
}