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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
19 #include "mutt_menu.h"
20 #include "mutt_idna.h"
24 #include "lib/debug.h"
30 typedef struct query {
37 typedef struct entry {
42 static struct mapping_t QueryHelp[] = {
43 {N_("Exit"), OP_EXIT},
44 {N_("Mail"), OP_MAIL},
45 {N_("New Query"), OP_QUERY},
46 {N_("Make Alias"), OP_CREATE_ALIAS},
47 {N_("Search"), OP_SEARCH},
48 {N_("Help"), OP_HELP},
52 /* Variables for outsizing output format */
53 static int FirstColumn;
54 static int SecondColumn;
56 static void query_menu (char *buf, size_t buflen, QUERY * results,
59 static ADDRESS *result_to_addr (QUERY * r)
63 tmp = rfc822_cpy_adr (r->addr);
65 if (!tmp->next && !tmp->personal)
66 tmp->personal = m_strdup(r->name);
68 mutt_addrlist_to_idna (tmp, NULL);
72 static QUERY *run_query (char *s, int quiet)
77 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) {
90 debug_print (1, ("unable to fork command: %s\n", cmd));
94 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)) != NULL) {
100 if ((p = strtok (buf, "\t\n"))) {
104 first = p_new(QUERY, 1);
108 cur->next = p_new(QUERY, 1);
112 l = mutt_strwidth (p);
113 if (l > SecondColumn)
116 cur->addr = rfc822_parse_adrlist (cur->addr, p);
117 p = strtok (NULL, "\t\n");
119 l = mutt_strwidth (p);
122 cur->name = m_strdup(p);
123 p = strtok (NULL, "\t\n");
125 cur->other = m_strdup(p);
132 if (mutt_wait_filter (thepid)) {
133 debug_print (1, ("Error: %s\n", msg));
135 mutt_error ("%s", msg);
139 mutt_message ("%s", msg);
145 static int query_search (MUTTMENU * m, regex_t * re, int n)
147 ENTRY *table = (ENTRY *) m->data;
149 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
151 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
153 if (table[n].data->addr) {
154 if (table[n].data->addr->personal &&
155 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
157 if (table[n].data->addr->mailbox &&
158 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
165 /* This is the callback routine from mutt_menuLoop() which is used to generate
166 * a menu entry for the requested item number.
168 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
169 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
171 ENTRY *table = (ENTRY *) m->data;
172 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
174 /* need a query format ... hard coded constants are not good */
175 while (FirstColumn + SecondColumn > 70) {
176 FirstColumn = FirstColumn * 3 / 4;
177 SecondColumn = SecondColumn * 3 / 4;
178 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
179 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
180 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
181 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
184 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
186 mutt_format_string (buf2, sizeof (buf2),
187 FirstColumn + 2, FirstColumn + 2,
188 0, ' ', table[num].data->name,
189 m_strlen(table[num].data->name), 0);
191 snprintf (s, slen, " %c %3d %s %-*.*s %s",
192 table[num].tagged ? '*' : ' ',
196 SecondColumn + 2, buf, NONULL (table[num].data->other));
199 static int query_tag (MUTTMENU * menu, int n, int m)
201 ENTRY *cur = &((ENTRY *) menu->data)[n];
202 int ot = cur->tagged;
204 cur->tagged = m >= 0 ? m : !cur->tagged;
205 return cur->tagged - ot;
208 int mutt_query_complete (char *buf, size_t buflen)
210 QUERY *results = NULL;
214 mutt_error _("Query command not defined.");
219 results = run_query (buf, 1);
221 /* only one response? */
222 if (results->next == NULL) {
223 tmpa = result_to_addr (results);
224 mutt_addrlist_to_local (tmpa);
226 rfc822_write_address (buf, buflen, tmpa, 0);
227 rfc822_free_address (&tmpa);
231 /* multiple results, choose from query menu */
232 query_menu (buf, buflen, results, 1);
237 void mutt_query_menu (char *buf, size_t buflen)
240 mutt_error _("Query command not defined.");
246 char buffer[STRING] = "";
248 query_menu (buffer, sizeof (buffer), NULL, 0);
251 query_menu (buf, buflen, NULL, 1);
255 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
259 ENTRY *QueryTable = NULL;
260 QUERY *queryp = NULL;
263 char helpstr[SHORT_STRING];
266 snprintf (title, sizeof (title), _("Query")); /* FIXME */
268 menu = mutt_new_menu ();
269 menu->make_entry = query_entry;
270 menu->search = query_search;
271 menu->tag = query_tag;
272 menu->menu = MENU_QUERY;
275 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
277 if (results == NULL) {
278 /* Prompt for Query */
279 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
280 results = run_query (buf, 0);
285 snprintf (title, sizeof (title), _("Query '%s'"), buf);
287 /* count the number of results */
288 for (queryp = results; queryp; queryp = queryp->next)
291 menu->data = QueryTable = p_new(ENTRY, menu->max);
293 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
294 QueryTable[i].data = queryp;
297 switch ((op = mutt_menuLoop (menu))) {
298 case OP_QUERY_APPEND:
300 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
301 QUERY *newresults = NULL;
303 newresults = run_query (buf, 0);
305 menu->redraw = REDRAW_FULL;
307 snprintf (title, sizeof (title), _("Query '%s'"), buf);
309 if (op == OP_QUERY) {
312 rfc822_free_address (&queryp->addr);
313 p_delete(&queryp->name);
314 p_delete(&queryp->other);
315 results = queryp->next;
319 results = newresults;
320 p_delete(&QueryTable);
324 for (queryp = results; queryp->next; queryp = queryp->next);
326 queryp->next = newresults;
331 mutt_menuDestroy (&menu);
332 menu = mutt_new_menu ();
333 menu->make_entry = query_entry;
334 menu->search = query_search;
335 menu->tag = query_tag;
336 menu->menu = MENU_QUERY;
339 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
342 /* count the number of results */
343 for (queryp = results; queryp; queryp = queryp->next)
346 if (op == OP_QUERY) {
347 menu->data = QueryTable = p_new(ENTRY, menu->max);
349 for (i = 0, queryp = results; queryp;
350 queryp = queryp->next, i++)
351 QueryTable[i].data = queryp;
357 p_realloc(&QueryTable, menu->max);
359 menu->data = QueryTable;
361 for (i = 0, queryp = results; queryp;
362 queryp = queryp->next, i++) {
363 /* once we hit new entries, clear/init the tag */
364 if (queryp == newresults)
367 QueryTable[i].data = queryp;
369 QueryTable[i].tagged = 0;
376 case OP_CREATE_ALIAS:
377 if (menu->tagprefix) {
378 ADDRESS *naddr = NULL;
380 for (i = 0; i < menu->max; i++)
381 if (QueryTable[i].tagged) {
382 ADDRESS *a = result_to_addr (QueryTable[i].data);
384 rfc822_append (&naddr, a);
385 rfc822_free_address (&a);
388 mutt_create_alias (NULL, naddr);
391 ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
393 mutt_create_alias (NULL, a);
394 rfc822_free_address (&a);
398 case OP_GENERIC_SELECT_ENTRY:
403 /* fall through to OP_MAIL */
406 msg = mutt_new_header ();
407 msg->env = mutt_new_envelope ();
408 if (!menu->tagprefix) {
409 msg->env->to = result_to_addr (QueryTable[menu->current].data);
412 for (i = 0; i < menu->max; i++)
413 if (QueryTable[i].tagged) {
414 ADDRESS *a = result_to_addr (QueryTable[i].data);
416 rfc822_append (&msg->env->to, a);
417 rfc822_free_address (&a);
420 ci_send_message (0, msg, NULL, Context, NULL);
421 menu->redraw = REDRAW_FULL;
430 /* if we need to return the selected entries */
431 if (retbuf && (done == 2)) {
435 memset (buf, 0, buflen);
437 /* check for tagged entries */
438 for (i = 0; i < menu->max; i++) {
439 if (QueryTable[i].tagged) {
441 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
443 mutt_addrlist_to_local (tmpa);
445 rfc822_write_address (buf, buflen, tmpa, 0);
446 curpos = m_strlen(buf);
447 rfc822_free_address (&tmpa);
449 else if (curpos + 2 < buflen) {
450 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
452 mutt_addrlist_to_local (tmpa);
453 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
454 rfc822_write_address ((char *) buf + curpos + 1,
455 buflen - curpos - 1, tmpa, 0);
456 curpos = m_strlen(buf);
457 rfc822_free_address (&tmpa);
461 /* then enter current message */
463 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
465 mutt_addrlist_to_local (tmpa);
466 rfc822_write_address (buf, buflen, tmpa, 0);
467 rfc822_free_address (&tmpa);
474 rfc822_free_address (&queryp->addr);
475 p_delete(&queryp->name);
476 p_delete(&queryp->other);
477 results = queryp->next;
481 p_delete(&QueryTable);
483 /* tell whoever called me to redraw the screen when I return */
484 set_option (OPTNEEDREDRAW);
487 mutt_menuDestroy (&menu);