2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17 #include <lib-lib/file.h>
18 #include <lib-lib/mapping.h>
21 #include "mutt_menu.h"
22 #include "mutt_idna.h"
25 #include "lib/debug.h"
31 typedef struct query {
38 typedef struct entry {
43 static struct mapping_t QueryHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("Mail"), OP_MAIL},
46 {N_("New Query"), OP_QUERY},
47 {N_("Make Alias"), OP_CREATE_ALIAS},
48 {N_("Search"), OP_SEARCH},
49 {N_("Help"), OP_HELP},
53 /* Variables for outsizing output format */
54 static int FirstColumn;
55 static int SecondColumn;
57 static void query_menu (char *buf, size_t buflen, QUERY * results,
60 static address_t *result_to_addr (QUERY * r)
62 static address_t *tmp;
64 tmp = address_list_dup (r->addr);
66 if (!tmp->next && !tmp->personal)
67 tmp->personal = m_strdup(r->name);
69 mutt_addrlist_to_idna (tmp, NULL);
73 static QUERY *run_query (char *s, int quiet)
78 char cmd[_POSIX_PATH_MAX];
88 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
90 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
91 debug_print (1, ("unable to fork command: %s\n", cmd));
95 mutt_message _("Waiting for response...");
97 fgets (msg, sizeof (msg), fp);
98 if ((p = strrchr (msg, '\n')))
100 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
101 if ((p = strtok (buf, "\t\n"))) {
105 first = p_new(QUERY, 1);
109 cur->next = p_new(QUERY, 1);
113 l = mutt_strwidth (p);
114 if (l > SecondColumn)
117 cur->addr = rfc822_parse_adrlist (cur->addr, p);
118 p = strtok (NULL, "\t\n");
120 l = mutt_strwidth (p);
123 cur->name = m_strdup(p);
124 p = strtok (NULL, "\t\n");
126 cur->other = m_strdup(p);
133 if (mutt_wait_filter (thepid)) {
134 debug_print (1, ("Error: %s\n", msg));
136 mutt_error ("%s", msg);
140 mutt_message ("%s", msg);
146 static int query_search (MUTTMENU * m, regex_t * re, int n)
148 ENTRY *table = (ENTRY *) m->data;
150 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
152 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
154 if (table[n].data->addr) {
155 if (table[n].data->addr->personal &&
156 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
158 if (table[n].data->addr->mailbox &&
159 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
166 /* This is the callback routine from mutt_menuLoop() which is used to generate
167 * a menu entry for the requested item number.
169 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
170 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
172 ENTRY *table = (ENTRY *) m->data;
173 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
175 /* need a query format ... hard coded constants are not good */
176 while (FirstColumn + SecondColumn > 70) {
177 FirstColumn = FirstColumn * 3 / 4;
178 SecondColumn = SecondColumn * 3 / 4;
179 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
180 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
181 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
182 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
185 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
187 mutt_format_string (buf2, sizeof (buf2),
188 FirstColumn + 2, FirstColumn + 2,
189 0, ' ', table[num].data->name,
190 m_strlen(table[num].data->name), 0);
192 snprintf (s, slen, " %c %3d %s %-*.*s %s",
193 table[num].tagged ? '*' : ' ',
197 SecondColumn + 2, buf, NONULL (table[num].data->other));
200 static int query_tag (MUTTMENU * menu, int n, int m)
202 ENTRY *cur = &((ENTRY *) menu->data)[n];
203 int ot = cur->tagged;
205 cur->tagged = m >= 0 ? m : !cur->tagged;
206 return cur->tagged - ot;
209 int mutt_query_complete (char *buf, size_t buflen)
211 QUERY *results = NULL;
215 mutt_error _("Query command not defined.");
220 results = run_query (buf, 1);
222 /* only one response? */
223 if (results->next == NULL) {
224 tmpa = result_to_addr (results);
225 mutt_addrlist_to_local (tmpa);
227 rfc822_write_address (buf, buflen, tmpa, 0);
228 address_delete (&tmpa);
232 /* multiple results, choose from query menu */
233 query_menu (buf, buflen, results, 1);
238 void mutt_query_menu (char *buf, size_t buflen)
241 mutt_error _("Query command not defined.");
247 char buffer[STRING] = "";
249 query_menu (buffer, sizeof (buffer), NULL, 0);
252 query_menu (buf, buflen, NULL, 1);
256 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
260 ENTRY *QueryTable = NULL;
261 QUERY *queryp = NULL;
264 char helpstr[SHORT_STRING];
267 snprintf (title, sizeof (title), _("Query")); /* FIXME */
269 menu = mutt_new_menu ();
270 menu->make_entry = query_entry;
271 menu->search = query_search;
272 menu->tag = query_tag;
273 menu->menu = MENU_QUERY;
276 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
278 if (results == NULL) {
279 /* Prompt for Query */
280 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
281 results = run_query (buf, 0);
286 snprintf (title, sizeof (title), _("Query '%s'"), buf);
288 /* count the number of results */
289 for (queryp = results; queryp; queryp = queryp->next)
292 menu->data = QueryTable = p_new(ENTRY, menu->max);
294 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
295 QueryTable[i].data = queryp;
298 switch ((op = mutt_menuLoop (menu))) {
299 case OP_QUERY_APPEND:
301 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
302 QUERY *newresults = NULL;
304 newresults = run_query (buf, 0);
306 menu->redraw = REDRAW_FULL;
308 snprintf (title, sizeof (title), _("Query '%s'"), buf);
310 if (op == OP_QUERY) {
313 address_delete (&queryp->addr);
314 p_delete(&queryp->name);
315 p_delete(&queryp->other);
316 results = queryp->next;
320 results = newresults;
321 p_delete(&QueryTable);
325 for (queryp = results; queryp->next; queryp = queryp->next);
327 queryp->next = newresults;
332 mutt_menuDestroy (&menu);
333 menu = mutt_new_menu ();
334 menu->make_entry = query_entry;
335 menu->search = query_search;
336 menu->tag = query_tag;
337 menu->menu = MENU_QUERY;
340 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
343 /* count the number of results */
344 for (queryp = results; queryp; queryp = queryp->next)
347 if (op == OP_QUERY) {
348 menu->data = QueryTable = p_new(ENTRY, menu->max);
350 for (i = 0, queryp = results; queryp;
351 queryp = queryp->next, i++)
352 QueryTable[i].data = queryp;
358 p_realloc(&QueryTable, menu->max);
360 menu->data = QueryTable;
362 for (i = 0, queryp = results; queryp;
363 queryp = queryp->next, i++) {
364 /* once we hit new entries, clear/init the tag */
365 if (queryp == newresults)
368 QueryTable[i].data = queryp;
370 QueryTable[i].tagged = 0;
377 case OP_CREATE_ALIAS:
378 if (menu->tagprefix) {
379 address_t *naddr = NULL;
381 for (i = 0; i < menu->max; i++)
382 if (QueryTable[i].tagged) {
383 address_list_append(&naddr, result_to_addr(QueryTable[i].data));
386 mutt_create_alias (NULL, naddr);
389 address_t *a = result_to_addr (QueryTable[menu->current].data);
391 mutt_create_alias (NULL, a);
396 case OP_GENERIC_SELECT_ENTRY:
401 /* fall through to OP_MAIL */
404 msg = mutt_new_header ();
405 msg->env = mutt_new_envelope ();
406 if (!menu->tagprefix) {
407 msg->env->to = result_to_addr (QueryTable[menu->current].data);
410 for (i = 0; i < menu->max; i++)
411 if (QueryTable[i].tagged) {
412 address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
415 ci_send_message (0, msg, NULL, Context, NULL);
416 menu->redraw = REDRAW_FULL;
425 /* if we need to return the selected entries */
426 if (retbuf && (done == 2)) {
430 p_clear(buf, buflen);
432 /* check for tagged entries */
433 for (i = 0; i < menu->max; i++) {
434 if (QueryTable[i].tagged) {
436 address_t *tmpa = result_to_addr (QueryTable[i].data);
438 mutt_addrlist_to_local (tmpa);
440 rfc822_write_address (buf, buflen, tmpa, 0);
441 curpos = m_strlen(buf);
442 address_delete (&tmpa);
444 else if (curpos + 2 < buflen) {
445 address_t *tmpa = result_to_addr (QueryTable[i].data);
447 mutt_addrlist_to_local (tmpa);
448 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
449 rfc822_write_address ((char *) buf + curpos + 1,
450 buflen - curpos - 1, tmpa, 0);
451 curpos = m_strlen(buf);
452 address_delete (&tmpa);
456 /* then enter current message */
458 address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
460 mutt_addrlist_to_local (tmpa);
461 rfc822_write_address (buf, buflen, tmpa, 0);
462 address_delete (&tmpa);
469 address_delete (&queryp->addr);
470 p_delete(&queryp->name);
471 p_delete(&queryp->other);
472 results = queryp->next;
476 p_delete(&QueryTable);
478 /* tell whoever called me to redraw the screen when I return */
479 set_option (OPTNEEDREDRAW);
482 mutt_menuDestroy (&menu);