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