0.8.8claws6
[claws.git] / src / prefs_actions.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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 "prefs_gtk.h"
41 #include "inc.h"
42 #include "utils.h"
43 #include "gtkutils.h"
44 #include "manage_window.h"
45 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "alertpanel.h"
48 #include "prefs_actions.h"
49 #include "compose.h"
50 #include "procmsg.h"
51 #include "gtkstext.h"
52 #include "mimeview.h"
53 #include "description_window.h"
54 #include "textview.h"
55
56 typedef enum
57 {
58         ACTION_NONE     = 1 << 0,
59         ACTION_PIPE_IN  = 1 << 1,
60         ACTION_PIPE_OUT = 1 << 2,
61         ACTION_SINGLE   = 1 << 3,
62         ACTION_MULTIPLE = 1 << 4,
63         ACTION_ASYNC    = 1 << 5,
64         ACTION_OPEN_IN  = 1 << 6,
65         ACTION_HIDE_IN  = 1 << 7,
66         ACTION_INSERT   = 1 << 8,
67         ACTION_ERROR    = 1 << 9,
68 } ActionType;
69
70 static struct Actions
71 {
72         GtkWidget *window;
73
74         GtkWidget *ok_btn;
75
76         GtkWidget *name_entry;
77         GtkWidget *cmd_entry;
78
79         GtkWidget *actions_clist;
80 } actions;
81
82 typedef struct _Children Children;
83 typedef struct _ChildInfo ChildInfo;
84
85 struct _Children
86 {
87         GtkWidget       *window;
88         GtkWidget       *dialog;
89         GtkWidget       *text;
90         GtkWidget       *input_entry;
91         GtkWidget       *input_hbox;
92         GtkWidget       *abort_btn;
93         GtkWidget       *close_btn;
94         GtkWidget       *scrolledwin;
95
96         gchar           *action;
97         GSList          *list;
98         gint             nb;
99         gint             open_in;
100         gboolean         output;
101 };
102
103 struct _ChildInfo
104 {
105         Children        *children;
106         gchar           *cmd;
107         guint            type;
108         pid_t            pid;
109         gint             chld_in;
110         gint             chld_out;
111         gint             chld_err;
112         gint             chld_status;
113         gint             tag_in;
114         gint             tag_out;
115         gint             tag_err;
116         gint             tag_status;
117         gint             new_out;
118         GString         *output;
119         GtkWidget       *text;
120         GdkFont         *msgfont;
121 };
122
123 /* widget creating functions */
124 static void prefs_actions_create        (MainWindow *mainwin);
125 static void prefs_actions_set_dialog    (void);
126 static gint prefs_actions_clist_set_row (gint row);
127
128 /* callback functions */
129 static void prefs_actions_help_cb       (GtkWidget      *w,
130                                          gpointer        data);
131 static void prefs_actions_register_cb   (GtkWidget      *w,
132                                          gpointer        data);
133 static void prefs_actions_substitute_cb (GtkWidget      *w,
134                                          gpointer        data);
135 static void prefs_actions_delete_cb     (GtkWidget      *w,
136                                          gpointer        data);
137 static void prefs_actions_up            (GtkWidget      *w,
138                                          gpointer        data);
139 static void prefs_actions_down          (GtkWidget      *w,
140                                          gpointer        data);
141 static void prefs_actions_select        (GtkCList       *clist,
142                                          gint            row,
143                                          gint            column,
144                                          GdkEvent       *event);
145 static void prefs_actions_row_move      (GtkCList       *clist,
146                                          gint            source_row,
147                                          gint            dest_row);
148 static gint prefs_actions_deleted       (GtkWidget      *widget,
149                                          GdkEventAny    *event,
150                                          gpointer       *data);
151 static void prefs_actions_key_pressed   (GtkWidget      *widget,
152                                          GdkEventKey    *event,
153                                          gpointer        data);
154 static void prefs_actions_cancel        (GtkWidget      *w,
155                                          gpointer        data);
156 static void prefs_actions_ok            (GtkWidget      *w,
157                                          gpointer        data);
158 static void update_actions_menu         (GtkItemFactory *ifactory,
159                                          gchar          *branch_path,
160                                          gpointer        callback,
161                                          gpointer        data);
162 static void mainwin_actions_execute_cb  (MainWindow     *mainwin,
163                                          guint           action_nb,
164                                          GtkWidget      *widget);
165 static void compose_actions_execute_cb  (Compose        *compose,
166                                          guint           action_nb,
167                                          GtkWidget      *widget);
168 static void msgview_actions_execute_cb  (MessageView    *msgview, 
169                                          guint           action_nb,
170                                          GtkWidget      *widget);
171 static guint get_action_type            (gchar          *action);
172
173 static gboolean execute_actions         (gchar          *action, 
174                                          GtkWidget      *window,
175                                          GtkCTree       *ctree, 
176                                          GtkWidget      *text,
177                                          GdkFont        *msgfont,
178                                          gint            body_pos,
179                                          MimeView       *mimeview);
180
181 static gchar *parse_action_cmd          (gchar          *action,
182                                          MsgInfo        *msginfo,
183                                          GtkCTree       *ctree,
184                                          MimeView       *mimeview);
185 static gboolean parse_append_filename   (GString        **cmd,
186                                          MsgInfo        *msginfo);
187
188 static gboolean parse_append_msgpart    (GString        **cmd,
189                                          MsgInfo        *msginfo,
190                                          MimeView       *mimeview);
191
192 ChildInfo *fork_child                   (gchar          *cmd,
193                                          gint            action_type,
194                                          GtkWidget      *text,
195                                          GdkFont        *msgfont,
196                                          gint            body_pos,
197                                          Children       *children);
198
199 static gint wait_for_children           (gpointer        data);
200
201 static void free_children               (Children       *children);
202
203 static void childinfo_close_pipes       (ChildInfo      *child_info);
204
205 static void create_io_dialog            (Children       *children);
206 static void update_io_dialog            (Children       *children);
207
208 static void hide_io_dialog_cb           (GtkWidget      *widget,
209                                          gpointer        data);
210
211 static void catch_output                (gpointer                data,
212                                          gint                    source,
213                                          GdkInputCondition       cond);
214 static void catch_input                 (gpointer                data, 
215                                          gint                    source,
216                                          GdkInputCondition       cond);
217 static void catch_status                (gpointer                data,
218                                          gint                    source,
219                                          GdkInputCondition       cond);
220
221
222 void prefs_actions_open(MainWindow *mainwin)
223 {
224 #if 0
225         if (prefs_rc_is_readonly(ACTIONS_RC))
226                 return;
227 #endif
228         inc_lock();
229
230         if (!actions.window)
231                 prefs_actions_create(mainwin);
232
233         manage_window_set_transient(GTK_WINDOW(actions.window));
234         gtk_widget_grab_focus(actions.ok_btn);
235
236         prefs_actions_set_dialog();
237
238         gtk_widget_show(actions.window);
239 }
240
241 static void prefs_actions_create(MainWindow *mainwin)
242 {
243         GtkWidget *window;
244         GtkWidget *vbox;
245         GtkWidget *ok_btn;
246         GtkWidget *cancel_btn;
247         GtkWidget *confirm_area;
248
249         GtkWidget *vbox1;
250
251         GtkWidget *entry_vbox;
252         GtkWidget *hbox;
253         GtkWidget *name_label;
254         GtkWidget *name_entry;
255         GtkWidget *cmd_label;
256         GtkWidget *cmd_entry;
257
258         GtkWidget *reg_hbox;
259         GtkWidget *btn_hbox;
260         GtkWidget *arrow;
261         GtkWidget *reg_btn;
262         GtkWidget *subst_btn;
263         GtkWidget *del_btn;
264
265         GtkWidget *cond_hbox;
266         GtkWidget *cond_scrolledwin;
267         GtkWidget *cond_clist;
268
269         GtkWidget *help_button;
270
271         GtkWidget *btn_vbox;
272         GtkWidget *up_btn;
273         GtkWidget *down_btn;
274
275         gchar *title[1];
276
277         debug_print("Creating actions configuration window...\n");
278
279         window = gtk_window_new (GTK_WINDOW_DIALOG);
280
281         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
282         gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
283         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
284         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
285         gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
286
287         vbox = gtk_vbox_new(FALSE, 6);
288         gtk_widget_show(vbox);
289         gtk_container_add(GTK_CONTAINER(window), vbox);
290
291         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
292                                 &cancel_btn, _("Cancel"), NULL, NULL);
293         gtk_widget_show(confirm_area);
294         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
295         gtk_widget_grab_default(ok_btn);
296
297         gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
298         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
299                            GTK_SIGNAL_FUNC(prefs_actions_deleted), NULL);
300         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
301                            GTK_SIGNAL_FUNC(prefs_actions_key_pressed), NULL);
302         MANAGE_WINDOW_SIGNALS_CONNECT(window);
303         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
304                            GTK_SIGNAL_FUNC(prefs_actions_ok), mainwin);
305         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
306                            GTK_SIGNAL_FUNC(prefs_actions_cancel), NULL);
307
308         vbox1 = gtk_vbox_new(FALSE, 8);
309         gtk_widget_show(vbox1);
310         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
311         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
312
313         entry_vbox = gtk_vbox_new(FALSE, 4);
314         gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0);
315
316         hbox = gtk_hbox_new(FALSE, 8);
317         gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0);
318
319         name_label = gtk_label_new(_("Menu name:"));
320         gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0);
321
322         name_entry = gtk_entry_new();
323         gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0);
324
325         hbox = gtk_hbox_new(FALSE, 8);
326         gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0);
327
328         cmd_label = gtk_label_new(_("Command line:"));
329         gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0);
330
331         cmd_entry = gtk_entry_new();
332         gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0);
333
334         gtk_widget_show_all(entry_vbox);
335
336         /* register / substitute / delete */
337
338         reg_hbox = gtk_hbox_new(FALSE, 4);
339         gtk_widget_show(reg_hbox);
340         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
341
342         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
343         gtk_widget_show(arrow);
344         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
345         gtk_widget_set_usize(arrow, -1, 16);
346
347         btn_hbox = gtk_hbox_new(TRUE, 4);
348         gtk_widget_show(btn_hbox);
349         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
350
351         reg_btn = gtk_button_new_with_label(_("Add"));
352         gtk_widget_show(reg_btn);
353         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
354         gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
355                            GTK_SIGNAL_FUNC(prefs_actions_register_cb), NULL);
356
357         subst_btn = gtk_button_new_with_label(_("  Replace  "));
358         gtk_widget_show(subst_btn);
359         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
360         gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
361                            GTK_SIGNAL_FUNC(prefs_actions_substitute_cb),
362                            NULL);
363
364         del_btn = gtk_button_new_with_label(_("Delete"));
365         gtk_widget_show(del_btn);
366         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
367         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
368                            GTK_SIGNAL_FUNC(prefs_actions_delete_cb), NULL);
369
370         help_button = gtk_button_new_with_label(_(" Syntax help "));
371         gtk_widget_show(help_button);
372         gtk_box_pack_end(GTK_BOX(reg_hbox), help_button, FALSE, FALSE, 0);
373         gtk_signal_connect(GTK_OBJECT(help_button), "clicked",
374                            GTK_SIGNAL_FUNC(prefs_actions_help_cb), NULL);
375
376         cond_hbox = gtk_hbox_new(FALSE, 8);
377         gtk_widget_show(cond_hbox);
378         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
379
380         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
381         gtk_widget_show(cond_scrolledwin);
382         gtk_widget_set_usize(cond_scrolledwin, -1, 150);
383         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
384                            TRUE, TRUE, 0);
385         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
386                                        GTK_POLICY_AUTOMATIC,
387                                        GTK_POLICY_AUTOMATIC);
388
389         title[0] = _("Current actions");
390         cond_clist = gtk_clist_new_with_titles(1, title);
391         gtk_widget_show(cond_clist);
392         gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist);
393         gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80);
394         gtk_clist_set_selection_mode(GTK_CLIST (cond_clist),
395                                      GTK_SELECTION_BROWSE);
396         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button,
397                                GTK_CAN_FOCUS);
398         gtk_signal_connect(GTK_OBJECT(cond_clist), "select_row",
399                            GTK_SIGNAL_FUNC(prefs_actions_select), NULL);
400         gtk_signal_connect_after(GTK_OBJECT(cond_clist), "row_move",
401                                  GTK_SIGNAL_FUNC(prefs_actions_row_move),
402                                  NULL);
403
404         btn_vbox = gtk_vbox_new(FALSE, 8);
405         gtk_widget_show(btn_vbox);
406         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
407
408         up_btn = gtk_button_new_with_label(_("Up"));
409         gtk_widget_show(up_btn);
410         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
411         gtk_signal_connect(GTK_OBJECT(up_btn), "clicked",
412                            GTK_SIGNAL_FUNC(prefs_actions_up), NULL);
413
414         down_btn = gtk_button_new_with_label(_("Down"));
415         gtk_widget_show(down_btn);
416         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
417         gtk_signal_connect(GTK_OBJECT(down_btn), "clicked",
418                            GTK_SIGNAL_FUNC(prefs_actions_down), NULL);
419
420         gtk_widget_show(window);
421
422         actions.window = window;
423         actions.ok_btn = ok_btn;
424
425         actions.name_entry = name_entry;
426         actions.cmd_entry  = cmd_entry;
427
428         actions.actions_clist = cond_clist;
429 }
430
431
432 void prefs_actions_read_config(void)
433 {
434         gchar *rcpath;
435         FILE *fp;
436         gchar buf[PREFSBUFSIZE];
437         gchar *act;
438
439         debug_print("Reading actions configurations...\n");
440
441         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
442         if ((fp = fopen(rcpath, "rb")) == NULL) {
443                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
444                 g_free(rcpath);
445                 return;
446         }
447         g_free(rcpath);
448
449         while (prefs_common.actions_list != NULL) {
450                 act = (gchar *)prefs_common.actions_list->data;
451                 prefs_common.actions_list =
452                         g_slist_remove(prefs_common.actions_list, act);
453                 g_free(act);
454         }
455
456         while (fgets(buf, sizeof(buf), fp) != NULL) {
457                 g_strchomp(buf);
458                 act = strstr(buf, ": ");
459                 if (act && act[2] && 
460                     get_action_type(&act[2]) != ACTION_ERROR)
461                         prefs_common.actions_list =
462                                 g_slist_append(prefs_common.actions_list,
463                                                g_strdup(buf));
464         }
465         fclose(fp);
466 }
467
468 void prefs_actions_write_config(void)
469 {
470         gchar *rcpath;
471         PrefFile *pfile;
472         GSList *cur;
473
474         debug_print("Writing actions configuration...\n");
475
476         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
477         if ((pfile= prefs_write_open(rcpath)) == NULL) {
478                 g_warning("failed to write configuration to file\n");
479                 g_free(rcpath);
480                 return;
481         }
482
483         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
484                 gchar *act = (gchar *)cur->data;
485                 if (fputs(act, pfile->fp) == EOF ||
486                     fputc('\n', pfile->fp) == EOF) {
487                         FILE_OP_ERROR(rcpath, "fputs || fputc");
488                         prefs_file_close_revert(pfile);
489                         g_free(rcpath);
490                         return;
491                 }
492         }
493         
494         g_free(rcpath);
495
496         if (prefs_file_close(pfile) < 0) {
497                 g_warning("failed to write configuration to file\n");
498                 return;
499         }
500 }
501
502 static guint get_action_type(gchar *action)
503 {
504         gchar *p;
505         guint action_type = ACTION_NONE;
506
507         g_return_val_if_fail(action,  ACTION_ERROR);
508         g_return_val_if_fail(*action, ACTION_ERROR);
509
510         p = action;
511
512         if (p[0] == '|') {
513                 action_type |= ACTION_PIPE_IN;
514                 p++;
515         } else if (p[0] == '>') {
516                 action_type |= ACTION_OPEN_IN;
517                 p++;
518         } else if (p[0] == '*') {
519                 action_type |= ACTION_HIDE_IN;
520                 p++;
521         }
522
523         if (p[0] == 0x00)
524                 return ACTION_ERROR;
525
526         while (*p && action_type != ACTION_ERROR) {
527                 if (p[0] == '%') {
528                         switch (p[1]) {
529                         case 'f':
530                                 action_type |= ACTION_SINGLE;
531                                 break;
532                         case 'F':
533                                 action_type |= ACTION_MULTIPLE;
534                                 break;
535                         case 'p':
536                                 action_type |= ACTION_SINGLE;
537                                 break;
538                         default:
539                                 action_type = ACTION_ERROR;
540                                 break;
541                         }
542                 } else if (p[0] == '|') {
543                         if (p[1] == 0x00)
544                                 action_type |= ACTION_PIPE_OUT;
545                 } else if (p[0] == '>') {
546                         if (p[1] == 0x00)
547                                 action_type |= ACTION_INSERT;
548                 } else if (p[0] == '&') {
549                         if (p[1] == 0x00)
550                                 action_type |= ACTION_ASYNC;
551                         else
552                                 action_type = ACTION_ERROR;
553                 }
554                 p++;
555         }
556
557         return action_type;
558 }
559
560 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
561                                GtkCTree *ctree, MimeView *mimeview)
562 {
563         GString *cmd;
564         gchar *p;
565         GList *cur;
566         MsgInfo *msg;
567         
568         p = action;
569         
570         if (p[0] == '|' || p[0] == '>' || p[0] == '*')
571                 p++;
572
573         cmd = g_string_sized_new(strlen(action));
574
575         while (p[0] &&
576                !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
577                 if (p[0] == '%' && p[1]) {
578                         switch (p[1]) {
579                         case 'f':
580                                 if (!parse_append_filename(&cmd, msginfo)) {
581                                         g_string_free(cmd, TRUE);
582                                         return NULL;
583                                 }
584                                 p++;
585                                 break;
586                         case 'F':
587                                 for (cur = GTK_CLIST(ctree)->selection;
588                                      cur != NULL; cur = cur->next) {
589                                         msg = gtk_ctree_node_get_row_data(ctree,
590                                               GTK_CTREE_NODE(cur->data));
591                                         if (!parse_append_filename(&cmd, msg)) {
592                                                 g_string_free(cmd, TRUE);
593                                                 return NULL;
594                                         }
595                                         if (cur->next)
596                                                 cmd = g_string_append_c(cmd, ' ');
597                                 }
598                                 p++;
599                                 break;
600                         case 'p':
601                                 if (!parse_append_msgpart(&cmd, msginfo,
602                                                           mimeview)) {
603                                         g_string_free(cmd, TRUE);
604                                         return NULL;
605                                 }
606                                 p++;
607                                 break;
608                         default:
609                                 cmd = g_string_append_c(cmd, p[0]);
610                                 cmd = g_string_append_c(cmd, p[1]);
611                                 p++;
612                         }
613                 } else {
614                         cmd = g_string_append_c(cmd, p[0]);
615                 }
616                 p++;
617         }
618         if (cmd->len == 0) {
619                 g_string_free(cmd, TRUE);
620                 return NULL;
621         }
622
623         p = cmd->str;
624         g_string_free(cmd, FALSE);
625         return p;
626 }
627
628 static gboolean parse_append_filename(GString **cmd, MsgInfo *msginfo)
629 {
630         gchar *filename;
631
632         g_return_val_if_fail(msginfo, FALSE);
633
634         filename = procmsg_get_message_file(msginfo);
635
636         if (filename) {
637                 *cmd = g_string_append(*cmd, filename);
638                 g_free(filename);
639         } else {
640                 alertpanel_error(_("Could not get message file %d"),
641                                 msginfo->msgnum);
642                 return FALSE;
643         }
644
645         return TRUE;
646 }
647
648 static gboolean parse_append_msgpart(GString **cmd, MsgInfo *msginfo,
649                                      MimeView *mimeview)
650 {
651         gchar    *filename;
652         gchar    *partname;
653         MimeInfo *partinfo;
654         gint      ret;
655         FILE     *fp;
656
657         if (!mimeview) {
658 #if USE_GPGME
659                 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
660                     == NULL) {
661                         alertpanel_error(_("Could not get message file."));
662                         return FALSE;
663                 }
664 #else
665                 if ((fp = procmsg_open_message(msginfo)) == NULL) {
666                         alertpanel_error(_("Could not get message file."));
667                         return FALSE;
668                 }
669                 partinfo = procmime_scan_mime_header(fp);
670 #endif
671                 fclose(fp);
672                 if (!partinfo) {
673                         procmime_mimeinfo_free_all(partinfo);
674                         alertpanel_error(_("Could not get message part."));
675                         return FALSE;
676                 }
677                 filename = procmsg_get_message_file(msginfo);
678         } else {
679                 if (!mimeview->opened) {
680                         alertpanel_error(_("No message part selected."));
681                         return FALSE;
682                 }
683                 if (!mimeview->file) {
684                         alertpanel_error(_("No message file selected."));
685                         return FALSE;
686                 }
687                 partinfo = gtk_ctree_node_get_row_data
688                                 (GTK_CTREE(mimeview->ctree),
689                                  mimeview->opened);
690                 g_return_val_if_fail(partinfo != NULL, FALSE);
691                 filename = mimeview->file;
692         }
693         partname = procmime_get_tmp_file_name(partinfo);
694
695         ret = procmime_get_part(partname, filename, partinfo); 
696
697         if (!mimeview) {
698                 procmime_mimeinfo_free_all(partinfo);
699                 g_free(filename);
700         }
701
702         if (ret < 0) {
703                 alertpanel_error(_("Can't get part of multipart message"));
704                 g_free(partname);
705                 return FALSE;
706         }
707
708         *cmd = g_string_append(*cmd, partname);
709
710         g_free(partname);
711
712         return TRUE;
713 }
714
715 static void prefs_actions_set_dialog(void)
716 {
717         GtkCList *clist = GTK_CLIST(actions.actions_clist);
718         GSList *cur;
719         gchar *action_str[1];
720         gint row;
721
722         gtk_clist_freeze(clist);
723         gtk_clist_clear(clist);
724
725         action_str[0] = _("(New)");
726         row = gtk_clist_append(clist, action_str);
727         gtk_clist_set_row_data(clist, row, NULL);
728
729         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
730                 gchar *action[1];
731
732                 action[0] = (gchar *)cur->data;
733                 row = gtk_clist_append(clist, action);
734                 gtk_clist_set_row_data(clist, row, action[0]);
735         }
736
737         gtk_clist_thaw(clist);
738 }
739
740 static void prefs_actions_set_list(void)
741 {
742         gint row = 1;
743         gchar *action;
744
745         g_slist_free(prefs_common.actions_list);
746         prefs_common.actions_list = NULL;
747
748         while ((action = (gchar *)gtk_clist_get_row_data
749                 (GTK_CLIST(actions.actions_clist), row)) != NULL) {
750                 prefs_common.actions_list =
751                         g_slist_append(prefs_common.actions_list, action);
752                 row++;
753         }
754 }
755
756 #define GET_ENTRY(entry) \
757         entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
758
759 static gint prefs_actions_clist_set_row(gint row)
760 {
761         GtkCList *clist = GTK_CLIST(actions.actions_clist);
762         gchar *entry_text;
763         gint len;
764         gchar action[PREFSBUFSIZE];
765         gchar *buf[1];
766
767         g_return_val_if_fail(row != 0, -1);
768
769         GET_ENTRY(actions.name_entry);
770         if (entry_text[0] == '\0') {
771                 alertpanel_error(_("Menu name is not set."));
772                 return -1;
773         }
774
775         if (strchr(entry_text, ':')) {
776                 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
777                 return -1;
778         }
779
780         strncpy(action, entry_text, PREFSBUFSIZE - 1);
781         g_strstrip(action);
782
783         /* Keep space for the ': ' delimiter */
784         len = strlen(action) + 2;
785         if (len >= PREFSBUFSIZE - 1) {
786                 alertpanel_error(_("Menu name is too long."));
787                 return -1;
788         }
789
790         strcat(action, ": ");
791
792         GET_ENTRY(actions.cmd_entry);
793
794         if (entry_text[0] == '\0') {
795                 alertpanel_error(_("Command line not set."));
796                 return -1;
797         }
798
799         if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
800                 alertpanel_error(_("Menu name and command are too long."));
801                 return -1;
802         }
803
804         if (get_action_type(entry_text) == ACTION_ERROR) {
805                 alertpanel_error(_("The command\n%s\nhas a syntax error."), 
806                                  entry_text);
807                 return -1;
808         }
809
810         strcat(action, entry_text);
811
812         buf[0] = action;
813         if (row < 0)
814                 row = gtk_clist_append(clist, buf);
815         else {
816                 gchar *old_action;
817                 gtk_clist_set_text(clist, row, 0, action);
818                 old_action = (gchar *) gtk_clist_get_row_data(clist, row);
819                 if (old_action)
820                         g_free(old_action);
821         }
822
823         buf[0] = g_strdup(action);
824
825         gtk_clist_set_row_data(clist, row, buf[0]);
826
827         prefs_actions_set_list();
828
829         return 0;
830 }
831
832 /* callback functions */
833
834 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
835 {
836         prefs_actions_clist_set_row(-1);
837 }
838
839 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
840 {
841         GtkCList *clist = GTK_CLIST(actions.actions_clist);
842         gchar *action;
843         gint row;
844
845         if (!clist->selection) return;
846
847         row = GPOINTER_TO_INT(clist->selection->data);
848         if (row == 0) return;
849
850         action = gtk_clist_get_row_data(clist, row);
851         if (!action) return;
852
853         prefs_actions_clist_set_row(row);
854 }
855
856 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
857 {
858         GtkCList *clist = GTK_CLIST(actions.actions_clist);
859         gchar *action;
860         gint row;
861
862         if (!clist->selection) return;
863         row = GPOINTER_TO_INT(clist->selection->data);
864         if (row == 0) return;
865
866         if (alertpanel(_("Delete action"),
867                        _("Do you really want to delete this action?"),
868                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
869                 return;
870
871         action = gtk_clist_get_row_data(clist, row);
872         g_free(action);
873         gtk_clist_remove(clist, row);
874         prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
875                                                    action);
876 }
877
878 static void prefs_actions_up(GtkWidget *w, gpointer data)
879 {
880         GtkCList *clist = GTK_CLIST(actions.actions_clist);
881         gint row;
882
883         if (!clist->selection) return;
884
885         row = GPOINTER_TO_INT(clist->selection->data);
886         if (row > 1)
887                 gtk_clist_row_move(clist, row, row - 1);
888 }
889
890 static void prefs_actions_down(GtkWidget *w, gpointer data)
891 {
892         GtkCList *clist = GTK_CLIST(actions.actions_clist);
893         gint row;
894
895         if (!clist->selection) return;
896
897         row = GPOINTER_TO_INT(clist->selection->data);
898         if (row > 0 && row < clist->rows - 1)
899                 gtk_clist_row_move(clist, row, row + 1);
900 }
901
902 #define ENTRY_SET_TEXT(entry, str) \
903         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
904
905 static void prefs_actions_select(GtkCList *clist, gint row, gint column,
906                                  GdkEvent *event)
907 {
908         gchar *action;
909         gchar *cmd;
910         gchar buf[PREFSBUFSIZE];
911         action = gtk_clist_get_row_data(clist, row);
912
913         if (!action) {
914                 ENTRY_SET_TEXT(actions.name_entry, "");
915                 ENTRY_SET_TEXT(actions.cmd_entry, "");
916                 return;
917         }
918
919         strncpy(buf, action, PREFSBUFSIZE - 1);
920         buf[PREFSBUFSIZE - 1] = 0x00;
921         cmd = strstr(buf, ": ");
922
923         if (cmd && cmd[2])
924                 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
925         else
926                 return;
927
928         *cmd = 0x00;
929         ENTRY_SET_TEXT(actions.name_entry, buf);
930 }
931
932 static void prefs_actions_row_move(GtkCList *clist,
933                                    gint source_row, gint dest_row)
934 {
935         prefs_actions_set_list();
936         if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
937                 gtk_clist_moveto(clist, dest_row, -1,
938                                  source_row < dest_row ? 1.0 : 0.0, 0.0);
939         }
940 }
941
942 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
943                                   gpointer *data)
944 {
945         prefs_actions_cancel(widget, data);
946         return TRUE;
947 }
948
949 static void prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
950                                       gpointer data)
951 {
952         if (event && event->keyval == GDK_Escape)
953                 prefs_actions_cancel(widget, data);
954 }
955
956 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
957 {
958         prefs_actions_read_config();
959         gtk_widget_hide(actions.window);
960         inc_unlock();
961 }
962
963 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
964 {
965         GtkItemFactory *ifactory;
966         MainWindow *mainwin = (MainWindow *)data;
967
968         prefs_actions_write_config();
969         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
970         update_mainwin_actions_menu(ifactory, mainwin);
971         gtk_widget_hide(actions.window);
972         inc_unlock();
973 }
974
975 void update_mainwin_actions_menu(GtkItemFactory *ifactory,
976                                  MainWindow *mainwin)
977 {
978         update_actions_menu(ifactory, "/Tools/Actions",
979                             mainwin_actions_execute_cb,
980                             mainwin);
981 }
982
983 void update_compose_actions_menu(GtkItemFactory *ifactory,
984                                  gchar *branch_path,
985                                  Compose *compose)
986 {
987         update_actions_menu(ifactory, branch_path,
988                             compose_actions_execute_cb,
989                             compose);
990 }
991
992
993 void actions_execute(gpointer data, 
994                      guint action_nb,
995                      GtkWidget *widget,
996                      gint source)
997 {
998         if (source == TOOLBAR_MAIN) 
999                 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
1000         else if (source == TOOLBAR_COMPOSE)
1001                 compose_actions_execute_cb((Compose*)data, action_nb, widget);
1002         else if (source == TOOLBAR_MSGVIEW)
1003                 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);      
1004 }
1005
1006
1007 static void update_actions_menu(GtkItemFactory *ifactory,
1008                                 gchar *branch_path,
1009                                 gpointer callback,
1010                                 gpointer data)
1011 {
1012         GtkWidget *menuitem;
1013         gchar *menu_path;
1014         GSList *cur;
1015         gchar *action, *action_p;
1016         GList *amenu;
1017         GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
1018
1019         ifentry.path = branch_path;
1020         menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
1021         g_return_if_fail(menuitem != NULL);
1022
1023         amenu = GTK_MENU_SHELL(menuitem)->children;
1024         while (amenu != NULL) {
1025                 GList *alist = amenu->next;
1026                 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1027                 amenu = alist;
1028         }
1029
1030         ifentry.accelerator     = NULL;
1031         ifentry.callback_action = 0;
1032         ifentry.callback        = callback;
1033         ifentry.item_type       = NULL;
1034
1035         for (cur = prefs_common.actions_list; cur; cur = cur->next) {
1036                 action   = g_strdup((gchar *)cur->data);
1037                 action_p = strstr(action, ": ");
1038                 if (action_p && action_p[2] &&
1039                     get_action_type(&action_p[2]) != ACTION_ERROR) {
1040                         action_p[0] = 0x00;
1041                         menu_path = g_strdup_printf("%s/%s", branch_path,
1042                                                     action);
1043                         ifentry.path = menu_path;
1044                         gtk_item_factory_create_item(ifactory, &ifentry, data,
1045                                                      1);
1046                         g_free(menu_path);
1047                 }
1048                 g_free(action);
1049                 ifentry.callback_action++;
1050         }
1051 }
1052
1053 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
1054                                        GtkWidget *widget)
1055 {
1056         gchar *buf, *action;
1057         guint action_type;
1058
1059         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1060
1061         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1062         g_return_if_fail(buf != NULL);
1063         action = strstr(buf, ": ");
1064         g_return_if_fail(action != NULL);
1065
1066         /* Point to the beginning of the command-line */
1067         action += 2;
1068
1069         action_type = get_action_type(action);
1070         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
1071                 alertpanel_warning
1072                         (_("The selected action cannot be used in the compose window\n"
1073                            "because it contains %%f, %%F or %%p."));
1074                 return;
1075         }
1076
1077         execute_actions(action, compose->window, NULL, compose->text, NULL, 0,
1078                         NULL);
1079 }
1080
1081 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
1082                                        GtkWidget *widget)
1083 {
1084         MessageView *messageview = mainwin->messageview;
1085         TextView    *textview = NULL;
1086         gchar       *buf,
1087                     *action;
1088         MimeView    *mimeview = NULL;
1089
1090         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1091
1092         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1093
1094         g_return_if_fail(buf);
1095         g_return_if_fail(action = strstr(buf, ": "));
1096
1097         /* Point to the beginning of the command-line */
1098         action += 2;
1099
1100         switch (messageview->type) {
1101         case MVIEW_TEXT:
1102                 if (messageview->textview && messageview->textview->text)
1103                         textview = messageview->textview;
1104                 break;
1105         case MVIEW_MIME:
1106                 if (messageview->mimeview) {
1107                         mimeview = messageview->mimeview;
1108                         if (messageview->mimeview->type == MIMEVIEW_TEXT &&
1109                                         messageview->mimeview->textview &&
1110                                         messageview->mimeview->textview->text)
1111                                 textview = messageview->mimeview->textview;
1112                 } 
1113                 break;
1114         }
1115
1116         execute_actions(action, mainwin->window,
1117                         GTK_CTREE(mainwin->summaryview->ctree), textview->text,
1118                         textview->msgfont, textview->body_pos, mimeview);
1119 }
1120
1121 /* FIXME: Code duplication mainwindow_actions_execute_cb
1122  */
1123 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
1124                                        GtkWidget *widget)
1125 {
1126         TextView    *textview = NULL;
1127         gchar       *buf,
1128                     *action;
1129         MimeView    *mimeview = NULL;
1130
1131         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
1132
1133         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
1134
1135         g_return_if_fail(buf);
1136         g_return_if_fail(action = strstr(buf, ": "));
1137
1138         /* Point to the beginning of the command-line */
1139         action += 2;
1140
1141         switch (msgview->type) {
1142         case MVIEW_TEXT:
1143                 if (msgview->textview && msgview->textview->text)
1144                         textview = msgview->textview;
1145                 break;
1146         case MVIEW_MIME:
1147                 if (msgview->mimeview) {
1148                         mimeview = msgview->mimeview;
1149                         if (msgview->mimeview->type == MIMEVIEW_TEXT &&
1150                             msgview->mimeview->textview &&
1151                             msgview->mimeview->textview->text)
1152                                 textview = msgview->mimeview->textview;
1153                 } 
1154                 break;
1155         }
1156
1157         execute_actions(action, msgview->window, NULL, textview->text,
1158                         textview->msgfont, textview->body_pos, mimeview);
1159 }
1160
1161 static gboolean execute_actions(gchar *action, GtkWidget *window,
1162                                 GtkCTree *ctree, GtkWidget *text, 
1163                                 GdkFont *msgfont, gint body_pos,
1164                                 MimeView *mimeview)
1165 {
1166         GList *cur, *selection = NULL;
1167         GSList *children_list = NULL;
1168         gint is_ok  = TRUE;
1169         gint selection_len = 0;
1170         Children *children;
1171         ChildInfo *child_info;
1172         gint action_type;
1173         MsgInfo *msginfo;
1174         gchar *cmd;
1175
1176         g_return_val_if_fail(action && *action, FALSE);
1177
1178         action_type = get_action_type(action);
1179
1180         if (action_type == ACTION_ERROR)
1181                 return FALSE;         /* ERR: syntax error */
1182
1183         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && 
1184             !(ctree && GTK_CLIST(ctree)->selection))
1185                 return FALSE;         /* ERR: file command without selection */
1186
1187         if (ctree) {
1188                 selection = GTK_CLIST(ctree)->selection;
1189                 selection_len = g_list_length(selection);
1190         }
1191
1192         if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
1193                 if (ctree && selection_len > 1)
1194                         return FALSE; /* ERR: pipe + multiple selection */
1195                 if (!text)
1196                         return FALSE; /* ERR: pipe and no displayed text */
1197         }
1198
1199         children = g_new0(Children, 1);
1200
1201         if (action_type & ACTION_SINGLE) {
1202                 for (cur = selection; cur && is_ok == TRUE; cur = cur->next) {
1203                         msginfo = gtk_ctree_node_get_row_data(ctree,
1204                                         GTK_CTREE_NODE(cur->data));
1205                         if (!msginfo) {
1206                                 is_ok  = FALSE; /* ERR: msginfo missing */
1207                                 break;
1208                         }
1209                         cmd = parse_action_cmd(action, msginfo, ctree,
1210                                                mimeview);
1211                         if (!cmd) {
1212                                 debug_print("Action command error\n");
1213                                 is_ok  = FALSE; /* ERR: incorrect command */
1214                                 break;
1215                         }
1216                         if ((child_info = fork_child(cmd, action_type, text,
1217                                                      msgfont, body_pos,
1218                                                      children))) {
1219                                 children_list = g_slist_append(children_list,
1220                                                                child_info);
1221                                 children->open_in = (selection_len == 1) ?
1222                                                     (action_type &
1223                                                      (ACTION_OPEN_IN |
1224                                                       ACTION_HIDE_IN)) : 0;
1225                         }
1226                         g_free(cmd);
1227                 }
1228         } else {
1229                 cmd = parse_action_cmd(action, NULL, ctree, mimeview);
1230                 if (cmd) {
1231                         if ((child_info = fork_child(cmd, action_type, text,
1232                                                      msgfont, body_pos,
1233                                                      children))) {
1234                                 children_list = g_slist_append(children_list,
1235                                                                child_info);
1236                                 children->open_in = action_type &
1237                                                     (ACTION_OPEN_IN |
1238                                                      ACTION_HIDE_IN);
1239                         }
1240                         g_free(cmd);
1241                 } else
1242                         is_ok  = FALSE;         /* ERR: incorrect command */
1243         }
1244
1245         if (!children_list) {
1246                  /* If not waiting for children, return */
1247                 g_free(children);
1248         } else {
1249                 GSList *cur;
1250
1251                 children->action  = g_strdup(action);
1252                 children->window  = window;
1253                 children->dialog  = NULL;
1254                 children->list    = children_list;
1255                 children->nb      = g_slist_length(children_list);
1256
1257                 for (cur = children_list; cur; cur = cur->next) {
1258                         child_info = (ChildInfo *) cur->data;
1259                         child_info->tag_status = 
1260                                 gdk_input_add(child_info->chld_status,
1261                                               GDK_INPUT_READ,
1262                                               catch_status, child_info);
1263                 }
1264
1265                 create_io_dialog(children);
1266         }
1267
1268         return is_ok;
1269 }
1270
1271 ChildInfo *fork_child(gchar *cmd, gint action_type, GtkWidget *text,
1272                       GdkFont *msgfont, gint body_pos, Children *children)
1273 {
1274         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
1275         gchar *cmdline[4];
1276         gint start, end, is_selection;
1277         gchar *selection;
1278         pid_t pid, gch_pid;
1279         ChildInfo *child_info;
1280         gint sync;
1281
1282         sync = !(action_type & ACTION_ASYNC);
1283
1284         chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
1285                 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
1286
1287         if (sync) {
1288                 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
1289                     pipe(chld_err)) {
1290                         alertpanel_error(_("Command could not be started. "
1291                                            "Pipe creation failed.\n%s"),
1292                                         g_strerror(errno));
1293                         /* Closing fd = -1 fails silently */
1294                         close(chld_in[0]);
1295                         close(chld_in[1]);
1296                         close(chld_out[0]);
1297                         close(chld_out[1]);
1298                         close(chld_err[0]);
1299                         close(chld_err[1]);
1300                         close(chld_status[0]);
1301                         close(chld_status[1]);
1302                         return NULL; /* Pipe error */
1303                 }
1304         }
1305
1306         debug_print("Forking child and grandchild.\n");
1307
1308         pid = fork();
1309         if (pid == 0) { /* Child */
1310                 if (setpgid(0, 0))
1311                         perror("setpgid");
1312
1313                 close(ConnectionNumber(gdk_display));
1314
1315                 gch_pid = fork();
1316
1317                 if (gch_pid == 0) {
1318                         if (setpgid(0, getppid()))
1319                                 perror("setpgid");
1320                         if (sync) {
1321                                 if (action_type &
1322                                     (ACTION_PIPE_IN |
1323                                      ACTION_OPEN_IN |
1324                                      ACTION_HIDE_IN)) {
1325                                         close(fileno(stdin));
1326                                         dup  (chld_in[0]);
1327                                 }
1328                                 close(chld_in[0]);
1329                                 close(chld_in[1]);
1330
1331                                 close(fileno(stdout));
1332                                 dup  (chld_out[1]);
1333                                 close(chld_out[0]);
1334                                 close(chld_out[1]);
1335
1336                                 close(fileno(stderr));
1337                                 dup  (chld_err[1]);
1338                                 close(chld_err[0]);
1339                                 close(chld_err[1]);
1340                         }
1341
1342                         cmdline[0] = "sh";
1343                         cmdline[1] = "-c";
1344                         cmdline[2] = cmd;
1345                         cmdline[3] = 0;
1346                         execvp("/bin/sh", cmdline);
1347
1348                         perror("execvp");
1349                         _exit(1);
1350                 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1351                         if (sync)
1352                                 write(chld_status[1], "1\n", 2);
1353                         perror("fork");
1354                         _exit(1);
1355                 } else {/* Child */
1356                         if (sync) {
1357                                 close(chld_in[0]);
1358                                 close(chld_in[1]);
1359                                 close(chld_out[0]);
1360                                 close(chld_out[1]);
1361                                 close(chld_err[0]);
1362                                 close(chld_err[1]);
1363                                 close(chld_status[0]);
1364                         }
1365                         if (sync) {
1366                                 debug_print("Child: Waiting for grandchild\n");
1367                                 waitpid(gch_pid, NULL, 0);
1368                                 debug_print("Child: grandchild ended\n");
1369                                 write(chld_status[1], "0\n", 2);
1370                                 close(chld_status[1]);
1371                         }
1372                         _exit(0);
1373                 }
1374         } else if (pid < 0) { /* Fork error */
1375                 alertpanel_error(_("Could not fork to execute the following "
1376                                    "command:\n%s\n%s"),
1377                                  cmd, g_strerror(errno));
1378                 return NULL; 
1379         }
1380
1381         /* Parent */
1382
1383         if (!sync) {
1384                 waitpid(pid, NULL, 0);
1385                 return NULL;
1386         }
1387
1388         close(chld_in[0]);
1389         if (!(action_type & (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN)))
1390                 close(chld_in[1]);
1391         close(chld_out[1]);
1392         close(chld_err[1]);
1393         close(chld_status[1]);
1394
1395         child_info = g_new0(ChildInfo, 1);
1396
1397         child_info->children    = children;
1398
1399         child_info->pid         = pid;
1400         child_info->cmd         = g_strdup(cmd);
1401         child_info->type        = action_type;
1402         child_info->new_out     = FALSE;
1403         child_info->output      = g_string_sized_new(0);
1404         child_info->chld_in     =
1405                 (action_type &
1406                  (ACTION_PIPE_IN | ACTION_OPEN_IN | ACTION_HIDE_IN))
1407                         ? chld_in [1] : -1;
1408         child_info->chld_out    = chld_out[0];
1409         child_info->chld_err    = chld_err[0];
1410         child_info->chld_status = chld_status[0];
1411         child_info->tag_in      = -1;
1412         child_info->tag_out     = gdk_input_add(chld_out[0], GDK_INPUT_READ,
1413                                                 catch_output, child_info);
1414         child_info->tag_err     = gdk_input_add(chld_err[0], GDK_INPUT_READ,
1415                                                 catch_output, child_info);
1416
1417         if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1418                 return child_info;
1419
1420         child_info->text        = text;
1421         child_info->msgfont     = msgfont;
1422
1423         start = body_pos;
1424         end   = gtk_stext_get_length(GTK_STEXT(text));
1425
1426         if (GTK_EDITABLE(text)->has_selection) {
1427                 start = GTK_EDITABLE(text)->selection_start_pos;
1428                 end   = GTK_EDITABLE(text)->selection_end_pos;
1429                 if (start > end) {
1430                         gint tmp;
1431                         tmp = start;
1432                         start = end;
1433                         end = tmp;
1434                 }
1435                 is_selection = TRUE;
1436                 if (start == end) {
1437                         start = 0;
1438                         end = gtk_stext_get_length(GTK_STEXT(text));
1439                         is_selection = FALSE;
1440                 }
1441         }
1442
1443         selection = gtk_editable_get_chars(GTK_EDITABLE(text), start, end);
1444
1445         if (action_type & ACTION_PIPE_IN) {
1446                 write(chld_in[1], selection, strlen(selection));
1447                 if (!(action_type & (ACTION_OPEN_IN | ACTION_HIDE_IN)))
1448                         close(chld_in[1]);
1449                 child_info->chld_in = -1; /* No more input */
1450         }
1451         g_free(selection);
1452
1453         gtk_stext_freeze(GTK_STEXT(text));
1454         if (action_type & ACTION_PIPE_OUT) {
1455                 gtk_stext_set_point(GTK_STEXT(text), start);
1456                 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
1457         }
1458
1459         gtk_stext_thaw(GTK_STEXT(text));
1460
1461         return child_info;
1462 }
1463
1464 static void kill_children_cb(GtkWidget *widget, gpointer data)
1465 {
1466         GSList *cur;
1467         Children *children = (Children *) data;
1468         ChildInfo *child_info;
1469
1470         for (cur = children->list; cur; cur = cur->next) {
1471                 child_info = (ChildInfo *)(cur->data);
1472                 debug_print("Killing child group id %d\n", child_info->pid);
1473                 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
1474                         perror("kill");
1475         }
1476 }
1477
1478 static gint wait_for_children(gpointer data)
1479 {
1480         gboolean new_output;
1481         Children *children = (Children *)data;
1482         ChildInfo *child_info;
1483         GSList *cur;
1484         gint nb = children->nb;
1485
1486         children->nb = 0;
1487
1488         cur = children->list;
1489         new_output = FALSE;
1490         while (cur) {
1491                 child_info = (ChildInfo *)cur->data;
1492                 if (child_info->pid)
1493                         children->nb++;
1494                 new_output |= child_info->new_out;
1495                 cur = cur->next;
1496         }
1497
1498         children->output |= new_output;
1499
1500         if (new_output || (children->dialog && (nb != children->nb)))
1501                 update_io_dialog(children);
1502
1503         if (children->nb)
1504                 return FALSE;
1505
1506         if (!children->dialog) {
1507                 free_children(children);
1508         } else if (!children->output) {
1509                 gtk_widget_destroy(children->dialog);
1510         }
1511
1512         return FALSE;
1513 }
1514
1515 static void send_input(GtkWidget *w, gpointer data)
1516 {
1517         Children *children = (Children *) data;
1518         ChildInfo *child_info = (ChildInfo *) children->list->data;
1519
1520         child_info->tag_in = gdk_input_add(child_info->chld_in,
1521                                            GDK_INPUT_WRITE,
1522                                            catch_input, children);
1523         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1524 }
1525
1526 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1527 {
1528         hide_io_dialog_cb(w, data);
1529         return TRUE;
1530 }
1531
1532 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1533 {
1534
1535         Children *children = (Children *)data;
1536
1537         if (!children->nb) {
1538                 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
1539                                               children);
1540                 gtk_widget_destroy(children->dialog);
1541                 free_children(children);
1542         }
1543 }
1544
1545 static void childinfo_close_pipes(ChildInfo *child_info)
1546 {
1547         if (child_info->tag_in > 0)
1548                 gdk_input_remove(child_info->tag_in);
1549         gdk_input_remove(child_info->tag_out);
1550         gdk_input_remove(child_info->tag_err);
1551
1552         if (child_info->chld_in >= 0)
1553                 close(child_info->chld_in);
1554         close(child_info->chld_out);
1555         close(child_info->chld_err);
1556         close(child_info->chld_status);
1557 }
1558
1559 static void free_children(Children *children)
1560 {
1561         GSList *cur;
1562         ChildInfo *child_info;
1563
1564         debug_print("Freeing children data %p\n", children);
1565
1566         g_free(children->action);
1567         for (cur = children->list; cur;) {
1568                 child_info = (ChildInfo *)cur->data;
1569                 g_free(child_info->cmd);
1570                 g_string_free(child_info->output, TRUE);
1571                 children->list = g_slist_remove(children->list, child_info);
1572                 g_free(child_info);
1573                 cur = children->list;
1574         }
1575         g_free(children);
1576 }
1577
1578 static void update_io_dialog(Children *children)
1579 {
1580         GSList *cur;
1581
1582         debug_print("Updating actions input/output dialog.\n");
1583
1584         if (!children->nb) {
1585                 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1586                 gtk_widget_set_sensitive(children->close_btn, TRUE);
1587                 if (children->input_hbox)
1588                         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1589         }
1590
1591         if (children->output) {
1592                 GtkWidget *text = children->text;
1593                 gchar *caption;
1594                 ChildInfo *child_info;
1595
1596                 gtk_widget_show(children->scrolledwin);
1597                 gtk_text_freeze(GTK_TEXT(text));
1598                 gtk_text_set_point(GTK_TEXT(text), 0);
1599                 gtk_text_forward_delete(GTK_TEXT(text), 
1600                                         gtk_text_get_length(GTK_TEXT(text)));
1601                 for (cur = children->list; cur; cur = cur->next) {
1602                         child_info = (ChildInfo *)cur->data;
1603                         if (child_info->pid)
1604                                 caption = g_strdup_printf
1605                                         (_("--- Running: %s\n"),
1606                                          child_info->cmd);
1607                         else
1608                                 caption = g_strdup_printf
1609                                         (_("--- Ended: %s\n"),
1610                                          child_info->cmd);
1611
1612                         gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1613                                         caption, -1);
1614                         gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1615                                         child_info->output->str, -1);
1616                         g_free(caption);
1617                         child_info->new_out = FALSE;
1618                 }
1619                 gtk_text_thaw(GTK_TEXT(text));
1620         }
1621 }
1622
1623 static void create_io_dialog(Children *children)
1624 {
1625         GtkWidget *dialog;
1626         GtkWidget *vbox;
1627         GtkWidget *entry = NULL;
1628         GtkWidget *input_hbox = NULL;
1629         GtkWidget *send_button;
1630         GtkWidget *label;
1631         GtkWidget *text;
1632         GtkWidget *scrolledwin;
1633         GtkWidget *hbox;
1634         GtkWidget *abort_button;
1635         GtkWidget *close_button;
1636
1637         debug_print("Creating action IO dialog\n");
1638
1639         dialog = gtk_dialog_new();
1640         gtk_container_set_border_width
1641                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1642         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1643         gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1644         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1645         manage_window_set_transient(GTK_WINDOW(dialog));
1646         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1647                         GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1648         gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1649                         GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1650                         children);
1651
1652         vbox = gtk_vbox_new(FALSE, 8);
1653         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1654         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1655         gtk_widget_show(vbox);
1656
1657         label = gtk_label_new(children->action);
1658         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1659         gtk_widget_show(label);
1660
1661         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1662         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1663                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1664         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1665         gtk_widget_set_usize(scrolledwin, 480, 200);
1666         gtk_widget_hide(scrolledwin);
1667
1668         text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1669                             (GTK_SCROLLED_WINDOW(scrolledwin)),
1670                             gtk_scrolled_window_get_vadjustment
1671                             (GTK_SCROLLED_WINDOW(scrolledwin)));
1672         gtk_text_set_editable(GTK_TEXT(text), FALSE);
1673         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1674         gtk_widget_show(text);
1675
1676         if (children->open_in) {
1677                 input_hbox = gtk_hbox_new(FALSE, 8);
1678                 gtk_widget_show(input_hbox);
1679
1680                 entry = gtk_entry_new();
1681                 gtk_widget_set_usize(entry, 320, -1);
1682                 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1683                                    GTK_SIGNAL_FUNC(send_input), children);
1684                 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1685                 if (children->open_in & ACTION_HIDE_IN)
1686                         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1687                 gtk_widget_show(entry);
1688
1689                 send_button = gtk_button_new_with_label(_(" Send "));
1690                 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1691                                    GTK_SIGNAL_FUNC(send_input), children);
1692                 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1693                                    FALSE, 0);
1694                 gtk_widget_show(send_button);
1695
1696                 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1697                 gtk_widget_grab_focus(entry);
1698         }
1699
1700         gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1701                                 &close_button, _("Close"), NULL, NULL);
1702         gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1703                         GTK_SIGNAL_FUNC(kill_children_cb), children);
1704         gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1705                         GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1706         gtk_widget_show(hbox);
1707
1708         if (children->nb)
1709                 gtk_widget_set_sensitive(close_button, FALSE);
1710
1711         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1712
1713         children->dialog      = dialog;
1714         children->scrolledwin = scrolledwin;
1715         children->text        = text;
1716         children->input_hbox  = children->open_in ? input_hbox : NULL;
1717         children->input_entry = children->open_in ? entry : NULL;
1718         children->abort_btn   = abort_button;
1719         children->close_btn   = close_button;
1720
1721         gtk_widget_show(dialog);
1722 }
1723
1724 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1725 {
1726         ChildInfo *child_info = (ChildInfo *)data;
1727         gchar buf;
1728         gint c;
1729
1730         gdk_input_remove(child_info->tag_status);
1731
1732         c = read(source, &buf, 1);
1733         debug_print("Child returned %c\n", buf);
1734
1735         waitpid(-child_info->pid, NULL, 0);
1736         childinfo_close_pipes(child_info);
1737         child_info->pid = 0;
1738
1739         wait_for_children(child_info->children);
1740 }
1741         
1742 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1743 {
1744         Children *children = (Children *)data;
1745         ChildInfo *child_info = (ChildInfo *)children->list->data;
1746         gchar *input;
1747         gint c;
1748
1749         debug_print("Sending input to grand child.\n");
1750         if (!(cond && GDK_INPUT_WRITE))
1751                 return;
1752
1753         gdk_input_remove(child_info->tag_in);
1754         child_info->tag_in = -1;
1755
1756         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1757                                        0, -1);
1758         c = write(child_info->chld_in, input, strlen(input));
1759
1760         g_free(input);
1761
1762         write(child_info->chld_in, "\n", 2);
1763
1764         gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1765         gtk_widget_set_sensitive(children->input_hbox, TRUE);
1766         debug_print("Input to grand child sent.\n");
1767 }
1768
1769 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1770 {
1771         ChildInfo *child_info = (ChildInfo *)data;
1772         gint c, i;
1773         gchar buf[PREFSBUFSIZE];
1774
1775         debug_print("Catching grand child's output.\n");
1776         if (child_info->type & (ACTION_PIPE_OUT | ACTION_INSERT)
1777             && source == child_info->chld_out) {
1778                 gboolean is_selection = FALSE;
1779                 GtkWidget *text = child_info->text;
1780                 if (GTK_EDITABLE(text)->has_selection)
1781                         is_selection = TRUE;
1782                 gtk_stext_freeze(GTK_STEXT(text));
1783                 while (TRUE) {
1784                         c = read(source, buf, PREFSBUFSIZE - 1);
1785                         if (c == 0)
1786                                 break;
1787                         gtk_stext_insert(GTK_STEXT(text), child_info->msgfont,
1788                                          NULL, NULL, buf, c);
1789                 }
1790                 if (is_selection) {
1791                         /* Using the select_region draws things. Should not.
1792                          * so we just change selection position and 
1793                          * defere drawing when thawing. Hack?
1794                          */
1795                         GTK_EDITABLE(text)->selection_end_pos =
1796                                         gtk_stext_get_point(GTK_STEXT(text));
1797                 }
1798                 gtk_stext_thaw(GTK_STEXT(child_info->text));
1799         } else {
1800                 c = read(source, buf, PREFSBUFSIZE - 1);
1801                 for (i = 0; i < c; i++)
1802                         child_info->output = g_string_append_c
1803                                 (child_info->output, buf[i]);
1804                 if (c > 0)
1805                         child_info->new_out = TRUE;
1806         }
1807         wait_for_children(child_info->children);
1808 }
1809
1810 /*
1811  * Strings describing action format strings
1812  * 
1813  * When adding new lines, remember to put one string for each line
1814  */
1815 static gchar *actions_desc_strings[] = {
1816         N_("Menu name:"),
1817         N_("   Use / in menu name to make submenus."),
1818         "",
1819         N_("Command line:"),
1820         N_("   Begin with:"),
1821         N_("     | to send message body or selection to command"),
1822         N_("     > to send user provided text to command"),
1823         N_("     * to send user provided hidden text to command"),
1824         N_(" End with:"),
1825         N_("     | to replace message body or selection with command output"),
1826         N_("     > to insert command's output without replacing old text"),
1827         N_("     & to run command asynchronously"),
1828         N_(" Use %f for message file name"),
1829         N_("     %F for the list of the file names of selected messages"),
1830         N_("     %p for the selected message MIME part."),
1831         NULL
1832 };
1833
1834
1835 static DescriptionWindow actions_desc_win = { 
1836         NULL, 
1837         1,
1838         N_("Description of symbols"),
1839         actions_desc_strings
1840 };
1841
1842
1843 static void prefs_actions_help_cb(GtkWidget *w, gpointer data)
1844 {
1845         description_window_create(&actions_desc_win);
1846 }