[PATCH v3 4/4] cli: refactor "notmuch restore" message tagging into a separate function
[notmuch-archives.git] / 50 / a7f8556b38b790bf89ab5a4556186981085d01
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 56BED431FAF\r
6         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:32 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id sKa5cwRN89ih for <notmuch@notmuchmail.org>;\r
17         Mon, 26 Mar 2012 14:04:30 -0700 (PDT)\r
18 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
19         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 51A6D431FBC\r
22         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:30 -0700 (PDT)\r
23 Received: by mail-lb0-f181.google.com with SMTP id k6so4671094lbo.26\r
24         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:29 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:x-gm-message-state;\r
29         bh=NbxBC2Q6wsUiVSAxHgQmlCZt5DEP72f9wsAhbaQEXU4=;\r
30         b=NUzT/SswmIKb1xfmTlGUjSwI8bxgFC5Gulz0/CettLlKNbi9YGNHGhaxqgveSo4Y4d\r
31         URZkCaSIWry+HNG4MH5km+BiekLR0RwjNJNMeZIo/ScZbkvMe9FH38LG6pTvzhHy/USe\r
32         OCaw74x0lSBCtnfYx56JwJRgE9gX+xnw9hpDKN0LGmkQtsqKn3mwttCeQ53j/RLNHYjh\r
33         fS0zsM87FyFfYUFi5o5dkaAz+HJgkjX4WXzVuEzroeMQEWpaCaZzpn29lu0f3Fro0fj8\r
34         7Ob85ZGXdwx8kI/T78NdEBfUsbtqCbfDERm7rj5YCu1GIkJMeqmHyVWDKx3dhMQ9cd8O\r
35         LaCQ==\r
36 Received: by 10.152.127.136 with SMTP id ng8mr17304075lab.16.1332795869902;\r
37         Mon, 26 Mar 2012 14:04:29 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
39         [84.248.80.253])\r
40         by mx.google.com with ESMTPS id cr11sm23301458lbb.0.2012.03.26.14.04.27\r
41         (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:28 -0700 (PDT)\r
42 From: Jani Nikula <jani@nikula.org>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH v3 4/4] cli: refactor "notmuch restore" message tagging into a\r
45         separate function\r
46 Date: Tue, 27 Mar 2012 00:04:11 +0300\r
47 Message-Id:\r
48  <62224861c399973dc60f33e616a61b0d641b0cab.1332795415.git.jani@nikula.org>\r
49 X-Mailer: git-send-email 1.7.5.4\r
50 In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
51 References: <cover.1332795415.git.jani@nikula.org>\r
52 In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
53 References: <cover.1332795415.git.jani@nikula.org>\r
54 X-Gm-Message-State:\r
55  ALoCoQlEAVp9ZAewx8KVONkZE1AhzFlvmaL2iqaYkxCagCdKR3lgDvg7hQmNDfvRfQPw3o4+/9wI\r
56 Cc: tomi.ollila@iki.fi\r
57 X-BeenThere: notmuch@notmuchmail.org\r
58 X-Mailman-Version: 2.1.13\r
59 Precedence: list\r
60 List-Id: "Use and development of the notmuch mail system."\r
61         <notmuch.notmuchmail.org>\r
62 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
63         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
64 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
65 List-Post: <mailto:notmuch@notmuchmail.org>\r
66 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
67 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
69 X-List-Received-Date: Mon, 26 Mar 2012 21:04:32 -0000\r
70 \r
71 Refactor to make tagging code easier to reuse in the future. No\r
72 functional changes.\r
73 \r
74 Signed-off-by: Jani Nikula <jani@nikula.org>\r
75 ---\r
76  notmuch-restore.c |  148 ++++++++++++++++++++++++++++-------------------------\r
77  1 files changed, 78 insertions(+), 70 deletions(-)\r
78 \r
79 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
80 index 87d9772..d3b9246 100644\r
81 --- a/notmuch-restore.c\r
82 +++ b/notmuch-restore.c\r
83 @@ -20,6 +20,81 @@\r
84  \r
85  #include "notmuch-client.h"\r
86  \r
87 +static int\r
88 +tag_message (notmuch_database_t *notmuch, const char *message_id,\r
89 +            char *file_tags, notmuch_bool_t remove_all,\r
90 +            notmuch_bool_t synchronize_flags)\r
91 +{\r
92 +    notmuch_status_t status;\r
93 +    notmuch_tags_t *db_tags;\r
94 +    char *db_tags_str;\r
95 +    notmuch_message_t *message = NULL;\r
96 +    const char *tag;\r
97 +    char *next;\r
98 +    int ret = 0;\r
99 +\r
100 +    status = notmuch_database_find_message (notmuch, message_id, &message);\r
101 +    if (status || message == NULL) {\r
102 +       fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",\r
103 +                message ? "" : "missing ", message_id);\r
104 +       if (status)\r
105 +           fprintf (stderr, "%s\n", notmuch_status_to_string(status));\r
106 +       return 1;\r
107 +    }\r
108 +\r
109 +    /* In order to detect missing messages, this check/optimization is\r
110 +     * intentionally done *after* first finding the message. */\r
111 +    if (!remove_all && (file_tags == NULL || *file_tags == '\0'))\r
112 +       goto DONE;\r
113 +\r
114 +    db_tags_str = NULL;\r
115 +    for (db_tags = notmuch_message_get_tags (message);\r
116 +        notmuch_tags_valid (db_tags);\r
117 +        notmuch_tags_move_to_next (db_tags)) {\r
118 +       tag = notmuch_tags_get (db_tags);\r
119 +\r
120 +       if (db_tags_str)\r
121 +           db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
122 +       else\r
123 +           db_tags_str = talloc_strdup (message, tag);\r
124 +    }\r
125 +\r
126 +    if (((file_tags == NULL || *file_tags == '\0') &&\r
127 +        (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
128 +       (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
129 +       goto DONE;\r
130 +\r
131 +    notmuch_message_freeze (message);\r
132 +\r
133 +    if (remove_all)\r
134 +       notmuch_message_remove_all_tags (message);\r
135 +\r
136 +    next = file_tags;\r
137 +    while (next) {\r
138 +       tag = strsep (&next, " ");\r
139 +       if (*tag == '\0')\r
140 +           continue;\r
141 +       status = notmuch_message_add_tag (message, tag);\r
142 +       if (status) {\r
143 +           fprintf (stderr, "Error applying tag %s to message %s:\n",\r
144 +                    tag, message_id);\r
145 +           fprintf (stderr, "%s\n", notmuch_status_to_string (status));\r
146 +           ret = 1;\r
147 +       }\r
148 +    }\r
149 +\r
150 +    notmuch_message_thaw (message);\r
151 +\r
152 +    if (synchronize_flags)\r
153 +       notmuch_message_tags_to_maildir_flags (message);\r
154 +\r
155 +DONE:\r
156 +    if (message)\r
157 +       notmuch_message_destroy (message);\r
158 +\r
159 +    return ret;\r
160 +}\r
161 +\r
162  int\r
163  notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
164  {\r
165 @@ -88,11 +163,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
166  \r
167      while ((line_len = getline (&line, &line_size, input)) != -1) {\r
168         regmatch_t match[3];\r
169 -       char *message_id, *file_tags, *tag, *next;\r
170 -       notmuch_message_t *message = NULL;\r
171 -       notmuch_status_t status;\r
172 -       notmuch_tags_t *db_tags;\r
173 -       char *db_tags_str;\r
174 +       char *message_id, *file_tags;\r
175  \r
176         chomp_newline (line);\r
177  \r
178 @@ -109,72 +180,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
179         file_tags = xstrndup (line + match[2].rm_so,\r
180                               match[2].rm_eo - match[2].rm_so);\r
181  \r
182 -       status = notmuch_database_find_message (notmuch, message_id, &message);\r
183 -       if (status || message == NULL) {\r
184 -           fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",\r
185 -                    message ? "" : "missing ", message_id);\r
186 -           if (status)\r
187 -               fprintf (stderr, "%s\n",\r
188 -                        notmuch_status_to_string(status));\r
189 -           goto NEXT_LINE;\r
190 -       }\r
191 -\r
192 -       /* In order to detect missing messages, this check/optimization is\r
193 -        * intentionally done *after* first finding the message.  */\r
194 -       if (accumulate && (file_tags == NULL || *file_tags == '\0'))\r
195 -       {\r
196 -           goto NEXT_LINE;\r
197 -       }\r
198 -\r
199 -       db_tags_str = NULL;\r
200 -       for (db_tags = notmuch_message_get_tags (message);\r
201 -            notmuch_tags_valid (db_tags);\r
202 -            notmuch_tags_move_to_next (db_tags))\r
203 -       {\r
204 -           const char *tag = notmuch_tags_get (db_tags);\r
205 -\r
206 -           if (db_tags_str)\r
207 -               db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
208 -           else\r
209 -               db_tags_str = talloc_strdup (message, tag);\r
210 -       }\r
211 -\r
212 -       if (((file_tags == NULL || *file_tags == '\0') &&\r
213 -            (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
214 -           (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
215 -       {\r
216 -           goto NEXT_LINE;\r
217 -       }\r
218 -\r
219 -       notmuch_message_freeze (message);\r
220 -\r
221 -       if (!accumulate)\r
222 -           notmuch_message_remove_all_tags (message);\r
223 -\r
224 -       next = file_tags;\r
225 -       while (next) {\r
226 -           tag = strsep (&next, " ");\r
227 -           if (*tag == '\0')\r
228 -               continue;\r
229 -           status = notmuch_message_add_tag (message, tag);\r
230 -           if (status) {\r
231 -               fprintf (stderr,\r
232 -                        "Error applying tag %s to message %s:\n",\r
233 -                        tag, message_id);\r
234 -               fprintf (stderr, "%s\n",\r
235 -                        notmuch_status_to_string (status));\r
236 -           }\r
237 -       }\r
238 -\r
239 -       notmuch_message_thaw (message);\r
240 -\r
241 -       if (synchronize_flags)\r
242 -           notmuch_message_tags_to_maildir_flags (message);\r
243 +       tag_message (notmuch, message_id, file_tags, !accumulate,\r
244 +                    synchronize_flags);\r
245  \r
246 -      NEXT_LINE:\r
247 -       if (message)\r
248 -           notmuch_message_destroy (message);\r
249 -       message = NULL;\r
250         free (message_id);\r
251         free (file_tags);\r
252      }\r
253 -- \r
254 1.7.5.4\r
255 \r