[PATCH 1/2] cli: add user address matching helpers for notmuch reply
[notmuch-archives.git] / 1d / fac32fbae5d86f5a4fffd37e1020b318a1a186
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id AADE5431FC2\r
6         for <notmuch@notmuchmail.org>; Fri, 11 May 2012 07:33:11 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id MG+GfKjqnVDV for <notmuch@notmuchmail.org>;\r
16         Fri, 11 May 2012 07:33:11 -0700 (PDT)\r
17 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
18         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id D8F01431FAE\r
21         for <notmuch@notmuchmail.org>; Fri, 11 May 2012 07:33:10 -0700 (PDT)\r
22 Received: by lbbgk8 with SMTP id gk8so2103376lbb.26\r
23         for <notmuch@notmuchmail.org>; Fri, 11 May 2012 07:33:09 -0700 (PDT)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=google.com; s=20120113;\r
26         h=from:to:subject:date:message-id:x-mailer:x-gm-message-state;\r
27         bh=M2f2d5cmAPw4V4gj0YK3vSVhSNrLGgKTEfzeTx+q9Eg=;\r
28         b=V8q+6g/Uik4CQwtaCnJJ9sR28ipd5JkP9n/U210+c2snAm0RzgcRdNVOVxiN38Hdz6\r
29         /BYqiVp0L7uEtBPj6NkwA1I+EV45Yns17pw8/ode9GdiupZrrt5veQfsETJJobq/5Thv\r
30         avHYUU0+n0ehia0x7Y2CdJMAWS5JIFY6k5dhVJTlByZxXB8t+NOlNQXy60i8s4b3tryD\r
31         q/5yHqHKGKIVTA0FG9AonbSl1m1EF8TcqojjgnSGPgiv2xr+85hB+1F4PL3aDbKgVU8a\r
32         lqegBJs+IfDUJR5QB55c9ftg9AS4nNa6TnpAofG/UqC41KOAQwleQIpTQ0W+X1RlHDKg\r
33         Bjlw==\r
34 Received: by 10.112.47.161 with SMTP id e1mr1913440lbn.42.1336746789256;\r
35         Fri, 11 May 2012 07:33:09 -0700 (PDT)\r
36 Received: from localhost (dsl-hkibrasgw4-fe50dc00-68.dhcp.inet.fi.\r
37         [80.220.80.68])\r
38         by mx.google.com with ESMTPS id pb13sm9590340lab.16.2012.05.11.07.33.07\r
39         (version=SSLv3 cipher=OTHER); Fri, 11 May 2012 07:33:08 -0700 (PDT)\r
40 From: Jani Nikula <jani@nikula.org>\r
41 To: notmuch@notmuchmail.org\r
42 Subject: [PATCH 1/2] cli: add user address matching helpers for notmuch reply\r
43 Date: Fri, 11 May 2012 17:33:04 +0300\r
44 Message-Id:\r
45  <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
46 X-Mailer: git-send-email 1.7.9.5\r
47 X-Gm-Message-State:\r
48  ALoCoQnRNmuvxBP3pSN8oQ7osmZ/MqoMGaSLF7eHMwdAgCVGtQw3fjOBYE+8rx6BepcrAheO9ZG/\r
49 X-BeenThere: notmuch@notmuchmail.org\r
50 X-Mailman-Version: 2.1.13\r
51 Precedence: list\r
52 List-Id: "Use and development of the notmuch mail system."\r
53         <notmuch.notmuchmail.org>\r
54 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
56 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
57 List-Post: <mailto:notmuch@notmuchmail.org>\r
58 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
59 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
60         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
61 X-List-Received-Date: Fri, 11 May 2012 14:33:11 -0000\r
62 \r
63 Add a multi-purpose address_match() function for matching strings\r
64 against user's configured primary and other email addresses. Add thin\r
65 wrappers user_address_in_string() and string_in_user_address() for\r
66 ease of use, and also convert existing address_is_users() to wrapper\r
67 for the same.\r
68 \r
69 No functional changes.\r
70 \r
71 Signed-off-by: Jani Nikula <jani@nikula.org>\r
72 ---\r
73  notmuch-reply.c |   72 +++++++++++++++++++++++++++++++++++++++++++++++--------\r
74  1 file changed, 62 insertions(+), 10 deletions(-)\r
75 \r
76 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
77 index 7184a5d..0c82755 100644\r
78 --- a/notmuch-reply.c\r
79 +++ b/notmuch-reply.c\r
80 @@ -98,25 +98,77 @@ format_part_reply (mime_node_t *node)\r
81         format_part_reply (mime_node_child (node, i));\r
82  }\r
83  \r
84 -/* Is the given address configured as one of the user's "personal" or\r
85 - * "other" addresses. */\r
86 -static int\r
87 -address_is_users (const char *address, notmuch_config_t *config)\r
88 +typedef enum {\r
89 +    USER_ADDRESS_IN_STRING,\r
90 +    STRING_IN_USER_ADDRESS,\r
91 +    STRING_IS_USER_ADDRESS,\r
92 +} address_match_t;\r
93 +\r
94 +/* Match given string against given address according to mode. */\r
95 +static notmuch_bool_t\r
96 +match_address (const char *str, const char *address, address_match_t mode)\r
97 +{\r
98 +    switch (mode) {\r
99 +    case USER_ADDRESS_IN_STRING:\r
100 +       return strcasestr (str, address) != NULL;\r
101 +    case STRING_IN_USER_ADDRESS:\r
102 +       return strcasestr (address, str) != NULL;\r
103 +    case STRING_IS_USER_ADDRESS:\r
104 +       return strcasecmp (address, str) == 0;\r
105 +    }\r
106 +\r
107 +    return FALSE;\r
108 +}\r
109 +\r
110 +/* Match given string against user's configured "primary" and "other"\r
111 + * addresses according to mode. */\r
112 +static const char *\r
113 +address_match (const char *str, notmuch_config_t *config, address_match_t mode)\r
114  {\r
115      const char *primary;\r
116      const char **other;\r
117      size_t i, other_len;\r
118  \r
119 +    if (!str || *str == '\0')\r
120 +       return NULL;\r
121 +\r
122      primary = notmuch_config_get_user_primary_email (config);\r
123 -    if (strcasecmp (primary, address) == 0)\r
124 -       return 1;\r
125 +    if (match_address (str, primary, mode))\r
126 +       return primary;\r
127  \r
128      other = notmuch_config_get_user_other_email (config, &other_len);\r
129 -    for (i = 0; i < other_len; i++)\r
130 -       if (strcasecmp (other[i], address) == 0)\r
131 -           return 1;\r
132 +    for (i = 0; i < other_len; i++) {\r
133 +       if (match_address (str, other[i], mode))\r
134 +           return other[i];\r
135 +    }\r
136  \r
137 -    return 0;\r
138 +    return NULL;\r
139 +}\r
140 +\r
141 +/* Does the given string contain an address configured as one of the\r
142 + * user's "primary" or "other" addresses. If so, return the matching\r
143 + * address, NULL otherwise. */\r
144 +static const char *\r
145 +user_address_in_string (const char *str, notmuch_config_t *config)\r
146 +{\r
147 +    return address_match (str, config, USER_ADDRESS_IN_STRING);\r
148 +}\r
149 +\r
150 +/* Do any of the addresses configured as one of the user's "primary"\r
151 + * or "other" addresses contain the given string. If so, return the\r
152 + * matching address, NULL otherwise. */\r
153 +static const char *\r
154 +string_in_user_address (const char *str, notmuch_config_t *config)\r
155 +{\r
156 +    return address_match (str, config, STRING_IN_USER_ADDRESS);\r
157 +}\r
158 +\r
159 +/* Is the given address configured as one of the user's "primary" or\r
160 + * "other" addresses. */\r
161 +static notmuch_bool_t\r
162 +address_is_users (const char *address, notmuch_config_t *config)\r
163 +{\r
164 +    return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;\r
165  }\r
166  \r
167  /* Scan addresses in 'list'.\r
168 -- \r
169 1.7.9.5\r
170 \r