2009-10-26 07:12:20 +01:00
|
|
|
/* thread.cc - Results of thread-based searches from a notmuch database
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Carl Worth
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2009-10-26 07:12:20 +01:00
|
|
|
*
|
|
|
|
* Author: Carl Worth <cworth@cworth.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-private.h"
|
|
|
|
#include "database-private.h"
|
|
|
|
|
2009-11-12 18:59:47 +01:00
|
|
|
#include <gmime/gmime.h>
|
2009-10-26 22:46:14 +01:00
|
|
|
#include <glib.h> /* GHashTable */
|
|
|
|
|
2018-09-03 16:00:26 +02:00
|
|
|
#ifdef DEBUG_THREADING
|
|
|
|
#define THREAD_DEBUG(format, ...) fprintf(stderr, format " (%s).\n", ##__VA_ARGS__, __location__)
|
|
|
|
#else
|
|
|
|
#define THREAD_DEBUG(format, ...) do {} while (0) /* ignored */
|
|
|
|
#endif
|
|
|
|
|
2014-10-29 21:51:43 +01:00
|
|
|
#define EMPTY_STRING(s) ((s)[0] == '\0')
|
|
|
|
|
2017-05-10 21:42:12 +02:00
|
|
|
struct _notmuch_thread {
|
2009-10-26 07:12:20 +01:00
|
|
|
notmuch_database_t *notmuch;
|
|
|
|
char *thread_id;
|
2009-10-27 01:35:31 +01:00
|
|
|
char *subject;
|
2009-11-13 06:19:42 +01:00
|
|
|
GHashTable *authors_hash;
|
2010-04-27 10:48:03 +02:00
|
|
|
GPtrArray *authors_array;
|
|
|
|
GHashTable *matched_authors_hash;
|
2010-12-08 01:33:29 +01:00
|
|
|
GPtrArray *matched_authors_array;
|
2009-11-12 18:59:47 +01:00
|
|
|
char *authors;
|
2009-10-26 22:46:14 +01:00
|
|
|
GHashTable *tags;
|
2009-10-30 01:31:07 +01:00
|
|
|
|
2012-11-25 05:57:03 +01:00
|
|
|
/* All messages, oldest first. */
|
2009-11-16 05:39:25 +01:00
|
|
|
notmuch_message_list_t *message_list;
|
2012-11-25 05:57:03 +01:00
|
|
|
/* Top-level messages, oldest first. */
|
|
|
|
notmuch_message_list_t *toplevel_list;
|
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
GHashTable *message_hash;
|
2009-11-13 07:01:44 +01:00
|
|
|
int total_messages;
|
2017-06-04 14:32:31 +02:00
|
|
|
int total_files;
|
2009-11-13 07:01:44 +01:00
|
|
|
int matched_messages;
|
2009-10-30 01:31:07 +01:00
|
|
|
time_t oldest;
|
|
|
|
time_t newest;
|
2009-10-26 07:12:20 +01:00
|
|
|
};
|
|
|
|
|
2009-10-26 22:46:14 +01:00
|
|
|
static int
|
|
|
|
_notmuch_thread_destructor (notmuch_thread_t *thread)
|
|
|
|
{
|
2009-11-13 06:19:42 +01:00
|
|
|
g_hash_table_unref (thread->authors_hash);
|
2010-04-27 10:48:03 +02:00
|
|
|
g_hash_table_unref (thread->matched_authors_hash);
|
2009-10-26 22:46:14 +01:00
|
|
|
g_hash_table_unref (thread->tags);
|
2009-11-16 05:39:25 +01:00
|
|
|
g_hash_table_unref (thread->message_hash);
|
2009-10-26 22:46:14 +01:00
|
|
|
|
2010-04-27 10:48:03 +02:00
|
|
|
if (thread->authors_array) {
|
2017-10-07 10:44:05 +02:00
|
|
|
g_ptr_array_free (thread->authors_array, true);
|
2010-04-27 10:48:03 +02:00
|
|
|
thread->authors_array = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
if (thread->matched_authors_array) {
|
2017-10-07 10:44:05 +02:00
|
|
|
g_ptr_array_free (thread->matched_authors_array, true);
|
2010-12-08 01:33:29 +01:00
|
|
|
thread->matched_authors_array = NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-26 22:46:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
/* Add each author of the thread to the thread's authors_hash and to
|
|
|
|
* the thread's authors_array. */
|
2009-11-13 06:19:42 +01:00
|
|
|
static void
|
|
|
|
_thread_add_author (notmuch_thread_t *thread,
|
|
|
|
const char *author)
|
|
|
|
{
|
2010-04-27 10:48:03 +02:00
|
|
|
char *author_copy;
|
|
|
|
|
2009-11-13 06:19:42 +01:00
|
|
|
if (author == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_hash_table_lookup_extended (thread->authors_hash,
|
|
|
|
author, NULL, NULL))
|
|
|
|
return;
|
|
|
|
|
2010-04-27 10:48:03 +02:00
|
|
|
author_copy = talloc_strdup (thread, author);
|
|
|
|
|
|
|
|
g_hash_table_insert (thread->authors_hash, author_copy, NULL);
|
|
|
|
|
|
|
|
g_ptr_array_add (thread->authors_array, author_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add each matched author of the thread to the thread's
|
2010-12-08 01:33:29 +01:00
|
|
|
* matched_authors_hash and to the thread's matched_authors_array. */
|
2010-04-27 10:48:03 +02:00
|
|
|
static void
|
|
|
|
_thread_add_matched_author (notmuch_thread_t *thread,
|
|
|
|
const char *author)
|
|
|
|
{
|
|
|
|
char *author_copy;
|
|
|
|
|
|
|
|
if (author == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_hash_table_lookup_extended (thread->matched_authors_hash,
|
|
|
|
author, NULL, NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
author_copy = talloc_strdup (thread, author);
|
|
|
|
|
|
|
|
g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
|
2009-11-13 06:19:42 +01:00
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
g_ptr_array_add (thread->matched_authors_array, author_copy);
|
2009-11-13 06:19:42 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
/* Construct an authors string from matched_authors_array and
|
|
|
|
* authors_array. The string contains matched authors first, then
|
|
|
|
* non-matched authors (with the two groups separated by '|'). Within
|
|
|
|
* each group, authors are listed in date order. */
|
2010-04-24 20:20:54 +02:00
|
|
|
static void
|
2010-12-08 01:33:29 +01:00
|
|
|
_resolve_thread_authors_string (notmuch_thread_t *thread)
|
2010-04-24 20:20:54 +02:00
|
|
|
{
|
2010-04-27 10:48:03 +02:00
|
|
|
unsigned int i;
|
|
|
|
char *author;
|
2010-12-08 01:33:29 +01:00
|
|
|
int first_non_matched_author = 1;
|
|
|
|
|
|
|
|
/* First, list all matched authors in date order. */
|
|
|
|
for (i = 0; i < thread->matched_authors_array->len; i++) {
|
|
|
|
author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
|
|
|
|
if (thread->authors)
|
|
|
|
thread->authors = talloc_asprintf (thread, "%s, %s",
|
|
|
|
thread->authors,
|
|
|
|
author);
|
|
|
|
else
|
|
|
|
thread->authors = author;
|
|
|
|
}
|
2010-04-27 10:48:03 +02:00
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
/* Next, append any non-matched authors that haven't already appeared. */
|
2010-04-27 10:48:03 +02:00
|
|
|
for (i = 0; i < thread->authors_array->len; i++) {
|
|
|
|
author = (char *) g_ptr_array_index (thread->authors_array, i);
|
|
|
|
if (g_hash_table_lookup_extended (thread->matched_authors_hash,
|
|
|
|
author, NULL, NULL))
|
|
|
|
continue;
|
2010-12-08 01:33:29 +01:00
|
|
|
if (first_non_matched_author) {
|
|
|
|
thread->authors = talloc_asprintf (thread, "%s| %s",
|
2010-04-27 10:48:03 +02:00
|
|
|
thread->authors,
|
|
|
|
author);
|
|
|
|
} else {
|
2010-12-08 01:33:29 +01:00
|
|
|
thread->authors = talloc_asprintf (thread, "%s, %s",
|
2010-04-27 10:48:03 +02:00
|
|
|
thread->authors,
|
|
|
|
author);
|
2010-04-24 20:20:54 +02:00
|
|
|
}
|
2010-12-08 01:33:29 +01:00
|
|
|
|
|
|
|
first_non_matched_author = 0;
|
2010-04-24 20:20:54 +02:00
|
|
|
}
|
2010-04-27 10:48:03 +02:00
|
|
|
|
2017-10-07 10:44:05 +02:00
|
|
|
g_ptr_array_free (thread->authors_array, true);
|
2010-04-27 10:48:03 +02:00
|
|
|
thread->authors_array = NULL;
|
2017-10-07 10:44:05 +02:00
|
|
|
g_ptr_array_free (thread->matched_authors_array, true);
|
2010-12-08 01:33:29 +01:00
|
|
|
thread->matched_authors_array = NULL;
|
2017-12-15 03:29:57 +01:00
|
|
|
|
|
|
|
if (!thread->authors)
|
|
|
|
thread->authors = talloc_strdup(thread, "");
|
2010-04-24 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2010-11-02 05:58:43 +01:00
|
|
|
/* clean up the ugly "Lastname, Firstname" format that some mail systems
|
2010-04-24 20:20:57 +02:00
|
|
|
* (most notably, Exchange) are creating to be "Firstname Lastname"
|
|
|
|
* To make sure that we don't change other potential situations where a
|
|
|
|
* comma is in the name, we check that we match one of these patterns
|
|
|
|
* "Last, First" <first.last@company.com>
|
|
|
|
* "Last, First MI" <first.mi.last@company.com>
|
|
|
|
*/
|
2010-11-02 05:58:43 +01:00
|
|
|
static char *
|
2010-04-24 20:20:57 +02:00
|
|
|
_thread_cleanup_author (notmuch_thread_t *thread,
|
|
|
|
const char *author, const char *from)
|
|
|
|
{
|
|
|
|
char *clean_author,*test_author;
|
|
|
|
const char *comma;
|
|
|
|
char *blank;
|
|
|
|
int fname,lname;
|
|
|
|
|
2010-04-28 01:29:22 +02:00
|
|
|
if (author == NULL)
|
|
|
|
return NULL;
|
2010-04-24 20:20:57 +02:00
|
|
|
clean_author = talloc_strdup(thread, author);
|
|
|
|
if (clean_author == NULL)
|
|
|
|
return NULL;
|
2010-04-28 01:29:22 +02:00
|
|
|
/* check if there's a comma in the name and that there's a
|
|
|
|
* component of the name behind it (so the name doesn't end with
|
|
|
|
* the comma - in which case the string that strchr finds is just
|
|
|
|
* one character long ",\0").
|
|
|
|
* Otherwise just return the copy of the original author name that
|
|
|
|
* we just made*/
|
2010-04-24 20:20:57 +02:00
|
|
|
comma = strchr(author,',');
|
2010-04-28 01:29:22 +02:00
|
|
|
if (comma && strlen(comma) > 1) {
|
2010-04-24 20:20:57 +02:00
|
|
|
/* let's assemble what we think is the correct name */
|
|
|
|
lname = comma - author;
|
2013-02-25 23:47:13 +01:00
|
|
|
|
|
|
|
/* Skip all the spaces after the comma */
|
|
|
|
fname = strlen(author) - lname - 1;
|
|
|
|
comma += 1;
|
|
|
|
while (*comma == ' ') {
|
|
|
|
fname -= 1;
|
|
|
|
comma += 1;
|
|
|
|
}
|
|
|
|
strncpy(clean_author, comma, fname);
|
|
|
|
|
2010-04-24 20:20:57 +02:00
|
|
|
*(clean_author+fname) = ' ';
|
|
|
|
strncpy(clean_author + fname + 1, author, lname);
|
|
|
|
*(clean_author+fname+1+lname) = '\0';
|
|
|
|
/* make a temporary copy and see if it matches the email */
|
|
|
|
test_author = talloc_strdup(thread,clean_author);
|
|
|
|
|
|
|
|
blank=strchr(test_author,' ');
|
|
|
|
while (blank != NULL) {
|
|
|
|
*blank = '.';
|
|
|
|
blank=strchr(test_author,' ');
|
|
|
|
}
|
|
|
|
if (strcasestr(from, test_author) == NULL)
|
|
|
|
/* we didn't identify this as part of the email address
|
|
|
|
* so let's punt and return the original author */
|
|
|
|
strcpy (clean_author, author);
|
|
|
|
}
|
|
|
|
return clean_author;
|
|
|
|
}
|
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
/* Add 'message' as a message that belongs to 'thread'.
|
|
|
|
*
|
|
|
|
* The 'thread' will talloc_steal the 'message' and hold onto a
|
|
|
|
* reference to it.
|
|
|
|
*/
|
2009-11-13 06:09:54 +01:00
|
|
|
static void
|
|
|
|
_thread_add_message (notmuch_thread_t *thread,
|
2012-03-01 23:30:38 +01:00
|
|
|
notmuch_message_t *message,
|
2013-05-11 21:50:02 +02:00
|
|
|
notmuch_string_list_t *exclude_terms,
|
|
|
|
notmuch_exclude_t omit_exclude)
|
2009-10-26 22:46:14 +01:00
|
|
|
{
|
2009-10-30 01:31:07 +01:00
|
|
|
notmuch_tags_t *tags;
|
|
|
|
const char *tag;
|
2010-04-24 15:46:43 +02:00
|
|
|
InternetAddressList *list = NULL;
|
2009-11-12 18:59:47 +01:00
|
|
|
InternetAddress *address;
|
|
|
|
const char *from, *author;
|
2010-04-24 20:20:57 +02:00
|
|
|
char *clean_author;
|
2017-10-07 10:44:05 +02:00
|
|
|
bool message_excluded = false;
|
2013-05-11 21:50:02 +02:00
|
|
|
|
2013-05-13 17:10:51 +02:00
|
|
|
if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
|
|
|
|
for (tags = notmuch_message_get_tags (message);
|
|
|
|
notmuch_tags_valid (tags);
|
|
|
|
notmuch_tags_move_to_next (tags))
|
2013-05-11 21:50:02 +02:00
|
|
|
{
|
2013-05-13 17:10:51 +02:00
|
|
|
tag = notmuch_tags_get (tags);
|
|
|
|
/* Is message excluded? */
|
|
|
|
for (notmuch_string_node_t *term = exclude_terms->head;
|
|
|
|
term != NULL;
|
|
|
|
term = term->next)
|
|
|
|
{
|
2015-01-17 16:51:45 +01:00
|
|
|
/* Check for an empty string, and then ignore initial 'K'. */
|
|
|
|
if (*(term->string) && strcmp(tag, (term->string + 1)) == 0) {
|
2017-10-07 10:44:05 +02:00
|
|
|
message_excluded = true;
|
2013-05-13 17:10:51 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-05-11 21:50:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
|
|
|
|
return;
|
2009-11-12 18:59:47 +01:00
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
_notmuch_message_list_add_message (thread->message_list,
|
|
|
|
talloc_steal (thread, message));
|
|
|
|
thread->total_messages++;
|
2017-06-04 14:32:31 +02:00
|
|
|
thread->total_files += notmuch_message_count_files (message);
|
2009-11-16 05:39:25 +01:00
|
|
|
|
|
|
|
g_hash_table_insert (thread->message_hash,
|
|
|
|
xstrdup (notmuch_message_get_message_id (message)),
|
|
|
|
message);
|
|
|
|
|
2009-11-12 18:59:47 +01:00
|
|
|
from = notmuch_message_get_header (message, "from");
|
2010-04-24 15:46:43 +02:00
|
|
|
if (from)
|
|
|
|
list = internet_address_list_parse_string (from);
|
|
|
|
|
2009-11-12 18:59:47 +01:00
|
|
|
if (list) {
|
|
|
|
address = internet_address_list_get_address (list, 0);
|
|
|
|
if (address) {
|
|
|
|
author = internet_address_get_name (address);
|
2014-11-22 14:17:16 +01:00
|
|
|
/* We treat quoted empty names as if they were empty. */
|
|
|
|
if (author == NULL || author[0] == '\0') {
|
2009-11-12 18:59:47 +01:00
|
|
|
InternetAddressMailbox *mailbox;
|
|
|
|
mailbox = INTERNET_ADDRESS_MAILBOX (address);
|
|
|
|
author = internet_address_mailbox_get_addr (mailbox);
|
|
|
|
}
|
2010-04-24 20:20:57 +02:00
|
|
|
clean_author = _thread_cleanup_author (thread, author, from);
|
|
|
|
_thread_add_author (thread, clean_author);
|
2014-05-13 11:44:05 +02:00
|
|
|
_notmuch_message_set_author (message, clean_author);
|
2009-11-12 18:59:47 +01:00
|
|
|
}
|
|
|
|
g_object_unref (G_OBJECT (list));
|
|
|
|
}
|
2009-10-26 22:46:14 +01:00
|
|
|
|
2010-04-22 23:00:44 +02:00
|
|
|
if (! thread->subject) {
|
|
|
|
const char *subject;
|
|
|
|
subject = notmuch_message_get_header (message, "subject");
|
2010-04-24 15:46:43 +02:00
|
|
|
thread->subject = talloc_strdup (thread, subject ? subject : "");
|
2010-04-22 23:00:44 +02:00
|
|
|
}
|
|
|
|
|
2009-10-30 01:31:07 +01:00
|
|
|
for (tags = notmuch_message_get_tags (message);
|
2010-03-09 18:22:29 +01:00
|
|
|
notmuch_tags_valid (tags);
|
|
|
|
notmuch_tags_move_to_next (tags))
|
2009-10-30 01:31:07 +01:00
|
|
|
{
|
|
|
|
tag = notmuch_tags_get (tags);
|
|
|
|
g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
|
|
|
|
}
|
2013-05-11 21:50:02 +02:00
|
|
|
|
|
|
|
/* Mark excluded messages. */
|
|
|
|
if (message_excluded)
|
2017-10-07 10:44:05 +02:00
|
|
|
notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
|
2009-11-13 08:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-04-24 15:45:51 +02:00
|
|
|
_thread_set_subject_from_message (notmuch_thread_t *thread,
|
|
|
|
notmuch_message_t *message)
|
2009-11-13 08:10:04 +01:00
|
|
|
{
|
2010-04-22 23:00:44 +02:00
|
|
|
const char *subject;
|
|
|
|
const char *cleaned_subject;
|
2010-04-17 19:59:22 +02:00
|
|
|
|
2010-04-22 23:00:44 +02:00
|
|
|
subject = notmuch_message_get_header (message, "subject");
|
2010-04-24 15:46:43 +02:00
|
|
|
if (! subject)
|
|
|
|
return;
|
2010-04-17 19:59:22 +02:00
|
|
|
|
2010-04-22 23:00:44 +02:00
|
|
|
if ((strncasecmp (subject, "Re: ", 4) == 0) ||
|
|
|
|
(strncasecmp (subject, "Aw: ", 4) == 0) ||
|
|
|
|
(strncasecmp (subject, "Vs: ", 4) == 0) ||
|
|
|
|
(strncasecmp (subject, "Sv: ", 4) == 0)) {
|
|
|
|
|
|
|
|
cleaned_subject = talloc_strndup (thread,
|
|
|
|
subject + 4,
|
|
|
|
strlen(subject) - 4);
|
|
|
|
} else {
|
|
|
|
cleaned_subject = talloc_strdup (thread, subject);
|
|
|
|
}
|
|
|
|
|
2014-10-29 21:51:43 +01:00
|
|
|
if (! EMPTY_STRING(cleaned_subject)) {
|
|
|
|
if (thread->subject)
|
|
|
|
talloc_free (thread->subject);
|
2010-04-24 15:45:51 +02:00
|
|
|
|
2014-10-29 21:51:43 +01:00
|
|
|
thread->subject = talloc_strdup (thread, cleaned_subject);
|
|
|
|
}
|
2010-04-24 15:45:51 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
/* Add a message to this thread which is known to match the original
|
|
|
|
* search specification. The 'sort' parameter controls whether the
|
|
|
|
* oldest or newest matching subject is applied to the thread as a
|
|
|
|
* whole. */
|
2010-04-24 15:45:51 +02:00
|
|
|
static void
|
|
|
|
_thread_add_matched_message (notmuch_thread_t *thread,
|
|
|
|
notmuch_message_t *message,
|
|
|
|
notmuch_sort_t sort)
|
|
|
|
{
|
|
|
|
time_t date;
|
|
|
|
notmuch_message_t *hashed_message;
|
|
|
|
|
|
|
|
date = notmuch_message_get_date (message);
|
|
|
|
|
2010-12-08 01:26:38 +01:00
|
|
|
if (date < thread->oldest || ! thread->matched_messages) {
|
2010-04-24 15:45:51 +02:00
|
|
|
thread->oldest = date;
|
2010-12-08 01:26:38 +01:00
|
|
|
if (sort == NOTMUCH_SORT_OLDEST_FIRST)
|
|
|
|
_thread_set_subject_from_message (thread, message);
|
|
|
|
}
|
2010-04-24 15:45:51 +02:00
|
|
|
|
2010-12-08 01:26:38 +01:00
|
|
|
if (date > thread->newest || ! thread->matched_messages) {
|
2010-04-24 15:45:51 +02:00
|
|
|
thread->newest = date;
|
2015-01-17 16:51:46 +01:00
|
|
|
const char *cur_subject = notmuch_thread_get_subject(thread);
|
2014-10-29 21:51:43 +01:00
|
|
|
if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING(cur_subject))
|
2010-12-08 01:26:38 +01:00
|
|
|
_thread_set_subject_from_message (thread, message);
|
|
|
|
}
|
2010-04-17 19:59:22 +02:00
|
|
|
|
2012-03-01 23:30:38 +01:00
|
|
|
if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
|
|
|
|
thread->matched_messages++;
|
2009-11-25 05:54:34 +01:00
|
|
|
|
|
|
|
if (g_hash_table_lookup_extended (thread->message_hash,
|
|
|
|
notmuch_message_get_message_id (message), NULL,
|
|
|
|
(void **) &hashed_message)) {
|
|
|
|
notmuch_message_set_flag (hashed_message,
|
2009-11-28 03:20:09 +01:00
|
|
|
NOTMUCH_MESSAGE_FLAG_MATCH, 1);
|
2009-11-25 05:54:34 +01:00
|
|
|
}
|
2010-04-27 10:48:03 +02:00
|
|
|
|
2014-05-13 11:44:05 +02:00
|
|
|
_thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
|
2009-10-27 01:35:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 13:29:06 +02:00
|
|
|
static bool
|
|
|
|
_parent_via_in_reply_to (notmuch_thread_t *thread, notmuch_message_t *message) {
|
|
|
|
notmuch_message_t *parent;
|
|
|
|
const char *in_reply_to;
|
|
|
|
|
|
|
|
in_reply_to = _notmuch_message_get_in_reply_to (message);
|
|
|
|
THREAD_DEBUG("checking message = %s in_reply_to=%s\n",
|
|
|
|
notmuch_message_get_message_id (message), in_reply_to);
|
|
|
|
|
2018-09-03 17:26:10 +02:00
|
|
|
if (in_reply_to && (! EMPTY_STRING(in_reply_to)) &&
|
2018-08-30 13:29:06 +02:00
|
|
|
g_hash_table_lookup_extended (thread->message_hash,
|
|
|
|
in_reply_to, NULL,
|
|
|
|
(void **) &parent)) {
|
|
|
|
_notmuch_message_add_reply (parent, message);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
static void
|
2012-11-25 05:57:03 +01:00
|
|
|
_resolve_thread_relationships (notmuch_thread_t *thread)
|
2009-11-16 05:39:25 +01:00
|
|
|
{
|
2018-04-20 16:59:48 +02:00
|
|
|
notmuch_message_node_t *node, *first_node;
|
2018-08-30 13:29:06 +02:00
|
|
|
notmuch_message_t *message;
|
2009-11-16 05:39:25 +01:00
|
|
|
|
2018-04-20 16:59:48 +02:00
|
|
|
first_node = thread->message_list->head;
|
|
|
|
if (! first_node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (node = first_node->next; node; node = node->next) {
|
2009-11-16 05:39:25 +01:00
|
|
|
message = node->message;
|
2018-08-30 13:29:06 +02:00
|
|
|
if (! _parent_via_in_reply_to (thread, message))
|
2018-04-20 16:59:48 +02:00
|
|
|
_notmuch_message_list_add_message (thread->toplevel_list, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we reach the end of the list without finding a top-level
|
|
|
|
* message, that means the thread is a cycle (or set of cycles)
|
|
|
|
* and any message can be considered top-level. Choose the oldest
|
|
|
|
* message, which happens to be first in our list.
|
|
|
|
*/
|
|
|
|
if (first_node) {
|
|
|
|
message = first_node->message;
|
2018-08-30 13:29:06 +02:00
|
|
|
if (_notmuch_message_list_empty(thread->toplevel_list) ||
|
|
|
|
! _parent_via_in_reply_to (thread, message)) {
|
2012-11-25 05:57:03 +01:00
|
|
|
_notmuch_message_list_add_message (thread->toplevel_list, message);
|
2018-08-30 13:29:06 +02:00
|
|
|
}
|
2009-11-16 05:39:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 13:29:04 +02:00
|
|
|
/* XXX this could be made conditional on messages being inserted
|
|
|
|
* (out of order) in later passes
|
|
|
|
*/
|
|
|
|
thread->toplevel_list = _notmuch_message_sort_subtrees (thread, thread->toplevel_list);
|
|
|
|
|
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
/* XXX: After scanning through the entire list looking for parents
|
|
|
|
* via "In-Reply-To", we should do a second pass that looks at the
|
|
|
|
* list of messages IDs in the "References" header instead. (And
|
|
|
|
* for this the parent would be the "deepest" message of all the
|
|
|
|
* messages found in the "References" list.)
|
|
|
|
*
|
|
|
|
* Doing this will allow messages and sub-threads to be positioned
|
|
|
|
* correctly in the thread even when an intermediate message is
|
|
|
|
* missing from the thread.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2010-11-17 20:28:26 +01:00
|
|
|
/* Create a new notmuch_thread_t object by finding the thread
|
|
|
|
* containing the message with the given doc ID, treating any messages
|
|
|
|
* contained in match_set as "matched". Remove all messages in the
|
|
|
|
* thread from match_set.
|
2009-11-13 06:09:54 +01:00
|
|
|
*
|
2010-11-17 20:28:26 +01:00
|
|
|
* Creating the thread will perform a database search to get all
|
|
|
|
* messages belonging to the thread and will get the first subject
|
|
|
|
* line, the total count of messages, and all authors in the thread.
|
|
|
|
* Each message in the thread is checked against match_set to allow
|
|
|
|
* for a separate count of matched messages, and to allow a viewer to
|
|
|
|
* display these messages differently.
|
2009-11-13 06:09:54 +01:00
|
|
|
*
|
2009-11-13 07:01:44 +01:00
|
|
|
* Here, 'ctx' is talloc context for the resulting thread object.
|
2009-11-13 06:09:54 +01:00
|
|
|
*
|
|
|
|
* This function returns NULL in the case of any error.
|
|
|
|
*/
|
|
|
|
notmuch_thread_t *
|
2009-11-13 07:01:44 +01:00
|
|
|
_notmuch_thread_create (void *ctx,
|
2009-11-13 06:09:54 +01:00
|
|
|
notmuch_database_t *notmuch,
|
2010-11-17 20:28:26 +01:00
|
|
|
unsigned int seed_doc_id,
|
|
|
|
notmuch_doc_id_set_t *match_set,
|
2012-03-01 23:30:38 +01:00
|
|
|
notmuch_string_list_t *exclude_terms,
|
2013-05-11 21:50:02 +02:00
|
|
|
notmuch_exclude_t omit_excluded,
|
2010-04-17 19:59:22 +02:00
|
|
|
notmuch_sort_t sort)
|
2009-11-13 06:09:54 +01:00
|
|
|
{
|
2012-11-25 05:57:02 +01:00
|
|
|
void *local = talloc_new (ctx);
|
|
|
|
notmuch_thread_t *thread = NULL;
|
2010-11-17 20:28:26 +01:00
|
|
|
notmuch_message_t *seed_message;
|
|
|
|
const char *thread_id;
|
2011-01-25 14:40:35 +01:00
|
|
|
char *thread_id_query_string;
|
2010-04-15 18:15:10 +02:00
|
|
|
notmuch_query_t *thread_id_query;
|
|
|
|
|
2009-11-13 06:09:54 +01:00
|
|
|
notmuch_messages_t *messages;
|
2009-11-18 03:33:42 +01:00
|
|
|
notmuch_message_t *message;
|
2015-09-27 17:32:02 +02:00
|
|
|
notmuch_status_t status;
|
2009-11-13 06:09:54 +01:00
|
|
|
|
2012-11-25 05:57:02 +01:00
|
|
|
seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
|
2010-11-17 20:28:26 +01:00
|
|
|
if (! seed_message)
|
|
|
|
INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
|
|
|
|
|
|
|
|
thread_id = notmuch_message_get_thread_id (seed_message);
|
2012-11-25 05:57:02 +01:00
|
|
|
thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
|
2010-11-17 20:28:26 +01:00
|
|
|
if (unlikely (thread_id_query_string == NULL))
|
2012-11-25 05:57:02 +01:00
|
|
|
goto DONE;
|
2009-11-13 06:09:54 +01:00
|
|
|
|
2012-11-25 05:57:02 +01:00
|
|
|
thread_id_query = talloc_steal (
|
|
|
|
local, notmuch_query_create (notmuch, thread_id_query_string));
|
2009-11-13 07:01:44 +01:00
|
|
|
if (unlikely (thread_id_query == NULL))
|
2012-11-25 05:57:02 +01:00
|
|
|
goto DONE;
|
2009-11-13 07:01:44 +01:00
|
|
|
|
2012-11-25 05:57:02 +01:00
|
|
|
thread = talloc (local, notmuch_thread_t);
|
2009-11-13 06:09:54 +01:00
|
|
|
if (unlikely (thread == NULL))
|
2012-11-25 05:57:02 +01:00
|
|
|
goto DONE;
|
2009-11-13 06:09:54 +01:00
|
|
|
|
|
|
|
talloc_set_destructor (thread, _notmuch_thread_destructor);
|
|
|
|
|
|
|
|
thread->notmuch = notmuch;
|
|
|
|
thread->thread_id = talloc_strdup (thread, thread_id);
|
|
|
|
thread->subject = NULL;
|
2009-11-13 06:19:42 +01:00
|
|
|
thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
|
2010-04-27 10:48:03 +02:00
|
|
|
NULL, NULL);
|
|
|
|
thread->authors_array = g_ptr_array_new ();
|
|
|
|
thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
|
|
|
|
g_str_equal,
|
|
|
|
NULL, NULL);
|
2010-12-08 01:33:29 +01:00
|
|
|
thread->matched_authors_array = g_ptr_array_new ();
|
2009-11-13 06:09:54 +01:00
|
|
|
thread->authors = NULL;
|
|
|
|
thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
free, NULL);
|
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
thread->message_list = _notmuch_message_list_create (thread);
|
2012-11-25 05:57:03 +01:00
|
|
|
thread->toplevel_list = _notmuch_message_list_create (thread);
|
|
|
|
if (unlikely (thread->message_list == NULL ||
|
|
|
|
thread->toplevel_list == NULL)) {
|
2012-11-25 05:57:02 +01:00
|
|
|
thread = NULL;
|
|
|
|
goto DONE;
|
|
|
|
}
|
2009-11-16 05:39:25 +01:00
|
|
|
|
|
|
|
thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
free, NULL);
|
|
|
|
|
2009-11-13 07:01:44 +01:00
|
|
|
thread->total_messages = 0;
|
2017-06-04 14:32:31 +02:00
|
|
|
thread->total_files = 0;
|
2009-11-13 07:01:44 +01:00
|
|
|
thread->matched_messages = 0;
|
2009-11-13 06:09:54 +01:00
|
|
|
thread->oldest = 0;
|
|
|
|
thread->newest = 0;
|
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
/* We use oldest-first order unconditionally here to obtain the
|
|
|
|
* proper author ordering for the thread. The 'sort' parameter
|
|
|
|
* passed to this function is used only to indicate whether the
|
|
|
|
* oldest or newest subject is desired. */
|
2010-04-22 23:00:33 +02:00
|
|
|
notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
|
2009-11-13 06:09:54 +01:00
|
|
|
|
2017-02-26 22:21:32 +01:00
|
|
|
status = notmuch_query_search_messages (thread_id_query, &messages);
|
2015-09-27 17:32:02 +02:00
|
|
|
if (status)
|
|
|
|
goto DONE;
|
|
|
|
|
|
|
|
for (;
|
2010-03-09 18:22:29 +01:00
|
|
|
notmuch_messages_valid (messages);
|
|
|
|
notmuch_messages_move_to_next (messages))
|
2009-11-13 06:09:54 +01:00
|
|
|
{
|
2010-11-17 20:28:26 +01:00
|
|
|
unsigned int doc_id;
|
|
|
|
|
2009-11-18 03:33:42 +01:00
|
|
|
message = notmuch_messages_get (messages);
|
2010-11-17 20:28:26 +01:00
|
|
|
doc_id = _notmuch_message_get_doc_id (message);
|
|
|
|
if (doc_id == seed_doc_id)
|
|
|
|
message = seed_message;
|
2010-04-15 18:15:10 +02:00
|
|
|
|
2013-05-11 21:50:02 +02:00
|
|
|
_thread_add_message (thread, message, exclude_terms, omit_excluded);
|
2010-04-15 18:15:10 +02:00
|
|
|
|
2010-11-17 20:28:26 +01:00
|
|
|
if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
|
|
|
|
_notmuch_doc_id_set_remove (match_set, doc_id);
|
2010-04-22 23:00:17 +02:00
|
|
|
_thread_add_matched_message (thread, message, sort);
|
2010-11-17 20:28:26 +01:00
|
|
|
}
|
2010-04-15 18:15:10 +02:00
|
|
|
|
2009-11-18 03:33:42 +01:00
|
|
|
_notmuch_message_close (message);
|
2009-11-13 06:09:54 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 01:33:29 +01:00
|
|
|
_resolve_thread_authors_string (thread);
|
2010-04-27 10:48:03 +02:00
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
_resolve_thread_relationships (thread);
|
|
|
|
|
2012-11-25 05:57:02 +01:00
|
|
|
/* Commit to returning thread. */
|
2014-01-17 17:21:05 +01:00
|
|
|
(void) talloc_steal (ctx, thread);
|
2012-11-25 05:57:02 +01:00
|
|
|
|
|
|
|
DONE:
|
|
|
|
talloc_free (local);
|
2009-11-13 06:09:54 +01:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2009-11-16 05:39:25 +01:00
|
|
|
notmuch_messages_t *
|
|
|
|
notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
|
|
|
|
{
|
2012-11-25 05:57:03 +01:00
|
|
|
return _notmuch_messages_create (thread->toplevel_list);
|
2009-11-16 05:39:25 +01:00
|
|
|
}
|
|
|
|
|
2012-11-25 05:57:05 +01:00
|
|
|
notmuch_messages_t *
|
|
|
|
notmuch_thread_get_messages (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return _notmuch_messages_create (thread->message_list);
|
|
|
|
}
|
|
|
|
|
2009-11-13 06:09:54 +01:00
|
|
|
const char *
|
|
|
|
notmuch_thread_get_thread_id (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->thread_id;
|
|
|
|
}
|
|
|
|
|
2009-11-13 07:01:44 +01:00
|
|
|
int
|
|
|
|
notmuch_thread_get_total_messages (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->total_messages;
|
|
|
|
}
|
|
|
|
|
2017-06-04 14:32:31 +02:00
|
|
|
int
|
|
|
|
notmuch_thread_get_total_files (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->total_files;
|
|
|
|
}
|
|
|
|
|
2009-11-13 07:01:44 +01:00
|
|
|
int
|
|
|
|
notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->matched_messages;
|
|
|
|
}
|
|
|
|
|
2009-11-12 18:59:47 +01:00
|
|
|
const char *
|
|
|
|
notmuch_thread_get_authors (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->authors;
|
|
|
|
}
|
|
|
|
|
2009-10-27 01:35:31 +01:00
|
|
|
const char *
|
2009-10-27 04:11:58 +01:00
|
|
|
notmuch_thread_get_subject (notmuch_thread_t *thread)
|
2009-10-27 01:35:31 +01:00
|
|
|
{
|
|
|
|
return thread->subject;
|
|
|
|
}
|
|
|
|
|
2009-10-30 01:31:07 +01:00
|
|
|
time_t
|
|
|
|
notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->oldest;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
notmuch_thread_get_newest_date (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
return thread->newest;
|
|
|
|
}
|
|
|
|
|
2009-10-26 22:46:14 +01:00
|
|
|
notmuch_tags_t *
|
|
|
|
notmuch_thread_get_tags (notmuch_thread_t *thread)
|
|
|
|
{
|
2010-12-09 01:26:05 +01:00
|
|
|
notmuch_string_list_t *tags;
|
2009-10-26 22:46:14 +01:00
|
|
|
GList *keys, *l;
|
|
|
|
|
2010-12-09 01:26:05 +01:00
|
|
|
tags = _notmuch_string_list_create (thread);
|
2009-10-26 22:46:14 +01:00
|
|
|
if (unlikely (tags == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
keys = g_hash_table_get_keys (thread->tags);
|
|
|
|
|
|
|
|
for (l = keys; l; l = l->next)
|
2010-12-09 01:26:05 +01:00
|
|
|
_notmuch_string_list_append (tags, (char *) l->data);
|
2009-10-26 22:46:14 +01:00
|
|
|
|
|
|
|
g_list_free (keys);
|
|
|
|
|
2010-12-09 01:26:05 +01:00
|
|
|
_notmuch_string_list_sort (tags);
|
2009-10-26 22:46:14 +01:00
|
|
|
|
2010-12-09 01:26:05 +01:00
|
|
|
return _notmuch_tags_create (thread, tags);
|
2009-10-26 22:46:14 +01:00
|
|
|
}
|
|
|
|
|
2009-10-26 07:12:20 +01:00
|
|
|
void
|
|
|
|
notmuch_thread_destroy (notmuch_thread_t *thread)
|
|
|
|
{
|
|
|
|
talloc_free (thread);
|
|
|
|
}
|