[PATCH] devel: add dkg's printmimestructure script to notmuch devel scripts
[notmuch-archives.git] / 39 / 16678c9f057e5bd96c902e113f249c1f1dec19
1 Return-Path: <bremner@tethera.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 olra.theworths.org (Postfix) with ESMTP id E6CDA429E26\r
6         for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:50 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] 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 A3Tmg-UfTR+I for <notmuch@notmuchmail.org>;\r
16         Sun, 18 Dec 2011 05:15:49 -0800 (PST)\r
17 Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])\r
18         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 5A65C429E32\r
21         for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:43 -0800 (PST)\r
22 Received: from zancas.localnet\r
23         (fctnnbsc36w-156034079193.pppoe-dynamic.High-Speed.nb.bellaliant.net\r
24         [156.34.79.193]) (authenticated bits=0)\r
25         by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBIDFdgE017881\r
26         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);\r
27         Sun, 18 Dec 2011 09:15:40 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.77)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1RcGaY-0008RS-S1; Sun, 18 Dec 2011 09:15:38 -0400\r
31 From: David Bremner <david@tethera.net>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH Draft 2 6/9] notmuch-restore: add --format=notmuch support\r
34 Date: Sun, 18 Dec 2011 09:15:08 -0400\r
35 Message-Id: <1324214111-32079-7-git-send-email-david@tethera.net>\r
36 X-Mailer: git-send-email 1.7.7.3\r
37 In-Reply-To: <1324214111-32079-1-git-send-email-david@tethera.net>\r
38 References: <id:87sjkmuck7.fsf@gmail.com>\r
39         <1324214111-32079-1-git-send-email-david@tethera.net>\r
40 Cc: David Bremner <bremner@debian.org>\r
41 X-BeenThere: notmuch@notmuchmail.org\r
42 X-Mailman-Version: 2.1.13\r
43 Precedence: list\r
44 List-Id: "Use and development of the notmuch mail system."\r
45         <notmuch.notmuchmail.org>\r
46 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
48 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
49 List-Post: <mailto:notmuch@notmuchmail.org>\r
50 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
51 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
53 X-List-Received-Date: Sun, 18 Dec 2011 13:15:51 -0000\r
54 \r
55 From: David Bremner <bremner@debian.org>\r
56 \r
57 This is format is whitespace separated tokens, encoded by\r
58 util/hex-escape.c\r
59 ---\r
60  notmuch-restore.c |   81 +++++++++++++++++++++++++++++++++++++++++------------\r
61  1 files changed, 63 insertions(+), 18 deletions(-)\r
62 \r
63 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
64 index 87d9772..f618b47 100644\r
65 --- a/notmuch-restore.c\r
66 +++ b/notmuch-restore.c\r
67 @@ -19,6 +19,7 @@\r
68   */\r
69  \r
70  #include "notmuch-client.h"\r
71 +#include "dump-restore-private.h"\r
72  \r
73  int\r
74  notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
75 @@ -35,6 +36,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
76      regex_t regex;\r
77      int rerr;\r
78      int opt_index;\r
79 +    int input_format = DUMP_FORMAT_SUP;\r
80  \r
81      config = notmuch_config_open (ctx, NULL, NULL);\r
82      if (config == NULL)\r
83 @@ -48,6 +50,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
84      synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
85  \r
86      notmuch_opt_desc_t options[] = {\r
87 +       FORMAT_DESC (input_format),\r
88         { NOTMUCH_OPT_POSITION, &input_file_name, 0, 0, 0 },\r
89         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
90         { 0, 0, 0, 0, 0 }\r
91 @@ -81,33 +84,63 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
92       * non-space characters for the message-id, then one or more\r
93       * spaces, then a list of space-separated tags as a sequence of\r
94       * characters within literal '(' and ')'. */\r
95 -    if ( xregcomp (&regex,\r
96 -                  "^([^ ]+) \\(([^)]*)\\)$",\r
97 -                  REG_EXTENDED) )\r
98 -       INTERNAL_ERROR("compile time constant regex failed.");\r
99 +    if (input_format == DUMP_FORMAT_SUP) {\r
100 +       if ( xregcomp (&regex,\r
101 +                      "^([^ ]+) \\(([^)]*)\\)$",\r
102 +                      REG_EXTENDED) )\r
103 +           INTERNAL_ERROR("compile time constant regex failed.");\r
104 +    }\r
105 +\r
106 +\r
107 +    /* These are out here to re-use the buffers with hex_decode */\r
108 +\r
109 +    char *message_id = NULL;\r
110 +    size_t message_id_size = 0;\r
111 +    char *tag = NULL;\r
112 +    size_t tag_size = 0;\r
113  \r
114      while ((line_len = getline (&line, &line_size, input)) != -1) {\r
115         regmatch_t match[3];\r
116 -       char *message_id, *file_tags, *tag, *next;\r
117 +       char  *file_tags, *next;\r
118         notmuch_message_t *message = NULL;\r
119 +\r
120         notmuch_status_t status;\r
121         notmuch_tags_t *db_tags;\r
122         char *db_tags_str;\r
123  \r
124         chomp_newline (line);\r
125  \r
126 -       rerr = xregexec (&regex, line, 3, match, 0);\r
127 -       if (rerr == REG_NOMATCH)\r
128 -       {\r
129 -           fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
130 -                    line);\r
131 +       /* Silently ignore blank lines */\r
132 +\r
133 +       if (line[0] == '\0') {\r
134             continue;\r
135         }\r
136  \r
137 -       message_id = xstrndup (line + match[1].rm_so,\r
138 -                              match[1].rm_eo - match[1].rm_so);\r
139 -       file_tags = xstrndup (line + match[2].rm_so,\r
140 -                             match[2].rm_eo - match[2].rm_so);\r
141 +       if (input_format == DUMP_FORMAT_SUP) {\r
142 +           rerr = xregexec (&regex, line, 3, match, 0);\r
143 +           if (rerr == REG_NOMATCH)\r
144 +           {\r
145 +               fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
146 +                        line);\r
147 +               continue;\r
148 +           }\r
149 +           message_id = talloc_strndup (notmuch, line + match[1].rm_so,\r
150 +                                        match[1].rm_eo - match[1].rm_so);\r
151 +           file_tags = talloc_strndup (notmuch, line + match[2].rm_so,\r
152 +                                       match[2].rm_eo - match[2].rm_so);\r
153 +       } else {\r
154 +           char *p = line;\r
155 +           char *raw_mid;\r
156 +\r
157 +           raw_mid = strsep (&p, " \t");\r
158 +\r
159 +           if (hex_decode (notmuch, raw_mid,\r
160 +                           &message_id, &message_id_size) != HEX_SUCCESS)\r
161 +               return 1;\r
162 +\r
163 +           file_tags = xstrdup (p);\r
164 +       }\r
165 +\r
166  \r
167         status = notmuch_database_find_message (notmuch, message_id, &message);\r
168         if (status || message == NULL) {\r
169 @@ -153,7 +186,16 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
170  \r
171         next = file_tags;\r
172         while (next) {\r
173 -           tag = strsep (&next, " ");\r
174 +           char *raw_tag = strsep (&next, " ");\r
175 +\r
176 +           if (input_format == DUMP_FORMAT_NOTMUCH) {\r
177 +               if (hex_decode (notmuch, raw_tag,\r
178 +                               &tag, &tag_size) != HEX_SUCCESS)\r
179 +                   return 1;\r
180 +           } else {\r
181 +               tag = talloc_strdup (notmuch, raw_tag);\r
182 +           }\r
183 +\r
184             if (*tag == '\0')\r
185                 continue;\r
186             status = notmuch_message_add_tag (message, tag);\r
187 @@ -175,11 +217,14 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
188         if (message)\r
189             notmuch_message_destroy (message);\r
190         message = NULL;\r
191 -       free (message_id);\r
192 -       free (file_tags);\r
193 +       if (input_format == DUMP_FORMAT_SUP) {\r
194 +           talloc_free (message_id);\r
195 +           talloc_free (file_tags);\r
196 +       }\r
197      }\r
198  \r
199 -    regfree (&regex);\r
200 +    if (input_format == DUMP_FORMAT_SUP)\r
201 +       regfree (&regex);\r
202  \r
203      if (line)\r
204         free (line);\r
205 -- \r
206 1.7.7.3\r
207 \r