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