Rocco Rutte:
[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 #ifdef EXACT_ADDRESS
160     if (table[n].data->addr->val &&
161         !regexec (re, table[n].data->addr->val, 0, NULL, 0))
162       return 0;
163 #endif
164   }
165
166   return REG_NOMATCH;
167 }
168
169 /* This is the callback routine from mutt_menuLoop() which is used to generate
170  * a menu entry for the requested item number.
171  */
172 #define QUERY_MIN_COLUMN_LENGHT 20      /* Must be < 70/2 */
173 static void query_entry (char *s, size_t slen, MUTTMENU * m, int num)
174 {
175   ENTRY *table = (ENTRY *) m->data;
176   char buf2[SHORT_STRING], buf[SHORT_STRING] = "";
177
178   /* need a query format ... hard coded constants are not good */
179   while (FirstColumn + SecondColumn > 70) {
180     FirstColumn = FirstColumn * 3 / 4;
181     SecondColumn = SecondColumn * 3 / 4;
182     if (FirstColumn < QUERY_MIN_COLUMN_LENGHT)
183       FirstColumn = QUERY_MIN_COLUMN_LENGHT;
184     if (SecondColumn < QUERY_MIN_COLUMN_LENGHT)
185       SecondColumn = QUERY_MIN_COLUMN_LENGHT;
186   }
187
188   rfc822_write_address (buf, sizeof (buf), table[num].data->addr, 1);
189
190   mutt_format_string (buf2, sizeof (buf2),
191                       FirstColumn + 2, FirstColumn + 2,
192                       0, ' ', table[num].data->name,
193                       safe_strlen (table[num].data->name), 0);
194
195   snprintf (s, slen, " %c %3d %s %-*.*s %s",
196             table[num].tagged ? '*' : ' ',
197             num + 1,
198             buf2,
199             SecondColumn + 2,
200             SecondColumn + 2, buf, NONULL (table[num].data->other));
201 }
202
203 static int query_tag (MUTTMENU * menu, int n, int m)
204 {
205   ENTRY *cur = &((ENTRY *) menu->data)[n];
206   int ot = cur->tagged;
207
208   cur->tagged = m >= 0 ? m : !cur->tagged;
209   return cur->tagged - ot;
210 }
211
212 int mutt_query_complete (char *buf, size_t buflen)
213 {
214   QUERY *results = NULL;
215   ADDRESS *tmpa;
216
217   if (!QueryCmd) {
218     mutt_error _("Query command not defined.");
219
220     return 0;
221   }
222
223   results = run_query (buf, 1);
224   if (results) {
225     /* only one response? */
226     if (results->next == NULL) {
227       tmpa = result_to_addr (results);
228       mutt_addrlist_to_local (tmpa);
229       buf[0] = '\0';
230       rfc822_write_address (buf, buflen, tmpa, 0);
231       rfc822_free_address (&tmpa);
232       mutt_clear_error ();
233       return (0);
234     }
235     /* multiple results, choose from query menu */
236     query_menu (buf, buflen, results, 1);
237   }
238   return (0);
239 }
240
241 void mutt_query_menu (char *buf, size_t buflen)
242 {
243   if (!QueryCmd) {
244     mutt_error _("Query command not defined.");
245
246     return;
247   }
248
249   if (buf == NULL) {
250     char buffer[STRING] = "";
251
252     query_menu (buffer, sizeof (buffer), NULL, 0);
253   }
254   else {
255     query_menu (buf, buflen, NULL, 1);
256   }
257 }
258
259 static void query_menu (char *buf, size_t buflen, QUERY * results, int retbuf)
260 {
261   MUTTMENU *menu;
262   HEADER *msg = NULL;
263   ENTRY *QueryTable = NULL;
264   QUERY *queryp = NULL;
265   int i, done = 0;
266   int op;
267   char helpstr[SHORT_STRING];
268   char title[STRING];
269
270   snprintf (title, sizeof (title), _("Query")); /* FIXME */
271
272   menu = mutt_new_menu ();
273   menu->make_entry = query_entry;
274   menu->search = query_search;
275   menu->tag = query_tag;
276   menu->menu = MENU_QUERY;
277   menu->title = title;
278   menu->help =
279     mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, QueryHelp);
280
281   if (results == NULL) {
282     /* Prompt for Query */
283     if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
284       results = run_query (buf, 0);
285     }
286   }
287
288   if (results) {
289     snprintf (title, sizeof (title), _("Query '%s'"), buf);
290
291     /* count the number of results */
292     for (queryp = results; queryp; queryp = queryp->next)
293       menu->max++;
294
295     menu->data = QueryTable =
296       (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
297
298     for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
299       QueryTable[i].data = queryp;
300
301     while (!done) {
302       switch ((op = mutt_menuLoop (menu))) {
303       case OP_QUERY_APPEND:
304       case OP_QUERY:
305         if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
306           QUERY *newresults = NULL;
307
308           newresults = run_query (buf, 0);
309
310           menu->redraw = REDRAW_FULL;
311           if (newresults) {
312             snprintf (title, sizeof (title), _("Query '%s'"), buf);
313
314             if (op == OP_QUERY) {
315               queryp = results;
316               while (queryp) {
317                 rfc822_free_address (&queryp->addr);
318                 FREE (&queryp->name);
319                 FREE (&queryp->other);
320                 results = queryp->next;
321                 FREE (&queryp);
322                 queryp = results;
323               }
324               results = newresults;
325               FREE (&QueryTable);
326             }
327             else {
328               /* append */
329               for (queryp = results; queryp->next; queryp = queryp->next);
330
331               queryp->next = newresults;
332             }
333
334
335             menu->current = 0;
336             mutt_menuDestroy (&menu);
337             menu = mutt_new_menu ();
338             menu->make_entry = query_entry;
339             menu->search = query_search;
340             menu->tag = query_tag;
341             menu->menu = MENU_QUERY;
342             menu->title = title;
343             menu->help =
344               mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
345                                  QueryHelp);
346
347             /* count the number of results */
348             for (queryp = results; queryp; queryp = queryp->next)
349               menu->max++;
350
351             if (op == OP_QUERY) {
352               menu->data = QueryTable =
353                 (ENTRY *) safe_calloc (menu->max, sizeof (ENTRY));
354
355               for (i = 0, queryp = results; queryp;
356                    queryp = queryp->next, i++)
357                 QueryTable[i].data = queryp;
358             }
359             else {
360               int clear = 0;
361
362               /* append */
363               safe_realloc (&QueryTable, menu->max * sizeof (ENTRY));
364
365               menu->data = QueryTable;
366
367               for (i = 0, queryp = results; queryp;
368                    queryp = queryp->next, i++) {
369                 /* once we hit new entries, clear/init the tag */
370                 if (queryp == newresults)
371                   clear = 1;
372
373                 QueryTable[i].data = queryp;
374                 if (clear)
375                   QueryTable[i].tagged = 0;
376               }
377             }
378           }
379         }
380         break;
381
382       case OP_CREATE_ALIAS:
383         if (menu->tagprefix) {
384           ADDRESS *naddr = NULL;
385
386           for (i = 0; i < menu->max; i++)
387             if (QueryTable[i].tagged) {
388               ADDRESS *a = result_to_addr (QueryTable[i].data);
389
390               rfc822_append (&naddr, a);
391               rfc822_free_address (&a);
392             }
393
394           mutt_create_alias (NULL, naddr);
395         }
396         else {
397           ADDRESS *a = result_to_addr (QueryTable[menu->current].data);
398
399           mutt_create_alias (NULL, a);
400           rfc822_free_address (&a);
401         }
402         break;
403
404       case OP_GENERIC_SELECT_ENTRY:
405         if (retbuf) {
406           done = 2;
407           break;
408         }
409         /* fall through to OP_MAIL */
410
411       case OP_MAIL:
412         msg = mutt_new_header ();
413         msg->env = mutt_new_envelope ();
414         if (!menu->tagprefix) {
415           msg->env->to = result_to_addr (QueryTable[menu->current].data);
416         }
417         else {
418           for (i = 0; i < menu->max; i++)
419             if (QueryTable[i].tagged) {
420               ADDRESS *a = result_to_addr (QueryTable[i].data);
421
422               rfc822_append (&msg->env->to, a);
423               rfc822_free_address (&a);
424             }
425         }
426         ci_send_message (0, msg, NULL, Context, NULL);
427         menu->redraw = REDRAW_FULL;
428         break;
429
430       case OP_EXIT:
431         done = 1;
432         break;
433       }
434     }
435
436     /* if we need to return the selected entries */
437     if (retbuf && (done == 2)) {
438       int tagged = 0;
439       size_t curpos = 0;
440
441       memset (buf, 0, buflen);
442
443       /* check for tagged entries */
444       for (i = 0; i < menu->max; i++) {
445         if (QueryTable[i].tagged) {
446           if (curpos == 0) {
447             ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
448
449             mutt_addrlist_to_local (tmpa);
450             tagged = 1;
451             rfc822_write_address (buf, buflen, tmpa, 0);
452             curpos = safe_strlen (buf);
453             rfc822_free_address (&tmpa);
454           }
455           else if (curpos + 2 < buflen) {
456             ADDRESS *tmpa = result_to_addr (QueryTable[i].data);
457
458             mutt_addrlist_to_local (tmpa);
459             strcat (buf, ", "); /* __STRCAT_CHECKED__ */
460             rfc822_write_address ((char *) buf + curpos + 1,
461                                   buflen - curpos - 1, tmpa, 0);
462             curpos = safe_strlen (buf);
463             rfc822_free_address (&tmpa);
464           }
465         }
466       }
467       /* then enter current message */
468       if (!tagged) {
469         ADDRESS *tmpa = result_to_addr (QueryTable[menu->current].data);
470
471         mutt_addrlist_to_local (tmpa);
472         rfc822_write_address (buf, buflen, tmpa, 0);
473         rfc822_free_address (&tmpa);
474       }
475
476     }
477
478     queryp = results;
479     while (queryp) {
480       rfc822_free_address (&queryp->addr);
481       FREE (&queryp->name);
482       FREE (&queryp->other);
483       results = queryp->next;
484       FREE (&queryp);
485       queryp = results;
486     }
487     FREE (&QueryTable);
488
489     /* tell whoever called me to redraw the screen when I return */
490     set_option (OPTNEEDREDRAW);
491   }
492
493   mutt_menuDestroy (&menu);
494 }