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"
24 typedef struct query {
31 typedef struct entry {
36 static struct mapping_t QueryHelp[] = {
37 {N_("Exit"), OP_EXIT},
38 {N_("Mail"), OP_MAIL},
39 {N_("New Query"), OP_QUERY},
40 {N_("Make Alias"), OP_CREATE_ALIAS},
41 {N_("Search"), OP_SEARCH},
42 {N_("Help"), OP_HELP},
46 /* Variables for outsizing output format */
47 static int FirstColumn;
48 static int SecondColumn;
50 static void query_menu (char *buf, size_t buflen, QUERY * results,
53 static ADDRESS *result_to_addr (QUERY * r)
57 tmp = rfc822_cpy_adr (r->addr);
59 if (!tmp->next && !tmp->personal)
60 tmp->personal = safe_strdup (r->name);
62 mutt_addrlist_to_idna (tmp, NULL);
66 static QUERY *run_query (char *s, int quiet)
71 char cmd[_POSIX_PATH_MAX];
81 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
83 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
84 dprint (1, (debugfile, "unable to fork command: %s", cmd));
88 mutt_message _("Waiting for response...");
90 fgets (msg, sizeof (msg), fp);
91 if ((p = strrchr (msg, '\n')))
93 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
94 if ((p = strtok (buf, "\t\n"))) {
98 first = (QUERY *) safe_calloc (1, sizeof (QUERY));
102 cur->next = (QUERY *) safe_calloc (1, sizeof (QUERY));
106 l = mutt_strwidth (p);
107 if (l > SecondColumn)
110 cur->addr = rfc822_parse_adrlist (cur->addr, p);
111 p = strtok (NULL, "\t\n");
113 l = mutt_strwidth (p);
116 cur->name = safe_strdup (p);
117 p = strtok (NULL, "\t\n");
119 cur->other = safe_strdup (p);
126 if (mutt_wait_filter (thepid)) {
127 dprint (1, (debugfile, "Error: %s\n", msg));
129 mutt_error ("%s", msg);
133 mutt_message ("%s", msg);
139 static int query_search (MUTTMENU * m, regex_t * re, int n)
141 ENTRY *table = (ENTRY *) m->data;
143 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
145 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
147 if (table[n].data->addr) {
148 if (table[n].data->addr->personal &&
149 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
151 if (table[n].data->addr->mailbox &&
152 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
155 if (table[n].data->addr->val &&
156 !regexec (re, table[n].data->addr->val, 0, NULL, 0))
164 /* This is the callback routine from mutt_menuLoop() which is used to generate
165 * a menu entry for the requested item number.
167 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
168 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
170 ENTRY *table = (ENTRY *) m->data;
171 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
173 /* need a query format ... hard coded constants are not good */
174 while (FirstColumn + SecondColumn > 70) {
175 FirstColumn = FirstColumn * 3 / 4;
176 SecondColumn = SecondColumn * 3 / 4;
177 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
178 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
179 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
180 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
183 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
185 mutt_format_string (buf2, sizeof (buf2),
186 FirstColumn + 2, FirstColumn + 2,
187 0, ' ', table[num].data->name,
188 mutt_strlen (table[num].data->name), 0);
190 snprintf (s, slen, " %c %3d %s %-*.*s %s",
191 table[num].tagged ? '*' : ' ',
195 SecondColumn + 2, buf, NONULL (table[num].data->other));
198 static int query_tag (MUTTMENU * menu, int n, int m)
200 ENTRY *cur = &((ENTRY *) menu->data)[n];
201 int ot = cur->tagged;
203 cur->tagged = m >= 0 ? m : !cur->tagged;
204 return cur->tagged - ot;
207 int mutt_query_complete (char *buf, size_t buflen)
209 QUERY *results = NULL;
213 mutt_error _("Query command not defined.");
218 results = run_query (buf, 1);
220 /* only one response? */
221 if (results->next == NULL) {
222 tmpa = result_to_addr (results);
223 mutt_addrlist_to_local (tmpa);
225 rfc822_write_address (buf, buflen, tmpa, 0);
226 rfc822_free_address (&tmpa);
230 /* multiple results, choose from query menu */
231 query_menu (buf, buflen, results, 1);
236 void mutt_query_menu (char *buf, size_t buflen)
239 mutt_error _("Query command not defined.");
245 char buffer[STRING] = "";
247 query_menu (buffer, sizeof (buffer), NULL, 0);
250 query_menu (buf, buflen, NULL, 1);
254 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
258 ENTRY *QueryTable = NULL;
259 QUERY *queryp = NULL;
262 char helpstr[SHORT_STRING];
265 snprintf (title, sizeof (title), _("Query")); /* FIXME */
267 menu = mutt_new_menu ();
268 menu->make_entry = query_entry;
269 menu->search = query_search;
270 menu->tag = query_tag;
271 menu->menu = MENU_QUERY;
274 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
276 if (results == NULL) {
277 /* Prompt for Query */
278 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
279 results = run_query (buf, 0);
284 snprintf (title, sizeof (title), _("Query '%s'"), buf);
286 /* count the number of results */
287 for (queryp = results; queryp; queryp = queryp->next)
290 menu->data = QueryTable =
291 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
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 FREE (&queryp->name);
314 FREE (&queryp->other);
315 results = queryp->next;
319 results = newresults;
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 =
348 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
350 for (i = 0, queryp = results; queryp;
351 queryp = queryp->next, i++)
352 QueryTable[i].data = queryp;
358 safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
360 menu->data = QueryTable;
362 for (i = 0, queryp = results; queryp;
363 queryp = queryp->next, i++) {
364 /* once we hit new entries, clear/init the tag */
365 if (queryp == newresults)
368 QueryTable[i].data = queryp;
370 QueryTable[i].tagged = 0;
377 case OP_CREATE_ALIAS:
378 if (menu->tagprefix) {
379 ADDRESS *naddr = NULL;
381 for (i = 0; i < menu->max; i++)
382 if (QueryTable[i].tagged) {
383 ADDRESS *a = result_to_addr (QueryTable[i].data);
385 rfc822_append (&naddr, a);
386 rfc822_free_address (&a);
389 mutt_create_alias (NULL, naddr);
392 ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
394 mutt_create_alias (NULL, a);
395 rfc822_free_address (&a);
399 case OP_GENERIC_SELECT_ENTRY:
404 /* fall through to OP_MAIL */
407 msg = mutt_new_header ();
408 msg->env = mutt_new_envelope ();
409 if (!menu->tagprefix) {
410 msg->env->to = result_to_addr (QueryTable[menu->current].data);
413 for (i = 0; i < menu->max; i++)
414 if (QueryTable[i].tagged) {
415 ADDRESS *a = result_to_addr (QueryTable[i].data);
417 rfc822_append (&msg->env->to, a);
418 rfc822_free_address (&a);
421 ci_send_message (0, msg, NULL, Context, NULL);
422 menu->redraw = REDRAW_FULL;
431 /* if we need to return the selected entries */
432 if (retbuf && (done == 2)) {
436 memset (buf, 0, buflen);
438 /* check for tagged entries */
439 for (i = 0; i < menu->max; i++) {
440 if (QueryTable[i].tagged) {
442 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
444 mutt_addrlist_to_local (tmpa);
446 rfc822_write_address (buf, buflen, tmpa, 0);
447 curpos = mutt_strlen (buf);
448 rfc822_free_address (&tmpa);
450 else if (curpos + 2 < buflen) {
451 ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
453 mutt_addrlist_to_local (tmpa);
454 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
455 rfc822_write_address ((char *) buf + curpos + 1,
456 buflen - curpos - 1, tmpa, 0);
457 curpos = mutt_strlen (buf);
458 rfc822_free_address (&tmpa);
462 /* then enter current message */
464 ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
466 mutt_addrlist_to_local (tmpa);
467 rfc822_write_address (buf, buflen, tmpa, 0);
468 rfc822_free_address (&tmpa);
475 rfc822_free_address (&queryp->addr);
476 FREE (&queryp->name);
477 FREE (&queryp->other);
478 results = queryp->next;
484 /* tell whoever called me to redraw the screen when I return */
485 set_option (OPTNEEDREDRAW);
488 mutt_menuDestroy (&menu);