From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id yAB+HeBVFWHRNwEAgWs5BA (envelope-from ) for ; Thu, 12 Aug 2021 19:09:52 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id yFkfGeBVFWElHgAAbx9fmQ (envelope-from ) for ; Thu, 12 Aug 2021 17:09:52 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 2632EBB4C for ; Thu, 12 Aug 2021 19:09:52 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 775C62BFF3; Thu, 12 Aug 2021 13:09:06 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id A490029213 for ; Thu, 12 Aug 2021 13:09:03 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id 9C40D5FD5C; Thu, 12 Aug 2021 13:09:03 -0400 (EDT) Received: (nullmailer pid 1348781 invoked by uid 1000); Thu, 12 Aug 2021 17:07:43 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 29/31] lib/parse-sexp: support saved s-expression queries Date: Thu, 12 Aug 2021 10:07:26 -0700 Message-Id: <20210812170728.1348333-30-david@tethera.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210812170728.1348333-1-david@tethera.net> References: <20210812170728.1348333-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: UETZYHGTPG75ACS4D5YWUYZDZI7YFEUX X-Message-ID-Hash: UETZYHGTPG75ACS4D5YWUYZDZI7YFEUX X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1628788192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=8LnrEug/NEFCkfcFEoxLqbgNMXNCzo0gBZKzjZ/oy+E=; b=A1WuxF/h9ah9kCJpUbtbOMzJEhy1Anemffrk1S6MGzIvreSaAO3y4oLi68fMtfGThyvANL lrm8sMOwbCJQmRRSX/PRKcqSE0ttMiI/zhTN8DpF0ilBaZt1/rIXC4Wxovm7jebRSPusk3 Nvof29H+dLHBNgA+cUOq0xz3g9GlkYBP22AxpHxaAQ8DJkQ5ZUfXk1Zd5SbJWeVutSw8eh ODJCV/gQYaRe5K7uOx/yh7+tfwBJcHW3XKJL4OFHEVzatsze9nqWYLBEao7NJ/k1b8CvS+ lMbSn/JpmxFxKC42UV4Yut0oHgdMhTRQIa3zCm0D3OgJ/Wmt6D9L8FuqF99H2Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1628788192; a=rsa-sha256; cv=none; b=vBkkpr9sF6Koq4oyvY4+kGtwpBAlg8V3Do7hNip9hBu8nYUV4OmfZIlijy4tq7zKKMMrg7 x8gValthLhxgJh12xbLMUeGX+tUbI2yvmXQRpJCXR8e27v/sRruL7RRLLZXpPJP61Jhsjp TUGPK+kuM9UeDoKI/KwYxGEThZGIlHuRWFl/O1szH9mebOe4qZVVS6aaDXvT4Rvx7DAucv Aw/oP4Jn8oQPFNmK60M8pVnYce6KxpHhvTVzaSVD0o64vEHe2tOokOI1hFp91hInjzmnU8 644kYKy+Zhkwhqx8kA3Q0XjKBY0RBf1UgoBA3yLoewNefI28doAZYtfRuAGQ0w== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: 0.56 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 2632EBB4C X-Spam-Score: 0.56 X-Migadu-Scanner: scn0.migadu.com X-TUID: L5d8cRoFFQ8t It turns out there is not really much code in query-fp.cc useful for supporting the new syntax. The code we could potentially factor out amounts to calling notmuch_database_get_config; both the key construction and the parsing of the results are specific to the query syntax involved. --- lib/parse-sexp.cc | 50 ++++++++++++++++++++++++++++++++++++++- test/T081-sexpr-search.sh | 32 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc index e582e350..53168d57 100644 --- a/lib/parse-sexp.cc +++ b/lib/parse-sexp.cc @@ -291,6 +291,49 @@ _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, sx->list->next, output); } +static notmuch_status_t +maybe_saved_squery (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx, + Xapian::Query &output) +{ + char *key; + char *expansion = NULL; + notmuch_status_t status; + sexp_t *saved_sexp; + void *local = talloc_new (notmuch); + char *buf; + + key = talloc_asprintf (local, "squery.%s", sx->list->val); + if (! key) { + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + goto DONE; + } + + status = notmuch_database_get_config (notmuch, key, &expansion); + if (status) + goto DONE; + if (EMPTY_STRING (expansion)) { + status = NOTMUCH_STATUS_IGNORED; + goto DONE; + } + + buf = talloc_strdup (local, expansion); + /* XXX TODO: free this memory */ + saved_sexp = parse_sexp (buf, strlen (expansion)); + if (! saved_sexp) { + _notmuch_database_log (notmuch, "invalid saved s-expression query: '%s'\n", expansion); + status = NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + goto DONE; + } + + status = _sexp_to_xapian_query (notmuch, parent, saved_sexp, output); + + DONE: + if (local) + talloc_free (local); + + return status; +} + /* Here we expect the s-expression to be a proper list, with first * element defining and operation, or as a special case the empty * list */ @@ -299,6 +342,8 @@ static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx, Xapian::Query &output) { + notmuch_status_t status; + if (sx->ty == SEXP_VALUE) { std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : ""; @@ -317,7 +362,6 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent Xapian::Query accumulator; for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) { if (prefix->flags & SEXP_FLAG_FIELD) { - notmuch_status_t status; Xapian::Query subquery; term_prefix = _notmuch_database_prefix (notmuch, prefix->name); status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery); @@ -343,6 +387,10 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } + status = maybe_saved_squery (notmuch, parent, sx, output); + if (status != NOTMUCH_STATUS_IGNORED) + return status; + /* Check for user defined field */ if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) { return _sexp_parse_header (notmuch, parent, sx, output); diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh index e0de0304..e8a77318 100755 --- a/test/T081-sexpr-search.sh +++ b/test/T081-sexpr-search.sh @@ -825,4 +825,36 @@ notmuch config set squery.Test '(subject override subject)' output=$(notmuch config get squery.Test) test_expect_equal "$output" '(subject override subject)' +test_begin_subtest "unknown saved query" +notmuch search --query=sexp '(Unknown foo bar)' >OUTPUT 2>&1 +cat < EXPECTED +notmuch search: Syntax error in query +unknown prefix 'Unknown' +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "syntax error in saved query" +notmuch config set squery.Bad '(Bad' +notmuch search --query=sexp '(Bad foo bar)' >OUTPUT 2>&1 +cat < EXPECTED +notmuch search: Syntax error in query +invalid saved s-expression query: '(Bad' +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "Saved Search by 'tag' and 'subject'" +notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED +notmuch config set squery.TagSubject '(and (tag inbox) (subject maildir))' +notmuch search --query=sexp '(TagSubject)' | notmuch_search_sanitize > OUTPUT +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "Saved Search: illegal nesting" +notmuch config set squery.TagSubject '(and (tag inbox) (subject maildir))' +notmuch search --query=sexp '(subject (TagSubject))' >OUTPUT 2>&1 +cat < EXPECTED +notmuch search: Syntax error in query +nested field: 'tag' inside 'subject' +EOF +test_expect_equal_file EXPECTED OUTPUT + test_done -- 2.30.2