#include #include "notmuch-private.h" #include "sexp.h" #include "parse-time-vrp.h" typedef struct { const char *name; Xapian::Query::op xapian_op; Xapian::Query initial; } _sexp_op_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 }, { } }; typedef struct { const char *name; Xapian::Query::op xapian_op; } _sexp_field_t; static _sexp_field_t fields[] = { { "attachment", Xapian::Query::OP_PHRASE }, { "body", Xapian::Query::OP_PHRASE }, { "date", Xapian::Query::OP_INVALID }, { "from", Xapian::Query::OP_PHRASE }, { "folder", Xapian::Query::OP_OR }, { "id", Xapian::Query::OP_OR }, { "is", Xapian::Query::OP_AND }, { "mid", Xapian::Query::OP_OR }, { "mimetype", Xapian::Query::OP_PHRASE }, { "path", Xapian::Query::OP_OR }, { "property", Xapian::Query::OP_AND }, { "subject", Xapian::Query::OP_PHRASE }, { "tag", Xapian::Query::OP_AND }, { "thread", Xapian::Query::OP_OR }, { "to", Xapian::Query::OP_PHRASE }, { } }; static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output); 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 = _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 _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; } } /* 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 */ static notmuch_status_t _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) { /* some fields need to be handled specially */ if (strcasecmp (field->name, "date") == 0) { return _sexp_parse_date (notmuch, sx, output); } return _sexp_combine_field (_find_prefix (field->name), field->xapian_op, sx->list->next, output); } } _notmuch_database_log_append (notmuch, "unimplemented prefix %s\n", sx->list->val); return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; }