unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
To: Notmuch Mail <notmuch@notmuchmail.org>
Subject: [PATCH v3 15/16] added notmuch_message_reindex
Date: Sun, 31 Jan 2016 15:40:00 -0500	[thread overview]
Message-ID: <1454272801-23623-16-git-send-email-dkg@fifthhorseman.net> (raw)
In-Reply-To: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net>

This new function asks the database to reindex a given message, using
the supplied indexopts.

This can be used, for example, to index the cleartext of an encrypted
message.
---
 lib/message.cc | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/notmuch.h  | 14 +++++++++
 2 files changed, 104 insertions(+), 1 deletion(-)

diff --git a/lib/message.cc b/lib/message.cc
index 8d72ea2..3b35418 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -529,7 +529,9 @@ static void
 _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
 {
     Xapian::TermIterator i;
-    size_t prefix_len = strlen (prefix);
+    size_t prefix_len = 0;
+
+    prefix_len = strlen (prefix);
 
     while (1) {
 	i = message->doc.termlist_begin ();
@@ -1667,3 +1669,90 @@ _notmuch_message_database (notmuch_message_t *message)
 {
     return message->notmuch;
 }
+
+notmuch_status_t
+notmuch_message_reindex (notmuch_message_t *message,
+			 notmuch_indexopts_t *indexopts)
+{
+    notmuch_database_t *notmuch = NULL;
+    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, status;
+    notmuch_tags_t *tags = NULL;
+    notmuch_filenames_t *filenames, *orig_filenames = NULL;
+    const char *filename = NULL, *tag = NULL;
+    notmuch_message_t *newmsg = NULL;
+    notmuch_bool_t readded = FALSE, skip;
+    const char *autotags[] = {
+		    "attachment",
+		    "encrypted",
+		    "signed",
+		    "index-decrypted",
+		    "index-decryption-failed" };
+
+    if (message == NULL)
+	return NOTMUCH_STATUS_NULL_POINTER;
+    
+    notmuch = _notmuch_message_database (message);
+
+    /* cache tags and filenames */
+    tags = notmuch_message_get_tags(message);
+    filenames = notmuch_message_get_filenames(message);
+    orig_filenames = notmuch_message_get_filenames(message);
+    
+    /* walk through filenames, removing them until the message is gone */
+    for ( ; notmuch_filenames_valid (filenames);
+	  notmuch_filenames_move_to_next (filenames)) {
+	filename = notmuch_filenames_get (filenames);
+
+	ret = notmuch_database_remove_message (notmuch, filename);
+	if (ret != NOTMUCH_STATUS_SUCCESS &&
+	    ret != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
+	    return ret;
+    }
+    if (ret != NOTMUCH_STATUS_SUCCESS)
+	return ret;
+    
+    /* re-add the filenames with the associated indexopts */
+    for (; notmuch_filenames_valid (orig_filenames);
+	 notmuch_filenames_move_to_next (orig_filenames)) {
+	filename = notmuch_filenames_get (orig_filenames);
+
+	status = notmuch_database_add_message_with_indexopts(notmuch,
+							     filename,
+							     indexopts,
+							     readded ? NULL : &newmsg);
+	if (status == NOTMUCH_STATUS_SUCCESS ||
+	    status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
+	    if (!readded) {
+		/* re-add tags */
+		for (; notmuch_tags_valid (tags);
+		     notmuch_tags_move_to_next (tags)) {
+		    tag = notmuch_tags_get (tags);
+		    skip = FALSE;
+		    
+		    for (size_t i = 0; i < ARRAY_SIZE(autotags); i++)
+			if (strcmp (tag, autotags[i]) == 0)
+			    skip = TRUE;
+		    
+		    if (!skip) {
+			status = notmuch_message_add_tag (newmsg, tag);
+			if (status != NOTMUCH_STATUS_SUCCESS)
+			    ret = status;
+		    }
+		}
+		readded = TRUE;
+	    }
+	} else {
+	    /* if we failed to add this filename, go ahead and try the
+	     * next one as though it were first, but report the
+	     * error... */
+	    ret = status;
+	}
+    }
+    if (newmsg)
+	notmuch_message_destroy (newmsg);
+	    		
+    /* should we also destroy the incoming message object?  at the
+     * moment, we leave that to the caller */
+    return ret;
+}
+
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 854a451..e6287cd 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -1377,6 +1377,20 @@ notmuch_filenames_t *
 notmuch_message_get_filenames (notmuch_message_t *message);
 
 /**
+ * Re-index the e-mail corresponding to 'message' using the supplied index options
+ *
+ * Returns the status of the re-index operation.  (see the return
+ * codes documented in notmuch_database_add_message)
+ *
+ * After reindexing, the user should discard the message object passed
+ * in here by calling notmuch_message_destroy, since it refers to the
+ * original message, not to the reindexed message.
+ */
+notmuch_status_t
+notmuch_message_reindex (notmuch_message_t *message,
+			 notmuch_indexopts_t *indexopts);
+
+/**
  * Message flags.
  */
 typedef enum _notmuch_message_flag {
-- 
2.7.0.rc3

  parent reply	other threads:[~2016-01-31 20:40 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-31 20:39 Allow indexing cleartext of encrypted messages (v3) Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 01/16] add util/search-path.{c, h} to test for executables in $PATH Daniel Kahn Gillmor
2016-02-09 12:57   ` David Bremner
2016-02-09 21:52     ` [PATCH v4] " Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 02/16] Move crypto.c into libutil Daniel Kahn Gillmor
2016-02-10  2:21   ` David Bremner
2016-02-10 14:34     ` Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 03/16] make shared crypto code behave library-like Daniel Kahn Gillmor
2016-02-10  2:37   ` David Bremner
2016-02-10 16:18     ` Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 04/16] Provide _notmuch_crypto_{set,get}_gpg_path Daniel Kahn Gillmor
2016-02-10 11:45   ` David Bremner
2016-02-10 16:31     ` Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 05/16] Use a blank _notmuch_crypto to choose the default gpg_path Daniel Kahn Gillmor
2016-02-10 11:49   ` David Bremner
2016-02-10 16:37     ` Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 06/16] Prefer gpg2 in the test suite if available Daniel Kahn Gillmor
2016-02-10 11:54   ` David Bremner
2016-02-10 16:39     ` Daniel Kahn Gillmor
2016-02-10 20:28       ` David Bremner
2016-02-11  7:16       ` Tomi Ollila
2016-01-31 20:39 ` [PATCH v3 07/16] create a notmuch_indexopts_t index options object Daniel Kahn Gillmor
2016-02-27 13:06   ` David Bremner
2016-01-31 20:39 ` [PATCH v3 08/16] reorganize indexing of multipart/signed and multipart/encrypted Daniel Kahn Gillmor
2016-02-27 13:14   ` David Bremner
2016-01-31 20:39 ` [PATCH v3 09/16] index encrypted parts when asked Daniel Kahn Gillmor
2016-02-27 15:49   ` David Bremner
2016-01-31 20:39 ` [PATCH v3 10/16] Add n_d_add_message_with_indexopts (extension of n_d_add_message) Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 11/16] add --try-decrypt to notmuch insert Daniel Kahn Gillmor
2016-02-27 15:55   ` David Bremner
2016-01-31 20:39 ` [PATCH v3 12/16] add --try-decrypt to notmuch new Daniel Kahn Gillmor
2016-01-31 20:39 ` [PATCH v3 13/16] add indexopts to notmuch python bindings Daniel Kahn Gillmor
2016-02-28 14:22   ` David Bremner
2016-01-31 20:39 ` [PATCH v3 14/16] test indexing cleartext version of delivered messages Daniel Kahn Gillmor
2016-01-31 20:40 ` Daniel Kahn Gillmor [this message]
2016-02-10  0:41   ` [PATCH v3 15/16] added notmuch_message_reindex Jameson Graef Rollins
2016-02-10  1:01     ` Daniel Kahn Gillmor
2016-02-10 17:21       ` Daniel Kahn Gillmor
2016-02-13 18:13         ` David Bremner
2016-02-28 14:52   ` David Bremner
2016-01-31 20:40 ` [PATCH v3 16/16] add "notmuch reindex" subcommand Daniel Kahn Gillmor
2016-02-28 15:05   ` David Bremner
2016-02-06 20:48 ` Allow indexing cleartext of encrypted messages (v3) Tomi Ollila
2016-02-09  8:08 ` Jameson Graef Rollins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1454272801-23623-16-git-send-email-dkg@fifthhorseman.net \
    --to=dkg@fifthhorseman.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).