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