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