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.
20 #include "mutt_menu.h"
21 #include "mutt_idna.h"
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, int retbuf);
59 static ADDRESS *result_to_addr (QUERY *r)
63 tmp = rfc822_cpy_adr (r->addr);
65 if(!tmp->next && !tmp->personal)
66 tmp->personal = safe_strdup (r->name);
68 mutt_addrlist_to_idna (tmp, NULL);
72 static QUERY *run_query (char *s, int quiet)
77 char cmd[_POSIX_PATH_MAX];
87 mutt_expand_file_fmt (cmd, sizeof(cmd), QueryCmd, s);
89 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
91 dprint (1, (debugfile, "unable to fork command: %s", cmd));
95 mutt_message _("Waiting for response...");
96 fgets (msg, sizeof (msg), fp);
97 if ((p = strrchr (msg, '\n')))
99 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL)
101 if ((p = strtok(buf, "\t\n")))
107 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
112 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
116 l = mutt_strwidth (p);
117 if (l > SecondColumn)
120 cur->addr = rfc822_parse_adrlist (cur->addr, p);
121 p = strtok(NULL, "\t\n");
124 l = mutt_strwidth (p);
127 cur->name = safe_strdup (p);
128 p = strtok(NULL, "\t\n");
131 cur->other = safe_strdup (p);
138 if (mutt_wait_filter (thepid))
140 dprint (1, (debugfile, "Error: %s\n", msg));
141 if (!quiet) mutt_error ("%s", msg);
146 mutt_message ("%s", msg);
152 static int query_search (MUTTMENU *m, regex_t *re, int n)
154 ENTRY *table = (ENTRY *) m->data;
156 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
158 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
160 if (table[n].data->addr)
162 if (table[n].data->addr->personal &&
163 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
165 if (table[n].data->addr->mailbox &&
166 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
169 if (table[n].data->addr->val &&
170 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
178 /* This is the callback routine from mutt_menuLoop() which is used to generate
179 * a menu entry for the requested item number.
181 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
182 static void query_entry (char *s, size_t slen, MUTTMENU *m, int num)
184 ENTRY *table = (ENTRY *) m->data;
185 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
187 /* need a query format ... hard coded constants are not good */
188 while (FirstColumn + SecondColumn > 70)
190 FirstColumn = FirstColumn * 3 / 4;
191 SecondColumn = SecondColumn * 3 / 4;
192 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
193 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
194 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
195 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
198 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
200 mutt_format_string (buf2, sizeof (buf2),
201 FirstColumn + 2, FirstColumn + 2,
202 0, ' ', table[num].data->name,
203 mutt_strlen (table[num].data->name), 0);
205 snprintf (s, slen, " %c %3d %s %-*.*s %s",
206 table[num].tagged ? '*':' ',
212 NONULL (table[num].data->other));
215 static int query_tag (MUTTMENU *menu, int n, int m)
217 ENTRY *cur = &((ENTRY *) menu->data)[n];
218 int ot = cur->tagged;
220 cur->tagged = m >= 0 ? m : !cur->tagged;
221 return cur->tagged - ot;
224 int mutt_query_complete (char *buf, size_t buflen)
226 QUERY *results = NULL;
231 mutt_error _("Query command not defined.");
235 results = run_query (buf, 1);
238 /* only one response? */
239 if (results->next == NULL)
241 tmpa = result_to_addr (results);
242 mutt_addrlist_to_local (tmpa);
244 rfc822_write_address (buf, buflen, tmpa, 0);
245 rfc822_free_address (&tmpa);
249 /* multiple results, choose from query menu */
250 query_menu (buf, buflen, results, 1);
255 void mutt_query_menu (char *buf, size_t buflen)
259 mutt_error _("Query command not defined.");
265 char buffer[STRING] = "";
267 query_menu (buffer, sizeof (buffer), NULL, 0);
271 query_menu (buf, buflen, NULL, 1);
275 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf)
279 ENTRY *QueryTable = NULL;
280 QUERY *queryp = NULL;
283 char helpstr[SHORT_STRING];
286 snprintf (title, sizeof (title), _("Query")); /* FIXME */
288 menu = mutt_new_menu ();
289 menu->make_entry = query_entry;
290 menu->search = query_search;
291 menu->tag = query_tag;
292 menu->menu = MENU_QUERY;
294 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
298 /* Prompt for Query */
299 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
301 results = run_query (buf, 0);
307 snprintf (title, sizeof (title), _("Query '%s'"), buf);
309 /* count the number of results */
310 for (queryp = results; queryp; queryp = queryp->next)
313 menu->data = QueryTable = (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
315 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
316 QueryTable[i].data = queryp;
320 switch ((op = mutt_menuLoop (menu)))
322 case OP_QUERY_APPEND:
324 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
326 QUERY *newresults = NULL;
328 newresults = run_query (buf, 0);
330 menu->redraw = REDRAW_FULL;
333 snprintf (title, sizeof (title), _("Query '%s'"), buf);
340 rfc822_free_address (&queryp->addr);
341 FREE (&queryp->name);
342 FREE (&queryp->other);
343 results = queryp->next;
347 results = newresults;
353 for (queryp = results; queryp->next; queryp = queryp->next);
355 queryp->next = newresults;
360 mutt_menuDestroy (&menu);
361 menu = mutt_new_menu ();
362 menu->make_entry = query_entry;
363 menu->search = query_search;
364 menu->tag = query_tag;
365 menu->menu = MENU_QUERY;
367 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
369 /* count the number of results */
370 for (queryp = results; queryp; queryp = queryp->next)
375 menu->data = QueryTable =
376 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
378 for (i = 0, queryp = results; queryp;
379 queryp = queryp->next, i++)
380 QueryTable[i].data = queryp;
387 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
389 menu->data = QueryTable;
391 for (i = 0, queryp = results; queryp;
392 queryp = queryp->next, i++)
394 /* once we hit new entries, clear/init the tag */
395 if (queryp == newresults)
398 QueryTable[i].data = queryp;
400 QueryTable[i].tagged = 0;
407 case OP_CREATE_ALIAS:
410 ADDRESS *naddr = NULL;
412 for (i = 0; i < menu->max; i++)
413 if (QueryTable[i].tagged)
415 ADDRESS *a = result_to_addr(QueryTable[i].data);
416 rfc822_append (&naddr, a);
417 rfc822_free_address (&a);
420 mutt_create_alias (NULL, naddr);
424 ADDRESS *a = result_to_addr(QueryTable[menu->current].data);
425 mutt_create_alias (NULL, a);
426 rfc822_free_address (&a);
430 case OP_GENERIC_SELECT_ENTRY:
436 /* fall through to OP_MAIL */
439 msg = mutt_new_header ();
440 msg->env = mutt_new_envelope ();
441 if (!menu->tagprefix)
443 msg->env->to = result_to_addr(QueryTable[menu->current].data);
447 for (i = 0; i < menu->max; i++)
448 if (QueryTable[i].tagged)
450 ADDRESS *a = result_to_addr(QueryTable[i].data);
451 rfc822_append (&msg->env->to, a);
452 rfc822_free_address (&a);
455 ci_send_message (0, msg, NULL, Context, NULL);
456 menu->redraw = REDRAW_FULL;
465 /* if we need to return the selected entries */
466 if (retbuf && (done == 2))
471 memset (buf, 0, buflen);
473 /* check for tagged entries */
474 for (i = 0; i < menu->max; i++)
476 if (QueryTable[i].tagged)
480 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
481 mutt_addrlist_to_local (tmpa);
483 rfc822_write_address (buf, buflen, tmpa, 0);
484 curpos = mutt_strlen (buf);
485 rfc822_free_address (&tmpa);
487 else if (curpos + 2 < buflen)
489 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
490 mutt_addrlist_to_local (tmpa);
491 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
492 rfc822_write_address ((char *) buf + curpos + 1, buflen - curpos - 1,
494 curpos = mutt_strlen (buf);
495 rfc822_free_address (&tmpa);
499 /* then enter current message */
502 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
503 mutt_addrlist_to_local (tmpa);
504 rfc822_write_address (buf, buflen, tmpa, 0);
505 rfc822_free_address (&tmpa);
513 rfc822_free_address (&queryp->addr);
514 FREE (&queryp->name);
515 FREE (&queryp->other);
516 results = queryp->next;
522 /* tell whoever called me to redraw the screen when I return */
523 set_option (OPTNEEDREDRAW);
526 mutt_menuDestroy (&menu);