--- /dev/null
+Return-Path: <bremner@tethera.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id BA4AF431FC0\r
+ for <notmuch@notmuchmail.org>; Sun, 23 Dec 2012 17:40:09 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id Kdjwz4+74QB2 for <notmuch@notmuchmail.org>;\r
+ Sun, 23 Dec 2012 17:40:07 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
+ (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 86280431FC2\r
+ for <notmuch@notmuchmail.org>; Sun, 23 Dec 2012 17:40:02 -0800 (PST)\r
+Received: from fctnnbsc30w-156034082078.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+ ([156.34.82.78] helo=zancas.localnet)\r
+ by tesseract.cs.unb.ca with esmtpsa\r
+ (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1Tmx1M-0008Kq-Ay; Sun, 23 Dec 2012 21:40:00 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1Tmx1G-0002nl-NS; Sun, 23 Dec 2012 21:39:54 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v9 07/17] notmuch-restore: move query handling for batch\r
+ restore to parser\r
+Date: Sun, 23 Dec 2012 21:39:33 -0400\r
+Message-Id: <1356313183-9266-8-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1356313183-9266-1-git-send-email-david@tethera.net>\r
+References: <1356313183-9266-1-git-send-email-david@tethera.net>\r
+X-Spam_bar: -\r
+Cc: David Bremner <bremner@debian.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 24 Dec 2012 01:40:10 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+We are able to detect more errors by looking at the string before it\r
+is hex-decoded. We also need this to avoid the query quoting for more\r
+general queries (to be written) that will mess up raw message-ids.\r
+---\r
+ notmuch-restore.c | 19 ++-----------------\r
+ tag-util.c | 26 ++++++++++++++++++++------\r
+ tag-util.h | 7 ++++++-\r
+ test/dump-restore | 5 ++---\r
+ 4 files changed, 30 insertions(+), 27 deletions(-)\r
+\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 9ed9b51..8a885de 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -216,24 +216,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ if (input_format == DUMP_FORMAT_SUP) {\r
+ ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);\r
+ } else {\r
+- ret = parse_tag_line (line_ctx, line, TAG_FLAG_BE_GENEROUS,\r
++ ret = parse_tag_line (line_ctx, line,\r
++ TAG_FLAG_BE_GENEROUS | TAG_FLAG_ID_DIRECT,\r
+ &query_string, tag_ops);\r
+-\r
+- if (ret == 0) {\r
+- if (strncmp ("id:", query_string, 3) != 0) {\r
+- fprintf (stderr, "Warning: unsupported query: %s\n", query_string);\r
+- continue;\r
+- }\r
+- /* delete id: from front of string; tag_message\r
+- * expects a raw message-id.\r
+- *\r
+- * XXX: Note that query string id:foo and bar will be\r
+- * interpreted as a message id "foo and bar". This\r
+- * should eventually be fixed to give a better error\r
+- * message.\r
+- */\r
+- query_string = query_string + 3;\r
+- }\r
+ }\r
+ \r
+ if (ret > 0)\r
+diff --git a/tag-util.c b/tag-util.c\r
+index b9b6099..277eb45 100644\r
+--- a/tag-util.c\r
++++ b/tag-util.c\r
+@@ -245,14 +245,28 @@ parse_tag_line (void *ctx, char *line,\r
+ }\r
+ \r
+ /* tok now points to the query string */\r
+- if (hex_decode_inplace (tok) != HEX_SUCCESS) {\r
+- ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
+- "hex decoding of query %s failed", tok);\r
+- goto DONE;\r
++ if (flags & TAG_FLAG_ID_DIRECT) {\r
++ /* this is under the assumption that any whitespace in the\r
++ * message-id must be hex-encoded. The check is probably not\r
++ * perfect for exotic unicode whitespace; as fallback the\r
++ * search for strange message-ids will fail */\r
++ if ((strncmp ("id:", tok, 3) != 0) ||\r
++ (strcspn (tok, " \t") < strlen (tok))) {\r
++ ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++ "query '%s' is not 'id:<message-id>'", tok);\r
++ goto DONE;\r
++ }\r
++ if (hex_decode_inplace (tok) != HEX_SUCCESS) {\r
++ ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
++ "hex decoding of query %s failed", tok);\r
++ goto DONE;\r
++ }\r
++ /* skip 'id:' */\r
++ *query_string = tok + 3;\r
++ } else {\r
++ ret = unhex_and_quote (ctx, tok, line_for_error, query_string);\r
+ }\r
+ \r
+- *query_string = tok;\r
+-\r
+ DONE:\r
+ talloc_free (line_for_error);\r
+ return ret;\r
+diff --git a/tag-util.h b/tag-util.h\r
+index 246de85..e5c7a1f 100644\r
+--- a/tag-util.h\r
++++ b/tag-util.h\r
+@@ -26,7 +26,12 @@ typedef enum {\r
+ /* Accept strange tags that might be user error;\r
+ * intended for use by notmuch-restore.\r
+ */\r
+- TAG_FLAG_BE_GENEROUS = (1 << 3)\r
++ TAG_FLAG_BE_GENEROUS = (1 << 3),\r
++\r
++ /* Directly look up messages by hex-decoded message-id, rather\r
++ * than parsing a general query. The query MUST be of the form\r
++ * id:$message-id. */\r
++ TAG_FLAG_ID_DIRECT = (1 << 4)\r
+ \r
+ } tag_op_flag_t;\r
+ \r
+diff --git a/test/dump-restore b/test/dump-restore\r
+index 6a989b6..eb7933a 100755\r
+--- a/test/dump-restore\r
++++ b/test/dump-restore\r
+@@ -199,19 +199,18 @@ a\r
+ # the next non-comment line should report an an empty tag error for\r
+ # batch tagging, but not for restore\r
+ + +e -- id:20091117232137.GA7669@griffis1.net\r
+-# highlight the sketchy id parsing; this should be last\r
+ +g -- id:foo and bar\r
+ EOF\r
+ \r
+ cat <<EOF > EXPECTED\r
+-Warning: unsupported query: a\r
++Warning: query 'a' is not 'id:<message-id>' [a]\r
+ Warning: no query string [+0]\r
+ Warning: no query string [+a +b]\r
+ Warning: missing query string [+a +b ]\r
+ Warning: no query string after -- [+c +d --]\r
+ Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]\r
+ Warning: hex decoding of query id:%yy failed [+e +f id:%yy]\r
+-Warning: cannot apply tags to missing message: foo and bar\r
++Warning: query 'id:foo and bar' is not 'id:<message-id>' [+g -- id:foo and bar]\r
+ EOF\r
+ \r
+ test_expect_equal_file EXPECTED OUTPUT\r
+-- \r
+1.7.10.4\r
+\r