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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_menu.h"
25 #include "mutt_idna.h"
48 static struct mapping_t QueryHelp[] = {
49 { N_("Exit"), OP_EXIT },
50 { N_("Mail"), OP_MAIL },
51 { N_("New Query"), OP_QUERY },
52 { N_("Make Alias"), OP_CREATE_ALIAS },
53 { N_("Search"), OP_SEARCH },
54 { N_("Help"), OP_HELP },
58 /* Variables for outsizing output format */
59 static int FirstColumn;
60 static int SecondColumn;
62 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf);
64 static ADDRESS *result_to_addr (QUERY *r)
68 if (!(tmp = rfc822_cpy_adr (r->addr)))
71 if(!tmp->next && !tmp->personal)
72 tmp->personal = safe_strdup (r->name);
74 mutt_addrlist_to_idna (tmp, NULL);
78 static QUERY *run_query (char *s, int quiet)
83 char cmd[_POSIX_PATH_MAX];
93 mutt_expand_file_fmt (cmd, sizeof(cmd), QueryCmd, s);
95 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
97 dprint (1, (debugfile, "unable to fork command: %s", cmd));
101 mutt_message _("Waiting for response...");
102 fgets (msg, sizeof (msg), fp);
103 if ((p = strrchr (msg, '\n')))
105 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL)
107 if ((p = strtok(buf, "\t\n")))
113 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
118 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
122 l = mutt_strwidth (p);
123 if (l > SecondColumn)
126 cur->addr = rfc822_parse_adrlist (cur->addr, p);
127 p = strtok(NULL, "\t\n");
130 l = mutt_strwidth (p);
133 cur->name = safe_strdup (p);
134 p = strtok(NULL, "\t\n");
137 cur->other = safe_strdup (p);
144 if (mutt_wait_filter (thepid))
146 dprint (1, (debugfile, "Error: %s\n", msg));
147 if (!quiet) mutt_error ("%s", msg);
152 mutt_message ("%s", msg);
158 static int query_search (MUTTMENU *m, regex_t *re, int n)
160 ENTRY *table = (ENTRY *) m->data;
162 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
164 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
166 if (table[n].data->addr)
168 if (table[n].data->addr->personal &&
169 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
171 if (table[n].data->addr->mailbox &&
172 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
175 if (table[n].data->addr->val &&
176 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
184 static const char * query_format_str (char *dest, size_t destlen, size_t col,
185 char op, const char *src,
186 const char *fmt, const char *ifstring,
187 const char *elsestring,
188 unsigned long data, format_flag flags)
190 ENTRY *entry = (ENTRY *)data;
191 QUERY *query = entry->data;
192 char tmp[SHORT_STRING];
193 char buf2[STRING] = "";
194 int optional = (flags & M_FORMAT_OPTIONAL);
199 rfc822_write_address (buf2, sizeof (buf2), query->addr, 1);
200 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
201 snprintf (dest, destlen, tmp, buf2);
204 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
205 snprintf (dest, destlen, tmp, query->num);
210 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
211 snprintf (dest, destlen, tmp, NONULL (query->other));
213 else if (!query->other || !*query->other)
217 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
218 snprintf (dest, destlen, tmp, NONULL (query->name));
221 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
222 snprintf (dest, destlen, tmp, entry->tagged ? '*' : ' ');
225 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
226 snprintf (dest, destlen, tmp, op);
231 mutt_FormatString (dest, destlen, col, ifstring, query_format_str, data, 0);
232 else if (flags & M_FORMAT_OPTIONAL)
233 mutt_FormatString (dest, destlen, col, elsestring, query_format_str, data, 0);
238 static void query_entry (char *s, size_t slen, MUTTMENU *m, int num)
240 ENTRY *entry = &((ENTRY *) m->data)[num];
242 entry->data->num = num;
243 mutt_FormatString (s, slen, 0, NONULL (QueryFormat), query_format_str,
244 (unsigned long) entry, M_FORMAT_ARROWCURSOR);
247 static int query_tag (MUTTMENU *menu, int n, int m)
249 ENTRY *cur = &((ENTRY *) menu->data)[n];
250 int ot = cur->tagged;
252 cur->tagged = m >= 0 ? m : !cur->tagged;
253 return cur->tagged - ot;
256 int mutt_query_complete (char *buf, size_t buflen)
258 QUERY *results = NULL;
263 mutt_error _("Query command not defined.");
267 results = run_query (buf, 1);
270 /* only one response? */
271 if (results->next == NULL)
273 tmpa = result_to_addr (results);
274 mutt_addrlist_to_local (tmpa);
276 rfc822_write_address (buf, buflen, tmpa, 0);
277 rfc822_free_address (&tmpa);
281 /* multiple results, choose from query menu */
282 query_menu (buf, buflen, results, 1);
287 void mutt_query_menu (char *buf, size_t buflen)
291 mutt_error _("Query command not defined.");
297 char buffer[STRING] = "";
299 query_menu (buffer, sizeof (buffer), NULL, 0);
303 query_menu (buf, buflen, NULL, 1);
307 static void query_menu (char *buf, size_t buflen, QUERY *results, int retbuf)
311 ENTRY *QueryTable = NULL;
312 QUERY *queryp = NULL;
315 char helpstr[LONG_STRING];
318 snprintf (title, sizeof (title), _("Query")); /* FIXME */
320 menu = mutt_new_menu ();
321 menu->make_entry = query_entry;
322 menu->search = query_search;
323 menu->tag = query_tag;
324 menu->menu = MENU_QUERY;
326 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
330 /* Prompt for Query */
331 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
333 results = run_query (buf, 0);
339 snprintf (title, sizeof (title), _("Query '%s'"), buf);
341 /* count the number of results */
342 for (queryp = results; queryp; queryp = queryp->next)
345 menu->data = QueryTable = (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
347 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
348 QueryTable[i].data = queryp;
352 switch ((op = mutt_menuLoop (menu)))
354 case OP_QUERY_APPEND:
356 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0])
358 QUERY *newresults = NULL;
360 newresults = run_query (buf, 0);
362 menu->redraw = REDRAW_FULL;
365 snprintf (title, sizeof (title), _("Query '%s'"), buf);
372 rfc822_free_address (&queryp->addr);
373 FREE (&queryp->name);
374 FREE (&queryp->other);
375 results = queryp->next;
379 results = newresults;
385 for (queryp = results; queryp->next; queryp = queryp->next);
387 queryp->next = newresults;
392 mutt_menuDestroy (&menu);
393 menu = mutt_new_menu ();
394 menu->make_entry = query_entry;
395 menu->search = query_search;
396 menu->tag = query_tag;
397 menu->menu = MENU_QUERY;
399 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
401 /* count the number of results */
402 for (queryp = results; queryp; queryp = queryp->next)
407 menu->data = QueryTable =
408 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
410 for (i = 0, queryp = results; queryp;
411 queryp = queryp->next, i++)
412 QueryTable[i].data = queryp;
419 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
421 menu->data = QueryTable;
423 for (i = 0, queryp = results; queryp;
424 queryp = queryp->next, i++)
426 /* once we hit new entries, clear/init the tag */
427 if (queryp == newresults)
430 QueryTable[i].data = queryp;
432 QueryTable[i].tagged = 0;
439 case OP_CREATE_ALIAS:
442 ADDRESS *naddr = NULL;
444 for (i = 0; i < menu->max; i++)
445 if (QueryTable[i].tagged)
447 ADDRESS *a = result_to_addr(QueryTable[i].data);
448 rfc822_append (&naddr, a);
449 rfc822_free_address (&a);
452 mutt_create_alias (NULL, naddr);
456 ADDRESS *a = result_to_addr(QueryTable[menu->current].data);
457 mutt_create_alias (NULL, a);
458 rfc822_free_address (&a);
462 case OP_GENERIC_SELECT_ENTRY:
468 /* fall through to OP_MAIL */
471 msg = mutt_new_header ();
472 msg->env = mutt_new_envelope ();
473 if (!menu->tagprefix)
475 msg->env->to = result_to_addr(QueryTable[menu->current].data);
479 for (i = 0; i < menu->max; i++)
480 if (QueryTable[i].tagged)
482 ADDRESS *a = result_to_addr(QueryTable[i].data);
483 rfc822_append (&msg->env->to, a);
484 rfc822_free_address (&a);
487 ci_send_message (0, msg, NULL, Context, NULL);
488 menu->redraw = REDRAW_FULL;
497 /* if we need to return the selected entries */
498 if (retbuf && (done == 2))
503 memset (buf, 0, buflen);
505 /* check for tagged entries */
506 for (i = 0; i < menu->max; i++)
508 if (QueryTable[i].tagged)
512 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
513 mutt_addrlist_to_local (tmpa);
515 rfc822_write_address (buf, buflen, tmpa, 0);
516 curpos = mutt_strlen (buf);
517 rfc822_free_address (&tmpa);
519 else if (curpos + 2 < buflen)
521 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
522 mutt_addrlist_to_local (tmpa);
523 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
524 rfc822_write_address ((char *) buf + curpos + 1, buflen - curpos - 1,
526 curpos = mutt_strlen (buf);
527 rfc822_free_address (&tmpa);
531 /* then enter current message */
534 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
535 mutt_addrlist_to_local (tmpa);
536 rfc822_write_address (buf, buflen, tmpa, 0);
537 rfc822_free_address (&tmpa);
545 rfc822_free_address (&queryp->addr);
546 FREE (&queryp->name);
547 FREE (&queryp->other);
548 results = queryp->next;
554 /* tell whoever called me to redraw the screen when I return */
555 set_option (OPTNEEDREDRAW);
558 mutt_menuDestroy (&menu);