index-search.c revision 043c8a96a035379bcba04f487d58457beefdfcaa
/* Copyright (C) 2002-2006 Timo Sirainen */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "istream.h"
#include "str.h"
#include "message-address.h"
#include "message-date.h"
#include "message-body-search.h"
#include "message-header-search.h"
#include "message-parser.h"
#include "imap-date.h"
#include "index-storage.h"
#include "index-mail.h"
#include "index-sort.h"
#include "mail-search.h"
#include <stdlib.h>
#include <ctype.h>
#define TXT_UNKNOWN_CHARSET "[BADCHARSET] Unknown charset"
#define TXT_INVALID_SEARCH_KEY "Invalid search key"
#define SEARCH_NONBLOCK_COUNT 20
#define SEARCH_NOTIFY_INTERVAL_SECS 10
struct index_search_context {
struct mail_search_context mail_ctx;
struct mail_index_view *view;
struct index_mailbox *ibox;
struct index_mail *imail;
const char *error;
unsigned int failed:1;
unsigned int sorted:1;
unsigned int have_seqsets:1;
};
struct search_header_context {
struct index_search_context *index_context;
struct mail_search_arg *args;
struct message_header_line *hdr;
unsigned int parse_headers:1;
unsigned int custom_header:1;
unsigned int threading:1;
};
struct search_body_context {
struct index_search_context *index_ctx;
const struct message_part *part;
};
struct search_arg_context {
};
static const enum message_header_parser_flags hdr_parser_flags =
const struct mail_index_header *hdr,
struct mail_search_arg *args,
bool not);
{
return TRUE;
}
return FALSE;
}
{
num = 0;
while (*str != '\0') {
return 0;
str++;
}
return num;
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
enum mail_search_arg_type type,
const char *value)
{
const char *const *keywords;
switch (type) {
/* flags */
case SEARCH_ANSWERED:
case SEARCH_DELETED:
case SEARCH_DRAFT:
case SEARCH_FLAGGED:
case SEARCH_SEEN:
case SEARCH_RECENT:
case SEARCH_KEYWORD:
return 1;
keywords++;
}
}
return 0;
default:
return -1;
}
}
struct index_search_context *ctx)
{
case SEARCH_SEQSET:
break;
case SEARCH_ALL:
break;
default:
break;
}
}
struct index_search_context *ctx)
{
else
ARG_SET_RESULT(arg, 0);
}
}
struct index_search_context *ctx)
{
/* expunged message */
ARG_SET_RESULT(arg, 0);
return;
}
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
enum mail_search_arg_type type,
const char *value)
{
int timezone_offset;
switch (type) {
/* internal dates */
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
return -1;
return 0;
switch (type) {
case SEARCH_BEFORE:
return date < search_time;
case SEARCH_ON:
return date >= search_time &&
case SEARCH_SINCE:
return date >= search_time;
default:
/* unreachable */
break;
}
/* sent dates */
case SEARCH_SENTBEFORE:
case SEARCH_SENTON:
case SEARCH_SENTSINCE:
/* NOTE: RFC-3501 specifies that timezone is ignored
in searches. date is returned as UTC, so change it. */
return -1;
return 0;
switch (type) {
case SEARCH_SENTBEFORE:
return date < search_time;
case SEARCH_SENTON:
return date >= search_time &&
case SEARCH_SENTSINCE:
return date >= search_time;
default:
/* unreachable */
break;
}
/* sizes */
case SEARCH_SMALLER:
case SEARCH_LARGER:
return -1;
if (type == SEARCH_SMALLER)
return virtual_size < search_size;
else
return virtual_size > search_size;
default:
return -1;
}
}
struct index_search_context *ctx)
{
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
{
int timezone_offset;
if (sent_value == NULL)
return 0;
return 0;
/* NOTE: RFC-3501 specifies that timezone is ignored
in searches. sent_time is returned as UTC, so change it. */
&sent_time, &timezone_offset))
return 0;
switch (type) {
case SEARCH_SENTBEFORE:
return sent_time < search_time;
case SEARCH_SENTON:
return sent_time >= search_time &&
case SEARCH_SENTSINCE:
return sent_time >= search_time;
default:
i_unreached();
}
}
static struct search_arg_context *
struct mail_search_arg *arg)
{
return arg_ctx;
return arg_ctx;
}
static struct message_header_search_context *
struct mail_search_arg *arg)
{
struct search_arg_context *arg_ctx;
int ret;
return arg_ctx->hdr_search_ctx;
}
if (ret > 0)
return arg_ctx->hdr_search_ctx;
if (ret == 0)
else
return NULL;
}
static struct message_body_search_context *
struct mail_search_arg *arg)
{
struct search_arg_context *arg_ctx;
int ret;
return arg_ctx->body_search_ctx;
if (ret > 0)
return arg_ctx->body_search_ctx;
if (ret == 0)
else
return NULL;
}
struct search_header_context *ctx)
{
int ret;
/* first check that the field name matches to argument. */
case SEARCH_SENTBEFORE:
case SEARCH_SENTON:
case SEARCH_SENTSINCE:
/* date is handled differently than others */
return;
}
}
return;
case SEARCH_HEADER:
case SEARCH_HEADER_ADDRESS:
return;
break;
default:
return;
}
/* we're just testing existence of the field. always matches. */
ret = 1;
} else {
return;
}
t_push();
if (hdr_search_ctx == NULL)
ret = 0;
/* we have to match against normalized address */
struct message_address *addr;
(unsigned int)-1, TRUE);
} else {
ret = 1;
else
ret = 0;
}
t_pop();
}
if (ret == 1 ||
/* set only when we definitely know if it's a match */
}
}
void *context __attr_unused__)
{
case SEARCH_SENTBEFORE:
case SEARCH_SENTON:
case SEARCH_SENTSINCE:
/* date header not found, so we match only for
NOT searches */
ARG_SET_RESULT(arg, 0);
}
break;
case SEARCH_HEADER:
case SEARCH_HEADER_ADDRESS:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
struct search_header_context *ctx)
{
/* end of headers, mark all unknown SEARCH_HEADERs unmatched */
return;
}
return;
if (ctx->parse_headers)
}
}
struct search_body_context *ctx)
{
int ret;
return;
case SEARCH_BODY:
case SEARCH_BODY_FAST:
case SEARCH_TEXT:
case SEARCH_TEXT_FAST:
break;
default:
return;
}
if (body_search_ctx == NULL) {
ARG_SET_RESULT(arg, 0);
return;
}
if (ret < 0) {
"Broken message structure for mail UID %u",
/* get the body parts, and try again */
if (ret < 0)
i_panic("Couldn't fix broken body structure");
}
}
struct index_search_context *ctx)
{
struct mailbox_header_lookup_ctx *headers_ctx;
const char *const *headers;
bool have_headers, have_body;
/* first check what we need to use */
if (!have_headers && !have_body)
return TRUE;
if (have_headers) {
struct search_header_context hdr_ctx;
if (have_body)
headers_ctx = NULL;
return FALSE;
} else {
/* FIXME: do this once in init */
headers);
return FALSE;
}
}
if (hdr_ctx.parse_headers)
search_header, &hdr_ctx);
if (headers_ctx != NULL)
} else {
struct message_size hdr_size;
return FALSE;
}
if (have_body) {
struct search_body_context body_ctx;
}
return TRUE;
}
const struct mail_index_header *hdr,
{
if (!not) {
/* seq1..seq2 */
} else {
/* seq2+1..count */
/* completely outside our range */
*seq2_r = 0;
} else {
}
/* 1..seq1-1 */
}
}
}
const struct mail_index_header *hdr,
struct mail_search_seqset *set,
{
struct mail_search_seqset full_set;
if (not)
continue;
/* completely outside our range */
*seq2_r = 0;
return 0;
}
/* either seq1 or seq2 is '*', so the last message is
in range. */
}
"Invalid messageset");
return -1;
}
}
return 0;
}
const struct mail_index_header *hdr,
struct mail_search_arg *args,
bool not)
{
if (not)
return -1;
cur_not) < 0)
return -1;
return -1;
}
if (min_seq1 == 0) {
} else {
}
}
return 0;
}
const struct mail_index_header *hdr,
struct mail_search_arg *args,
bool not)
{
if (not)
cur_not) < 0)
return -1;
/* go through our children and use the widest seqset
range */
cur_not) < 0)
return -1;
return -1;
}
}
return 0;
}
{
if (uid_lowwater == 0)
return 0;
return -1;
}
return 0;
}
const struct mail_index_header *hdr,
struct mail_search_arg *args,
{
case SEARCH_SEEN:
/* SEEN with 0 seen? */
return 0;
/* UNSEEN with all seen? */
return 0;
/* SEEN with all seen */
/* UNSEEN with lowwater limiting */
if (search_limit_lowwater(ctx,
seq1) < 0)
return -1;
}
break;
case SEARCH_DELETED:
/* DELETED with 0 deleted? */
return 0;
if (hdr->deleted_messages_count ==
hdr->messages_count) {
/* UNDELETED with all deleted? */
return 0;
/* DELETED with all deleted */
/* DELETED with lowwater limiting */
if (search_limit_lowwater(ctx,
seq1) < 0)
return -1;
}
break;
case SEARCH_ALL:
return 0;
break;
default:
break;
}
}
}
struct mail_search_arg *args)
{
const struct mail_index_header *hdr;
int ret;
if (hdr->messages_count == 0) {
/* no messages, don't check sequence ranges. although we could
give error message then for FETCH, we shouldn't do it for
UID FETCH. */
return 0;
}
return -1;
}
/* no matches */
return 0;
}
/* UNSEEN and DELETED in root search level may limit the range */
if (ret < 0)
return -1;
if (ret == 0) {
/* no matches */
}
return 0;
}
struct mail_search_context *
const enum mail_sort_type *sort_program)
{
struct index_transaction_context *t =
(struct index_transaction_context *)_t;
struct index_search_context *ctx;
sizeof(void *), 5);
} else {
ctx);
/* Need to reset results for match_always cases */
}
}
{
int ret;
}
return ret;
}
{
struct mail_search_arg *arg;
int ret;
/* check the index matches first */
if (ret >= 0)
return ret > 0;
/* expunged message, no way to check if the rest would have
matched */
return FALSE;
}
/* next search only from cached arguments */
if (ret >= 0)
return ret > 0;
/* open the mail file and check the rest */
return FALSE;
return FALSE;
}
return TRUE;
}
struct index_search_context *ctx)
{
const struct mail_index_header *hdr;
const char *text;
float percentage;
/* set the search time in here, in case a plugin
already spent some time indexing the mailbox */
t_push();
"ETA %d:%02d", (int)percentage,
t_pop();
}
}
{
unsigned int count = 0;
int ret;
*tryagain_r = FALSE;
/* everything searched at this point already. just returning
matches from sort list */
}
ret = -1;
break;
}
t_push();
t_pop();
ret = -1;
if (ret != 0) {
break;
mail) < 0) {
ret = -1;
break;
}
}
if (++count == SEARCH_NONBLOCK_COUNT) {
*tryagain_r = TRUE;
return 0;
}
}
if (ret < 0)
/* finished searching the messages. now sort them and start
returning the messages. */
return -1;
}
return ret;
}
{
int ret;
/* first time */
} else {
}
if (!ctx->have_seqsets)
ret = 0;
/* check if the sequence matches */
if (ret != 0)
break;
/* doesn't, try next one */
}
return ret == 0 ? 0 : 1;
}