/* notmuch - Not much of an email program, (just index and search)
*
* 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
* along with this program. If not, see https://www.gnu.org/licenses/ .
*
* Author: Carl Worth <cworth@cworth.org>
*/
#include "notmuch-client.h"
#include "sprinter.h"
#include "string-util.h"
typedef enum {
/* Search command */
OUTPUT_SUMMARY = 1 << 0,
OUTPUT_THREADS = 1 << 1,
OUTPUT_MESSAGES = 1 << 2,
OUTPUT_FILES = 1 << 3,
OUTPUT_TAGS = 1 << 4,
/* Address command */
OUTPUT_SENDER = 1 << 5,
OUTPUT_RECIPIENTS = 1 << 6,
OUTPUT_COUNT = 1 << 7,
OUTPUT_ADDRESS = 1 << 8,
} output_t;
typedef enum {
DEDUP_NONE,
DEDUP_MAILBOX,
DEDUP_ADDRESS,
} dedup_t;
typedef enum {
NOTMUCH_FORMAT_JSON,
NOTMUCH_FORMAT_TEXT,
NOTMUCH_FORMAT_TEXT0,
NOTMUCH_FORMAT_SEXP
} format_sel_t;
typedef struct {
notmuch_database_t *notmuch;
void *talloc_ctx;
int format_sel;
sprinter_t *format;
int exclude;
int query_syntax;
notmuch_query_t *query;
int sort;
int output;
int offset;
int limit;
int dupe;
GHashTable *addresses;
int dedup;
} search_context_t;
typedef struct {
const char *name;
const char *addr;
int count;
} mailbox_t;
/* Return two stable query strings that identify exactly the matched
* and unmatched messages currently in thread. If there are no
* matched or unmatched messages, the returned buffers will be
* NULL. */
static int
get_thread_query (notmuch_thread_t *thread,
char **matched_out, char **unmatched_out)
{
notmuch_messages_t *messages;
char *escaped = NULL;
size_t escaped_len = 0;
*matched_out = *unmatched_out = NULL;
for (messages = notmuch_thread_get_messages (thread);
notmuch_messages_valid (messages);
notmuch_messages_move_to_next (messages)) {
notmuch_message_t *message = notmuch_messages_get (messages);
const char *mid = notmuch_message_get_message_id (message);
notmuch_bool_t is_set;
char **buf;
if (notmuch_message_get_flag_st (message, NOTMUCH_MESSAGE_FLAG_MATCH, &is_set))
return -1;
/* Determine which query buffer to extend */
buf = is_set ? matched_out : unmatched_out;
/* Add this message's id: query. Since "id" is an exclusive
* prefix, it is implicitly 'or'd together, so we only need to
* join queries with a space. */
if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
return -1;
if (*buf)
*buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
else
*buf = talloc_strdup (thread, escaped);
if (! *buf)
return -1;
}
talloc_free (escaped);
return 0;
}
static int
do_search_threads (search_context_t *ctx)
{
notmuch_thread_t *thread;
notmuch_threads_t *threads;
notmuch_tags_t *tags;
sprinter_t *format = ctx->format;
time_t date;
int i;
notmuch_status_t status;
if (ctx->offset < 0) {
unsigned count;
notmuch_status_t status;
status = notmuch_query_count_threads (ctx->query, &count);
if (print_status_query ("notmuch search", ctx->query, status))
return 1;
ctx->offset += count;
if (ctx->offset < 0)
ctx->offset = 0;
}
status = notmuch_query_search_threads (ctx->query, &threads);
if (print_status_query ("notmuch search", ctx->query, status))
return 1;
format->begin_list (format);
for (i = 0;
notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
notmuch_threads_move_to_next (threads), i++) {
thread = notmuch_threads_get (threads);
if (i < ctx->offset) {
notmuch_thread_destroy (thread);
continue;
}
if (ctx->output == OUTPUT_THREADS) {
format