]> git.notmuchmail.org Git - notmuch/commitdiff
Introduce _notmuch_message_has_term()
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>
Sat, 9 Apr 2016 01:54:50 +0000 (22:54 -0300)
committerDavid Bremner <david@tethera.net>
Fri, 15 Apr 2016 10:07:23 +0000 (07:07 -0300)
It can be useful to easily tell if a given message has a given term
associated with it.

lib/message.cc
lib/notmuch-private.h

index 415eac1b9862033f7cf05f9f49d08eaf440b70de..4677f1df1565bc6df7cff75d459f7d063b10139a 100644 (file)
@@ -1217,6 +1217,41 @@ _notmuch_message_remove_term (notmuch_message_t *message,
     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
 }
 
+notmuch_private_status_t
+_notmuch_message_has_term (notmuch_message_t *message,
+                          const char *prefix_name,
+                          const char *value,
+                          notmuch_bool_t *result)
+{
+    char *term;
+    notmuch_bool_t out = FALSE;
+    notmuch_private_status_t status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
+
+    if (value == NULL)
+       return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
+
+    term = talloc_asprintf (message, "%s%s",
+                           _find_prefix (prefix_name), value);
+
+    if (strlen (term) > NOTMUCH_TERM_MAX)
+       return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
+
+    try {
+       /* Look for the exact term */
+       Xapian::TermIterator i = message->doc.termlist_begin ();
+       i.skip_to (term);
+       if (i != message->doc.termlist_end () &&
+           !strcmp ((*i).c_str (), term))
+           out = TRUE;
+    } catch (Xapian::Error &error) {
+       status = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
+    }
+    talloc_free (term);
+
+    *result = out;
+    return status;
+}
+
 notmuch_status_t
 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
 {
index d95bf31053e7499d9d5a12d3aa0424544dc7d070..92807975bb94562faefc949c19c080b9ad76618e 100644 (file)
@@ -279,6 +279,12 @@ _notmuch_message_remove_term (notmuch_message_t *message,
                              const char *prefix_name,
                              const char *value);
 
+notmuch_private_status_t
+_notmuch_message_has_term (notmuch_message_t *message,
+                          const char *prefix_name,
+                          const char *value,
+                          notmuch_bool_t *result);
+
 notmuch_private_status_t
 _notmuch_message_gen_terms (notmuch_message_t *message,
                            const char *prefix_name,