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