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 <fnmatch.h>
22 #include <string.h>
23
24 #include "fdm.h"
25 #include "match.h"
26
27 int match_account_match(struct mail_ctx *, struct expritem *);
28 void match_account_desc(struct expritem *, char *, size_t);
29
30 struct match match_account = {
31 "account",
32 match_account_match,
33 match_account_desc
34 };
35
36 int
match_account_match(struct mail_ctx * mctx,struct expritem * ei)37 match_account_match(struct mail_ctx *mctx, struct expritem *ei)
38 {
39 struct match_account_data *data = ei->data;
40 struct account *a = mctx->account;
41 struct mail *m = mctx->mail;
42 char *s;
43 u_int i;
44
45 for (i = 0; i < ARRAY_LENGTH(data->accounts); i++) {
46 s = replacestr(
47 &ARRAY_ITEM(data->accounts, i), m->tags, m, &m->rml);
48 if (s == NULL || *s == '\0') {
49 if (s != NULL)
50 xfree(s);
51 log_warnx("%s: empty account", a->name);
52 return (MATCH_ERROR);
53 }
54 if (account_match(s, a->name)) {
55 xfree(s);
56 return (MATCH_TRUE);
57 }
58 xfree(s);
59 }
60
61 return (MATCH_FALSE);
62 }
63
64 void
match_account_desc(struct expritem * ei,char * buf,size_t len)65 match_account_desc(struct expritem *ei, char *buf, size_t len)
66 {
67 struct match_account_data *data = ei->data;
68 char *accounts;
69
70 accounts = fmt_replstrs("account ", data->accounts);
71 strlcpy(buf, accounts, len);
72 xfree(accounts);
73 }
74