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