2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_menu.h"
25 #include "mutt_idna.h"
47 static struct mapping_t QueryHelp[] = {
48 { N_("Exit"), OP_EXIT },
49 { N_("Mail"), OP_MAIL },
50 { N_("New Query"), OP_QUERY },
51 { N_("Make Alias"), OP_CREATE_ALIAS },
52 { N_("Search"), OP_SEARCH },
53 { N_("Help"), OP_HELP },
57 /* Variables for outsizing output format */
58 static int FirstColumn;
59 static int SecondColumn;
61 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf);
63 static ADDRESS *result_to_addr (QUERY *r)
67 tmp = rfc822_cpy_adr (r->addr);
69 if(!tmp->next && !tmp->personal)
70 tmp->personal = safe_strdup (r->name);
72 mutt_addrlist_to_idna (tmp, NULL);
76 static QUERY *run_query (char *s, int quiet)
81 char cmd[_POSIX_PATH_MAX];
91 mutt_expand_file_fmt (cmd, sizeof(cmd), QueryCmd, s);
93 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
95 dprint (1, (debugfile, "unable to fork command: %s", cmd));
99 mutt_message _("Waiting for response...");
100 fgets (msg, sizeof (msg), fp);
101 if ((p = strrchr (msg, '\n')))
103 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL)
105 if ((p = strtok(buf, "\t\n")))
111 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
116 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
120 l = mutt_strwidth (p);
121 if (l > SecondColumn)
124 cur->addr = rfc822_parse_adrlist (cur->addr, p);
125 p = strtok(NULL, "\t\n");
128 l = mutt_strwidth (p);
131 cur->name = safe_strdup (p);
132 p = strtok(NULL, "\t\n");
135 cur->other = safe_strdup (p);
142 if (mutt_wait_filter (thepid))
144 dprint (1, (debugfile, "Error: %s\n", msg));
145 if (!quiet) mutt_error ("%s", msg);
150 mutt_message ("%s", msg);
156 static int query_search (MUTTMENU *m, regex_t *re, int n)
158 ENTRY *table = (ENTRY *) m->data;
160 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
162 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
164 if (table[n].data->addr)
166 if (table[n].data->addr->personal &&
167 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
169 if (table[n].data->addr->mailbox &&
170 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
173 if (table[n].data->addr->val &&
174 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
182 /* This is the callback routine from mutt_menuLoop() which is used to generate
183 * a menu entry for the requested item number.
185 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
186 static void query_entry (char *s, size_t slen, MUTTMENU *m, int num)
188 ENTRY *table = (ENTRY *) m->data;
189 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
191 /* need a query format ... hard coded constants are not good */
192 while (FirstColumn + SecondColumn > 70)
194 FirstColumn = FirstColumn * 3 / 4;
195 SecondColumn = SecondColumn * 3 / 4;
196 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
197 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
198 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
199 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
202 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
204 mutt_format_string (buf2, sizeof (buf2),
205 FirstColumn + 2, FirstColumn + 2,
206 0, ' ', table[num].data->name,
207 mutt_strlen (table[num].data->name), 0);
209 snprintf (s, slen, " %c %3d %s %-*.*s %s",
210 table[num].tagged ? '*':' ',
216 NONULL (table[num].data->other));
219 static int query_tag (MUTTMENU *menu, int n, int m)
221 ENTRY *cur = &((ENTRY *) menu->data)[n];
222 int ot = cur->tagged;
224 cur->tagged = m >= 0 ? m : !cur->tagged;
225 return cur->tagged - ot;
228 int mutt_query_complete (char *buf, size_t buflen)
230 QUERY *results = NULL;
235 mutt_error _("Query command not defined.");
239 results = run_query (buf, 1);
242 /* only one response? */
243 if (results->next == NULL)
245 tmpa = result_to_addr (results);
246 mutt_addrlist_to_local (tmpa);
248 rfc822_write_address (buf, buflen, tmpa, 0);
249 rfc822_free_address (&tmpa);
253 /* multiple results, choose from query menu */
254 query_menu (buf, buflen, results, 1);
259 void mutt_query_menu (char *buf, size_t buflen)
263 mutt_error _("Query command not defined.");
269 char buffer[STRING] = "";
271 query_menu (buffer, sizeof (buffer), NULL, 0);
275 query_menu (buf, buflen, NULL, 1);
279 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf)
283 ENTRY *QueryTable = NULL;
284 QUERY *queryp = NULL;
287 char helpstr[SHORT_STRING];
290 snprintf (title, sizeof (title), _("Query")); /* FIXME */
292 menu = mutt_new_menu ();
293 menu->make_entry = query_entry;
294 menu->search = query_search;
295 menu->tag = query_tag;
296 menu->menu = MENU_QUERY;
298 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
302 /* Prompt for Query */
303 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
305 results = run_query (buf, 0);
311 snprintf (title, sizeof (title), _("Query '%s'"), buf);
313 /* count the number of results */
314 for (queryp = results; queryp; queryp = queryp->next)
317 menu->data = QueryTable = (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
319 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
320 QueryTable[i].data = queryp;
324 switch ((op = mutt_menuLoop (menu)))
326 case OP_QUERY_APPEND:
328 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
330 QUERY *newresults = NULL;
332 newresults = run_query (buf, 0);
334 menu->redraw = REDRAW_FULL;
337 snprintf (title, sizeof (title), _("Query '%s'"), buf);
344 rfc822_free_address (&queryp->addr);
345 FREE (&queryp->name);
346 FREE (&queryp->other);
347 results = queryp->next;
351 results = newresults;
357 for (queryp = results; queryp->next; queryp = queryp->next);
359 queryp->next = newresults;
364 mutt_menuDestroy (&menu);
365 menu = mutt_new_menu ();
366 menu->make_entry = query_entry;
367 menu->search = query_search;
368 menu->tag = query_tag;
369 menu->menu = MENU_QUERY;
371 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
373 /* count the number of results */
374 for (queryp = results; queryp; queryp = queryp->next)
379 menu->data = QueryTable =
380 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
382 for (i = 0, queryp = results; queryp;
383 queryp = queryp->next, i++)
384 QueryTable[i].data = queryp;
391 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
393 menu->data = QueryTable;
395 for (i = 0, queryp = results; queryp;
396 queryp = queryp->next, i++)
398 /* once we hit new entries, clear/init the tag */
399 if (queryp == newresults)
402 QueryTable[i].data = queryp;
404 QueryTable[i].tagged = 0;
411 case OP_CREATE_ALIAS:
414 ADDRESS *naddr = NULL;
416 for (i = 0; i < menu->max; i++)
417 if (QueryTable[i].tagged)
419 ADDRESS *a = result_to_addr(QueryTable[i].data);
420 rfc822_append (&naddr, a);
421 rfc822_free_address (&a);
424 mutt_create_alias (NULL, naddr);
428 ADDRESS *a = result_to_addr(QueryTable[menu->current].data);
429 mutt_create_alias (NULL, a);
430 rfc822_free_address (&a);
434 case OP_GENERIC_SELECT_ENTRY:
440 /* fall through to OP_MAIL */
443 msg = mutt_new_header ();
444 msg->env = mutt_new_envelope ();
445 if (!menu->tagprefix)
447 msg->env->to = result_to_addr(QueryTable[menu->current].data);
451 for (i = 0; i < menu->max; i++)
452 if (QueryTable[i].tagged)
454 ADDRESS *a = result_to_addr(QueryTable[i].data);
455 rfc822_append (&msg->env->to, a);
456 rfc822_free_address (&a);
459 ci_send_message (0, msg, NULL, Context, NULL);
460 menu->redraw = REDRAW_FULL;
469 /* if we need to return the selected entries */
470 if (retbuf && (done == 2))
475 memset (buf, 0, buflen);
477 /* check for tagged entries */
478 for (i = 0; i < menu->max; i++)
480 if (QueryTable[i].tagged)
484 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
485 mutt_addrlist_to_local (tmpa);
487 rfc822_write_address (buf, buflen, tmpa, 0);
488 curpos = mutt_strlen (buf);
489 rfc822_free_address (&tmpa);
491 else if (curpos + 2 < buflen)
493 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
494 mutt_addrlist_to_local (tmpa);
495 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
496 rfc822_write_address ((char *) buf + curpos + 1, buflen - curpos - 1,
498 curpos = mutt_strlen (buf);
499 rfc822_free_address (&tmpa);
503 /* then enter current message */
506 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
507 mutt_addrlist_to_local (tmpa);
508 rfc822_write_address (buf, buflen, tmpa, 0);
509 rfc822_free_address (&tmpa);
517 rfc822_free_address (&queryp->addr);
518 FREE (&queryp->name);
519 FREE (&queryp->other);
520 results = queryp->next;
526 /* tell whoever called me to redraw the screen when I return */
527 set_option (OPTNEEDREDRAW);
530 mutt_menuDestroy (&menu);