we don't care about sun attachments, this is long dead
[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 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17 #include <lib-lib/file.h>
18 #include <lib-lib/mapping.h>
19
20 #include "mutt.h"
21 #include "mutt_menu.h"
22 #include "mutt_idna.h"
23 #include "sort.h"
24
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_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, size_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   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 = p_new(QUERY, 1);
106         cur = first;
107       }
108       else {
109         cur->next = p_new(QUERY, 1);
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 = m_strdup(p);
124         p = strtok (NULL, "\t\n");
125         if (p) {
126           cur->other = m_strdup(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                       m_strlen(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_t *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       address_delete (&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 = p_new(ENTRY, menu->max);
293
294     for (i = 0, queryp = results; queryp; queryp = queryp->next, i++)
295       QueryTable[i].data = queryp;
296
297     while (!done) {
298       switch ((op = mutt_menuLoop (menu))) {
299       case OP_QUERY_APPEND:
300       case OP_QUERY:
301         if (mutt_get_field (_("Query: "), buf, buflen, 0) == 0 && buf[0]) {
302           QUERY *newresults = NULL;
303
304           newresults = run_query (buf, 0);
305
306           menu->redraw = REDRAW_FULL;
307           if (newresults) {
308             snprintf (title, sizeof (title), _("Query '%s'"), buf);
309
310             if (op == OP_QUERY) {
311               queryp = results;
312               while (queryp) {
313                 address_delete (&queryp->addr);
314                 p_delete(&queryp->name);
315                 p_delete(&queryp->other);
316                 results = queryp->next;
317                 p_delete(&queryp);
318                 queryp = results;
319               }
320               results = newresults;
321               p_delete(&QueryTable);
322             }
323             else {
324               /* append */
325               for (queryp = results; queryp->next; queryp = queryp->next);
326
327               queryp->next = newresults;
328             }
329
330
331             menu->current = 0;
332             mutt_menuDestroy (&menu);
333             menu = mutt_new_menu ();
334             menu->make_entry = query_entry;
335             menu->search = query_search;
336             menu->tag = query_tag;
337             menu->menu = MENU_QUERY;
338             menu->title = title;
339             menu->help =
340               mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY,
341                                  QueryHelp);
342
343             /* count the number of results */
344             for (queryp = results; queryp; queryp = queryp->next)
345               menu->max++;
346
347             if (op == OP_QUERY) {
348               menu->data = QueryTable = p_new(ENTRY, menu->max);
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               p_realloc(&QueryTable, menu->max);
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_t *naddr = NULL;
380
381           for (i = 0; i < menu->max; i++)
382             if (QueryTable[i].tagged) {
383               address_list_append(&naddr, result_to_addr(QueryTable[i].data));
384             }
385
386           mutt_create_alias (NULL, naddr);
387         }
388         else {
389           address_t *a = result_to_addr (QueryTable[menu->current].data);
390
391           mutt_create_alias (NULL, a);
392           address_delete (&a);
393         }
394         break;
395
396       case OP_GENERIC_SELECT_ENTRY:
397         if (retbuf) {
398           done = 2;
399           break;
400         }
401         /* fall through to OP_MAIL */
402
403       case OP_MAIL:
404         msg = mutt_new_header ();
405         msg->env = mutt_new_envelope ();
406         if (!menu->tagprefix) {
407           msg->env->to = result_to_addr (QueryTable[menu->current].data);
408         }
409         else {
410           for (i = 0; i < menu->max; i++)
411             if (QueryTable[i].tagged) {
412               address_list_append(&msg->env->to, result_to_addr(QueryTable[i].data));
413             }
414         }
415         ci_send_message (0, msg, NULL, Context, NULL);
416         menu->redraw = REDRAW_FULL;
417         break;
418
419       case OP_EXIT:
420         done = 1;
421         break;
422       }
423     }
424
425     /* if we need to return the selected entries */
426     if (retbuf && (done == 2)) {
427       int tagged = 0;
428       size_t curpos = 0;
429
430       p_clear(buf, buflen);
431
432       /* check for tagged entries */
433       for (i = 0; i < menu->max; i++) {
434         if (QueryTable[i].tagged) {
435           if (curpos == 0) {
436             address_t *tmpa = result_to_addr (QueryTable[i].data);
437
438             mutt_addrlist_to_local (tmpa);
439             tagged = 1;
440             rfc822_write_address (buf, buflen, tmpa, 0);
441             curpos = m_strlen(buf);
442             address_delete (&tmpa);
443           }
444           else if (curpos + 2 < buflen) {
445             address_t *tmpa = result_to_addr (QueryTable[i].data);
446
447             mutt_addrlist_to_local (tmpa);
448             strcat (buf, ", "); /* __STRCAT_CHECKED__ */
449             rfc822_write_address ((char *) buf + curpos + 1,
450                                   buflen - curpos - 1, tmpa, 0);
451             curpos = m_strlen(buf);
452             address_delete (&tmpa);
453           }
454         }
455       }
456       /* then enter current message */
457       if (!tagged) {
458         address_t *tmpa = result_to_addr (QueryTable[menu->current].data);
459
460         mutt_addrlist_to_local (tmpa);
461         rfc822_write_address (buf, buflen, tmpa, 0);
462         address_delete (&tmpa);
463       }
464
465     }
466
467     queryp = results;
468     while (queryp) {
469       address_delete (&queryp->addr);
470       p_delete(&queryp->name);
471       p_delete(&queryp->other);
472       results = queryp->next;
473       p_delete(&queryp);
474       queryp = results;
475     }
476     p_delete(&QueryTable);
477
478     /* tell whoever called me to redraw the screen when I return */
479     set_option (OPTNEEDREDRAW);
480   }
481
482   mutt_menuDestroy (&menu);
483 }