From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id KGMZAVAOJWFm4gAAgWs5BA (envelope-from ) for ; Tue, 24 Aug 2021 17:20:48 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id MO9mOE8OJWEpbQAAB5/wlQ (envelope-from ) for ; Tue, 24 Aug 2021 15:20:47 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (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 9443F844B for ; Tue, 24 Aug 2021 17:20:47 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 1A38E20522; Tue, 24 Aug 2021 11:20:25 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 7F4212050A for ; Tue, 24 Aug 2021 11:20:18 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id 6CF365FD5C; Tue, 24 Aug 2021 11:20:18 -0400 (EDT) Received: (nullmailer pid 2942889 invoked by uid 1000); Tue, 24 Aug 2021 15:17:52 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 30/36] lib/parse-sexp: support saved s-expression queries Date: Tue, 24 Aug 2021 08:17:39 -0700 Message-Id: <20210824151745.2941868-31-david@tethera.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210824151745.2941868-1-david@tethera.net> References: <20210824151745.2941868-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: 2RU5RUQK266LHVHQ5O7ZEEHWZJ62SHDE X-Message-ID-Hash: 2RU5RUQK266LHVHQ5O7ZEEHWZJ62SHDE 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=1629818447; 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=Ze9Z0r0LOLIcQ5wuhttdyzTc1DCNTa7VdvQgjMRHpoI=; b=jT7+EFo2E4aCDO2OkFO3hM5R5dhztfbGZkGIFlNoUV/v4Zurn3nEymoIV+hg/SSFOPyeeg 64Fhk6HC6yDk77yn8rnTg+WfPfJpohidR+EyLsyktZXGHw3E0d+Akqci4n0A7fr0x4B2hL zcxFC1aa8lj791aIS5FjBRGi9K5cTKm+VOe7LeUDv6UbKgO+B87huGqy5wENFCCKs6mprj JM2w4UaZbFe9NkjbRnarVgZz4jWH2X5IbX6lsFrPvJ0iuh7WwCMTWClRAkBX0gGRJwSTR/ 2uTsemaOjxF8AQh8J/BCbPIVjqtWtgkq+4beGBbXfhnXEpd27tb6kiZ3HYnIWQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1629818447; a=rsa-sha256; cv=none; b=KuzpNRMriZnJ9vb6W9AE6ycu3pHIhUP2rs5IuuwOW7SNr7LzlVDKtfH7lkXCMZcqntcrp0 0g1vA+sh7LRJ9XilmD3AhwTrhQiyD1gbLD0ArMntfIuQH8KsgLIQnX6Ly85E+WZdp+OBQr uPtYP/KitwrKrUurOzggEGZBx6e4veIjSEZ5rSA7QgCdIJkyEhh2DKhYXZeq9IwTAnWYT9 oEpDzuAumUfCqMoJ7KgMl7fVjBKAnSE6BzSdUT2pS8K4DwRDImQ4+7QRvvcknZTovcfDFN RFLFSeLMSOA6jTKFnsnNJVtC5C/SpNwA+eNF60XLujBL3M31gXu+MFIy3kq4FQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.00 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 9443F844B X-Spam-Score: -1.00 X-Migadu-Scanner: scn0.migadu.com X-TUID: 2TtW474stX6M 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 0fbb2afc..291480ca 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.32.0