Re: notmuch-emacs should correctly handle signature status on reply
[notmuch-archives.git] / 77 / 2b301827d30475634f585ccbba6e444e395ef8
1 Return-Path: <BATV+28bf489b39c899822e75+2435+infradead.org+hohndel@bombadil.srs.infradead.org>\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 22B2F4196F2\r
6         for <notmuch@notmuchmail.org>; Fri, 23 Apr 2010 18:41:54 -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: -4.2\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-4.2 tagged_above=-999 required=5\r
12         tests=[BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3] autolearn=ham\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 KcrsGZoPvjMj for <notmuch@notmuchmail.org>;\r
16         Fri, 23 Apr 2010 18:41:52 -0700 (PDT)\r
17 Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id 9CA39431FC1\r
19         for <notmuch@notmuchmail.org>; Fri, 23 Apr 2010 18:41:52 -0700 (PDT)\r
20 Received: from localhost ([::1] helo=x200.gr8dns.org)\r
21         by bombadil.infradead.org with esmtp (Exim 4.69 #1 (Red Hat Linux))\r
22         id 1O5UNT-0001ur-Vb\r
23         for notmuch@notmuchmail.org; Sat, 24 Apr 2010 01:41:52 +0000\r
24 Received: by x200.gr8dns.org (Postfix, from userid 500)\r
25         id DEB05C0C63; Fri, 23 Apr 2010 15:52:15 -0700 (PDT)\r
26 From: Dirk Hohndel <hohndel@infradead.org>\r
27 To: notmuch <notmuch@notmuchmail.org>\r
28 Subject: [PATCH] RFC: Add From guessing when forwarding email\r
29 User-Agent: Notmuch/0.2-209-g2a4596b (http://notmuchmail.org) Emacs/23.1.1\r
30         (i386-redhat-linux-gnu)\r
31 Date: Fri, 23 Apr 2010 15:52:15 -0700\r
32 Message-ID: <m3eii5lr34.fsf@x200.gr8dns.org>\r
33 MIME-Version: 1.0\r
34 Content-Type: text/plain; charset=us-ascii\r
35 X-SRS-Rewrite: SMTP reverse-path rewritten from <hohndel@infradead.org> by\r
36         bombadil.infradead.org See http://www.infradead.org/rpr.html\r
37 X-BeenThere: notmuch@notmuchmail.org\r
38 X-Mailman-Version: 2.1.13\r
39 Precedence: list\r
40 List-Id: "Use and development of the notmuch mail system."\r
41         <notmuch.notmuchmail.org>\r
42 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
43         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
44 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
45 List-Post: <mailto:notmuch@notmuchmail.org>\r
46 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
47 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
48         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
49 X-List-Received-Date: Sat, 24 Apr 2010 01:41:54 -0000\r
50 \r
51 \r
52 This adds a new "guess-from" option to notmuch and modifies the\r
53 emacs UI to use this to use the best guess from address when\r
54 forwarding email.\r
55 \r
56 Given how little elisp I know I'm quite interested in feedback\r
57 and better implementations\r
58 \r
59 Signed-off-by: Dirk Hohndel <hohndel@infradead.org>\r
60 ---\r
61  emacs/notmuch-show.el |    8 +++-\r
62  notmuch-client.h      |    3 +\r
63  notmuch-reply.c       |  110 +++++++++++++++++++++++++++++++++++++++++++++++++\r
64  notmuch.c             |    8 ++++\r
65  4 files changed, 127 insertions(+), 2 deletions(-)\r
66 \r
67 diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
68 index 53af301..8cec9e8 100644\r
69 --- a/emacs/notmuch-show.el\r
70 +++ b/emacs/notmuch-show.el\r
71 @@ -809,8 +809,12 @@ any effects from previous calls to\r
72  (defun notmuch-show-forward-message ()\r
73    "Forward the current message."\r
74    (interactive)\r
75 -  (with-current-notmuch-show-message\r
76 -   (notmuch-mua-forward-message)))\r
77 +  (progn\r
78 +    (let ((message-id (notmuch-show-get-message-id)))\r
79 +      (with-current-notmuch-show-message\r
80 +       (progn\r
81 +        (setq user-mail-address (shell-command-to-string (concat "notmuch guess-from " message-id)))\r
82 +        (notmuch-mua-forward-message))))))\r
83  \r
84  (defun notmuch-show-next-message ()\r
85    "Show the next message."\r
86 diff --git a/notmuch-client.h b/notmuch-client.h\r
87 index 20be43b..ba5b002 100644\r
88 --- a/notmuch-client.h\r
89 +++ b/notmuch-client.h\r
90 @@ -93,6 +93,9 @@ int\r
91  notmuch_reply_command (void *ctx, int argc, char *argv[]);\r
92  \r
93  int\r
94 +notmuch_guess_from_command (void *ctx, int argc, char *argv[]);\r
95 +\r
96 +int\r
97  notmuch_restore_command (void *ctx, int argc, char *argv[]);\r
98  \r
99  int\r
100 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
101 index 230cacc..e9c8449 100644\r
102 --- a/notmuch-reply.c\r
103 +++ b/notmuch-reply.c\r
104 @@ -364,6 +364,55 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
105      return NULL;\r
106  }\r
107  \r
108 +/*\r
109 + * simply figure out the best from address to use without creating\r
110 + * a reply buffer or anything else\r
111 + */\r
112 +static const char *\r
113 +notmuch_guess_from(notmuch_config_t *config, notmuch_query_t *query)\r
114 +{\r
115 +    notmuch_messages_t *messages;\r
116 +    notmuch_message_t *message;\r
117 +    InternetAddressList *list;\r
118 +    InternetAddressMailbox *mailbox;\r
119 +    InternetAddress *address;\r
120 +    char *recipients;\r
121 +    const char *addr;\r
122 +    const char *from_addr = NULL;\r
123 +    int i;\r
124 +\r
125 +    for (messages = notmuch_query_search_messages (query);\r
126 +        notmuch_messages_valid (messages);\r
127 +        notmuch_messages_move_to_next (messages))\r
128 +    {\r
129 +       message = notmuch_messages_get (messages);\r
130 +       if ((asprintf (&recipients, "%s,%s", notmuch_message_get_header (message, "to"),\r
131 +                      notmuch_message_get_header (message, "cc")) == -1) || recipients == NULL) {\r
132 +           fprintf (stderr, "Out of memory\n");\r
133 +           return NULL;\r
134 +       }\r
135 +       list = internet_address_list_parse_string (recipients);\r
136 +       for (i = 0; i < internet_address_list_length (list); i++) {\r
137 +           address = internet_address_list_get_address (list, i);\r
138 +           if (! INTERNET_ADDRESS_IS_GROUP (address)) {\r
139 +               mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
140 +               addr = internet_address_mailbox_get_addr (mailbox);\r
141 +               if (address_is_users (addr, config) && !from_addr) {\r
142 +                   from_addr = addr;\r
143 +                   break;\r
144 +               }\r
145 +           }\r
146 +       }\r
147 +       free (recipients);\r
148 +       if (from_addr == NULL)\r
149 +           from_addr = guess_from_received_header (config, message);\r
150 +    \r
151 +       if (from_addr == NULL)\r
152 +           from_addr = notmuch_config_get_user_primary_email (config);\r
153 +    }\r
154 +    return from_addr;\r
155 +}\r
156 +\r
157  static int\r
158  notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)\r
159  {\r
160 @@ -567,3 +616,64 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
161  \r
162      return ret;\r
163  }\r
164 +\r
165 +int\r
166 +notmuch_guess_from_command (void *ctx, int argc, char *argv[])\r
167 +{\r
168 +    notmuch_config_t *config;\r
169 +    notmuch_database_t *notmuch;\r
170 +    notmuch_query_t *query;\r
171 +    char *query_string;\r
172 +    char const *addr;\r
173 +    int i, ret = 0;\r
174 +\r
175 +    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
176 +       if (strcmp (argv[i], "--") == 0) {\r
177 +           i++;\r
178 +           break;\r
179 +       }\r
180 +       fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
181 +       return 1;\r
182 +    }\r
183 +\r
184 +    argc -= i;\r
185 +    argv += i;\r
186 +\r
187 +    config = notmuch_config_open (ctx, NULL, NULL);\r
188 +    if (config == NULL)\r
189 +       return 1;\r
190 +\r
191 +    query_string = query_string_from_args (ctx, argc, argv);\r
192 +    if (query_string == NULL) {\r
193 +       fprintf (stderr, "Out of memory\n");\r
194 +       return 1;\r
195 +    }\r
196 +\r
197 +    if (*query_string == '\0') {\r
198 +       fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");\r
199 +       return 1;\r
200 +    }\r
201 +\r
202 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
203 +                                    NOTMUCH_DATABASE_MODE_READ_ONLY);\r
204 +    if (notmuch == NULL)\r
205 +       return 1;\r
206 +\r
207 +    query = notmuch_query_create (notmuch, query_string);\r
208 +    if (query == NULL) {\r
209 +       fprintf (stderr, "Out of memory\n");\r
210 +       return 1;\r
211 +    }\r
212 +\r
213 +    addr = notmuch_guess_from(config, query);\r
214 +    if (addr == NULL) {\r
215 +       fprintf (stderr, "fatal error\n");\r
216 +       return 1;\r
217 +    }\r
218 +    printf("%s", addr);\r
219 +\r
220 +    notmuch_query_destroy (query);\r
221 +    notmuch_database_close (notmuch);\r
222 +\r
223 +    return ret;\r
224 +}\r
225 diff --git a/notmuch.c b/notmuch.c\r
226 index 13e2612..2810722 100644\r
227 --- a/notmuch.c\r
228 +++ b/notmuch.c\r
229 @@ -252,6 +252,14 @@ command_t commands[] = {\r
230        "\n"\r
231        "\tSee \"notmuch help search-terms\" for details of the search\n"\r
232        "\tterms syntax." },\r
233 +    { "guess-from", notmuch_guess_from_command,\r
234 +      "[options...] <search-terms> [...]",\r
235 +      "Return the best guess for the from address when forwarding.",\r
236 +      "\tbased on the headers in the specified message, create the\n"\r
237 +      "\tbest guess From header and return it on stdout.\n"\r
238 +      "\n"\r
239 +      "\tSee \"notmuch help search-terms\" for details of the search\n"\r
240 +      "\tterms syntax." },\r
241      { "tag", notmuch_tag_command,\r
242        "+<tag>|-<tag> [...] [--] <search-terms> [...]",\r
243        "Add/remove tags for all messages matching the search terms.",\r
244 -- \r
245 1.6.6.1\r
246 \r
247 \r
248 -- \r
249 Dirk Hohndel\r
250 Intel Open Source Technology Center\r