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>
17 #include "mutt_menu.h"
18 #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 *result_to_addr (QUERY * r)
64 tmp = rfc822_cpy_adr (r->addr);
66 if (!tmp->next && !tmp->personal)
67 tmp->personal = str_dup (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 = (QUERY *) mem_calloc (1, sizeof (QUERY));
109 cur->next = (QUERY *) mem_calloc (1, sizeof (QUERY));
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 = str_dup (p);
124 p = strtok (NULL, "\t\n");
126 cur->other = str_dup (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 str_len (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 rfc822_free_address (&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 =
293 (ENTRY *) mem_calloc (menu->max, sizeof (ENTRY));
295 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
296 QueryTable[i].data = queryp;
299 switch ((op = mutt_menuLoop (menu))) {
300 case OP_QUERY_APPEND:
302 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
303 QUERY *newresults = NULL;
305 newresults = run_query (buf, 0);
307 menu->redraw = REDRAW_FULL;
309 snprintf (title, sizeof (title), _("Query '%s'"), buf);
311 if (op == OP_QUERY) {
314 rfc822_free_address (&queryp->addr);
315 p_delete(&queryp->name);
316 p_delete(&queryp->other);
317 results = queryp->next;
321 results = newresults;
322 p_delete(&QueryTable);
326 for (queryp = results; queryp->next; queryp = queryp->next);
328 queryp->next = newresults;
333 mutt_menuDestroy (&menu);
334 menu = mutt_new_menu ();
335 menu->make_entry = query_entry;
336 menu->search = query_search;
337 menu->tag = query_tag;
338 menu->menu = MENU_QUERY;
341 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
344 /* count the number of results */
345 for (queryp = results; queryp; queryp = queryp->next)
348 if (op == OP_QUERY) {
349 menu->data = QueryTable =
350 (ENTRY *) mem_calloc (menu->max, sizeof (ENTRY));
352 for (i = 0, queryp = results; queryp;
353 queryp = queryp->next, i++)
354 QueryTable[i].data = queryp;
360 mem_realloc (&QueryTable, menu->max * sizeof (ENTRY));
362 menu->data = QueryTable;
364 for (i = 0, queryp = results; queryp;
365 queryp = queryp->next, i++) {
366 /* once we hit new entries, clear/init the tag */
367 if (queryp == newresults)
370 QueryTable[i].data = queryp;
372 QueryTable[i].tagged = 0;
379 case OP_CREATE_ALIAS:
380 if (menu->tagprefix) {
381 ADDRESS *naddr = NULL;
383 for (i = 0; i < menu->max; i++)
384 if (QueryTable[i].tagged) {
385 ADDRESS *a = result_to_addr (QueryTable[i].data);
387 rfc822_append (&naddr, a);
388 rfc822_free_address (&a);
391 mutt_create_alias (NULL, naddr);
394 ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
396 mutt_create_alias (NULL, a);
397 rfc822_free_address (&a);
401 case OP_GENERIC_SELECT_ENTRY:
406 /* fall through to OP_MAIL */
409 msg = mutt_new_header ();
410 msg->env = mutt_new_envelope ();
411 if (!menu->tagprefix) {
412 msg->env->to = result_to_addr (QueryTable[menu->current].data);
415 for (i = 0; i < menu->max; i++)
416 if (QueryTable[i].tagged) {
417 ADDRESS *a = result_to_addr (QueryTable[i].data);
419 rfc822_append (&msg->env->to, a);
420 rfc822_free_address (&a);
423 ci_send_message (0, msg, NULL, Context, NULL);
424 menu->redraw = REDRAW_FULL;
433 /* if we need to return the selected entries */
434 if (retbuf && (done == 2)) {
438 memset (buf, 0, buflen);
440 /* check for tagged entries */
441 for (i = 0; i < menu->max; i++) {
442 if (QueryTable[i].tagged) {
444 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
446 mutt_addrlist_to_local (tmpa);
448 rfc822_write_address (buf, buflen, tmpa, 0);
449 curpos = str_len (buf);
450 rfc822_free_address (&tmpa);
452 else if (curpos + 2 < buflen) {
453 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
455 mutt_addrlist_to_local (tmpa);
456 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
457 rfc822_write_address ((char *) buf + curpos + 1,
458 buflen - curpos - 1, tmpa, 0);
459 curpos = str_len (buf);
460 rfc822_free_address (&tmpa);
464 /* then enter current message */
466 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
468 mutt_addrlist_to_local (tmpa);
469 rfc822_write_address (buf, buflen, tmpa, 0);
470 rfc822_free_address (&tmpa);
477 rfc822_free_address (&queryp->addr);
478 p_delete(&queryp->name);
479 p_delete(&queryp->other);
480 results = queryp->next;
484 p_delete(&QueryTable);
486 /* tell whoever called me to redraw the screen when I return */
487 set_option (OPTNEEDREDRAW);
490 mutt_menuDestroy (&menu);