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"
23 #include "lib/debug.h"
29 typedef struct query {
36 typedef struct entry {
41 static struct mapping_t QueryHelp[] = {
42 {N_("Exit"), OP_EXIT},
43 {N_("Mail"), OP_MAIL},
44 {N_("New Query"), OP_QUERY},
45 {N_("Make Alias"), OP_CREATE_ALIAS},
46 {N_("Search"), OP_SEARCH},
47 {N_("Help"), OP_HELP},
51 /* Variables for outsizing output format */
52 static int FirstColumn;
53 static int SecondColumn;
55 static void query_menu (char *buf, size_t buflen, QUERY * results,
58 static ADDRESS *result_to_addr (QUERY * r)
62 tmp = rfc822_cpy_adr (r->addr);
64 if (!tmp->next && !tmp->personal)
65 tmp->personal = safe_strdup (r->name);
67 mutt_addrlist_to_idna (tmp, NULL);
71 static QUERY *run_query (char *s, int quiet)
76 char cmd[_POSIX_PATH_MAX];
86 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
88 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
89 debug_print (1, ("unable to fork command: %s\n", cmd));
93 mutt_message _("Waiting for response...");
95 fgets (msg, sizeof (msg), fp);
96 if ((p = strrchr (msg, '\n')))
98 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
99 if ((p = strtok (buf, "\t\n"))) {
103 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
107 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
111 l = mutt_strwidth (p);
112 if (l > SecondColumn)
115 cur->addr = rfc822_parse_adrlist (cur->addr, p);
116 p = strtok (NULL, "\t\n");
118 l = mutt_strwidth (p);
121 cur->name = safe_strdup (p);
122 p = strtok (NULL, "\t\n");
124 cur->other = safe_strdup (p);
131 if (mutt_wait_filter (thepid)) {
132 debug_print (1, ("Error: %s\n", msg));
134 mutt_error ("%s", msg);
138 mutt_message ("%s", msg);
144 static int query_search (MUTTMENU * m, regex_t * re, int n)
146 ENTRY *table = (ENTRY *) m->data;
148 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
150 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
152 if (table[n].data->addr) {
153 if (table[n].data->addr->personal &&
154 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
156 if (table[n].data->addr->mailbox &&
157 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
160 if (table[n].data->addr->val &&
161 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
169 /* This is the callback routine from mutt_menuLoop() which is used to generate
170 * a menu entry for the requested item number.
172 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
173 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
175 ENTRY *table = (ENTRY *) m->data;
176 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
178 /* need a query format ... hard coded constants are not good */
179 while (FirstColumn + SecondColumn > 70) {
180 FirstColumn = FirstColumn * 3 / 4;
181 SecondColumn = SecondColumn * 3 / 4;
182 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
183 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
184 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
185 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
188 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
190 mutt_format_string (buf2, sizeof (buf2),
191 FirstColumn + 2, FirstColumn + 2,
192 0, ' ', table[num].data->name,
193 safe_strlen (table[num].data->name), 0);
195 snprintf (s, slen, " %c %3d %s %-*.*s %s",
196 table[num].tagged ? '*' : ' ',
200 SecondColumn + 2, buf, NONULL (table[num].data->other));
203 static int query_tag (MUTTMENU * menu, int n, int m)
205 ENTRY *cur = &((ENTRY *) menu->data)[n];
206 int ot = cur->tagged;
208 cur->tagged = m >= 0 ? m : !cur->tagged;
209 return cur->tagged - ot;
212 int mutt_query_complete (char *buf, size_t buflen)
214 QUERY *results = NULL;
218 mutt_error _("Query command not defined.");
223 results = run_query (buf, 1);
225 /* only one response? */
226 if (results->next == NULL) {
227 tmpa = result_to_addr (results);
228 mutt_addrlist_to_local (tmpa);
230 rfc822_write_address (buf, buflen, tmpa, 0);
231 rfc822_free_address (&tmpa);
235 /* multiple results, choose from query menu */
236 query_menu (buf, buflen, results, 1);
241 void mutt_query_menu (char *buf, size_t buflen)
244 mutt_error _("Query command not defined.");
250 char buffer[STRING] = "";
252 query_menu (buffer, sizeof (buffer), NULL, 0);
255 query_menu (buf, buflen, NULL, 1);
259 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
263 ENTRY *QueryTable = NULL;
264 QUERY *queryp = NULL;
267 char helpstr[SHORT_STRING];
270 snprintf (title, sizeof (title), _("Query")); /* FIXME */
272 menu = mutt_new_menu ();
273 menu->make_entry = query_entry;
274 menu->search = query_search;
275 menu->tag = query_tag;
276 menu->menu = MENU_QUERY;
279 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
281 if (results == NULL) {
282 /* Prompt for Query */
283 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
284 results = run_query (buf, 0);
289 snprintf (title, sizeof (title), _("Query '%s'"), buf);
291 /* count the number of results */
292 for (queryp = results; queryp; queryp = queryp->next)
295 menu->data = QueryTable =
296 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
298 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
299 QueryTable[i].data = queryp;
302 switch ((op = mutt_menuLoop (menu))) {
303 case OP_QUERY_APPEND:
305 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
306 QUERY *newresults = NULL;
308 newresults = run_query (buf, 0);
310 menu->redraw = REDRAW_FULL;
312 snprintf (title, sizeof (title), _("Query '%s'"), buf);
314 if (op == OP_QUERY) {
317 rfc822_free_address (&queryp->addr);
318 FREE (&queryp->name);
319 FREE (&queryp->other);
320 results = queryp->next;
324 results = newresults;
329 for (queryp = results; queryp->next; queryp = queryp->next);
331 queryp->next = newresults;
336 mutt_menuDestroy (&menu);
337 menu = mutt_new_menu ();
338 menu->make_entry = query_entry;
339 menu->search = query_search;
340 menu->tag = query_tag;
341 menu->menu = MENU_QUERY;
344 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
347 /* count the number of results */
348 for (queryp = results; queryp; queryp = queryp->next)
351 if (op == OP_QUERY) {
352 menu->data = QueryTable =
353 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
355 for (i = 0, queryp = results; queryp;
356 queryp = queryp->next, i++)
357 QueryTable[i].data = queryp;
363 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
365 menu->data = QueryTable;
367 for (i = 0, queryp = results; queryp;
368 queryp = queryp->next, i++) {
369 /* once we hit new entries, clear/init the tag */
370 if (queryp == newresults)
373 QueryTable[i].data = queryp;
375 QueryTable[i].tagged = 0;
382 case OP_CREATE_ALIAS:
383 if (menu->tagprefix) {
384 ADDRESS *naddr = NULL;
386 for (i = 0; i < menu->max; i++)
387 if (QueryTable[i].tagged) {
388 ADDRESS *a = result_to_addr (QueryTable[i].data);
390 rfc822_append (&naddr, a);
391 rfc822_free_address (&a);
394 mutt_create_alias (NULL, naddr);
397 ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
399 mutt_create_alias (NULL, a);
400 rfc822_free_address (&a);
404 case OP_GENERIC_SELECT_ENTRY:
409 /* fall through to OP_MAIL */
412 msg = mutt_new_header ();
413 msg->env = mutt_new_envelope ();
414 if (!menu->tagprefix) {
415 msg->env->to = result_to_addr (QueryTable[menu->current].data);
418 for (i = 0; i < menu->max; i++)
419 if (QueryTable[i].tagged) {
420 ADDRESS *a = result_to_addr (QueryTable[i].data);
422 rfc822_append (&msg->env->to, a);
423 rfc822_free_address (&a);
426 ci_send_message (0, msg, NULL, Context, NULL);
427 menu->redraw = REDRAW_FULL;
436 /* if we need to return the selected entries */
437 if (retbuf && (done == 2)) {
441 memset (buf, 0, buflen);
443 /* check for tagged entries */
444 for (i = 0; i < menu->max; i++) {
445 if (QueryTable[i].tagged) {
447 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
449 mutt_addrlist_to_local (tmpa);
451 rfc822_write_address (buf, buflen, tmpa, 0);
452 curpos = safe_strlen (buf);
453 rfc822_free_address (&tmpa);
455 else if (curpos + 2 < buflen) {
456 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
458 mutt_addrlist_to_local (tmpa);
459 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
460 rfc822_write_address ((char *) buf + curpos + 1,
461 buflen - curpos - 1, tmpa, 0);
462 curpos = safe_strlen (buf);
463 rfc822_free_address (&tmpa);
467 /* then enter current message */
469 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
471 mutt_addrlist_to_local (tmpa);
472 rfc822_write_address (buf, buflen, tmpa, 0);
473 rfc822_free_address (&tmpa);
480 rfc822_free_address (&queryp->addr);
481 FREE (&queryp->name);
482 FREE (&queryp->other);
483 results = queryp->next;
489 /* tell whoever called me to redraw the screen when I return */
490 set_option (OPTNEEDREDRAW);
493 mutt_menuDestroy (&menu);