#include "database-private.h" #include "parse-sexp.h" #include "parse-time-vrp.h" #include "query-fp.h" #include "regexp-fields.h" typedef struct { const char *name; Xapian::Query::op xapian_op; Xapian::Query initial; } _sexp_op_t; typedef enum { SEXP_FLAG_NONE = 0, SEXP_FLAG_WILDCARD = 1 << 0, SEXP_FLAG_REGEXP = 1 << 1, SEXP_FLAG_THREAD_MATCH = 1 << 2, } _sexp_flag_t; /* * define bitwise operators to hide casts */ inline _sexp_flag_t operator| (_sexp_flag_t a, _sexp_flag_t b) { return static_cast<_sexp_flag_t>( static_cast(a) | static_cast(b)); } inline _sexp_flag_t operator& (_sexp_flag_t a, _sexp_flag_t b) { return static_cast<_sexp_flag_t>( static_cast(a) & static_cast(b)); } typedef struct { const char *name; Xapian::Query::op xapian_op; _sexp_flag_t flags_allowed; } _sexp_field_t; static _sexp_op_t operations[] = { { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll }, { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll }, { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing }, { } }; static _sexp_field_t fields[] = { { "attachment", Xapian::Query::OP_PHRASE, SEXP_FLAG_WILDCARD }, { "body", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { "date", Xapian::Query::OP_INVALID, SEXP_FLAG_NONE }, { "from", Xapian::Query::OP_PHRASE, SEXP_FLAG_REGEXP }, { "folder", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "id", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "is", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEXP }, { "mid", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "mimetype", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { "path", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP }, { "property", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEXP }, { "query", Xapian::Query::OP_INVALID, SEXP_FLAG_NONE }, { "subject", Xapian::Query::OP_PHRASE, SEXP_FLAG_REGEXP }, { "tag", Xapian::Query::OP_AND, SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEXP }, { "thread", Xapian::Query::OP_OR, SEXP_FLAG_REGEXP | SEXP_FLAG_THREAD_MATCH }, { "to", Xapian::Query::OP_PHRASE, SEXP_FLAG_NONE }, { } }; typedef struct { const char *name; _sexp_flag_t flag; } _sexp_keyword_t; static _sexp_keyword_t keywords[] = { { "any", SEXP_FLAG_WILDCARD }, { "*", SEXP_FLAG_WILDCARD }, { "rx", SEXP_FLAG_REGEXP }, { "match", SEXP_FLAG_THREAD_MATCH }, { } }; static notmuch_status_t _sexp_combine_query (notmuch_database_t *notmuch, Xapian::Query::op operation, Xapian::Query left, const sexp_t *sx, Xapian::Query &output) { Xapian::Query subquery; notmuch_status_t status; /* if we run out elements, return accumulator */ if (! sx) { output = left; return NOTMUCH_STATUS_SUCCESS; } status = _notmuch_sexp_to_xapian_query (notmuch, sx, subquery); if (status) return status; return _sexp_combine_query (notmuch, operation, Xapian::Query (operation, left, subquery), sx->next, output); } notmuch_status_t _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr, Xapian::Query &output) { const sexp_t *sx = NULL; char *buf = talloc_strdup (notmuch, querystr); sx = parse_sexp (buf, strlen (querystr)); if (! sx) return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; return _notmuch_sexp_to_xapian_query (notmuch, sx, output); } static void _sexp_find_words (const char *str, std::string pref_str, std::vector &terms) { Xapian::Utf8Iterator p (str); Xapian::Utf8Iterator end; while (p != end) { Xapian::Utf8Iterator start; while (p != end && ! Xapian::Unicode::is_wordchar (*p)) p++; if (p == end) break; start = p; while (p != end && Xapian::Unicode::is_wordchar (*p)) p++; if (p != start) { std::string word (start, p); word = Xapian::Unicode::tolower (word); terms.push_back (pref_str + word); } } } static notmuch_status_t _sexp_combine_field (const char *prefix, Xapian::Query::op operation, const sexp_t *sx, Xapian::Query &output) { std::vector terms; for (const sexp_t *cur = sx; cur; cur = cur->next) { std::string pref_str = prefix; if (operation == Xapian::Query::OP_PHRASE) { _sexp_find_words (cur->val, pref_str, terms); } else { terms.push_back (pref_str + cur->val); } } output = Xapian::Query (operation, terms.begin (), terms.end ()); return NOTMUCH_STATUS_SUCCESS; } static notmuch_status_t _sexp_parse_date (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output) { std::string begin, end, msg; notmuch_status_t status; const sexp_t *cur = sx->list->next; if (cur) { begin = cur->val; cur = cur->next; if (cur) { end = cur->val; cur = cur->next; if (cur) { _notmuch_database_log_append (notmuch, "extra argument(s) to date"); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } status = _notmuch_time_range_to_query (NOTMUCH_VALUE_TIMESTAMP, begin, end, msg, output); } else { status = _notmuch_time_string_to_query (NOTMUCH_VALUE_TIMESTAMP, begin, msg, output); } if (status) _notmuch_database_log_append (notmuch, "%s", msg.c_str ()); return status; } else { _notmuch_database_log_append (notmuch, "missing argument(s) to date"); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } } static notmuch_status_t _sexp_parse_keywords (notmuch_database_t *notmuch, const char *prefix, const sexp_t *sx, _sexp_flag_t mask, _sexp_flag_t &flags, const sexp_t *&rest) { flags = SEXP_FLAG_NONE; for (; sx && sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ':'; sx = sx->next) { _sexp_keyword_t *keyword; for (keyword = keywords; keyword && keyword->name; keyword++) { if (strcasecmp (keyword->name, sx->val + 1) == 0) { if ((mask & keyword->flag) == 0) { _notmuch_database_log_append (notmuch, "unsupported keyword '%s' for prefix '%s'\n", sx->val, prefix); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } else { flags = flags | keyword->flag; break; } } } if (! keyword || ! keyword->name) { _notmuch_database_log (notmuch, "unknown keyword %s\n", sx->val); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } } rest = sx; return NOTMUCH_STATUS_SUCCESS; } /* Here we expect the s-expression to be a proper list, with first * element defining and operation, or as a special case the empty * list */ notmuch_status_t _notmuch_sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output) { const _sexp_op_t *op; if (sx->ty == SEXP_VALUE) { Xapian::Query accumulator; for (const _sexp_field_t *field = fields; field && field->name; field++) { std::vector terms; if (field->xapian_op == Xapian::Query::OP_INVALID) continue; _sexp_find_words (sx->val, _find_prefix (field->name), terms); accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ())); } output = accumulator; return NOTMUCH_STATUS_SUCCESS; } /* Empty list */ if (! sx->list) { output = Xapian::Query::MatchAll; return NOTMUCH_STATUS_SUCCESS; } for (op = operations; op && op->name; op++) { if (strcasecmp (op->name, hd_sexp (sx)->val) == 0) { return _sexp_combine_query (notmuch, op->xapian_op, op->initial, sx->list->next, output); } } for (const _sexp_field_t *field = fields; field && field->name; field++) { if (strcasecmp (field->name, hd_sexp (sx)->val) == 0) { _sexp_flag_t flags; const char *term_prefix; const sexp_t *rest; notmuch_status_t status = _sexp_parse_keywords (notmuch, sx->list->val, sx->list->next, field->flags_allowed, flags, rest); if (status) return status; /* some fields need to be handled specially */ if (strcasecmp (field->name, "date") == 0) { return _sexp_parse_date (notmuch, sx, output); } if (strcasecmp (field->name, "query") == 0) { if (! sx->list->next || ! sx->list->next->val) { _notmuch_database_log (notmuch, "missing query name\n"); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } else { return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output); } } term_prefix = _find_prefix (field->name); if (flags & SEXP_FLAG_WILDCARD) { if (rest) { _notmuch_database_log (notmuch, "extra term(s) after wildcard\n", sx->val); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } output = Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix); return NOTMUCH_STATUS_SUCCESS; } else if (flags & SEXP_FLAG_REGEXP) { if (! rest || ! rest->val) { _notmuch_database_log (notmuch, "missing regular expression\n"); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } else { std::string msg; /* ignored */ return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, field->name, rest->val, output, msg); } } else if (flags & SEXP_FLAG_THREAD_MATCH) { if (! rest) { _notmuch_database_log (notmuch, "missing subquery\n"); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; } else { notmuch_query_t *query; std::string msg; status = _notmuch_query_from_sexpr (notmuch, rest, query); if (status) return status; status = _notmuch_query_expand_to_threads (query, output, msg); if (status) { _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ()); } return status; } } else { return _sexp_combine_field (term_prefix, field->xapian_op, rest, output); } } } _notmuch_database_log_append (notmuch, "unimplemented prefix %s\n", sx->list->val); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; }