893d2db5e5db2a7583544df903079cbdba346365
[apps/madmutt.git] / query.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <lib-lib/mem.h>
15
16 #include "mutt.h"
17 #include "mutt_menu.h"
18 #include "mutt_idna.h"
19 #include "mapping.h"
20 #include "sort.h"
21
22 #include "lib/mem.h"
23 #include "lib/intl.h"
24 #include "lib/str.h"
25 #include "lib/debug.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 typedef struct query {
32   ADDRESS *addr;
33   char *name;
34   char *other;
35   struct query *next;
36 } QUERY;
37
38 typedef struct entry {
39   int tagged;
40   QUERY *data;
41 } ENTRY;
42
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},
50   {NULL, OP_NULL}
51 };
52
53 /* Variables for outsizing output format */
54 static int FirstColumn;
55 static int SecondColumn;
56
57 static void query_menu (char *buf, size_t buflen, QUERY * results,
58                         int retbuf);
59
60 static ADDRESS *result_to_addr (QUERY * r)
61 {
62   static ADDRESS *tmp;
63
64   tmp = rfc822_cpy_adr (r->addr);
65
66   if (!tmp->next && !tmp->personal)
67     tmp->personal = str_dup (r->name);
68
69   mutt_addrlist_to_idna (tmp, NULL);
70   return tmp;
71 }
72
73 static QUERY *run_query (char *s, int quiet)
74 {
75   FILE *fp;
76   QUERY *first = NULL;
77   QUERY *cur = NULL;
78   char cmd[_POSIX_PATH_MAX];
79   char *buf = NULL;
80   size_t buflen;
81   int dummy = 0;
82   char msg[STRING];
83   char *p;
84   pid_t thepid;
85   int l;
86
87
88   mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
89
90   if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
91     debug_print (1, ("unable to fork command: %s\n", cmd));
92     return 0;
93   }
94   if (!quiet)
95     mutt_message _("Waiting for response...");
96
97   fgets (msg, sizeof (msg), fp);
98   if ((p = strrchr (msg, '\n')))
99     *p = '\0';
100   while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
101     if ((p = strtok (buf, "\t\n"))) {
102       if (first == NULL) {
103         FirstColumn = 0;
104         SecondColumn = 0;
105         first = (QUERY *) mem_calloc (1, sizeof (QUERY));
106         cur = first;
107       }
108       else {
109         cur->next = (QUERY *) mem_calloc (1, sizeof (QUERY));
110         cur = cur->next;
111       }
112
113       l = mutt_strwidth (p);
114       if (l > SecondColumn)
115         SecondColumn = l;
116
117       cur->addr = rfc822_parse_adrlist (cur->addr, p);
118       p = strtok (NULL, "\t\n");
119       if (p) {
120         l = mutt_strwidth (p);
121         if (l > FirstColumn)
122           FirstColumn = l;
123         cur->name = str_dup (p);
124         p = strtok (NULL, "\t\n");
125         if (p) {
126           cur->other = str_dup (p);
127         }
128       }
129     }
130   }
131   p_delete(&buf);
132   fclose (fp);
133   if (mutt_wait_filter (thepid)) {
134     debug_print (1, ("Error: %s\n", msg));
135     if (!quiet)
136       mutt_error ("%s", msg);
137   }
138   else {
139     if (!quiet)
140       mutt_message ("%s", msg);
141   }
142
143   return first;
144 }
145
146 static int query_search (MUTTMENU * m, regex_t * re, int n)
147 {
148   ENTRY *table = (ENTRY *) m->data;
149
150   if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
151     return 0;
152   if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
153     return 0;
154   if (table[n].data->addr) {
155     if (table[n].data->addr->personal &&
156         !regexec (re, table[n].data->addr->personal, 0, NULL, 0))
157       return 0;
158     if (table[n].data->addr->mailbox &&
159         !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
160       return 0;
161   }
162
163   return REG_NOMATCH;
164 }
165
166 /* This is the callback routine from mutt_menuLoop() which is used to generate
167  * a menu entry for the requested item number.
168  */
169 #define QUERY_MIN_COLUMN_LENGHT 20      /* Must be < 70/2 */
170 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
171 {
172   ENTRY *table = (ENTRY *) m->data;
173   char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
174
175   /* need a query format ... hard coded constants are not good */
176   while (FirstColumn + SecondColumn > 70) {
177     FirstColumn = FirstColumn * 3 / 4;
178     SecondColumn = SecondColumn * 3 / 4;
179     if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
180       FirstColumn = QUERY_MIN_COLUMN_LENGHT;
181     if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
182       SecondColumn = QUERY_MIN_COLUMN_LENGHT;
183   }
184
185   rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
186
187   mutt_format_string (buf2, sizeof (buf2),
188                       FirstColumn + 2, FirstColumn + 2,
189                       0, ' ', table[num].data->name,
190                       str_len (table[num].data->name), 0);
191
192   snprintf (s, slen, " %c %3d %s %-*.*s %s",
193             table[num].tagged ? '*' : ' ',
194             num + 1,
195             buf2,
196             SecondColumn + 2,
197             SecondColumn + 2, buf, NONULL (table[num].data->other));
198 }
199
200 static int query_tag (MUTTMENU * menu, int n, int m)
201 {
202   ENTRY *cur = &((ENTRY *) menu->data)[n];
203   int ot = cur->tagged;
204
205   cur->tagged = m >= 0 ? m : !cur->tagged;
206   return cur->tagged - ot;
207 }
208
209 int mutt_query_complete (char *buf, size_t buflen)
210 {
211   QUERY *results = NULL;
212   ADDRESS *tmpa;
213
214   if (!QueryCmd) {
215     mutt_error _("Query command not defined.");
216
217     return 0;
218   }
219
220   results = run_query (buf, 1);
221   if (results) {
222     /* only one response? */
223     if (results->next == NULL) {
224       tmpa = result_to_addr (results);
225       mutt_addrlist_to_local (tmpa);
226       buf[0] = '\0';
227       rfc822_write_address (buf, buflen, tmpa, 0);
228       rfc822_free_address (&tmpa);
229       mutt_clear_error ();
230       return (0);
231     }
232     /* multiple results, choose from query menu */
233     query_menu (buf, buflen, results, 1);
234   }
235   return (0);
236 }
237
238 void mutt_query_menu (char *buf, size_t buflen)
239 {
240   if (!QueryCmd) {
241     mutt_error _("Query command not defined.");
242
243     return;
244   }
245
246   if (buf == NULL) {
247     char buffer[STRING] = "";
248
249     query_menu (buffer, sizeof (buffer), NULL, 0);
250   }
251   else {
252     query_menu (buf, buflen, NULL, 1);
253   }
254 }
255
256 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
257 {
258   MUTTMENU *menu;
259   HEADER *msg = NULL;
260   ENTRY *QueryTable = NULL;
261   QUERY *queryp = NULL;
262   int i, done = 0;
263   int op;
264   char helpstr[SHORT_STRING];
265   char title[STRING];
266
267   snprintf (title, sizeof (title), _("Query")); /* FIXME */
268
269   menu = mutt_new_menu ();
270   menu->make_entry = query_entry;
271   menu->search = query_search;
272   menu->tag = query_tag;
273   menu->menu = MENU_QUERY;
274   menu->title = title;
275   menu->help =
276     mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
277
278   if (results == NULL) {
279     /* Prompt for Query */
280     if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
281       results = run_query (buf, 0);
282     }
283   }
284
285   if (results) {
286     snprintf (title, sizeof (title), _("Query '%s'"), buf);
287
288     /* count the number of results */
289     for (queryp = results; queryp; queryp = queryp->next)
290       menu->max++;
291
292     menu->data = QueryTable =
293       (ENTRY *) mem_calloc (menu->max, sizeof (ENTRY));
294
295     for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
296       QueryTable[i].data = queryp;
297
298     while (!done) {
299       switch ((op = mutt_menuLoop (menu))) {
300       case OP_QUERY_APPEND:
301       case OP_QUERY:
302         if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
303           QUERY *newresults = NULL;
304
305           newresults = run_query (buf, 0);
306
307           menu->redraw = REDRAW_FULL;
308           if (newresults) {
309             snprintf (title, sizeof (title), _("Query '%s'"), buf);
310
311             if (op == OP_QUERY) {
312               queryp = results;
313               while (queryp) {
314                 rfc822_free_address (&queryp->addr);
315                 p_delete(&queryp->name);
316                 p_delete(&queryp->other);
317                 results = queryp->next;
318                 p_delete(&queryp);
319                 queryp = results;
320               }
321               results = newresults;
322               p_delete(&QueryTable);
323             }
324             else {
325               /* append */
326               for (queryp = results; queryp->next; queryp = queryp->next);
327
328               queryp->next = newresults;
329             }
330
331
332             menu->current = 0;
333             mutt_menuDestroy (&menu);
334             menu = mutt_new_menu ();
335             menu->make_entry = query_entry;
336             menu->search = query_search;
337             menu->tag = query_tag;
338             menu->menu = MENU_QUERY;
339             menu->title = title;
340             menu->help =
341               mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
342                                  QueryHelp);
343
344             /* count the number of results */
345             for (queryp = results; queryp; queryp = queryp->next)
346               menu->max++;
347
348             if (op == OP_QUERY) {
349               menu->data = QueryTable =
350                 (ENTRY *) mem_calloc (menu->max, sizeof (ENTRY));
351
352               for (i = 0, queryp = results; queryp;
353                    queryp = queryp->next, i++)
354                 QueryTable[i].data = queryp;
355             }
356             else {
357               int clear = 0;
358
359               /* append */
360               mem_realloc (&QueryTable, menu->max * sizeof (ENTRY));
361
362               menu->data = QueryTable;
363
364               for (i = 0, queryp = results; queryp;
365                    queryp = queryp->next, i++) {
366                 /* once we hit new entries, clear/init the tag */
367                 if (queryp == newresults)
368                   clear = 1;
369
370                 QueryTable[i].data = queryp;
371                 if (clear)
372                   QueryTable[i].tagged = 0;
373               }
374             }
375           }
376         }
377         break;
378
379       case OP_CREATE_ALIAS:
380         if (menu->tagprefix) {
381           ADDRESS *naddr = NULL;
382
383           for (i = 0; i < menu->max; i++)
384             if (QueryTable[i].tagged) {
385               ADDRESS *a = result_to_addr (QueryTable[i].data);
386
387               rfc822_append (&naddr, a);
388               rfc822_free_address (&a);
389             }
390
391           mutt_create_alias (NULL, naddr);
392         }
393         else {
394           ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
395
396           mutt_create_alias (NULL, a);
397           rfc822_free_address (&a);
398         }
399         break;
400
401       case OP_GENERIC_SELECT_ENTRY:
402         if (retbuf) {
403           done = 2;
404           break;
405         }
406         /* fall through to OP_MAIL */
407
408       case OP_MAIL:
409         msg = mutt_new_header ();
410         msg->env = mutt_new_envelope ();
411         if (!menu->tagprefix) {
412           msg->env->to = result_to_addr (QueryTable[menu->current].data);
413         }
414         else {
415           for (i = 0; i < menu->max; i++)
416             if (QueryTable[i].tagged) {
417               ADDRESS *a = result_to_addr (QueryTable[i].data);
418
419               rfc822_append (&msg->env->to, a);
420               rfc822_free_address (&a);
421             }
422         }
423         ci_send_message (0, msg, NULL, Context, NULL);
424         menu->redraw = REDRAW_FULL;
425         break;
426
427       case OP_EXIT:
428         done = 1;
429         break;
430       }
431     }
432
433     /* if we need to return the selected entries */
434     if (retbuf && (done == 2)) {
435       int tagged = 0;
436       size_t curpos = 0;
437
438       memset (buf, 0, buflen);
439
440       /* check for tagged entries */
441       for (i = 0; i < menu->max; i++) {
442         if (QueryTable[i].tagged) {
443           if (curpos == 0) {
444             ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
445
446             mutt_addrlist_to_local (tmpa);
447             tagged = 1;
448             rfc822_write_address (buf, buflen, tmpa, 0);
449             curpos = str_len (buf);
450             rfc822_free_address (&tmpa);
451           }
452           else if (curpos + 2 < buflen) {
453             ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
454
455             mutt_addrlist_to_local (tmpa);
456             strcat (buf, ", "); /* __STRCAT_CHECKED__ */
457             rfc822_write_address ((char *) buf + curpos + 1,
458                                   buflen - curpos - 1, tmpa, 0);
459             curpos = str_len (buf);
460             rfc822_free_address (&tmpa);
461           }
462         }
463       }
464       /* then enter current message */
465       if (!tagged) {
466         ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
467
468         mutt_addrlist_to_local (tmpa);
469         rfc822_write_address (buf, buflen, tmpa, 0);
470         rfc822_free_address (&tmpa);
471       }
472
473     }
474
475     queryp = results;
476     while (queryp) {
477       rfc822_free_address (&queryp->addr);
478       p_delete(&queryp->name);
479       p_delete(&queryp->other);
480       results = queryp->next;
481       p_delete(&queryp);
482       queryp = results;
483     }
484     p_delete(&QueryTable);
485
486     /* tell whoever called me to redraw the screen when I return */
487     set_option (OPTNEEDREDRAW);
488   }
489
490   mutt_menuDestroy (&menu);
491 }