cc3447523c9d0e41f4374cadc872a88df87bf578
[claws.git] / src / action.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto & The Sylpheed Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gdk/gdkx.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37 #include <unistd.h>
38
39 #include "intl.h"
40 #include "utils.h"
41 #include "gtkutils.h"
42 #include "manage_window.h"
43 #include "mainwindow.h"
44 #include "prefs_common.h"
45 #include "alertpanel.h"
46 #include "inputdialog.h"
47 #include "action.h"
48 #include "compose.h"
49 #include "procmsg.h"
50 #include "gtkstext.h"
51 #include "textview.h"
52 #include "matcher_parser.h" /* CLAWS */
53 #include "filtering.h"
54
55 typedef struct _Children                Children;
56 typedef struct _ChildInfo               ChildInfo;
57 typedef struct _UserStringDialog        UserStringDialog;
58
59 struct _Children
60 {
61         GtkWidget       *dialog;
62         GtkWidget       *text;
63         GtkWidget       *input_entry;
64         GtkWidget       *input_hbox;
65         GtkWidget       *progress_bar;
66         GtkWidget       *abort_btn;
67         GtkWidget       *close_btn;
68         GtkWidget       *scrolledwin;
69
70         gchar           *action;
71         ActionType       action_type;
72         GSList          *list;
73         gint             nb;
74         gint             initial_nb;
75         gint             open_in;
76         gboolean         output;
77
78         GtkWidget       *msg_text;
79         GdkFont         *msgfont;
80 };
81
82 struct _ChildInfo
83 {
84         Children        *children;
85         gchar           *cmd;
86         pid_t            pid;
87         gint             chld_in;
88         gint             chld_out;
89         gint             chld_err;
90         gint             chld_status;
91         gint             tag_in;
92         gint             tag_out;
93         gint             tag_err;
94         gint             tag_status;
95         gint             new_out;
96
97         GString         *output;
98 };
99
100 static void action_update_menu          (GtkItemFactory *ifactory,
101                                          gchar          *branch_path,
102                                          gpointer        callback,
103                                          gpointer        data);
104 static void compose_actions_execute_cb  (Compose        *compose,
105                                          guint           action_nb,
106                                          GtkWidget      *widget);
107 static void mainwin_actions_execute_cb  (MainWindow     *mainwin,
108                                          guint           action_nb,
109                                          GtkWidget      *widget);
110 static void msgview_actions_execute_cb  (MessageView    *msgview,
111                                          guint           action_nb,
112                                          GtkWidget      *widget);
113 static void message_actions_execute     (MessageView    *msgview,
114                                          guint           action_nb,
115                                          GSList         *msg_list);
116
117 static gboolean execute_filtering_actions(gchar         *action, 
118                                           GSList        *msglist);
119
120 static gboolean execute_actions         (gchar          *action, 
121                                          GSList         *msg_list, 
122                                          GtkWidget      *text,
123                                          GdkFont        *msgfont,
124                                          gint            body_pos,
125                                          MimeInfo       *partinfo);
126
127 static gchar *parse_action_cmd          (gchar          *action,
128                                          MsgInfo        *msginfo,
129                                          GSList         *msg_list,
130                                          MimeInfo       *partinfo,
131                                          const gchar    *user_str,
132                                          const gchar    *user_hidden_str,
133                                          const gchar    *sel_str);
134 static gboolean parse_append_filename   (GString        *cmd,
135                                          MsgInfo        *msginfo);
136
137 static gboolean parse_append_msgpart    (GString        *cmd,
138                                          MsgInfo        *msginfo,
139                                          MimeInfo       *partinfo);
140
141 static ChildInfo *fork_child            (gchar          *cmd,
142                                          const gchar    *msg_str,
143                                          Children       *children);
144
145 static gint wait_for_children           (Children       *children);
146
147 static void free_children               (Children       *children);
148
149 static void childinfo_close_pipes       (ChildInfo      *child_info);
150
151 static void create_io_dialog            (Children       *children);
152 static void update_io_dialog            (Children       *children);
153
154 static void hide_io_dialog_cb           (GtkWidget      *widget,
155                                          gpointer        data);
156 static gint io_dialog_key_pressed_cb    (GtkWidget      *widget,
157                                          GdkEventKey    *event,
158                                          gpointer        data);
159
160 static void catch_output                (gpointer                data,
161                                          gint                    source,
162                                          GdkInputCondition       cond);
163 static void catch_input                 (gpointer                data, 
164                                          gint                    source,
165                                          GdkInputCondition       cond);
166 static void catch_status                (gpointer                data,
167                                          gint                    source,
168                                          GdkInputCondition       cond);
169
170 static gchar *get_user_string           (const gchar    *action,
171                                          ActionType      type);
172
173
174 ActionType action_get_type(const gchar *action_str)
175 {
176         const gchar *p;
177         ActionType action_type = ACTION_NONE;
178
179         g_return_val_if_fail(action_str,  ACTION_ERROR);
180         g_return_val_if_fail(*action_str, ACTION_ERROR);
181
182         p = action_str;
183
184         if (p[0] == '|') {
185                 action_type |= ACTION_PIPE_IN;
186                 p++;
187         } else if (p[0] == '>') {
188                 action_type |= ACTION_USER_IN;
189                 p++;
190         } else if (p[0] == '*') {
191                 action_type |= ACTION_USER_HIDDEN_IN;
192                 p++;
193         }
194
195         if (p[0] == '\0')
196                 return ACTION_ERROR;
197
198         while (*p && action_type != ACTION_ERROR) {
199                 if (p[0] == '%' && p[1]) {
200                         switch (p[1]) {
201                         case 'a':
202                                 /* CLAWS: filtering action is a mutually exclusive
203                                  * action. we can enable others if needed later. we
204                                  * add ACTION_SINGLE | ACTION_MULTIPLE so it will
205                                  * only be executed from the main window toolbar */
206                                 if (p[2] == 's')  /* source messages */
207                                         action_type = ACTION_FILTERING_ACTION 
208                                                     | ACTION_SINGLE 
209                                                     | ACTION_MULTIPLE;
210                                 break;
211                         case 'f':
212                                 action_type |= ACTION_SINGLE;
213                                 break;
214                         case 'F':
215                                 action_type |= ACTION_MULTIPLE;
216                                 break;
217                         case 'p':
218                                 action_type |= ACTION_SINGLE;
219                                 break;
220                         case 's':
221                                 action_type |= ACTION_SELECTION_STR;
222                                 break;
223                         case 'u':
224                                 action_type |= ACTION_USER_STR;
225                                 break;
226                         case 'h':
227                                 action_type |= ACTION_USER_HIDDEN_STR;
228                                 break;
229                         default:
230                                 action_type = ACTION_ERROR;
231                                 break;
232                         }
233                 } else if (p[0] == '|') {
234                         if (p[1] == '\0')
235                                 action_type |= ACTION_PIPE_OUT;
236                 } else if (p[0] == '>') {
237                         if (p[1] == '\0')
238                                 action_type |= ACTION_INSERT;
239                 } else if (p[0] == '&') {
240                         if (p[1] == '\0')
241                                 action_type |= ACTION_ASYNC;
242                 }
243                 p++;
244         }
245
246         return action_type;
247 }
248
249 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
250                                GSList *msg_list, MimeInfo *partinfo,
251                                const gchar *user_str,
252                                const gchar *user_hidden_str,
253                                const gchar *sel_str)
254 {
255         GString *cmd;
256         gchar *p;
257         GSList *cur;
258         
259         p = action;
260         
261         if (p[0] == '|' || p[0] == '>' || p[0] == '*')
262                 p++;
263
264         cmd = g_string_sized_new(strlen(action));
265
266         while (p[0] &&
267                !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
268                 if (p[0] == '%' && p[1]) {
269                         switch (p[1]) {
270                         case 'f':
271                                 if (!parse_append_filename(cmd, msginfo)) {
272                                         g_string_free(cmd, TRUE);
273                                         return NULL;
274                                 }
275                                 p++;
276                                 break;
277                         case 'F':
278                                 for (cur = msg_list; cur != NULL;
279                                      cur = cur->next) {
280                                         MsgInfo *msg = (MsgInfo *)cur->data;
281
282                                         if (!parse_append_filename(cmd, msg)) {
283                                                 g_string_free(cmd, TRUE);
284                                                 return NULL;
285                                         }
286                                         if (cur->next)
287                                                 g_string_append_c(cmd, ' ');
288                                 }
289                                 p++;
290                                 break;
291                         case 'p':
292                                 if (!parse_append_msgpart(cmd, msginfo,
293                                                           partinfo)) {
294                                         g_string_free(cmd, TRUE);
295                                         return NULL;
296                                 }
297                                 p++;
298                                 break;
299                         case 's':
300                                 if (sel_str)
301                                         g_string_append(cmd, sel_str);
302                                 p++;
303                                 break;
304                         case 'u':
305                                 if (user_str)
306                                         g_string_append(cmd, user_str);
307                                 p++;
308                                 break;
309                         case 'h':
310                                 if (user_hidden_str)
311                                         g_string_append(cmd, user_hidden_str);
312                                 p++;
313                                 break;
314                         default:
315                                 g_string_append_c(cmd, p[0]);
316                                 g_string_append_c(cmd, p[1]);
317                                 p++;
318                         }
319                 } else {
320                         g_string_append_c(cmd, p[0]);
321                 }
322                 p++;
323         }
324         if (cmd->len == 0) {
325                 g_string_free(cmd, TRUE);
326                 return NULL;
327         }
328
329         p = cmd->str;
330         g_string_free(cmd, FALSE);
331         return p;
332 }
333
334 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
335 {
336         gchar *filename;
337         gchar *p;
338
339         g_return_val_if_fail(msginfo, FALSE);
340
341         filename = procmsg_get_message_file(msginfo);
342
343         if (filename) {
344                 g_string_append_c(cmd, '"');
345                 for (p = filename; *p != '\0'; p++) {
346                         switch (*p) {
347                         case '$':
348                         case '"':
349                         case '`':
350                         case '\\':
351                                 g_string_append_c(cmd, '\\');
352                                 break;
353                         default:
354                                 break;
355                         }
356                         g_string_append_c(cmd, *p);
357                 }
358                 g_string_append_c(cmd, '"');
359                 g_free(filename);
360         } else {
361                 alertpanel_error(_("Could not get message file %d"),
362                                  msginfo->msgnum);
363                 return FALSE;
364         }
365
366         return TRUE;
367 }
368
369 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
370                                      MimeInfo *partinfo)
371 {
372         gboolean single_part = FALSE;
373         gchar *filename;
374         gchar *part_filename;
375         gint ret;
376
377         if (!partinfo) {
378                 partinfo = procmime_scan_message(msginfo);
379                 if (!partinfo) {
380                         alertpanel_error(_("Could not get message part."));
381                         return FALSE;
382                 }
383
384                 single_part = TRUE;
385         }
386
387         filename = procmsg_get_message_file_path(msginfo);
388         part_filename = procmime_get_tmp_file_name(partinfo);
389
390         ret = procmime_get_part(part_filename, partinfo);
391
392         if (single_part)
393                 procmime_mimeinfo_free_all(partinfo);
394         g_free(filename);
395
396         if (ret < 0) {
397                 alertpanel_error(_("Can't get part of multipart message"));
398                 g_free(part_filename);
399                 return FALSE;
400         }
401
402         g_string_append(cmd, part_filename);
403
404         g_free(part_filename);
405
406         return TRUE;
407 }
408
409 void actions_execute(gpointer data, 
410                      guint action_nb,
411                      GtkWidget *widget,
412                      gint source)
413 {
414         if (source == TOOLBAR_MAIN) 
415                 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
416         else if (source == TOOLBAR_COMPOSE)
417                 compose_actions_execute_cb((Compose*)data, action_nb, widget);
418         else if (source == TOOLBAR_MSGVIEW)
419                 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);      
420 }
421
422 void action_update_mainwin_menu(GtkItemFactory *ifactory,
423                                 gchar *branch_path,
424                                 MainWindow *mainwin)
425 {
426         action_update_menu(ifactory, branch_path,
427                            mainwin_actions_execute_cb, mainwin);
428 }
429
430 void action_update_msgview_menu(GtkItemFactory *ifactory,
431                                 gchar *branch_path,
432                                 MessageView *msgview)
433 {
434         action_update_menu(ifactory, branch_path,
435                            msgview_actions_execute_cb, msgview);
436 }
437
438 void action_update_compose_menu(GtkItemFactory *ifactory, 
439                                 gchar *branch_path,
440                                 Compose *compose)
441 {
442         action_update_menu(ifactory, branch_path,
443                            compose_actions_execute_cb, compose);
444 }
445
446 static void action_update_menu(GtkItemFactory *ifactory, 
447                                gchar *branch_path,
448                                gpointer callback, gpointer data)
449 {
450         GtkWidget *menuitem;
451         gchar *menu_path;
452         GSList *cur;
453         gchar *action, *action_p;
454         GList *amenu;
455         GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
456
457         ifentry.path = branch_path;
458         menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
459         g_return_if_fail(menuitem != NULL);
460
461         amenu = GTK_MENU_SHELL(menuitem)->children;
462         while (amenu != NULL) {
463                 GList *alist = amenu->next;
464                 gtk_widget_destroy(GTK_WIDGET(amenu->data));
465                 amenu = alist;
466         }
467
468         ifentry.accelerator     = NULL;
469         ifentry.callback_action = 0;
470         ifentry.callback        = callback;
471         ifentry.item_type       = NULL;
472
473         for (cur = prefs_common.actions_list; cur; cur = cur->next) {
474                 action   = g_strdup((gchar *)cur->data);
475                 action_p = strstr(action, ": ");
476                 if (action_p && action_p[2] &&
477                     action_get_type(&action_p[2]) != ACTION_ERROR) {
478                         action_p[0] = '\0';
479                         menu_path = g_strdup_printf("%s/%s", branch_path,
480                                                     action);
481                         ifentry.path = menu_path;
482                         gtk_item_factory_create_item(ifactory, &ifentry, data,
483                                                      1);
484                         g_free(menu_path);
485                 }
486                 g_free(action);
487                 ifentry.callback_action++;
488         }
489 }
490
491 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
492                                        GtkWidget *widget)
493 {
494         gchar *buf, *action;
495         ActionType action_type;
496
497         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
498
499         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
500         g_return_if_fail(buf != NULL);
501         action = strstr(buf, ": ");
502         g_return_if_fail(action != NULL);
503
504         /* Point to the beginning of the command-line */
505         action += 2;
506
507         action_type = action_get_type(action);
508         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
509                 alertpanel_warning
510                         (_("The selected action cannot be used in the compose window\n"
511                            "because it contains %%f, %%F, %%as or %%p."));
512                 return;
513         }
514
515         execute_actions(action, NULL, compose->text, NULL, 0, NULL);
516 }
517
518 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
519                                        GtkWidget *widget)
520 {
521         GSList *msg_list;
522
523         msg_list = summary_get_selected_msg_list(mainwin->summaryview);
524         message_actions_execute(mainwin->messageview, action_nb, msg_list);
525         g_slist_free(msg_list);
526 }
527
528 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
529                                        GtkWidget *widget)
530 {
531         GSList *msg_list = NULL;
532
533         if (msgview->msginfo)
534                 msg_list = g_slist_append(msg_list, msgview->msginfo);
535         message_actions_execute(msgview, action_nb, msg_list);
536         g_slist_free(msg_list);
537 }
538
539 static void message_actions_execute(MessageView *msgview, guint action_nb,
540                                     GSList *msg_list)
541 {
542         TextView *textview;
543         MimeInfo *partinfo;
544         gchar *buf;
545         gchar *action;
546         GtkWidget *text = NULL;
547         GdkFont *msgfont = NULL;
548         guint body_pos = 0;
549         ActionType action_type;
550         
551         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
552
553         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
554
555         g_return_if_fail(buf);
556         g_return_if_fail(action = strstr(buf, ": "));
557
558         /* Point to the beginning of the command-line */
559         action += 2;
560
561         textview = messageview_get_current_textview(msgview);
562         if (textview) {
563                 text     = textview->text;
564                 msgfont  = textview->msgfont;
565                 body_pos = textview->body_pos;
566         }
567         partinfo = messageview_get_selected_mime_part(msgview);
568
569         /* this command will alter the message text */
570         action_type = action_get_type(action);
571         if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
572                 msgview->filtered = TRUE;
573
574         if (action_type & ACTION_FILTERING_ACTION) 
575                 /* CLAWS: most of the above code is not necessary for applying
576                  * filtering */
577                 execute_filtering_actions(action, msg_list);
578         else
579                 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
580 }
581
582 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
583 {
584         GSList *action_list, *p;
585         const gchar *sbegin, *send;
586         gchar *action_string;
587         
588         if (NULL == (sbegin = strstr2(action, "%as{")))
589                 return FALSE;
590         sbegin += sizeof "%as{" - 1;
591         if (NULL == (send = strrchr(sbegin, '}')))
592                 return FALSE;
593         action_string = g_strndup(sbegin, send - sbegin);
594         
595         action_list = matcher_parser_get_action_list(action_string);
596         g_free(action_string);
597         if (action_list == NULL) return FALSE;
598         
599         /* apply actions on each message info */
600         for (p = msglist; p && p->data; p = g_slist_next(p))
601                 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
602                 
603         for (p = action_list; p; p = g_slist_next(p))
604                 if (p->data) filteringaction_free(p->data);     
605         g_slist_free(action_list);              
606         return TRUE;    
607 }
608
609 static gboolean execute_actions(gchar *action, GSList *msg_list,
610                                 GtkWidget *text, GdkFont *msgfont,
611                                 gint body_pos, MimeInfo *partinfo)
612 {
613         GSList *children_list = NULL;
614         gint is_ok  = TRUE;
615         gint msg_list_len;
616         Children *children;
617         ChildInfo *child_info;
618         ActionType action_type;
619         MsgInfo *msginfo;
620         gchar *cmd;
621         guint start = 0, end = 0;
622         gchar *sel_str = NULL;
623         gchar *msg_str = NULL;
624         gchar *user_str = NULL;
625         gchar *user_hidden_str = NULL;
626
627         g_return_val_if_fail(action && *action, FALSE);
628
629         action_type = action_get_type(action);
630
631         if (action_type == ACTION_ERROR)
632                 return FALSE;         /* ERR: syntax error */
633
634         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
635                 return FALSE;         /* ERR: file command without selection */
636
637         msg_list_len = g_slist_length(msg_list);
638
639         if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
640                 if (msg_list_len > 1)
641                         return FALSE; /* ERR: pipe + multiple selection */
642                 if (!text)
643                         return FALSE; /* ERR: pipe and no displayed text */
644         }
645
646         if (action_type & ACTION_SELECTION_STR) {
647                 if (!text)
648                         return FALSE; /* ERR: selection string but no text */
649         }
650
651         if (text) {
652                 if (GTK_EDITABLE(text)->has_selection) {
653                         start = GTK_EDITABLE(text)->selection_start_pos;
654                         end   = GTK_EDITABLE(text)->selection_end_pos;
655                         if (start > end) {
656                                 guint tmp;
657                                 tmp = start;
658                                 start = end;
659                                 end = tmp;
660                         }
661
662                         if (start == end) {
663                                 start = body_pos;
664                                 end = gtk_stext_get_length(GTK_STEXT(text));
665                                 msg_str = gtk_editable_get_chars
666                                         (GTK_EDITABLE(text), start, end);
667                         } else {
668                                 sel_str = gtk_editable_get_chars
669                                         (GTK_EDITABLE(text), start, end);
670                                 msg_str = g_strdup(sel_str);
671                         }
672                 } else {
673                         start = body_pos;
674                         end = gtk_stext_get_length(GTK_STEXT(text));
675                         msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
676                                                          start, end);
677                 }
678         }
679
680         if (action_type & ACTION_USER_STR) {
681                 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
682                         g_free(msg_str);
683                         g_free(sel_str);
684                         return FALSE;
685                 }
686         }
687
688         if (action_type & ACTION_USER_HIDDEN_STR) {
689                 if (!(user_hidden_str =
690                         get_user_string(action, ACTION_USER_HIDDEN_STR))) {
691                         g_free(msg_str);
692                         g_free(sel_str);
693                         g_free(user_str);
694                         return FALSE;
695                 }
696         }
697
698         if (action_type & ACTION_PIPE_OUT) {
699                 gtk_stext_freeze(GTK_STEXT(text));
700                 gtk_stext_set_point(GTK_STEXT(text), start);
701                 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
702                 gtk_stext_thaw(GTK_STEXT(text));
703         }
704
705         children = g_new0(Children, 1);
706
707         children->action      = g_strdup(action);
708         children->action_type = action_type;
709         children->msg_text    = text;
710         children->msgfont     = msgfont;
711
712         if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
713             ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
714                 children->open_in = 1;
715
716         if (action_type & ACTION_SINGLE) {
717                 GSList *cur;
718
719                 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
720                         msginfo = (MsgInfo *)cur->data;
721                         if (!msginfo) {
722                                 is_ok  = FALSE; /* ERR: msginfo missing */
723                                 break;
724                         }
725                         cmd = parse_action_cmd(action, msginfo, msg_list,
726                                                partinfo, user_str,
727                                                user_hidden_str, sel_str);
728                         if (!cmd) {
729                                 debug_print("Action command error\n");
730                                 is_ok  = FALSE; /* ERR: incorrect command */
731                                 break;
732                         }
733                         if ((child_info = fork_child(cmd, msg_str, children))) {
734                                 children_list = g_slist_append(children_list,
735                                                                child_info);
736                         }
737                         g_free(cmd);
738                 }
739         } else {
740                 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
741                                        user_str, user_hidden_str, sel_str);
742                 if (cmd) {
743                         if ((child_info = fork_child(cmd, msg_str, children))) {
744                                 children_list = g_slist_append(children_list,
745                                                                child_info);
746                         }
747                         g_free(cmd);
748                 } else
749                         is_ok  = FALSE;         /* ERR: incorrect command */
750         }
751
752         g_free(msg_str);
753         g_free(sel_str);
754         g_free(user_str);
755         g_free(user_hidden_str);
756
757         if (!children_list) {
758                  /* If not waiting for children, return */
759                 free_children(children);
760         } else {
761                 GSList *cur;
762
763                 children->list        = children_list;
764                 children->nb          = g_slist_length(children_list);
765                 children->initial_nb  = children->nb;
766
767                 for (cur = children_list; cur; cur = cur->next) {
768                         child_info = (ChildInfo *) cur->data;
769                         child_info->tag_status = 
770                                 gdk_input_add(child_info->chld_status,
771                                               GDK_INPUT_READ,
772                                               catch_status, child_info);
773                 }
774
775                 create_io_dialog(children);
776         }
777
778         return is_ok;
779 }
780
781 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
782                              Children *children)
783 {
784         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
785         gchar *cmdline[4];
786         pid_t pid, gch_pid;
787         ChildInfo *child_info;
788         gint sync;
789
790         sync = !(children->action_type & ACTION_ASYNC);
791
792         chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
793                 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
794
795         if (sync) {
796                 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
797                     pipe(chld_err)) {
798                         alertpanel_error(_("Command could not be started. "
799                                            "Pipe creation failed.\n%s"),
800                                         g_strerror(errno));
801                         /* Closing fd = -1 fails silently */
802                         close(chld_in[0]);
803                         close(chld_in[1]);
804                         close(chld_out[0]);
805                         close(chld_out[1]);
806                         close(chld_err[0]);
807                         close(chld_err[1]);
808                         close(chld_status[0]);
809                         close(chld_status[1]);
810                         return NULL; /* Pipe error */
811                 }
812         }
813
814         debug_print("Forking child and grandchild.\n");
815         debug_print("Executing: /bin/sh -c %s\n", cmd);
816
817         pid = fork();
818         if (pid == 0) { /* Child */
819                 if (setpgid(0, 0))
820                         perror("setpgid");
821
822                 close(ConnectionNumber(gdk_display));
823
824                 gch_pid = fork();
825
826                 if (gch_pid == 0) {
827                         if (setpgid(0, getppid()))
828                                 perror("setpgid");
829
830                         if (sync) {
831                                 if (children->action_type &
832                                     (ACTION_PIPE_IN |
833                                      ACTION_USER_IN |
834                                      ACTION_USER_HIDDEN_IN)) {
835                                         close(fileno(stdin));
836                                         dup  (chld_in[0]);
837                                 }
838                                 close(chld_in[0]);
839                                 close(chld_in[1]);
840
841                                 close(fileno(stdout));
842                                 dup  (chld_out[1]);
843                                 close(chld_out[0]);
844                                 close(chld_out[1]);
845
846                                 close(fileno(stderr));
847                                 dup  (chld_err[1]);
848                                 close(chld_err[0]);
849                                 close(chld_err[1]);
850                         }
851
852                         cmdline[0] = "sh";
853                         cmdline[1] = "-c";
854                         cmdline[2] = cmd;
855                         cmdline[3] = NULL;
856                         execvp("/bin/sh", cmdline);
857
858                         perror("execvp");
859                         _exit(1);
860                 } else if (gch_pid < (pid_t) 0) { /* Fork error */
861                         if (sync)
862                                 write(chld_status[1], "1\n", 2);
863                         perror("fork");
864                         _exit(1);
865                 } else { /* Child */
866                         if (sync) {
867                                 close(chld_in[0]);
868                                 close(chld_in[1]);
869                                 close(chld_out[0]);
870                                 close(chld_out[1]);
871                                 close(chld_err[0]);
872                                 close(chld_err[1]);
873                                 close(chld_status[0]);
874
875                                 debug_print("Child: Waiting for grandchild\n");
876                                 waitpid(gch_pid, NULL, 0);
877                                 debug_print("Child: grandchild ended\n");
878                                 write(chld_status[1], "0\n", 2);
879                                 close(chld_status[1]);
880                         }
881                         _exit(0);
882                 }
883         } else if (pid < 0) { /* Fork error */
884                 alertpanel_error(_("Could not fork to execute the following "
885                                    "command:\n%s\n%s"),
886                                  cmd, g_strerror(errno));
887                 return NULL; 
888         }
889
890         /* Parent */
891
892         if (!sync) {
893                 waitpid(pid, NULL, 0);
894                 return NULL;
895         }
896
897         close(chld_in[0]);
898         if (!(children->action_type &
899               (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
900                 close(chld_in[1]);
901         close(chld_out[1]);
902         close(chld_err[1]);
903         close(chld_status[1]);
904
905         child_info = g_new0(ChildInfo, 1);
906
907         child_info->children    = children;
908
909         child_info->pid         = pid;
910         child_info->cmd         = g_strdup(cmd);
911         child_info->new_out     = FALSE;
912         child_info->output      = g_string_new(NULL);
913         child_info->chld_in     =
914                 (children->action_type &
915                  (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
916                         ? chld_in [1] : -1;
917         child_info->chld_out    = chld_out[0];
918         child_info->chld_err    = chld_err[0];
919         child_info->chld_status = chld_status[0];
920         child_info->tag_in      = -1;
921         child_info->tag_out     = gdk_input_add(chld_out[0], GDK_INPUT_READ,
922                                                 catch_output, child_info);
923         child_info->tag_err     = gdk_input_add(chld_err[0], GDK_INPUT_READ,
924                                                 catch_output, child_info);
925
926         if (!(children->action_type &
927               (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
928                 return child_info;
929
930         if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
931                 write(chld_in[1], msg_str, strlen(msg_str));
932                 if (!(children->action_type &
933                       (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
934                         close(chld_in[1]);
935                 child_info->chld_in = -1; /* No more input */
936         }
937
938         return child_info;
939 }
940
941 static void kill_children_cb(GtkWidget *widget, gpointer data)
942 {
943         GSList *cur;
944         Children *children = (Children *) data;
945         ChildInfo *child_info;
946
947         for (cur = children->list; cur; cur = cur->next) {
948                 child_info = (ChildInfo *)(cur->data);
949                 debug_print("Killing child group id %d\n", child_info->pid);
950                 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
951                         perror("kill");
952         }
953 }
954
955 static gint wait_for_children(Children *children)
956 {
957         gboolean new_output;
958         ChildInfo *child_info;
959         GSList *cur;
960         gint nb = children->nb;
961
962         children->nb = 0;
963
964         cur = children->list;
965         new_output = FALSE;
966         while (cur) {
967                 child_info = (ChildInfo *)cur->data;
968                 if (child_info->pid)
969                         children->nb++;
970                 new_output |= child_info->new_out;
971                 cur = cur->next;
972         }
973
974         children->output |= new_output;
975
976         if (new_output || (children->dialog && (nb != children->nb)))
977                 update_io_dialog(children);
978
979         if (children->nb)
980                 return FALSE;
981
982         if (!children->dialog) {
983                 free_children(children);
984         } else if (!children->output) {
985                 gtk_widget_destroy(children->dialog);
986         }
987
988         return FALSE;
989 }
990
991 static void send_input(GtkWidget *w, gpointer data)
992 {
993         Children *children = (Children *) data;
994         ChildInfo *child_info = (ChildInfo *) children->list->data;
995
996         child_info->tag_in = gdk_input_add(child_info->chld_in,
997                                            GDK_INPUT_WRITE,
998                                            catch_input, children);
999         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1000 }
1001
1002 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1003 {
1004         hide_io_dialog_cb(w, data);
1005         return TRUE;
1006 }
1007
1008 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1009 {
1010
1011         Children *children = (Children *)data;
1012
1013         if (!children->nb) {
1014                 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1015                                               children);
1016                 gtk_widget_destroy(children->dialog);
1017                 free_children(children);
1018         }
1019 }
1020
1021 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1022                                      gpointer data)
1023 {
1024         if (event && event->keyval == GDK_Escape)
1025                 hide_io_dialog_cb(widget, data);
1026         return TRUE;
1027 }
1028
1029 static void childinfo_close_pipes(ChildInfo *child_info)
1030 {
1031         /* stdout and stderr pipes are guaranteed to be removed by
1032          * their handler, but in case where we receive child exit notification
1033          * before grand-child's pipes closing signals, we check them and close
1034          * them if necessary
1035          */
1036         if (child_info->tag_in > 0)
1037                 gdk_input_remove(child_info->tag_in);
1038         if (child_info->tag_out > 0)
1039                 gdk_input_remove(child_info->tag_out);
1040         if (child_info->tag_err > 0)
1041                 gdk_input_remove(child_info->tag_err);
1042
1043         if (child_info->chld_in >= 0)
1044                 close(child_info->chld_in);
1045         if (child_info->chld_out >= 0)
1046                 close(child_info->chld_out);
1047         if (child_info->chld_err >= 0)
1048                 close(child_info->chld_err);
1049
1050         close(child_info->chld_status);
1051 }
1052
1053 static void free_children(Children *children)
1054 {
1055         ChildInfo *child_info;
1056
1057         debug_print("Freeing children data %p\n", children);
1058
1059         g_free(children->action);
1060         while (children->list != NULL) {
1061                 child_info = (ChildInfo *)children->list->data;
1062                 g_free(child_info->cmd);
1063                 g_string_free(child_info->output, TRUE);
1064                 children->list = g_slist_remove(children->list, child_info);
1065                 g_free(child_info);
1066         }
1067         g_free(children);
1068 }
1069
1070 static void update_io_dialog(Children *children)
1071 {
1072         GSList *cur;
1073
1074         debug_print("Updating actions input/output dialog.\n");
1075
1076         if (children->progress_bar) {
1077                 gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
1078                                 children->initial_nb -children->nb,
1079                                 0.0, children->initial_nb);
1080         }
1081
1082         if (!children->nb) {
1083                 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1084                 gtk_widget_set_sensitive(children->close_btn, TRUE);
1085                 if (children->input_hbox)
1086                         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1087                 gtk_widget_grab_focus(children->close_btn);
1088                 gtk_signal_connect(GTK_OBJECT(children->dialog),
1089                                    "key_press_event",
1090                                    GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1091                                    children);
1092         }
1093
1094         if (children->output) {
1095                 GtkWidget *text = children->text;
1096                 gchar *caption;
1097                 ChildInfo *child_info;
1098                 GdkFont *font;
1099
1100                 font = gtk_object_get_data(GTK_OBJECT(children->dialog), 
1101                                            "s_txtfont");
1102                 gtk_widget_show(children->scrolledwin);
1103                 gtk_text_freeze(GTK_TEXT(text));
1104                 gtk_text_set_point(GTK_TEXT(text), 0);
1105                 gtk_text_forward_delete(GTK_TEXT(text), 
1106                                         gtk_text_get_length(GTK_TEXT(text)));
1107                 for (cur = children->list; cur; cur = cur->next) {
1108                         child_info = (ChildInfo *)cur->data;
1109                         if (child_info->pid)
1110                                 caption = g_strdup_printf
1111                                         (_("--- Running: %s\n"),
1112                                          child_info->cmd);
1113                         else
1114                                 caption = g_strdup_printf
1115                                         (_("--- Ended: %s\n"),
1116                                          child_info->cmd);
1117
1118                         gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1119                                         caption, -1);
1120                         gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1121                                         child_info->output->str, -1);
1122                         g_free(caption);
1123                         child_info->new_out = FALSE;
1124                 }
1125                 gtk_text_thaw(GTK_TEXT(text));
1126         }
1127 }
1128
1129 static void create_io_dialog(Children *children)
1130 {
1131         GtkWidget *dialog;
1132         GtkWidget *vbox;
1133         GtkWidget *entry = NULL;
1134         GtkWidget *input_hbox = NULL;
1135         GtkWidget *send_button;
1136         GtkWidget *label;
1137         GtkWidget *text;
1138         GtkWidget *scrolledwin;
1139         GtkWidget *hbox;
1140         GtkWidget *progress_bar = NULL;
1141         GtkWidget *abort_button;
1142         GtkWidget *close_button;
1143         GdkFont   *output_font;
1144
1145         debug_print("Creating action IO dialog\n");
1146
1147         dialog = gtk_dialog_new();
1148         gtk_container_set_border_width
1149                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1150         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1151         gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1152         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1153         manage_window_set_transient(GTK_WINDOW(dialog));
1154         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1155                         GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1156         gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1157                         GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1158                         children);
1159
1160         vbox = gtk_vbox_new(FALSE, 8);
1161         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1162         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1163         gtk_widget_show(vbox);
1164
1165         label = gtk_label_new(children->action);
1166         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1167         gtk_widget_show(label);
1168
1169         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1170         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1171                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1172         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1173         gtk_widget_set_usize(scrolledwin, 480, 200);
1174         gtk_widget_hide(scrolledwin);
1175
1176         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1177                             (GTK_SCROLLED_WINDOW(scrolledwin)),
1178                             gtk_scrolled_window_get_vadjustment
1179                             (GTK_SCROLLED_WINDOW(scrolledwin)));
1180         gtk_text_set_editable(GTK_TEXT(text), FALSE);
1181         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1182         gtk_widget_show(text);
1183
1184         if (children->open_in) {
1185                 input_hbox = gtk_hbox_new(FALSE, 8);
1186                 gtk_widget_show(input_hbox);
1187
1188                 entry = gtk_entry_new();
1189                 gtk_widget_set_usize(entry, 320, -1);
1190                 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1191                                    GTK_SIGNAL_FUNC(send_input), children);
1192                 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1193                 if (children->action_type & ACTION_USER_HIDDEN_IN)
1194                         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1195                 gtk_widget_show(entry);
1196
1197                 send_button = gtk_button_new_with_label(_(" Send "));
1198                 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1199                                    GTK_SIGNAL_FUNC(send_input), children);
1200                 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1201                                    FALSE, 0);
1202                 gtk_widget_show(send_button);
1203
1204                 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1205                 gtk_widget_grab_focus(entry);
1206         }
1207
1208         if (children->initial_nb > 1) {
1209                 progress_bar = gtk_progress_bar_new();
1210                 gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
1211                                 GTK_PROGRESS_CONTINUOUS);
1212                 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1213                                 GTK_PROGRESS_LEFT_TO_RIGHT);
1214                 gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar), 
1215                                 FALSE);
1216                 gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
1217                                 _("Completed %v/%u"));
1218                 gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
1219                 gtk_progress_configure(GTK_PROGRESS(progress_bar),
1220                                 children->initial_nb -children->nb,
1221                                 0.0, children->initial_nb);
1222
1223                 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1224                 gtk_widget_show(progress_bar);
1225         }
1226
1227         gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1228                                 &close_button, _("Close"), NULL, NULL);
1229         gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1230                         GTK_SIGNAL_FUNC(kill_children_cb), children);
1231         gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1232                         GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1233         gtk_widget_show(hbox);
1234
1235         if (children->nb)
1236                 gtk_widget_set_sensitive(close_button, FALSE);
1237
1238         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1239
1240         children->dialog       = dialog;
1241         children->scrolledwin  = scrolledwin;
1242         children->text         = text;
1243         children->input_hbox   = children->open_in ? input_hbox : NULL;
1244         children->input_entry  = children->open_in ? entry : NULL;
1245         children->progress_bar = progress_bar;
1246         children->abort_btn    = abort_button;
1247         children->close_btn    = close_button;
1248
1249         output_font = gtkut_font_load_from_fontset(prefs_common.textfont);
1250         gtk_object_set_data_full(GTK_OBJECT(dialog), "s_txtfont",
1251                                  output_font, 
1252                                  (GtkDestroyNotify)gdk_font_unref); 
1253         gtk_widget_show(dialog);
1254 }
1255
1256 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1257 {
1258         ChildInfo *child_info = (ChildInfo *)data;
1259         gchar buf;
1260         gint c;
1261
1262         gdk_input_remove(child_info->tag_status);
1263
1264         c = read(source, &buf, 1);
1265         debug_print("Child returned %c\n", buf);
1266
1267         waitpid(-child_info->pid, NULL, 0);
1268         childinfo_close_pipes(child_info);
1269         child_info->pid = 0;
1270
1271         wait_for_children(child_info->children);
1272 }
1273         
1274 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1275 {
1276         Children *children = (Children *)data;
1277         ChildInfo *child_info = (ChildInfo *)children->list->data;
1278         gchar *input;
1279         gint c, count, len;
1280
1281         debug_print("Sending input to grand child.\n");
1282         if (!(cond && GDK_INPUT_WRITE))
1283                 return;
1284
1285         gdk_input_remove(child_info->tag_in);
1286         child_info->tag_in = -1;
1287
1288         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1289                                        0, -1);
1290         len = strlen(input);
1291         count = 0;
1292
1293         do {
1294                 c = write(child_info->chld_in, input + count, len - count);
1295                 if (c >= 0)
1296                         count += c;
1297         } while (c >= 0 && count < len);
1298
1299         if (c >= 0)
1300                 write(child_info->chld_in, "\n", 2);
1301
1302         g_free(input);
1303
1304         gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1305         gtk_widget_set_sensitive(children->input_hbox, TRUE);
1306         close(child_info->chld_in);
1307         child_info->chld_in = -1;
1308         debug_print("Input to grand child sent.\n");
1309 }
1310
1311 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1312 {
1313         ChildInfo *child_info = (ChildInfo *)data;
1314         gint c, i;
1315         gchar buf[BUFFSIZE];
1316
1317         debug_print("Catching grand child's output.\n");
1318         if (child_info->children->action_type &
1319             (ACTION_PIPE_OUT | ACTION_INSERT)
1320             && source == child_info->chld_out) {
1321                 gboolean is_selection = FALSE;
1322                 GtkWidget *text = child_info->children->msg_text;
1323
1324                 if (GTK_EDITABLE(text)->has_selection)
1325                         is_selection = TRUE;
1326                 gtk_stext_freeze(GTK_STEXT(text));
1327                 while (TRUE) {
1328                         c = read(source, buf, sizeof(buf) - 1);
1329                         if (c == 0)
1330                                 break;
1331                         gtk_stext_insert(GTK_STEXT(text),
1332                                          child_info->children->msgfont,
1333                                          NULL, NULL, buf, c);
1334                 }
1335                 if (is_selection) {
1336                         /* Using the select_region draws things. Should not.
1337                          * so we just change selection position and 
1338                          * defere drawing when thawing. Hack?
1339                          */
1340                         GTK_EDITABLE(text)->selection_end_pos =
1341                                         gtk_stext_get_point(GTK_STEXT(text));
1342                 }
1343                 gtk_stext_thaw(GTK_STEXT(text));
1344         } else {
1345                 c = read(source, buf, sizeof(buf) - 1);
1346                 for (i = 0; i < c; i++)
1347                         g_string_append_c(child_info->output, buf[i]);
1348                 if (c > 0)
1349                         child_info->new_out = TRUE;
1350         }
1351         if (c == 0) {
1352                 if (source == child_info->chld_out) {
1353                         gdk_input_remove(child_info->tag_out);
1354                         child_info->tag_out = -1;
1355                         close(child_info->chld_out);
1356                         child_info->chld_out = -1;
1357                 } else {
1358                         gdk_input_remove(child_info->tag_err);
1359                         child_info->tag_err = -1;
1360                         close(child_info->chld_err);
1361                         child_info->chld_err = -1;
1362                 }
1363         }
1364         
1365         wait_for_children(child_info->children);
1366 }
1367
1368 static gchar *get_user_string(const gchar *action, ActionType type)
1369 {
1370         gchar *message;
1371         gchar *user_str = NULL;
1372
1373         switch (type) {
1374         case ACTION_USER_HIDDEN_STR:
1375                 message = g_strdup_printf
1376                         (_("Enter the argument for the following action:\n"
1377                            "(`%%h' will be replaced with the argument)\n"
1378                            "  %s"),
1379                          action);
1380                 user_str = input_dialog_with_invisible
1381                         (_("Action's hidden user argument"), message, NULL);
1382                 break;
1383         case ACTION_USER_STR:
1384                 message = g_strdup_printf
1385                         (_("Enter the argument for the following action:\n"
1386                            "(`%%u' will be replaced with the argument)\n"
1387                            "  %s"),
1388                          action);
1389                 user_str = input_dialog
1390                         (_("Action's user argument"), message, NULL);
1391                 break;
1392         default:
1393                 g_warning("Unsupported action type %d", type);
1394         }
1395
1396         return user_str;
1397 }