Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 74675431FBC for ; Wed, 13 Jan 2010 05:17:46 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=5 tests=[AWL=0.881, 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 9UB43sZg3a4A for ; Wed, 13 Jan 2010 05:17:45 -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 4FD28431FAE for ; Wed, 13 Jan 2010 05:17:45 -0800 (PST) Received: by fxm9 with SMTP id 9so20804210fxm.30 for ; Wed, 13 Jan 2010 05:17:44 -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:in-reply-to:references:organization; bh=2HggMywjex1jI3NqygpcK8qh0Ptb9EqzPbD7dv/tv60=; b=iGr3g4q+PK9j+9P9Ia+rdrvaYuWAB0oRuHeqZMTuuDjz8qAHpqQeYMoaAz6FXU6hdg 2FzByFA/lPX1ISky9XWdHZNQcKpc/huFgyaa78/c46jtxkpW3zNexGN86ukDnLCHU5h1 FhUT8oH6OYaFq4hwsLTVzEbJQiX9jgJLIC2mI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references:organization; b=Yb9aNRjdvYbveLNqfdeC2/JjAmIovI2pG3l0W2IyfRqM/vSh8mnkM8+YXdFZ7gA/e4 QVpJkMoD73U7UZXHZg1r02zDaoKY8pXip61dAMafOKFnxH93RvmHVjpkkX3EzEP0+qt1 bKGN63YAfCtjHEoSk3afOi1Q579/RPcwfEYSE= Received: by 10.86.198.9 with SMTP id v9mr5216790fgf.14.1263388664293; Wed, 13 Jan 2010 05:17:44 -0800 (PST) Received: from harikalardiyari ([78.179.54.193]) by mx.google.com with ESMTPS id 14sm10796277fxm.7.2010.01.13.05.17.42 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 13 Jan 2010 05:17:43 -0800 (PST) Sender: Ali Polatel From: Ali Polatel To: notmuch@notmuchmail.org Date: Wed, 13 Jan 2010 15:17:41 +0200 Message-Id: X-Mailer: git-send-email 1.6.6 In-Reply-To: References: Organization: Pink Floyd Subject: [notmuch] [RFC/PATCH v2] 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 13:17:46 -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. Version 2: - Update copyright - Respect coding style about long lines - Free config structure on exit --- Makefile.local | 1 + notmuch-client.h | 3 + notmuch-search-files.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ notmuch.c | 13 ++++++ 4 files changed, 126 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..7b6d14d --- /dev/null +++ b/notmuch-search-files.c @@ -0,0 +1,109 @@ +/* notmuch - Not much of an email program, (just index and search) + * + * Copyright © 2010 Ali Polatel + * + * 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_config_close (config); + 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