9a39be487b33596a83706e4068d2c8b132ad4582
[claws.git] / src / action.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto & The Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #ifdef GDK_WINDOWING_X11
31 #  include <gdk/gdkx.h>
32 #endif /* GDK_WINDOWING_X11 */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #if HAVE_SYS_WAIT_H
39 #  include <sys/wait.h>
40 #endif
41 #include <signal.h>
42 #include <unistd.h>
43
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "manage_window.h"
47 #include "mainwindow.h"
48 #include "prefs_common.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
51 #include "action.h"
52 #include "compose.h"
53 #include "procmsg.h"
54 #include "textview.h"
55 #include "matcher_parser.h" /* CLAWS */
56 #include "filtering.h"
57 #include "procheader.h"
58
59 typedef struct _Children                Children;
60 typedef struct _ChildInfo               ChildInfo;
61 typedef struct _UserStringDialog        UserStringDialog;
62
63 struct _Children
64 {
65         GtkWidget       *dialog;
66         GtkWidget       *text;
67         GtkWidget       *input_entry;
68         GtkWidget       *input_hbox;
69         GtkWidget       *progress_bar;
70         GtkWidget       *abort_btn;
71         GtkWidget       *close_btn;
72         GtkWidget       *scrolledwin;
73
74         gchar           *action;
75         ActionType       action_type;
76         GSList          *list;
77         gint             nb;
78         gint             initial_nb;
79         gint             open_in;
80         gboolean         output;
81
82         GtkWidget       *msg_text;
83
84         gboolean         is_selection;
85 };
86
87 struct _ChildInfo
88 {
89         Children        *children;
90         gchar           *cmd;
91         pid_t            pid;
92         gint             next_sig;
93         gint             chld_in;
94         gint             chld_out;
95         gint             chld_err;
96         gint             chld_status;
97         gint             tag_in;
98         gint             tag_out;
99         gint             tag_err;
100         gint             tag_status;
101         gint             new_out;
102
103         GString         *output;
104         void (*callback)(void *data);
105         void *data;
106
107         GSList          *msginfo_list;
108 };
109
110 static void action_update_menu          (GtkUIManager   *ui_manager,
111                                          const gchar    *accel_group,
112                                          gchar          *branch_path,
113                                          gpointer        callback,
114                                          gpointer        data);
115 static void compose_actions_execute_cb  (GtkWidget      *widget, 
116                                          gpointer        data);
117 static void compose_actions_execute     (Compose        *compose,
118                                          guint           action_nb,
119                                          GtkWidget      *widget);
120
121 static void mainwin_actions_execute_cb  (GtkWidget      *widget, 
122                                          gpointer        data);
123 static void mainwin_actions_execute     (MainWindow     *mainwin,
124                                          guint           action_nb,
125                                          GtkWidget      *widget);
126
127 static void msgview_actions_execute_cb  (GtkWidget      *widget, 
128                                          gpointer        data);
129 static void msgview_actions_execute     (MessageView    *msgview,
130                                          guint           action_nb,
131                                          GtkWidget      *widget);
132
133 static void message_actions_execute     (MessageView    *msgview,
134                                          guint           action_nb,
135                                          GSList         *msg_list);
136
137 static gboolean execute_filtering_actions(gchar         *action, 
138                                           GSList        *msglist);
139
140 static gboolean execute_actions         (gchar          *action, 
141                                          GSList         *msg_list, 
142                                          GtkWidget      *text,
143                                          gint            body_pos,
144                                          MimeInfo       *partinfo, 
145                                          void (*callback)(void *data),
146                                          void *data);
147
148 static gchar *parse_action_cmd          (gchar          *action,
149                                          MsgInfo        *msginfo,
150                                          GSList         *msg_list,
151                                          MimeInfo       *partinfo,
152                                          const gchar    *user_str,
153                                          const gchar    *user_hidden_str,
154                                          const gchar    *sel_str);
155 static gboolean parse_append_filename   (GString        *cmd,
156                                          MsgInfo        *msginfo);
157
158 static gboolean parse_append_msgpart    (GString        *cmd,
159                                          MsgInfo        *msginfo,
160                                          MimeInfo       *partinfo);
161
162 static ChildInfo *fork_child            (gchar          *cmd,
163                                          const gchar    *msg_str,
164                                          Children       *children);
165
166 static gint wait_for_children           (Children       *children);
167
168 static void free_children               (Children       *children);
169
170 static void childinfo_close_pipes       (ChildInfo      *child_info);
171
172 static void create_io_dialog            (Children       *children);
173 static void update_io_dialog            (Children       *children);
174
175 static void hide_io_dialog_cb           (GtkWidget      *widget,
176                                          gpointer        data);
177 static gint io_dialog_key_pressed_cb    (GtkWidget      *widget,
178                                          GdkEventKey    *event,
179                                          gpointer        data);
180
181 static void catch_output                (gpointer                data,
182                                          gint                    source,
183                                          GdkInputCondition       cond);
184 static void catch_input                 (gpointer                data, 
185                                          gint                    source,
186                                          GdkInputCondition       cond);
187 static void catch_status                (gpointer                data,
188                                          gint                    source,
189                                          GdkInputCondition       cond);
190
191 static gchar *get_user_string           (const gchar    *action,
192                                          ActionType      type);
193
194
195 ActionType action_get_type(const gchar *action_str)
196 {
197         const gchar *p;
198         gboolean in_filtering_action = FALSE;
199         ActionType action_type = ACTION_NONE;
200
201         cm_return_val_if_fail(action_str,  ACTION_ERROR);
202         cm_return_val_if_fail(*action_str, ACTION_ERROR);
203
204         p = action_str;
205
206         if (p[0] == '|') {
207                 action_type |= ACTION_PIPE_IN;
208                 p++;
209         } else if (p[0] == '>') {
210                 action_type |= ACTION_USER_IN;
211                 p++;
212         } else if (p[0] == '*') {
213                 action_type |= ACTION_USER_HIDDEN_IN;
214                 p++;
215         }
216
217         if (p[0] == '\0')
218                 return ACTION_ERROR;
219
220         while (*p && action_type != ACTION_ERROR) {
221                 if (!in_filtering_action) {
222                         if (p[0] == '%' && p[1]) {
223                                 switch (p[1]) {
224                                 case 'a':
225                                         /* CLAWS: filtering action is a mutually exclusive
226                                         * action. we can enable others if needed later. we
227                                         * add ACTION_SINGLE | ACTION_MULTIPLE so it will
228                                         * only be executed from the main window toolbar */
229                                         if (p[2] == 's')  /* source messages */
230                                                 action_type = ACTION_FILTERING_ACTION 
231                                                                 | ACTION_SINGLE 
232                                                                 | ACTION_MULTIPLE;
233                                         in_filtering_action = TRUE;
234                                         break;
235                                 case 'f':
236                                         action_type |= ACTION_SINGLE;
237                                         break;
238                                 case 'F':
239                                         action_type |= ACTION_MULTIPLE;
240                                         break;
241                                 case 'p':
242                                         action_type |= ACTION_SINGLE;
243                                         break;
244                                 case 's':
245                                         action_type |= ACTION_SELECTION_STR;
246                                         break;
247                                 case 'u':
248                                         action_type |= ACTION_USER_STR;
249                                         break;
250                                 case 'h':
251                                         action_type |= ACTION_USER_HIDDEN_STR;
252                                         break;
253                                 case '%':
254                                         /* literal '%' */
255                                         break;
256                                 default:
257                                         action_type = ACTION_ERROR;
258                                         break;
259                                 }
260                                 p++;
261                         } else if (p[0] == '|') {
262                                 if (p[1] == '\0')
263                                         action_type |= ACTION_PIPE_OUT;
264                         } else if (p[0] == '>') {
265                                 if (p[1] == '\0')
266                                         action_type |= ACTION_INSERT;
267                         } else if (p[0] == '&') {
268                                 if (p[1] == '\0')
269                                         action_type |= ACTION_ASYNC;
270                         } else if (p[0] == '}') {
271                                 in_filtering_action = FALSE;
272                         }
273                 }
274                         p++;
275         }
276
277         return action_type;
278 }
279
280 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
281                                GSList *msg_list, MimeInfo *partinfo,
282                                const gchar *user_str,
283                                const gchar *user_hidden_str,
284                                const gchar *sel_str)
285 {
286         GString *cmd;
287         gchar *p;
288         GSList *cur;
289         
290         p = action;
291         
292         if (p[0] == '|' || p[0] == '>' || p[0] == '*')
293                 p++;
294
295         cmd = g_string_sized_new(strlen(action));
296
297         while (p[0] &&
298                !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
299                 if (p[0] == '%' && p[1]) {
300                         switch (p[1]) {
301                         case 'f':
302                                 if (!parse_append_filename(cmd, msginfo)) {
303                                         g_string_free(cmd, TRUE);
304                                         return NULL;
305                                 }
306                                 p++;
307                                 break;
308                         case 'F':
309                                 for (cur = msg_list; cur != NULL;
310                                      cur = cur->next) {
311                                         MsgInfo *msg = (MsgInfo *)cur->data;
312
313                                         if (!parse_append_filename(cmd, msg)) {
314                                                 g_string_free(cmd, TRUE);
315                                                 return NULL;
316                                         }
317                                         if (cur->next)
318                                                 g_string_append_c(cmd, ' ');
319                                 }
320                                 p++;
321                                 break;
322                         case 'p':
323                                 if (!parse_append_msgpart(cmd, msginfo,
324                                                           partinfo)) {
325                                         g_string_free(cmd, TRUE);
326                                         return NULL;
327                                 }
328                                 p++;
329                                 break;
330                         case 's':
331                                 if (sel_str)
332                                         g_string_append(cmd, sel_str);
333                                 p++;
334                                 break;
335                         case 'u':
336                                 if (user_str)
337                                         g_string_append(cmd, user_str);
338                                 p++;
339                                 break;
340                         case 'h':
341                                 if (user_hidden_str)
342                                         g_string_append(cmd, user_hidden_str);
343                                 p++;
344                                 break;
345                         case '%':
346                                 g_string_append_c(cmd, p[1]);
347                                 p++;
348                                 break;
349                         default:
350                                 g_string_append_c(cmd, p[0]);
351                                 g_string_append_c(cmd, p[1]);
352                                 p++;
353                         }
354                 } else {
355                         g_string_append_c(cmd, p[0]);
356                 }
357                 p++;
358         }
359         if (cmd->len == 0) {
360                 g_string_free(cmd, TRUE);
361                 return NULL;
362         }
363
364         p = cmd->str;
365         g_string_free(cmd, FALSE);
366         return p;
367 }
368
369 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
370 {
371         gchar *filename;
372         gchar *p, *q;
373         gchar escape_ch[] = "\\ ";
374
375         cm_return_val_if_fail(msginfo, FALSE);
376
377         filename = procmsg_get_message_file(msginfo);
378
379         if (!filename) {
380                 alertpanel_error(_("Could not get message file %d"),
381                                  msginfo->msgnum);
382                 return FALSE;
383         }
384
385         p = filename;
386         while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
387                 escape_ch[1] = *q;
388                 *q = '\0';
389                 g_string_append(cmd, p);
390                 g_string_append(cmd, escape_ch);
391                 p = q + 1;
392         }
393         g_string_append(cmd, p);
394
395         g_free(filename);
396
397         return TRUE;
398 }
399
400 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
401                                      MimeInfo *partinfo)
402 {
403         gboolean single_part = FALSE;
404         gchar *filename;
405         gchar *part_filename;
406         gint ret;
407
408         if (!partinfo) {
409                 partinfo = procmime_scan_message(msginfo);
410                 if (!partinfo) {
411                         alertpanel_error(_("Could not get message part."));
412                         return FALSE;
413                 }
414
415                 single_part = TRUE;
416         }
417
418         filename = procmsg_get_message_file_path(msginfo);
419         part_filename = procmime_get_tmp_file_name(partinfo);
420
421         ret = procmime_get_part(part_filename, partinfo);
422
423         if (single_part)
424                 procmime_mimeinfo_free_all(partinfo);
425         g_free(filename);
426
427         if (ret < 0) {
428                 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
429                 g_free(part_filename);
430                 return FALSE;
431         }
432
433         g_string_append(cmd, part_filename);
434
435         g_free(part_filename);
436
437         return TRUE;
438 }
439
440 void actions_execute(gpointer data, 
441                      guint action_nb,
442                      GtkWidget *widget,
443                      gint source)
444 {
445         if (source == TOOLBAR_MAIN) 
446                 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
447         else if (source == TOOLBAR_COMPOSE)
448                 compose_actions_execute((Compose*)data, action_nb, widget);
449         else if (source == TOOLBAR_MSGVIEW)
450                 msgview_actions_execute((MessageView*)data, action_nb, widget); 
451 }
452
453 void action_update_mainwin_menu(GtkUIManager *ui_manager,
454                                 gchar *branch_path,
455                                 MainWindow *mainwin)
456 {
457         action_update_menu(ui_manager, "<MainwinActions>", branch_path,
458                            mainwin_actions_execute_cb, mainwin);
459 }
460
461 void action_update_msgview_menu(GtkUIManager    *ui_manager,
462                                 gchar *branch_path,
463                                 MessageView *msgview)
464 {
465         action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
466                            msgview_actions_execute_cb, msgview);
467 }
468
469 void action_update_compose_menu(GtkUIManager *ui_manager, 
470                                 gchar *branch_path,
471                                 Compose *compose)
472 {
473         action_update_menu(ui_manager, "<ComposeActions>", branch_path,
474                            compose_actions_execute_cb, compose);
475 }
476
477 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
478 {
479         GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu)));
480         GList *amenu = children;
481         const gchar *existing_name;
482         while (amenu) {
483                 GtkWidget *item = GTK_WIDGET(amenu->data);
484                 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
485                     !strcmp2(name, existing_name))
486                 {
487                         g_list_free(children);
488                          return item;
489                 }
490                 amenu = amenu->next;
491         }
492
493         g_list_free(children);
494
495         return NULL;
496 }
497
498 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
499 {
500         gchar *submenu = g_strdup(action);
501         GtkWidget *new_menu = NULL;
502         
503         if (strchr(submenu, '/')) {
504                 const gchar *end = (strchr(submenu, '/')+1);
505                 GtkWidget *menu_item = NULL;
506                 if (end && *end) {
507                         *strchr(submenu, '/') = '\0';
508                         if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
509                                 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
510                                 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
511                                 gtk_widget_show(menu_item);
512                                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);         
513                                 new_menu = gtk_menu_new();
514                                 gtk_widget_show(new_menu);
515                                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
516                         } else {
517                                 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
518                         }
519                         new_menu = create_submenus(new_menu, end);
520                 }
521         }
522         g_free(submenu);
523         return new_menu ? new_menu : menu;
524 }
525
526 static void action_update_menu(GtkUIManager *ui_manager,
527                                const gchar *accel_group,
528                                gchar *branch_path,
529                                gpointer callback, gpointer data)
530 {
531         GSList *cur;
532         gchar *action, *action_p;
533         int callback_action = 0;
534         GtkWidget *menu = gtk_menu_new();
535         GtkWidget *item;
536
537         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
538                 GtkWidget *cur_menu = menu;
539                 const gchar *action_name = NULL;
540                 action   = g_strdup((gchar *)cur->data);
541                 action_p = strstr(action, ": ");
542                 if (action_p && action_p[2] &&
543                     (action_get_type(&action_p[2]) != ACTION_ERROR) &&
544                     (action[0] != '/')) {
545                         gchar *accel_path = NULL;
546
547                         action_p[0] = '\0';
548                         if (strchr(action, '/')) {
549                                 cur_menu = create_submenus(cur_menu, action);
550                                 action_name = strrchr(action, '/')+1;
551                         } else {
552                                 action_name = action;
553                         }
554                         gtk_menu_set_accel_group (GTK_MENU (cur_menu), 
555                                 gtk_ui_manager_get_accel_group(ui_manager));
556                         item = gtk_menu_item_new_with_label(action_name);
557                         gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
558                         g_signal_connect(G_OBJECT(item), "activate",
559                                          G_CALLBACK(callback), data);
560                         g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
561                         gtk_widget_show(item);
562                         accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
563                         gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
564                         g_free(accel_path);
565
566                 }
567                 g_free(action);
568                 callback_action++;
569         }
570
571         gtk_widget_show(menu);
572         gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
573 }
574
575 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
576 {
577         Compose *compose = (Compose *)data;
578         gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
579         compose_actions_execute(compose, action_nb, NULL);
580 }
581
582 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
583 {
584         gchar *buf, *action;
585         ActionType action_type;
586
587         cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
588
589         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
590         cm_return_if_fail(buf != NULL);
591         action = strstr(buf, ": ");
592         cm_return_if_fail(action != NULL);
593
594         /* Point to the beginning of the command-line */
595         action += 2;
596
597         action_type = action_get_type(action);
598         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
599                 alertpanel_warning
600                         (_("The selected action cannot be used in the compose window\n"
601                            "because it contains %%f, %%F, %%as or %%p."));
602                 return;
603         }
604
605         execute_actions(action, NULL, compose->text, 0, NULL, 
606                 compose_action_cb, compose);
607 }
608
609 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
610 {
611         MainWindow *mainwin = (MainWindow *)data;
612         gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
613         mainwin_actions_execute(mainwin, action_nb, NULL);
614 }
615
616 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
617                                        GtkWidget *widget)
618 {
619         GSList *msg_list;
620
621         msg_list = summary_get_selected_msg_list(mainwin->summaryview);
622         message_actions_execute(mainwin->messageview, action_nb, msg_list);
623         g_slist_free(msg_list);
624 }
625
626 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
627 {
628         MessageView *msgview = (MessageView *)data;
629         gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
630         msgview_actions_execute(msgview, action_nb, NULL);
631 }
632
633 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
634                                        GtkWidget *widget)
635 {
636         GSList *msg_list = NULL;
637
638         if (msgview->msginfo)
639                 msg_list = g_slist_append(msg_list, msgview->msginfo);
640         message_actions_execute(msgview, action_nb, msg_list);
641         g_slist_free(msg_list);
642 }
643
644 static void message_actions_execute(MessageView *msgview, guint action_nb,
645                                     GSList *msg_list)
646 {
647         TextView *textview;
648         MimeInfo *partinfo;
649         gchar *buf;
650         gchar *action;
651         GtkWidget *text = NULL;
652         guint body_pos = 0;
653         ActionType action_type;
654         
655         cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
656
657         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
658
659         cm_return_if_fail(buf);
660         cm_return_if_fail((action = strstr(buf, ": ")));
661
662         /* Point to the beginning of the command-line */
663         action += 2;
664
665         textview = messageview_get_current_textview(msgview);
666         if (textview) {
667                 text     = textview->text;
668                 body_pos = textview->body_pos;
669         }
670         partinfo = messageview_get_selected_mime_part(msgview);
671
672         /* this command will alter the message text */
673         action_type = action_get_type(action);
674         if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
675                 msgview->filtered = TRUE;
676
677         if (action_type & ACTION_FILTERING_ACTION) 
678                 /* CLAWS: most of the above code is not necessary for applying
679                  * filtering */
680                 execute_filtering_actions(action, msg_list);
681         else
682                 execute_actions(action, msg_list, text, body_pos, partinfo,
683                         NULL, NULL);
684 }
685
686 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
687 {
688         GSList *action_list, *p;
689         const gchar *sbegin, *send;
690         gchar *action_string;
691         SummaryView *summaryview = NULL;
692         MainWindow *mainwin = NULL;
693
694         if (mainwindow_get_mainwindow()) {
695                 summaryview = mainwindow_get_mainwindow()->summaryview;
696                 mainwin = mainwindow_get_mainwindow();
697         }
698
699         if (NULL == (sbegin = strstr2(action, "%as{")))
700                 return FALSE;
701         sbegin += sizeof "%as{" - 1;
702         if (NULL == (send = strrchr(sbegin, '}')))
703                 return FALSE;
704         action_string = g_strndup(sbegin, send - sbegin);
705         
706         action_list = matcher_parser_get_action_list(action_string);
707         if (action_list == NULL) {
708                 gchar *tmp = g_strdup(action_string);
709
710                 g_strstrip(tmp);
711                 if (*tmp == '\0')
712                         alertpanel_error(_("There is no filtering action set"));
713                 else
714                         alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
715                 g_free(action_string);
716                 g_free(tmp);
717                 return FALSE;
718         }
719         g_free(action_string);
720         
721         /* apply actions on each message info */
722         for (p = msglist; p && p->data; p = g_slist_next(p)) {
723                 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
724         }
725
726         if (summaryview) {
727                 summary_lock(summaryview);                              
728                 main_window_cursor_wait(mainwin);               
729                 summary_freeze(summaryview);    
730                 folder_item_update_freeze();                            
731         }
732
733         filtering_move_and_copy_msgs(msglist);
734
735         if (summaryview) {
736                 folder_item_update_thaw();                              
737                 summary_thaw(summaryview);              
738                 main_window_cursor_normal(mainwin);     
739                 summary_unlock(summaryview);                            
740                 summary_show(summaryview, summaryview->folder_item);
741         }
742         for (p = action_list; p; p = g_slist_next(p))
743                 if (p->data) filteringaction_free(p->data);     
744         g_slist_free(action_list);              
745         return TRUE;    
746 }
747
748 static gboolean execute_actions(gchar *action, GSList *msg_list,
749                                 GtkWidget *text,
750                                 gint body_pos, MimeInfo *partinfo,
751                                 void (*callback)(void *data), void *data)
752 {
753         GSList *children_list = NULL;
754         gint is_ok  = TRUE;
755         gint msg_list_len;
756         Children *children;
757         ChildInfo *child_info;
758         ActionType action_type;
759         MsgInfo *msginfo;
760         gchar *cmd;
761         gchar *sel_str = NULL;
762         gchar *msg_str = NULL;
763         gchar *user_str = NULL;
764         gchar *user_hidden_str = NULL;
765         GtkTextIter start_iter, end_iter;
766         gboolean is_selection = FALSE;
767
768         cm_return_val_if_fail(action && *action, FALSE);
769
770         action_type = action_get_type(action);
771
772         if (action_type == ACTION_ERROR)
773                 return FALSE;         /* ERR: syntax error */
774
775         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
776                 return FALSE;         /* ERR: file command without selection */
777
778         msg_list_len = g_slist_length(msg_list);
779
780         if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
781                 if (msg_list_len > 1)
782                         return FALSE; /* ERR: pipe + multiple selection */
783                 if (!text)
784                         return FALSE; /* ERR: pipe and no displayed text */
785         }
786
787         if (action_type & ACTION_SELECTION_STR) {
788                 if (!text)
789                         return FALSE; /* ERR: selection string but no text */
790         }
791
792         if (text) {
793                 GtkTextBuffer *textbuf;
794
795                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
796                 is_selection = gtk_text_buffer_get_selection_bounds
797                         (textbuf, &start_iter, &end_iter);
798                 if (!is_selection) {
799                         gtk_text_buffer_get_iter_at_offset
800                                 (textbuf, &start_iter, body_pos);
801                         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
802                 }
803                 msg_str = gtk_text_buffer_get_text
804                         (textbuf, &start_iter, &end_iter, FALSE);
805                 if (is_selection)
806                         sel_str = g_strdup(msg_str);
807         }
808
809         if (action_type & ACTION_USER_STR) {
810                 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
811                         g_free(msg_str);
812                         g_free(sel_str);
813                         return FALSE;
814                 }
815         }
816
817         if (action_type & ACTION_USER_HIDDEN_STR) {
818                 if (!(user_hidden_str =
819                         get_user_string(action, ACTION_USER_HIDDEN_STR))) {
820                         g_free(msg_str);
821                         g_free(sel_str);
822                         g_free(user_str);
823                         return FALSE;
824                 }
825         }
826
827         if (text && (action_type & ACTION_PIPE_OUT)) {
828                 GtkTextBuffer *textbuf;
829                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
830                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
831         }
832
833         children = g_new0(Children, 1);
834
835         children->action      = g_strdup(action);
836         children->action_type = action_type;
837         children->msg_text    = text;
838         children->is_selection = is_selection;
839
840         if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
841             ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
842                 children->open_in = 1;
843
844         if (action_type & ACTION_SINGLE) {
845                 GSList *cur;
846
847                 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
848                         msginfo = (MsgInfo *)cur->data;
849                         if (!msginfo) {
850                                 is_ok  = FALSE; /* ERR: msginfo missing */
851                                 break;
852                         }
853                         cmd = parse_action_cmd(action, msginfo, msg_list,
854                                                partinfo, user_str,
855                                                user_hidden_str, sel_str);
856                         if (!cmd) {
857                                 debug_print("Action command error\n");
858                                 is_ok  = FALSE; /* ERR: incorrect command */
859                                 break;
860                         }
861                         if ((child_info = fork_child(cmd, msg_str, children))) {
862                                 /* Pass msginfo to catch_status () */
863                                 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
864                                         child_info->msginfo_list = 
865                                                 g_slist_append (NULL, msginfo);
866                                 children_list = g_slist_append(children_list,
867                                                                child_info);
868                         }
869                         g_free(cmd);
870                 }
871         } else {
872                 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
873                                        user_str, user_hidden_str, sel_str);
874                 if (cmd) {
875                         if ((child_info = fork_child(cmd, msg_str, children))) {
876                                 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
877                                         child_info->msginfo_list = 
878                                                 g_slist_copy (msg_list);
879                                 children_list = g_slist_append(children_list,
880                                                                 child_info);
881                         }
882                         g_free(cmd);
883                 } else
884                         is_ok  = FALSE;         /* ERR: incorrect command */
885         }
886
887         g_free(msg_str);
888         g_free(sel_str);
889         g_free(user_str);
890         g_free(user_hidden_str);
891
892         if (!children_list) {
893                  /* If not waiting for children, return */
894                 free_children(children);
895         } else {
896                 GSList *cur;
897
898                 children->list        = children_list;
899                 children->nb          = g_slist_length(children_list);
900                 children->initial_nb  = children->nb;
901
902                 for (cur = children_list; cur; cur = cur->next) {
903                         child_info = (ChildInfo *) cur->data;
904                         child_info->callback = callback;
905                         child_info->data = data;
906                         child_info->tag_status = 
907                                 claws_input_add(child_info->chld_status,
908                                               GDK_INPUT_READ,
909                                               catch_status, child_info,
910                                               FALSE);
911                 }
912
913                 create_io_dialog(children);
914         }
915         return is_ok;
916 }
917
918 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
919                              Children *children)
920 {
921 #ifdef G_OS_UNIX
922         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
923         gchar *cmdline[4], *ret_str;
924         pid_t pid, gch_pid;
925         ChildInfo *child_info;
926         gint sync;
927         gssize by_written = 0, by_read = 0;
928
929         sync = !(children->action_type & ACTION_ASYNC);
930
931         chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
932                 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
933
934         if (sync) {
935                 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
936                     pipe(chld_err)) {
937                         alertpanel_error(_("Command could not be started. "
938                                            "Pipe creation failed.\n%s"),
939                                         g_strerror(errno));
940                         /* Closing fd = -1 fails silently */
941                         (void)close(chld_in[0]);
942                         (void)close(chld_in[1]);
943                         (void)close(chld_out[0]);
944                         (void)close(chld_out[1]);
945                         (void)close(chld_err[0]);
946                         (void)close(chld_err[1]);
947                         (void)close(chld_status[0]);
948                         (void)close(chld_status[1]);
949                         return NULL; /* Pipe error */
950                 }
951         }
952
953         debug_print("Forking child and grandchild.\n");
954         debug_print("Executing: /bin/sh -c %s\n", cmd);
955
956         pid = fork();
957         if (pid == 0) { /* Child */
958                 int r = 0;
959                 if (setpgid(0, 0))
960                         perror("setpgid");
961
962                 gch_pid = fork();
963
964                 if (gch_pid == 0) {
965                         if (setpgid(0, getppid()))
966                                 perror("setpgid");
967
968                         if (sync) {
969                                 if (children->action_type &
970                                     (ACTION_PIPE_IN |
971                                      ACTION_USER_IN |
972                                      ACTION_USER_HIDDEN_IN)) {
973                                         r |= close(fileno(stdin));
974                                         (void) dup  (chld_in[0]);
975                                 }
976                                 r |= close(chld_in[0]);
977                                 r |= close(chld_in[1]);
978
979                                 r |= close(fileno(stdout));
980                                 (void) dup  (chld_out[1]);
981                                 r |= close(chld_out[0]);
982                                 r |= close(chld_out[1]);
983
984                                 r |= close(fileno(stderr));
985                                 (void) dup  (chld_err[1]);
986                                 r |= close(chld_err[0]);
987                                 r |= close(chld_err[1]);
988
989                                 if (r != 0)
990                                         debug_print("%s(%d)", strerror(errno), errno);
991                         }
992
993                         cmdline[0] = "sh";
994                         cmdline[1] = "-c";
995                         ret_str = g_locale_from_utf8(cmd, strlen(cmd),
996                                                      &by_read, &by_written,
997                                                      NULL);
998                         if (ret_str && by_written)
999                                 cmdline[2] = ret_str;
1000                         else
1001                                 cmdline[2] = cmd;
1002                         cmdline[3] = NULL;
1003                         execvp("/bin/sh", cmdline);
1004
1005                         perror("execvp");
1006                         g_free(ret_str);
1007                         _exit(1);
1008                 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1009                         if (sync)
1010                                 r = write(chld_status[1], "1\n", 2);
1011                         if (r != 0)
1012                                 debug_print("%s(%d)", strerror(errno), errno);
1013                         perror("fork");
1014                         _exit(1);
1015                 } else { /* Child */
1016                         if (sync) {
1017                                 r |= close(chld_in[0]);
1018                                 r |= close(chld_in[1]);
1019                                 r |= close(chld_out[0]);
1020                                 r |= close(chld_out[1]);
1021                                 r |= close(chld_err[0]);
1022                                 r |= close(chld_err[1]);
1023                                 r |= close(chld_status[0]);
1024
1025                                 debug_print("Child: waiting for grandchild\n");
1026                                 r |= waitpid(gch_pid, NULL, 0);
1027                                 debug_print("Child: grandchild ended\n");
1028                                 r |= write(chld_status[1], "0\n", 2);
1029                                 r |= close(chld_status[1]);
1030
1031                                 if (r != 0)
1032                                         debug_print("%s(%d)", strerror(errno), errno);
1033                         }
1034                         _exit(0);
1035                 }
1036         } else if (pid < 0) { /* Fork error */
1037                 alertpanel_error(_("Could not fork to execute the following "
1038                                    "command:\n%s\n%s"),
1039                                  cmd, g_strerror(errno));
1040                 return NULL; 
1041         }
1042
1043         /* Parent */
1044
1045         if (!sync) {
1046                 waitpid(pid, NULL, 0);
1047                 return NULL;
1048         }
1049
1050         (void)close(chld_in[0]);
1051         if (!(children->action_type &
1052               (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1053                 (void)close(chld_in[1]);
1054         (void)close(chld_out[1]);
1055         (void)close(chld_err[1]);
1056         (void)close(chld_status[1]);
1057
1058         child_info = g_new0(ChildInfo, 1);
1059
1060         child_info->children    = children;
1061
1062         child_info->pid         = pid;
1063         child_info->next_sig    = SIGTERM;
1064         child_info->cmd         = g_strdup(cmd);
1065         child_info->new_out     = FALSE;
1066         child_info->output      = g_string_new(NULL);
1067         child_info->chld_in     =
1068                 (children->action_type &
1069                  (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1070                         ? chld_in [1] : -1;
1071         child_info->chld_out    = chld_out[0];
1072         child_info->chld_err    = chld_err[0];
1073         child_info->chld_status = chld_status[0];
1074         child_info->tag_in      = -1;
1075         child_info->tag_out     = claws_input_add(chld_out[0], GDK_INPUT_READ,
1076                                                 catch_output, child_info, FALSE);
1077         child_info->tag_err     = claws_input_add(chld_err[0], GDK_INPUT_READ,
1078                                                 catch_output, child_info, FALSE);
1079
1080         if (!(children->action_type &
1081               (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1082                 return child_info;
1083
1084         if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1085                 int r;
1086                 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1087                                              &by_read, &by_written, NULL);
1088                 if (ret_str && by_written) {
1089                         r = write(chld_in[1], ret_str, strlen(ret_str));
1090                         g_free(ret_str);
1091                 } else
1092                         r = write(chld_in[1], msg_str, strlen(msg_str));
1093                 if (!(children->action_type &
1094                       (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1095                         r = close(chld_in[1]);
1096                 child_info->chld_in = -1; /* No more input */
1097                 if (r != 0)
1098                         debug_print("%s(%d)", strerror(errno), errno);
1099         }
1100
1101         return child_info;
1102 #else
1103         return NULL;
1104 #endif /* G_OS_UNIX */
1105 }
1106
1107 static void kill_children_cb(GtkWidget *widget, gpointer data)
1108 {
1109 #ifdef G_OS_UNIX
1110         GSList *cur;
1111         Children *children = (Children *) data;
1112         ChildInfo *child_info;
1113
1114         for (cur = children->list; cur; cur = cur->next) {
1115                 child_info = (ChildInfo *)(cur->data);
1116                 debug_print("Killing child group id %d\n", child_info->pid);
1117                 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1118                         perror("kill");
1119                 child_info->next_sig = SIGKILL;
1120         }
1121 #endif /* G_OS_UNIX */
1122 }
1123
1124 static gint wait_for_children(Children *children)
1125 {
1126         gboolean new_output;
1127         ChildInfo *child_info;
1128         GSList *cur;
1129
1130         cur = children->list;
1131         new_output = FALSE;
1132         while (cur) {
1133                 child_info = (ChildInfo *)cur->data;
1134                 new_output |= child_info->new_out;
1135                 cur = cur->next;
1136         }
1137
1138         children->output |= new_output;
1139
1140         if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1141                 update_io_dialog(children);
1142
1143         if (children->nb)
1144                 return FALSE;
1145
1146         if (!children->dialog) {
1147                 free_children(children);
1148         } else if (!children->output) {
1149                 gtk_widget_destroy(children->dialog);
1150         }
1151
1152         return FALSE;
1153 }
1154
1155 static void send_input(GtkWidget *w, gpointer data)
1156 {
1157         Children *children = (Children *) data;
1158         ChildInfo *child_info = (ChildInfo *) children->list->data;
1159
1160         child_info->tag_in = claws_input_add(child_info->chld_in,
1161                                            GDK_INPUT_WRITE,
1162                                            catch_input, children, FALSE);
1163 }
1164
1165 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1166 {
1167         hide_io_dialog_cb(w, data);
1168         return TRUE;
1169 }
1170
1171 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1172 {
1173
1174         Children *children = (Children *)data;
1175
1176         if (!children->nb) {
1177                 g_signal_handlers_disconnect_matched
1178                         (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1179                          0, 0, NULL, NULL, children);
1180                 gtk_widget_destroy(children->dialog);
1181                 free_children(children);
1182         }
1183 }
1184
1185 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1186                                      gpointer data)
1187 {
1188         if (event && (event->keyval == GDK_KEY_Escape ||
1189                       event->keyval == GDK_KEY_Return ||
1190                           event->keyval == GDK_KEY_KP_Enter))
1191                 hide_io_dialog_cb(widget, data);
1192         return TRUE;
1193 }
1194
1195 static void childinfo_close_pipes(ChildInfo *child_info)
1196 {
1197         /* stdout and stderr pipes are guaranteed to be removed by
1198          * their handler, but in case where we receive child exit notification
1199          * before grand-child's pipes closing signals, we check them and close
1200          * them if necessary
1201          */
1202         if (child_info->tag_in > 0)
1203                 g_source_remove(child_info->tag_in);
1204         if (child_info->tag_out > 0)
1205                 g_source_remove(child_info->tag_out);
1206         if (child_info->tag_err > 0)
1207                 g_source_remove(child_info->tag_err);
1208
1209         if (child_info->chld_in >= 0)
1210                 (void)close(child_info->chld_in);
1211         if (child_info->chld_out >= 0)
1212                 (void)close(child_info->chld_out);
1213         if (child_info->chld_err >= 0)
1214                 (void)close(child_info->chld_err);
1215
1216         (void)close(child_info->chld_status);
1217 }
1218
1219 static void free_children(Children *children)
1220 {
1221         ChildInfo *child_info;
1222         void (*callback)(void *data) = NULL;
1223         void *data = NULL;
1224
1225         debug_print("Freeing children data %p\n", children);
1226
1227         g_free(children->action);
1228         while (children->list != NULL) {
1229                 child_info = (ChildInfo *)children->list->data;
1230                 g_free(child_info->cmd);
1231                 g_string_free(child_info->output, TRUE);
1232                 children->list = g_slist_remove(children->list, child_info);
1233                 callback = child_info->callback;
1234                 data = child_info->data;
1235                 g_free(child_info);
1236         }
1237
1238         if (callback)
1239                 callback(data);
1240         
1241         g_free(children);
1242 }
1243
1244 static void update_io_dialog(Children *children)
1245 {
1246         GSList *cur;
1247
1248         debug_print("Updating actions input/output dialog.\n");
1249
1250         if (children->progress_bar) {
1251                 gchar *text;
1252 #ifdef GENERIC_UMPC
1253                 /* use a more compact format */
1254                 const gchar *format = "%s %d/%d";
1255 #else
1256                 const gchar *format = "%s %d / %d";
1257 #endif
1258                 
1259                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1260                                                   (children->initial_nb == 0) ? 0 :
1261                                               (gdouble) (children->initial_nb - children->nb) /
1262                                               (gdouble) children->initial_nb);
1263                 text = g_strdup_printf(format, _("Completed"), 
1264                                        children->initial_nb - children->nb,
1265                                        children->initial_nb);
1266                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1267                 g_free(text);
1268         }
1269
1270         if (!children->nb) {
1271                 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1272                 gtk_widget_set_sensitive(children->close_btn, TRUE);
1273                 if (children->input_hbox)
1274                         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1275                 gtk_widget_grab_focus(children->close_btn);
1276                 g_signal_connect(G_OBJECT(children->dialog),
1277                                  "key_press_event",
1278                                  G_CALLBACK(io_dialog_key_pressed_cb),
1279                                  children);
1280         }
1281
1282         if (children->output) {
1283                 GtkWidget *text = children->text;
1284                 GtkTextBuffer *textbuf;
1285                 GtkTextIter iter, start_iter, end_iter;
1286                 gchar *caption;
1287                 ChildInfo *child_info;
1288
1289                 gtk_widget_show(children->scrolledwin);
1290                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1291                 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1292                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1293                 gtk_text_buffer_get_start_iter(textbuf, &iter);
1294
1295                 for (cur = children->list; cur; cur = cur->next) {
1296                         child_info = (ChildInfo *)cur->data;
1297                         if (child_info->pid)
1298                                 caption = g_strdup_printf
1299                                         (_("--- Running: %s\n"),
1300                                          child_info->cmd);
1301                         else
1302                                 caption = g_strdup_printf
1303                                         (_("--- Ended: %s\n"),
1304                                          child_info->cmd);
1305
1306                         gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1307                         gtk_text_buffer_insert(textbuf, &iter,
1308                                                child_info->output->str, -1);
1309                         g_free(caption);
1310                         child_info->new_out = FALSE;
1311                 }
1312         }
1313 }
1314
1315 static void create_io_dialog(Children *children)
1316 {
1317         GtkWidget *dialog;
1318         GtkWidget *vbox;
1319         GtkWidget *entry = NULL;
1320         GtkWidget *input_hbox = NULL;
1321         GtkWidget *send_button;
1322         GtkWidget *label;
1323         GtkWidget *text;
1324         GtkWidget *scrolledwin;
1325         GtkWidget *hbox;
1326         GtkWidget *progress_bar = NULL;
1327         GtkWidget *abort_button;
1328         GtkWidget *close_button;
1329
1330         debug_print("Creating action IO dialog\n");
1331
1332         dialog = gtk_dialog_new();
1333         gtk_container_set_border_width
1334                 (GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), 5);
1335         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1336         gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1337         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1338         manage_window_set_transient(GTK_WINDOW(dialog));
1339         g_signal_connect(G_OBJECT(dialog), "delete_event",
1340                          G_CALLBACK(delete_io_dialog_cb), children);
1341         g_signal_connect(G_OBJECT(dialog), "destroy",
1342                          G_CALLBACK(hide_io_dialog_cb),
1343                          children);
1344
1345         vbox = gtk_vbox_new(FALSE, 8);
1346         gtk_container_add(GTK_CONTAINER(
1347                                 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox);
1348         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1349         gtk_widget_show(vbox);
1350
1351         label = gtk_label_new(children->action);
1352         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1353         gtk_widget_show(label);
1354
1355         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1356         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1357                                        GTK_POLICY_AUTOMATIC,
1358                                        GTK_POLICY_AUTOMATIC);
1359         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1360                                             GTK_SHADOW_IN);
1361         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1362         gtk_widget_set_size_request(scrolledwin, 560, 200);
1363         gtk_widget_hide(scrolledwin);
1364
1365         text = gtk_text_view_new();
1366
1367         if (prefs_common.textfont) {
1368                 PangoFontDescription *font_desc;
1369                 font_desc = pango_font_description_from_string
1370                         (prefs_common.textfont);
1371                 if (font_desc) {
1372                         gtk_widget_modify_font(text, font_desc);
1373                         pango_font_description_free(font_desc);
1374                 }
1375         }
1376
1377         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1378         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1379         gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1380         gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1381         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1382         gtk_widget_show(text);
1383
1384         if (children->open_in) {
1385                 input_hbox = gtk_hbox_new(FALSE, 8);
1386                 gtk_widget_show(input_hbox);
1387
1388                 entry = gtk_entry_new();
1389                 gtk_widget_set_size_request(entry, 320, -1);
1390                 g_signal_connect(G_OBJECT(entry), "activate",
1391                                  G_CALLBACK(send_input), children);
1392                 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1393                 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1394                         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1395 #ifdef MAEMO
1396                         hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
1397                                 HILDON_GTK_INPUT_MODE_FULL | 
1398                                 HILDON_GTK_INPUT_MODE_INVISIBLE);
1399 #endif
1400                 }
1401                 gtk_widget_show(entry);
1402
1403                 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1404                 g_signal_connect(G_OBJECT(send_button), "clicked",
1405                                  G_CALLBACK(send_input), children);
1406                 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1407                                    FALSE, 0);
1408                 gtk_widget_show(send_button);
1409
1410                 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1411                 gtk_widget_grab_focus(entry);
1412         }
1413
1414         if (children->initial_nb > 1) {
1415                 gchar *text;
1416 #ifdef GENERIC_UMPC
1417                 /* use a more compact format */
1418                 const gchar *format = "%s 0/%d\n";
1419 #else
1420                 const gchar *format = "%s 0 / %d\n";
1421 #endif
1422                 
1423                 progress_bar = gtk_progress_bar_new();
1424 #if !GTK_CHECK_VERSION(3, 0, 0)
1425                 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1426                                 GTK_PROGRESS_LEFT_TO_RIGHT);
1427 #else
1428                 gtk_orientable_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1429                                 GTK_ORIENTATION_HORIZONTAL);
1430                 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar),
1431                                 FALSE);
1432 #endif
1433                 text = g_strdup_printf(format, _("Completed"), 
1434                                        children->initial_nb);
1435                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1436                                           text);
1437                 g_free(text);
1438                 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1439                 gtk_widget_show(progress_bar);
1440         }
1441
1442         gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1443                                       &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1444         g_signal_connect(G_OBJECT(abort_button), "clicked",
1445                          G_CALLBACK(kill_children_cb), children);
1446         g_signal_connect(G_OBJECT(close_button), "clicked",
1447                          G_CALLBACK(hide_io_dialog_cb), children);
1448         gtk_widget_show(hbox);
1449
1450         if (children->nb)
1451                 gtk_widget_set_sensitive(close_button, FALSE);
1452
1453         gtk_container_add(GTK_CONTAINER(
1454                         gtk_dialog_get_action_area(GTK_DIALOG(dialog))), hbox);
1455
1456         children->dialog       = dialog;
1457         children->scrolledwin  = scrolledwin;
1458         children->text         = text;
1459         children->input_hbox   = children->open_in ? input_hbox : NULL;
1460         children->input_entry  = children->open_in ? entry : NULL;
1461         children->progress_bar = progress_bar;
1462         children->abort_btn    = abort_button;
1463         children->close_btn    = close_button;
1464
1465         gtk_widget_show(dialog);
1466 }
1467
1468 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1469 {
1470         ChildInfo *child_info = (ChildInfo *)data;
1471         gchar buf;
1472         gint c;
1473
1474         g_source_remove(child_info->tag_status);
1475
1476         c = read(source, &buf, 1);
1477         debug_print("Child returned %c\n", buf);
1478
1479 #ifdef G_OS_UNIX
1480         waitpid(-child_info->pid, NULL, 0);
1481 #endif
1482         childinfo_close_pipes(child_info);
1483         child_info->pid = 0;
1484
1485         if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1486             && child_info->msginfo_list) {
1487                 /* Actions on message *files* might change size and
1488                 * time stamp, and thus invalidate the cache */
1489                 SummaryView *summaryview  = NULL;
1490                 GSList      *cur;
1491                 MsgInfo     *msginfo, *nmi;     /* newmsginfo */
1492                 char        *file;
1493                 gboolean     modified_something = FALSE;
1494                 FolderItem  *last_item = NULL;
1495                 if (mainwindow_get_mainwindow ())
1496                         summaryview = mainwindow_get_mainwindow ()->summaryview;
1497                 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1498                         msginfo = (MsgInfo *)cur->data;
1499                         if (!(msginfo && /* Stuff used valid? */
1500                             msginfo->folder && msginfo->folder->cache)) 
1501                                 continue;
1502                         file = procmsg_get_message_file_path (msginfo);
1503                         if (!file) 
1504                                 continue;
1505                         nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1506                         if (!nmi) 
1507                                 continue; /* Deleted? */
1508                         if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1509                                 nmi->folder = msginfo->folder;
1510                                 nmi->msgnum = msginfo->msgnum;
1511                                 msgcache_update_msg (msginfo->folder->cache, nmi);
1512                                 modified_something = TRUE;
1513                                 last_item = nmi->folder;
1514                         }
1515                         procmsg_msginfo_free (nmi);
1516                         if (summaryview && summaryview->displayed &&
1517                             summaryview->folder_item == msginfo->folder &&
1518                             summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1519                                 summary_redisplay_msg(summaryview);
1520                                         
1521                 }
1522                 if (modified_something && last_item && 
1523                     summaryview && summaryview->folder_item == last_item) {
1524                         summary_show (summaryview, summaryview->folder_item);
1525                 }
1526                 g_slist_free (child_info->msginfo_list);
1527                 child_info->msginfo_list = NULL;
1528         }
1529
1530         if (!child_info->pid)
1531                 child_info->children->nb--;
1532
1533         wait_for_children(child_info->children);
1534 }
1535         
1536 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1537 {
1538         Children *children = (Children *)data;
1539         ChildInfo *child_info = (ChildInfo *)children->list->data;
1540         gchar *input, *ret_str;
1541         gint c, count, len, r;
1542         gssize by_read = 0, by_written = 0;
1543
1544         debug_print("Sending input to grand child.\n");
1545         if (!(cond & GDK_INPUT_WRITE))
1546                 return;
1547
1548         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1549         gtk_widget_grab_focus(children->abort_btn);
1550
1551         g_source_remove(child_info->tag_in);
1552         child_info->tag_in = -1;
1553
1554         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1555                                        0, -1);
1556         ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1557                                      &by_written, NULL);
1558         if (ret_str && by_written) {
1559                 g_free(input);
1560                 input = ret_str;
1561         }
1562
1563         len = strlen(input);
1564         count = 0;
1565
1566         do {
1567                 c = write(child_info->chld_in, input + count, len - count);
1568                 if (c >= 0)
1569                         count += c;
1570         } while (c >= 0 && count < len);
1571
1572         if (c >= 0)
1573                 r = write(child_info->chld_in, "\n", 2);
1574
1575         g_free(input);
1576
1577         r = close(child_info->chld_in);
1578         if (r != 0)
1579                 debug_print("%s(%d)", strerror(errno), errno);
1580         child_info->chld_in = -1;
1581         debug_print("Input to grand child sent.\n");
1582 }
1583
1584 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1585 {
1586         ChildInfo *child_info = (ChildInfo *)data;
1587         gint c;
1588         gchar buf[BUFFSIZE];
1589
1590         debug_print("Catching grand child's output.\n");
1591         if (child_info->children->action_type &
1592             (ACTION_PIPE_OUT | ACTION_INSERT)
1593             && source == child_info->chld_out) {
1594                 GtkTextView *text =
1595                         GTK_TEXT_VIEW(child_info->children->msg_text);
1596                 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1597                 GtkTextIter iter;
1598                 GtkTextMark *mark;
1599                 gint ins_pos;
1600
1601                 mark = gtk_text_buffer_get_insert(textbuf);
1602                 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1603                 ins_pos = gtk_text_iter_get_offset(&iter);
1604
1605                 while (TRUE) {
1606                         gsize bytes_read = 0, bytes_written = 0;
1607                         gchar *ret_str;
1608
1609                         c = read(source, buf, sizeof(buf) - 1);
1610                         if (c == 0)
1611                                 break;
1612
1613                         ret_str = g_locale_to_utf8
1614                                 (buf, c, &bytes_read, &bytes_written, NULL);
1615                         if (ret_str && bytes_written > 0) {
1616                                 gtk_text_buffer_insert
1617                                         (textbuf, &iter, ret_str,
1618                                          -1);
1619                                 g_free(ret_str);
1620                         } else
1621                                 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1622                 }
1623
1624                 if (child_info->children->is_selection) {
1625                         GtkTextIter ins;
1626
1627                         gtk_text_buffer_get_iter_at_offset
1628                                 (textbuf, &ins, ins_pos);
1629                         gtk_text_buffer_select_range(textbuf, &ins, &iter);
1630                 }
1631         } else {
1632                 c = read(source, buf, sizeof(buf) - 1);
1633                 if (c > 0) {
1634                         gsize bytes_read = 0, bytes_written = 0;
1635                         gchar *ret_str;
1636
1637                         ret_str = g_locale_to_utf8
1638                                 (buf, c, &bytes_read, &bytes_written, NULL);
1639                         if (ret_str && bytes_written > 0) {
1640                                 g_string_append_len
1641                                         (child_info->output, ret_str,
1642                                          bytes_written);
1643                                 g_free(ret_str);
1644                         } else
1645                                 g_string_append_len(child_info->output, buf, c);
1646
1647                         child_info->new_out = TRUE;
1648                 }
1649         }
1650         if (c == 0) {
1651                 if (source == child_info->chld_out) {
1652                         g_source_remove(child_info->tag_out);
1653                         child_info->tag_out = -1;
1654                         (void)close(child_info->chld_out);
1655                         child_info->chld_out = -1;
1656                 } else {
1657                         g_source_remove(child_info->tag_err);
1658                         child_info->tag_err = -1;
1659                         (void)close(child_info->chld_err);
1660                         child_info->chld_err = -1;
1661                 }
1662         }
1663         
1664         wait_for_children(child_info->children);
1665 }
1666
1667 static gchar *get_user_string(const gchar *action, ActionType type)
1668 {
1669         gchar *message;
1670         gchar *user_str = NULL;
1671
1672         switch (type) {
1673         case ACTION_USER_HIDDEN_STR:
1674                 message = g_strdup_printf
1675                         (_("Enter the argument for the following action:\n"
1676                            "('%%h' will be replaced with the argument)\n"
1677                            "  %s"),
1678                          action);
1679                 user_str = input_dialog_with_invisible
1680                         (_("Action's hidden user argument"), message, NULL);
1681                 break;
1682         case ACTION_USER_STR:
1683                 message = g_strdup_printf
1684                         (_("Enter the argument for the following action:\n"
1685                            "('%%u' will be replaced with the argument)\n"
1686                            "  %s"),
1687                          action);
1688                 user_str = input_dialog
1689                         (_("Action's user argument"), message, NULL);
1690                 break;
1691         default:
1692                 g_warning("Unsupported action type %d", type);
1693         }
1694
1695         return user_str;
1696 }