index-search.c revision 8282d9cd182f6d6ed4e015abe8fb3a1712ebc3b9
/* Copyright (C) 2002 Timo Sirainen */
#include "lib.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 "imap-date.h"
#include "imap-envelope.h"
#include "index-storage.h"
#include "index-messageset.h"
#include "index-mail.h"
#include "mail-custom-flags.h"
#include "mail-modifylog.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"
struct mail_search_context {
struct index_mailbox *ibox;
char *charset;
struct mail_search_arg *args;
struct messageset_context *msgset_ctx;
struct index_mail imail;
const char *error;
int failed;
};
struct search_header_context {
struct mail_search_context *index_context;
struct mail_search_arg *args;
struct message_header_line *hdr;
unsigned int custom_header:1;
unsigned int threading:1;
};
struct search_body_context {
struct mail_search_context *index_ctx;
const struct message_part *part;
};
unsigned int max_num)
{
while (*set != '\0') {
if (*set == '*') {
set++;
} else {
num = 0;
set++;
}
if (num == 0)
return FALSE;
}
return TRUE;
if (*set == '\0')
return FALSE;
} else if (*set == ':') {
set++;
if (*set == '*') {
set++;
return TRUE;
} else {
num2 = 0;
set++;
}
if (num2 == 0)
return FALSE;
/* swap, as specified by RFC-3501 */
}
return TRUE;
}
if (*set != ',')
return FALSE;
}
set++;
}
return FALSE;
}
{
num = 0;
while (*str != '\0') {
return 0;
str++;
}
return num;
}
{
const char **custom_flags;
int i;
return FALSE;
for (i = 0; i < MAIL_CUSTOM_FLAGS_COUNT; i++) {
if (custom_flags[i] != NULL &&
(1 << (MAIL_CUSTOM_FLAG_1_BIT+i));
}
}
return FALSE;
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
struct mail_index_record *rec,
unsigned int client_seq,
enum mail_search_arg_type type,
const char *value)
{
switch (type) {
case SEARCH_ALL:
return 1;
case SEARCH_SET:
case SEARCH_UID:
/* flags */
case SEARCH_ANSWERED:
case SEARCH_DELETED:
case SEARCH_DRAFT:
case SEARCH_FLAGGED:
case SEARCH_SEEN:
case SEARCH_RECENT:
case SEARCH_KEYWORD:
default:
return -1;
}
}
{
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)
{
switch (type) {
/* internal dates */
case SEARCH_BEFORE:
case SEARCH_ON:
case SEARCH_SINCE:
return -1;
return 0;
switch (type) {
case SEARCH_BEFORE:
return internal_date < search_time;
case SEARCH_ON:
return internal_date >= search_time &&
case SEARCH_SINCE:
return internal_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;
}
}
{
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 header_search_context *
struct mail_search_arg *arg)
{
int unknown_charset;
}
}
}
}
/* Returns >0 = matched, 0 = not matched, -1 = unknown */
struct mail_search_arg *arg)
{
enum imap_envelope_field env_field;
struct header_search_context *hdr_search_ctx;
int ret;
case SEARCH_SENTBEFORE:
case SEARCH_SENTON:
case SEARCH_SENTSINCE:
break;
case SEARCH_FROM:
break;
case SEARCH_TO:
break;
case SEARCH_CC:
break;
case SEARCH_BCC:
break;
case SEARCH_SUBJECT:
break;
case SEARCH_IN_REPLY_TO:
break;
case SEARCH_MESSAGE_ID:
break;
default:
return -1;
}
t_push();
/* get field from hopefully cached envelope */
} else {
ret = -1;
}
if (ret != -1) {
case SEARCH_SENTBEFORE:
case SEARCH_SENTON:
case SEARCH_SENTSINCE:
(const unsigned char *) field,
(size_t)-1);
break;
default:
/* we're just testing existence of the field.
assume it matches with non-NIL values. */
break;
}
/* doesn't exist */
ret = 0;
break;
}
if (hdr_search_ctx == NULL) {
ret = 0;
break;
}
(const unsigned char *) field,
hdr_search_ctx) ? 1 : 0;
}
}
t_pop();
return ret;
}
{
case -1:
/* unknown */
break;
case 0:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
{
struct header_search_context *hdr_search_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_FROM:
return;
break;
case SEARCH_TO:
return;
break;
case SEARCH_CC:
return;
break;
case SEARCH_BCC:
return;
break;
case SEARCH_SUBJECT:
return;
break;
case SEARCH_IN_REPLY_TO:
return;
break;
case SEARCH_MESSAGE_ID:
return;
break;
case SEARCH_HEADER:
return;
case SEARCH_TEXT:
/* TEXT goes through all headers */
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;
0);
hdr_search_ctx) ? 1 : 0;
} else {
hdr_search_ctx) ? 1 : 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_FROM:
case SEARCH_TO:
case SEARCH_CC:
case SEARCH_BCC:
case SEARCH_SUBJECT:
case SEARCH_HEADER:
case SEARCH_IN_REPLY_TO:
case SEARCH_MESSAGE_ID:
ARG_SET_RESULT(arg, 0);
break;
default:
break;
}
}
{
/* end of headers, mark all unknown SEARCH_HEADERs unmatched */
return;
}
return;
if (ctx->custom_header ||
}
}
{
int ret, unknown_charset;
return;
if (ret < 0) {
}
}
}
struct mail_search_context *ctx)
{
/* first check what we need to use */
return TRUE;
if (have_headers || have_text) {
struct search_header_context hdr_ctx;
return FALSE;
search_header, &hdr_ctx);
} else {
struct message_size hdr_size;
return FALSE;
}
struct search_body_context body_ctx;
}
return TRUE;
}
{
unsigned int seq;
while (*set != '\0') {
if (*set == '*') {
set++;
} else {
seq = 0;
set++;
}
}
if (seq == 0)
return FALSE;
if (*set != '\0') {
if (*set == ',')
else
return FALSE;
set++;
}
}
return TRUE;
}
const struct mail_search_arg *args,
{
/* FIXME: we don't check if OR condition can limit the range.
It's a bit tricky and unlikely to affect performance much. */
return FALSE;
"Invalid messageset: %s",
return FALSE;
}
"Invalid messageset: %s",
return FALSE;
}
/* go through everything */
*first_seq = 1;
return TRUE;
}
}
return TRUE;
}
const struct mail_search_arg *args,
unsigned int *first_uid,
unsigned int *last_uid)
{
struct mail_index_header *hdr;
unsigned int uid;
/* SEEN with 0 seen? */
return FALSE;
/* UNSEEN with all seen? */
return FALSE;
/* UNSEEN with lowwater limiting */
}
/* DELETED with 0 deleted? */
return FALSE;
/* UNDELETED with all deleted? */
return FALSE;
/* DELETED with lowwater limiting */
}
}
}
}
{
struct mail_index_record *rec;
unsigned int expunges_before;
"Sequence out of range: %u", seq);
return FALSE;
}
&expunges_before) == NULL)
return FALSE;
seq -= expunges_before;
return TRUE;
}
const struct mail_search_arg *args,
{
return -1;
/* seq_update() should make sure that these can't happen */
if (first_seq > 1) {
return -1;
if (uid == 0)
return 0;
}
return -1;
if (uid == 0)
return 0;
}
if (*first_uid == 0)
*first_uid = 1;
/* UNSEEN and DELETED in root search level may limit the range */
return 0;
return 1;
}
enum mail_sort_type *sort_program)
{
/* currently we don't support sorting */
return TRUE;
}
struct mail_search_context *
struct mail_search_arg *args,
const enum mail_sort_type *sort_program,
const char *const wanted_headers[])
{
struct mail_search_context *ctx;
i_error("BUG: index_storage_search_init(): "
"invalid sort_program");
return NULL;
}
return NULL;
if (ibox->synced_messages_count == 0)
return ctx;
/* see if we can limit the records we look at */
case -1:
/* error */
return ctx;
case 0:
/* nothing found */
return ctx;
}
ctx->msgset_ctx =
return ctx;
}
{
int ret;
}
}
return ret;
}
{
const struct messageset_mail *msgset_mail;
struct mail_search_arg *arg;
/* initialization failed or didn't found any messages */
return NULL;
}
do {
if (msgset_mail == NULL)
return NULL;
if (ret < 0)
return NULL;
if (ret == 0)
else {
t_push();
ctx);
ctx);
ctx);
t_pop();
return NULL;
}
if (found) {
break;
}
}
}
} while (!found);
}