0bd1db715690077e2c4d279b27e2712ac82996f4
[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 *amenu = GTK_MENU_SHELL(menu)->children;
480         const gchar *existing_name;
481         while (amenu) {
482                 GtkWidget *item = GTK_WIDGET(amenu->data);
483                 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
484                     !strcmp2(name, existing_name))
485                          return item;
486                 amenu = amenu->next;
487         }
488         return NULL;
489 }
490
491 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
492 {
493         gchar *submenu = g_strdup(action);
494         GtkWidget *new_menu = NULL;
495         
496         if (strchr(submenu, '/')) {
497                 const gchar *end = (strchr(submenu, '/')+1);
498                 GtkWidget *menu_item = NULL;
499                 if (end && *end) {
500                         *strchr(submenu, '/') = '\0';
501                         if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
502                                 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
503                                 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
504                                 gtk_widget_show(menu_item);
505                                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);         
506                                 new_menu = gtk_menu_new();
507                                 gtk_widget_show(new_menu);
508                                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
509                         } else {
510                                 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
511                         }
512                         new_menu = create_submenus(new_menu, end);
513                 }
514         }
515         g_free(submenu);
516         return new_menu ? new_menu : menu;
517 }
518
519 static void action_update_menu(GtkUIManager *ui_manager,
520                                const gchar *accel_group,
521                                gchar *branch_path,
522                                gpointer callback, gpointer data)
523 {
524         GSList *cur;
525         gchar *action, *action_p;
526         int callback_action = 0;
527         GtkWidget *menu = gtk_menu_new();
528         GtkWidget *item;
529
530         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
531                 GtkWidget *cur_menu = menu;
532                 const gchar *action_name = NULL;
533                 action   = g_strdup((gchar *)cur->data);
534                 action_p = strstr(action, ": ");
535                 if (action_p && action_p[2] &&
536                     (action_get_type(&action_p[2]) != ACTION_ERROR) &&
537                     (action[0] != '/')) {
538                         gchar *accel_path = NULL;
539
540                         action_p[0] = '\0';
541                         if (strchr(action, '/')) {
542                                 cur_menu = create_submenus(cur_menu, action);
543                                 action_name = strrchr(action, '/')+1;
544                         } else {
545                                 action_name = action;
546                         }
547                         gtk_menu_set_accel_group (GTK_MENU (cur_menu), 
548                                 gtk_ui_manager_get_accel_group(ui_manager));
549                         item = gtk_menu_item_new_with_label(action_name);
550                         gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
551                         g_signal_connect(G_OBJECT(item), "activate",
552                                          G_CALLBACK(callback), data);
553                         g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
554                         gtk_widget_show(item);
555                         accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
556                         gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
557                         g_free(accel_path);
558
559                 }
560                 g_free(action);
561                 callback_action++;
562         }
563
564         gtk_widget_show(menu);
565         gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
566 }
567
568 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
569 {
570         Compose *compose = (Compose *)data;
571         gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
572         compose_actions_execute(compose, action_nb, NULL);
573 }
574
575 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
576 {
577         gchar *buf, *action;
578         ActionType action_type;
579
580         cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
581
582         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
583         cm_return_if_fail(buf != NULL);
584         action = strstr(buf, ": ");
585         cm_return_if_fail(action != NULL);
586
587         /* Point to the beginning of the command-line */
588         action += 2;
589
590         action_type = action_get_type(action);
591         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
592                 alertpanel_warning
593                         (_("The selected action cannot be used in the compose window\n"
594                            "because it contains %%f, %%F, %%as or %%p."));
595                 return;
596         }
597
598         execute_actions(action, NULL, compose->text, 0, NULL, 
599                 compose_action_cb, compose);
600 }
601
602 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
603 {
604         MainWindow *mainwin = (MainWindow *)data;
605         gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
606         mainwin_actions_execute(mainwin, action_nb, NULL);
607 }
608
609 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
610                                        GtkWidget *widget)
611 {
612         GSList *msg_list;
613
614         msg_list = summary_get_selected_msg_list(mainwin->summaryview);
615         message_actions_execute(mainwin->messageview, action_nb, msg_list);
616         g_slist_free(msg_list);
617 }
618
619 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
620 {
621         MessageView *msgview = (MessageView *)data;
622         gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
623         msgview_actions_execute(msgview, action_nb, NULL);
624 }
625
626 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
627                                        GtkWidget *widget)
628 {
629         GSList *msg_list = NULL;
630
631         if (msgview->msginfo)
632                 msg_list = g_slist_append(msg_list, msgview->msginfo);
633         message_actions_execute(msgview, action_nb, msg_list);
634         g_slist_free(msg_list);
635 }
636
637 static void message_actions_execute(MessageView *msgview, guint action_nb,
638                                     GSList *msg_list)
639 {
640         TextView *textview;
641         MimeInfo *partinfo;
642         gchar *buf;
643         gchar *action;
644         GtkWidget *text = NULL;
645         guint body_pos = 0;
646         ActionType action_type;
647         
648         cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
649
650         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
651
652         cm_return_if_fail(buf);
653         cm_return_if_fail((action = strstr(buf, ": ")));
654
655         /* Point to the beginning of the command-line */
656         action += 2;
657
658         textview = messageview_get_current_textview(msgview);
659         if (textview) {
660                 text     = textview->text;
661                 body_pos = textview->body_pos;
662         }
663         partinfo = messageview_get_selected_mime_part(msgview);
664
665         /* this command will alter the message text */
666         action_type = action_get_type(action);
667         if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
668                 msgview->filtered = TRUE;
669
670         if (action_type & ACTION_FILTERING_ACTION) 
671                 /* CLAWS: most of the above code is not necessary for applying
672                  * filtering */
673                 execute_filtering_actions(action, msg_list);
674         else
675                 execute_actions(action, msg_list, text, body_pos, partinfo,
676                         NULL, NULL);
677 }
678
679 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
680 {
681         GSList *action_list, *p;
682         const gchar *sbegin, *send;
683         gchar *action_string;
684         SummaryView *summaryview = NULL;
685         MainWindow *mainwin = NULL;
686
687         if (mainwindow_get_mainwindow()) {
688                 summaryview = mainwindow_get_mainwindow()->summaryview;
689                 mainwin = mainwindow_get_mainwindow();
690         }
691
692         if (NULL == (sbegin = strstr2(action, "%as{")))
693                 return FALSE;
694         sbegin += sizeof "%as{" - 1;
695         if (NULL == (send = strrchr(sbegin, '}')))
696                 return FALSE;
697         action_string = g_strndup(sbegin, send - sbegin);
698         
699         action_list = matcher_parser_get_action_list(action_string);
700         if (action_list == NULL) {
701                 gchar *tmp = g_strdup(action_string);
702
703                 g_strstrip(tmp);
704                 if (*tmp == '\0')
705                         alertpanel_error(_("There is no filtering action set"));
706                 else
707                         alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
708                 g_free(action_string);
709                 g_free(tmp);
710                 return FALSE;
711         }
712         g_free(action_string);
713         
714         /* apply actions on each message info */
715         for (p = msglist; p && p->data; p = g_slist_next(p)) {
716                 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
717         }
718
719         if (summaryview) {
720                 summary_lock(summaryview);                              
721                 main_window_cursor_wait(mainwin);               
722                 summary_freeze(summaryview);    
723                 folder_item_update_freeze();                            
724         }
725
726         filtering_move_and_copy_msgs(msglist);
727
728         if (summaryview) {
729                 folder_item_update_thaw();                              
730                 summary_thaw(summaryview);              
731                 main_window_cursor_normal(mainwin);     
732                 summary_unlock(summaryview);                            
733                 summary_show(summaryview, summaryview->folder_item);
734         }
735         for (p = action_list; p; p = g_slist_next(p))
736                 if (p->data) filteringaction_free(p->data);     
737         g_slist_free(action_list);              
738         return TRUE;    
739 }
740
741 static gboolean execute_actions(gchar *action, GSList *msg_list,
742                                 GtkWidget *text,
743                                 gint body_pos, MimeInfo *partinfo,
744                                 void (*callback)(void *data), void *data)
745 {
746         GSList *children_list = NULL;
747         gint is_ok  = TRUE;
748         gint msg_list_len;
749         Children *children;
750         ChildInfo *child_info;
751         ActionType action_type;
752         MsgInfo *msginfo;
753         gchar *cmd;
754         gchar *sel_str = NULL;
755         gchar *msg_str = NULL;
756         gchar *user_str = NULL;
757         gchar *user_hidden_str = NULL;
758         GtkTextIter start_iter, end_iter;
759         gboolean is_selection = FALSE;
760
761         cm_return_val_if_fail(action && *action, FALSE);
762
763         action_type = action_get_type(action);
764
765         if (action_type == ACTION_ERROR)
766                 return FALSE;         /* ERR: syntax error */
767
768         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
769                 return FALSE;         /* ERR: file command without selection */
770
771         msg_list_len = g_slist_length(msg_list);
772
773         if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
774                 if (msg_list_len > 1)
775                         return FALSE; /* ERR: pipe + multiple selection */
776                 if (!text)
777                         return FALSE; /* ERR: pipe and no displayed text */
778         }
779
780         if (action_type & ACTION_SELECTION_STR) {
781                 if (!text)
782                         return FALSE; /* ERR: selection string but no text */
783         }
784
785         if (text) {
786                 GtkTextBuffer *textbuf;
787
788                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
789                 is_selection = gtk_text_buffer_get_selection_bounds
790                         (textbuf, &start_iter, &end_iter);
791                 if (!is_selection) {
792                         gtk_text_buffer_get_iter_at_offset
793                                 (textbuf, &start_iter, body_pos);
794                         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
795                 }
796                 msg_str = gtk_text_buffer_get_text
797                         (textbuf, &start_iter, &end_iter, FALSE);
798                 if (is_selection)
799                         sel_str = g_strdup(msg_str);
800         }
801
802         if (action_type & ACTION_USER_STR) {
803                 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
804                         g_free(msg_str);
805                         g_free(sel_str);
806                         return FALSE;
807                 }
808         }
809
810         if (action_type & ACTION_USER_HIDDEN_STR) {
811                 if (!(user_hidden_str =
812                         get_user_string(action, ACTION_USER_HIDDEN_STR))) {
813                         g_free(msg_str);
814                         g_free(sel_str);
815                         g_free(user_str);
816                         return FALSE;
817                 }
818         }
819
820         if (text && (action_type & ACTION_PIPE_OUT)) {
821                 GtkTextBuffer *textbuf;
822                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
823                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
824         }
825
826         children = g_new0(Children, 1);
827
828         children->action      = g_strdup(action);
829         children->action_type = action_type;
830         children->msg_text    = text;
831         children->is_selection = is_selection;
832
833         if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
834             ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
835                 children->open_in = 1;
836
837         if (action_type & ACTION_SINGLE) {
838                 GSList *cur;
839
840                 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
841                         msginfo = (MsgInfo *)cur->data;
842                         if (!msginfo) {
843                                 is_ok  = FALSE; /* ERR: msginfo missing */
844                                 break;
845                         }
846                         cmd = parse_action_cmd(action, msginfo, msg_list,
847                                                partinfo, user_str,
848                                                user_hidden_str, sel_str);
849                         if (!cmd) {
850                                 debug_print("Action command error\n");
851                                 is_ok  = FALSE; /* ERR: incorrect command */
852                                 break;
853                         }
854                         if ((child_info = fork_child(cmd, msg_str, children))) {
855                                 /* Pass msginfo to catch_status () */
856                                 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
857                                         child_info->msginfo_list = 
858                                                 g_slist_append (NULL, msginfo);
859                                 children_list = g_slist_append(children_list,
860                                                                child_info);
861                         }
862                         g_free(cmd);
863                 }
864         } else {
865                 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
866                                        user_str, user_hidden_str, sel_str);
867                 if (cmd) {
868                         if ((child_info = fork_child(cmd, msg_str, children))) {
869                                 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
870                                         child_info->msginfo_list = 
871                                                 g_slist_copy (msg_list);
872                                 children_list = g_slist_append(children_list,
873                                                                 child_info);
874                         }
875                         g_free(cmd);
876                 } else
877                         is_ok  = FALSE;         /* ERR: incorrect command */
878         }
879
880         g_free(msg_str);
881         g_free(sel_str);
882         g_free(user_str);
883         g_free(user_hidden_str);
884
885         if (!children_list) {
886                  /* If not waiting for children, return */
887                 free_children(children);
888         } else {
889                 GSList *cur;
890
891                 children->list        = children_list;
892                 children->nb          = g_slist_length(children_list);
893                 children->initial_nb  = children->nb;
894
895                 for (cur = children_list; cur; cur = cur->next) {
896                         child_info = (ChildInfo *) cur->data;
897                         child_info->callback = callback;
898                         child_info->data = data;
899                         child_info->tag_status = 
900                                 claws_input_add(child_info->chld_status,
901                                               GDK_INPUT_READ,
902                                               catch_status, child_info,
903                                               FALSE);
904                 }
905
906                 create_io_dialog(children);
907         }
908         return is_ok;
909 }
910
911 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
912                              Children *children)
913 {
914 #ifdef G_OS_UNIX
915         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
916         gchar *cmdline[4], *ret_str;
917         pid_t pid, gch_pid;
918         ChildInfo *child_info;
919         gint sync;
920         gssize by_written = 0, by_read = 0;
921
922         sync = !(children->action_type & ACTION_ASYNC);
923
924         chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
925                 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
926
927         if (sync) {
928                 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
929                     pipe(chld_err)) {
930                         alertpanel_error(_("Command could not be started. "
931                                            "Pipe creation failed.\n%s"),
932                                         g_strerror(errno));
933                         /* Closing fd = -1 fails silently */
934                         (void)close(chld_in[0]);
935                         (void)close(chld_in[1]);
936                         (void)close(chld_out[0]);
937                         (void)close(chld_out[1]);
938                         (void)close(chld_err[0]);
939                         (void)close(chld_err[1]);
940                         (void)close(chld_status[0]);
941                         (void)close(chld_status[1]);
942                         return NULL; /* Pipe error */
943                 }
944         }
945
946         debug_print("Forking child and grandchild.\n");
947         debug_print("Executing: /bin/sh -c %s\n", cmd);
948
949         pid = fork();
950         if (pid == 0) { /* Child */
951                 int r = 0;
952                 if (setpgid(0, 0))
953                         perror("setpgid");
954
955                 gch_pid = fork();
956
957                 if (gch_pid == 0) {
958                         if (setpgid(0, getppid()))
959                                 perror("setpgid");
960
961                         if (sync) {
962                                 if (children->action_type &
963                                     (ACTION_PIPE_IN |
964                                      ACTION_USER_IN |
965                                      ACTION_USER_HIDDEN_IN)) {
966                                         r |= close(fileno(stdin));
967                                         (void) dup  (chld_in[0]);
968                                 }
969                                 r |= close(chld_in[0]);
970                                 r |= close(chld_in[1]);
971
972                                 r |= close(fileno(stdout));
973                                 (void) dup  (chld_out[1]);
974                                 r |= close(chld_out[0]);
975                                 r |= close(chld_out[1]);
976
977                                 r |= close(fileno(stderr));
978                                 (void) dup  (chld_err[1]);
979                                 r |= close(chld_err[0]);
980                                 r |= close(chld_err[1]);
981
982                                 if (r != 0)
983                                         debug_print("%s(%d)", strerror(errno), errno);
984                         }
985
986                         cmdline[0] = "sh";
987                         cmdline[1] = "-c";
988                         ret_str = g_locale_from_utf8(cmd, strlen(cmd),
989                                                      &by_read, &by_written,
990                                                      NULL);
991                         if (ret_str && by_written)
992                                 cmdline[2] = ret_str;
993                         else
994                                 cmdline[2] = cmd;
995                         cmdline[3] = NULL;
996                         execvp("/bin/sh", cmdline);
997
998                         perror("execvp");
999                         g_free(ret_str);
1000                         _exit(1);
1001                 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1002                         if (sync)
1003                                 r = write(chld_status[1], "1\n", 2);
1004                         if (r != 0)
1005                                 debug_print("%s(%d)", strerror(errno), errno);
1006                         perror("fork");
1007                         _exit(1);
1008                 } else { /* Child */
1009                         if (sync) {
1010                                 r |= close(chld_in[0]);
1011                                 r |= close(chld_in[1]);
1012                                 r |= close(chld_out[0]);
1013                                 r |= close(chld_out[1]);
1014                                 r |= close(chld_err[0]);
1015                                 r |= close(chld_err[1]);
1016                                 r |= close(chld_status[0]);
1017
1018                                 debug_print("Child: waiting for grandchild\n");
1019                                 r |= waitpid(gch_pid, NULL, 0);
1020                                 debug_print("Child: grandchild ended\n");
1021                                 r |= write(chld_status[1], "0\n", 2);
1022                                 r |= close(chld_status[1]);
1023
1024                                 if (r != 0)
1025                                         debug_print("%s(%d)", strerror(errno), errno);
1026                         }
1027                         _exit(0);
1028                 }
1029         } else if (pid < 0) { /* Fork error */
1030                 alertpanel_error(_("Could not fork to execute the following "
1031                                    "command:\n%s\n%s"),
1032                                  cmd, g_strerror(errno));
1033                 return NULL; 
1034         }
1035
1036         /* Parent */
1037
1038         if (!sync) {
1039                 waitpid(pid, NULL, 0);
1040                 return NULL;
1041         }
1042
1043         (void)close(chld_in[0]);
1044         if (!(children->action_type &
1045               (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1046                 (void)close(chld_in[1]);
1047         (void)close(chld_out[1]);
1048         (void)close(chld_err[1]);
1049         (void)close(chld_status[1]);
1050
1051         child_info = g_new0(ChildInfo, 1);
1052
1053         child_info->children    = children;
1054
1055         child_info->pid         = pid;
1056         child_info->next_sig    = SIGTERM;
1057         child_info->cmd         = g_strdup(cmd);
1058         child_info->new_out     = FALSE;
1059         child_info->output      = g_string_new(NULL);
1060         child_info->chld_in     =
1061                 (children->action_type &
1062                  (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1063                         ? chld_in [1] : -1;
1064         child_info->chld_out    = chld_out[0];
1065         child_info->chld_err    = chld_err[0];
1066         child_info->chld_status = chld_status[0];
1067         child_info->tag_in      = -1;
1068         child_info->tag_out     = claws_input_add(chld_out[0], GDK_INPUT_READ,
1069                                                 catch_output, child_info, FALSE);
1070         child_info->tag_err     = claws_input_add(chld_err[0], GDK_INPUT_READ,
1071                                                 catch_output, child_info, FALSE);
1072
1073         if (!(children->action_type &
1074               (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1075                 return child_info;
1076
1077         if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1078                 int r;
1079                 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1080                                              &by_read, &by_written, NULL);
1081                 if (ret_str && by_written) {
1082                         r = write(chld_in[1], ret_str, strlen(ret_str));
1083                         g_free(ret_str);
1084                 } else
1085                         r = write(chld_in[1], msg_str, strlen(msg_str));
1086                 if (!(children->action_type &
1087                       (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1088                         r = close(chld_in[1]);
1089                 child_info->chld_in = -1; /* No more input */
1090                 if (r != 0)
1091                         debug_print("%s(%d)", strerror(errno), errno);
1092         }
1093
1094         return child_info;
1095 #else
1096         return NULL;
1097 #endif /* G_OS_UNIX */
1098 }
1099
1100 static void kill_children_cb(GtkWidget *widget, gpointer data)
1101 {
1102 #ifdef G_OS_UNIX
1103         GSList *cur;
1104         Children *children = (Children *) data;
1105         ChildInfo *child_info;
1106
1107         for (cur = children->list; cur; cur = cur->next) {
1108                 child_info = (ChildInfo *)(cur->data);
1109                 debug_print("Killing child group id %d\n", child_info->pid);
1110                 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1111                         perror("kill");
1112                 child_info->next_sig = SIGKILL;
1113         }
1114 #endif /* G_OS_UNIX */
1115 }
1116
1117 static gint wait_for_children(Children *children)
1118 {
1119         gboolean new_output;
1120         ChildInfo *child_info;
1121         GSList *cur;
1122
1123         cur = children->list;
1124         new_output = FALSE;
1125         while (cur) {
1126                 child_info = (ChildInfo *)cur->data;
1127                 new_output |= child_info->new_out;
1128                 cur = cur->next;
1129         }
1130
1131         children->output |= new_output;
1132
1133         if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1134                 update_io_dialog(children);
1135
1136         if (children->nb)
1137                 return FALSE;
1138
1139         if (!children->dialog) {
1140                 free_children(children);
1141         } else if (!children->output) {
1142                 gtk_widget_destroy(children->dialog);
1143         }
1144
1145         return FALSE;
1146 }
1147
1148 static void send_input(GtkWidget *w, gpointer data)
1149 {
1150         Children *children = (Children *) data;
1151         ChildInfo *child_info = (ChildInfo *) children->list->data;
1152
1153         child_info->tag_in = claws_input_add(child_info->chld_in,
1154                                            GDK_INPUT_WRITE,
1155                                            catch_input, children, FALSE);
1156 }
1157
1158 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1159 {
1160         hide_io_dialog_cb(w, data);
1161         return TRUE;
1162 }
1163
1164 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1165 {
1166
1167         Children *children = (Children *)data;
1168
1169         if (!children->nb) {
1170                 g_signal_handlers_disconnect_matched
1171                         (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1172                          0, 0, NULL, NULL, children);
1173                 gtk_widget_destroy(children->dialog);
1174                 free_children(children);
1175         }
1176 }
1177
1178 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1179                                      gpointer data)
1180 {
1181         if (event && (event->keyval == GDK_Escape ||
1182                       event->keyval == GDK_Return ||
1183                           event->keyval == GDK_KP_Enter))
1184                 hide_io_dialog_cb(widget, data);
1185         return TRUE;
1186 }
1187
1188 static void childinfo_close_pipes(ChildInfo *child_info)
1189 {
1190         /* stdout and stderr pipes are guaranteed to be removed by
1191          * their handler, but in case where we receive child exit notification
1192          * before grand-child's pipes closing signals, we check them and close
1193          * them if necessary
1194          */
1195         if (child_info->tag_in > 0)
1196                 g_source_remove(child_info->tag_in);
1197         if (child_info->tag_out > 0)
1198                 g_source_remove(child_info->tag_out);
1199         if (child_info->tag_err > 0)
1200                 g_source_remove(child_info->tag_err);
1201
1202         if (child_info->chld_in >= 0)
1203                 (void)close(child_info->chld_in);
1204         if (child_info->chld_out >= 0)
1205                 (void)close(child_info->chld_out);
1206         if (child_info->chld_err >= 0)
1207                 (void)close(child_info->chld_err);
1208
1209         (void)close(child_info->chld_status);
1210 }
1211
1212 static void free_children(Children *children)
1213 {
1214         ChildInfo *child_info;
1215         void (*callback)(void *data) = NULL;
1216         void *data = NULL;
1217
1218         debug_print("Freeing children data %p\n", children);
1219
1220         g_free(children->action);
1221         while (children->list != NULL) {
1222                 child_info = (ChildInfo *)children->list->data;
1223                 g_free(child_info->cmd);
1224                 g_string_free(child_info->output, TRUE);
1225                 children->list = g_slist_remove(children->list, child_info);
1226                 callback = child_info->callback;
1227                 data = child_info->data;
1228                 g_free(child_info);
1229         }
1230
1231         if (callback)
1232                 callback(data);
1233         
1234         g_free(children);
1235 }
1236
1237 static void update_io_dialog(Children *children)
1238 {
1239         GSList *cur;
1240
1241         debug_print("Updating actions input/output dialog.\n");
1242
1243         if (children->progress_bar) {
1244                 gchar *text;
1245 #ifdef GENERIC_UMPC
1246                 /* use a more compact format */
1247                 const gchar *format = "%s %d/%d";
1248 #else
1249                 const gchar *format = "%s %d / %d";
1250 #endif
1251                 
1252                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1253                                                   (children->initial_nb == 0) ? 0 :
1254                                               (gdouble) (children->initial_nb - children->nb) /
1255                                               (gdouble) children->initial_nb);
1256                 text = g_strdup_printf(format, _("Completed"), 
1257                                        children->initial_nb - children->nb,
1258                                        children->initial_nb);
1259                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1260                 g_free(text);
1261         }
1262
1263         if (!children->nb) {
1264                 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1265                 gtk_widget_set_sensitive(children->close_btn, TRUE);
1266                 if (children->input_hbox)
1267                         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1268                 gtk_widget_grab_focus(children->close_btn);
1269                 g_signal_connect(G_OBJECT(children->dialog),
1270                                  "key_press_event",
1271                                  G_CALLBACK(io_dialog_key_pressed_cb),
1272                                  children);
1273         }
1274
1275         if (children->output) {
1276                 GtkWidget *text = children->text;
1277                 GtkTextBuffer *textbuf;
1278                 GtkTextIter iter, start_iter, end_iter;
1279                 gchar *caption;
1280                 ChildInfo *child_info;
1281
1282                 gtk_widget_show(children->scrolledwin);
1283                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1284                 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1285                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1286                 gtk_text_buffer_get_start_iter(textbuf, &iter);
1287
1288                 for (cur = children->list; cur; cur = cur->next) {
1289                         child_info = (ChildInfo *)cur->data;
1290                         if (child_info->pid)
1291                                 caption = g_strdup_printf
1292                                         (_("--- Running: %s\n"),
1293                                          child_info->cmd);
1294                         else
1295                                 caption = g_strdup_printf
1296                                         (_("--- Ended: %s\n"),
1297                                          child_info->cmd);
1298
1299                         gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1300                         gtk_text_buffer_insert(textbuf, &iter,
1301                                                child_info->output->str, -1);
1302                         g_free(caption);
1303                         child_info->new_out = FALSE;
1304                 }
1305         }
1306 }
1307
1308 static void create_io_dialog(Children *children)
1309 {
1310         GtkWidget *dialog;
1311         GtkWidget *vbox;
1312         GtkWidget *entry = NULL;
1313         GtkWidget *input_hbox = NULL;
1314         GtkWidget *send_button;
1315         GtkWidget *label;
1316         GtkWidget *text;
1317         GtkWidget *scrolledwin;
1318         GtkWidget *hbox;
1319         GtkWidget *progress_bar = NULL;
1320         GtkWidget *abort_button;
1321         GtkWidget *close_button;
1322
1323         debug_print("Creating action IO dialog\n");
1324
1325         dialog = gtk_dialog_new();
1326         gtk_container_set_border_width
1327                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1328         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1329         gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1330         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1331         manage_window_set_transient(GTK_WINDOW(dialog));
1332         g_signal_connect(G_OBJECT(dialog), "delete_event",
1333                          G_CALLBACK(delete_io_dialog_cb), children);
1334         g_signal_connect(G_OBJECT(dialog), "destroy",
1335                          G_CALLBACK(hide_io_dialog_cb),
1336                          children);
1337
1338         vbox = gtk_vbox_new(FALSE, 8);
1339         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1340         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1341         gtk_widget_show(vbox);
1342
1343         label = gtk_label_new(children->action);
1344         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1345         gtk_widget_show(label);
1346
1347         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1348         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1349                                        GTK_POLICY_AUTOMATIC,
1350                                        GTK_POLICY_AUTOMATIC);
1351         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1352                                             GTK_SHADOW_IN);
1353         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1354         gtk_widget_set_size_request(scrolledwin, 560, 200);
1355         gtk_widget_hide(scrolledwin);
1356
1357         text = gtk_text_view_new();
1358
1359         if (prefs_common.textfont) {
1360                 PangoFontDescription *font_desc;
1361                 font_desc = pango_font_description_from_string
1362                         (prefs_common.textfont);
1363                 if (font_desc) {
1364                         gtk_widget_modify_font(text, font_desc);
1365                         pango_font_description_free(font_desc);
1366                 }
1367         }
1368
1369         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1370         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1371         gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1372         gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1373         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1374         gtk_widget_show(text);
1375
1376         if (children->open_in) {
1377                 input_hbox = gtk_hbox_new(FALSE, 8);
1378                 gtk_widget_show(input_hbox);
1379
1380                 entry = gtk_entry_new();
1381                 gtk_widget_set_size_request(entry, 320, -1);
1382                 g_signal_connect(G_OBJECT(entry), "activate",
1383                                  G_CALLBACK(send_input), children);
1384                 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1385                 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1386                         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1387 #ifdef MAEMO
1388                         hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
1389                                 HILDON_GTK_INPUT_MODE_FULL | 
1390                                 HILDON_GTK_INPUT_MODE_INVISIBLE);
1391 #endif
1392                 }
1393                 gtk_widget_show(entry);
1394
1395                 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1396                 g_signal_connect(G_OBJECT(send_button), "clicked",
1397                                  G_CALLBACK(send_input), children);
1398                 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1399                                    FALSE, 0);
1400                 gtk_widget_show(send_button);
1401
1402                 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1403                 gtk_widget_grab_focus(entry);
1404         }
1405
1406         if (children->initial_nb > 1) {
1407                 gchar *text;
1408 #ifdef GENERIC_UMPC
1409                 /* use a more compact format */
1410                 const gchar *format = "%s 0/%d\n";
1411 #else
1412                 const gchar *format = "%s 0 / %d\n";
1413 #endif
1414                 
1415                 progress_bar = gtk_progress_bar_new();
1416                 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1417                                 GTK_PROGRESS_LEFT_TO_RIGHT);
1418                 text = g_strdup_printf(format, _("Completed"), 
1419                                        children->initial_nb);
1420                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1421                                           text);
1422                 g_free(text);
1423                 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1424                 gtk_widget_show(progress_bar);
1425         }
1426
1427         gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1428                                       &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1429         g_signal_connect(G_OBJECT(abort_button), "clicked",
1430                          G_CALLBACK(kill_children_cb), children);
1431         g_signal_connect(G_OBJECT(close_button), "clicked",
1432                          G_CALLBACK(hide_io_dialog_cb), children);
1433         gtk_widget_show(hbox);
1434
1435         if (children->nb)
1436                 gtk_widget_set_sensitive(close_button, FALSE);
1437
1438         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1439
1440         children->dialog       = dialog;
1441         children->scrolledwin  = scrolledwin;
1442         children->text         = text;
1443         children->input_hbox   = children->open_in ? input_hbox : NULL;
1444         children->input_entry  = children->open_in ? entry : NULL;
1445         children->progress_bar = progress_bar;
1446         children->abort_btn    = abort_button;
1447         children->close_btn    = close_button;
1448
1449         gtk_widget_show(dialog);
1450 }
1451
1452 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1453 {
1454         ChildInfo *child_info = (ChildInfo *)data;
1455         gchar buf;
1456         gint c;
1457
1458         g_source_remove(child_info->tag_status);
1459
1460         c = read(source, &buf, 1);
1461         debug_print("Child returned %c\n", buf);
1462
1463 #ifdef G_OS_UNIX
1464         waitpid(-child_info->pid, NULL, 0);
1465 #endif
1466         childinfo_close_pipes(child_info);
1467         child_info->pid = 0;
1468
1469         if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1470             && child_info->msginfo_list) {
1471                 /* Actions on message *files* might change size and
1472                 * time stamp, and thus invalidate the cache */
1473                 SummaryView *summaryview  = NULL;
1474                 GSList      *cur;
1475                 MsgInfo     *msginfo, *nmi;     /* newmsginfo */
1476                 char        *file;
1477                 gboolean     modified_something = FALSE;
1478                 FolderItem  *last_item = NULL;
1479                 if (mainwindow_get_mainwindow ())
1480                         summaryview = mainwindow_get_mainwindow ()->summaryview;
1481                 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1482                         msginfo = (MsgInfo *)cur->data;
1483                         if (!(msginfo && /* Stuff used valid? */
1484                             msginfo->folder && msginfo->folder->cache)) 
1485                                 continue;
1486                         file = procmsg_get_message_file_path (msginfo);
1487                         if (!file) 
1488                                 continue;
1489                         nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1490                         if (!nmi) 
1491                                 continue; /* Deleted? */
1492                         if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1493                                 nmi->folder = msginfo->folder;
1494                                 nmi->msgnum = msginfo->msgnum;
1495                                 msgcache_update_msg (msginfo->folder->cache, nmi);
1496                                 modified_something = TRUE;
1497                                 last_item = nmi->folder;
1498                         }
1499                         procmsg_msginfo_free (nmi);
1500                         if (summaryview && summaryview->displayed &&
1501                             summaryview->folder_item == msginfo->folder &&
1502                             summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1503                                 summary_redisplay_msg(summaryview);
1504                                         
1505                 }
1506                 if (modified_something && last_item && 
1507                     summaryview && summaryview->folder_item == last_item) {
1508                         summary_show (summaryview, summaryview->folder_item);
1509                 }
1510                 g_slist_free (child_info->msginfo_list);
1511                 child_info->msginfo_list = NULL;
1512         }
1513
1514         if (!child_info->pid)
1515                 child_info->children->nb--;
1516
1517         wait_for_children(child_info->children);
1518 }
1519         
1520 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1521 {
1522         Children *children = (Children *)data;
1523         ChildInfo *child_info = (ChildInfo *)children->list->data;
1524         gchar *input, *ret_str;
1525         gint c, count, len, r;
1526         gssize by_read = 0, by_written = 0;
1527
1528         debug_print("Sending input to grand child.\n");
1529         if (!(cond && GDK_INPUT_WRITE))
1530                 return;
1531
1532         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1533         gtk_widget_grab_focus(children->abort_btn);
1534
1535         g_source_remove(child_info->tag_in);
1536         child_info->tag_in = -1;
1537
1538         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1539                                        0, -1);
1540         ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1541                                      &by_written, NULL);
1542         if (ret_str && by_written) {
1543                 g_free(input);
1544                 input = ret_str;
1545         }
1546
1547         len = strlen(input);
1548         count = 0;
1549
1550         do {
1551                 c = write(child_info->chld_in, input + count, len - count);
1552                 if (c >= 0)
1553                         count += c;
1554         } while (c >= 0 && count < len);
1555
1556         if (c >= 0)
1557                 r = write(child_info->chld_in, "\n", 2);
1558
1559         g_free(input);
1560
1561         r = close(child_info->chld_in);
1562         if (r != 0)
1563                 debug_print("%s(%d)", strerror(errno), errno);
1564         child_info->chld_in = -1;
1565         debug_print("Input to grand child sent.\n");
1566 }
1567
1568 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1569 {
1570         ChildInfo *child_info = (ChildInfo *)data;
1571         gint c;
1572         gchar buf[BUFFSIZE];
1573
1574         debug_print("Catching grand child's output.\n");
1575         if (child_info->children->action_type &
1576             (ACTION_PIPE_OUT | ACTION_INSERT)
1577             && source == child_info->chld_out) {
1578                 GtkTextView *text =
1579                         GTK_TEXT_VIEW(child_info->children->msg_text);
1580                 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1581                 GtkTextIter iter;
1582                 GtkTextMark *mark;
1583                 gint ins_pos;
1584
1585                 mark = gtk_text_buffer_get_insert(textbuf);
1586                 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1587                 ins_pos = gtk_text_iter_get_offset(&iter);
1588
1589                 while (TRUE) {
1590                         gsize bytes_read = 0, bytes_written = 0;
1591                         gchar *ret_str;
1592
1593                         c = read(source, buf, sizeof(buf) - 1);
1594                         if (c == 0)
1595                                 break;
1596
1597                         ret_str = g_locale_to_utf8
1598                                 (buf, c, &bytes_read, &bytes_written, NULL);
1599                         if (ret_str && bytes_written > 0) {
1600                                 gtk_text_buffer_insert
1601                                         (textbuf, &iter, ret_str,
1602                                          -1);
1603                                 g_free(ret_str);
1604                         } else
1605                                 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1606                 }
1607
1608                 if (child_info->children->is_selection) {
1609                         GtkTextIter ins;
1610
1611                         gtk_text_buffer_get_iter_at_offset
1612                                 (textbuf, &ins, ins_pos);
1613                         gtk_text_buffer_select_range(textbuf, &ins, &iter);
1614                 }
1615         } else {
1616                 c = read(source, buf, sizeof(buf) - 1);
1617                 if (c > 0) {
1618                         gsize bytes_read = 0, bytes_written = 0;
1619                         gchar *ret_str;
1620
1621                         ret_str = g_locale_to_utf8
1622                                 (buf, c, &bytes_read, &bytes_written, NULL);
1623                         if (ret_str && bytes_written > 0) {
1624                                 g_string_append_len
1625                                         (child_info->output, ret_str,
1626                                          bytes_written);
1627                                 g_free(ret_str);
1628                         } else
1629                                 g_string_append_len(child_info->output, buf, c);
1630
1631                         child_info->new_out = TRUE;
1632                 }
1633         }
1634         if (c == 0) {
1635                 if (source == child_info->chld_out) {
1636                         g_source_remove(child_info->tag_out);
1637                         child_info->tag_out = -1;
1638                         (void)close(child_info->chld_out);
1639                         child_info->chld_out = -1;
1640                 } else {
1641                         g_source_remove(child_info->tag_err);
1642                         child_info->tag_err = -1;
1643                         (void)close(child_info->chld_err);
1644                         child_info->chld_err = -1;
1645                 }
1646         }
1647         
1648         wait_for_children(child_info->children);
1649 }
1650
1651 static gchar *get_user_string(const gchar *action, ActionType type)
1652 {
1653         gchar *message;
1654         gchar *user_str = NULL;
1655
1656         switch (type) {
1657         case ACTION_USER_HIDDEN_STR:
1658                 message = g_strdup_printf
1659                         (_("Enter the argument for the following action:\n"
1660                            "('%%h' will be replaced with the argument)\n"
1661                            "  %s"),
1662                          action);
1663                 user_str = input_dialog_with_invisible
1664                         (_("Action's hidden user argument"), message, NULL);
1665                 break;
1666         case ACTION_USER_STR:
1667                 message = g_strdup_printf
1668                         (_("Enter the argument for the following action:\n"
1669                            "('%%u' will be replaced with the argument)\n"
1670                            "  %s"),
1671                          action);
1672                 user_str = input_dialog
1673                         (_("Action's user argument"), message, NULL);
1674                 break;
1675         default:
1676                 g_warning("Unsupported action type %d", type);
1677         }
1678
1679         return user_str;
1680 }