Re: [PATCH] cli: add --output=address-{from, to, all} to notmuch search
authorJani Nikula <jani@nikula.org>
Sat, 6 Sep 2014 16:47:36 +0000 (19:47 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:04:29 +0000 (10:04 -0800)
df/179d1677580d73200c49877d74536b21ae38c6 [new file with mode: 0644]

diff --git a/df/179d1677580d73200c49877d74536b21ae38c6 b/df/179d1677580d73200c49877d74536b21ae38c6
new file mode 100644 (file)
index 0000000..fa5aec9
--- /dev/null
@@ -0,0 +1,279 @@
+Return-Path: <jani@nikula.org>\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 1209E431FB6\r
+       for <notmuch@notmuchmail.org>; Sat,  6 Sep 2014 09:47:48 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\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 0F6Ririxd--E for <notmuch@notmuchmail.org>;\r
+       Sat,  6 Sep 2014 09:47:40 -0700 (PDT)\r
+Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com\r
+ [74.125.82.51])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 190EA431FAE   for <notmuch@notmuchmail.org>; Sat,  6 Sep 2014 09:47:39 -0700\r
+ (PDT)\r
+Received: by mail-wg0-f51.google.com with SMTP id l18so12841023wgh.34\r
+       for <notmuch@notmuchmail.org>; Sat, 06 Sep 2014 09:47:38 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=1e100.net; s=20130820;\r
+       h=x-gm-message-state:from:to:subject:in-reply-to:references\r
+       :user-agent:date:message-id:mime-version:content-type;\r
+       bh=6cFxVmVfVaxKfehvTfSkBc7oNdUMKCj9dVmcFahuLjI=;\r
+       b=aK1ZxxykiG2sV804zrEd8mDIZDXLY/Jj8GnS5vzAa50M021D1qEyAOybcQ+oz2JuFw\r
+       xloCBdMlrRc6Wi2HWcCAtGIHxRM/ugLL/dwCRSz0/Ka6eNuufX/FIpF7/lUB25VT8B7f\r
+       OYJR/U1wRAN3bL9RycudzK5Ml+4B5lJFFHWajDy+5T/DZeRdg6u9MLQbOEwqh5CjWZuJ\r
+       lsfPXtevVlxt0ylX9TxAZtPlJ4GV9EHNEKBePUAcJ9hL5ucoxW3oMP8xa9O/UKDYuWST\r
+       MWiPj+X6B6n9zk0Q8Gj7/UH3C2dLtBnYoRaNRBvkl7Ch6soS+K7H7xKbvTuU01sW+B6X\r
+       LXeg==\r
+X-Gm-Message-State:\r
+ ALoCoQnt59mUA6+6WHmLa3E8EXmQDnGq60rKPO6C1u4dEAbZAfazseCddW4BYUuZZPFVXsdEemmG\r
+X-Received: by 10.195.13.34 with SMTP id ev2mr21723821wjd.55.1410022057770;\r
+       Sat, 06 Sep 2014 09:47:37 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw2-58c374-75.dhcp.inet.fi.\r
+       [88.195.116.75])\r
+       by mx.google.com with ESMTPSA id pn5sm5065005wjc.4.2014.09.06.09.47.36\r
+       for <notmuch@notmuchmail.org>\r
+       (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+       Sat, 06 Sep 2014 09:47:37 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: Re: [PATCH] cli: add --output=address-{from, to,\r
+       all} to notmuch search\r
+In-Reply-To: <1410021689-15901-1-git-send-email-jani@nikula.org>\r
+References: <871troub1v.fsf@qmul.ac.uk>\r
+       <1410021689-15901-1-git-send-email-jani@nikula.org>\r
+User-Agent: Notmuch/0.18.1+65~g9f0f30f (http://notmuchmail.org) Emacs/24.3.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Sat, 06 Sep 2014 19:47:36 +0300\r
+Message-ID: <8738c41ys7.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\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: Sat, 06 Sep 2014 16:47:48 -0000\r
+\r
+On Sat, 06 Sep 2014, Jani Nikula <jani@nikula.org> wrote:\r
+> address-from prints reply-to or from, address-to prints to, cc, and\r
+> bcc, and address-all prints all of them.\r
+\r
+*sigh* this was supposed to be:\r
+\r
+---\r
+\r
+cli: add --output=sender and --output=recipients to notmuch search\r
+    \r
+sender prints from, recipients prints to, cc, and bcc.\r
+\r
+---\r
+\r
+I don't have the time for this, and I'm rushing too much...\r
+\r
+\r
+BR,\r
+Jani.\r
+\r
+\r
+\r
+> ---\r
+>  notmuch-search.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++-----\r
+>  1 file changed, 100 insertions(+), 9 deletions(-)\r
+>\r
+> diff --git a/notmuch-search.c b/notmuch-search.c\r
+> index bc9be4593ecc..e7cf3d2a0fdf 100644\r
+> --- a/notmuch-search.c\r
+> +++ b/notmuch-search.c\r
+> @@ -23,11 +23,14 @@\r
+>  #include "string-util.h"\r
+>  \r
+>  typedef enum {\r
+> -    OUTPUT_SUMMARY,\r
+> -    OUTPUT_THREADS,\r
+> -    OUTPUT_MESSAGES,\r
+> -    OUTPUT_FILES,\r
+> -    OUTPUT_TAGS\r
+> +    OUTPUT_SUMMARY  = 1 << 0,\r
+> +    OUTPUT_THREADS  = 1 << 1,\r
+> +    OUTPUT_MESSAGES = 1 << 2,\r
+> +    OUTPUT_FILES    = 1 << 3,\r
+> +    OUTPUT_TAGS             = 1 << 4,\r
+> +    OUTPUT_SENDER   = 1 << 5,\r
+> +    OUTPUT_RECIPIENTS       = 1 << 6,\r
+> +    OUTPUT_ADDRESSES        = OUTPUT_SENDER | OUTPUT_RECIPIENTS,\r
+>  } output_t;\r
+>  \r
+>  /* Return two stable query strings that identify exactly the matched\r
+> @@ -214,6 +217,66 @@ do_search_threads (sprinter_t *format,\r
+>      return 0;\r
+>  }\r
+>  \r
+> +static void\r
+> +print_address_list (sprinter_t *format, InternetAddressList *list)\r
+> +{\r
+> +    InternetAddress *address;\r
+> +    int i;\r
+> +\r
+> +    for (i = 0; i < internet_address_list_length (list); i++) {\r
+> +    address = internet_address_list_get_address (list, i);\r
+> +    if (INTERNET_ADDRESS_IS_GROUP (address)) {\r
+> +        InternetAddressGroup *group;\r
+> +        InternetAddressList *group_list;\r
+> +\r
+> +        group = INTERNET_ADDRESS_GROUP (address);\r
+> +        group_list = internet_address_group_get_members (group);\r
+> +        if (group_list == NULL)\r
+> +            continue;\r
+> +\r
+> +        print_address_list (format, group_list);\r
+> +    } else {\r
+> +        InternetAddressMailbox *mailbox;\r
+> +        const char *name;\r
+> +        const char *addr;\r
+> +        char *full_address;\r
+> +\r
+> +        mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
+> +\r
+> +        name = internet_address_get_name (address);\r
+> +        addr = internet_address_mailbox_get_addr (mailbox);\r
+> +\r
+> +        if (name && *name)\r
+> +            full_address = talloc_asprintf (NULL, "%s <%s>", name, addr);\r
+> +        else\r
+> +            full_address = talloc_asprintf (NULL, "<%s>", addr);\r
+> +\r
+> +        if (!full_address)\r
+> +            break;\r
+> +\r
+> +        format->string (format, full_address);\r
+> +        format->separator (format);\r
+> +\r
+> +        talloc_free (full_address);\r
+> +    }\r
+> +    }\r
+> +}\r
+> +\r
+> +static void\r
+> +print_address_string (sprinter_t *format, const char *recipients)\r
+> +{\r
+> +    InternetAddressList *list;\r
+> +\r
+> +    if (recipients == NULL)\r
+> +    return;\r
+> +\r
+> +    list = internet_address_list_parse_string (recipients);\r
+> +    if (list == NULL)\r
+> +    return;\r
+> +\r
+> +    print_address_list (format, list);\r
+> +}\r
+> +\r
+>  static int\r
+>  do_search_messages (sprinter_t *format,\r
+>                  notmuch_query_t *query,\r
+> @@ -264,11 +327,29 @@ do_search_messages (sprinter_t *format,\r
+>          \r
+>          notmuch_filenames_destroy( filenames );\r
+>  \r
+> -    } else { /* output == OUTPUT_MESSAGES */\r
+> +    } else if (output == OUTPUT_MESSAGES) {\r
+>          format->set_prefix (format, "id");\r
+>          format->string (format,\r
+>                          notmuch_message_get_message_id (message));\r
+>          format->separator (format);\r
+> +    } else {\r
+> +        if (output & OUTPUT_SENDER) {\r
+> +            const char *addrs;\r
+> +\r
+> +            addrs = notmuch_message_get_header (message, "from");\r
+> +            print_address_string (format, addrs);\r
+> +        }\r
+> +\r
+> +        if (output & OUTPUT_RECIPIENTS) {\r
+> +            const char *hdrs[] = { "to", "cc", "bcc" };\r
+> +            const char *addrs;\r
+> +            size_t j;\r
+> +\r
+> +            for (j = 0; j < ARRAY_SIZE (hdrs); j++) {\r
+> +                addrs = notmuch_message_get_header (message, hdrs[j]);\r
+> +                print_address_string (format, addrs);\r
+> +            }\r
+> +        }\r
+>      }\r
+>  \r
+>      notmuch_message_destroy (message);\r
+> @@ -338,7 +419,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+>      notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
+>      sprinter_t *format = NULL;\r
+>      int opt_index, ret;\r
+> -    output_t output = OUTPUT_SUMMARY;\r
+> +    output_t output = 0;\r
+>      int offset = 0;\r
+>      int limit = -1; /* unlimited */\r
+>      notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
+> @@ -364,10 +445,12 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+>                                { "text0", NOTMUCH_FORMAT_TEXT0 },\r
+>                                { 0, 0 } } },\r
+>      { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },\r
+> -    { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
+> +    { NOTMUCH_OPT_KEYWORD_FLAGS, &output, "output", 'o',\r
+>        (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
+>                                { "threads", OUTPUT_THREADS },\r
+>                                { "messages", OUTPUT_MESSAGES },\r
+> +                              { "sender", OUTPUT_SENDER },\r
+> +                              { "recipients", OUTPUT_RECIPIENTS },\r
+>                                { "files", OUTPUT_FILES },\r
+>                                { "tags", OUTPUT_TAGS },\r
+>                                { 0, 0 } } },\r
+> @@ -387,6 +470,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+>      if (opt_index < 0)\r
+>      return EXIT_FAILURE;\r
+>  \r
+> +    if (! output)\r
+> +    output = OUTPUT_SUMMARY;\r
+> +\r
+>      switch (format_sel) {\r
+>      case NOTMUCH_FORMAT_TEXT:\r
+>      format = sprinter_text_create (config, stdout);\r
+> @@ -453,18 +539,23 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
+>      }\r
+>  \r
+>      switch (output) {\r
+> -    default:\r
+>      case OUTPUT_SUMMARY:\r
+>      case OUTPUT_THREADS:\r
+>      ret = do_search_threads (format, query, sort, output, offset, limit);\r
+>      break;\r
+>      case OUTPUT_MESSAGES:\r
+> +    case OUTPUT_SENDER:\r
+> +    case OUTPUT_RECIPIENTS:\r
+> +    case OUTPUT_ADDRESSES:\r
+>      case OUTPUT_FILES:\r
+>      ret = do_search_messages (format, query, output, offset, limit, dupe);\r
+>      break;\r
+>      case OUTPUT_TAGS:\r
+>      ret = do_search_tags (notmuch, format, query);\r
+>      break;\r
+> +    default:\r
+> +    fprintf (stderr, "Error: the combination of outputs is not supported.\n");\r
+> +    ret = 1;\r
+>      }\r
+>  \r
+>      notmuch_query_destroy (query);\r
+> -- \r
+> 2.1.0\r