unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Florian Friesdorf <flo@chaoflow.net>
To: notmuch@notmuchmail.org
Cc: a.amann@ucc.ie
Subject: [PATCH] implement search --format=sanitized_text + emacs UI to use it
Date: Sat,  7 May 2011 01:17:26 +0200	[thread overview]
Message-ID: <1304723846-29134-2-git-send-email-flo@chaoflow.net> (raw)
In-Reply-To: <1304723846-29134-1-git-send-email-flo@chaoflow.net>

Sanitize "Subject:" and "Author:" fields to not contain control
characters for sanitized_text format.

When a Subject field contains encoded CRLF sequences, these sequences
would appear unfiltered in the output of notmuch search. This confused
the notmuch emacs interface leading to "Unexpected Output"
messages. This is now fixed by replacing all characters with ASCII
code less than 32 with a question mark for the sanitized_text
format. The emacs UI uses this format.

Thank you to Andreas Amann <a.amann@ucc.ie>, who wrote the initial
patch, I just turned it into a new format.

missing:
- man page update
- test, (works for me)
- investigate initialization warning:
CC -O2 notmuch-search.o
notmuch-search.c:98:1: warning: missing initializer
notmuch-search.c:98:1: warning: (near initialization for
'format_sanitized_text.results_null')
---
 emacs/notmuch.el |    3 +-
 notmuch-search.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 1d683f8..9d7c212 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -879,7 +879,8 @@ The optional parameters are used as follows:
 		     (if oldest-first
 			 "--sort=oldest-first"
 		       "--sort=newest-first")
-		     query)))
+                     "--format=sanitized_text"
+                     query)))
 	  (set-process-sentinel proc 'notmuch-search-process-sentinel)
 	  (set-process-filter proc 'notmuch-search-process-filter))))
     (run-hooks 'notmuch-search-hook)))
diff --git a/notmuch-search.c b/notmuch-search.c
index 5e39511..d59dc44 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -78,6 +78,26 @@ static const search_format_t format_text = {
 };
 
 static void
+format_thread_sanitized_text (const void *ctx,
+			      const char *thread_id,
+			      const time_t date,
+			      const int matched,
+			      const int total,
+			      const char *authors,
+			      const char *subject);
+static const search_format_t format_sanitized_text = {
+    "",
+	"",
+	    format_item_id_text,
+	    format_thread_sanitized_text,
+	    " (",
+		"%s", " ",
+	    ")", "\n",
+	"",
+    "",
+};
+
+static void
 format_item_id_json (const void *ctx,
 		     const char *item_type,
 		     const char *item_id);
@@ -129,6 +149,42 @@ format_thread_text (const void *ctx,
 	    subject);
 }
 
+static char *
+sanitize_string(const void *ctx, const char *str)
+{
+    char *out, *loop;
+
+    loop = out = talloc_strdup (ctx, str);
+
+    for(;*loop;loop++){
+	if ((unsigned char)(*loop) < 32)
+	    *loop = '?';
+    }
+    return out;
+}
+
+static void
+format_thread_sanitized_text (const void *ctx,
+			      const char *thread_id,
+			      const time_t date,
+			      const int matched,
+			      const int total,
+			      const char *authors,
+			      const char *subject)
+{
+    void *ctx_quote = talloc_new (ctx);
+
+    printf ("thread:%s %12s [%d/%d] %s; %s",
+	    thread_id,
+	    notmuch_time_relative_date (ctx, date),
+	    matched,
+	    total,
+	    sanitize_string(ctx_quote, authors),
+	    sanitize_string(ctx_quote, subject));
+
+    talloc_free (ctx_quote);
+}
+
 static void
 format_item_id_json (const void *ctx,
 		     unused (const char *item_type),
@@ -378,6 +434,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 	    opt = argv[i] + sizeof ("--format=") - 1;
 	    if (strcmp (opt, "text") == 0) {
 		format = &format_text;
+	    } else if (strcmp (opt, "sanitized_text") == 0) {
+		format = &format_sanitized_text;
 	    } else if (strcmp (opt, "json") == 0) {
 		format = &format_json;
 	    } else {
-- 
1.7.5.1

  reply	other threads:[~2011-05-06 23:17 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-06 23:17 [PATCH] search --format=sanitized_text Florian Friesdorf
2011-05-06 23:17 ` Florian Friesdorf [this message]
2011-05-06 23:55   ` [PATCH] implement search --format=sanitized_text + emacs UI to use it Austin Clements
2011-05-07  0:15     ` Florian Friesdorf
2011-05-07  1:06     ` Jameson Graef Rollins
2011-05-07  9:14       ` Pieter Praet
2011-05-07  9:25         ` Pieter Praet
2011-05-08 21:14       ` [PATCH] sanitize notmuch-search output - rewrapped Florian Friesdorf
2011-05-08 21:14         ` [PATCH] Sanitize "Subject:" and "Author:" fields to not contain control characters in notmuch-search Florian Friesdorf
2011-05-08 21:40           ` Austin Clements
2011-05-08 21:54             ` Florian Friesdorf
2011-05-08 22:02               ` Austin Clements
2011-05-08 23:12                 ` [PATCH] sanitize notmuch-search output - rewrapped Florian Friesdorf
2011-05-08 23:16                   ` Florian Friesdorf
2011-05-08 23:13                 ` [PATCH 1/2] style fixes Florian Friesdorf
2011-05-08 23:13                   ` [PATCH 2/2] test for sanitized notmuch-search output Florian Friesdorf
2011-05-08 23:58           ` [PATCH] Sanitize "Subject:" and "Author:" fields to not contain control characters in notmuch-search 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=1304723846-29134-2-git-send-email-flo@chaoflow.net \
    --to=flo@chaoflow.net \
    --cc=a.amann@ucc.ie \
    --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).