Re: [feature request] emacs: use `notmuch insert` for FCC
[notmuch-archives.git] / ff / 4af82e1203af910880b3dad6fe78d2e166fa15
1 Return-Path: <dkg@fifthhorseman.net>\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 arlo.cworth.org (Postfix) with ESMTP id A76D66DE1BA2\r
6  for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:22 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12  autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id fyvCM6BOp48V for <notmuch@notmuchmail.org>;\r
16  Sun, 31 Jan 2016 12:40:20 -0800 (PST)\r
17 Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
18  by arlo.cworth.org (Postfix) with ESMTP id B1CBC6DE1AD2\r
19  for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:09 -0800 (PST)\r
20 Received: from fifthhorseman.net (ip-64-134-185-108.public.wayport.net\r
21  [64.134.185.108])\r
22  by che.mayfirst.org (Postfix) with ESMTPSA id F2200F9A5\r
23  for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 15:40:06 -0500 (EST)\r
24 Received: by fifthhorseman.net (Postfix, from userid 1000)\r
25  id 647F52119A; Sun, 31 Jan 2016 15:40:06 -0500 (EST)\r
26 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
27 To: Notmuch Mail <notmuch@notmuchmail.org>\r
28 Subject: [PATCH v3 15/16] added notmuch_message_reindex\r
29 Date: Sun, 31 Jan 2016 15:40:00 -0500\r
30 Message-Id: <1454272801-23623-16-git-send-email-dkg@fifthhorseman.net>\r
31 X-Mailer: git-send-email 2.7.0.rc3\r
32 In-Reply-To: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net>\r
33 References: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.20\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38  <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
40  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
45  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sun, 31 Jan 2016 20:40:22 -0000\r
47 \r
48 This new function asks the database to reindex a given message, using\r
49 the supplied indexopts.\r
50 \r
51 This can be used, for example, to index the cleartext of an encrypted\r
52 message.\r
53 ---\r
54  lib/message.cc | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-\r
55  lib/notmuch.h  | 14 +++++++++\r
56  2 files changed, 104 insertions(+), 1 deletion(-)\r
57 \r
58 diff --git a/lib/message.cc b/lib/message.cc\r
59 index 8d72ea2..3b35418 100644\r
60 --- a/lib/message.cc\r
61 +++ b/lib/message.cc\r
62 @@ -529,7 +529,9 @@ static void\r
63  _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
64  {\r
65      Xapian::TermIterator i;\r
66 -    size_t prefix_len = strlen (prefix);\r
67 +    size_t prefix_len = 0;\r
68 +\r
69 +    prefix_len = strlen (prefix);\r
70  \r
71      while (1) {\r
72         i = message->doc.termlist_begin ();\r
73 @@ -1667,3 +1669,90 @@ _notmuch_message_database (notmuch_message_t *message)\r
74  {\r
75      return message->notmuch;\r
76  }\r
77 +\r
78 +notmuch_status_t\r
79 +notmuch_message_reindex (notmuch_message_t *message,\r
80 +                        notmuch_indexopts_t *indexopts)\r
81 +{\r
82 +    notmuch_database_t *notmuch = NULL;\r
83 +    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, status;\r
84 +    notmuch_tags_t *tags = NULL;\r
85 +    notmuch_filenames_t *filenames, *orig_filenames = NULL;\r
86 +    const char *filename = NULL, *tag = NULL;\r
87 +    notmuch_message_t *newmsg = NULL;\r
88 +    notmuch_bool_t readded = FALSE, skip;\r
89 +    const char *autotags[] = {\r
90 +                   "attachment",\r
91 +                   "encrypted",\r
92 +                   "signed",\r
93 +                   "index-decrypted",\r
94 +                   "index-decryption-failed" };\r
95 +\r
96 +    if (message == NULL)\r
97 +       return NOTMUCH_STATUS_NULL_POINTER;\r
98 +    \r
99 +    notmuch = _notmuch_message_database (message);\r
100 +\r
101 +    /* cache tags and filenames */\r
102 +    tags = notmuch_message_get_tags(message);\r
103 +    filenames = notmuch_message_get_filenames(message);\r
104 +    orig_filenames = notmuch_message_get_filenames(message);\r
105 +    \r
106 +    /* walk through filenames, removing them until the message is gone */\r
107 +    for ( ; notmuch_filenames_valid (filenames);\r
108 +         notmuch_filenames_move_to_next (filenames)) {\r
109 +       filename = notmuch_filenames_get (filenames);\r
110 +\r
111 +       ret = notmuch_database_remove_message (notmuch, filename);\r
112 +       if (ret != NOTMUCH_STATUS_SUCCESS &&\r
113 +           ret != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
114 +           return ret;\r
115 +    }\r
116 +    if (ret != NOTMUCH_STATUS_SUCCESS)\r
117 +       return ret;\r
118 +    \r
119 +    /* re-add the filenames with the associated indexopts */\r
120 +    for (; notmuch_filenames_valid (orig_filenames);\r
121 +        notmuch_filenames_move_to_next (orig_filenames)) {\r
122 +       filename = notmuch_filenames_get (orig_filenames);\r
123 +\r
124 +       status = notmuch_database_add_message_with_indexopts(notmuch,\r
125 +                                                            filename,\r
126 +                                                            indexopts,\r
127 +                                                            readded ? NULL : &newmsg);\r
128 +       if (status == NOTMUCH_STATUS_SUCCESS ||\r
129 +           status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
130 +           if (!readded) {\r
131 +               /* re-add tags */\r
132 +               for (; notmuch_tags_valid (tags);\r
133 +                    notmuch_tags_move_to_next (tags)) {\r
134 +                   tag = notmuch_tags_get (tags);\r
135 +                   skip = FALSE;\r
136 +                   \r
137 +                   for (size_t i = 0; i < ARRAY_SIZE(autotags); i++)\r
138 +                       if (strcmp (tag, autotags[i]) == 0)\r
139 +                           skip = TRUE;\r
140 +                   \r
141 +                   if (!skip) {\r
142 +                       status = notmuch_message_add_tag (newmsg, tag);\r
143 +                       if (status != NOTMUCH_STATUS_SUCCESS)\r
144 +                           ret = status;\r
145 +                   }\r
146 +               }\r
147 +               readded = TRUE;\r
148 +           }\r
149 +       } else {\r
150 +           /* if we failed to add this filename, go ahead and try the\r
151 +            * next one as though it were first, but report the\r
152 +            * error... */\r
153 +           ret = status;\r
154 +       }\r
155 +    }\r
156 +    if (newmsg)\r
157 +       notmuch_message_destroy (newmsg);\r
158 +                       \r
159 +    /* should we also destroy the incoming message object?  at the\r
160 +     * moment, we leave that to the caller */\r
161 +    return ret;\r
162 +}\r
163 +\r
164 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
165 index 854a451..e6287cd 100644\r
166 --- a/lib/notmuch.h\r
167 +++ b/lib/notmuch.h\r
168 @@ -1377,6 +1377,20 @@ notmuch_filenames_t *\r
169  notmuch_message_get_filenames (notmuch_message_t *message);\r
170  \r
171  /**\r
172 + * Re-index the e-mail corresponding to 'message' using the supplied index options\r
173 + *\r
174 + * Returns the status of the re-index operation.  (see the return\r
175 + * codes documented in notmuch_database_add_message)\r
176 + *\r
177 + * After reindexing, the user should discard the message object passed\r
178 + * in here by calling notmuch_message_destroy, since it refers to the\r
179 + * original message, not to the reindexed message.\r
180 + */\r
181 +notmuch_status_t\r
182 +notmuch_message_reindex (notmuch_message_t *message,\r
183 +                        notmuch_indexopts_t *indexopts);\r
184 +\r
185 +/**\r
186   * Message flags.\r
187   */\r
188  typedef enum _notmuch_message_flag {\r
189 -- \r
190 2.7.0.rc3\r
191 \r