use gperf for the charsets as well.
[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 <string.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17
18 #include <lib-lib/mem.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/macros.h>
21 #include <lib-lib/file.h>
22 #include <lib-lib/mapping.h>
23
24 #include <lib-ui/menu.h>
25
26 #include "mutt.h"
27 #include "alias.h"
28 #include "mutt_idna.h"
29 #include "sort.h"
30
31 typedef struct query {
32   address_t *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, ssize_t buflen, QUERY * results,
58                         int retbuf);
59
60 static address_t *result_to_addr (QUERY * r)
61 {
62   static address_t *tmp;
63
64   tmp = address_list_dup (r->addr);
65
66   if (!tmp->next && !tmp->personal)
67     tmp->personal = m_strdup(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   ssize_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     return 0;
92   }
93   if (!quiet)
94     mutt_message _("Waiting for response...");
95
96   fgets (msg, sizeof (msg), fp);
97   if ((p = strrchr (msg, '\n')))
98     *p = '\0';
99   while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
100     if ((p = strtok (buf, "\t\n"))) {
101       if (first == NULL) {
102         FirstColumn = 0;
103         SecondColumn = 0;
104         first = p_new(QUERY, 1);
105         cur = first;
106       }
107       else {
108         cur->next = p_new(QUERY, 1);
109         cur = cur->next;
110       }
111
112       l = mutt_strwidth (p);
113       if (l > SecondColumn)
114         SecondColumn = l;
115
116       cur->addr = rfc822_parse_adrlist (cur->addr, p);
117       p = strtok (NULL, "\t\n");
118       if (p) {
119         l = mutt_strwidth (p);
120         if (l > FirstColumn)
121           FirstColumn = l;
122         cur->name = m_strdup(p);
123         p = strtok (NULL, "\t\n");
124         if (p) {
125           cur->other = m_strdup(p);
126         }
127       }
128     }
129   }
130   p_delete(&buf);
131   fclose (fp);
132   if (mutt_wait_filter (thepid)) {
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, ssize_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                       m_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, ssize_t buflen)
208 {
209   QUERY *results = NULL;
210   address_t *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       address_list_wipe(&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, ssize_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, ssize_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 = p_new(ENTRY, menu->max);
291
292     for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
293       QueryTable[i].data = queryp;
294
295     while (!done) {
296       switch ((op = mutt_menuLoop (menu))) {
297       case OP_QUERY_APPEND:
298       case OP_QUERY:
299         if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
300           QUERY *newresults = NULL;
301
302           newresults = run_query (buf, 0);
303
304           menu->redraw = REDRAW_FULL;
305           if (newresults) {
306             snprintf (title, sizeof (title), _("Query '%s'"), buf);
307
308             if (op == OP_QUERY) {
309               queryp = results;
310               while (queryp) {
311                 address_list_wipe(&queryp->addr);
312                 p_delete(&queryp->name);
313                 p_delete(&queryp->other);
314                 results = queryp->next;
315                 p_delete(&queryp);
316                 queryp = results;
317               }
318               results = newresults;
319               p_delete(&QueryTable);
320             }
321             else {
322               /* append */
323               for (queryp = results; queryp->next; queryp = queryp->next);
324
325               queryp->next = newresults;
326             }
327
328
329             menu->current = 0;
330             mutt_menuDestroy (&menu);
331             menu = mutt_new_menu ();
332             menu->make_entry = query_entry;
333             menu->search = query_search;
334             menu->tag = query_tag;
335             menu->menu = MENU_QUERY;
336             menu->title = title;
337             menu->help =
338               mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
339                                  QueryHelp);
340
341             /* count the number of results */
342             for (queryp = results; queryp; queryp = queryp->next)
343               menu->max++;
344
345             if (op == OP_QUERY) {
346               menu->data = QueryTable = p_new(ENTRY, menu->max);
347
348               for (i = 0, queryp = results; queryp;
349                    queryp = queryp->next, i++)
350                 QueryTable[i].data = queryp;
351             }
352             else {
353               int clear = 0;
354
355               /* append */
356               p_realloc(&QueryTable, menu->max);
357
358               menu->data = QueryTable;
359
360               for (i = 0, queryp = results; queryp;
361                    queryp = queryp->next, i++) {
362                 /* once we hit new entries, clear/init the tag */
363                 if (queryp == newresults)
364                   clear = 1;
365
366                 QueryTable[i].data = queryp;
367                 if (clear)
368                   QueryTable[i].tagged = 0;
369               }
370             }
371           }
372         }
373         break;
374
375       case OP_CREATE_ALIAS:
376         if (menu->tagprefix) {
377           address_t *naddr = NULL;
378
379           for (i = 0; i < menu->max; i++)
380             if (QueryTable[i].tagged) {
381               address_list_append(&naddr, result_to_addr(QueryTable[i].data));
382             }
383
384           mutt_create_alias (NULL, naddr);
385         }
386         else {
387           address_t *a = result_to_addr (QueryTable[menu->current].data);
388
389           mutt_create_alias (NULL, a);
390           address_list_wipe(&a);
391         }
392         break;
393
394       case OP_GENERIC_SELECT_ENTRY:
395         if (retbuf) {
396           done = 2;
397           break;
398         }
399         /* fall through to OP_MAIL */
400
401       case OP_MAIL:
402         msg = header_new();
403         msg->env = envelope_new();
404         if (!menu->tagprefix) {
405           msg->env->to = result_to_addr (QueryTable[menu->current].data);
406         }
407         else {
408           for (i = 0; i < menu->max; i++)
409             if (QueryTable[i].tagged) {
410               address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
411             }
412         }
413         ci_send_message (0, msg, NULL, Context, NULL);
414         menu->redraw = REDRAW_FULL;
415         break;
416
417       case OP_EXIT:
418         done = 1;
419         break;
420       }
421     }
422
423     /* if we need to return the selected entries */
424     if (retbuf && (done == 2)) {
425       int tagged = 0;
426       ssize_t curpos = 0;
427
428       p_clear(buf, buflen);
429
430       /* check for tagged entries */
431       for (i = 0; i < menu->max; i++) {
432         if (QueryTable[i].tagged) {
433           if (curpos == 0) {
434             address_t *tmpa = result_to_addr (QueryTable[i].data);
435
436             mutt_addrlist_to_local (tmpa);
437             tagged = 1;
438             rfc822_write_address (buf, buflen, tmpa, 0);
439             curpos = m_strlen(buf);
440             address_list_wipe(&tmpa);
441           }
442           else if (curpos + 2 < buflen) {
443             address_t *tmpa = result_to_addr (QueryTable[i].data);
444
445             mutt_addrlist_to_local (tmpa);
446             strcat (buf, ", "); /* __STRCAT_CHECKED__ */
447             rfc822_write_address ((char *) buf + curpos + 1,
448                                   buflen - curpos - 1, tmpa, 0);
449             curpos = m_strlen(buf);
450             address_list_wipe(&tmpa);
451           }
452         }
453       }
454       /* then enter current message */
455       if (!tagged) {
456         address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
457
458         mutt_addrlist_to_local (tmpa);
459         rfc822_write_address (buf, buflen, tmpa, 0);
460         address_list_wipe(&tmpa);
461       }
462
463     }
464
465     queryp = results;
466     while (queryp) {
467       address_list_wipe(&queryp->addr);
468       p_delete(&queryp->name);
469       p_delete(&queryp->other);
470       results = queryp->next;
471       p_delete(&queryp);
472       queryp = results;
473     }
474     p_delete(&QueryTable);
475
476     /* tell whoever called me to redraw the screen when I return */
477     set_option (OPTNEEDREDRAW);
478   }
479
480   mutt_menuDestroy (&menu);
481 }