action.c (42343B)
1 /* 2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client 3 * Copyright (C) 1999-2025 the Claws Mail Team and 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 3 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, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include "defs.h" 20 21 #include <glib.h> 22 #include <glib/gi18n.h> 23 #include <gtk/gtk.h> 24 #include <gdk/gdkkeysyms.h> 25 #ifdef GDK_WINDOWING_X11 26 # include <gdk/gdkx.h> 27 #endif /* GDK_WINDOWING_X11 */ 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <errno.h> 32 #include <sys/types.h> 33 #include <signal.h> 34 #include <unistd.h> 35 36 #include "utils.h" 37 #include "gtkutils.h" 38 #include "manage_window.h" 39 #include "mainwindow.h" 40 #include "prefs_common.h" 41 #include "alertpanel.h" 42 #include "inputdialog.h" 43 #include "action.h" 44 #include "compose.h" 45 #include "procmsg.h" 46 #include "msgcache.h" 47 #include "textview.h" 48 #include "procheader.h" 49 50 typedef struct _Children Children; 51 typedef struct _ChildInfo ChildInfo; 52 typedef struct _UserStringDialog UserStringDialog; 53 54 struct _Children 55 { 56 GtkWidget *dialog; 57 GtkWidget *text; 58 GtkWidget *input_entry; 59 GtkWidget *input_hbox; 60 GtkWidget *progress_bar; 61 GtkWidget *abort_btn; 62 GtkWidget *close_btn; 63 GtkWidget *scrolledwin; 64 65 gchar *action; 66 ActionType action_type; 67 GSList *list; 68 gint nb; 69 gint initial_nb; 70 gint open_in; 71 gboolean output; 72 73 GtkWidget *msg_text; 74 75 gboolean is_selection; 76 }; 77 78 struct _ChildInfo 79 { 80 Children *children; 81 gchar *cmd; 82 GPid pid; 83 gint next_sig; 84 gint chld_in; 85 gint chld_out; 86 gint chld_err; 87 gint tag_in; 88 gint tag_out; 89 gint tag_err; 90 gint tag_status; 91 gint new_out; 92 93 GString *output; 94 void (*callback)(void *data); 95 void *data; 96 97 GSList *msginfo_list; 98 }; 99 100 static void action_update_menu (GtkUIManager *ui_manager, 101 const gchar *accel_group, 102 gchar *branch_path, 103 gpointer callback, 104 gpointer data); 105 static void compose_actions_execute_cb (GtkWidget *widget, 106 gpointer data); 107 static void compose_actions_execute (Compose *compose, 108 guint action_nb, 109 GtkWidget *widget); 110 111 static void mainwin_actions_execute_cb (GtkWidget *widget, 112 gpointer data); 113 static void mainwin_actions_execute (MainWindow *mainwin, 114 guint action_nb, 115 GtkWidget *widget); 116 117 static void msgview_actions_execute_cb (GtkWidget *widget, 118 gpointer data); 119 static void msgview_actions_execute (MessageView *msgview, 120 guint action_nb, 121 GtkWidget *widget); 122 123 static void message_actions_execute (MessageView *msgview, 124 guint action_nb, 125 GSList *msg_list); 126 127 static gboolean execute_filtering_actions(gchar *action, 128 GSList *msglist); 129 130 static gboolean execute_actions (gchar *action, 131 GSList *msg_list, 132 GtkWidget *text, 133 gint body_pos, 134 MimeInfo *partinfo, 135 void (*callback)(void *data), 136 void *data); 137 138 static gchar *parse_action_cmd (gchar *action, 139 MsgInfo *msginfo, 140 GSList *msg_list, 141 MimeInfo *partinfo, 142 const gchar *user_str, 143 const gchar *user_hidden_str, 144 const gchar *sel_str); 145 static gboolean parse_append_filename (GString *cmd, 146 MsgInfo *msginfo); 147 148 static gboolean parse_append_msgpart (GString *cmd, 149 MsgInfo *msginfo, 150 MimeInfo *partinfo); 151 152 static ChildInfo *fork_child (gchar *cmd, 153 const gchar *msg_str, 154 Children *children); 155 156 static gint wait_for_children (Children *children); 157 158 static void free_children (Children *children); 159 160 static void childinfo_close_pipes (ChildInfo *child_info); 161 162 static void create_io_dialog (Children *children); 163 static void update_io_dialog (Children *children); 164 165 static void hide_io_dialog_cb (GtkWidget *widget, 166 gpointer data); 167 168 static void catch_output (gpointer data, 169 gint source, 170 GIOCondition cond); 171 static void catch_input (gpointer data, 172 gint source, 173 GIOCondition cond); 174 static void catch_status (GPid pid, gint status, gpointer data); 175 176 static gchar *get_user_string (const gchar *action, 177 ActionType type); 178 179 180 ActionType action_get_type(const gchar *action_str) 181 { 182 const gchar *p; 183 gboolean in_filtering_action = FALSE; 184 ActionType action_type = ACTION_NONE; 185 186 p = action_str; 187 188 if (p[0] == '|') { 189 action_type |= ACTION_PIPE_IN; 190 p++; 191 } else if (p[0] == '>') { 192 action_type |= ACTION_USER_IN; 193 p++; 194 } else if (p[0] == '*') { 195 action_type |= ACTION_USER_HIDDEN_IN; 196 p++; 197 } 198 199 if (p[0] == '\0') 200 return ACTION_ERROR; 201 202 while (*p && action_type != ACTION_ERROR) { 203 if (!in_filtering_action) { 204 if (p[0] == '%' && p[1]) { 205 switch (p[1]) { 206 case 'a': 207 /* CLAWS: filtering action is a mutually exclusive 208 * action. we can enable others if needed later. we 209 * add ACTION_SINGLE | ACTION_MULTIPLE so it will 210 * only be executed from the main window toolbar */ 211 if (p[2] == 's') /* source messages */ 212 action_type = ACTION_FILTERING_ACTION 213 | ACTION_SINGLE 214 | ACTION_MULTIPLE; 215 in_filtering_action = TRUE; 216 break; 217 case 'f': 218 action_type |= ACTION_SINGLE; 219 break; 220 case 'F': 221 action_type |= ACTION_MULTIPLE; 222 break; 223 case 'p': 224 action_type |= ACTION_SINGLE; 225 break; 226 case 's': 227 action_type |= ACTION_SELECTION_STR; 228 break; 229 case 'u': 230 action_type |= ACTION_USER_STR; 231 break; 232 case 'h': 233 action_type |= ACTION_USER_HIDDEN_STR; 234 break; 235 case '%': 236 /* literal '%' */ 237 break; 238 default: 239 action_type = ACTION_ERROR; 240 break; 241 } 242 p++; 243 } else if (p[0] == '|') { 244 if (p[1] == '\0') 245 action_type |= ACTION_PIPE_OUT; 246 } else if (p[0] == '>') { 247 if (p[1] == '\0') 248 action_type |= ACTION_INSERT; 249 } else if (p[0] == '&') { 250 if (p[1] == '\0') 251 action_type |= ACTION_ASYNC; 252 } else if (p[0] == '}') { 253 in_filtering_action = FALSE; 254 } 255 } 256 p++; 257 } 258 259 return action_type; 260 } 261 262 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo, 263 GSList *msg_list, MimeInfo *partinfo, 264 const gchar *user_str, 265 const gchar *user_hidden_str, 266 const gchar *sel_str) 267 { 268 GString *cmd; 269 gchar *p; 270 GSList *cur; 271 272 p = action; 273 274 if (p[0] == '|' || p[0] == '>' || p[0] == '*') 275 p++; 276 277 cmd = g_string_sized_new(strlen(action)); 278 279 while (p[0] && 280 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) { 281 if (p[0] == '%' && p[1]) { 282 switch (p[1]) { 283 case 'f': 284 if (!parse_append_filename(cmd, msginfo)) { 285 g_string_free(cmd, TRUE); 286 return NULL; 287 } 288 p++; 289 break; 290 case 'F': 291 for (cur = msg_list; cur != NULL; 292 cur = cur->next) { 293 MsgInfo *msg = (MsgInfo *)cur->data; 294 295 if (!parse_append_filename(cmd, msg)) { 296 g_string_free(cmd, TRUE); 297 return NULL; 298 } 299 if (cur->next) 300 g_string_append_c(cmd, ' '); 301 } 302 p++; 303 break; 304 case 'p': 305 if (!parse_append_msgpart(cmd, msginfo, 306 partinfo)) { 307 g_string_free(cmd, TRUE); 308 return NULL; 309 } 310 p++; 311 break; 312 case 's': 313 if (sel_str) 314 g_string_append(cmd, sel_str); 315 p++; 316 break; 317 case 'u': 318 if (user_str) 319 g_string_append(cmd, user_str); 320 p++; 321 break; 322 case 'h': 323 if (user_hidden_str) 324 g_string_append(cmd, user_hidden_str); 325 p++; 326 break; 327 case '%': 328 g_string_append_c(cmd, p[1]); 329 p++; 330 break; 331 default: 332 g_string_append_c(cmd, p[0]); 333 g_string_append_c(cmd, p[1]); 334 p++; 335 } 336 } else { 337 g_string_append_c(cmd, p[0]); 338 } 339 p++; 340 } 341 if (cmd->len == 0) { 342 g_string_free(cmd, TRUE); 343 return NULL; 344 } 345 346 return g_string_free(cmd, FALSE); 347 } 348 349 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo) 350 { 351 gchar *filename; 352 353 filename = procmsg_get_message_file(msginfo); 354 355 if (!filename) { 356 alertpanel_error(_("Could not get message file %d"), 357 msginfo->msgnum); 358 return FALSE; 359 } 360 361 g_string_append(cmd, "\""); 362 #ifdef G_OS_UNIX 363 gchar *p = filename, *q; 364 gchar escape_ch[] = "\\ "; 365 while ((q = strpbrk(p, "$\"`\\~")) != NULL) { 366 escape_ch[1] = *q; 367 *q = '\0'; 368 g_string_append(cmd, p); 369 g_string_append(cmd, escape_ch); 370 p = q + 1; 371 } 372 373 g_string_append(cmd, p); 374 #else 375 g_string_append(cmd, filename); 376 #endif 377 g_string_append(cmd, "\""); 378 g_free(filename); 379 380 return TRUE; 381 } 382 383 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo, 384 MimeInfo *partinfo) 385 { 386 gboolean single_part = FALSE; 387 gchar *filename; 388 gchar *part_filename; 389 gint ret; 390 391 if (!partinfo) { 392 partinfo = procmime_scan_message(msginfo); 393 if (!partinfo) { 394 alertpanel_error(_("Could not get message part.")); 395 return FALSE; 396 } 397 398 single_part = TRUE; 399 } 400 401 filename = procmsg_get_message_file_path(msginfo); 402 part_filename = procmime_get_tmp_file_name(partinfo); 403 404 ret = procmime_get_part(part_filename, partinfo); 405 406 if (single_part) 407 procmime_mimeinfo_free_all(&partinfo); 408 g_free(filename); 409 410 if (ret < 0) { 411 alertpanel_error(_("Can't get part of multipart message: %s"), g_strerror(-ret)); 412 g_free(part_filename); 413 return FALSE; 414 } 415 416 g_string_append(cmd, part_filename); 417 418 g_free(part_filename); 419 420 return TRUE; 421 } 422 423 void actions_execute(gpointer data, 424 guint action_nb, 425 GtkWidget *widget, 426 gint source) 427 { 428 if (source == TOOLBAR_MAIN) 429 mainwin_actions_execute((MainWindow*)data, action_nb, widget); 430 else if (source == TOOLBAR_COMPOSE) 431 compose_actions_execute((Compose*)data, action_nb, widget); 432 else if (source == TOOLBAR_MSGVIEW) 433 msgview_actions_execute((MessageView*)data, action_nb, widget); 434 } 435 436 void action_update_mainwin_menu(GtkUIManager *ui_manager, 437 gchar *branch_path, 438 MainWindow *mainwin) 439 { 440 action_update_menu(ui_manager, "<MainwinActions>", branch_path, 441 mainwin_actions_execute_cb, mainwin); 442 } 443 444 void action_update_msgview_menu(GtkUIManager *ui_manager, 445 gchar *branch_path, 446 MessageView *msgview) 447 { 448 action_update_menu(ui_manager, "<MsgviewActions>", branch_path, 449 msgview_actions_execute_cb, msgview); 450 } 451 452 void action_update_compose_menu(GtkUIManager *ui_manager, 453 gchar *branch_path, 454 Compose *compose) 455 { 456 action_update_menu(ui_manager, "<ComposeActions>", branch_path, 457 compose_actions_execute_cb, compose); 458 } 459 460 static GtkWidget *find_item_in_menu(GtkWidget *menu, gchar *name) 461 { 462 GList *children = gtk_container_get_children(GTK_CONTAINER(GTK_MENU_SHELL(menu))); 463 GList *amenu = children; 464 const gchar *existing_name; 465 while (amenu) { 466 GtkWidget *item = GTK_WIDGET(amenu->data); 467 if ((existing_name = g_object_get_data(G_OBJECT(item), "s_name")) != NULL && 468 !g_strcmp0(name, existing_name)) 469 { 470 g_list_free(children); 471 return item; 472 } 473 amenu = amenu->next; 474 } 475 476 g_list_free(children); 477 478 return NULL; 479 } 480 481 static GtkWidget *create_submenus(GtkWidget *menu, const gchar *action) 482 { 483 gchar *submenu = g_strdup(action); 484 GtkWidget *new_menu = NULL; 485 486 if (strchr(submenu, '/')) { 487 const gchar *end = (strchr(submenu, '/')+1); 488 GtkWidget *menu_item = NULL; 489 if (end && *end) { 490 *strchr(submenu, '/') = '\0'; 491 if ((menu_item = find_item_in_menu(menu, submenu)) == NULL) { 492 menu_item = gtk_menu_item_new_with_mnemonic(submenu); 493 g_object_set_data_full(G_OBJECT(menu_item), "s_name", g_strdup(submenu), g_free); 494 gtk_widget_show(menu_item); 495 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); 496 new_menu = gtk_menu_new(); 497 gtk_widget_show(new_menu); 498 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), new_menu); 499 } else { 500 new_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item)); 501 } 502 new_menu = create_submenus(new_menu, end); 503 } 504 } 505 g_free(submenu); 506 return new_menu ? new_menu : menu; 507 } 508 509 static void action_update_menu(GtkUIManager *ui_manager, 510 const gchar *accel_group, 511 gchar *branch_path, 512 gpointer callback, gpointer data) 513 { 514 GSList *cur; 515 gchar *action, *action_p; 516 int callback_action = 0; 517 GtkWidget *menu = gtk_menu_new(); 518 GtkWidget *item; 519 520 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) { 521 GtkWidget *cur_menu = menu; 522 const gchar *action_name = NULL; 523 action = g_strdup((gchar *)cur->data); 524 action_p = strstr(action, ": "); 525 if (action_p && action_p[2] && 526 (action_get_type(&action_p[2]) != ACTION_ERROR) && 527 (action[0] != '/')) { 528 gchar *accel_path = NULL; 529 530 action_p[0] = '\0'; 531 if (strchr(action, '/')) { 532 cur_menu = create_submenus(cur_menu, action); 533 action_name = strrchr(action, '/')+1; 534 } else { 535 action_name = action; 536 } 537 gtk_menu_set_accel_group (GTK_MENU (cur_menu), 538 gtk_ui_manager_get_accel_group(ui_manager)); 539 item = gtk_menu_item_new_with_label(action_name); 540 gtk_menu_shell_append(GTK_MENU_SHELL(cur_menu), item); 541 g_signal_connect(G_OBJECT(item), "activate", 542 G_CALLBACK(callback), data); 543 g_object_set_data(G_OBJECT(item), "action_num", GINT_TO_POINTER(callback_action)); 544 gtk_widget_show(item); 545 accel_path = g_strconcat(accel_group,branch_path, "/", action, NULL); 546 gtk_menu_item_set_accel_path(GTK_MENU_ITEM(item), accel_path); 547 g_free(accel_path); 548 549 } 550 g_free(action); 551 callback_action++; 552 } 553 554 gtk_widget_show(menu); 555 gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtk_ui_manager_get_widget(ui_manager, branch_path)), menu); 556 } 557 558 static void compose_actions_execute_cb(GtkWidget *widget, gpointer data) 559 { 560 Compose *compose = (Compose *)data; 561 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num")); 562 compose_actions_execute(compose, action_nb, NULL); 563 } 564 565 static void compose_actions_execute(Compose *compose, guint action_nb, GtkWidget *widget) 566 { 567 gchar *buf, *action; 568 ActionType action_type; 569 570 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb); 571 action = strstr(buf, ": "); 572 /* Point to the beginning of the command-line */ 573 action += 2; 574 575 action_type = action_get_type(action); 576 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) { 577 alertpanel_warning 578 (_("The selected action cannot be used in the write window\n" 579 "because it contains %%f, %%F, %%as or %%p.")); 580 return; 581 } 582 583 execute_actions(action, NULL, compose->text, 0, NULL, 584 compose_action_cb, compose); 585 } 586 587 static void mainwin_actions_execute_cb(GtkWidget *widget, gpointer data) 588 { 589 MainWindow *mainwin = (MainWindow *)data; 590 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num")); 591 mainwin_actions_execute(mainwin, action_nb, NULL); 592 } 593 594 static void _free_msginfos(gpointer data, gpointer user_data) 595 { 596 MsgInfo *msginfo = (MsgInfo *)data; 597 598 procmsg_msginfo_free(&msginfo); 599 } 600 601 static void mainwin_actions_execute(MainWindow *mainwin, guint action_nb, 602 GtkWidget *widget) 603 { 604 GSList *msg_list; 605 606 msg_list = summary_get_selected_msg_list(mainwin->summaryview); 607 message_actions_execute(mainwin->messageview, action_nb, msg_list); 608 summary_select_by_msg_list(mainwin->summaryview, msg_list); 609 g_slist_foreach(msg_list, _free_msginfos, NULL); 610 g_slist_free(msg_list); 611 } 612 613 static void msgview_actions_execute_cb(GtkWidget *widget, gpointer data) 614 { 615 MessageView *msgview = (MessageView *)data; 616 gint action_nb = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "action_num")); 617 msgview_actions_execute(msgview, action_nb, NULL); 618 } 619 620 static void msgview_actions_execute(MessageView *msgview, guint action_nb, 621 GtkWidget *widget) 622 { 623 GSList *msg_list = NULL; 624 625 if (msgview->msginfo) 626 msg_list = g_slist_append(msg_list, msgview->msginfo); 627 message_actions_execute(msgview, action_nb, msg_list); 628 g_slist_free(msg_list); 629 } 630 631 static void message_actions_execute(MessageView *msgview, guint action_nb, 632 GSList *msg_list) 633 { 634 TextView *textview; 635 MimeInfo *partinfo; 636 gchar *buf; 637 gchar *action; 638 GtkWidget *text = NULL; 639 guint body_pos = 0; 640 ActionType action_type; 641 642 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb); 643 644 /* Point to the beginning of the command-line */ 645 action += 2; 646 647 textview = messageview_get_current_textview(msgview); 648 if (textview) { 649 text = textview->text; 650 body_pos = textview->body_pos; 651 } 652 partinfo = messageview_get_selected_mime_part(msgview); 653 654 /* this command will alter the message text */ 655 action_type = action_get_type(action); 656 if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT)) 657 msgview->filtered = TRUE; 658 659 if (action_type & ACTION_FILTERING_ACTION) 660 /* CLAWS: most of the above code is not necessary for applying 661 * filtering */ 662 execute_filtering_actions(action, msg_list); 663 else 664 execute_actions(action, msg_list, text, body_pos, partinfo, 665 NULL, NULL); 666 } 667 668 static gboolean execute_filtering_actions(gchar *action, GSList *msglist) 669 { 670 GSList *action_list; 671 const gchar *sbegin, *send; 672 gchar *action_string; 673 SummaryView *summaryview = NULL; 674 MainWindow *mainwin = NULL; 675 676 if (mainwindow_get_mainwindow()) { 677 summaryview = mainwindow_get_mainwindow()->summaryview; 678 mainwin = mainwindow_get_mainwindow(); 679 } 680 681 if (NULL == (sbegin = g_strstr_len(action, -1, "%as{"))) 682 return FALSE; 683 sbegin += sizeof "%as{" - 1; 684 if (NULL == (send = strrchr(sbegin, '}'))) 685 return FALSE; 686 action_string = g_strndup(sbegin, send - sbegin); 687 g_free(action_string); 688 689 if (summaryview) { 690 summary_lock(summaryview); 691 main_window_cursor_wait(mainwin); 692 summary_freeze(summaryview); 693 folder_item_update_freeze(); 694 } 695 696 if (summaryview) { 697 folder_item_update_thaw(); 698 summary_thaw(summaryview); 699 main_window_cursor_normal(mainwin); 700 summary_unlock(summaryview); 701 summary_show(summaryview, summaryview->folder_item, FALSE); 702 } 703 g_slist_free(action_list); 704 return TRUE; 705 } 706 707 static gboolean execute_actions(gchar *action, GSList *msg_list, 708 GtkWidget *text, 709 gint body_pos, MimeInfo *partinfo, 710 void (*callback)(void *data), void *data) 711 { 712 GSList *children_list = NULL, *cur = NULL; 713 gint is_ok = TRUE; 714 gint msg_list_len; 715 Children *children; 716 ChildInfo *child_info; 717 ActionType action_type; 718 MsgInfo *msginfo; 719 gchar *cmd; 720 gchar *sel_str = NULL; 721 gchar *msg_str = NULL; 722 gchar *user_str = NULL; 723 gchar *user_hidden_str = NULL; 724 GtkTextIter start_iter, end_iter; 725 gboolean is_selection = FALSE; 726 727 action_type = action_get_type(action); 728 729 if (action_type == ACTION_ERROR) 730 return FALSE; /* ERR: syntax error */ 731 732 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list) 733 return FALSE; /* ERR: file command without selection */ 734 735 msg_list_len = g_slist_length(msg_list); 736 737 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) { 738 if (msg_list_len > 1) 739 return FALSE; /* ERR: pipe + multiple selection */ 740 if (!text) 741 return FALSE; /* ERR: pipe and no displayed text */ 742 } 743 744 if (action_type & ACTION_SELECTION_STR) { 745 if (!text) 746 return FALSE; /* ERR: selection string but no text */ 747 } 748 749 if (text) { 750 GtkTextBuffer *textbuf; 751 752 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); 753 is_selection = gtk_text_buffer_get_selection_bounds 754 (textbuf, &start_iter, &end_iter); 755 if (!is_selection) { 756 gtk_text_buffer_get_iter_at_offset 757 (textbuf, &start_iter, body_pos); 758 gtk_text_buffer_get_end_iter(textbuf, &end_iter); 759 } 760 msg_str = gtk_text_buffer_get_text 761 (textbuf, &start_iter, &end_iter, FALSE); 762 if (is_selection) 763 sel_str = g_strdup(msg_str); 764 } 765 766 if (action_type & ACTION_USER_STR) { 767 if (!(user_str = get_user_string(action, ACTION_USER_STR))) { 768 g_free(msg_str); 769 g_free(sel_str); 770 return FALSE; 771 } 772 } 773 774 if (action_type & ACTION_USER_HIDDEN_STR) { 775 if (!(user_hidden_str = 776 get_user_string(action, ACTION_USER_HIDDEN_STR))) { 777 g_free(msg_str); 778 g_free(sel_str); 779 g_free(user_str); 780 return FALSE; 781 } 782 } 783 784 if (text && (action_type & ACTION_PIPE_OUT)) { 785 GtkTextBuffer *textbuf; 786 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); 787 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter); 788 } 789 790 children = g_new0(Children, 1); 791 792 children->nb = 0; 793 children->action = g_strdup(action); 794 children->action_type = action_type; 795 children->msg_text = text; 796 children->is_selection = is_selection; 797 798 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) && 799 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1)) 800 children->open_in = 1; 801 802 /* Pre-fetch bodies, makes it easier on IMAP (see bug #3011) */ 803 for (cur = msg_list; cur; cur = cur->next) { 804 gchar *dummy; 805 msginfo = (MsgInfo *)cur->data; 806 807 dummy = procmsg_get_message_file((MsgInfo *)cur->data); 808 if (dummy) 809 g_free(dummy); 810 else 811 is_ok = FALSE; 812 } 813 814 if (is_ok && (action_type & ACTION_SINGLE)) { 815 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) { 816 msginfo = (MsgInfo *)cur->data; 817 if (!msginfo) { 818 is_ok = FALSE; /* ERR: msginfo missing */ 819 break; 820 } 821 cmd = parse_action_cmd(action, msginfo, msg_list, 822 partinfo, user_str, 823 user_hidden_str, sel_str); 824 if (!cmd) { 825 debug_print("Action command error\n"); 826 is_ok = FALSE; /* ERR: incorrect command */ 827 break; 828 } 829 if ((child_info = fork_child(cmd, msg_str, children))) { 830 /* Pass msginfo to catch_status () */ 831 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT))) 832 child_info->msginfo_list = 833 g_slist_append (NULL, msginfo); 834 children_list = g_slist_append(children_list, 835 child_info); 836 children->nb++; 837 } 838 g_free(cmd); 839 } 840 } else if (is_ok) { 841 cmd = parse_action_cmd(action, NULL, msg_list, partinfo, 842 user_str, user_hidden_str, sel_str); 843 if (cmd) { 844 if ((child_info = fork_child(cmd, msg_str, children))) { 845 if (!(action_type & (ACTION_PIPE_OUT | ACTION_INSERT))) 846 child_info->msginfo_list = 847 g_slist_copy (msg_list); 848 children_list = g_slist_append(children_list, 849 child_info); 850 children->nb++; 851 } 852 g_free(cmd); 853 } else 854 is_ok = FALSE; /* ERR: incorrect command */ 855 } 856 857 g_free(msg_str); 858 g_free(sel_str); 859 g_free(user_str); 860 g_free(user_hidden_str); 861 862 if (!children_list) { 863 /* If not waiting for children, return */ 864 free_children(children); 865 } else { 866 GSList *cur; 867 868 children->list = children_list; 869 children->initial_nb = children->nb; 870 871 for (cur = children_list; cur; cur = cur->next) { 872 child_info = (ChildInfo *) cur->data; 873 child_info->callback = callback; 874 child_info->data = data; 875 child_info->tag_status = 876 g_child_watch_add(child_info->pid, catch_status, child_info); 877 } 878 879 create_io_dialog(children); 880 } 881 return is_ok; 882 } 883 884 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str, 885 Children *children) 886 { 887 gint chld_in, chld_out, chld_err; 888 gchar **argv, *ret_str, *trim_cmd; 889 GPid pid; 890 ChildInfo *child_info; 891 gint follow_child; 892 gssize by_written = 0, by_read = 0; 893 gboolean result = FALSE; 894 GError *error = NULL; 895 896 follow_child = !(children->action_type & ACTION_ASYNC); 897 898 chld_in = chld_out = chld_err = -1; 899 900 ret_str = g_locale_from_utf8(cmd, strlen(cmd), 901 &by_read, &by_written, 902 NULL); 903 if (!ret_str || !by_written) { 904 if (ret_str) 905 g_free(ret_str); 906 ret_str = g_strdup(cmd); 907 } 908 909 trim_cmd = ret_str; 910 911 while (g_ascii_isspace(trim_cmd[0])) 912 trim_cmd++; 913 914 #ifdef G_OS_UNIX 915 argv = g_new0(gchar *, 4); 916 argv[0] = g_strdup("/bin/sh"); 917 argv[1] = g_strdup("-c"); 918 argv[2] = g_strdup(trim_cmd); 919 argv[3] = 0; 920 #else 921 argv = strsplit_with_quote(trim_cmd, " ", 0); 922 #endif 923 g_free(ret_str); 924 925 if (follow_child) { 926 result = g_spawn_async_with_pipes(NULL, argv, NULL, 927 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, 928 NULL, NULL, &pid, &chld_in, &chld_out, 929 &chld_err, &error); 930 } else { 931 result = g_spawn_async(NULL, argv, NULL, 932 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, 933 &pid, &error); 934 } 935 936 debug_print("spawning %s: %d\n", cmd, result); 937 938 g_strfreev(argv); 939 940 if (!result) { 941 alertpanel_error(_("Could not fork to execute the following " 942 "command:\n%s\n%s"), 943 cmd, error ? error->message : _("Unknown error")); 944 if (error) 945 g_error_free(error); 946 return NULL; 947 } 948 949 if (!(children->action_type & 950 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))) 951 (void)close(chld_in); 952 953 if (!follow_child) { 954 g_spawn_close_pid(pid); 955 return NULL; 956 } 957 child_info = g_new0(ChildInfo, 1); 958 959 child_info->children = children; 960 961 child_info->pid = pid; 962 #ifdef G_OS_UNIX 963 child_info->next_sig = SIGTERM; 964 #endif 965 child_info->cmd = g_strdup(cmd); 966 child_info->new_out = FALSE; 967 child_info->output = g_string_new(NULL); 968 child_info->chld_in = 969 (children->action_type & 970 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) 971 ? chld_in : -1; 972 child_info->chld_out = chld_out; 973 child_info->chld_err = chld_err; 974 child_info->tag_status = -1; 975 child_info->tag_in = -1; 976 child_info->tag_out = claws_input_add(chld_out, G_IO_IN | G_IO_HUP | G_IO_ERR, 977 catch_output, child_info, FALSE); 978 child_info->tag_err = claws_input_add(chld_err, G_IO_IN | G_IO_HUP | G_IO_ERR, 979 catch_output, child_info, FALSE); 980 981 if (!(children->action_type & 982 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT))) 983 return child_info; 984 985 if ((children->action_type & ACTION_PIPE_IN) && msg_str) { 986 int r; 987 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str), 988 &by_read, &by_written, NULL); 989 if (ret_str && by_written) 990 r = write(chld_in, ret_str, strlen(ret_str)); 991 else 992 r = write(chld_in, msg_str, strlen(msg_str)); 993 if (ret_str) 994 g_free(ret_str); 995 996 if (!(children->action_type & 997 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN))) 998 r = close(chld_in); 999 child_info->chld_in = -1; /* No more input */ 1000 if (r != 0) 1001 debug_print("piping to child process: %s (%d)\n", g_strerror(errno), errno); 1002 } 1003 1004 return child_info; 1005 } 1006 1007 static void kill_children_cb(GtkWidget *widget, gpointer data) 1008 { 1009 GSList *cur; 1010 Children *children = (Children *) data; 1011 ChildInfo *child_info; 1012 1013 for (cur = children->list; cur; cur = cur->next) { 1014 child_info = (ChildInfo *)(cur->data); 1015 debug_print("Killing child group id %d\n", child_info->pid); 1016 if (child_info->pid && kill(child_info->pid, child_info->next_sig) < 0) 1017 perror("kill"); 1018 child_info->next_sig = SIGKILL; 1019 } 1020 } 1021 1022 static gint wait_for_children(Children *children) 1023 { 1024 gboolean new_output; 1025 ChildInfo *child_info; 1026 GSList *cur; 1027 1028 cur = children->list; 1029 new_output = FALSE; 1030 while (cur) { 1031 child_info = (ChildInfo *)cur->data; 1032 new_output |= child_info->new_out; 1033 cur = cur->next; 1034 } 1035 1036 children->output |= new_output; 1037 1038 if (new_output || (children->dialog && (children->initial_nb != children->nb))) 1039 update_io_dialog(children); 1040 1041 if (children->nb) 1042 return FALSE; 1043 1044 if (!children->dialog) { 1045 free_children(children); 1046 } else if (!children->output) { 1047 gtk_widget_destroy(children->dialog); 1048 } 1049 1050 return FALSE; 1051 } 1052 1053 static void send_input(GtkWidget *w, gpointer data) 1054 { 1055 Children *children = (Children *) data; 1056 ChildInfo *child_info = (ChildInfo *) children->list->data; 1057 1058 child_info->tag_in = claws_input_add(child_info->chld_in, 1059 G_IO_OUT | G_IO_ERR, 1060 catch_input, children, FALSE); 1061 } 1062 1063 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data) 1064 { 1065 hide_io_dialog_cb(w, data); 1066 return TRUE; 1067 } 1068 1069 static void hide_io_dialog_cb(GtkWidget *w, gpointer data) 1070 { 1071 1072 Children *children = (Children *)data; 1073 1074 if (!children->nb) { 1075 g_signal_handlers_disconnect_matched 1076 (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA, 1077 0, 0, NULL, NULL, children); 1078 gtk_widget_destroy(children->dialog); 1079 free_children(children); 1080 } 1081 } 1082 1083 static void childinfo_close_pipes(ChildInfo *child_info) 1084 { 1085 /* stdout and stderr pipes are guaranteed to be removed by 1086 * their handler, but in case where we receive child exit notification 1087 * before grand-child's pipes closing signals, we check them and close 1088 * them if necessary 1089 */ 1090 if (child_info->tag_in > 0) 1091 g_source_remove(child_info->tag_in); 1092 if (child_info->tag_out > 0) 1093 g_source_remove(child_info->tag_out); 1094 if (child_info->tag_err > 0) 1095 g_source_remove(child_info->tag_err); 1096 1097 if (child_info->chld_in >= 0) 1098 (void)close(child_info->chld_in); 1099 if (child_info->chld_out >= 0) 1100 (void)close(child_info->chld_out); 1101 if (child_info->chld_err >= 0) 1102 (void)close(child_info->chld_err); 1103 } 1104 1105 static void free_children(Children *children) 1106 { 1107 ChildInfo *child_info; 1108 void (*callback)(void *data) = NULL; 1109 void *data = NULL; 1110 1111 debug_print("Freeing children data %p\n", children); 1112 1113 g_free(children->action); 1114 while (children->list != NULL) { 1115 child_info = (ChildInfo *)children->list->data; 1116 g_free(child_info->cmd); 1117 g_string_free(child_info->output, TRUE); 1118 children->list = g_slist_remove(children->list, child_info); 1119 callback = child_info->callback; 1120 data = child_info->data; 1121 g_free(child_info); 1122 } 1123 1124 if (callback) 1125 callback(data); 1126 1127 g_free(children); 1128 } 1129 1130 static void update_io_dialog(Children *children) 1131 { 1132 GSList *cur; 1133 1134 debug_print("Updating actions input/output dialog.\n"); 1135 1136 if (children->progress_bar) { 1137 gchar *text; 1138 const gchar *format = "%s %d / %d"; 1139 1140 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar), 1141 (children->initial_nb == 0) ? 0 : 1142 (gdouble) (children->initial_nb - children->nb) / 1143 (gdouble) children->initial_nb); 1144 text = g_strdup_printf(format, _("Completed"), 1145 children->initial_nb - children->nb, 1146 children->initial_nb); 1147 gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(children->progress_bar), TRUE); 1148 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text); 1149 g_free(text); 1150 } 1151 1152 if (!children->nb) { 1153 gtk_widget_set_sensitive(children->abort_btn, FALSE); 1154 gtk_widget_set_sensitive(children->close_btn, TRUE); 1155 if (children->input_hbox) 1156 gtk_widget_set_sensitive(children->input_hbox, FALSE); 1157 gtk_widget_grab_focus(children->close_btn); 1158 } 1159 1160 if (children->output) { 1161 GtkWidget *text = children->text; 1162 GtkTextBuffer *textbuf; 1163 GtkTextIter iter, start_iter, end_iter; 1164 gchar *caption; 1165 ChildInfo *child_info; 1166 1167 gtk_widget_show(children->scrolledwin); 1168 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); 1169 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter); 1170 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter); 1171 gtk_text_buffer_get_start_iter(textbuf, &iter); 1172 1173 for (cur = children->list; cur; cur = cur->next) { 1174 child_info = (ChildInfo *)cur->data; 1175 if (child_info->pid) 1176 caption = g_strdup_printf 1177 (_("--- Running: %s\n"), 1178 child_info->cmd); 1179 else 1180 caption = g_strdup_printf 1181 (_("--- Ended: %s\n"), 1182 child_info->cmd); 1183 1184 gtk_text_buffer_insert(textbuf, &iter, caption, -1); 1185 gtk_text_buffer_insert(textbuf, &iter, 1186 child_info->output->str, -1); 1187 g_free(caption); 1188 child_info->new_out = FALSE; 1189 } 1190 } 1191 } 1192 1193 /*! 1194 *\brief Save Gtk object size to prefs dataset 1195 */ 1196 static void actions_io_size_allocate_cb(GtkWidget *widget, 1197 GtkAllocation *allocation) 1198 { 1199 gtk_window_get_size(GTK_WINDOW(widget), 1200 &prefs_common.actionsiodialog_width, &prefs_common.actionsiodialog_height); 1201 } 1202 1203 static void create_io_dialog(Children *children) 1204 { 1205 GtkWidget *dialog; 1206 GtkWidget *vbox; 1207 GtkWidget *entry = NULL; 1208 GtkWidget *input_hbox = NULL; 1209 GtkWidget *send_button; 1210 GtkWidget *label; 1211 GtkWidget *text; 1212 GtkWidget *scrolledwin; 1213 GtkWidget *progress_bar = NULL; 1214 GtkWidget *abort_button; 1215 GtkWidget *close_button; 1216 static GdkGeometry geometry; 1217 1218 debug_print("Creating action IO dialog\n"); 1219 1220 dialog = gtk_dialog_new(); 1221 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); 1222 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output")); 1223 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); 1224 manage_window_set_transient(GTK_WINDOW(dialog)); 1225 g_signal_connect(G_OBJECT(dialog), "delete_event", 1226 G_CALLBACK(delete_io_dialog_cb), children); 1227 g_signal_connect(G_OBJECT(dialog), "destroy", 1228 G_CALLBACK(hide_io_dialog_cb), 1229 children); 1230 g_signal_connect(G_OBJECT(dialog), "size_allocate", 1231 G_CALLBACK(actions_io_size_allocate_cb), NULL); 1232 1233 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); 1234 gtk_container_add(GTK_CONTAINER( 1235 gtk_dialog_get_content_area(GTK_DIALOG(dialog))), vbox); 1236 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8); 1237 gtk_widget_show(vbox); 1238 1239 label = gtk_label_new(children->action); 1240 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); 1241 gtk_widget_show(label); 1242 1243 scrolledwin = gtk_scrolled_window_new(NULL, NULL); 1244 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), 1245 GTK_POLICY_AUTOMATIC, 1246 GTK_POLICY_AUTOMATIC); 1247 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin), 1248 GTK_SHADOW_IN); 1249 gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolledwin), TRUE); 1250 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0); 1251 gtk_widget_hide(scrolledwin); 1252 1253 text = gtk_text_view_new(); 1254 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE); 1255 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD); 1256 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6); 1257 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6); 1258 gtk_container_add(GTK_CONTAINER(scrolledwin), text); 1259 gtk_widget_show(text); 1260 1261 if (children->open_in) { 1262 input_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8); 1263 gtk_widget_show(input_hbox); 1264 1265 entry = gtk_entry_new(); 1266 gtk_widget_set_size_request(entry, 320, -1); 1267 g_signal_connect(G_OBJECT(entry), "activate", 1268 G_CALLBACK(send_input), children); 1269 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0); 1270 if (children->action_type & ACTION_USER_HIDDEN_IN) { 1271 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); 1272 } 1273 gtk_widget_show(entry); 1274 1275 send_button = gtkut_stock_button("system-run", _("_Execute")); 1276 g_signal_connect(G_OBJECT(send_button), "clicked", 1277 G_CALLBACK(send_input), children); 1278 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE, 1279 FALSE, 0); 1280 gtk_widget_show(send_button); 1281 1282 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0); 1283 gtk_widget_grab_focus(entry); 1284 } 1285 1286 if (children->initial_nb > 1) { 1287 gchar *text; 1288 const gchar *format = "%s 0 / %d\n"; 1289 1290 progress_bar = gtk_progress_bar_new(); 1291 gtk_orientable_set_orientation(GTK_ORIENTABLE(progress_bar), 1292 GTK_ORIENTATION_HORIZONTAL); 1293 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR(progress_bar), 1294 FALSE); 1295 1296 text = g_strdup_printf(format, _("Completed"), 1297 children->initial_nb); 1298 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), 1299 text); 1300 g_free(text); 1301 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0); 1302 gtk_widget_show(progress_bar); 1303 } 1304 1305 abort_button = gtk_dialog_add_button(GTK_DIALOG(dialog), _("_Stop"), 1306 GTK_RESPONSE_NONE); 1307 close_button = gtk_dialog_add_button(GTK_DIALOG(dialog),_("_Close"), 1308 GTK_RESPONSE_NONE); 1309 gtk_widget_grab_default(close_button); 1310 g_signal_connect(G_OBJECT(abort_button), "clicked", 1311 G_CALLBACK(kill_children_cb), children); 1312 g_signal_connect(G_OBJECT(close_button), "clicked", 1313 G_CALLBACK(hide_io_dialog_cb), children); 1314 1315 if (children->nb) 1316 gtk_widget_set_sensitive(close_button, FALSE); 1317 1318 if (!geometry.min_height) { 1319 geometry.min_width = 582; 1320 geometry.min_height = 310; 1321 } 1322 1323 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry, 1324 GDK_HINT_MIN_SIZE); 1325 gtk_window_set_default_size(GTK_WINDOW(dialog), prefs_common.actionsiodialog_width, 1326 prefs_common.actionsiodialog_height); 1327 1328 gtk_widget_show(dialog); 1329 1330 children->dialog = dialog; 1331 children->scrolledwin = scrolledwin; 1332 children->text = text; 1333 children->input_hbox = children->open_in ? input_hbox : NULL; 1334 children->input_entry = children->open_in ? entry : NULL; 1335 children->progress_bar = progress_bar; 1336 children->abort_btn = abort_button; 1337 children->close_btn = close_button; 1338 } 1339 1340 static void catch_status(GPid pid, gint status, gpointer data) 1341 { 1342 ChildInfo *child_info = (ChildInfo *)data; 1343 1344 debug_print("Child returned %d\n", status); 1345 1346 childinfo_close_pipes(child_info); 1347 g_spawn_close_pid(child_info->pid); 1348 child_info->pid = 0; 1349 1350 if (child_info->children->action_type & (ACTION_SINGLE | ACTION_MULTIPLE) 1351 && child_info->msginfo_list) { 1352 /* Actions on message *files* might change size and 1353 * time stamp, and thus invalidate the cache */ 1354 SummaryView *summaryview = NULL; 1355 GSList *cur; 1356 MsgInfo *msginfo, *nmi; /* newmsginfo */ 1357 char *file; 1358 gboolean modified_something = FALSE; 1359 FolderItem *last_item = NULL; 1360 if (mainwindow_get_mainwindow ()) 1361 summaryview = mainwindow_get_mainwindow ()->summaryview; 1362 for (cur = child_info->msginfo_list; cur; cur = cur->next) { 1363 msginfo = (MsgInfo *)cur->data; 1364 if (!(msginfo && /* Stuff used valid? */ 1365 msginfo->folder && msginfo->folder->cache)) 1366 continue; 1367 file = procmsg_get_message_file_path (msginfo); 1368 if (!file) 1369 continue; 1370 nmi = procheader_parse_file (file, msginfo->flags, TRUE, FALSE); 1371 if (!nmi) 1372 continue; /* Deleted? */ 1373 if (msginfo->mtime != nmi->mtime || msginfo->size != nmi->size) { 1374 nmi->folder = msginfo->folder; 1375 nmi->msgnum = msginfo->msgnum; 1376 msgcache_update_msg (msginfo->folder->cache, nmi); 1377 modified_something = TRUE; 1378 last_item = nmi->folder; 1379 } 1380 procmsg_msginfo_free (&nmi); 1381 if (summaryview && summaryview->displayed && 1382 summaryview->folder_item == msginfo->folder && 1383 summary_get_msgnum(summaryview, summaryview->displayed) == msginfo->msgnum) 1384 summary_redisplay_msg(summaryview); 1385 1386 } 1387 if (modified_something && last_item && 1388 summaryview && summaryview->folder_item == last_item) { 1389 summary_show (summaryview, summaryview->folder_item, FALSE); 1390 } 1391 g_slist_free (child_info->msginfo_list); 1392 child_info->msginfo_list = NULL; 1393 } 1394 1395 if (!child_info->pid) 1396 child_info->children->nb--; 1397 1398 wait_for_children(child_info->children); 1399 } 1400 1401 static void catch_input(gpointer data, gint source, GIOCondition cond) 1402 { 1403 Children *children = (Children *)data; 1404 ChildInfo *child_info = (ChildInfo *)children->list->data; 1405 gchar *input, *ret_str; 1406 gint c, count, len, r; 1407 gssize by_read = 0, by_written = 0; 1408 1409 debug_print("Sending input to grand child.\n"); 1410 if (!(cond & (G_IO_OUT | G_IO_ERR))) 1411 return; 1412 1413 gtk_widget_set_sensitive(children->input_hbox, FALSE); 1414 gtk_widget_grab_focus(children->abort_btn); 1415 1416 g_source_remove(child_info->tag_in); 1417 child_info->tag_in = -1; 1418 1419 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry), 1420 0, -1); 1421 ret_str = g_locale_from_utf8(input, strlen(input), &by_read, 1422 &by_written, NULL); 1423 if (ret_str) { 1424 if (by_written) { 1425 g_free(input); 1426 input = ret_str; 1427 } else 1428 g_free(ret_str); 1429 } 1430 1431 len = strlen(input); 1432 count = 0; 1433 1434 do { 1435 c = write(child_info->chld_in, input + count, len - count); 1436 if (c >= 0) 1437 count += c; 1438 } while (c >= 0 && count < len); 1439 1440 if (c >= 0) 1441 r = write(child_info->chld_in, "\n", 2); 1442 1443 g_free(input); 1444 1445 r = close(child_info->chld_in); 1446 child_info->chld_in = -1; 1447 if (r != 0) 1448 debug_print("closing child input fd: %s (%d)\n", g_strerror(errno), errno); 1449 child_info->chld_in = -1; 1450 debug_print("Input to grand child sent.\n"); 1451 } 1452 1453 static void catch_output(gpointer data, gint source, GIOCondition cond) 1454 { 1455 ChildInfo *child_info = (ChildInfo *)data; 1456 gint c; 1457 gchar buf[BUFFSIZE]; 1458 1459 debug_print("Catching grand child's output.\n"); 1460 if (child_info->children->action_type & 1461 (ACTION_PIPE_OUT | ACTION_INSERT) 1462 && source == child_info->chld_out) { 1463 GtkTextView *text = 1464 GTK_TEXT_VIEW(child_info->children->msg_text); 1465 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text); 1466 GtkTextIter iter; 1467 GtkTextMark *mark; 1468 gint ins_pos; 1469 1470 mark = gtk_text_buffer_get_insert(textbuf); 1471 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark); 1472 ins_pos = gtk_text_iter_get_offset(&iter); 1473 1474 while (TRUE) { 1475 gsize bytes_read = 0, bytes_written = 0; 1476 gchar *ret_str; 1477 1478 c = read(source, buf, sizeof(buf) - 1); 1479 if (c <= 0) 1480 break; 1481 1482 buf[c] = 0; 1483 ret_str = g_locale_to_utf8 1484 (buf, c, &bytes_read, &bytes_written, NULL); 1485 if (ret_str && bytes_written > 0) 1486 gtk_text_buffer_insert(textbuf, &iter, ret_str, -1); 1487 else 1488 gtk_text_buffer_insert(textbuf, &iter, buf, c); 1489 if (ret_str) 1490 g_free(ret_str); 1491 } 1492 1493 if (child_info->children->is_selection) { 1494 GtkTextIter ins; 1495 1496 gtk_text_buffer_get_iter_at_offset 1497 (textbuf, &ins, ins_pos); 1498 gtk_text_buffer_select_range(textbuf, &ins, &iter); 1499 } 1500 } else { 1501 c = read(source, buf, sizeof(buf) - 1); 1502 if (c > 0) { 1503 gsize bytes_read = 0, bytes_written = 0; 1504 gchar *ret_str; 1505 1506 buf[c] = 0; 1507 ret_str = g_locale_to_utf8 1508 (buf, c, &bytes_read, &bytes_written, NULL); 1509 if (ret_str) { 1510 if (bytes_written) { 1511 g_string_append_len 1512 (child_info->output, ret_str, 1513 bytes_written); 1514 } 1515 g_free(ret_str); 1516 } else 1517 g_string_append_len(child_info->output, buf, c); 1518 1519 child_info->new_out = TRUE; 1520 } 1521 } 1522 if (c == 0) { 1523 if (source == child_info->chld_out) { 1524 g_source_remove(child_info->tag_out); 1525 child_info->tag_out = -1; 1526 (void)close(child_info->chld_out); 1527 child_info->chld_out = -1; 1528 } else { 1529 g_source_remove(child_info->tag_err); 1530 child_info->tag_err = -1; 1531 (void)close(child_info->chld_err); 1532 child_info->chld_err = -1; 1533 } 1534 } 1535 1536 wait_for_children(child_info->children); 1537 } 1538 1539 static gchar *get_user_string(const gchar *action, ActionType type) 1540 { 1541 gchar *message; 1542 gchar *user_str = NULL; 1543 1544 switch (type) { 1545 case ACTION_USER_HIDDEN_STR: 1546 message = g_strdup_printf 1547 (_("Enter the argument for the following action:\n" 1548 "('%%h' will be replaced with the argument)\n" 1549 " %s"), 1550 action); 1551 user_str = input_dialog_with_invisible 1552 (_("Action's hidden user argument"), message, NULL); 1553 break; 1554 case ACTION_USER_STR: 1555 message = g_strdup_printf 1556 (_("Enter the argument for the following action:\n" 1557 "('%%u' will be replaced with the argument)\n" 1558 " %s"), 1559 action); 1560 user_str = input_dialog 1561 (_("Action's user argument"), message, NULL); 1562 break; 1563 default: 1564 g_warning("unsupported action type %d", type); 1565 } 1566 1567 return user_str; 1568 }