[notmuch] [RFC/PATCH] Add search-files command
authorAli Polatel <alip@exherbo.org>
Wed, 13 Jan 2010 10:24:22 +0000 (12:24 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:35:58 +0000 (09:35 -0800)
33/407ed0706c5335252db8b792eca13d9b5e0607 [new file with mode: 0644]

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