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.
18 #include <lib-lib/mem.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/macros.h>
21 #include <lib-lib/file.h>
22 #include <lib-lib/mapping.h>
24 #include <lib-ui/menu.h>
28 #include "mutt_idna.h"
31 typedef struct query {
38 typedef struct entry {
43 static struct mapping_t QueryHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("Mail"), OP_MAIL},
46 {N_("New Query"), OP_QUERY},
47 {N_("Make Alias"), OP_CREATE_ALIAS},
48 {N_("Search"), OP_SEARCH},
49 {N_("Help"), OP_HELP},
53 /* Variables for outsizing output format */
54 static int FirstColumn;
55 static int SecondColumn;
57 static void query_menu (char *buf, ssize_t buflen, QUERY * results,
60 static address_t *result_to_addr (QUERY * r)
62 static address_t *tmp;
64 tmp = address_list_dup (r->addr);
66 if (!tmp->next && !tmp->personal)
67 tmp->personal = m_strdup(r->name);
69 mutt_addrlist_to_idna (tmp, NULL);
73 static QUERY *run_query (char *s, int quiet)
78 char cmd[_POSIX_PATH_MAX];
88 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
90 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
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)) {
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))
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, ssize_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 m_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, ssize_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 address_list_wipe(&tmpa);
230 /* multiple results, choose from query menu */
231 query_menu (buf, buflen, results, 1);
236 void mutt_query_menu (char *buf, ssize_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, ssize_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 = p_new(ENTRY, menu->max);
292 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
293 QueryTable[i].data = queryp;
296 switch ((op = mutt_menuLoop (menu))) {
297 case OP_QUERY_APPEND:
299 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
300 QUERY *newresults = NULL;
302 newresults = run_query (buf, 0);
304 menu->redraw = REDRAW_FULL;
306 snprintf (title, sizeof (title), _("Query '%s'"), buf);
308 if (op == OP_QUERY) {
311 address_list_wipe(&queryp->addr);
312 p_delete(&queryp->name);
313 p_delete(&queryp->other);
314 results = queryp->next;
318 results = newresults;
319 p_delete(&QueryTable);
323 for (queryp = results; queryp->next; queryp = queryp->next);
325 queryp->next = newresults;
330 mutt_menuDestroy (&menu);
331 menu = mutt_new_menu ();
332 menu->make_entry = query_entry;
333 menu->search = query_search;
334 menu->tag = query_tag;
335 menu->menu = MENU_QUERY;
338 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
341 /* count the number of results */
342 for (queryp = results; queryp; queryp = queryp->next)
345 if (op == OP_QUERY) {
346 menu->data = QueryTable = p_new(ENTRY, menu->max);
348 for (i = 0, queryp = results; queryp;
349 queryp = queryp->next, i++)
350 QueryTable[i].data = queryp;
356 p_realloc(&QueryTable, menu->max);
358 menu->data = QueryTable;
360 for (i = 0, queryp = results; queryp;
361 queryp = queryp->next, i++) {
362 /* once we hit new entries, clear/init the tag */
363 if (queryp == newresults)
366 QueryTable[i].data = queryp;
368 QueryTable[i].tagged = 0;
375 case OP_CREATE_ALIAS:
376 if (menu->tagprefix) {
377 address_t *naddr = NULL;
379 for (i = 0; i < menu->max; i++)
380 if (QueryTable[i].tagged) {
381 address_list_append(&naddr, result_to_addr(QueryTable[i].data));
384 mutt_create_alias (NULL, naddr);
387 address_t *a = result_to_addr (QueryTable[menu->current].data);
389 mutt_create_alias (NULL, a);
390 address_list_wipe(&a);
394 case OP_GENERIC_SELECT_ENTRY:
399 /* fall through to OP_MAIL */
403 msg->env = envelope_new();
404 if (!menu->tagprefix) {
405 msg->env->to = result_to_addr (QueryTable[menu->current].data);
408 for (i = 0; i < menu->max; i++)
409 if (QueryTable[i].tagged) {
410 address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
413 ci_send_message (0, msg, NULL, Context, NULL);
414 menu->redraw = REDRAW_FULL;
423 /* if we need to return the selected entries */
424 if (retbuf && (done == 2)) {
428 p_clear(buf, buflen);
430 /* check for tagged entries */
431 for (i = 0; i < menu->max; i++) {
432 if (QueryTable[i].tagged) {
434 address_t *tmpa = result_to_addr (QueryTable[i].data);
436 mutt_addrlist_to_local (tmpa);
438 rfc822_write_address (buf, buflen, tmpa, 0);
439 curpos = m_strlen(buf);
440 address_list_wipe(&tmpa);
442 else if (curpos + 2 < buflen) {
443 address_t *tmpa = result_to_addr (QueryTable[i].data);
445 mutt_addrlist_to_local (tmpa);
446 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
447 rfc822_write_address ((char *) buf + curpos + 1,
448 buflen - curpos - 1, tmpa, 0);
449 curpos = m_strlen(buf);
450 address_list_wipe(&tmpa);
454 /* then enter current message */
456 address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
458 mutt_addrlist_to_local (tmpa);
459 rfc822_write_address (buf, buflen, tmpa, 0);
460 address_list_wipe(&tmpa);
467 address_list_wipe(&queryp->addr);
468 p_delete(&queryp->name);
469 p_delete(&queryp->other);
470 results = queryp->next;
474 p_delete(&QueryTable);
476 /* tell whoever called me to redraw the screen when I return */
477 set_option (OPTNEEDREDRAW);
480 mutt_menuDestroy (&menu);