2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_menu.h"
16 #include "mutt_idna.h"
28 typedef struct query {
35 typedef struct entry {
40 static struct mapping_t QueryHelp[] = {
41 {N_("Exit"), OP_EXIT},
42 {N_("Mail"), OP_MAIL},
43 {N_("New Query"), OP_QUERY},
44 {N_("Make Alias"), OP_CREATE_ALIAS},
45 {N_("Search"), OP_SEARCH},
46 {N_("Help"), OP_HELP},
50 /* Variables for outsizing output format */
51 static int FirstColumn;
52 static int SecondColumn;
54 static void query_menu (char *buf, size_t buflen, QUERY * results,
57 static ADDRESS *result_to_addr (QUERY * r)
61 tmp = rfc822_cpy_adr (r->addr);
63 if (!tmp->next && !tmp->personal)
64 tmp->personal = safe_strdup (r->name);
66 mutt_addrlist_to_idna (tmp, NULL);
70 static QUERY *run_query (char *s, int quiet)
75 char cmd[_POSIX_PATH_MAX];
85 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
87 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
88 dprint (1, (debugfile, "unable to fork command: %s", cmd));
92 mutt_message _("Waiting for response...");
94 fgets (msg, sizeof (msg), fp);
95 if ((p = strrchr (msg, '\n')))
97 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
98 if ((p = strtok (buf, "\t\n"))) {
102 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
106 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
110 l = mutt_strwidth (p);
111 if (l > SecondColumn)
114 cur->addr = rfc822_parse_adrlist (cur->addr, p);
115 p = strtok (NULL, "\t\n");
117 l = mutt_strwidth (p);
120 cur->name = safe_strdup (p);
121 p = strtok (NULL, "\t\n");
123 cur->other = safe_strdup (p);
130 if (mutt_wait_filter (thepid)) {
131 dprint (1, (debugfile, "Error: %s\n", msg));
133 mutt_error ("%s", msg);
137 mutt_message ("%s", msg);
143 static int query_search (MUTTMENU * m, regex_t * re, int n)
145 ENTRY *table = (ENTRY *) m->data;
147 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
149 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
151 if (table[n].data->addr) {
152 if (table[n].data->addr->personal &&
153 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
155 if (table[n].data->addr->mailbox &&
156 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
159 if (table[n].data->addr->val &&
160 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
168 /* This is the callback routine from mutt_menuLoop() which is used to generate
169 * a menu entry for the requested item number.
171 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
172 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
174 ENTRY *table = (ENTRY *) m->data;
175 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
177 /* need a query format ... hard coded constants are not good */
178 while (FirstColumn + SecondColumn > 70) {
179 FirstColumn = FirstColumn * 3 / 4;
180 SecondColumn = SecondColumn * 3 / 4;
181 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
182 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
183 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
184 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
187 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
189 mutt_format_string (buf2, sizeof (buf2),
190 FirstColumn + 2, FirstColumn + 2,
191 0, ' ', table[num].data->name,
192 mutt_strlen (table[num].data->name), 0);
194 snprintf (s, slen, " %c %3d %s %-*.*s %s",
195 table[num].tagged ? '*' : ' ',
199 SecondColumn + 2, buf, NONULL (table[num].data->other));
202 static int query_tag (MUTTMENU * menu, int n, int m)
204 ENTRY *cur = &((ENTRY *) menu->data)[n];
205 int ot = cur->tagged;
207 cur->tagged = m >= 0 ? m : !cur->tagged;
208 return cur->tagged - ot;
211 int mutt_query_complete (char *buf, size_t buflen)
213 QUERY *results = NULL;
217 mutt_error _("Query command not defined.");
222 results = run_query (buf, 1);
224 /* only one response? */
225 if (results->next == NULL) {
226 tmpa = result_to_addr (results);
227 mutt_addrlist_to_local (tmpa);
229 rfc822_write_address (buf, buflen, tmpa, 0);
230 rfc822_free_address (&tmpa);
234 /* multiple results, choose from query menu */
235 query_menu (buf, buflen, results, 1);
240 void mutt_query_menu (char *buf, size_t buflen)
243 mutt_error _("Query command not defined.");
249 char buffer[STRING] = "";
251 query_menu (buffer, sizeof (buffer), NULL, 0);
254 query_menu (buf, buflen, NULL, 1);
258 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
262 ENTRY *QueryTable = NULL;
263 QUERY *queryp = NULL;
266 char helpstr[SHORT_STRING];
269 snprintf (title, sizeof (title), _("Query")); /* FIXME */
271 menu = mutt_new_menu ();
272 menu->make_entry = query_entry;
273 menu->search = query_search;
274 menu->tag = query_tag;
275 menu->menu = MENU_QUERY;
278 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
280 if (results == NULL) {
281 /* Prompt for Query */
282 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
283 results = run_query (buf, 0);
288 snprintf (title, sizeof (title), _("Query '%s'"), buf);
290 /* count the number of results */
291 for (queryp = results; queryp; queryp = queryp->next)
294 menu->data = QueryTable =
295 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
297 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
298 QueryTable[i].data = queryp;
301 switch ((op = mutt_menuLoop (menu))) {
302 case OP_QUERY_APPEND:
304 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
305 QUERY *newresults = NULL;
307 newresults = run_query (buf, 0);
309 menu->redraw = REDRAW_FULL;
311 snprintf (title, sizeof (title), _("Query '%s'"), buf);
313 if (op == OP_QUERY) {
316 rfc822_free_address (&queryp->addr);
317 FREE (&queryp->name);
318 FREE (&queryp->other);
319 results = queryp->next;
323 results = newresults;
328 for (queryp = results; queryp->next; queryp = queryp->next);
330 queryp->next = newresults;
335 mutt_menuDestroy (&menu);
336 menu = mutt_new_menu ();
337 menu->make_entry = query_entry;
338 menu->search = query_search;
339 menu->tag = query_tag;
340 menu->menu = MENU_QUERY;
343 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
346 /* count the number of results */
347 for (queryp = results; queryp; queryp = queryp->next)
350 if (op == OP_QUERY) {
351 menu->data = QueryTable =
352 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
354 for (i = 0, queryp = results; queryp;
355 queryp = queryp->next, i++)
356 QueryTable[i].data = queryp;
362 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
364 menu->data = QueryTable;
366 for (i = 0, queryp = results; queryp;
367 queryp = queryp->next, i++) {
368 /* once we hit new entries, clear/init the tag */
369 if (queryp == newresults)
372 QueryTable[i].data = queryp;
374 QueryTable[i].tagged = 0;
381 case OP_CREATE_ALIAS:
382 if (menu->tagprefix) {
383 ADDRESS *naddr = NULL;
385 for (i = 0; i < menu->max; i++)
386 if (QueryTable[i].tagged) {
387 ADDRESS *a = result_to_addr (QueryTable[i].data);
389 rfc822_append (&naddr, a);
390 rfc822_free_address (&a);
393 mutt_create_alias (NULL, naddr);
396 ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
398 mutt_create_alias (NULL, a);
399 rfc822_free_address (&a);
403 case OP_GENERIC_SELECT_ENTRY:
408 /* fall through to OP_MAIL */
411 msg = mutt_new_header ();
412 msg->env = mutt_new_envelope ();
413 if (!menu->tagprefix) {
414 msg->env->to = result_to_addr (QueryTable[menu->current].data);
417 for (i = 0; i < menu->max; i++)
418 if (QueryTable[i].tagged) {
419 ADDRESS *a = result_to_addr (QueryTable[i].data);
421 rfc822_append (&msg->env->to, a);
422 rfc822_free_address (&a);
425 ci_send_message (0, msg, NULL, Context, NULL);
426 menu->redraw = REDRAW_FULL;
435 /* if we need to return the selected entries */
436 if (retbuf && (done == 2)) {
440 memset (buf, 0, buflen);
442 /* check for tagged entries */
443 for (i = 0; i < menu->max; i++) {
444 if (QueryTable[i].tagged) {
446 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
448 mutt_addrlist_to_local (tmpa);
450 rfc822_write_address (buf, buflen, tmpa, 0);
451 curpos = mutt_strlen (buf);
452 rfc822_free_address (&tmpa);
454 else if (curpos + 2 < buflen) {
455 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
457 mutt_addrlist_to_local (tmpa);
458 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
459 rfc822_write_address ((char *) buf + curpos + 1,
460 buflen - curpos - 1, tmpa, 0);
461 curpos = mutt_strlen (buf);
462 rfc822_free_address (&tmpa);
466 /* then enter current message */
468 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
470 mutt_addrlist_to_local (tmpa);
471 rfc822_write_address (buf, buflen, tmpa, 0);
472 rfc822_free_address (&tmpa);
479 rfc822_free_address (&queryp->addr);
480 FREE (&queryp->name);
481 FREE (&queryp->other);
482 results = queryp->next;
488 /* tell whoever called me to redraw the screen when I return */
489 set_option (OPTNEEDREDRAW);
492 mutt_menuDestroy (&menu);