1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <string.h>
22 
23 #include "fdm.h"
24 #include "deliver.h"
25 
26 int	 deliver_add_header_deliver(struct deliver_ctx *, struct actitem *);
27 void	 deliver_add_header_desc(struct actitem *, char *, size_t);
28 
29 struct deliver deliver_add_header = {
30 	"add-header",
31 	DELIVER_INCHILD,
32 	deliver_add_header_deliver,
33 	deliver_add_header_desc
34 };
35 
36 int
deliver_add_header_deliver(struct deliver_ctx * dctx,struct actitem * ti)37 deliver_add_header_deliver(struct deliver_ctx *dctx, struct actitem *ti)
38 {
39 	struct account			*a = dctx->account;
40 	struct mail			*m = dctx->mail;
41 	struct deliver_add_header_data	*data = ti->data;
42 	char				*hdr, *value = NULL;
43 
44 	hdr = replacestr(&data->hdr, m->tags, m, &m->rml);
45 	if (hdr == NULL || *hdr == '\0') {
46 		log_warnx("%s: empty header", a->name);
47 		goto error;
48 	}
49 	value = replacestr(&data->value, m->tags, m, &m->rml);
50 	if (value == NULL) {
51 		log_warnx("%s: bad value for header %s", a->name, hdr);
52 		goto error;
53 	}
54 	log_debug2("%s: adding header: %s", a->name, hdr);
55 
56 	if (insert_header(m, NULL, "%s: %s", hdr, value) != 0) {
57 		log_warnx("%s: failed to add header %s (%s)", a->name,
58 		    hdr, value);
59 		goto error;
60 	}
61 
62 	ARRAY_FREE(&m->wrapped);
63 	m->wrapchar = '\0';
64 	fill_wrapped(m);
65 
66 	/* Invalidate the match data since stuff may have moved. */
67 	m->rml.valid = 0;
68 
69 	xfree(hdr);
70 	xfree(value);
71 	return (DELIVER_SUCCESS);
72 
73 error:
74 	if (hdr != NULL)
75 		xfree(hdr);
76 	if (value != NULL)
77 		xfree(value);
78 	return (DELIVER_FAILURE);
79 }
80 
81 void
deliver_add_header_desc(struct actitem * ti,char * buf,size_t len)82 deliver_add_header_desc(struct actitem *ti, char *buf, size_t len)
83 {
84 	struct deliver_add_header_data	*data = ti->data;
85 
86 	xsnprintf(buf, len,
87 	    "add-header \"%s\" value \"%s\"", data->hdr.str, data->value.str);
88 }
89