]> rtime.felk.cvut.cz Git - notmuch.git/commitdiff
cli: Extend the search command for --output=addresses and similar
authorMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 21 Sep 2014 18:28:31 +0000 (20:28 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 22 Sep 2014 08:36:36 +0000 (10:36 +0200)
The new outputs allow printing senders, recipients or both of matching
messages.

This code based on a patch from Jani Nikula.

completion/notmuch-completion.bash
completion/notmuch-completion.zsh
doc/man1/notmuch-search.rst
notmuch-search.c

index 0571dc9da8e332b3fa3c24ab27e9fa61a3c637f4..c37ddf56233bb3f8040e870eb30ec37d5373e7ab 100644 (file)
@@ -294,7 +294,7 @@ _notmuch_search()
            return
            ;;
        --output)
-           COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
+           COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients addresses" -- "${cur}" ) )
            return
            ;;
        --sort)
index 67a9aba85f694f0fe652f1e92a27b5202c39de32..bff8fd5bbf6240deada0255a8bd3188c85f92f70 100644 (file)
@@ -52,7 +52,8 @@ _notmuch_search()
   _arguments -s : \
     '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \
     '--first=[omit the first x threads from the search results]:number of threads to omit: ' \
-    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))'
+    '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
+    '--output=[select what to output]:output:((summary threads messages files tags sender recipients addresses))'
 }
 
 _notmuch()
index 90160f21e23c12161876d019b07ca9a0c8dc7bfc..6094906822c1f15e51af1bf7da2bbfe40cd3cfc5 100644 (file)
@@ -35,7 +35,7 @@ Supported options for **search** include
         intended for programs that invoke **notmuch(1)** internally. If
         omitted, the latest supported version will be used.
 
-    ``--output=(summary|threads|messages|files|tags)``
+    ``--output=(summary|threads|messages|files|tags|sender|recipients|addresses)``
 
         **summary**
             Output a summary of each thread with any message matching
@@ -78,6 +78,26 @@ Supported options for **search** include
             by null characters (--format=text0), as a JSON array
             (--format=json), or as an S-Expression list (--format=sexp).
 
+       **sender**
+            Output all addresses from the *From* header that appear on
+            any message matching the search terms, either one per line
+            (--format=text), separated by null characters
+            (--format=text0), as a JSON array (--format=json), or as
+            an S-Expression list (--format=sexp).
+
+           Note: Searching for **sender** should much be faster than
+           searching for **recipients** or **addresses**, because
+           sender addresses are cached directly in the database
+           whereas other addresses need to be fetched from the
+           message file by parsing it.
+
+       **recipients**
+            Like **sender** but for addresses from *To*, *Cc* and
+           *Bcc* headers.
+
+       **addresses**
+           Like **sender** and **recipients** together.
+
     ``--sort=``\ (**newest-first**\ \|\ **oldest-first**)
         This option can be used to present results in either
         chronological order (**oldest-first**) or reverse chronological
index 5ac2a26faab7e3fcc47822b51c465c6b65294ee1..0614f1023b88da5f3259329d37dd79d4bd37c9da 100644 (file)
 #include "string-util.h"
 
 typedef enum {
-    OUTPUT_SUMMARY,
-    OUTPUT_THREADS,
-    OUTPUT_MESSAGES,
-    OUTPUT_FILES,
-    OUTPUT_TAGS
+    OUTPUT_SUMMARY     = 1 << 0,
+    OUTPUT_THREADS     = 1 << 1,
+    OUTPUT_MESSAGES    = 1 << 2,
+    OUTPUT_FILES       = 1 << 3,
+    OUTPUT_TAGS                = 1 << 4,
+    OUTPUT_SENDER      = 1 << 5,
+    OUTPUT_RECIPIENTS  = 1 << 6,
+    OUTPUT_ADDRESSES   = OUTPUT_SENDER | OUTPUT_RECIPIENTS,
 } output_t;
 
 typedef struct {
@@ -220,6 +223,67 @@ do_search_threads (search_options_t *o)
     return 0;
 }
 
+static void
+print_address_list (const search_options_t *o, InternetAddressList *list)
+{
+    InternetAddress *address;
+    int i;
+
+    for (i = 0; i < internet_address_list_length (list); i++) {
+       address = internet_address_list_get_address (list, i);
+       if (INTERNET_ADDRESS_IS_GROUP (address)) {
+           InternetAddressGroup *group;
+           InternetAddressList *group_list;
+
+           group = INTERNET_ADDRESS_GROUP (address);
+           group_list = internet_address_group_get_members (group);
+           if (group_list == NULL)
+               continue;
+
+           print_address_list (o, group_list);
+       } else {
+           InternetAddressMailbox *mailbox;
+           const char *name;
+           const char *addr;
+           char *full_address;
+
+           mailbox = INTERNET_ADDRESS_MAILBOX (address);
+
+           name = internet_address_get_name (address);
+           addr = internet_address_mailbox_get_addr (mailbox);
+
+           if (name && *name)
+               full_address = talloc_asprintf (o->format, "%s <%s>", name, addr);
+           else
+               full_address = talloc_strdup (o->format, addr);
+
+           if (!full_address) {
+               fprintf (stderr, "Error: out of memory\n");
+               break;
+           }
+           o->format->string (o->format, full_address);
+           o->format->separator (o->format);
+
+           talloc_free (full_address);
+       }
+    }
+}
+
+static void
+print_address_string (const search_options_t *o, const char *recipients)
+{
+    InternetAddressList *list;
+
+    if (recipients == NULL)
+       return;
+
+    list = internet_address_list_parse_string (recipients);
+    if (list == NULL)
+       return;
+
+    print_address_list (o, list);
+}
+
 static int
 do_search_messages (search_options_t *o)
 {
@@ -266,11 +330,29 @@ do_search_messages (search_options_t *o)
            
            notmuch_filenames_destroy( filenames );
 
-       } else { /* output == OUTPUT_MESSAGES */
+       } else if (o->output == OUTPUT_MESSAGES) {
            format->set_prefix (format, "id");
            format->string (format,
                            notmuch_message_get_message_id (message));
            format->separator (format);
+       } else {
+           if (o->output & OUTPUT_SENDER) {
+               const char *addrs;
+
+               addrs = notmuch_message_get_header (message, "from");
+               print_address_string (o, addrs);
+           }
+
+           if (o->output & OUTPUT_RECIPIENTS) {
+               const char *hdrs[] = { "to", "cc", "bcc" };
+               const char *addrs;
+               size_t j;
+
+               for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
+                   addrs = notmuch_message_get_header (message, hdrs[j]);
+                   print_address_string (o, addrs);
+               }
+           }
        }
 
        notmuch_message_destroy (message);
@@ -370,6 +452,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
          (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
                                  { "threads", OUTPUT_THREADS },
                                  { "messages", OUTPUT_MESSAGES },
+                                 { "sender", OUTPUT_SENDER },
+                                 { "recipients", OUTPUT_RECIPIENTS },
+                                 { "addresses", OUTPUT_ADDRESSES },
                                  { "files", OUTPUT_FILES },
                                  { "tags", OUTPUT_TAGS },
                                  { 0, 0 } } },
@@ -461,6 +546,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
        ret = do_search_threads (&o);
        break;
     case OUTPUT_MESSAGES:
+    case OUTPUT_SENDER:
+    case OUTPUT_RECIPIENTS:
+    case OUTPUT_ADDRESSES:
     case OUTPUT_FILES:
        ret = do_search_messages (&o);
        break;