summaryrefslogtreecommitdiff
path: root/libmu_sieve/extensions/editheader.c
blob: 52c745eb4423f0cb66194eb7fd038622ad96e264 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2012, 2014-2016 Free Software Foundation, Inc.

   GNU Mailutils 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, 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with GNU Mailutils.  If not, see
   <http://www.gnu.org/licenses/>. */

/* This module implements the Editheader Extension (RFC 5293) */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif  

#include <mailutils/types.h>
#include <mailutils/message.h>
#include <mailutils/header.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/sieve.h>

/* Syntax: addheader [:last] <field-name: string> <value: string>
 */
int
sieve_addheader (mu_sieve_machine_t mach)
{
  const char *field_name;
  const char *field_value;
  mu_message_t msg;
  mu_header_t hdr;
  int rc;
  
  mu_sieve_get_arg (mach, 0, SVT_STRING, &field_name);
  mu_sieve_get_arg (mach, 1, SVT_STRING, &field_value);

  mu_sieve_log_action (mach, "ADDHEADER", "%s: %s", field_name, field_value);

  if (mu_sieve_is_dry_run (mach))
    return 0;

  msg = mu_sieve_get_message (mach);
  rc = mu_message_get_header (msg, &hdr);
  if (rc)
    {
      mu_sieve_error (mach, "%lu: %s: %s",
		      (unsigned long) mu_sieve_get_message_num (mach),
		      _("cannot get message header"),
		      mu_strerror (rc));
      mu_sieve_abort (mach);
    }

  rc = (mu_sieve_get_tag (mach, "last", SVT_VOID, NULL) ?
	mu_header_append : mu_header_prepend) (hdr, field_name, field_value);
  if (rc)
    {
      mu_sieve_error (mach, "%lu: %s: %s",
		      (unsigned long) mu_sieve_get_message_num (mach),
		      _("cannot append message header"),
		      mu_strerror (rc));
      mu_sieve_abort (mach);
    }
  return 0;
}

/* Syntax: deleteheader [:index <fieldno: number> [:last]]
                        [COMPARATOR] [MATCH-TYPE]
                        <field-name: string>
                        [<value-patterns: string-list>]
 */
int
sieve_deleteheader (mu_sieve_machine_t mach)
{
  mu_sieve_value_t *val;
  const char *field_name;
  const char *field_pattern;
  mu_message_t msg;
  mu_header_t hdr;
  int rc;
  mu_sieve_comparator_t comp;
  mu_iterator_t itr;
  size_t i, idx = 0;
  
  mu_sieve_get_arg (mach, 0, SVT_STRING, &field_name);
  val = mu_sieve_get_arg_optional (mach, 1);
  if (!val)
    {
      field_pattern = NULL;
      mu_sieve_log_action (mach, "DELETEHEADER", "%s", field_name);
    }
  else
    {
      switch (val->type)
	{
	case SVT_STRING_LIST:
	  if (mu_list_get (val->v.list, 0, (void**)&field_pattern))
	    {
	      mu_sieve_error (mach, "%lu: %s",
			      (unsigned long) mu_sieve_get_message_num (mach),
			      _("cannot get list item"));
	      mu_sieve_abort (mach);
	    }
	  mu_sieve_log_action (mach, "DELETEHEADER", "%s: (regexp)",
			       field_name);
	  break;
	  
	case SVT_STRING:
	  field_pattern = val->v.string;
	  mu_sieve_log_action (mach, "DELETEHEADER", "%s: %s", field_name,
			       field_pattern);
	  break;

	default:
	  mu_sieve_error (mach, "%lu: %s: %d",
			  (unsigned long) mu_sieve_get_message_num (mach),
			  _("unexpected value type"), val->type);
	  mu_sieve_abort (mach);
	  
	}
    }
  
  if (mu_sieve_is_dry_run (mach))
    return 0;

  msg = mu_sieve_get_message (mach);
  rc = mu_message_get_header (msg, &hdr);
  if (rc)
    {
      mu_sieve_error (mach, "%lu: %s: %s",
		      (unsigned long) mu_sieve_get_message_num (mach),
		      _("cannot get message header"),
		      mu_strerror (rc));
      mu_sieve_abort (mach);
    }

  mu_header_get_iterator (hdr, &itr);
  if (mu_sieve_get_tag (mach, "last", SVT_VOID, NULL))
    {
      int backwards = 1;
      mu_iterator_ctl (itr, mu_itrctl_set_direction, &backwards);
    }
  comp = mu_sieve_get_comparator (mach);

  mu_sieve_get_tag (mach, "index", SVT_NUMBER, &idx);
  
  for (i = 0, mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      const char *fn, *fv;

      mu_iterator_current_kv (itr, (const void **)&fn, (void **)&fv);
      if (strcmp (field_name, fn))
	continue;
      if (idx && ++i < idx)
	continue;
	  
      if (field_pattern)
	{
	  if (comp (field_pattern, fv))
	    mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
	}
      else
	mu_iterator_ctl (itr, mu_itrctl_delete, NULL);

      if (idx)
	break;
    }
  mu_iterator_destroy (&itr);
  return 0;
}


/* addheader tagged arguments: */
static mu_sieve_tag_def_t addheader_tags[] = {
  { "last", SVT_VOID },
  { NULL }
};

static mu_sieve_tag_group_t addheader_tag_groups[] = {
  { addheader_tags, NULL },
  { NULL }
};

/* addheader required arguments: */
static mu_sieve_data_type addheader_args[] = {
  SVT_STRING,			/* field name */
  SVT_STRING,                   /* field value */ 
  SVT_VOID
};

#if 0
/* FIXME: The checker interface should be redone. Until then this function
   is commented out.  Problems:

   1. Checkers are called per group, there's no way to call them per tag.
   2. See FIXMEs in the code.
*/
static int
index_checker (const char *name, mu_list_t tags, mu_list_t args)
{
  mu_iterator_t itr;
  mu_sieve_runtime_tag_t *match = NULL;
  int err;
  
  if (!tags || mu_list_get_iterator (tags, &itr))
    return 0;

  err = 0;
  for (mu_iterator_first (itr); !err && !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      mu_sieve_runtime_tag_t *t;
      mu_iterator_current (itr, (void **)&t);
      
      if (strcmp (t->tag, "index") == 0)
	{
	  if (match)
	    {
	      /* FIXME: 1. This function must be public.
		        2. locus should be included in t
	      */
	      mu_sv_compile_error (&mu_sieve_locus, 
			      _("index specified twice in call to `%s'"),
				   name);
	      err = 1;
	      break;
	    }    
	}
    }

  mu_iterator_destroy (&itr);

  if (err)
    return 1;

  if (match)
    {
      if (match->arg->v.number < 1)
	{
	// See FIXME above 
	  mu_sv_compile_error (&mu_sieve_locus, 
			       _("invalid index value: %s"),
			       match->arg->v.string);
	  return 1;
	}
    }
  
  return 0;
}
#endif

static mu_sieve_tag_def_t match_part_tags[] = {
  { "is", SVT_VOID },
  { "contains", SVT_VOID },
  { "matches", SVT_VOID },
  { "regex", SVT_VOID },
  { "count", SVT_STRING },
  { "value", SVT_STRING },
  { "comparator", SVT_STRING },
  { NULL }
};

/* deleteheader tagged arguments: */
static mu_sieve_tag_def_t deleteheader_tags[] = {
  { "last", SVT_VOID },
  { "index", SVT_NUMBER },
  { NULL }
};

static mu_sieve_tag_group_t deleteheader_tag_groups[] = {
  { deleteheader_tags, NULL },
  { match_part_tags, mu_sieve_match_part_checker },
  { NULL }
};

/* deleteheader required arguments: */
static mu_sieve_data_type deleteheader_args[] = {
  SVT_STRING,			/* field name or value pattern */
  SVT_VOID
};

int
SIEVE_EXPORT (editheader, init) (mu_sieve_machine_t mach)
{
  int rc;

  /* This dummy record is required by libmu_sieve  */
  rc = mu_sieve_register_action (mach, "editheader", NULL, NULL, NULL, 1);
  if (rc)
    return rc;
  rc = mu_sieve_register_action (mach, "addheader", sieve_addheader,
				 addheader_args, addheader_tag_groups, 1);
  if (rc)
    return rc;
  rc = mu_sieve_register_action_ext (mach, "deleteheader", sieve_deleteheader,
				     deleteheader_args, deleteheader_args,
				     deleteheader_tag_groups,
				     1);
  if (rc)
    return rc;

  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.