Re: [PATCH] Fix typo in Message.maildir_flags_to_tags
[notmuch-archives.git] / 06 / 4150b4b1e01922b0f8c83c341231da4f3bcb4e
1 Return-Path: <neil@linux.intel.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 62B17431FAF\r
6         for <notmuch@notmuchmail.org>; Sun,  7 Jul 2013 04:13:40 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -5\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-5 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_HI=-5] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id C4ffbisR8eQa for <notmuch@notmuchmail.org>;\r
16         Sun,  7 Jul 2013 04:13:35 -0700 (PDT)\r
17 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24])\r
18         by olra.theworths.org (Postfix) with ESMTP id 300AB431FB6\r
19         for <notmuch@notmuchmail.org>; Sun,  7 Jul 2013 04:13:30 -0700 (PDT)\r
20 Received: from orsmga002.jf.intel.com ([10.7.209.21])\r
21         by orsmga102.jf.intel.com with ESMTP; 07 Jul 2013 04:10:56 -0700\r
22 X-ExtLoop1: 1\r
23 X-IronPort-AV: E=Sophos;i="4.87,1013,1363158000"; d="scan'208";a="365957330"\r
24 Received: from unknown (HELO neilpc.config) ([10.252.122.25])\r
25         by orsmga002.jf.intel.com with ESMTP; 07 Jul 2013 04:13:21 -0700\r
26 From: Neil Roberts <neil@linux.intel.com>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [PATCH 2/2] emacs: crypto: Handle prompting for passwords\r
29 Date: Sun,  7 Jul 2013 12:14:32 +0100\r
30 Message-Id: <1373195672-9338-3-git-send-email-neil@linux.intel.com>\r
31 X-Mailer: git-send-email 1.7.11.3.g3c3efa5\r
32 In-Reply-To: <1373195672-9338-1-git-send-email-neil@linux.intel.com>\r
33 References: <1373195672-9338-1-git-send-email-neil@linux.intel.com>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.13\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38         <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
40         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
45         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sun, 07 Jul 2013 11:13:40 -0000\r
47 \r
48 This makes the Emacs client handle prompting for passwords when\r
49 decrypting PGP messages via the new --status-fd and --command-fd\r
50 options to the show and reply commands.\r
51 \r
52 The prompting is handled directly in notmuch-call-notmuch-sexp.\r
53 Instead of calling call-process to invoke notmuch synchronously it is\r
54 now run asynchronously via notmuch-start-notmuch so that it can\r
55 install a process filter to handle the status messages. The function\r
56 then runs a loop calling accept-process-output so that it can block\r
57 until the process actually completes.\r
58 \r
59 Emacs doesn't support having multiple file descriptors to talk to a\r
60 child process apart from stdin/out so the --status-fd is set to 1 and\r
61 the --command-fd is set to 0. This means that the status messages will\r
62 actually be interleaved with the sexp output. I think this shouldn't\r
63 be a problem because both the sexp output and the status messages are\r
64 split into lines and it shouldn't be possible for a sexp message to\r
65 begin with [NOTMUCH:] so it is easy to separate out the two types of\r
66 message.\r
67 \r
68 The process filter collects the output into a temporary buffer.\r
69 Whenever it receives a line beginning with [NOTMUCH:] it will process\r
70 the command and remove the line so that the final buffer only contains\r
71 the sexp. The only handler currently is for GET_HIDDEN which just\r
72 calls read-passwd to prompt for the password and then writes it back\r
73 out to the stdin of the notmuch process.\r
74 \r
75 This is based on similar functionality in epg.el which handles\r
76 invoking GPG.\r
77 ---\r
78  emacs/notmuch-lib.el   | 66 +++++++++++++++++++++++++++++++++++++++++++-------\r
79  emacs/notmuch-mua.el   |  2 +-\r
80  emacs/notmuch-query.el |  3 ++-\r
81  3 files changed, 60 insertions(+), 11 deletions(-)\r
82 \r
83 diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
84 index 8deb7de..0cc23fe 100644\r
85 --- a/emacs/notmuch-lib.el\r
86 +++ b/emacs/notmuch-lib.el\r
87 @@ -467,6 +467,48 @@ You may need to restart Emacs or upgrade your notmuch package."))\r
88         ;; `notmuch-logged-error' does not return.\r
89         ))))\r
90  \r
91 +(defun notmuch--status-GET_HIDDEN (process args)\r
92 +  ;; The args string should be readable by emacs if it is made into a list\r
93 +  (let* ((args-list (read (concat "(" args ")")))\r
94 +        (prompt (nth 1 args-list))\r
95 +        (pwd (read-passwd (if prompt\r
96 +                              (concat prompt ": ")\r
97 +                            "GPG password: "))))\r
98 +    (process-send-string process (concat pwd "\n"))\r
99 +    (clear-string pwd)))\r
100 +\r
101 +(defun notmuch--process-filter (process string)\r
102 +  "Callback used by `notmuch-call-notmuch-sexp` used to handle the status fd."\r
103 +\r
104 +  (when (buffer-live-p (process-buffer process))\r
105 +    (with-current-buffer (process-buffer process)\r
106 +\r
107 +      (goto-char (point-max))\r
108 +\r
109 +      (let ((insertion-point (point)))\r
110 +       (insert string)\r
111 +\r
112 +       ;; Check if the string contains any status lines\r
113 +       (goto-char insertion-point)\r
114 +       (beginning-of-line)\r
115 +\r
116 +       (while (re-search-forward "^\\[NOTMUCH:\\].*\n" nil t)\r
117 +         (let ((line-beginning (match-beginning 0))\r
118 +               (line-end (match-end 0)))\r
119 +           (goto-char (+ line-beginning 10))\r
120 +           (when (looking-at " *\\([A-Z_]+\\)")\r
121 +             ;; If there is a function defined that looks like it is made\r
122 +             ;; to handle this particular status then call it passing the\r
123 +             ;; remainder of the line as an argument\r
124 +             (let ((symbol (intern-soft (concat "notmuch--status-"\r
125 +                                                (match-string 1)))))\r
126 +               (when (and symbol (fboundp symbol))\r
127 +                 (funcall symbol\r
128 +                          process\r
129 +                          (buffer-substring (match-end 0) line-end)))))\r
130 +           ;; Remove the status so that it won't form part of the sexp\r
131 +           (delete-region line-beginning line-end)))))))\r
132 +\r
133  (defun notmuch-call-notmuch-sexp (&rest args)\r
134    "Invoke `notmuch-command' with ARGS and return the parsed S-exp output.\r
135  \r
136 @@ -474,15 +516,21 @@ If notmuch exits with a non-zero status, this will pop up a\r
137  buffer containing notmuch's output and signal an error."\r
138  \r
139    (with-temp-buffer\r
140 -    (let ((err-file (make-temp-file "nmerr")))\r
141 -      (unwind-protect\r
142 -         (let ((status (apply #'call-process\r
143 -                              notmuch-command nil (list t err-file) nil args)))\r
144 -           (notmuch-check-exit-status status (cons notmuch-command args)\r
145 -                                      (buffer-string) err-file)\r
146 -           (goto-char (point-min))\r
147 -           (read (current-buffer)))\r
148 -       (delete-file err-file)))))\r
149 +    (let ((proc (apply #'notmuch-start-notmuch\r
150 +                      "notmuch"\r
151 +                      (current-buffer) nil args)))\r
152 +      (set-process-filter proc #'notmuch--process-filter)\r
153 +\r
154 +      ;; Synchronously wait until the process completes\r
155 +      (while (eq (process-status proc) 'run)\r
156 +       (accept-process-output proc 1))\r
157 +\r
158 +      ;; According to similar code in epg-wait-for-completion, this is\r
159 +      ;; needed to run the process filter right now.\r
160 +      (sleep-for 0.1)\r
161 +\r
162 +      (goto-char (point-min))\r
163 +      (read (current-buffer)))))\r
164  \r
165  (defun notmuch-start-notmuch (name buffer sentinel &rest args)\r
166    "Start and return an asynchronous notmuch command.\r
167 diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el\r
168 index 329d342..af7dd03 100644\r
169 --- a/emacs/notmuch-mua.el\r
170 +++ b/emacs/notmuch-mua.el\r
171 @@ -150,7 +150,7 @@ list."\r
172         reply\r
173         original)\r
174      (when notmuch-show-process-crypto\r
175 -      (setq args (append args '("--decrypt"))))\r
176 +      (setq args (append args '("--decrypt" "--status-fd=1" "--command-fd=0"))))\r
177  \r
178      (if reply-all\r
179         (setq args (append args '("--reply-to=all")))\r
180 diff --git a/emacs/notmuch-query.el b/emacs/notmuch-query.el\r
181 index 51d427f..0788e8c 100644\r
182 --- a/emacs/notmuch-query.el\r
183 +++ b/emacs/notmuch-query.el\r
184 @@ -31,7 +31,8 @@ is a possibly empty forest of replies.\r
185  "\r
186    (let ((args '("show" "--format=sexp" "--format-version=1")))\r
187      (if notmuch-show-process-crypto\r
188 -       (setq args (append args '("--decrypt"))))\r
189 +       (setq args (append args\r
190 +                          '("--decrypt" "--status-fd=1" "--command-fd=0"))))\r
191      (setq args (append args search-terms))\r
192      (apply #'notmuch-call-notmuch-sexp args)))\r
193  \r
194 -- \r
195 1.7.11.3.g3c3efa5\r
196 \r