1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 2006 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 "match.h"
25 
26 int	match_in_cache_match(struct mail_ctx *, struct expritem *);
27 void	match_in_cache_desc(struct expritem *, char *, size_t);
28 
29 struct match match_in_cache = {
30 	"in-cache",
31 	match_in_cache_match,
32 	match_in_cache_desc
33 };
34 
35 int
match_in_cache_match(struct mail_ctx * mctx,struct expritem * ei)36 match_in_cache_match(struct mail_ctx *mctx, struct expritem *ei)
37 {
38 	struct match_in_cache_data	*data = ei->data;
39 	struct account			*a = mctx->account;
40 	struct mail			*m = mctx->mail;
41 	char				*key;
42 	struct cache			*cache;
43 
44 	key = replacestr(&data->key, m->tags, m, &m->rml);
45 	if (key == NULL || *key == '\0') {
46 		log_warnx("%s: empty key", a->name);
47 		goto error;
48 	}
49 	log_debug2("%s: matching to cache %s: %s", a->name, data->path, key);
50 
51 	TAILQ_FOREACH(cache, &conf.caches, entry) {
52 		if (strcmp(data->path, cache->path) == 0) {
53 			if (open_cache(a, cache) != 0)
54 				goto error;
55 			if (db_contains(cache->db, key)) {
56 				xfree(key);
57 				return (MATCH_TRUE);
58 			}
59 			xfree(key);
60 			return (MATCH_FALSE);
61 		}
62 	}
63 	log_warnx("%s: cache %s not declared", a->name, data->path);
64 
65 error:
66 	if (key != NULL)
67 		xfree(key);
68 	return (MATCH_ERROR);
69 }
70 
71 void
match_in_cache_desc(struct expritem * ei,char * buf,size_t len)72 match_in_cache_desc(struct expritem *ei, char *buf, size_t len)
73 {
74 	struct match_in_cache_data	*data = ei->data;
75 
76 	xsnprintf(buf, len,
77 	    "in-cache \"%s\" key \"%s\"", data->path, data->key.str);
78 }
79