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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-sys/unix.h>
14 #include <lib-ui/menu.h>
15 #include <lib-ui/curses.h>
19 #include "mutt_idna.h"
22 typedef struct query {
29 typedef struct entry {
34 static struct mapping_t QueryHelp[] = {
35 {N_("Exit"), OP_EXIT},
36 {N_("Mail"), OP_MAIL},
37 {N_("New Query"), OP_QUERY},
38 {N_("Make Alias"), OP_CREATE_ALIAS},
39 {N_("Search"), OP_SEARCH},
40 {N_("Help"), OP_HELP},
44 /* Variables for outsizing output format */
45 static int FirstColumn;
46 static int SecondColumn;
48 static void query_menu (char *buf, ssize_t buflen, QUERY * results,
51 static address_t *result_to_addr (QUERY * r)
53 static address_t *tmp;
55 tmp = address_list_dup (r->addr);
57 if (!tmp->next && !tmp->personal)
58 tmp->personal = m_strdup(r->name);
60 mutt_addrlist_to_idna (tmp, NULL);
64 static QUERY *run_query (char *s, int quiet)
69 char cmd[_POSIX_PATH_MAX];
79 mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
81 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
85 mutt_message _("Waiting for response...");
87 fgets (msg, sizeof (msg), fp);
88 if ((p = strrchr (msg, '\n')))
90 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
91 if ((p = strtok (buf, "\t\n"))) {
95 first = p_new(QUERY, 1);
99 cur->next = p_new(QUERY, 1);
103 l = mutt_strwidth (p);
104 if (l > SecondColumn)
107 cur->addr = rfc822_parse_adrlist (cur->addr, p);
108 p = strtok (NULL, "\t\n");
110 l = mutt_strwidth (p);
113 cur->name = m_strdup(p);
114 p = strtok (NULL, "\t\n");
116 cur->other = m_strdup(p);
123 if (mutt_wait_filter (thepid)) {
125 mutt_error ("%s", msg);
129 mutt_message ("%s", msg);
135 static int query_search (MUTTMENU * m, regex_t * re, int n)
137 ENTRY *table = (ENTRY *) m->data;
139 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
141 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
143 if (table[n].data->addr) {
144 if (table[n].data->addr->personal &&
145 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
147 if (table[n].data->addr->mailbox &&
148 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
155 /* This is the callback routine from mutt_menuLoop() which is used to generate
156 * a menu entry for the requested item number.
158 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
159 static void query_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
161 ENTRY *table = (ENTRY *) m->data;
162 char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
164 /* need a query format ... hard coded constants are not good */
165 while (FirstColumn + SecondColumn > 70) {
166 FirstColumn = FirstColumn * 3 / 4;
167 SecondColumn = SecondColumn * 3 / 4;
168 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
169 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
170 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
171 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
174 rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
176 mutt_format_string (buf2, sizeof (buf2),
177 FirstColumn + 2, FirstColumn + 2,
178 0, ' ', table[num].data->name,
179 m_strlen(table[num].data->name), 0);
181 snprintf (s, slen, " %c %3d %s %-*.*s %s",
182 table[num].tagged ? '*' : ' ',
186 SecondColumn + 2, buf, NONULL (table[num].data->other));
189 static int query_tag (MUTTMENU * menu, int n, int m)
191 ENTRY *cur = &((ENTRY *) menu->data)[n];
192 int ot = cur->tagged;
194 cur->tagged = m >= 0 ? m : !cur->tagged;
195 return cur->tagged - ot;
198 int mutt_query_complete (char *buf, ssize_t buflen)
200 QUERY *results = NULL;
204 mutt_error _("Query command not defined.");
209 results = run_query (buf, 1);
211 /* only one response? */
212 if (results->next == NULL) {
213 tmpa = result_to_addr (results);
214 mutt_addrlist_to_local (tmpa);
216 rfc822_write_address (buf, buflen, tmpa, 0);
217 address_list_wipe(&tmpa);
221 /* multiple results, choose from query menu */
222 query_menu (buf, buflen, results, 1);
227 void mutt_query_menu (char *buf, ssize_t buflen)
230 mutt_error _("Query command not defined.");
236 char buffer[STRING] = "";
238 query_menu (buffer, sizeof (buffer), NULL, 0);
241 query_menu (buf, buflen, NULL, 1);
245 static void query_menu (char *buf, ssize_t buflen, QUERY * results, int retbuf)
249 ENTRY *QueryTable = NULL;
250 QUERY *queryp = NULL;
253 char helpstr[SHORT_STRING];
256 snprintf (title, sizeof (title), _("Query")); /* FIXME */
258 menu = mutt_new_menu ();
259 menu->make_entry = query_entry;
260 menu->search = query_search;
261 menu->tag = query_tag;
262 menu->menu = MENU_QUERY;
265 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
267 if (results == NULL) {
268 /* Prompt for Query */
269 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
270 results = run_query (buf, 0);
275 snprintf (title, sizeof (title), _("Query '%s'"), buf);
277 /* count the number of results */
278 for (queryp = results; queryp; queryp = queryp->next)
281 menu->data = QueryTable = p_new(ENTRY, menu->max);
283 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
284 QueryTable[i].data = queryp;
287 switch ((op = mutt_menuLoop (menu))) {
288 case OP_QUERY_APPEND:
290 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
291 QUERY *newresults = NULL;
293 newresults = run_query (buf, 0);
295 menu->redraw = REDRAW_FULL;
297 snprintf (title, sizeof (title), _("Query '%s'"), buf);
299 if (op == OP_QUERY) {
302 address_list_wipe(&queryp->addr);
303 p_delete(&queryp->name);
304 p_delete(&queryp->other);
305 results = queryp->next;
309 results = newresults;
310 p_delete(&QueryTable);
314 for (queryp = results; queryp->next; queryp = queryp->next);
316 queryp->next = newresults;
321 mutt_menuDestroy (&menu);
322 menu = mutt_new_menu ();
323 menu->make_entry = query_entry;
324 menu->search = query_search;
325 menu->tag = query_tag;
326 menu->menu = MENU_QUERY;
329 mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
332 /* count the number of results */
333 for (queryp = results; queryp; queryp = queryp->next)
336 if (op == OP_QUERY) {
337 menu->data = QueryTable = p_new(ENTRY, menu->max);
339 for (i = 0, queryp = results; queryp;
340 queryp = queryp->next, i++)
341 QueryTable[i].data = queryp;
347 p_realloc(&QueryTable, menu->max);
349 menu->data = QueryTable;
351 for (i = 0, queryp = results; queryp;
352 queryp = queryp->next, i++) {
353 /* once we hit new entries, clear/init the tag */
354 if (queryp == newresults)
357 QueryTable[i].data = queryp;
359 QueryTable[i].tagged = 0;
366 case OP_CREATE_ALIAS:
367 if (menu->tagprefix) {
368 address_t *naddr = NULL;
370 for (i = 0; i < menu->max; i++)
371 if (QueryTable[i].tagged) {
372 address_list_append(&naddr, result_to_addr(QueryTable[i].data));
375 mutt_create_alias (NULL, naddr);
378 address_t *a = result_to_addr (QueryTable[menu->current].data);
380 mutt_create_alias (NULL, a);
381 address_list_wipe(&a);
385 case OP_GENERIC_SELECT_ENTRY:
390 /* fall through to OP_MAIL */
394 msg->env = envelope_new();
395 if (!menu->tagprefix) {
396 msg->env->to = result_to_addr (QueryTable[menu->current].data);
399 for (i = 0; i < menu->max; i++)
400 if (QueryTable[i].tagged) {
401 address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
404 ci_send_message (0, msg, NULL, Context, NULL);
405 menu->redraw = REDRAW_FULL;
414 /* if we need to return the selected entries */
415 if (retbuf && (done == 2)) {
419 p_clear(buf, buflen);
421 /* check for tagged entries */
422 for (i = 0; i < menu->max; i++) {
423 if (QueryTable[i].tagged) {
425 address_t *tmpa = result_to_addr (QueryTable[i].data);
427 mutt_addrlist_to_local (tmpa);
429 rfc822_write_address (buf, buflen, tmpa, 0);
430 curpos = m_strlen(buf);
431 address_list_wipe(&tmpa);
433 else if (curpos + 2 < buflen) {
434 address_t *tmpa = result_to_addr (QueryTable[i].data);
436 mutt_addrlist_to_local (tmpa);
437 strcat (buf, ", "); /* __STRCAT_CHECKED__ */
438 rfc822_write_address ((char *) buf + curpos + 1,
439 buflen - curpos - 1, tmpa, 0);
440 curpos = m_strlen(buf);
441 address_list_wipe(&tmpa);
445 /* then enter current message */
447 address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
449 mutt_addrlist_to_local (tmpa);
450 rfc822_write_address (buf, buflen, tmpa, 0);
451 address_list_wipe(&tmpa);
458 address_list_wipe(&queryp->addr);
459 p_delete(&queryp->name);
460 p_delete(&queryp->other);
461 results = queryp->next;
465 p_delete(&QueryTable);
467 /* tell whoever called me to redraw the screen when I return */
468 set_option (OPTNEEDREDRAW);
471 mutt_menuDestroy (&menu);