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/lib-lib.h>
19 #include <lib-sys/unix.h>
21 #include <lib-ui/menu.h>
22 #include <lib-ui/curses.h>
26 #include "mutt_idna.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, ssize_t buflen, QUERY * results,
58 static address_t *result_to_addr (QUERY * r)
60 static address_t *tmp;
62 tmp = address_list_dup (r->addr);
64 if (!tmp->next && !tmp->personal)
65 tmp->personal = m_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) {
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 = p_new(QUERY, 1);
106 cur->next = p_new(QUERY, 1);
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 = m_strdup(p);
121 p = strtok (NULL, "\t\n");
123 cur->other = m_strdup(p);
130 if (mutt_wait_filter (thepid)) {
132 mutt_error ("%s", msg);
136 mutt_message ("%s", msg);
142 static int query_search (MUTTMENU * m, regex_t * re, int n)
144 ENTRY *table = (ENTRY *) m->data;
146 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
148 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
150 if (table[n].data->addr) {
151 if (table[n].data->addr->personal &&
152 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
154 if (table[n].data->addr->mailbox &&
155 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
162 /* This is the callback routine from mutt_menuLoop() which is used to generate
163 * a menu entry for the requested item number.
165 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
166 static void query_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
168 ENTRY *table = (ENTRY *) m->data;
169 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
171 /* need a query format ... hard coded constants are not good */
172 while (FirstColumn + SecondColumn > 70) {
173 FirstColumn = FirstColumn * 3 / 4;
174 SecondColumn = SecondColumn * 3 / 4;
175 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
176 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
177 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
178 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
181 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
183 mutt_format_string (buf2, sizeof (buf2),
184 FirstColumn + 2, FirstColumn + 2,
185 0, ' ', table[num].data->name,
186 m_strlen(table[num].data->name), 0);
188 snprintf (s, slen, " %c %3d %s %-*.*s %s",
189 table[num].tagged ? '*' : ' ',
193 SecondColumn + 2, buf, NONULL (table[num].data->other));
196 static int query_tag (MUTTMENU * menu, int n, int m)
198 ENTRY *cur = &((ENTRY *) menu->data)[n];
199 int ot = cur->tagged;
201 cur->tagged = m >= 0 ? m : !cur->tagged;
202 return cur->tagged - ot;
205 int mutt_query_complete (char *buf, ssize_t buflen)
207 QUERY *results = NULL;
211 mutt_error _("Query command not defined.");
216 results = run_query (buf, 1);
218 /* only one response? */
219 if (results->next == NULL) {
220 tmpa = result_to_addr (results);
221 mutt_addrlist_to_local (tmpa);
223 rfc822_write_address (buf, buflen, tmpa, 0);
224 address_list_wipe(&tmpa);
228 /* multiple results, choose from query menu */
229 query_menu (buf, buflen, results, 1);
234 void mutt_query_menu (char *buf, ssize_t buflen)
237 mutt_error _("Query command not defined.");
243 char buffer[STRING] = "";
245 query_menu (buffer, sizeof (buffer), NULL, 0);
248 query_menu (buf, buflen, NULL, 1);
252 static void query_menu (char *buf, ssize_t buflen, QUERY * results, int retbuf)
256 ENTRY *QueryTable = NULL;
257 QUERY *queryp = NULL;
260 char helpstr[SHORT_STRING];
263 snprintf (title, sizeof (title), _("Query")); /* FIXME */
265 menu = mutt_new_menu ();
266 menu->make_entry = query_entry;
267 menu->search = query_search;
268 menu->tag = query_tag;
269 menu->menu = MENU_QUERY;
272 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
274 if (results == NULL) {
275 /* Prompt for Query */
276 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
277 results = run_query (buf, 0);
282 snprintf (title, sizeof (title), _("Query '%s'"), buf);
284 /* count the number of results */
285 for (queryp = results; queryp; queryp = queryp->next)
288 menu->data = QueryTable = p_new(ENTRY, menu->max);
290 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
291 QueryTable[i].data = queryp;
294 switch ((op = mutt_menuLoop (menu))) {
295 case OP_QUERY_APPEND:
297 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
298 QUERY *newresults = NULL;
300 newresults = run_query (buf, 0);
302 menu->redraw = REDRAW_FULL;
304 snprintf (title, sizeof (title), _("Query '%s'"), buf);
306 if (op == OP_QUERY) {
309 address_list_wipe(&queryp->addr);
310 p_delete(&queryp->name);
311 p_delete(&queryp->other);
312 results = queryp->next;
316 results = newresults;
317 p_delete(&QueryTable);
321 for (queryp = results; queryp->next; queryp = queryp->next);
323 queryp->next = newresults;
328 mutt_menuDestroy (&menu);
329 menu = mutt_new_menu ();
330 menu->make_entry = query_entry;
331 menu->search = query_search;
332 menu->tag = query_tag;
333 menu->menu = MENU_QUERY;
336 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
339 /* count the number of results */
340 for (queryp = results; queryp; queryp = queryp->next)
343 if (op == OP_QUERY) {
344 menu->data = QueryTable = p_new(ENTRY, menu->max);
346 for (i = 0, queryp = results; queryp;
347 queryp = queryp->next, i++)
348 QueryTable[i].data = queryp;
354 p_realloc(&QueryTable, menu->max);
356 menu->data = QueryTable;
358 for (i = 0, queryp = results; queryp;
359 queryp = queryp->next, i++) {
360 /* once we hit new entries, clear/init the tag */
361 if (queryp == newresults)
364 QueryTable[i].data = queryp;
366 QueryTable[i].tagged = 0;
373 case OP_CREATE_ALIAS:
374 if (menu->tagprefix) {
375 address_t *naddr = NULL;
377 for (i = 0; i < menu->max; i++)
378 if (QueryTable[i].tagged) {
379 address_list_append(&naddr, result_to_addr(QueryTable[i].data));
382 mutt_create_alias (NULL, naddr);
385 address_t *a = result_to_addr (QueryTable[menu->current].data);
387 mutt_create_alias (NULL, a);
388 address_list_wipe(&a);
392 case OP_GENERIC_SELECT_ENTRY:
397 /* fall through to OP_MAIL */
401 msg->env = envelope_new();
402 if (!menu->tagprefix) {
403 msg->env->to = result_to_addr (QueryTable[menu->current].data);
406 for (i = 0; i < menu->max; i++)
407 if (QueryTable[i].tagged) {
408 address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
411 ci_send_message (0, msg, NULL, Context, NULL);
412 menu->redraw = REDRAW_FULL;
421 /* if we need to return the selected entries */
422 if (retbuf && (done == 2)) {
426 p_clear(buf, buflen);
428 /* check for tagged entries */
429 for (i = 0; i < menu->max; i++) {
430 if (QueryTable[i].tagged) {
432 address_t *tmpa = result_to_addr (QueryTable[i].data);
434 mutt_addrlist_to_local (tmpa);
436 rfc822_write_address (buf, buflen, tmpa, 0);
437 curpos = m_strlen(buf);
438 address_list_wipe(&tmpa);
440 else if (curpos + 2 < buflen) {
441 address_t *tmpa = result_to_addr (QueryTable[i].data);
443 mutt_addrlist_to_local (tmpa);
444 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
445 rfc822_write_address ((char *) buf + curpos + 1,
446 buflen - curpos - 1, tmpa, 0);
447 curpos = m_strlen(buf);
448 address_list_wipe(&tmpa);
452 /* then enter current message */
454 address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
456 mutt_addrlist_to_local (tmpa);
457 rfc822_write_address (buf, buflen, tmpa, 0);
458 address_list_wipe(&tmpa);
465 address_list_wipe(&queryp->addr);
466 p_delete(&queryp->name);
467 p_delete(&queryp->other);
468 results = queryp->next;
472 p_delete(&QueryTable);
474 /* tell whoever called me to redraw the screen when I return */
475 set_option (OPTNEEDREDRAW);
478 mutt_menuDestroy (&menu);