2 * Copyright (C) 1996-2000,2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf);
59 static ADDRESS *result_to_addr (QUERY *r)
63 if (!(tmp = rfc822_cpy_adr (r->addr, 0)))
66 if(!tmp->next && !tmp->personal)
67 tmp->personal = safe_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];
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, 0)) != NULL)
101 if ((p = strtok(buf, "\t\n")))
105 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
110 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
114 cur->addr = rfc822_parse_adrlist (cur->addr, p);
115 p = strtok(NULL, "\t\n");
118 cur->name = safe_strdup (p);
119 p = strtok(NULL, "\t\n");
121 cur->other = safe_strdup (p);
127 if (mutt_wait_filter (thepid))
129 dprint (1, (debugfile, "Error: %s\n", msg));
130 if (!quiet) mutt_error ("%s", msg);
135 mutt_message ("%s", msg);
141 static int query_search (MUTTMENU *m, regex_t *re, int n)
143 ENTRY *table = (ENTRY *) m->data;
145 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
147 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
149 if (table[n].data->addr)
151 if (table[n].data->addr->personal &&
152 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
154 if (table[n].data->addr->mailbox &&
155 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
158 if (table[n].data->addr->val &&
159 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
167 static const char * query_format_str (char *dest, size_t destlen, size_t col,
168 char op, const char *src,
169 const char *fmt, const char *ifstring,
170 const char *elsestring,
171 unsigned long data, format_flag flags)
173 ENTRY *entry = (ENTRY *)data;
174 QUERY *query = entry->data;
175 char tmp[SHORT_STRING];
176 char buf2[STRING] = "";
177 int optional = (flags & M_FORMAT_OPTIONAL);
182 rfc822_write_address (buf2, sizeof (buf2), query->addr, 1);
183 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
184 snprintf (dest, destlen, tmp, buf2);
187 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
188 snprintf (dest, destlen, tmp, query->num + 1);
193 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
194 snprintf (dest, destlen, tmp, NONULL (query->other));
196 else if (!query->other || !*query->other)
200 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
201 snprintf (dest, destlen, tmp, NONULL (query->name));
204 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
205 snprintf (dest, destlen, tmp, entry->tagged ? '*' : ' ');
208 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
209 snprintf (dest, destlen, tmp, op);
214 mutt_FormatString (dest, destlen, col, ifstring, query_format_str, data, 0);
215 else if (flags & M_FORMAT_OPTIONAL)
216 mutt_FormatString (dest, destlen, col, elsestring, query_format_str, data, 0);
221 static void query_entry (char *s, size_t slen, MUTTMENU *m, int num)
223 ENTRY *entry = &((ENTRY *) m->data)[num];
225 entry->data->num = num;
226 mutt_FormatString (s, slen, 0, NONULL (QueryFormat), query_format_str,
227 (unsigned long) entry, M_FORMAT_ARROWCURSOR);
230 static int query_tag (MUTTMENU *menu, int n, int m)
232 ENTRY *cur = &((ENTRY *) menu->data)[n];
233 int ot = cur->tagged;
235 cur->tagged = m >= 0 ? m : !cur->tagged;
236 return cur->tagged - ot;
239 int mutt_query_complete (char *buf, size_t buflen)
241 QUERY *results = NULL;
246 mutt_error _("Query command not defined.");
250 results = run_query (buf, 1);
253 /* only one response? */
254 if (results->next == NULL)
256 tmpa = result_to_addr (results);
257 mutt_addrlist_to_local (tmpa);
259 rfc822_write_address (buf, buflen, tmpa, 0);
260 rfc822_free_address (&tmpa);
264 /* multiple results, choose from query menu */
265 query_menu (buf, buflen, results, 1);
270 void mutt_query_menu (char *buf, size_t buflen)
274 mutt_error _("Query command not defined.");
280 char buffer[STRING] = "";
282 query_menu (buffer, sizeof (buffer), NULL, 0);
286 query_menu (buf, buflen, NULL, 1);
290 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf)
294 ENTRY *QueryTable = NULL;
295 QUERY *queryp = NULL;
298 char helpstr[LONG_STRING];
301 snprintf (title, sizeof (title), _("Query")); /* FIXME */
303 menu = mutt_new_menu (MENU_QUERY);
304 menu->make_entry = query_entry;
305 menu->search = query_search;
306 menu->tag = query_tag;
308 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
312 /* Prompt for Query */
313 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
315 results = run_query (buf, 0);
321 snprintf (title, sizeof (title), _("Query '%s'"), buf);
323 /* count the number of results */
324 for (queryp = results; queryp; queryp = queryp->next)
327 menu->data = QueryTable = (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
329 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
330 QueryTable[i].data = queryp;
334 switch ((op = mutt_menuLoop (menu)))
336 case OP_QUERY_APPEND:
338 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
340 QUERY *newresults = NULL;
342 newresults = run_query (buf, 0);
344 menu->redraw = REDRAW_FULL;
347 snprintf (title, sizeof (title), _("Query '%s'"), buf);
354 rfc822_free_address (&queryp->addr);
355 FREE (&queryp->name);
356 FREE (&queryp->other);
357 results = queryp->next;
361 results = newresults;
367 for (queryp = results; queryp->next; queryp = queryp->next);
369 queryp->next = newresults;
374 mutt_menuDestroy (&menu);
375 menu = mutt_new_menu (MENU_QUERY);
376 menu->make_entry = query_entry;
377 menu->search = query_search;
378 menu->tag = query_tag;
380 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
382 /* count the number of results */
383 for (queryp = results; queryp; queryp = queryp->next)
388 menu->data = QueryTable =
389 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
391 for (i = 0, queryp = results; queryp;
392 queryp = queryp->next, i++)
393 QueryTable[i].data = queryp;
400 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
402 menu->data = QueryTable;
404 for (i = 0, queryp = results; queryp;
405 queryp = queryp->next, i++)
407 /* once we hit new entries, clear/init the tag */
408 if (queryp == newresults)
411 QueryTable[i].data = queryp;
413 QueryTable[i].tagged = 0;
420 case OP_CREATE_ALIAS:
423 ADDRESS *naddr = NULL;
425 for (i = 0; i < menu->max; i++)
426 if (QueryTable[i].tagged)
428 ADDRESS *a = result_to_addr(QueryTable[i].data);
429 rfc822_append (&naddr, a, 0);
430 rfc822_free_address (&a);
433 mutt_create_alias (NULL, naddr);
437 ADDRESS *a = result_to_addr(QueryTable[menu->current].data);
438 mutt_create_alias (NULL, a);
439 rfc822_free_address (&a);
443 case OP_GENERIC_SELECT_ENTRY:
449 /* fall through to OP_MAIL */
452 msg = mutt_new_header ();
453 msg->env = mutt_new_envelope ();
454 if (!menu->tagprefix)
456 msg->env->to = result_to_addr(QueryTable[menu->current].data);
460 for (i = 0; i < menu->max; i++)
461 if (QueryTable[i].tagged)
463 ADDRESS *a = result_to_addr(QueryTable[i].data);
464 rfc822_append (&msg->env->to, a, 0);
465 rfc822_free_address (&a);
468 ci_send_message (0, msg, NULL, Context, NULL);
469 menu->redraw = REDRAW_FULL;
478 /* if we need to return the selected entries */
479 if (retbuf && (done == 2))
484 memset (buf, 0, buflen);
486 /* check for tagged entries */
487 for (i = 0; i < menu->max; i++)
489 if (QueryTable[i].tagged)
493 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
494 mutt_addrlist_to_local (tmpa);
496 rfc822_write_address (buf, buflen, tmpa, 0);
497 curpos = mutt_strlen (buf);
498 rfc822_free_address (&tmpa);
500 else if (curpos + 2 < buflen)
502 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
503 mutt_addrlist_to_local (tmpa);
504 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
505 rfc822_write_address ((char *) buf + curpos + 1, buflen - curpos - 1,
507 curpos = mutt_strlen (buf);
508 rfc822_free_address (&tmpa);
512 /* then enter current message */
515 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
516 mutt_addrlist_to_local (tmpa);
517 rfc822_write_address (buf, buflen, tmpa, 0);
518 rfc822_free_address (&tmpa);
526 rfc822_free_address (&queryp->addr);
527 FREE (&queryp->name);
528 FREE (&queryp->other);
529 results = queryp->next;
535 /* tell whoever called me to redraw the screen when I return */
536 set_option (OPTNEEDREDRAW);
539 mutt_menuDestroy (&menu);