mu: handle funny msgids

Query msgids without trying to parse them. Also, clean up some code.
This commit is contained in:
djcb
2017-12-03 22:17:30 +02:00
parent 620912c62b
commit 7a62758afa

View File

@ -265,70 +265,37 @@ get_bool_from_args (GHashTable *args, const char *param, gboolean optional,
} while (0); } while (0);
/* NOTE: this assumes there is only _one_ docid (message) for the
* particular message id */
static unsigned
get_docid_from_msgid (MuQuery *query, const char *str, GError **err)
{
gchar *querystr;
unsigned docid;
MuMsgIter *iter;
querystr = g_strdup_printf ("msgid:%s", str);
iter = mu_query_run (query, querystr,
MU_MSG_FIELD_ID_NONE,
1, MU_QUERY_FLAG_NONE, err);
g_free (querystr);
docid = MU_STORE_INVALID_DOCID;
if (!iter || mu_msg_iter_is_done (iter))
mu_util_g_set_error (err, MU_ERROR_NO_MATCHES,
"could not find message %s", str);
else {
MuMsg *msg;
msg = mu_msg_iter_get_msg_floating (iter);
if (!mu_msg_is_readable(msg)) {
mu_util_g_set_error (err, MU_ERROR_FILE_CANNOT_READ,
"'%s' is not readable",
mu_msg_get_path(msg));
} else
docid = mu_msg_iter_get_docid (iter);
mu_msg_iter_destroy (iter);
}
return docid;
}
/* get a *list* of all messages with the given message id */ /* get a *list* of all messages with the given message id */
static GSList* static GSList*
get_docids_from_msgids (MuQuery *query, const char *str, GError **err) get_docids_from_msgids (MuQuery *query, const char *msgid,
int max, GError **err)
{ {
gchar *querystr; char xprefix;
char *rawq, *tmp;
MuMsgIter *iter; MuMsgIter *iter;
GSList *lst; GSList *lst;
querystr = g_strdup_printf ("msgid:%s", str); xprefix = mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_MSGID);
iter = mu_query_run (query, querystr, MU_MSG_FIELD_ID_NONE, /*XXX this is a bit dodgy */
-1 /*unlimited*/, MU_QUERY_FLAG_NONE, tmp = g_ascii_strdown(msgid, -1);
err); rawq = g_strdup_printf("%c%s", xprefix, tmp);
g_free (querystr); g_free(tmp);
iter = mu_query_run (query, rawq, MU_MSG_FIELD_ID_NONE,
max, MU_QUERY_FLAG_RAW, err);
g_free (rawq);
if (!iter || mu_msg_iter_is_done (iter)) { if (!iter || mu_msg_iter_is_done (iter)) {
mu_util_g_set_error (err, MU_ERROR_NO_MATCHES, mu_util_g_set_error (err, MU_ERROR_NO_MATCHES,
"could not find message %s", str); "could not find message(s) for msgid %s",
msgid);
return NULL; return NULL;
} }
lst = NULL; lst = NULL;
do { do {
lst = g_slist_prepend lst = g_slist_prepend
(lst, (lst, GSIZE_TO_POINTER(mu_msg_iter_get_docid (iter)));
GSIZE_TO_POINTER(mu_msg_iter_get_docid (iter)));
} while (mu_msg_iter_next (iter)); } while (mu_msg_iter_next (iter));
mu_msg_iter_destroy (iter); mu_msg_iter_destroy (iter);
return lst; return lst;
@ -342,6 +309,8 @@ get_docids_from_msgids (MuQuery *query, const char *str, GError **err)
static unsigned static unsigned
determine_docid (MuQuery *query, GHashTable *args, GError **err) determine_docid (MuQuery *query, GHashTable *args, GError **err)
{ {
GSList *docids;
unsigned docid;
const char* docidstr, *msgidstr; const char* docidstr, *msgidstr;
docidstr = get_string_from_args (args, "docid", TRUE, err); docidstr = get_string_from_args (args, "docid", TRUE, err);
@ -356,7 +325,14 @@ determine_docid (MuQuery *query, GHashTable *args, GError **err)
return MU_STORE_INVALID_DOCID; return MU_STORE_INVALID_DOCID;
} }
return get_docid_from_msgid (query, msgidstr, err); docids = get_docids_from_msgids (query, msgidstr, 1, err);
if (!docids)
return MU_STORE_INVALID_DOCID;
docid = GPOINTER_TO_UINT(docids->data);
g_slist_free (docids);
return docid;
} }
@ -1291,7 +1267,8 @@ move_msgid_maybe (ServerContext *ctx, GHashTable *args, GError **err)
if (!msgid || !flagstr || maildir) if (!msgid || !flagstr || maildir)
return FALSE; return FALSE;
if (!(docids = get_docids_from_msgids (ctx->query, msgid, err))) { if (!(docids = get_docids_from_msgids (ctx->query, msgid,
-1/*unlimited*/, err))) {
print_and_clear_g_error (err); print_and_clear_g_error (err);
return TRUE; return TRUE;
} }