index-search.c revision 6f338d07c32978a8118bd7e30a07dff067d0f8ee
/* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */
#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-search.h"
#include "message-parser.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;
};
static const enum message_header_parser_flags hdr_parser_flags =
struct mail_search_arg *args,
{
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:
return flags & MAIL_ANSWERED;
case SEARCH_DELETED:
return flags & MAIL_DELETED;
case SEARCH_DRAFT:
return flags & MAIL_DRAFT;
case SEARCH_FLAGGED:
return flags & MAIL_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)
{
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
struct mail_search_arg *arg)
{
int timezone_offset;
/* internal dates */
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
return -1;
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
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;
case SEARCH_SENTBEFORE:
case SEARCH_SENTON:
case SEARCH_SENTSINCE:
default:
/* unreachable */
break;
}
/* sizes */
case SEARCH_SMALLER:
case SEARCH_LARGER:
return -1;
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;
/* 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 message_search_context *
struct mail_search_arg *arg)
{
enum message_search_flags flags;
int ret;
return arg_ctx;
&arg_ctx);
if (ret > 0) {
return arg_ctx;
}
if (ret == 0)
else
return NULL;
}
struct search_header_context *ctx)
{
struct message_search_context *msg_search_ctx;
struct message_block block;
struct message_header_line hdr;
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. */
return;
}
return;
}
if (msg_search_ctx == NULL)
ret = 0;
/* we have to match against normalized address */
struct message_address *addr;
t_push();
(unsigned int)-1, TRUE);
t_pop();
} else {
}
if (ret > 0 ||
/* 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)
{
struct message_search_context *msg_search_ctx;
int ret;
return;
case SEARCH_BODY:
case SEARCH_BODY_FAST:
case SEARCH_TEXT:
case SEARCH_TEXT_FAST:
break;
default:
return;
}
if (msg_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);
&input) < 0) {
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;
}
{
return FALSE;
if (not)
continue;
/* completely outside our range */
return FALSE;
}
/* either seq1 or seq2 is '*', so the last message is
in range. */
}
/* this shouldn't happen. treat as nonexisting. */
return FALSE;
}
}
return TRUE;
}
{
}
static struct mail_search_seqset *
{
unsigned int i, count;
count++;
/* @UNSAFE */
for (i = 0; i < count-1; i++)
return set;
}
struct mail_search_seqset **last_r)
{
struct mail_search_seqset *cur;
} else {
}
}
}
struct mail_search_seqset **set_p,
{
struct mail_search_seqset *last;
*seq2_r = 0;
return;
}
if (!not) {
} else {
*seq2_r = 0;
return;
}
}
}
struct mail_search_arg *args,
{
}
if (min_seq1 == 0) {
} else {
}
}
}
struct mail_search_arg *args,
{
/* go through our children and use the widest seqset
range */
}
}
}
{
if (uid_lowwater == 0)
return;
}
const struct mail_index_header *hdr,
struct mail_search_arg *args,
{
case SEARCH_SEEN:
/* SEEN with 0 seen? */
return FALSE;
/* UNSEEN with all seen? */
return FALSE;
/* SEEN with all seen */
/* UNSEEN with lowwater limiting */
}
break;
case SEARCH_DELETED:
/* DELETED with 0 deleted? */
return FALSE;
if (hdr->deleted_messages_count ==
hdr->messages_count) {
/* UNDELETED with all deleted? */
return FALSE;
/* DELETED with all deleted */
/* DELETED with lowwater limiting */
}
break;
case SEARCH_ALL:
return FALSE;
break;
default:
break;
}
}
}
struct mail_search_arg *args)
{
const struct mail_index_header *hdr;
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;
}
}
/* no matches */
return;
}
/* UNSEEN and DELETED in root search level may limit the range */
/* no matches */
}
}
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);
/* Need to reset results for match_always cases */
}
void *context ATTR_UNUSED)
{
if (search_ctx != NULL) {
}
}
{
int ret;
}
return ret;
}
{
struct mail_search_arg *arg;
int ret;
/* check the index matches first */
if (ret >= 0)
return ret > 0;
/* 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 */
return 0;
return 1;
}
t_push();
t_pop();
ret = -1;
if (ret != 0) {
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 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;
}