rationnalize includes a lot:
[apps/madmutt.git] / lib-ui / 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 #include <lib-lib/lib-lib.h>
11
12 #include <lib-sys/unix.h>
13
14 #include <lib-ui/menu.h>
15 #include <lib-ui/curses.h>
16
17 #include "mutt.h"
18 #include "alias.h"
19 #include "mutt_idna.h"
20 #include "sort.h"
21
22 typedef struct query {
23   address_t *addr;
24   char *name;
25   char *other;
26   struct query *next;
27 } QUERY;
28
29 typedef struct entry {
30   int tagged;
31   QUERY *data;
32 } ENTRY;
33
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},
41   {NULL, OP_NULL}
42 };
43
44 /* Variables for outsizing output format */
45 static int FirstColumn;
46 static int SecondColumn;
47
48 static void query_menu (char *buf, ssize_t buflen, QUERY * results,
49                         int retbuf);
50
51 static address_t *result_to_addr (QUERY * r)
52 {
53   static address_t *tmp;
54
55   tmp = address_list_dup (r->addr);
56
57   if (!tmp->next && !tmp->personal)
58     tmp->personal = m_strdup(r->name);
59
60   mutt_addrlist_to_idna (tmp, NULL);
61   return tmp;
62 }
63
64 static QUERY *run_query (char *s, int quiet)
65 {
66   FILE *fp;
67   QUERY *first = NULL;
68   QUERY *cur = NULL;
69   char cmd[_POSIX_PATH_MAX];
70   char *buf = NULL;
71   ssize_t buflen;
72   int dummy = 0;
73   char msg[STRING];
74   char *p;
75   pid_t thepid;
76   int l;
77
78
79   mutt_expand_file_fmt (cmd, sizeof (cmd), QueryCmd, s);
80
81   if ((thepid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0) {
82     return 0;
83   }
84   if (!quiet)
85     mutt_message _("Waiting for response...");
86
87   fgets (msg, sizeof (msg), fp);
88   if ((p = strrchr (msg, '\n')))
89     *p = '\0';
90   while ((buf = mutt_read_line (buf, &buflen, fp, &dummy)) != NULL) {
91     if ((p = strtok (buf, "\t\n"))) {
92       if (first == NULL) {
93         FirstColumn = 0;
94         SecondColumn = 0;
95         first = p_new(QUERY, 1);
96         cur = first;
97       }
98       else {
99         cur->next = p_new(QUERY, 1);
100         cur = cur->next;
101       }
102
103       l = mutt_strwidth (p);
104       if (l > SecondColumn)
105         SecondColumn = l;
106
107       cur->addr = rfc822_parse_adrlist (cur->addr, p);
108       p = strtok (NULL, "\t\n");
109       if (p) {
110         l = mutt_strwidth (p);
111         if (l > FirstColumn)
112           FirstColumn = l;
113         cur->name = m_strdup(p);
114         p = strtok (NULL, "\t\n");
115         if (p) {
116           cur->other = m_strdup(p);
117         }
118       }
119     }
120   }
121   p_delete(&buf);
122   fclose (fp);
123   if (mutt_wait_filter (thepid)) {
124     if (!quiet)
125       mutt_error ("%s", msg);
126   }
127   else {
128     if (!quiet)
129       mutt_message ("%s", msg);
130   }
131
132   return first;
133 }
134
135 static int query_search (MUTTMENU * m, regex_t * re, int n)
136 {
137   ENTRY *table = (ENTRY *) m->data;
138
139   if (table[n].data->name && !regexec (re, table[n].data->name, 0, NULL, 0))
140     return 0;
141   if (table[n].data->other && !regexec (re, table[n].data->other, 0, NULL, 0))
142     return 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))
146       return 0;
147     if (table[n].data->addr->mailbox &&
148         !regexec (re, table[n].data->addr->mailbox, 0, NULL, 0))
149       return 0;
150   }
151
152   return REG_NOMATCH;
153 }
154
155 /* This is the callback routine from mutt_menuLoop() which is used to generate
156  * a menu entry for the requested item number.
157  */
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)
160 {
161   ENTRY *table = (ENTRY *) m->data;
162   char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
163
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;
172   }
173
174   rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
175
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);
180
181   snprintf (s, slen, " %c %3d %s %-*.*s %s",
182             table[num].tagged ? '*' : ' ',
183             num + 1,
184             buf2,
185             SecondColumn + 2,
186             SecondColumn + 2, buf, NONULL (table[num].data->other));
187 }
188
189 static int query_tag (MUTTMENU * menu, int n, int m)
190 {
191   ENTRY *cur = &((ENTRY *) menu->data)[n];
192   int ot = cur->tagged;
193
194   cur->tagged = m >= 0 ? m : !cur->tagged;
195   return cur->tagged - ot;
196 }
197
198 int mutt_query_complete (char *buf, ssize_t buflen)
199 {
200   QUERY *results = NULL;
201   address_t *tmpa;
202
203   if (!QueryCmd) {
204     mutt_error _("Query command not defined.");
205
206     return 0;
207   }
208
209   results = run_query (buf, 1);
210   if (results) {
211     /* only one response? */
212     if (results->next == NULL) {
213       tmpa = result_to_addr (results);
214       mutt_addrlist_to_local (tmpa);
215       buf[0] = '\0';
216       rfc822_write_address (buf, buflen, tmpa, 0);
217       address_list_wipe(&tmpa);
218       mutt_clear_error ();
219       return (0);
220     }
221     /* multiple results, choose from query menu */
222     query_menu (buf, buflen, results, 1);
223   }
224   return (0);
225 }
226
227 void mutt_query_menu (char *buf, ssize_t buflen)
228 {
229   if (!QueryCmd) {
230     mutt_error _("Query command not defined.");
231
232     return;
233   }
234
235   if (buf == NULL) {
236     char buffer[STRING] = "";
237
238     query_menu (buffer, sizeof (buffer), NULL, 0);
239   }
240   else {
241     query_menu (buf, buflen, NULL, 1);
242   }
243 }
244
245 static void query_menu (char *buf, ssize_t buflen, QUERY * results, int retbuf)
246 {
247   MUTTMENU *menu;
248   HEADER *msg = NULL;
249   ENTRY *QueryTable = NULL;
250   QUERY *queryp = NULL;
251   int i, done = 0;
252   int op;
253   char helpstr[SHORT_STRING];
254   char title[STRING];
255
256   snprintf (title, sizeof (title), _("Query")); /* FIXME */
257
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;
263   menu->title = title;
264   menu->help =
265     mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
266
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);
271     }
272   }
273
274   if (results) {
275     snprintf (title, sizeof (title), _("Query '%s'"), buf);
276
277     /* count the number of results */
278     for (queryp = results; queryp; queryp = queryp->next)
279       menu->max++;
280
281     menu->data = QueryTable = p_new(ENTRY, menu->max);
282
283     for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
284       QueryTable[i].data = queryp;
285
286     while (!done) {
287       switch ((op = mutt_menuLoop (menu))) {
288       case OP_QUERY_APPEND:
289       case OP_QUERY:
290         if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
291           QUERY *newresults = NULL;
292
293           newresults = run_query (buf, 0);
294
295           menu->redraw = REDRAW_FULL;
296           if (newresults) {
297             snprintf (title, sizeof (title), _("Query '%s'"), buf);
298
299             if (op == OP_QUERY) {
300               queryp = results;
301               while (queryp) {
302                 address_list_wipe(&queryp->addr);
303                 p_delete(&queryp->name);
304                 p_delete(&queryp->other);
305                 results = queryp->next;
306                 p_delete(&queryp);
307                 queryp = results;
308               }
309               results = newresults;
310               p_delete(&QueryTable);
311             }
312             else {
313               /* append */
314               for (queryp = results; queryp->next; queryp = queryp->next);
315
316               queryp->next = newresults;
317             }
318
319
320             menu->current = 0;
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;
327             menu->title = title;
328             menu->help =
329               mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
330                                  QueryHelp);
331
332             /* count the number of results */
333             for (queryp = results; queryp; queryp = queryp->next)
334               menu->max++;
335
336             if (op == OP_QUERY) {
337               menu->data = QueryTable = p_new(ENTRY, menu->max);
338
339               for (i = 0, queryp = results; queryp;
340                    queryp = queryp->next, i++)
341                 QueryTable[i].data = queryp;
342             }
343             else {
344               int doclear = 0;
345
346               /* append */
347               p_realloc(&QueryTable, menu->max);
348
349               menu->data = QueryTable;
350
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)
355                   doclear = 1;
356
357                 QueryTable[i].data = queryp;
358                 if (doclear)
359                   QueryTable[i].tagged = 0;
360               }
361             }
362           }
363         }
364         break;
365
366       case OP_CREATE_ALIAS:
367         if (menu->tagprefix) {
368           address_t *naddr = NULL;
369
370           for (i = 0; i < menu->max; i++)
371             if (QueryTable[i].tagged) {
372               address_list_append(&naddr, result_to_addr(QueryTable[i].data));
373             }
374
375           mutt_create_alias (NULL, naddr);
376         }
377         else {
378           address_t *a = result_to_addr (QueryTable[menu->current].data);
379
380           mutt_create_alias (NULL, a);
381           address_list_wipe(&a);
382         }
383         break;
384
385       case OP_GENERIC_SELECT_ENTRY:
386         if (retbuf) {
387           done = 2;
388           break;
389         }
390         /* fall through to OP_MAIL */
391
392       case OP_MAIL:
393         msg = header_new();
394         msg->env = envelope_new();
395         if (!menu->tagprefix) {
396           msg->env->to = result_to_addr (QueryTable[menu->current].data);
397         }
398         else {
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));
402             }
403         }
404         ci_send_message (0, msg, NULL, Context, NULL);
405         menu->redraw = REDRAW_FULL;
406         break;
407
408       case OP_EXIT:
409         done = 1;
410         break;
411       }
412     }
413
414     /* if we need to return the selected entries */
415     if (retbuf && (done == 2)) {
416       int tagged = 0;
417       ssize_t curpos = 0;
418
419       p_clear(buf, buflen);
420
421       /* check for tagged entries */
422       for (i = 0; i < menu->max; i++) {
423         if (QueryTable[i].tagged) {
424           if (curpos == 0) {
425             address_t *tmpa = result_to_addr (QueryTable[i].data);
426
427             mutt_addrlist_to_local (tmpa);
428             tagged = 1;
429             rfc822_write_address (buf, buflen, tmpa, 0);
430             curpos = m_strlen(buf);
431             address_list_wipe(&tmpa);
432           }
433           else if (curpos + 2 < buflen) {
434             address_t *tmpa = result_to_addr (QueryTable[i].data);
435
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);
442           }
443         }
444       }
445       /* then enter current message */
446       if (!tagged) {
447         address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
448
449         mutt_addrlist_to_local (tmpa);
450         rfc822_write_address (buf, buflen, tmpa, 0);
451         address_list_wipe(&tmpa);
452       }
453
454     }
455
456     queryp = results;
457     while (queryp) {
458       address_list_wipe(&queryp->addr);
459       p_delete(&queryp->name);
460       p_delete(&queryp->other);
461       results = queryp->next;
462       p_delete(&queryp);
463       queryp = results;
464     }
465     p_delete(&QueryTable);
466
467     /* tell whoever called me to redraw the screen when I return */
468     set_option (OPTNEEDREDRAW);
469   }
470
471   mutt_menuDestroy (&menu);
472 }