Re: [PATCH v2 1/3] search: Separately report matching and non-matching authors.
[notmuch-archives.git] / ab / f45141a90fe36d56625823d0c340db810a3dd6
1 Return-Path: <dme@dme.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 1E683431FB6\r
6         for <notmuch@notmuchmail.org>; Mon, 19 Jan 2015 01:15:00 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 1.739\r
10 X-Spam-Level: *\r
11 X-Spam-Status: No, score=1.739 tagged_above=-999 required=5\r
12         tests=[DNS_FROM_AHBL_RHSBL=2.438, RCVD_IN_DNSWL_LOW=-0.7,\r
13         UNPARSEABLE_RELAY=0.001] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 56YoD3jwHBAG for <notmuch@notmuchmail.org>;\r
17         Mon, 19 Jan 2015 01:14:57 -0800 (PST)\r
18 Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com\r
19  [74.125.82.45])        (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
20  certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
21  1CEBB431FAF    for <notmuch@notmuchmail.org>; Mon, 19 Jan 2015 01:14:57 -0800\r
22  (PST)\r
23 Received: by mail-wg0-f45.google.com with SMTP id y19so30579762wgg.4\r
24         for <notmuch@notmuchmail.org>; Mon, 19 Jan 2015 01:14:52 -0800 (PST)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:to:subject:in-reply-to:references:user-agent\r
28         :from:date:message-id:mime-version:content-type;\r
29         bh=k/AkbE/POg66ByQx1o/X/0SAj6ZL/mRShmtG/6IMDlk=;\r
30         b=TWtkXEPRHEt84vq8JTkxjUOAWHpK+anlPfjEYLWoBRRWrKcPq1blVbtMEx9gTqp667\r
31         jNv1PHqo5jioOg6gUzXKNdWO4+8al+FRLejAJji/ROf4dsbQqULERqUrYZOIrkXYHsYB\r
32         b//Fgp55pWEJ/vg4rXAtKnwi9IMzrXDndGHcfzJ88ffMx5BKzTkhGVvD9KmrP/KTyLRO\r
33         zhPxiZbXOhDMpdx6Uf2FAzvfs8VqOO2d8xaHHc5rdkMWcqh8gpaqAazDU+4NaHQ52YBY\r
34         5aQ8n97jae8Bwu1BCyMM4AbVc6tg4opiyqhsMDN+6m+RDF7dx10+CC8Hxx+DpHOCx8Zx\r
35         Tu/A==\r
36 X-Gm-Message-State:\r
37  ALoCoQmTYeSt8etqkApq1QHMIkKtYDYla2W9mhKPbI/zXehrsDiH/+GSyc+KQzgDYN+vQJEzf6nv\r
38 X-Received: by 10.180.83.129 with SMTP id q1mr33065141wiy.8.1421658891341;\r
39         Mon, 19 Jan 2015 01:14:51 -0800 (PST)\r
40 Received: from disaster-area.hh.sledj.net\r
41         ([2a01:348:1a2:1:ea39:35ff:fe2c:a227])\r
42         by mx.google.com with ESMTPSA id\r
43         gu5sm13360570wib.24.2015.01.19.01.14.50\r
44         (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
45         Mon, 19 Jan 2015 01:14:50 -0800 (PST)\r
46 Received: from localhost (30000@localhost [local]);\r
47         by localhost (OpenSMTPD) with ESMTPA id 1ec861d2;\r
48         Mon, 19 Jan 2015 09:14:48 +0000 (UTC)\r
49 To: Mark Walters <markwalters1009@gmail.com>, notmuch@notmuchmail.org\r
50 Subject: Re: [PATCH v2 1/3] search: Separately report matching and\r
51         non-matching    authors.\r
52 In-Reply-To: <87r3usj7fo.fsf@qmul.ac.uk>\r
53 References: <1414172643-28270-1-git-send-email-dme@dme.org>\r
54         <1414172643-28270-2-git-send-email-dme@dme.org>\r
55         <87r3usj7fo.fsf@qmul.ac.uk>\r
56 User-Agent: none\r
57 From: David Edmondson <dme@dme.org>\r
58 Date: Mon, 19 Jan 2015 09:14:48 +0000\r
59 Message-ID: <cunfvb7ku7r.fsf@gargravarr.hh.sledj.net>\r
60 MIME-Version: 1.0\r
61 Content-Type: text/plain\r
62 X-BeenThere: notmuch@notmuchmail.org\r
63 X-Mailman-Version: 2.1.13\r
64 Precedence: list\r
65 List-Id: "Use and development of the notmuch mail system."\r
66         <notmuch.notmuchmail.org>\r
67 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
69 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
70 List-Post: <mailto:notmuch@notmuchmail.org>\r
71 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
72 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
73         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
74 X-List-Received-Date: Mon, 19 Jan 2015 09:15:00 -0000\r
75 \r
76 On Sun, Jan 18 2015, Mark Walters wrote:\r
77 > On Fri, 24 Oct 2014, David Edmondson <dme@dme.org> wrote:\r
78 >> In addition to the 'authors' attribute of each search result, include\r
79 >> 'authors_matched' and 'authors_non_matched' attributes. Both\r
80 >> attributes are always included and are formatted as a list of\r
81 >> authors. If there are no matching authors, the 'authors_non_matched'\r
82 >> attribute is set to the empty list.\r
83 >\r
84 > Hi\r
85 >\r
86 > Sorry to be so slow reviewing this. Would it be possible to do the\r
87 > matching/non-matching stuff in lib/thread.cc and just call that from\r
88 > notmuch-search.c? I guess you would need to add a matched_authors, and\r
89 > unmatched_authors string to the notmuch_thread struct.\r
90 >\r
91 > Doing this in search.c seems to redo things that the thread code is\r
92 > already doing but maybe I don't really know this code.\r
93 \r
94 Is that different to what I did originally? Austin suggested the\r
95 approach in this version (id:20141024124016.GG7970@csail.mit.edu),\r
96 unless I misunderstood him.\r
97 \r
98 > Best wishes\r
99 >\r
100 > Mark\r
101 >\r
102 >> ---\r
103 >>  notmuch-search.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
104 >>  1 file changed, 105 insertions(+)\r
105 >>\r
106 >> diff --git a/notmuch-search.c b/notmuch-search.c\r
107 >> index bc9be45..18c3b20 100644\r
108 >> --- a/notmuch-search.c\r
109 >> +++ b/notmuch-search.c\r
110 >> @@ -22,6 +22,8 @@\r
111 >>  #include "sprinter.h"\r
112 >>  #include "string-util.h"\r
113 >>  \r
114 >> +#include <glib.h>\r
115 >> +\r
116 >>  typedef enum {\r
117 >>      OUTPUT_SUMMARY,\r
118 >>      OUTPUT_THREADS,\r
119 >> @@ -69,6 +71,105 @@ get_thread_query (notmuch_thread_t *thread,\r
120 >>      return 0;\r
121 >>  }\r
122 >>  \r
123 >> +/* Return a more pleasent rendering of the mail address\r
124 >> + * `nasty_author'. */\r
125 >> +static const char *\r
126 >> +_nice_author (void *ctx, const char *nasty_author)\r
127 >> +{\r
128 >> +    const char *nice_author = NULL;\r
129 >> +\r
130 >> +    InternetAddressList *list = internet_address_list_parse_string (nasty_author);\r
131 >> +    if (list) {\r
132 >> +    InternetAddress *address = internet_address_list_get_address (list, 0);\r
133 >> +    if (address) {\r
134 >> +        nice_author = internet_address_get_name (address);\r
135 >> +        if (nice_author == NULL) {\r
136 >> +            InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
137 >> +            nice_author = internet_address_mailbox_get_addr (mailbox);\r
138 >> +        }\r
139 >> +    }\r
140 >> +    /* Duplicate the string before `g_object_unref' destroys\r
141 >> +     * it. */\r
142 >> +    if (nice_author)\r
143 >> +        nice_author = talloc_strdup (ctx, nice_author);\r
144 >> +\r
145 >> +    g_object_unref (G_OBJECT (list));\r
146 >> +    }\r
147 >> +\r
148 >> +    if (nice_author)\r
149 >> +    return nice_author;\r
150 >> +    else\r
151 >> +    return nasty_author;\r
152 >> +}\r
153 >> +\r
154 >> +static int\r
155 >> +_enumerate_authors (sprinter_t *format,\r
156 >> +             notmuch_thread_t *thread)\r
157 >> +{\r
158 >> +    notmuch_messages_t *messages;\r
159 >> +    GHashTable *matched_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);\r
160 >> +    GHashTable *unmatched_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);\r
161 >> +    GPtrArray *matched_array = g_ptr_array_new ();\r
162 >> +    GPtrArray *unmatched_array = g_ptr_array_new ();\r
163 >> +\r
164 >> +    /* Iterate over the messages in the thread collecting matching and\r
165 >> +     * non-matching authors. */\r
166 >> +    for (messages = notmuch_thread_get_messages (thread);\r
167 >> +     notmuch_messages_valid (messages);\r
168 >> +     notmuch_messages_move_to_next (messages))\r
169 >> +    {\r
170 >> +    notmuch_message_t *message = notmuch_messages_get (messages);\r
171 >> +    const char *author = _nice_author (thread, notmuch_message_get_header (message, "from"));\r
172 >> +\r
173 >> +    if (author) {\r
174 >> +        GHashTable *hash;\r
175 >> +        GPtrArray *array;\r
176 >> +\r
177 >> +        if (notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH)) {\r
178 >> +            hash = matched_hash;\r
179 >> +            array = matched_array;\r
180 >> +        } else {\r
181 >> +            hash = unmatched_hash;\r
182 >> +            array = unmatched_array;\r
183 >> +        }\r
184 >> +\r
185 >> +        if (!g_hash_table_lookup_extended (hash, author, NULL, NULL)) {\r
186 >> +            char *copy = talloc_strdup (thread, author);\r
187 >> +            g_hash_table_insert (hash, copy, NULL);\r
188 >> +            g_ptr_array_add (array, (char *) copy);\r
189 >> +        }\r
190 >> +    }\r
191 >> +    }\r
192 >> +\r
193 >> +    /* Output the matched authors. */\r
194 >> +    unsigned int i;\r
195 >> +    format->map_key (format, "authors_matched");\r
196 >> +    format->begin_list (format);\r
197 >> +    for (i = 0; i < matched_array->len; i++)\r
198 >> +    format->string (format, (char *) g_ptr_array_index( matched_array, i));\r
199 >> +    format->end (format);\r
200 >> +\r
201 >> +    /* Output the non-matched authors, but not if they were seen\r
202 >> +     * already in the matched authors list. */\r
203 >> +    format->map_key (format, "authors_non_matched");\r
204 >> +    format->begin_list (format);\r
205 >> +    for (i = 0; i < unmatched_array->len; i++) {\r
206 >> +    char *author = (char *) g_ptr_array_index( unmatched_array, i);\r
207 >> +\r
208 >> +    if (!g_hash_table_lookup_extended (matched_hash, author, NULL, NULL))\r
209 >> +        format->string (format, author);\r
210 >> +    }\r
211 >> +    format->end (format);\r
212 >> +\r
213 >> +    g_hash_table_unref (matched_hash);\r
214 >> +    g_hash_table_unref (unmatched_hash);\r
215 >> +\r
216 >> +    g_ptr_array_free (matched_array, TRUE);\r
217 >> +    g_ptr_array_free (unmatched_array, TRUE);\r
218 >> +\r
219 >> +    return 0;\r
220 >> +}\r
221 >> +\r
222 >>  static int\r
223 >>  do_search_threads (sprinter_t *format,\r
224 >>                 notmuch_query_t *query,\r
225 >> @@ -152,6 +253,10 @@ do_search_threads (sprinter_t *format,\r
226 >>              format->integer (format, total);\r
227 >>              format->map_key (format, "authors");\r
228 >>              format->string (format, authors);\r
229 >> +            if (_enumerate_authors (format, thread) < 0) {\r
230 >> +                fprintf (stderr, "Out of memory\n");\r
231 >> +                return 1;\r
232 >> +            }\r
233 >>              format->map_key (format, "subject");\r
234 >>              format->string (format, subject);\r
235 >>              if (notmuch_format_version >= 2) {\r
236 >> -- \r
237 >> 2.1.1\r
238 >>\r
239 >> _______________________________________________\r
240 >> notmuch mailing list\r
241 >> notmuch@notmuchmail.org\r
242 >> http://notmuchmail.org/mailman/listinfo/notmuch\r