root / src / main.c @ 1
History | View | Annotate | Download (17.2 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2004 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/gtkmain.h> |
| 28 | #include <gtk/gtkrc.h> |
| 29 | |
| 30 | #if HAVE_GDK_IMLIB
|
| 31 | # include <gdk_imlib.h> |
| 32 | #endif
|
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <ctype.h> |
| 38 | #include <unistd.h> |
| 39 | #include <time.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <signal.h> |
| 43 | |
| 44 | #if HAVE_LOCALE_H
|
| 45 | # include <locale.h> |
| 46 | #endif
|
| 47 | |
| 48 | #if USE_GPGME
|
| 49 | # include <gpgme.h> |
| 50 | #endif
|
| 51 | |
| 52 | #include "intl.h" |
| 53 | #include "main.h" |
| 54 | #include "mainwindow.h" |
| 55 | #include "folderview.h" |
| 56 | #include "summaryview.h" |
| 57 | #include "prefs_common.h" |
| 58 | #include "prefs_account.h" |
| 59 | #include "prefs_actions.h" |
| 60 | #include "prefs_display_header.h" |
| 61 | #include "account.h" |
| 62 | #include "procmsg.h" |
| 63 | #include "filter.h" |
| 64 | #include "inc.h" |
| 65 | #include "import.h" |
| 66 | #include "manage_window.h" |
| 67 | #include "alertpanel.h" |
| 68 | #include "statusbar.h" |
| 69 | #include "addressbook.h" |
| 70 | #include "compose.h" |
| 71 | #include "folder.h" |
| 72 | #include "setup.h" |
| 73 | #include "utils.h" |
| 74 | #include "gtkutils.h" |
| 75 | #include "socket.h" |
| 76 | |
| 77 | #if USE_GPGME
|
| 78 | # include "rfc2015.h" |
| 79 | #endif
|
| 80 | #if USE_SSL
|
| 81 | # include "ssl.h" |
| 82 | #endif
|
| 83 | |
| 84 | #include "version.h" |
| 85 | |
| 86 | gchar *prog_version; |
| 87 | gchar *startup_dir; |
| 88 | gboolean debug_mode = FALSE; |
| 89 | |
| 90 | static gint lock_socket = -1; |
| 91 | static gint lock_socket_tag = 0; |
| 92 | |
| 93 | static struct RemoteCmd { |
| 94 | gboolean receive; |
| 95 | gboolean receive_all; |
| 96 | gboolean compose; |
| 97 | const gchar *compose_mailto;
|
| 98 | GPtrArray *attach_files; |
| 99 | gboolean status; |
| 100 | gboolean status_full; |
| 101 | GPtrArray *status_folders; |
| 102 | GPtrArray *status_full_folders; |
| 103 | gboolean send; |
| 104 | } cmd; |
| 105 | |
| 106 | static void parse_cmd_opt(int argc, char *argv[]); |
| 107 | |
| 108 | #if USE_GPGME
|
| 109 | static void idle_function_for_gpgme(void); |
| 110 | #endif /* USE_GPGME */ |
| 111 | |
| 112 | static gint prohibit_duplicate_launch (void); |
| 113 | static gint lock_socket_remove (void); |
| 114 | static void lock_socket_input_cb (gpointer data, |
| 115 | gint source, |
| 116 | GdkInputCondition condition); |
| 117 | static gchar *get_socket_name (void); |
| 118 | |
| 119 | static void open_compose_new (const gchar *address, |
| 120 | GPtrArray *attach_files); |
| 121 | |
| 122 | static void send_queue (void); |
| 123 | |
| 124 | #if 0
|
| 125 | /* for gettext */ |
| 126 | _("File `%s' already exists.\n"
|
| 127 | "Can't create folder.") |
| 128 | #endif |
| 129 | |
| 130 | #define MAKE_DIR_IF_NOT_EXIST(dir) \
|
| 131 | { \
|
| 132 | if (!is_dir_exist(dir)) { \
|
| 133 | if (is_file_exist(dir)) { \
|
| 134 | alertpanel_warning \ |
| 135 | (_("File `%s' already exists.\n" \
|
| 136 | "Can't create folder."), \
|
| 137 | dir); \ |
| 138 | return 1; \ |
| 139 | } \ |
| 140 | if (make_dir(dir) < 0) \ |
| 141 | return 1; \ |
| 142 | } \ |
| 143 | } |
| 144 | |
| 145 | int main(int argc, char *argv[]) |
| 146 | {
|
| 147 | gchar *userrc; |
| 148 | MainWindow *mainwin; |
| 149 | FolderView *folderview; |
| 150 | |
| 151 | setlocale(LC_ALL, "");
|
| 152 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 153 | bind_textdomain_codeset(PACKAGE, CS_UTF_8); |
| 154 | textdomain(PACKAGE); |
| 155 | |
| 156 | prog_version = PROG_VERSION; |
| 157 | startup_dir = g_get_current_dir(); |
| 158 | |
| 159 | parse_cmd_opt(argc, argv); |
| 160 | |
| 161 | /* check and create unix domain socket for remote operation */
|
| 162 | lock_socket = prohibit_duplicate_launch(); |
| 163 | if (lock_socket < 0) return 0; |
| 164 | |
| 165 | if (cmd.status || cmd.status_full) {
|
| 166 | puts("0 Sylpheed not running.");
|
| 167 | lock_socket_remove(); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | gtk_set_locale(); |
| 172 | gtk_init(&argc, &argv); |
| 173 | |
| 174 | gdk_rgb_init(); |
| 175 | gtk_widget_set_default_colormap(gdk_rgb_get_cmap()); |
| 176 | gtk_widget_set_default_visual(gdk_rgb_get_visual()); |
| 177 | |
| 178 | #if USE_THREADS || USE_LDAP
|
| 179 | g_thread_init(NULL);
|
| 180 | if (!g_thread_supported())
|
| 181 | g_error(_("g_thread is not supported by glib.\n"));
|
| 182 | #endif
|
| 183 | |
| 184 | #if HAVE_GDK_IMLIB
|
| 185 | gdk_imlib_init(); |
| 186 | gtk_widget_push_visual(gdk_imlib_get_visual()); |
| 187 | gtk_widget_push_colormap(gdk_imlib_get_colormap()); |
| 188 | #endif
|
| 189 | |
| 190 | #if USE_SSL
|
| 191 | ssl_init(); |
| 192 | #endif
|
| 193 | |
| 194 | srandom((gint)time(NULL));
|
| 195 | |
| 196 | /* parse gtkrc files */
|
| 197 | userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
|
| 198 | NULL);
|
| 199 | gtk_rc_parse(userrc); |
| 200 | g_free(userrc); |
| 201 | userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
|
| 202 | G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL); |
| 203 | gtk_rc_parse(userrc); |
| 204 | g_free(userrc); |
| 205 | userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL); |
| 206 | gtk_rc_parse(userrc); |
| 207 | g_free(userrc); |
| 208 | |
| 209 | userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
|
| 210 | gtk_accel_map_load(userrc); |
| 211 | g_free(userrc); |
| 212 | |
| 213 | CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
|
| 214 | |
| 215 | /* backup if old rc file exists */
|
| 216 | if (is_file_exist(RC_DIR)) {
|
| 217 | if (rename(RC_DIR, RC_DIR ".bak") < 0) |
| 218 | FILE_OP_ERROR(RC_DIR, "rename");
|
| 219 | } |
| 220 | MAKE_DIR_IF_NOT_EXIST(RC_DIR); |
| 221 | MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir()); |
| 222 | MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir()); |
| 223 | MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir()); |
| 224 | MAKE_DIR_IF_NOT_EXIST(get_tmp_dir()); |
| 225 | MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
|
| 226 | |
| 227 | if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) { |
| 228 | if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log", |
| 229 | RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0) |
| 230 | FILE_OP_ERROR("sylpheed.log", "rename"); |
| 231 | } |
| 232 | set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
|
| 233 | |
| 234 | prefs_common_read_config(); |
| 235 | |
| 236 | #if USE_GPGME
|
| 237 | if (gpgme_check_engine()) { /* Also does some gpgme init */ |
| 238 | rfc2015_disable_all(); |
| 239 | debug_print("gpgme_engine_version:\n%s\n",
|
| 240 | gpgme_get_engine_info()); |
| 241 | |
| 242 | if (prefs_common.gpg_warning) {
|
| 243 | AlertValue val; |
| 244 | |
| 245 | val = alertpanel_message_with_disable |
| 246 | (_("Warning"),
|
| 247 | _("GnuPG is not installed properly, or its version is too old.\n"
|
| 248 | "OpenPGP support disabled."),
|
| 249 | ALERT_WARNING); |
| 250 | if (val & G_ALERTDISABLE)
|
| 251 | prefs_common.gpg_warning = FALSE; |
| 252 | } |
| 253 | } |
| 254 | gpgme_register_idle(idle_function_for_gpgme); |
| 255 | #endif
|
| 256 | |
| 257 | sock_set_io_timeout(prefs_common.io_timeout_secs); |
| 258 | |
| 259 | prefs_filter_read_config(); |
| 260 | prefs_actions_read_config(); |
| 261 | prefs_display_header_read_config(); |
| 262 | |
| 263 | gtkut_widget_init(); |
| 264 | |
| 265 | mainwin = main_window_create |
| 266 | (prefs_common.sep_folder | prefs_common.sep_msg << 1);
|
| 267 | folderview = mainwin->folderview; |
| 268 | |
| 269 | /* register the callback of unix domain socket input */
|
| 270 | lock_socket_tag = gdk_input_add(lock_socket, |
| 271 | GDK_INPUT_READ | GDK_INPUT_EXCEPTION, |
| 272 | lock_socket_input_cb, |
| 273 | mainwin); |
| 274 | |
| 275 | account_read_config_all(); |
| 276 | |
| 277 | if (folder_read_list() < 0) { |
| 278 | setup(mainwin); |
| 279 | folder_write_list(); |
| 280 | } |
| 281 | if (!account_get_list()) {
|
| 282 | account_edit_open(); |
| 283 | account_add(); |
| 284 | } |
| 285 | |
| 286 | account_set_missing_folder(); |
| 287 | folder_set_missing_folders(); |
| 288 | folderview_set(folderview); |
| 289 | |
| 290 | addressbook_read_file(); |
| 291 | |
| 292 | inc_autocheck_timer_init(mainwin); |
| 293 | |
| 294 | /* ignore SIGPIPE signal for preventing sudden death of program */
|
| 295 | signal(SIGPIPE, SIG_IGN); |
| 296 | |
| 297 | if (cmd.receive_all)
|
| 298 | inc_all_account_mail(mainwin, FALSE); |
| 299 | else if (prefs_common.chk_on_startup) |
| 300 | inc_all_account_mail(mainwin, TRUE); |
| 301 | else if (cmd.receive) |
| 302 | inc_mail(mainwin); |
| 303 | else
|
| 304 | gtk_widget_grab_focus(folderview->ctree); |
| 305 | |
| 306 | if (cmd.compose)
|
| 307 | open_compose_new(cmd.compose_mailto, cmd.attach_files); |
| 308 | if (cmd.attach_files) {
|
| 309 | ptr_array_free_strings(cmd.attach_files); |
| 310 | g_ptr_array_free(cmd.attach_files, TRUE); |
| 311 | cmd.attach_files = NULL;
|
| 312 | } |
| 313 | if (cmd.send)
|
| 314 | send_queue(); |
| 315 | if (cmd.status_folders) {
|
| 316 | g_ptr_array_free(cmd.status_folders, TRUE); |
| 317 | cmd.status_folders = NULL;
|
| 318 | } |
| 319 | if (cmd.status_full_folders) {
|
| 320 | g_ptr_array_free(cmd.status_full_folders, TRUE); |
| 321 | cmd.status_full_folders = NULL;
|
| 322 | } |
| 323 | |
| 324 | gtk_main(); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void parse_cmd_opt(int argc, char *argv[]) |
| 330 | {
|
| 331 | gint i; |
| 332 | |
| 333 | for (i = 1; i < argc; i++) { |
| 334 | if (!strncmp(argv[i], "--debug", 7)) |
| 335 | debug_mode = TRUE; |
| 336 | else if (!strncmp(argv[i], "--receive-all", 13)) |
| 337 | cmd.receive_all = TRUE; |
| 338 | else if (!strncmp(argv[i], "--receive", 9)) |
| 339 | cmd.receive = TRUE; |
| 340 | else if (!strncmp(argv[i], "--compose", 9)) { |
| 341 | const gchar *p = argv[i + 1]; |
| 342 | |
| 343 | cmd.compose = TRUE; |
| 344 | cmd.compose_mailto = NULL;
|
| 345 | if (p && *p != '\0' && *p != '-') { |
| 346 | if (!strncmp(p, "mailto:", 7)) |
| 347 | cmd.compose_mailto = p + 7;
|
| 348 | else
|
| 349 | cmd.compose_mailto = p; |
| 350 | i++; |
| 351 | } |
| 352 | } else if (!strncmp(argv[i], "--attach", 8)) { |
| 353 | const gchar *p = argv[i + 1]; |
| 354 | gchar *file; |
| 355 | |
| 356 | while (p && *p != '\0' && *p != '-') { |
| 357 | if (!cmd.attach_files)
|
| 358 | cmd.attach_files = g_ptr_array_new(); |
| 359 | if (*p != G_DIR_SEPARATOR)
|
| 360 | file = g_strconcat(startup_dir, |
| 361 | G_DIR_SEPARATOR_S, |
| 362 | p, NULL);
|
| 363 | else
|
| 364 | file = g_strdup(p); |
| 365 | g_ptr_array_add(cmd.attach_files, file); |
| 366 | i++; |
| 367 | p = argv[i + 1];
|
| 368 | } |
| 369 | } else if (!strncmp(argv[i], "--send", 6)) { |
| 370 | cmd.send = TRUE; |
| 371 | } else if (!strncmp(argv[i], "--version", 9)) { |
| 372 | puts("Sylpheed version " VERSION);
|
| 373 | exit(0);
|
| 374 | } else if (!strncmp(argv[i], "--status-full", 13)) { |
| 375 | const gchar *p = argv[i + 1]; |
| 376 | |
| 377 | cmd.status_full = TRUE; |
| 378 | while (p && *p != '\0' && *p != '-') { |
| 379 | if (!cmd.status_full_folders)
|
| 380 | cmd.status_full_folders = |
| 381 | g_ptr_array_new(); |
| 382 | g_ptr_array_add(cmd.status_full_folders, |
| 383 | g_strdup(p)); |
| 384 | i++; |
| 385 | p = argv[i + 1];
|
| 386 | } |
| 387 | } else if (!strncmp(argv[i], "--status", 8)) { |
| 388 | const gchar *p = argv[i + 1]; |
| 389 | |
| 390 | cmd.status = TRUE; |
| 391 | while (p && *p != '\0' && *p != '-') { |
| 392 | if (!cmd.status_folders)
|
| 393 | cmd.status_folders = g_ptr_array_new(); |
| 394 | g_ptr_array_add(cmd.status_folders, |
| 395 | g_strdup(p)); |
| 396 | i++; |
| 397 | p = argv[i + 1];
|
| 398 | } |
| 399 | } else if (!strncmp(argv[i], "--help", 6)) { |
| 400 | g_print(_("Usage: %s [OPTION]...\n"),
|
| 401 | g_basename(argv[0]));
|
| 402 | |
| 403 | puts(_(" --compose [address] open composition window"));
|
| 404 | puts(_(" --attach file1 [file2]...\n"
|
| 405 | " open composition window with specified files\n"
|
| 406 | " attached"));
|
| 407 | puts(_(" --receive receive new messages"));
|
| 408 | puts(_(" --receive-all receive new messages of all accounts"));
|
| 409 | puts(_(" --send send all queued messages"));
|
| 410 | puts(_(" --status [folder]... show the total number of messages"));
|
| 411 | puts(_(" --status-full [folder]...\n"
|
| 412 | " show the status of each folder"));
|
| 413 | puts(_(" --debug debug mode"));
|
| 414 | puts(_(" --help display this help and exit"));
|
| 415 | puts(_(" --version output version information and exit"));
|
| 416 | |
| 417 | exit(1);
|
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (cmd.attach_files && cmd.compose == FALSE) {
|
| 422 | cmd.compose = TRUE; |
| 423 | cmd.compose_mailto = NULL;
|
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static gint get_queued_message_num(void) |
| 428 | {
|
| 429 | FolderItem *queue; |
| 430 | |
| 431 | queue = folder_get_default_queue(); |
| 432 | if (!queue) return -1; |
| 433 | |
| 434 | folder_item_scan(queue); |
| 435 | return queue->total;
|
| 436 | } |
| 437 | |
| 438 | void app_will_exit(GtkWidget *widget, gpointer data)
|
| 439 | {
|
| 440 | MainWindow *mainwin = data; |
| 441 | gchar *filename; |
| 442 | |
| 443 | if (compose_get_compose_list()) {
|
| 444 | if (alertpanel(_("Notice"), |
| 445 | _("Composing message exists. Really quit?"),
|
| 446 | _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT) |
| 447 | return;
|
| 448 | manage_window_focus_in(mainwin->window, NULL, NULL); |
| 449 | } |
| 450 | |
| 451 | if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) { |
| 452 | if (alertpanel(_("Queued messages"), |
| 453 | _("Some unsent messages are queued. Exit now?"),
|
| 454 | _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT) |
| 455 | return;
|
| 456 | manage_window_focus_in(mainwin->window, NULL, NULL); |
| 457 | } |
| 458 | |
| 459 | inc_autocheck_timer_remove(); |
| 460 | |
| 461 | if (prefs_common.clean_on_exit)
|
| 462 | main_window_empty_trash(mainwin, prefs_common.ask_on_clean); |
| 463 | |
| 464 | /* save all state before exiting */
|
| 465 | folder_write_list(); |
| 466 | summary_write_cache(mainwin->summaryview); |
| 467 | |
| 468 | main_window_get_size(mainwin); |
| 469 | main_window_get_position(mainwin); |
| 470 | prefs_common_write_config(); |
| 471 | prefs_filter_write_config(); |
| 472 | account_write_config_all(); |
| 473 | addressbook_export_to_file(); |
| 474 | |
| 475 | filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
|
| 476 | gtk_accel_map_save(filename); |
| 477 | g_free(filename); |
| 478 | |
| 479 | /* delete temporary files */
|
| 480 | remove_all_files(get_mime_tmp_dir()); |
| 481 | |
| 482 | close_log_file(); |
| 483 | lock_socket_remove(); |
| 484 | |
| 485 | #if USE_SSL
|
| 486 | ssl_done(); |
| 487 | #endif
|
| 488 | |
| 489 | gtk_main_quit(); |
| 490 | } |
| 491 | |
| 492 | #if USE_GPGME
|
| 493 | static void idle_function_for_gpgme(void) |
| 494 | {
|
| 495 | while (gtk_events_pending())
|
| 496 | gtk_main_iteration(); |
| 497 | } |
| 498 | #endif /* USE_GPGME */ |
| 499 | |
| 500 | static gchar *get_socket_name(void) |
| 501 | {
|
| 502 | static gchar *filename = NULL; |
| 503 | |
| 504 | if (filename == NULL) { |
| 505 | filename = g_strdup_printf("%s%csylpheed-%d",
|
| 506 | g_get_tmp_dir(), G_DIR_SEPARATOR, |
| 507 | getuid()); |
| 508 | } |
| 509 | |
| 510 | return filename;
|
| 511 | } |
| 512 | |
| 513 | static gint prohibit_duplicate_launch(void) |
| 514 | {
|
| 515 | gint uxsock; |
| 516 | gchar *path; |
| 517 | |
| 518 | path = get_socket_name(); |
| 519 | uxsock = fd_connect_unix(path); |
| 520 | if (uxsock < 0) { |
| 521 | unlink(path); |
| 522 | return fd_open_unix(path);
|
| 523 | } |
| 524 | |
| 525 | /* remote command mode */
|
| 526 | |
| 527 | debug_print(_("another Sylpheed is already running.\n"));
|
| 528 | |
| 529 | if (cmd.receive_all)
|
| 530 | fd_write_all(uxsock, "receive_all\n", 12); |
| 531 | else if (cmd.receive) |
| 532 | fd_write_all(uxsock, "receive\n", 8); |
| 533 | else if (cmd.compose && cmd.attach_files) { |
| 534 | gchar *str, *compose_str; |
| 535 | gint i; |
| 536 | |
| 537 | if (cmd.compose_mailto)
|
| 538 | compose_str = g_strdup_printf("compose_attach %s\n",
|
| 539 | cmd.compose_mailto); |
| 540 | else
|
| 541 | compose_str = g_strdup("compose_attach\n");
|
| 542 | |
| 543 | fd_write_all(uxsock, compose_str, strlen(compose_str)); |
| 544 | g_free(compose_str); |
| 545 | |
| 546 | for (i = 0; i < cmd.attach_files->len; i++) { |
| 547 | str = g_ptr_array_index(cmd.attach_files, i); |
| 548 | fd_write_all(uxsock, str, strlen(str)); |
| 549 | fd_write_all(uxsock, "\n", 1); |
| 550 | } |
| 551 | |
| 552 | fd_write_all(uxsock, ".\n", 2); |
| 553 | } else if (cmd.compose) { |
| 554 | gchar *compose_str; |
| 555 | |
| 556 | if (cmd.compose_mailto)
|
| 557 | compose_str = g_strdup_printf |
| 558 | ("compose %s\n", cmd.compose_mailto);
|
| 559 | else
|
| 560 | compose_str = g_strdup("compose\n");
|
| 561 | |
| 562 | fd_write_all(uxsock, compose_str, strlen(compose_str)); |
| 563 | g_free(compose_str); |
| 564 | } else if (cmd.send) { |
| 565 | fd_write_all(uxsock, "send\n", 5); |
| 566 | } else if (cmd.status || cmd.status_full) { |
| 567 | gchar buf[BUFFSIZE]; |
| 568 | gint i; |
| 569 | const gchar *command;
|
| 570 | GPtrArray *folders; |
| 571 | gchar *folder; |
| 572 | |
| 573 | command = cmd.status_full ? "status-full\n" : "status\n"; |
| 574 | folders = cmd.status_full ? cmd.status_full_folders : |
| 575 | cmd.status_folders; |
| 576 | |
| 577 | fd_write_all(uxsock, command, strlen(command)); |
| 578 | for (i = 0; folders && i < folders->len; ++i) { |
| 579 | folder = g_ptr_array_index(folders, i); |
| 580 | fd_write_all(uxsock, folder, strlen(folder)); |
| 581 | fd_write_all(uxsock, "\n", 1); |
| 582 | } |
| 583 | fd_write_all(uxsock, ".\n", 2); |
| 584 | for (;;) {
|
| 585 | fd_gets(uxsock, buf, sizeof(buf));
|
| 586 | if (!strncmp(buf, ".\n", 2)) break; |
| 587 | fputs(buf, stdout); |
| 588 | } |
| 589 | } else
|
| 590 | fd_write_all(uxsock, "popup\n", 6); |
| 591 | |
| 592 | fd_close(uxsock); |
| 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | static gint lock_socket_remove(void) |
| 597 | {
|
| 598 | gchar *filename; |
| 599 | |
| 600 | if (lock_socket < 0) return -1; |
| 601 | |
| 602 | if (lock_socket_tag > 0) |
| 603 | gdk_input_remove(lock_socket_tag); |
| 604 | fd_close(lock_socket); |
| 605 | filename = get_socket_name(); |
| 606 | unlink(filename); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static GPtrArray *get_folder_item_list(gint sock)
|
| 612 | {
|
| 613 | gchar buf[BUFFSIZE]; |
| 614 | FolderItem *item; |
| 615 | GPtrArray *folders = NULL;
|
| 616 | |
| 617 | for (;;) {
|
| 618 | fd_gets(sock, buf, sizeof(buf));
|
| 619 | if (!strncmp(buf, ".\n", 2)) break; |
| 620 | strretchomp(buf); |
| 621 | if (!folders) folders = g_ptr_array_new();
|
| 622 | item = folder_find_item_from_identifier(buf); |
| 623 | if (item)
|
| 624 | g_ptr_array_add(folders, item); |
| 625 | else
|
| 626 | g_warning("no such folder: %s\n", buf);
|
| 627 | } |
| 628 | |
| 629 | return folders;
|
| 630 | } |
| 631 | |
| 632 | static void lock_socket_input_cb(gpointer data, |
| 633 | gint source, |
| 634 | GdkInputCondition condition) |
| 635 | {
|
| 636 | MainWindow *mainwin = (MainWindow *)data; |
| 637 | gint sock; |
| 638 | gchar buf[BUFFSIZE]; |
| 639 | |
| 640 | sock = fd_accept(source); |
| 641 | fd_gets(sock, buf, sizeof(buf));
|
| 642 | |
| 643 | if (!strncmp(buf, "popup", 5)) { |
| 644 | main_window_popup(mainwin); |
| 645 | } else if (!strncmp(buf, "receive_all", 11)) { |
| 646 | main_window_popup(mainwin); |
| 647 | inc_all_account_mail(mainwin, FALSE); |
| 648 | } else if (!strncmp(buf, "receive", 7)) { |
| 649 | main_window_popup(mainwin); |
| 650 | inc_mail(mainwin); |
| 651 | } else if (!strncmp(buf, "compose_attach", 14)) { |
| 652 | GPtrArray *files; |
| 653 | gchar *mailto; |
| 654 | |
| 655 | mailto = g_strdup(buf + strlen("compose_attach") + 1); |
| 656 | files = g_ptr_array_new(); |
| 657 | while (fd_gets(sock, buf, sizeof(buf)) > 0) { |
| 658 | if (buf[0] == '.' && buf[1] == '\n') break; |
| 659 | strretchomp(buf); |
| 660 | g_ptr_array_add(files, g_strdup(buf)); |
| 661 | } |
| 662 | open_compose_new(mailto, files); |
| 663 | ptr_array_free_strings(files); |
| 664 | g_ptr_array_free(files, TRUE); |
| 665 | g_free(mailto); |
| 666 | } else if (!strncmp(buf, "compose", 7)) { |
| 667 | open_compose_new(buf + strlen("compose") + 1, NULL); |
| 668 | } else if (!strncmp(buf, "send", 4)) { |
| 669 | send_queue(); |
| 670 | } else if (!strncmp(buf, "status-full", 11) || |
| 671 | !strncmp(buf, "status", 6)) { |
| 672 | gchar *status; |
| 673 | GPtrArray *folders; |
| 674 | |
| 675 | folders = get_folder_item_list(sock); |
| 676 | status = folder_get_status |
| 677 | (folders, !strncmp(buf, "status-full", 11)); |
| 678 | fd_write_all(sock, status, strlen(status)); |
| 679 | fd_write_all(sock, ".\n", 2); |
| 680 | g_free(status); |
| 681 | if (folders) g_ptr_array_free(folders, TRUE);
|
| 682 | } |
| 683 | |
| 684 | fd_close(sock); |
| 685 | } |
| 686 | |
| 687 | static void open_compose_new(const gchar *address, GPtrArray *attach_files) |
| 688 | {
|
| 689 | gchar *addr = NULL;
|
| 690 | |
| 691 | if (address) {
|
| 692 | Xstrdup_a(addr, address, return);
|
| 693 | g_strstrip(addr); |
| 694 | } |
| 695 | |
| 696 | compose_new(NULL, NULL, addr, attach_files); |
| 697 | } |
| 698 | |
| 699 | static void send_queue(void) |
| 700 | {
|
| 701 | GList *list; |
| 702 | |
| 703 | if (!main_window_toggle_online_if_offline(main_window_get()))
|
| 704 | return;
|
| 705 | |
| 706 | for (list = folder_get_list(); list != NULL; list = list->next) { |
| 707 | Folder *folder = list->data; |
| 708 | |
| 709 | if (folder->queue) {
|
| 710 | gint ret; |
| 711 | |
| 712 | ret = procmsg_send_queue(folder->queue, |
| 713 | prefs_common.savemsg); |
| 714 | statusbar_pop_all(); |
| 715 | if (ret > 0) |
| 716 | folder_item_scan(folder->queue); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | folderview_update_all_updated(TRUE); |
| 721 | } |