got back the function addressbook_browse_entry_cb() that is needed in case we compile...
[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
338         g_return_val_if_fail(msginfo, FALSE);
339
340         filename = procmsg_get_message_file(msginfo);
341
342         if (filename) {
343                 g_string_append(cmd, filename);
344                 g_free(filename);
345         } else {
346                 alertpanel_error(_("Could not get message file %d"),
347                                  msginfo->msgnum);
348                 return FALSE;
349         }
350
351         return TRUE;
352 }
353
354 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
355                                      MimeInfo *partinfo)
356 {
357         gboolean single_part = FALSE;
358         gchar *filename;
359         gchar *part_filename;
360         gint ret;
361
362         if (!partinfo) {
363                 partinfo = procmime_scan_message(msginfo);
364                 if (!partinfo) {
365                         alertpanel_error(_("Could not get message part."));
366                         return FALSE;
367                 }
368
369                 single_part = TRUE;
370         }
371
372         filename = procmsg_get_message_file_path(msginfo);
373         part_filename = procmime_get_tmp_file_name(partinfo);
374
375         ret = procmime_get_part(part_filename, partinfo);
376
377         if (single_part)
378                 procmime_mimeinfo_free_all(partinfo);
379         g_free(filename);
380
381         if (ret < 0) {
382                 alertpanel_error(_("Can't get part of multipart message"));
383                 g_free(part_filename);
384                 return FALSE;
385         }
386
387         g_string_append(cmd, part_filename);
388
389         g_free(part_filename);
390
391         return TRUE;
392 }
393
394 void actions_execute(gpointer data, 
395                      guint action_nb,
396                      GtkWidget *widget,
397                      gint source)
398 {
399         if (source == TOOLBAR_MAIN) 
400                 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
401         else if (source == TOOLBAR_COMPOSE)
402                 compose_actions_execute_cb((Compose*)data, action_nb, widget);
403         else if (source == TOOLBAR_MSGVIEW)
404                 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);      
405 }
406
407 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
408 {
409         action_update_menu(ifactory, "/Tools/Actions",
410                            mainwin_actions_execute_cb, mainwin);
411 }
412
413 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
414 {
415         action_update_menu(ifactory, "/Tools/Actions",
416                            msgview_actions_execute_cb, msgview);
417 }
418
419 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
420 {
421         action_update_menu(ifactory, "/Tools/Actions",
422                            compose_actions_execute_cb, compose);
423 }
424
425 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
426                                gpointer callback, gpointer data)
427 {
428         GtkWidget *menuitem;
429         gchar *menu_path;
430         GSList *cur;
431         gchar *action, *action_p;
432         GList *amenu;
433         GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
434
435         ifentry.path = branch_path;
436         menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
437         g_return_if_fail(menuitem != NULL);
438
439         amenu = GTK_MENU_SHELL(menuitem)->children;
440         while (amenu != NULL) {
441                 GList *alist = amenu->next;
442                 gtk_widget_destroy(GTK_WIDGET(amenu->data));
443                 amenu = alist;
444         }
445
446         ifentry.accelerator     = NULL;
447         ifentry.callback_action = 0;
448         ifentry.callback        = callback;
449         ifentry.item_type       = NULL;
450
451         for (cur = prefs_common.actions_list; cur; cur = cur->next) {
452                 action   = g_strdup((gchar *)cur->data);
453                 action_p = strstr(action, ": ");
454                 if (action_p && action_p[2] &&
455                     action_get_type(&action_p[2]) != ACTION_ERROR) {
456                         action_p[0] = 0x00;
457                         menu_path = g_strdup_printf("%s/%s", branch_path,
458                                                     action);
459                         ifentry.path = menu_path;
460                         gtk_item_factory_create_item(ifactory, &ifentry, data,
461                                                      1);
462                         g_free(menu_path);
463                 }
464                 g_free(action);
465                 ifentry.callback_action++;
466         }
467 }
468
469 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
470                                        GtkWidget *widget)
471 {
472         gchar *buf, *action;
473         ActionType action_type;
474
475         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
476
477         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
478         g_return_if_fail(buf != NULL);
479         action = strstr(buf, ": ");
480         g_return_if_fail(action != NULL);
481
482         /* Point to the beginning of the command-line */
483         action += 2;
484
485         action_type = action_get_type(action);
486         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
487                 alertpanel_warning
488                         (_("The selected action cannot be used in the compose window\n"
489                            "because it contains %%f, %%F, %%as or %%p."));
490                 return;
491         }
492
493         execute_actions(action, NULL, compose->text, NULL, 0, NULL);
494 }
495
496 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
497                                        GtkWidget *widget)
498 {
499         GSList *msg_list;
500
501         msg_list = summary_get_selected_msg_list(mainwin->summaryview);
502         message_actions_execute(mainwin->messageview, action_nb, msg_list);
503         g_slist_free(msg_list);
504 }
505
506 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
507                                        GtkWidget *widget)
508 {
509         GSList *msg_list = NULL;
510
511         if (msgview->msginfo)
512                 msg_list = g_slist_append(msg_list, msgview->msginfo);
513         message_actions_execute(msgview, action_nb, msg_list);
514         g_slist_free(msg_list);
515 }
516
517 static void message_actions_execute(MessageView *msgview, guint action_nb,
518                                     GSList *msg_list)
519 {
520         TextView *textview;
521         MimeInfo *partinfo;
522         gchar *buf;
523         gchar *action;
524         GtkWidget *text = NULL;
525         GdkFont *msgfont = NULL;
526         guint body_pos = 0;
527         ActionType action_type;
528         
529         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
530
531         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
532
533         g_return_if_fail(buf);
534         g_return_if_fail(action = strstr(buf, ": "));
535
536         /* Point to the beginning of the command-line */
537         action += 2;
538
539         textview = messageview_get_current_textview(msgview);
540         if (textview) {
541                 text     = textview->text;
542                 msgfont  = textview->msgfont;
543                 body_pos = textview->body_pos;
544         }
545         partinfo = messageview_get_selected_mime_part(msgview);
546
547         /* this command will alter the message text */
548         action_type = action_get_type(action);
549         if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
550                 msgview->filtered = TRUE;
551
552         if (action_type & ACTION_FILTERING_ACTION) 
553                 /* CLAWS: most of the above code is not necessary for applying
554                  * filtering */
555                 execute_filtering_actions(action, msg_list);
556         else
557                 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
558 }
559
560 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
561 {
562         GSList *action_list, *p;
563         const gchar *sbegin, *send;
564         gchar *action_string;
565         
566         if (NULL == (sbegin = strstr2(action, "%as{")))
567                 return FALSE;
568         sbegin += sizeof "%as{" - 1;
569         if (NULL == (send = strrchr(sbegin, '}')))
570                 return FALSE;
571         action_string = g_strndup(sbegin, send - sbegin);
572         
573         action_list = matcher_parser_get_action_list(action_string);
574         g_free(action_string);
575         if (action_list == NULL) return FALSE;
576         
577         /* apply actions on each message info */
578         for (p = msglist; p && p->data; p = g_slist_next(p))
579                 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
580                 
581         for (p = action_list; p; p = g_slist_next(p))
582                 if (p->data) filteringaction_free(p->data);     
583         g_slist_free(action_list);              
584         return TRUE;    
585 }
586
587 static gboolean execute_actions(gchar *action, GSList *msg_list,
588                                 GtkWidget *text, GdkFont *msgfont,
589                                 gint body_pos, MimeInfo *partinfo)
590 {
591         GSList *children_list = NULL;
592         gint is_ok  = TRUE;
593         gint msg_list_len;
594         Children *children;
595         ChildInfo *child_info;
596         ActionType action_type;
597         MsgInfo *msginfo;
598         gchar *cmd;
599         guint start = 0, end = 0;
600         gchar *sel_str = NULL;
601         gchar *msg_str = NULL;
602         gchar *user_str = NULL;
603         gchar *user_hidden_str = NULL;
604
605         g_return_val_if_fail(action && *action, FALSE);
606
607         action_type = action_get_type(action);
608
609         if (action_type == ACTION_ERROR)
610                 return FALSE;         /* ERR: syntax error */
611
612         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
613                 return FALSE;         /* ERR: file command without selection */
614
615         msg_list_len = g_slist_length(msg_list);
616
617         if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
618                 if (msg_list_len > 1)
619                         return FALSE; /* ERR: pipe + multiple selection */
620                 if (!text)
621                         return FALSE; /* ERR: pipe and no displayed text */
622         }
623
624         if (action_type & ACTION_SELECTION_STR) {
625                 if (!text)
626                         return FALSE; /* ERR: selection string but no text */
627         }
628
629         if (text) {
630                 if (GTK_EDITABLE(text)->has_selection) {
631                         start = GTK_EDITABLE(text)->selection_start_pos;
632                         end   = GTK_EDITABLE(text)->selection_end_pos;
633                         if (start > end) {
634                                 guint tmp;
635                                 tmp = start;
636                                 start = end;
637                                 end = tmp;
638                         }
639
640                         if (start == end) {
641                                 start = body_pos;
642                                 end = gtk_stext_get_length(GTK_STEXT(text));
643                                 msg_str = gtk_editable_get_chars
644                                         (GTK_EDITABLE(text), start, end);
645                         } else {
646                                 sel_str = gtk_editable_get_chars
647                                         (GTK_EDITABLE(text), start, end);
648                                 msg_str = g_strdup(sel_str);
649                         }
650                 } else {
651                         start = body_pos;
652                         end = gtk_stext_get_length(GTK_STEXT(text));
653                         msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
654                                                          start, end);
655                 }
656         }
657
658         if (action_type & ACTION_USER_STR) {
659                 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
660                         g_free(msg_str);
661                         g_free(sel_str);
662                         return FALSE;
663                 }
664         }
665
666         if (action_type & ACTION_USER_HIDDEN_STR) {
667                 if (!(user_hidden_str =
668                         get_user_string(action, ACTION_USER_HIDDEN_STR))) {
669                         g_free(msg_str);
670                         g_free(sel_str);
671                         g_free(user_str);
672                         return FALSE;
673                 }
674         }
675
676         if (action_type & ACTION_PIPE_OUT) {
677                 gtk_stext_freeze(GTK_STEXT(text));
678                 gtk_stext_set_point(GTK_STEXT(text), start);
679                 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
680                 gtk_stext_thaw(GTK_STEXT(text));
681         }
682
683         children = g_new0(Children, 1);
684
685         children->action      = g_strdup(action);
686         children->action_type = action_type;
687         children->msg_text    = text;
688         children->msgfont     = msgfont;
689
690         if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
691             ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
692                 children->open_in = 1;
693
694         if (action_type & ACTION_SINGLE) {
695                 GSList *cur;
696
697                 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
698                         msginfo = (MsgInfo *)cur->data;
699                         if (!msginfo) {
700                                 is_ok  = FALSE; /* ERR: msginfo missing */
701                                 break;
702                         }
703                         cmd = parse_action_cmd(action, msginfo, msg_list,
704                                                partinfo, user_str,
705                                                user_hidden_str, sel_str);
706                         if (!cmd) {
707                                 debug_print("Action command error\n");
708                                 is_ok  = FALSE; /* ERR: incorrect command */
709                                 break;
710                         }
711                         if ((child_info = fork_child(cmd, msg_str, children))) {
712                                 children_list = g_slist_append(children_list,
713                                                                child_info);
714                         }
715                         g_free(cmd);
716                 }
717         } else {
718                 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
719                                        user_str, user_hidden_str, sel_str);
720                 if (cmd) {
721                         if ((child_info = fork_child(cmd, msg_str, children))) {
722                                 children_list = g_slist_append(children_list,
723                                                                child_info);
724                         }
725                         g_free(cmd);
726                 } else
727                         is_ok  = FALSE;         /* ERR: incorrect command */
728         }
729
730         g_free(msg_str);
731         g_free(sel_str);
732         g_free(user_str);
733         g_free(user_hidden_str);
734
735         if (!children_list) {
736                  /* If not waiting for children, return */
737                 free_children(children);
738         } else {
739                 GSList *cur;
740
741                 children->list        = children_list;
742                 children->nb          = g_slist_length(children_list);
743                 children->initial_nb  = children->nb;
744
745                 for (cur = children_list; cur; cur = cur->next) {
746                         child_info = (ChildInfo *) cur->data;
747                         child_info->tag_status = 
748                                 gdk_input_add(child_info->chld_status,
749                                               GDK_INPUT_READ,
750                                               catch_status, child_info);
751                 }
752
753                 create_io_dialog(children);
754         }
755
756         return is_ok;
757 }
758
759 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
760                              Children *children)
761 {
762         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
763         gchar *cmdline[4];
764         pid_t pid, gch_pid;
765         ChildInfo *child_info;
766         gint sync;
767
768         sync = !(children->action_type & ACTION_ASYNC);
769
770         chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
771                 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
772
773         if (sync) {
774                 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
775                     pipe(chld_err)) {
776                         alertpanel_error(_("Command could not be started. "
777                                            "Pipe creation failed.\n%s"),
778                                         g_strerror(errno));
779                         /* Closing fd = -1 fails silently */
780                         close(chld_in[0]);
781                         close(chld_in[1]);
782                         close(chld_out[0]);
783                         close(chld_out[1]);
784                         close(chld_err[0]);
785                         close(chld_err[1]);
786                         close(chld_status[0]);
787                         close(chld_status[1]);
788                         return NULL; /* Pipe error */
789                 }
790         }
791
792         debug_print("Forking child and grandchild.\n");
793
794         pid = fork();
795         if (pid == 0) { /* Child */
796                 if (setpgid(0, 0))
797                         perror("setpgid");
798
799                 close(ConnectionNumber(gdk_display));
800
801                 gch_pid = fork();
802
803                 if (gch_pid == 0) {
804                         if (setpgid(0, getppid()))
805                                 perror("setpgid");
806
807                         if (sync) {
808                                 if (children->action_type &
809                                     (ACTION_PIPE_IN |
810                                      ACTION_USER_IN |
811                                      ACTION_USER_HIDDEN_IN)) {
812                                         close(fileno(stdin));
813                                         dup  (chld_in[0]);
814                                 }
815                                 close(chld_in[0]);
816                                 close(chld_in[1]);
817
818                                 close(fileno(stdout));
819                                 dup  (chld_out[1]);
820                                 close(chld_out[0]);
821                                 close(chld_out[1]);
822
823                                 close(fileno(stderr));
824                                 dup  (chld_err[1]);
825                                 close(chld_err[0]);
826                                 close(chld_err[1]);
827                         }
828
829                         cmdline[0] = "sh";
830                         cmdline[1] = "-c";
831                         cmdline[2] = cmd;
832                         cmdline[3] = 0;
833                         execvp("/bin/sh", cmdline);
834
835                         perror("execvp");
836                         _exit(1);
837                 } else if (gch_pid < (pid_t) 0) { /* Fork error */
838                         if (sync)
839                                 write(chld_status[1], "1\n", 2);
840                         perror("fork");
841                         _exit(1);
842                 } else { /* Child */
843                         if (sync) {
844                                 close(chld_in[0]);
845                                 close(chld_in[1]);
846                                 close(chld_out[0]);
847                                 close(chld_out[1]);
848                                 close(chld_err[0]);
849                                 close(chld_err[1]);
850                                 close(chld_status[0]);
851
852                                 debug_print("Child: Waiting for grandchild\n");
853                                 waitpid(gch_pid, NULL, 0);
854                                 debug_print("Child: grandchild ended\n");
855                                 write(chld_status[1], "0\n", 2);
856                                 close(chld_status[1]);
857                         }
858                         _exit(0);
859                 }
860         } else if (pid < 0) { /* Fork error */
861                 alertpanel_error(_("Could not fork to execute the following "
862                                    "command:\n%s\n%s"),
863                                  cmd, g_strerror(errno));
864                 return NULL; 
865         }
866
867         /* Parent */
868
869         if (!sync) {
870                 waitpid(pid, NULL, 0);
871                 return NULL;
872         }
873
874         close(chld_in[0]);
875         if (!(children->action_type &
876               (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
877                 close(chld_in[1]);
878         close(chld_out[1]);
879         close(chld_err[1]);
880         close(chld_status[1]);
881
882         child_info = g_new0(ChildInfo, 1);
883
884         child_info->children    = children;
885
886         child_info->pid         = pid;
887         child_info->cmd         = g_strdup(cmd);
888         child_info->new_out     = FALSE;
889         child_info->output      = g_string_new(NULL);
890         child_info->chld_in     =
891                 (children->action_type &
892                  (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
893                         ? chld_in [1] : -1;
894         child_info->chld_out    = chld_out[0];
895         child_info->chld_err    = chld_err[0];
896         child_info->chld_status = chld_status[0];
897         child_info->tag_in      = -1;
898         child_info->tag_out     = gdk_input_add(chld_out[0], GDK_INPUT_READ,
899                                                 catch_output, child_info);
900         child_info->tag_err     = gdk_input_add(chld_err[0], GDK_INPUT_READ,
901                                                 catch_output, child_info);
902
903         if (!(children->action_type &
904               (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
905                 return child_info;
906
907         if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
908                 write(chld_in[1], msg_str, strlen(msg_str));
909                 if (!(children->action_type &
910                       (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
911                         close(chld_in[1]);
912                 child_info->chld_in = -1; /* No more input */
913         }
914
915         return child_info;
916 }
917
918 static void kill_children_cb(GtkWidget *widget, gpointer data)
919 {
920         GSList *cur;
921         Children *children = (Children *) data;
922         ChildInfo *child_info;
923
924         for (cur = children->list; cur; cur = cur->next) {
925                 child_info = (ChildInfo *)(cur->data);
926                 debug_print("Killing child group id %d\n", child_info->pid);
927                 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
928                         perror("kill");
929         }
930 }
931
932 static gint wait_for_children(Children *children)
933 {
934         gboolean new_output;
935         ChildInfo *child_info;
936         GSList *cur;
937         gint nb = children->nb;
938
939         children->nb = 0;
940
941         cur = children->list;
942         new_output = FALSE;
943         while (cur) {
944                 child_info = (ChildInfo *)cur->data;
945                 if (child_info->pid)
946                         children->nb++;
947                 new_output |= child_info->new_out;
948                 cur = cur->next;
949         }
950
951         children->output |= new_output;
952
953         if (new_output || (children->dialog && (nb != children->nb)))
954                 update_io_dialog(children);
955
956         if (children->nb)
957                 return FALSE;
958
959         if (!children->dialog) {
960                 free_children(children);
961         } else if (!children->output) {
962                 gtk_widget_destroy(children->dialog);
963         }
964
965         return FALSE;
966 }
967
968 static void send_input(GtkWidget *w, gpointer data)
969 {
970         Children *children = (Children *) data;
971         ChildInfo *child_info = (ChildInfo *) children->list->data;
972
973         child_info->tag_in = gdk_input_add(child_info->chld_in,
974                                            GDK_INPUT_WRITE,
975                                            catch_input, children);
976         gtk_widget_set_sensitive(children->input_hbox, FALSE);
977 }
978
979 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
980 {
981         hide_io_dialog_cb(w, data);
982         return TRUE;
983 }
984
985 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
986 {
987
988         Children *children = (Children *)data;
989
990         if (!children->nb) {
991                 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
992                                               children);
993                 gtk_widget_destroy(children->dialog);
994                 free_children(children);
995         }
996 }
997
998 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
999                                      gpointer data)
1000 {
1001         if (event && event->keyval == GDK_Escape)
1002                 hide_io_dialog_cb(widget, data);
1003         return TRUE;
1004 }
1005
1006 static void childinfo_close_pipes(ChildInfo *child_info)
1007 {
1008         /* stdout and stderr pipes are guaranteed to be removed by
1009          * their handler, but in case where we receive child exit notification
1010          * before grand-child's pipes closing signals, we check them and close
1011          * them if necessary
1012          */
1013         if (child_info->tag_in > 0)
1014                 gdk_input_remove(child_info->tag_in);
1015         if (child_info->tag_out > 0)
1016                 gdk_input_remove(child_info->tag_out);
1017         if (child_info->tag_err > 0)
1018                 gdk_input_remove(child_info->tag_err);
1019
1020         if (child_info->chld_in >= 0)
1021                 close(child_info->chld_in);
1022         if (child_info->chld_out >= 0)
1023                 close(child_info->chld_out);
1024         if (child_info->chld_err >= 0)
1025                 close(child_info->chld_err);
1026
1027         close(child_info->chld_status);
1028 }
1029
1030 static void free_children(Children *children)
1031 {
1032         ChildInfo *child_info;
1033
1034         debug_print("Freeing children data %p\n", children);
1035
1036         g_free(children->action);
1037         while (children->list != NULL) {
1038                 child_info = (ChildInfo *)children->list->data;
1039                 g_free(child_info->cmd);
1040                 g_string_free(child_info->output, TRUE);
1041                 children->list = g_slist_remove(children->list, child_info);
1042                 g_free(child_info);
1043         }
1044         g_free(children);
1045 }
1046
1047 static void update_io_dialog(Children *children)
1048 {
1049         GSList *cur;
1050
1051         debug_print("Updating actions input/output dialog.\n");
1052
1053         if (children->progress_bar) {
1054                 gtk_progress_configure(GTK_PROGRESS(children->progress_bar),
1055                                 children->initial_nb -children->nb,
1056                                 0.0, children->initial_nb);
1057         }
1058
1059         if (!children->nb) {
1060                 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1061                 gtk_widget_set_sensitive(children->close_btn, TRUE);
1062                 if (children->input_hbox)
1063                         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1064                 gtk_widget_grab_focus(children->close_btn);
1065                 gtk_signal_connect(GTK_OBJECT(children->dialog),
1066                                    "key_press_event",
1067                                    GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1068                                    children);
1069         }
1070
1071         if (children->output) {
1072                 GtkWidget *text = children->text;
1073                 gchar *caption;
1074                 ChildInfo *child_info;
1075                 GdkFont *font;
1076
1077                 font = gtk_object_get_data(GTK_OBJECT(children->dialog), 
1078                                            "s_txtfont");
1079                 gtk_widget_show(children->scrolledwin);
1080                 gtk_text_freeze(GTK_TEXT(text));
1081                 gtk_text_set_point(GTK_TEXT(text), 0);
1082                 gtk_text_forward_delete(GTK_TEXT(text), 
1083                                         gtk_text_get_length(GTK_TEXT(text)));
1084                 for (cur = children->list; cur; cur = cur->next) {
1085                         child_info = (ChildInfo *)cur->data;
1086                         if (child_info->pid)
1087                                 caption = g_strdup_printf
1088                                         (_("--- Running: %s\n"),
1089                                          child_info->cmd);
1090                         else
1091                                 caption = g_strdup_printf
1092                                         (_("--- Ended: %s\n"),
1093                                          child_info->cmd);
1094
1095                         gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1096                                         caption, -1);
1097                         gtk_text_insert(GTK_TEXT(text), font, NULL, NULL,
1098                                         child_info->output->str, -1);
1099                         g_free(caption);
1100                         child_info->new_out = FALSE;
1101                 }
1102                 gtk_text_thaw(GTK_TEXT(text));
1103         }
1104 }
1105
1106 static void create_io_dialog(Children *children)
1107 {
1108         GtkWidget *dialog;
1109         GtkWidget *vbox;
1110         GtkWidget *entry = NULL;
1111         GtkWidget *input_hbox = NULL;
1112         GtkWidget *send_button;
1113         GtkWidget *label;
1114         GtkWidget *text;
1115         GtkWidget *scrolledwin;
1116         GtkWidget *hbox;
1117         GtkWidget *progress_bar = NULL;
1118         GtkWidget *abort_button;
1119         GtkWidget *close_button;
1120         GdkFont   *output_font;
1121
1122         debug_print("Creating action IO dialog\n");
1123
1124         dialog = gtk_dialog_new();
1125         gtk_container_set_border_width
1126                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1127         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1128         gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1129         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1130         manage_window_set_transient(GTK_WINDOW(dialog));
1131         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1132                         GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1133         gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1134                         GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1135                         children);
1136
1137         vbox = gtk_vbox_new(FALSE, 8);
1138         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1139         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1140         gtk_widget_show(vbox);
1141
1142         label = gtk_label_new(children->action);
1143         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1144         gtk_widget_show(label);
1145
1146         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1147         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1148                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1149         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1150         gtk_widget_set_usize(scrolledwin, 480, 200);
1151         gtk_widget_hide(scrolledwin);
1152
1153         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1154                             (GTK_SCROLLED_WINDOW(scrolledwin)),
1155                             gtk_scrolled_window_get_vadjustment
1156                             (GTK_SCROLLED_WINDOW(scrolledwin)));
1157         gtk_text_set_editable(GTK_TEXT(text), FALSE);
1158         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1159         gtk_widget_show(text);
1160
1161         if (children->open_in) {
1162                 input_hbox = gtk_hbox_new(FALSE, 8);
1163                 gtk_widget_show(input_hbox);
1164
1165                 entry = gtk_entry_new();
1166                 gtk_widget_set_usize(entry, 320, -1);
1167                 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1168                                    GTK_SIGNAL_FUNC(send_input), children);
1169                 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1170                 if (children->action_type & ACTION_USER_HIDDEN_IN)
1171                         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1172                 gtk_widget_show(entry);
1173
1174                 send_button = gtk_button_new_with_label(_(" Send "));
1175                 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1176                                    GTK_SIGNAL_FUNC(send_input), children);
1177                 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1178                                    FALSE, 0);
1179                 gtk_widget_show(send_button);
1180
1181                 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1182                 gtk_widget_grab_focus(entry);
1183         }
1184
1185         if (children->initial_nb > 1) {
1186                 progress_bar = gtk_progress_bar_new();
1187                 gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(progress_bar),
1188                                 GTK_PROGRESS_CONTINUOUS);
1189                 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1190                                 GTK_PROGRESS_LEFT_TO_RIGHT);
1191                 gtk_progress_set_activity_mode(GTK_PROGRESS(progress_bar), 
1192                                 FALSE);
1193                 gtk_progress_set_format_string(GTK_PROGRESS(progress_bar),
1194                                 _("Completed %v/%u"));
1195                 gtk_progress_set_show_text(GTK_PROGRESS(progress_bar), TRUE);
1196                 gtk_progress_configure(GTK_PROGRESS(progress_bar),
1197                                 children->initial_nb -children->nb,
1198                                 0.0, children->initial_nb);
1199
1200                 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1201                 gtk_widget_show(progress_bar);
1202         }
1203
1204         gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1205                                 &close_button, _("Close"), NULL, NULL);
1206         gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1207                         GTK_SIGNAL_FUNC(kill_children_cb), children);
1208         gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1209                         GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1210         gtk_widget_show(hbox);
1211
1212         if (children->nb)
1213                 gtk_widget_set_sensitive(close_button, FALSE);
1214
1215         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1216
1217         children->dialog       = dialog;
1218         children->scrolledwin  = scrolledwin;
1219         children->text         = text;
1220         children->input_hbox   = children->open_in ? input_hbox : NULL;
1221         children->input_entry  = children->open_in ? entry : NULL;
1222         children->progress_bar = progress_bar;
1223         children->abort_btn    = abort_button;
1224         children->close_btn    = close_button;
1225
1226         output_font = gtkut_font_load_from_fontset(prefs_common.textfont);
1227         gtk_object_set_data_full(GTK_OBJECT(dialog), "s_txtfont",
1228                                  output_font, 
1229                                  (GtkDestroyNotify)gdk_font_unref); 
1230         gtk_widget_show(dialog);
1231 }
1232
1233 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1234 {
1235         ChildInfo *child_info = (ChildInfo *)data;
1236         gchar buf;
1237         gint c;
1238
1239         gdk_input_remove(child_info->tag_status);
1240
1241         c = read(source, &buf, 1);
1242         debug_print("Child returned %c\n", buf);
1243
1244         waitpid(-child_info->pid, NULL, 0);
1245         childinfo_close_pipes(child_info);
1246         child_info->pid = 0;
1247
1248         wait_for_children(child_info->children);
1249 }
1250         
1251 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1252 {
1253         Children *children = (Children *)data;
1254         ChildInfo *child_info = (ChildInfo *)children->list->data;
1255         gchar *input;
1256         gint c, count, len;
1257
1258         debug_print("Sending input to grand child.\n");
1259         if (!(cond && GDK_INPUT_WRITE))
1260                 return;
1261
1262         gdk_input_remove(child_info->tag_in);
1263         child_info->tag_in = -1;
1264
1265         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1266                                        0, -1);
1267         len = strlen(input);
1268         count = 0;
1269
1270         do {
1271                 c = write(child_info->chld_in, input + count, len - count);
1272                 if (c >= 0)
1273                         count += c;
1274         } while (c >= 0 && count < len);
1275
1276         if (c >= 0)
1277                 write(child_info->chld_in, "\n", 2);
1278
1279         g_free(input);
1280
1281         gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1282         gtk_widget_set_sensitive(children->input_hbox, TRUE);
1283         close(child_info->chld_in);
1284         child_info->chld_in = -1;
1285         debug_print("Input to grand child sent.\n");
1286 }
1287
1288 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1289 {
1290         ChildInfo *child_info = (ChildInfo *)data;
1291         gint c, i;
1292         gchar buf[BUFFSIZE];
1293
1294         debug_print("Catching grand child's output.\n");
1295         if (child_info->children->action_type &
1296             (ACTION_PIPE_OUT | ACTION_INSERT)
1297             && source == child_info->chld_out) {
1298                 gboolean is_selection = FALSE;
1299                 GtkWidget *text = child_info->children->msg_text;
1300
1301                 if (GTK_EDITABLE(text)->has_selection)
1302                         is_selection = TRUE;
1303                 gtk_stext_freeze(GTK_STEXT(text));
1304                 while (TRUE) {
1305                         c = read(source, buf, sizeof(buf) - 1);
1306                         if (c == 0)
1307                                 break;
1308                         gtk_stext_insert(GTK_STEXT(text),
1309                                          child_info->children->msgfont,
1310                                          NULL, NULL, buf, c);
1311                 }
1312                 if (is_selection) {
1313                         /* Using the select_region draws things. Should not.
1314                          * so we just change selection position and 
1315                          * defere drawing when thawing. Hack?
1316                          */
1317                         GTK_EDITABLE(text)->selection_end_pos =
1318                                         gtk_stext_get_point(GTK_STEXT(text));
1319                 }
1320                 gtk_stext_thaw(GTK_STEXT(text));
1321         } else {
1322                 c = read(source, buf, sizeof(buf) - 1);
1323                 for (i = 0; i < c; i++)
1324                         g_string_append_c(child_info->output, buf[i]);
1325                 if (c > 0)
1326                         child_info->new_out = TRUE;
1327         }
1328         if (c == 0) {
1329                 if (source == child_info->chld_out) {
1330                         gdk_input_remove(child_info->tag_out);
1331                         child_info->tag_out = -1;
1332                         close(child_info->chld_out);
1333                         child_info->chld_out = -1;
1334                 } else {
1335                         gdk_input_remove(child_info->tag_err);
1336                         child_info->tag_err = -1;
1337                         close(child_info->chld_err);
1338                         child_info->chld_err = -1;
1339                 }
1340         }
1341         
1342         wait_for_children(child_info->children);
1343 }
1344
1345 static gchar *get_user_string(const gchar *action, ActionType type)
1346 {
1347         gchar *message;
1348         gchar *user_str = NULL;
1349
1350         switch (type) {
1351         case ACTION_USER_HIDDEN_STR:
1352                 message = g_strdup_printf
1353                         (_("Enter the argument for the following action:\n"
1354                            "(`%%h' will be replaced with the argument)\n"
1355                            "  %s"),
1356                          action);
1357                 user_str = input_dialog_with_invisible
1358                         (_("Action's hidden user argument"), message, NULL);
1359                 break;
1360         case ACTION_USER_STR:
1361                 message = g_strdup_printf
1362                         (_("Enter the argument for the following action:\n"
1363                            "(`%%u' will be replaced with the argument)\n"
1364                            "  %s"),
1365                          action);
1366                 user_str = input_dialog
1367                         (_("Action's user argument"), message, NULL);
1368                 break;
1369         default:
1370                 g_warning("Unsupported action type %d", type);
1371         }
1372
1373         return user_str;
1374 }