[PATCH] lib: Add a new prefix "list" to the search-terms syntax

Subject: [PATCH] lib: Add a new prefix "list" to the search-terms syntax

Date: Wed, 3 Apr 2013 17:46:03 +0400

To: notmuch@notmuchmail.org

Cc: Alexey I. Froloff

From: Alexey I. Froloff


From: "Alexey I. Froloff" <raorn@raorn.name>

Add support for indexing and searching the message's List-Id header.
This is useful when matching all the messages belonging to a particular
mailing list.

Rework of the patch by Pablo Oliveira <pablo@sifflez.org>

Cc: Pablo Oliveira <pablo@sifflez.org>
Signed-off-by: Alexey I. Froloff <raorn@raorn.name>
---
 lib/database.cc                 |  1 +
 lib/index.cc                    | 48 ++++++++++++++++++++++++++++++++++++++++-
 man/man7/notmuch-search-terms.7 |  8 +++++++
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/lib/database.cc b/lib/database.cc
index 91d4329..9311505 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -214,6 +214,7 @@ static prefix_t PROBABILISTIC_PREFIX[]= {
     { "to",			"XTO" },
     { "attachment",		"XATTACHMENT" },
     { "subject",		"XSUBJECT"},
+    { "list",			"XLIST"},
     { "folder",			"XFOLDER"}
 };
 
diff --git a/lib/index.cc b/lib/index.cc
index a2edd6d..d79bd95 100644
--- a/lib/index.cc
+++ b/lib/index.cc
@@ -304,6 +304,49 @@ _index_address_list (notmuch_message_t *message,
     }
 }
 
+static void
+_index_list_id (notmuch_message_t *message,
+               const char *list_id_header)
+{
+    const char *begin_list_id, *end_list_id;
+
+    if (list_id_header == NULL)
+	return;
+
+    /* RFC2919 says that the list-id is found at the end of the header
+     * and enclosed between angle brackets. If we cannot find a
+     * matching pair of brackets containing at least one character,
+     * we ignore the list id header. */
+    begin_list_id = strrchr (list_id_header, '<');
+    if (!begin_list_id)
+	return;
+
+    end_list_id = strrchr(begin_list_id, '>');
+    if (!end_list_id || (end_list_id - begin_list_id < 2))
+	return;
+
+    void *local = talloc_new (message);
+
+    /* We extract the list id between the angle brackets */
+    const char *list_id = talloc_strndup(local, begin_list_id + 1,
+					 end_list_id - begin_list_id - 1);
+
+    /* All the text before is the description of the list */
+    const char *description = talloc_strndup(local, list_id_header,
+					     begin_list_id - list_id_header);
+
+    /* Description may be RFC2047 encoded */
+    char *decoded_desc = g_mime_utils_header_decode_phrase(description);
+
+    _notmuch_message_gen_terms(message, "list", list_id);
+
+    if (decoded_desc)
+	_notmuch_message_gen_terms(message, "list", decoded_desc);
+
+    free(decoded_desc);
+    talloc_free (local);
+}
+
 /* Callback to generate terms for each mime part of a message. */
 static void
 _index_mime_part (notmuch_message_t *message,
@@ -432,7 +475,7 @@ _notmuch_message_index_file (notmuch_message_t *message,
     GMimeMessage *mime_message = NULL;
     InternetAddressList *addresses;
     FILE *file = NULL;
-    const char *from, *subject;
+    const char *from, *subject, *list_id;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
     static int initialized = 0;
     char from_buf[5];
@@ -500,6 +543,9 @@ mboxes is deprecated and may be removed in the future.\n", filename);
     subject = g_mime_message_get_subject (mime_message);
     _notmuch_message_gen_terms (message, "subject", subject);
 
+    list_id = g_mime_object_get_header (GMIME_OBJECT (mime_message), "List-Id");
+    _index_list_id (message, list_id);
+
     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
 
   DONE:
diff --git a/man/man7/notmuch-search-terms.7 b/man/man7/notmuch-search-terms.7
index eb417ba..9cae107 100644
--- a/man/man7/notmuch-search-terms.7
+++ b/man/man7/notmuch-search-terms.7
@@ -52,6 +52,8 @@ terms to match against specific portions of an email, (where
 
 	thread:<thread-id>
 
+	list:<list-id>
+
 	folder:<directory-path>
 
 	date:<since>..<until>
@@ -100,6 +102,12 @@ thread ID values can be seen in the first column of output from
 .B "notmuch search"
 
 The
+.BR list: ,
+is used to match mailing list ID of an email message \- contents of the
+List\-Id: header without the '<', '>' delimiters or decoded list
+description.
+
+The
 .B folder:
 prefix can be used to search for email message files that are
 contained within particular directories within the mail store. Only
-- 
1.8.1.4


Thread: