2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 Hiroyuki Yamamoto & The Claws Mail Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #ifdef GDK_WINDOWING_X11
31 # include <gdk/gdkx.h>
32 #endif /* GDK_WINDOWING_X11 */
37 #include <sys/types.h>
39 # include <sys/wait.h>
46 #include "manage_window.h"
47 #include "mainwindow.h"
48 #include "prefs_common.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
55 #include "matcher_parser.h" /* CLAWS */
56 #include "filtering.h"
57 #include "procheader.h"
59 typedef struct _Children Children;
60 typedef struct _ChildInfo ChildInfo;
61 typedef struct _UserStringDialog UserStringDialog;
67 GtkWidget *input_entry;
68 GtkWidget *input_hbox;
69 GtkWidget *progress_bar;
72 GtkWidget *scrolledwin;
75 ActionType action_type;
84 gboolean is_selection;
104 void (*callback)(void *data);
107 GSList *msginfo_list;
110 static void action_update_menu (GtkUIManager *ui_manager,
111 const gchar *accel_group,
115 static void compose_actions_execute_cb (GtkWidget *widget,
117 static void compose_actions_execute (Compose *compose,
121 static void mainwin_actions_execute_cb (GtkWidget *widget,
123 static void mainwin_actions_execute (MainWindow *mainwin,
127 static void msgview_actions_execute_cb (GtkWidget *widget,
129 static void msgview_actions_execute (MessageView *msgview,
133 static void message_actions_execute (MessageView *msgview,
137 static gboolean execute_filtering_actions(gchar *action,
140 static gboolean execute_actions (gchar *action,
145 void (*callback)(void *data),
148 static gchar *parse_action_cmd (gchar *action,
152 const gchar *user_str,
153 const gchar *user_hidden_str,
154 const gchar *sel_str);
155 static gboolean parse_append_filename (GString *cmd,
158 static gboolean parse_append_msgpart (GString *cmd,
162 static ChildInfo *fork_child (gchar *cmd,
163 const gchar *msg_str,
166 static gint wait_for_children (Children *children);
168 static void free_children (Children *children);
170 static void childinfo_close_pipes (ChildInfo *child_info);
172 static void create_io_dialog (Children *children);
173 static void update_io_dialog (Children *children);
175 static void hide_io_dialog_cb (GtkWidget *widget,
177 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
181 static void catch_output (gpointer data,
183 GdkInputCondition cond);
184 static void catch_input (gpointer data,
186 GdkInputCondition cond);
187 static void catch_status (gpointer data,
189 GdkInputCondition cond);
191 static gchar *get_user_string (const gchar *action,
195 ActionType action_get_type(const gchar *action_str)
198 gboolean in_filtering_action = FALSE;
199 ActionType action_type = ACTION_NONE;
201 cm_return_val_if_fail(action_str, ACTION_ERROR);
202 cm_return_val_if_fail(*action_str, ACTION_ERROR);
207 action_type |= ACTION_PIPE_IN;
209 } else if (p[0] == '>') {
210 action_type |= ACTION_USER_IN;
212 } else if (p[0] == '*') {
213 action_type |= ACTION_USER_HIDDEN_IN;
220 while (*p && action_type != ACTION_ERROR) {
221 if (!in_filtering_action) {
222 if (p[0] == '%' && p[1]) {
225 /* CLAWS: filtering action is a mutually exclusive
226 * action. we can enable others if needed later. we
227 * add ACTION_SINGLE | ACTION_MULTIPLE so it will
228 * only be executed from the main window toolbar */
229 if (p[2] == 's') /* source messages */
230 action_type = ACTION_FILTERING_ACTION
233 in_filtering_action = TRUE;
236 action_type |= ACTION_SINGLE;
239 action_type |= ACTION_MULTIPLE;
242 action_type |= ACTION_SINGLE;
245 action_type |= ACTION_SELECTION_STR;
248 action_type |= ACTION_USER_STR;
251 action_type |= ACTION_USER_HIDDEN_STR;
257 action_type = ACTION_ERROR;
261 } else if (p[0] == '|') {
263 action_type |= ACTION_PIPE_OUT;
264 } else if (p[0] == '>') {
266 action_type |= ACTION_INSERT;
267 } else if (p[0] == '&') {
269 action_type |= ACTION_ASYNC;
270 } else if (p[0] == '}') {
271 in_filtering_action = FALSE;
280 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
281 GSList *msg_list, MimeInfo *partinfo,
282 const gchar *user_str,
283 const gchar *user_hidden_str,
284 const gchar *sel_str)
292 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
295 cmd = g_string_sized_new(strlen(action));
298 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
299 if (p[0] == '%' && p[1]) {
302 if (!parse_append_filename(cmd, msginfo)) {
303 g_string_free(cmd, TRUE);
309 for (cur = msg_list; cur != NULL;
311 MsgInfo *msg = (MsgInfo *)cur->data;
313 if (!parse_append_filename(cmd, msg)) {
314 g_string_free(cmd, TRUE);
318 g_string_append_c(cmd, ' ');
323 if (!parse_append_msgpart(cmd, msginfo,
325 g_string_free(cmd, TRUE);
332 g_string_append(cmd, sel_str);
337 g_string_append(cmd, user_str);
342 g_string_append(cmd, user_hidden_str);
346 g_string_append_c(cmd, p[1]);
350 g_string_append_c(cmd, p[0]);
351 g_string_append_c(cmd, p[1]);
355 g_string_append_c(cmd, p[0]);
360 g_string_free(cmd, TRUE);
365 g_string_free(cmd, FALSE);
369 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
373 gchar escape_ch[] = "\\ ";
375 cm_return_val_if_fail(msginfo, FALSE);
377 filename = procmsg_get_message_file(msginfo);
380 alertpanel_error(_("Could not get message file %d"),
386 while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
389 g_string_append(cmd, p);
390 g_string_append(cmd, escape_ch);
393 g_string_append(cmd, p);
400 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
403 gboolean single_part = FALSE;
405 gchar *part_filename;
409 partinfo = procmime_scan_message(msginfo);
411 alertpanel_error(_("Could not get message part."));
418 filename = procmsg_get_message_file_path(msginfo);
419 part_filename = procmime_get_tmp_file_name(partinfo);
421 ret = procmime_get_part(part_filename, partinfo);
424 procmime_mimeinfo_free_all(partinfo);
428 alertpanel_error(_("Can't get part of multipart message: %s"), strerror(-ret));
429 g_free(part_filename);
433 g_string_append(cmd, part_filename);
435 g_free(part_filename);
440 void actions_execute(gpointer data,
445 if (source == TOOLBAR_MAIN)
446 mainwin_actions_execute((MainWindow*)data, action_nb, widget);
447 else if (source == TOOLBAR_COMPOSE)
448 compose_actions_execute((Compose*)data, action_nb, widget);
449 else if (source == TOOLBAR_MSGVIEW)
450 msgview_actions_execute((MessageView*)data, action_nb, widget);
453 void action_update_mainwin_menu(GtkUIManager *ui_manager,
457 action_update_menu(ui_manager, "<MainwinActions>", branch_path,
458 mainwin_actions_execute_cb, mainwin);
461 void action_update_msgview_menu(GtkUIManager *ui_manager,
463 MessageView *msgview)
465 action_update_menu(ui_manager, "<MsgviewActions>", branch_path,
466 msgview_actions_execute_cb, msgview);
469 void action_update_compose_menu(GtkUIManager *ui_manager,
473 action_update_menu(ui_manager, "<ComposeActions>", branch_path,
474 compose_actions_execute_cb, compose);
477 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name)
479 GList *amenu = GTK_MENU_SHELL(menu)->children;
480 const gchar *existing_name;
482 GtkWidget *item = GTK_WIDGET(amenu->data);
483 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL &&
484 !strcmp2(name, existing_name))
491 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action)
493 gchar *submenu = g_strdup(action);
494 GtkWidget *new_menu = NULL;
496 if (strchr(submenu, '/')) {
497 const gchar *end = (strchr(submenu, '/')+1);
498 GtkWidget *menu_item = NULL;
500 *strchr(submenu, '/') = '\0';
501 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) {
502 menu_item = gtk_menu_item_new_with_mnemonic(submenu);
503 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free);
504 gtk_widget_show(menu_item);
505 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
506 new_menu = gtk_menu_new();
507 gtk_widget_show(new_menu);
508 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu);
510 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
512 new_menu = create_submenus(new_menu, end);
516 return new_menu ? new_menu : menu;
519 static void action_update_menu(GtkUIManager *ui_manager,
520 const gchar *accel_group,
522 gpointer callback, gpointer data)
525 gchar *action, *action_p;
526 int callback_action = 0;
527 GtkWidget *menu = gtk_menu_new();
530 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
531 GtkWidget *cur_menu = menu;
532 const gchar *action_name = NULL;
533 action = g_strdup((gchar *)cur->data);
534 action_p = strstr(action, ": ");
535 if (action_p && action_p[2] &&
536 (action_get_type(&action_p[2]) != ACTION_ERROR) &&
537 (action[0] != '/')) {
538 gchar *accel_path = NULL;
541 if (strchr(action, '/')) {
542 cur_menu = create_submenus(cur_menu, action);
543 action_name = strrchr(action, '/')+1;
545 action_name = action;
547 gtk_menu_set_accel_group (GTK_MENU (cur_menu),
548 gtk_ui_manager_get_accel_group(ui_manager));
549 item = gtk_menu_item_new_with_label(action_name);
550 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item);
551 g_signal_connect(G_OBJECT(item), "activate",
552 G_CALLBACK(callback), data);
553 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action));
554 gtk_widget_show(item);
555 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL);
556 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path);
564 gtk_widget_show(menu);
565 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu);
568 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data)
570 Compose *compose = (Compose *)data;
571 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
572 compose_actions_execute(compose, action_nb, NULL);
575 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget)
578 ActionType action_type;
580 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
582 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
583 cm_return_if_fail(buf != NULL);
584 action = strstr(buf, ": ");
585 cm_return_if_fail(action != NULL);
587 /* Point to the beginning of the command-line */
590 action_type = action_get_type(action);
591 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
593 (_("The selected action cannot be used in the compose window\n"
594 "because it contains %%f, %%F, %%as or %%p."));
598 execute_actions(action, NULL, compose->text, 0, NULL,
599 compose_action_cb, compose);
602 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data)
604 MainWindow *mainwin = (MainWindow *)data;
605 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
606 mainwin_actions_execute(mainwin, action_nb, NULL);
609 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb,
614 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
615 message_actions_execute(mainwin->messageview, action_nb, msg_list);
616 g_slist_free(msg_list);
619 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data)
621 MessageView *msgview = (MessageView *)data;
622 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num"));
623 msgview_actions_execute(msgview, action_nb, NULL);
626 static void msgview_actions_execute(MessageView *msgview, guint action_nb,
629 GSList *msg_list = NULL;
631 if (msgview->msginfo)
632 msg_list = g_slist_append(msg_list, msgview->msginfo);
633 message_actions_execute(msgview, action_nb, msg_list);
634 g_slist_free(msg_list);
637 static void message_actions_execute(MessageView *msgview, guint action_nb,
644 GtkWidget *text = NULL;
646 ActionType action_type;
648 cm_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
650 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
652 cm_return_if_fail(buf);
653 cm_return_if_fail((action = strstr(buf, ": ")));
655 /* Point to the beginning of the command-line */
658 textview = messageview_get_current_textview(msgview);
660 text = textview->text;
661 body_pos = textview->body_pos;
663 partinfo = messageview_get_selected_mime_part(msgview);
665 /* this command will alter the message text */
666 action_type = action_get_type(action);
667 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
668 msgview->filtered = TRUE;
670 if (action_type & ACTION_FILTERING_ACTION)
671 /* CLAWS: most of the above code is not necessary for applying
673 execute_filtering_actions(action, msg_list);
675 execute_actions(action, msg_list, text, body_pos, partinfo,
679 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
681 GSList *action_list, *p;
682 const gchar *sbegin, *send;
683 gchar *action_string;
684 SummaryView *summaryview = NULL;
685 MainWindow *mainwin = NULL;
687 if (mainwindow_get_mainwindow()) {
688 summaryview = mainwindow_get_mainwindow()->summaryview;
689 mainwin = mainwindow_get_mainwindow();
692 if (NULL == (sbegin = strstr2(action, "%as{")))
694 sbegin += sizeof "%as{" - 1;
695 if (NULL == (send = strrchr(sbegin, '}')))
697 action_string = g_strndup(sbegin, send - sbegin);
699 action_list = matcher_parser_get_action_list(action_string);
700 if (action_list == NULL) {
701 gchar *tmp = g_strdup(action_string);
705 alertpanel_error(_("There is no filtering action set"));
707 alertpanel_error(_("Invalid filtering action(s):\n%s"), tmp);
708 g_free(action_string);
712 g_free(action_string);
714 /* apply actions on each message info */
715 for (p = msglist; p && p->data; p = g_slist_next(p)) {
716 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
720 summary_lock(summaryview);
721 main_window_cursor_wait(mainwin);
722 summary_freeze(summaryview);
723 folder_item_update_freeze();
726 filtering_move_and_copy_msgs(msglist);
729 folder_item_update_thaw();
730 summary_thaw(summaryview);
731 main_window_cursor_normal(mainwin);
732 summary_unlock(summaryview);
733 summary_show(summaryview, summaryview->folder_item);
735 for (p = action_list; p; p = g_slist_next(p))
736 if (p->data) filteringaction_free(p->data);
737 g_slist_free(action_list);
741 static gboolean execute_actions(gchar *action, GSList *msg_list,
743 gint body_pos, MimeInfo *partinfo,
744 void (*callback)(void *data), void *data)
746 GSList *children_list = NULL;
750 ChildInfo *child_info;
751 ActionType action_type;
754 gchar *sel_str = NULL;
755 gchar *msg_str = NULL;
756 gchar *user_str = NULL;
757 gchar *user_hidden_str = NULL;
758 GtkTextIter start_iter, end_iter;
759 gboolean is_selection = FALSE;
761 cm_return_val_if_fail(action && *action, FALSE);
763 action_type = action_get_type(action);
765 if (action_type == ACTION_ERROR)
766 return FALSE; /* ERR: syntax error */
768 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
769 return FALSE; /* ERR: file command without selection */
771 msg_list_len = g_slist_length(msg_list);
773 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
774 if (msg_list_len > 1)
775 return FALSE; /* ERR: pipe + multiple selection */
777 return FALSE; /* ERR: pipe and no displayed text */
780 if (action_type & ACTION_SELECTION_STR) {
782 return FALSE; /* ERR: selection string but no text */
786 GtkTextBuffer *textbuf;
788 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
789 is_selection = gtk_text_buffer_get_selection_bounds
790 (textbuf, &start_iter, &end_iter);
792 gtk_text_buffer_get_iter_at_offset
793 (textbuf, &start_iter, body_pos);
794 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
796 msg_str = gtk_text_buffer_get_text
797 (textbuf, &start_iter, &end_iter, FALSE);
799 sel_str = g_strdup(msg_str);
802 if (action_type & ACTION_USER_STR) {
803 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
810 if (action_type & ACTION_USER_HIDDEN_STR) {
811 if (!(user_hidden_str =
812 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
820 if (text && (action_type & ACTION_PIPE_OUT)) {
821 GtkTextBuffer *textbuf;
822 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
823 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
826 children = g_new0(Children, 1);
828 children->action = g_strdup(action);
829 children->action_type = action_type;
830 children->msg_text = text;
831 children->is_selection = is_selection;
833 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
834 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
835 children->open_in = 1;
837 if (action_type & ACTION_SINGLE) {
840 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
841 msginfo = (MsgInfo *)cur->data;
843 is_ok = FALSE; /* ERR: msginfo missing */
846 cmd = parse_action_cmd(action, msginfo, msg_list,
848 user_hidden_str, sel_str);
850 debug_print("Action command error\n");
851 is_ok = FALSE; /* ERR: incorrect command */
854 if ((child_info = fork_child(cmd, msg_str, children))) {
855 /* Pass msginfo to catch_status () */
856 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
857 child_info->msginfo_list =
858 g_slist_append (NULL, msginfo);
859 children_list = g_slist_append(children_list,
865 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
866 user_str, user_hidden_str, sel_str);
868 if ((child_info = fork_child(cmd, msg_str, children))) {
869 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT)))
870 child_info->msginfo_list =
871 g_slist_copy (msg_list);
872 children_list = g_slist_append(children_list,
877 is_ok = FALSE; /* ERR: incorrect command */
883 g_free(user_hidden_str);
885 if (!children_list) {
886 /* If not waiting for children, return */
887 free_children(children);
891 children->list = children_list;
892 children->nb = g_slist_length(children_list);
893 children->initial_nb = children->nb;
895 for (cur = children_list; cur; cur = cur->next) {
896 child_info = (ChildInfo *) cur->data;
897 child_info->callback = callback;
898 child_info->data = data;
899 child_info->tag_status =
900 claws_input_add(child_info->chld_status,
902 catch_status, child_info,
906 create_io_dialog(children);
911 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
915 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
916 gchar *cmdline[4], *ret_str;
918 ChildInfo *child_info;
920 gssize by_written = 0, by_read = 0;
922 sync = !(children->action_type & ACTION_ASYNC);
924 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
925 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
928 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
930 alertpanel_error(_("Command could not be started. "
931 "Pipe creation failed.\n%s"),
933 /* Closing fd = -1 fails silently */
934 (void)close(chld_in[0]);
935 (void)close(chld_in[1]);
936 (void)close(chld_out[0]);
937 (void)close(chld_out[1]);
938 (void)close(chld_err[0]);
939 (void)close(chld_err[1]);
940 (void)close(chld_status[0]);
941 (void)close(chld_status[1]);
942 return NULL; /* Pipe error */
946 debug_print("Forking child and grandchild.\n");
947 debug_print("Executing: /bin/sh -c %s\n", cmd);
950 if (pid == 0) { /* Child */
958 if (setpgid(0, getppid()))
962 if (children->action_type &
965 ACTION_USER_HIDDEN_IN)) {
966 r |= close(fileno(stdin));
967 (void) dup (chld_in[0]);
969 r |= close(chld_in[0]);
970 r |= close(chld_in[1]);
972 r |= close(fileno(stdout));
973 (void) dup (chld_out[1]);
974 r |= close(chld_out[0]);
975 r |= close(chld_out[1]);
977 r |= close(fileno(stderr));
978 (void) dup (chld_err[1]);
979 r |= close(chld_err[0]);
980 r |= close(chld_err[1]);
983 debug_print("%s(%d)", strerror(errno), errno);
988 ret_str = g_locale_from_utf8(cmd, strlen(cmd),
989 &by_read, &by_written,
991 if (ret_str && by_written)
992 cmdline[2] = ret_str;
996 execvp("/bin/sh", cmdline);
1001 } else if (gch_pid < (pid_t) 0) { /* Fork error */
1003 r = write(chld_status[1], "1\n", 2);
1005 debug_print("%s(%d)", strerror(errno), errno);
1008 } else { /* Child */
1010 r |= close(chld_in[0]);
1011 r |= close(chld_in[1]);
1012 r |= close(chld_out[0]);
1013 r |= close(chld_out[1]);
1014 r |= close(chld_err[0]);
1015 r |= close(chld_err[1]);
1016 r |= close(chld_status[0]);
1018 debug_print("Child: waiting for grandchild\n");
1019 r |= waitpid(gch_pid, NULL, 0);
1020 debug_print("Child: grandchild ended\n");
1021 r |= write(chld_status[1], "0\n", 2);
1022 r |= close(chld_status[1]);
1025 debug_print("%s(%d)", strerror(errno), errno);
1029 } else if (pid < 0) { /* Fork error */
1030 alertpanel_error(_("Could not fork to execute the following "
1031 "command:\n%s\n%s"),
1032 cmd, g_strerror(errno));
1039 waitpid(pid, NULL, 0);
1043 (void)close(chld_in[0]);
1044 if (!(children->action_type &
1045 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1046 (void)close(chld_in[1]);
1047 (void)close(chld_out[1]);
1048 (void)close(chld_err[1]);
1049 (void)close(chld_status[1]);
1051 child_info = g_new0(ChildInfo, 1);
1053 child_info->children = children;
1055 child_info->pid = pid;
1056 child_info->next_sig = SIGTERM;
1057 child_info->cmd = g_strdup(cmd);
1058 child_info->new_out = FALSE;
1059 child_info->output = g_string_new(NULL);
1060 child_info->chld_in =
1061 (children->action_type &
1062 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
1064 child_info->chld_out = chld_out[0];
1065 child_info->chld_err = chld_err[0];
1066 child_info->chld_status = chld_status[0];
1067 child_info->tag_in = -1;
1068 child_info->tag_out = claws_input_add(chld_out[0], GDK_INPUT_READ,
1069 catch_output, child_info, FALSE);
1070 child_info->tag_err = claws_input_add(chld_err[0], GDK_INPUT_READ,
1071 catch_output, child_info, FALSE);
1073 if (!(children->action_type &
1074 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
1077 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
1079 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
1080 &by_read, &by_written, NULL);
1081 if (ret_str && by_written) {
1082 r = write(chld_in[1], ret_str, strlen(ret_str));
1085 r = write(chld_in[1], msg_str, strlen(msg_str));
1086 if (!(children->action_type &
1087 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
1088 r = close(chld_in[1]);
1089 child_info->chld_in = -1; /* No more input */
1091 debug_print("%s(%d)", strerror(errno), errno);
1097 #endif /* G_OS_UNIX */
1100 static void kill_children_cb(GtkWidget *widget, gpointer data)
1104 Children *children = (Children *) data;
1105 ChildInfo *child_info;
1107 for (cur = children->list; cur; cur = cur->next) {
1108 child_info = (ChildInfo *)(cur->data);
1109 debug_print("Killing child group id %d\n", child_info->pid);
1110 if (child_info->pid && kill(-child_info->pid, child_info->next_sig) < 0)
1112 child_info->next_sig = SIGKILL;
1114 #endif /* G_OS_UNIX */
1117 static gint wait_for_children(Children *children)
1119 gboolean new_output;
1120 ChildInfo *child_info;
1123 cur = children->list;
1126 child_info = (ChildInfo *)cur->data;
1127 new_output |= child_info->new_out;
1131 children->output |= new_output;
1133 if (new_output || (children->dialog && (children->initial_nb != children->nb)))
1134 update_io_dialog(children);
1139 if (!children->dialog) {
1140 free_children(children);
1141 } else if (!children->output) {
1142 gtk_widget_destroy(children->dialog);
1148 static void send_input(GtkWidget *w, gpointer data)
1150 Children *children = (Children *) data;
1151 ChildInfo *child_info = (ChildInfo *) children->list->data;
1153 child_info->tag_in = claws_input_add(child_info->chld_in,
1155 catch_input, children, FALSE);
1158 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1160 hide_io_dialog_cb(w, data);
1164 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1167 Children *children = (Children *)data;
1169 if (!children->nb) {
1170 g_signal_handlers_disconnect_matched
1171 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1172 0, 0, NULL, NULL, children);
1173 gtk_widget_destroy(children->dialog);
1174 free_children(children);
1178 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1181 if (event && (event->keyval == GDK_Escape ||
1182 event->keyval == GDK_Return ||
1183 event->keyval == GDK_KP_Enter))
1184 hide_io_dialog_cb(widget, data);
1188 static void childinfo_close_pipes(ChildInfo *child_info)
1190 /* stdout and stderr pipes are guaranteed to be removed by
1191 * their handler, but in case where we receive child exit notification
1192 * before grand-child's pipes closing signals, we check them and close
1195 if (child_info->tag_in > 0)
1196 g_source_remove(child_info->tag_in);
1197 if (child_info->tag_out > 0)
1198 g_source_remove(child_info->tag_out);
1199 if (child_info->tag_err > 0)
1200 g_source_remove(child_info->tag_err);
1202 if (child_info->chld_in >= 0)
1203 (void)close(child_info->chld_in);
1204 if (child_info->chld_out >= 0)
1205 (void)close(child_info->chld_out);
1206 if (child_info->chld_err >= 0)
1207 (void)close(child_info->chld_err);
1209 (void)close(child_info->chld_status);
1212 static void free_children(Children *children)
1214 ChildInfo *child_info;
1215 void (*callback)(void *data) = NULL;
1218 debug_print("Freeing children data %p\n", children);
1220 g_free(children->action);
1221 while (children->list != NULL) {
1222 child_info = (ChildInfo *)children->list->data;
1223 g_free(child_info->cmd);
1224 g_string_free(child_info->output, TRUE);
1225 children->list = g_slist_remove(children->list, child_info);
1226 callback = child_info->callback;
1227 data = child_info->data;
1237 static void update_io_dialog(Children *children)
1241 debug_print("Updating actions input/output dialog.\n");
1243 if (children->progress_bar) {
1246 /* use a more compact format */
1247 const gchar *format = "%s %d/%d";
1249 const gchar *format = "%s %d / %d";
1252 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1253 (children->initial_nb == 0) ? 0 :
1254 (gdouble) (children->initial_nb - children->nb) /
1255 (gdouble) children->initial_nb);
1256 text = g_strdup_printf(format, _("Completed"),
1257 children->initial_nb - children->nb,
1258 children->initial_nb);
1259 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1263 if (!children->nb) {
1264 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1265 gtk_widget_set_sensitive(children->close_btn, TRUE);
1266 if (children->input_hbox)
1267 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1268 gtk_widget_grab_focus(children->close_btn);
1269 g_signal_connect(G_OBJECT(children->dialog),
1271 G_CALLBACK(io_dialog_key_pressed_cb),
1275 if (children->output) {
1276 GtkWidget *text = children->text;
1277 GtkTextBuffer *textbuf;
1278 GtkTextIter iter, start_iter, end_iter;
1280 ChildInfo *child_info;
1282 gtk_widget_show(children->scrolledwin);
1283 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1284 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1285 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1286 gtk_text_buffer_get_start_iter(textbuf, &iter);
1288 for (cur = children->list; cur; cur = cur->next) {
1289 child_info = (ChildInfo *)cur->data;
1290 if (child_info->pid)
1291 caption = g_strdup_printf
1292 (_("--- Running: %s\n"),
1295 caption = g_strdup_printf
1296 (_("--- Ended: %s\n"),
1299 gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1300 gtk_text_buffer_insert(textbuf, &iter,
1301 child_info->output->str, -1);
1303 child_info->new_out = FALSE;
1308 static void create_io_dialog(Children *children)
1312 GtkWidget *entry = NULL;
1313 GtkWidget *input_hbox = NULL;
1314 GtkWidget *send_button;
1317 GtkWidget *scrolledwin;
1319 GtkWidget *progress_bar = NULL;
1320 GtkWidget *abort_button;
1321 GtkWidget *close_button;
1323 debug_print("Creating action IO dialog\n");
1325 dialog = gtk_dialog_new();
1326 gtk_container_set_border_width
1327 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1328 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1329 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1330 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1331 manage_window_set_transient(GTK_WINDOW(dialog));
1332 g_signal_connect(G_OBJECT(dialog), "delete_event",
1333 G_CALLBACK(delete_io_dialog_cb), children);
1334 g_signal_connect(G_OBJECT(dialog), "destroy",
1335 G_CALLBACK(hide_io_dialog_cb),
1338 vbox = gtk_vbox_new(FALSE, 8);
1339 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1340 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1341 gtk_widget_show(vbox);
1343 label = gtk_label_new(children->action);
1344 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1345 gtk_widget_show(label);
1347 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1348 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1349 GTK_POLICY_AUTOMATIC,
1350 GTK_POLICY_AUTOMATIC);
1351 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1353 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1354 gtk_widget_set_size_request(scrolledwin, 560, 200);
1355 gtk_widget_hide(scrolledwin);
1357 text = gtk_text_view_new();
1359 if (prefs_common.textfont) {
1360 PangoFontDescription *font_desc;
1361 font_desc = pango_font_description_from_string
1362 (prefs_common.textfont);
1364 gtk_widget_modify_font(text, font_desc);
1365 pango_font_description_free(font_desc);
1369 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1370 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1371 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1372 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1373 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1374 gtk_widget_show(text);
1376 if (children->open_in) {
1377 input_hbox = gtk_hbox_new(FALSE, 8);
1378 gtk_widget_show(input_hbox);
1380 entry = gtk_entry_new();
1381 gtk_widget_set_size_request(entry, 320, -1);
1382 g_signal_connect(G_OBJECT(entry), "activate",
1383 G_CALLBACK(send_input), children);
1384 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1385 if (children->action_type & ACTION_USER_HIDDEN_IN) {
1386 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1388 hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry),
1389 HILDON_GTK_INPUT_MODE_FULL |
1390 HILDON_GTK_INPUT_MODE_INVISIBLE);
1393 gtk_widget_show(entry);
1395 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1396 g_signal_connect(G_OBJECT(send_button), "clicked",
1397 G_CALLBACK(send_input), children);
1398 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1400 gtk_widget_show(send_button);
1402 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1403 gtk_widget_grab_focus(entry);
1406 if (children->initial_nb > 1) {
1409 /* use a more compact format */
1410 const gchar *format = "%s 0/%d\n";
1412 const gchar *format = "%s 0 / %d\n";
1415 progress_bar = gtk_progress_bar_new();
1416 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1417 GTK_PROGRESS_LEFT_TO_RIGHT);
1418 text = g_strdup_printf(format, _("Completed"),
1419 children->initial_nb);
1420 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1423 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1424 gtk_widget_show(progress_bar);
1427 gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1428 &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1429 g_signal_connect(G_OBJECT(abort_button), "clicked",
1430 G_CALLBACK(kill_children_cb), children);
1431 g_signal_connect(G_OBJECT(close_button), "clicked",
1432 G_CALLBACK(hide_io_dialog_cb), children);
1433 gtk_widget_show(hbox);
1436 gtk_widget_set_sensitive(close_button, FALSE);
1438 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1440 children->dialog = dialog;
1441 children->scrolledwin = scrolledwin;
1442 children->text = text;
1443 children->input_hbox = children->open_in ? input_hbox : NULL;
1444 children->input_entry = children->open_in ? entry : NULL;
1445 children->progress_bar = progress_bar;
1446 children->abort_btn = abort_button;
1447 children->close_btn = close_button;
1449 gtk_widget_show(dialog);
1452 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1454 ChildInfo *child_info = (ChildInfo *)data;
1458 g_source_remove(child_info->tag_status);
1460 c = read(source, &buf, 1);
1461 debug_print("Child returned %c\n", buf);
1464 waitpid(-child_info->pid, NULL, 0);
1466 childinfo_close_pipes(child_info);
1467 child_info->pid = 0;
1469 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE)
1470 && child_info->msginfo_list) {
1471 /* Actions on message *files* might change size and
1472 * time stamp, and thus invalidate the cache */
1473 SummaryView *summaryview = NULL;
1475 MsgInfo *msginfo, *nmi; /* newmsginfo */
1477 gboolean modified_something = FALSE;
1478 FolderItem *last_item = NULL;
1479 if (mainwindow_get_mainwindow ())
1480 summaryview = mainwindow_get_mainwindow ()->summaryview;
1481 for (cur = child_info->msginfo_list; cur; cur = cur->next) {
1482 msginfo = (MsgInfo *)cur->data;
1483 if (!(msginfo && /* Stuff used valid? */
1484 msginfo->folder && msginfo->folder->cache))
1486 file = procmsg_get_message_file_path (msginfo);
1489 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE);
1491 continue; /* Deleted? */
1492 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) {
1493 nmi->folder = msginfo->folder;
1494 nmi->msgnum = msginfo->msgnum;
1495 msgcache_update_msg (msginfo->folder->cache, nmi);
1496 modified_something = TRUE;
1497 last_item = nmi->folder;
1499 procmsg_msginfo_free (nmi);
1500 if (summaryview && summaryview->displayed &&
1501 summaryview->folder_item == msginfo->folder &&
1502 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum)
1503 summary_redisplay_msg(summaryview);
1506 if (modified_something && last_item &&
1507 summaryview && summaryview->folder_item == last_item) {
1508 summary_show (summaryview, summaryview->folder_item);
1510 g_slist_free (child_info->msginfo_list);
1511 child_info->msginfo_list = NULL;
1514 if (!child_info->pid)
1515 child_info->children->nb--;
1517 wait_for_children(child_info->children);
1520 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1522 Children *children = (Children *)data;
1523 ChildInfo *child_info = (ChildInfo *)children->list->data;
1524 gchar *input, *ret_str;
1525 gint c, count, len, r;
1526 gssize by_read = 0, by_written = 0;
1528 debug_print("Sending input to grand child.\n");
1529 if (!(cond & GDK_INPUT_WRITE))
1532 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1533 gtk_widget_grab_focus(children->abort_btn);
1535 g_source_remove(child_info->tag_in);
1536 child_info->tag_in = -1;
1538 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1540 ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1542 if (ret_str && by_written) {
1547 len = strlen(input);
1551 c = write(child_info->chld_in, input + count, len - count);
1554 } while (c >= 0 && count < len);
1557 r = write(child_info->chld_in, "\n", 2);
1561 r = close(child_info->chld_in);
1563 debug_print("%s(%d)", strerror(errno), errno);
1564 child_info->chld_in = -1;
1565 debug_print("Input to grand child sent.\n");
1568 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1570 ChildInfo *child_info = (ChildInfo *)data;
1572 gchar buf[BUFFSIZE];
1574 debug_print("Catching grand child's output.\n");
1575 if (child_info->children->action_type &
1576 (ACTION_PIPE_OUT | ACTION_INSERT)
1577 && source == child_info->chld_out) {
1579 GTK_TEXT_VIEW(child_info->children->msg_text);
1580 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1585 mark = gtk_text_buffer_get_insert(textbuf);
1586 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1587 ins_pos = gtk_text_iter_get_offset(&iter);
1590 gsize bytes_read = 0, bytes_written = 0;
1593 c = read(source, buf, sizeof(buf) - 1);
1597 ret_str = g_locale_to_utf8
1598 (buf, c, &bytes_read, &bytes_written, NULL);
1599 if (ret_str && bytes_written > 0) {
1600 gtk_text_buffer_insert
1601 (textbuf, &iter, ret_str,
1605 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1608 if (child_info->children->is_selection) {
1611 gtk_text_buffer_get_iter_at_offset
1612 (textbuf, &ins, ins_pos);
1613 gtk_text_buffer_select_range(textbuf, &ins, &iter);
1616 c = read(source, buf, sizeof(buf) - 1);
1618 gsize bytes_read = 0, bytes_written = 0;
1621 ret_str = g_locale_to_utf8
1622 (buf, c, &bytes_read, &bytes_written, NULL);
1623 if (ret_str && bytes_written > 0) {
1625 (child_info->output, ret_str,
1629 g_string_append_len(child_info->output, buf, c);
1631 child_info->new_out = TRUE;
1635 if (source == child_info->chld_out) {
1636 g_source_remove(child_info->tag_out);
1637 child_info->tag_out = -1;
1638 (void)close(child_info->chld_out);
1639 child_info->chld_out = -1;
1641 g_source_remove(child_info->tag_err);
1642 child_info->tag_err = -1;
1643 (void)close(child_info->chld_err);
1644 child_info->chld_err = -1;
1648 wait_for_children(child_info->children);
1651 static gchar *get_user_string(const gchar *action, ActionType type)
1654 gchar *user_str = NULL;
1657 case ACTION_USER_HIDDEN_STR:
1658 message = g_strdup_printf
1659 (_("Enter the argument for the following action:\n"
1660 "('%%h' will be replaced with the argument)\n"
1663 user_str = input_dialog_with_invisible
1664 (_("Action's hidden user argument"), message, NULL);
1666 case ACTION_USER_STR:
1667 message = g_strdup_printf
1668 (_("Enter the argument for the following action:\n"
1669 "('%%u' will be replaced with the argument)\n"
1672 user_str = input_dialog
1673 (_("Action's user argument"), message, NULL);
1676 g_warning("Unsupported action type %d", type);