From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id D6D9C431FBC for ; Wed, 13 Jan 2010 02:24:26 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.601 X-Spam-Level: X-Spam-Status: No, score=-0.601 tagged_above=-999 required=5 tests=[AWL=1.379, BAYES_00=-2.599, RCVD_IN_SORBS_WEB=0.619] autolearn=no Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2Ji5myKUjyvr for ; Wed, 13 Jan 2010 02:24:26 -0800 (PST) Received: from mail-fx0-f217.google.com (mail-fx0-f217.google.com [209.85.220.217]) by olra.theworths.org (Postfix) with ESMTP id F2FA5431FAE for ; Wed, 13 Jan 2010 02:24:25 -0800 (PST) Received: by fxm9 with SMTP id 9so20655115fxm.30 for ; Wed, 13 Jan 2010 02:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:from:to:subject:date :message-id:x-mailer:organization; bh=ael2cZISmH3AmFN3o8pwRrbpF3oz8wB5dBirFauuZhI=; b=MnQsZvt0hxH3JDOxocbtXbLs6BdN0BIXxGrGhxxtMJskDnwBPZH8oFT4l10qN9IoHq g8YY141n9/tRbiMfaBMX6RWwgRF3YCnwwtSdjp2yzheDHpvM+v+fNa/o2wnNMCLLwSmy uHH+txSzV322yH3bkGdtQ10gYmfm08WnashKw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:organization; b=wsCphEo6hRuc6IeY/vLX8xg1GTfWNOMF4sd57OK4EQzbA94k00yZNWJJMdOsKWWDTh UbutjFn0IbUYSLSGvj+od6dJYZmZ35zYnW5br+sOmc5m/Vuc0Q6ouhExM0/Lh0PIMRWc PY/4wneGchqNJhkRJgsZ6cZO5S9DYrkSX1JbQ= Received: by 10.87.70.26 with SMTP id x26mr766629fgk.57.1263378264591; Wed, 13 Jan 2010 02:24:24 -0800 (PST) Received: from harikalardiyari ([78.179.54.193]) by mx.google.com with ESMTPS id l19sm3526274fgb.3.2010.01.13.02.24.23 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 13 Jan 2010 02:24:24 -0800 (PST) Sender: Ali Polatel From: Ali Polatel To: notmuch@notmuchmail.org Date: Wed, 13 Jan 2010 12:24:22 +0200 Message-Id: X-Mailer: git-send-email 1.6.6 Organization: Pink Floyd Subject: [RFC/PATCH] Add search-files command X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jan 2010 10:24:27 -0000 This command can be used to integrate notmuch with other MUAs as a searching client. The idea is simple, a simple script could get search-terms as argument and create a "virtual" maildir which has symbolic links to files output by search-files command. This is similar to nmzmail. --- Makefile.local | 1 + notmuch-client.h | 3 + notmuch-search-files.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ notmuch.c | 13 ++++++ 4 files changed, 124 insertions(+), 0 deletions(-) create mode 100644 notmuch-search-files.c diff --git a/Makefile.local b/Makefile.local index 933ff4c..78bc25d 100644 --- a/Makefile.local +++ b/Makefile.local @@ -12,6 +12,7 @@ notmuch_client_srcs = \ notmuch-reply.c \ notmuch-restore.c \ notmuch-search.c \ + notmuch-search-files.c \ notmuch-search-tags.c \ notmuch-setup.c \ notmuch-show.c \ diff --git a/notmuch-client.h b/notmuch-client.h index 77766de..d505d30 100644 --- a/notmuch-client.h +++ b/notmuch-client.h @@ -96,6 +96,9 @@ int notmuch_search_command (void *ctx, int argc, char *argv[]); int +notmuch_search_files_command (void *ctx, int argc, char *argv[]); + +int notmuch_setup_command (void *ctx, int argc, char *argv[]); int diff --git a/notmuch-search-files.c b/notmuch-search-files.c new file mode 100644 index 0000000..b48783a --- /dev/null +++ b/notmuch-search-files.c @@ -0,0 +1,107 @@ +/* notmuch - Not much of an email program, (just index and search) + * + * Copyright © 2009 Carl Worth + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/ . + * + * Author: Ali Polatel + */ + +#include "notmuch-client.h" + +static void +do_search_files (notmuch_query_t *query) +{ + notmuch_message_t *message; + notmuch_messages_t *messages; + + for (messages = notmuch_query_search_messages (query); + notmuch_messages_has_more (messages); + notmuch_messages_advance (messages)) + { + message = notmuch_messages_get (messages); + printf ("%s\n", notmuch_message_get_filename (message)); + notmuch_message_destroy(message); + } +} + +int +notmuch_search_files_command (void *ctx, int argc, char *argv[]) +{ + notmuch_config_t *config; + notmuch_database_t *notmuch; + notmuch_query_t *query; + char *query_str; + char *opt; + notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST; + int i; + + for (i = 0; i < argc && argv[i][0] == '-'; i++) { + if (strcmp (argv[i], "--") == 0) { + i++; + break; + } + if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) { + opt = argv[i] + sizeof ("--sort=") - 1; + if (strcmp (opt, "oldest-first") == 0) { + sort = NOTMUCH_SORT_OLDEST_FIRST; + } else if (strcmp (opt, "newest-first") == 0) { + sort = NOTMUCH_SORT_NEWEST_FIRST; + } else { + fprintf (stderr, "Invalid value for --sort: %s\n", opt); + return 1; + } + } else { + fprintf (stderr, "Unrecognized option: %s\n", argv[i]); + return 1; + } + } + + argc -= i; + argv += i; + + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; + + notmuch = notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY); + if (notmuch == NULL) + return 1; + + query_str = query_string_from_args (ctx, argc, argv); + if (query_str == NULL) { + fprintf (stderr, "Out of memory.\n"); + return 1; + } + if (*query_str == '\0') { + fprintf (stderr, "Error: notmuch search-files requires at least one search term.\n"); + return 1; + } + + query = notmuch_query_create (notmuch, query_str); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); + return 1; + } + + notmuch_query_set_sort (query, sort); + + do_search_files (query); + + notmuch_query_destroy (query); + notmuch_database_close (notmuch); + + return 0; +} diff --git a/notmuch.c b/notmuch.c index 87479f8..4907339 100644 --- a/notmuch.c +++ b/notmuch.c @@ -165,6 +165,19 @@ command_t commands[] = { "\n" "\t\tSee \"notmuch help search-terms\" for details of the search\n" "\t\tterms syntax." }, + { "search-files", notmuch_search_files_command, + "[options...] [...]", + "\t\tSearch for filenames matching the given search terms.", + "\t\tSupported options for search-files include:\n" + "\n" + "\t\t--sort=(newest-first|oldest-first)\n" + "\n" + "\t\t\tPresent results in either chronological order\n" + "\t\t\t(oldest-first) or reverse chronological order\n" + "\t\t\t(newest-first), which is the default.\n" + "\n" + "\t\tSee \"notmuch help search-terms\" for details of the search\n" + "\t\tterms syntax." }, { "show", notmuch_show_command, " [...]", "\t\tShow all messages matching the search terms.", -- 1.6.6