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"
33 typedef struct query {
40 typedef struct entry {
45 static struct mapping_t QueryHelp[] = {
46 {N_("Exit"), OP_EXIT},
47 {N_("Mail"), OP_MAIL},
48 {N_("New Query"), OP_QUERY},
49 {N_("Make Alias"), OP_CREATE_ALIAS},
50 {N_("Search"), OP_SEARCH},
51 {N_("Help"), OP_HELP},
55 /* Variables for outsizing output format */
56 static int FirstColumn;
57 static int SecondColumn;
59 static void query_menu (char *buf, size_t buflen, QUERY * results,
62 static ADDRESS *result_to_addr (QUERY * r)
66 tmp = rfc822_cpy_adr (r->addr);
68 if (!tmp->next && !tmp->personal)
69 tmp->personal = safe_strdup (r->name);
71 mutt_addrlist_to_idna (tmp, NULL);
75 static QUERY *run_query (char *s, int quiet)
80 char cmd[_POSIX_PATH_MAX];
90 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
92 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
93 dprint (1, (debugfile, "unable to fork command: %s", cmd));
97 mutt_message _("Waiting for response...");
99 fgets (msg, sizeof (msg), fp);
100 if ((p = strrchr (msg, '\n')))
102 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
103 if ((p = strtok (buf, "\t\n"))) {
107 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
111 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
115 l = mutt_strwidth (p);
116 if (l > SecondColumn)
119 cur->addr = rfc822_parse_adrlist (cur->addr, p);
120 p = strtok (NULL, "\t\n");
122 l = mutt_strwidth (p);
125 cur->name = safe_strdup (p);
126 p = strtok (NULL, "\t\n");
128 cur->other = safe_strdup (p);
135 if (mutt_wait_filter (thepid)) {
136 dprint (1, (debugfile, "Error: %s\n", msg));
138 mutt_error ("%s", msg);
142 mutt_message ("%s", msg);
148 static int query_search (MUTTMENU * m, regex_t * re, int n)
150 ENTRY *table = (ENTRY *) m->data;
152 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
154 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
156 if (table[n].data->addr) {
157 if (table[n].data->addr->personal &&
158 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
160 if (table[n].data->addr->mailbox &&
161 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
164 if (table[n].data->addr->val &&
165 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
173 /* This is the callback routine from mutt_menuLoop() which is used to generate
174 * a menu entry for the requested item number.
176 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
177 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
179 ENTRY *table = (ENTRY *) m->data;
180 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
182 /* need a query format ... hard coded constants are not good */
183 while (FirstColumn + SecondColumn > 70) {
184 FirstColumn = FirstColumn * 3 / 4;
185 SecondColumn = SecondColumn * 3 / 4;
186 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
187 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
188 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
189 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
192 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
194 mutt_format_string (buf2, sizeof (buf2),
195 FirstColumn + 2, FirstColumn + 2,
196 0, ' ', table[num].data->name,
197 mutt_strlen (table[num].data->name), 0);
199 snprintf (s, slen, " %c %3d %s %-*.*s %s",
200 table[num].tagged ? '*' : ' ',
204 SecondColumn + 2, buf, NONULL (table[num].data->other));
207 static int query_tag (MUTTMENU * menu, int n, int m)
209 ENTRY *cur = &((ENTRY *) menu->data)[n];
210 int ot = cur->tagged;
212 cur->tagged = m >= 0 ? m : !cur->tagged;
213 return cur->tagged - ot;
216 int mutt_query_complete (char *buf, size_t buflen)
218 QUERY *results = NULL;
222 mutt_error _("Query command not defined.");
227 results = run_query (buf, 1);
229 /* only one response? */
230 if (results->next == NULL) {
231 tmpa = result_to_addr (results);
232 mutt_addrlist_to_local (tmpa);
234 rfc822_write_address (buf, buflen, tmpa, 0);
235 rfc822_free_address (&tmpa);
239 /* multiple results, choose from query menu */
240 query_menu (buf, buflen, results, 1);
245 void mutt_query_menu (char *buf, size_t buflen)
248 mutt_error _("Query command not defined.");
254 char buffer[STRING] = "";
256 query_menu (buffer, sizeof (buffer), NULL, 0);
259 query_menu (buf, buflen, NULL, 1);
263 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
267 ENTRY *QueryTable = NULL;
268 QUERY *queryp = NULL;
271 char helpstr[SHORT_STRING];
274 snprintf (title, sizeof (title), _("Query")); /* FIXME */
276 menu = mutt_new_menu ();
277 menu->make_entry = query_entry;
278 menu->search = query_search;
279 menu->tag = query_tag;
280 menu->menu = MENU_QUERY;
283 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
285 if (results == NULL) {
286 /* Prompt for Query */
287 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
288 results = run_query (buf, 0);
293 snprintf (title, sizeof (title), _("Query '%s'"), buf);
295 /* count the number of results */
296 for (queryp = results; queryp; queryp = queryp->next)
299 menu->data = QueryTable =
300 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
302 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
303 QueryTable[i].data = queryp;
306 switch ((op = mutt_menuLoop (menu))) {
307 case OP_QUERY_APPEND:
309 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
310 QUERY *newresults = NULL;
312 newresults = run_query (buf, 0);
314 menu->redraw = REDRAW_FULL;
316 snprintf (title, sizeof (title), _("Query '%s'"), buf);
318 if (op == OP_QUERY) {
321 rfc822_free_address (&queryp->addr);
322 FREE (&queryp->name);
323 FREE (&queryp->other);
324 results = queryp->next;
328 results = newresults;
333 for (queryp = results; queryp->next; queryp = queryp->next);
335 queryp->next = newresults;
340 mutt_menuDestroy (&menu);
341 menu = mutt_new_menu ();
342 menu->make_entry = query_entry;
343 menu->search = query_search;
344 menu->tag = query_tag;
345 menu->menu = MENU_QUERY;
348 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
351 /* count the number of results */
352 for (queryp = results; queryp; queryp = queryp->next)
355 if (op == OP_QUERY) {
356 menu->data = QueryTable =
357 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
359 for (i = 0, queryp = results; queryp;
360 queryp = queryp->next, i++)
361 QueryTable[i].data = queryp;
367 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
369 menu->data = QueryTable;
371 for (i = 0, queryp = results; queryp;
372 queryp = queryp->next, i++) {
373 /* once we hit new entries, clear/init the tag */
374 if (queryp == newresults)
377 QueryTable[i].data = queryp;
379 QueryTable[i].tagged = 0;
386 case OP_CREATE_ALIAS:
387 if (menu->tagprefix) {
388 ADDRESS *naddr = NULL;
390 for (i = 0; i < menu->max; i++)
391 if (QueryTable[i].tagged) {
392 ADDRESS *a = result_to_addr (QueryTable[i].data);
394 rfc822_append (&naddr, a);
395 rfc822_free_address (&a);
398 mutt_create_alias (NULL, naddr);
401 ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
403 mutt_create_alias (NULL, a);
404 rfc822_free_address (&a);
408 case OP_GENERIC_SELECT_ENTRY:
413 /* fall through to OP_MAIL */
416 msg = mutt_new_header ();
417 msg->env = mutt_new_envelope ();
418 if (!menu->tagprefix) {
419 msg->env->to = result_to_addr (QueryTable[menu->current].data);
422 for (i = 0; i < menu->max; i++)
423 if (QueryTable[i].tagged) {
424 ADDRESS *a = result_to_addr (QueryTable[i].data);
426 rfc822_append (&msg->env->to, a);
427 rfc822_free_address (&a);
430 ci_send_message (0, msg, NULL, Context, NULL);
431 menu->redraw = REDRAW_FULL;
440 /* if we need to return the selected entries */
441 if (retbuf && (done == 2)) {
445 memset (buf, 0, buflen);
447 /* check for tagged entries */
448 for (i = 0; i < menu->max; i++) {
449 if (QueryTable[i].tagged) {
451 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
453 mutt_addrlist_to_local (tmpa);
455 rfc822_write_address (buf, buflen, tmpa, 0);
456 curpos = mutt_strlen (buf);
457 rfc822_free_address (&tmpa);
459 else if (curpos + 2 < buflen) {
460 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
462 mutt_addrlist_to_local (tmpa);
463 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
464 rfc822_write_address ((char *) buf + curpos + 1,
465 buflen - curpos - 1, tmpa, 0);
466 curpos = mutt_strlen (buf);
467 rfc822_free_address (&tmpa);
471 /* then enter current message */
473 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
475 mutt_addrlist_to_local (tmpa);
476 rfc822_write_address (buf, buflen, tmpa, 0);
477 rfc822_free_address (&tmpa);
484 rfc822_free_address (&queryp->addr);
485 FREE (&queryp->name);
486 FREE (&queryp->other);
487 results = queryp->next;
493 /* tell whoever called me to redraw the screen when I return */
494 set_option (OPTNEEDREDRAW);
497 mutt_menuDestroy (&menu);