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-ui/lib-ui.h>
12 #include <lib-sys/unix.h>
14 #include <lib-ui/menu.h>
18 #include "mutt_idna.h"
21 typedef struct query {
28 typedef struct entry {
33 /* Variables for outsizing output format */
34 static int FirstColumn;
35 static int SecondColumn;
37 static void query_menu (char *buf, ssize_t buflen, QUERY * results,
40 static address_t *result_to_addr (QUERY * r)
42 static address_t *tmp;
44 tmp = address_list_dup (r->addr);
46 if (!tmp->next && !tmp->personal)
47 tmp->personal = m_strdup(r->name);
49 mutt_addrlist_to_idna (tmp, NULL);
53 static QUERY *run_query (char *s, int quiet)
58 char cmd[_POSIX_PATH_MAX];
68 m_quotefile_fmt(cmd, sizeof (cmd), QueryCmd, s);
70 if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
74 mutt_message _("Waiting for response...");
76 fgets (msg, sizeof (msg), fp);
77 if ((p = strrchr (msg, '\n')))
79 while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
80 if ((p = strtok (buf, "\t\n"))) {
84 first = p_new(QUERY, 1);
88 cur->next = p_new(QUERY, 1);
96 cur->addr = rfc822_parse_adrlist (cur->addr, p);
97 p = strtok (NULL, "\t\n");
102 cur->name = m_strdup(p);
103 p = strtok (NULL, "\t\n");
105 cur->other = m_strdup(p);
112 if (mutt_wait_filter (thepid)) {
114 mutt_error ("%s", msg);
118 mutt_message ("%s", msg);
124 static int query_search (MUTTMENU * m, regex_t * re, int n)
126 ENTRY *table = (ENTRY *) m->data;
128 if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
130 if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
132 if (table[n].data->addr) {
133 if (table[n].data->addr->personal &&
134 !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
136 if (table[n].data->addr->mailbox &&
137 !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
144 /* This is the callback routine from mutt_menuLoop() which is used to generate
145 * a menu entry for the requested item number.
147 #define QUERY_MIN_COLUMN_LENGHT 20 /* Must be < 70/2 */
148 static void query_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
150 ENTRY *table = (ENTRY *) m->data;
151 char buf2[STRING], buf[STRING] = "";
153 /* need a query format ... hard coded constants are not good */
154 while (FirstColumn + SecondColumn > 70) {
155 FirstColumn = FirstColumn * 3 / 4;
156 SecondColumn = SecondColumn * 3 / 4;
157 if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
158 FirstColumn = QUERY_MIN_COLUMN_LENGHT;
159 if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
160 SecondColumn = QUERY_MIN_COLUMN_LENGHT;
163 rfc822_addrcat(buf, sizeof (buf), table[num].data->addr, 1);
165 mutt_format_string (buf2, sizeof (buf2),
166 FirstColumn + 2, FirstColumn + 2,
167 0, ' ', table[num].data->name,
168 m_strlen(table[num].data->name), 0);
170 snprintf (s, slen, " %c %3d %s %-*.*s %s",
171 table[num].tagged ? '*' : ' ',
175 SecondColumn + 2, buf, NONULL (table[num].data->other));
178 static int query_tag (MUTTMENU * menu, int n, int m)
180 ENTRY *cur = &((ENTRY *) menu->data)[n];
181 int ot = cur->tagged;
183 cur->tagged = m >= 0 ? m : !cur->tagged;
184 return cur->tagged - ot;
187 int mutt_query_complete (char *buf, ssize_t buflen)
189 QUERY *results = NULL;
193 mutt_error _("Query command not defined.");
198 results = run_query (buf, 1);
200 /* only one response? */
201 if (results->next == NULL) {
202 tmpa = result_to_addr (results);
203 mutt_addrlist_to_local (tmpa);
205 rfc822_addrcat(buf, buflen, tmpa, 0);
206 address_list_wipe(&tmpa);
210 /* multiple results, choose from query menu */
211 query_menu (buf, buflen, results, 1);
216 void mutt_query_menu (char *buf, ssize_t buflen)
219 mutt_error _("Query command not defined.");
225 char buffer[STRING] = "";
227 query_menu (buffer, sizeof (buffer), NULL, 0);
230 query_menu (buf, buflen, NULL, 1);
234 static void query_menu (char *buf, ssize_t buflen, QUERY * results, int retbuf)
238 ENTRY *QueryTable = NULL;
239 QUERY *queryp = NULL;
244 snprintf (title, sizeof (title), _("Query")); /* FIXME */
246 menu = mutt_new_menu ();
247 menu->make_entry = query_entry;
248 menu->search = query_search;
249 menu->tag = query_tag;
250 menu->menu = MENU_QUERY;
253 if (results == NULL) {
254 /* Prompt for Query */
255 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
256 results = run_query (buf, 0);
261 snprintf (title, sizeof (title), _("Query '%s'"), buf);
263 /* count the number of results */
264 for (queryp = results; queryp; queryp = queryp->next)
267 menu->data = QueryTable = p_new(ENTRY, menu->max);
269 for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
270 QueryTable[i].data = queryp;
273 switch ((op = mutt_menuLoop (menu))) {
274 case OP_QUERY_APPEND:
276 if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
277 QUERY *newresults = NULL;
279 newresults = run_query (buf, 0);
281 menu->redraw = REDRAW_FULL;
283 snprintf (title, sizeof (title), _("Query '%s'"), buf);
285 if (op == OP_QUERY) {
288 address_list_wipe(&queryp->addr);
289 p_delete(&queryp->name);
290 p_delete(&queryp->other);
291 results = queryp->next;
295 results = newresults;
296 p_delete(&QueryTable);
300 for (queryp = results; queryp->next; queryp = queryp->next);
302 queryp->next = newresults;
307 mutt_menuDestroy (&menu);
308 menu = mutt_new_menu ();
309 menu->make_entry = query_entry;
310 menu->search = query_search;
311 menu->tag = query_tag;
312 menu->menu = MENU_QUERY;
315 /* count the number of results */
316 for (queryp = results; queryp; queryp = queryp->next)
319 if (op == OP_QUERY) {
320 menu->data = QueryTable = p_new(ENTRY, menu->max);
322 for (i = 0, queryp = results; queryp;
323 queryp = queryp->next, i++)
324 QueryTable[i].data = queryp;
330 p_realloc(&QueryTable, menu->max);
332 menu->data = QueryTable;
334 for (i = 0, queryp = results; queryp;
335 queryp = queryp->next, i++) {
336 /* once we hit new entries, clear/init the tag */
337 if (queryp == newresults)
340 QueryTable[i].data = queryp;
342 QueryTable[i].tagged = 0;
349 case OP_CREATE_ALIAS:
350 if (menu->tagprefix) {
351 address_t *naddr = NULL;
353 for (i = 0; i < menu->max; i++)
354 if (QueryTable[i].tagged) {
355 address_list_append(&naddr, result_to_addr(QueryTable[i].data));
358 mutt_create_alias (NULL, naddr);
361 address_t *a = result_to_addr (QueryTable[menu->current].data);
363 mutt_create_alias (NULL, a);
364 address_list_wipe(&a);
368 case OP_GENERIC_SELECT_ENTRY:
373 /* fall through to OP_MAIL */
377 msg->env = envelope_new();
378 if (!menu->tagprefix) {
379 msg->env->to = result_to_addr (QueryTable[menu->current].data);
382 for (i = 0; i < menu->max; i++)
383 if (QueryTable[i].tagged) {
384 address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
387 ci_send_message (0, msg, NULL, Context, NULL);
388 menu->redraw = REDRAW_FULL;
397 /* if we need to return the selected entries */
398 if (retbuf && (done == 2)) {
402 p_clear(buf, buflen);
404 /* check for tagged entries */
405 for (i = 0; i < menu->max; i++) {
406 if (QueryTable[i].tagged) {
408 address_t *tmpa = result_to_addr (QueryTable[i].data);
410 mutt_addrlist_to_local (tmpa);
412 rfc822_addrcat(buf, buflen, tmpa, 0);
413 curpos = m_strlen(buf);
414 address_list_wipe(&tmpa);
416 else if (curpos + 2 < buflen) {
417 address_t *tmpa = result_to_addr (QueryTable[i].data);
419 mutt_addrlist_to_local (tmpa);
420 m_strcat(buf, buflen, ", ");
421 rfc822_addrcat(buf + curpos + 1, buflen - curpos - 1, tmpa, 0);
422 curpos = m_strlen(buf);
423 address_list_wipe(&tmpa);
427 /* then enter current message */
429 address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
431 mutt_addrlist_to_local (tmpa);
432 rfc822_addrcat(buf, buflen, tmpa, 0);
433 address_list_wipe(&tmpa);
440 address_list_wipe(&queryp->addr);
441 p_delete(&queryp->name);
442 p_delete(&queryp->other);
443 results = queryp->next;
447 p_delete(&QueryTable);
449 /* tell whoever called me to redraw the screen when I return */
450 set_option (OPTNEEDREDRAW);
453 mutt_menuDestroy (&menu);