2005-11-07 [cleroy] 1.9.99cvs16
[claws.git] / src / send_message.c
index 353ab7044381c0cac83951df74c9a98d5af3eb5a..34aa6b991a711ff6521487e1959726dfd3cd0c04 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <gtk/gtkmain.h>
 #include <gtk/gtksignal.h>
 #include <gtk/gtkwindow.h>
-#include <gtk/gtkclist.h>
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include "send_message.h"
 #include "session.h"
@@ -46,6 +47,7 @@
 #include "inputdialog.h"
 #include "alertpanel.h"
 #include "manage_window.h"
+#include "socket.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "inc.h"
@@ -89,20 +91,23 @@ gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
        g_return_val_if_fail(ac_prefs != NULL, -1);
        g_return_val_if_fail(to_list != NULL, -1);
 
-       if ((fp = fopen(file, "rb")) == NULL) {
+       if ((fp = g_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return -1;
        }
 
+       inc_lock();
        if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
            (*ac_prefs->mail_command)) {
                val = send_message_local(ac_prefs->mail_command, fp);
                fclose(fp);
+               inc_unlock();
                return val;
        } else {
                val = send_message_smtp(ac_prefs, to_list, fp);
                
                fclose(fp);
+               inc_unlock();
                return val;
        }
 }
@@ -117,39 +122,62 @@ enum
 
 gint send_message_local(const gchar *command, FILE *fp)
 {
-       FILE *pipefp;
+       gchar **argv;
+       GPid pid;
+       gint child_stdin;
        gchar buf[BUFFSIZE];
-       int r;
-       sigset_t osig, mask;
+       gboolean err = FALSE;
+       gint status;
 
        g_return_val_if_fail(command != NULL, -1);
        g_return_val_if_fail(fp != NULL, -1);
 
-       pipefp = popen(command, "w");
-       if (!pipefp) {
-               g_warning("Can't execute external command: %s\n", command);
+       log_message(_("Sending message using command: %s\n"), command);
+
+       argv = strsplit_with_quote(command, " ", 0);
+
+       if (g_spawn_async_with_pipes(NULL, argv, NULL,
+                                    G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
+                                    &pid, &child_stdin, NULL, NULL,
+                                    NULL) == FALSE) {
+               g_snprintf(buf, sizeof(buf),
+                          _("Can't execute command: %s"), command);
+               log_warning("%s\n", buf);
+               alertpanel_error("%s", buf);
+               g_strfreev(argv);
                return -1;
        }
+       g_strfreev(argv);
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
-               fputs(buf, pipefp);
-               fputc('\n', pipefp);
+               if (buf[0] == '.' && buf[1] == '\0') {
+                       if (fd_write_all(child_stdin, ".", 1) < 0) {
+                               err = TRUE;
+                               break;
+                       }
+               }
+               if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
+                   fd_write_all(child_stdin, "\n", 1) < 0) {
+                       err = TRUE;
+                       break;
+               }
        }
 
-       /* we need to block SIGCHLD, otherwise pspell's handler will wait()
-        * the pipecommand away and pclose will return -1 because of its
-        * failed wait4().
-        */
-       sigemptyset(&mask);
-       sigaddset(&mask, SIGCHLD);
-       sigprocmask(SIG_BLOCK, &mask, &osig);
-       
-       r = pclose(pipefp);
+       fd_close(child_stdin);
+
+       waitpid(pid, &status, 0);
+       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               err = TRUE;
 
-       sigprocmask(SIG_SETMASK, &osig, NULL);
-       if (r != 0) {
-               g_warning("external command `%s' failed with code `%i'\n", command, r);
+       g_spawn_close_pid(pid);
+
+       if (err) {
+               g_snprintf(buf, sizeof(buf),
+                          _("Error occurred while executing command: %s"),
+                          command);
+               log_warning("%s\n", buf);
+               alertpanel_error("%s", buf);
                return -1;
        }
 
@@ -160,7 +188,7 @@ gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, g
 {
        Session *session;
        SMTPSession *smtp_session;
-       gushort port;
+       gushort port = 0;
        SendProgressDialog *dialog;
        gchar buf[BUFFSIZE];
        gint ret = 0;
@@ -230,6 +258,20 @@ gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, g
                if (ac_prefs->ssl_smtp != SSL_NONE)
                        session->nonblocking = ac_prefs->use_nonblocking_ssl;
        #else
+               if (ac_prefs->ssl_smtp != SSL_NONE) {
+                       if (alertpanel_full(_("Insecure connection"),
+                               _("This connection is configured to be secured "
+                                 "using SSL, but SSL is not available in this "
+                                 "build of Sylpheed-Claws. \n\n"
+                                 "Do you want to continue connecting to this "
+                                 "server? The communication would not be "
+                                 "secure."),
+                                 _("Con_tinue connecting"), 
+                                 GTK_STOCK_CANCEL, NULL,
+                                 FALSE, NULL, ALERT_WARNING,
+                                 G_ALERTALTERNATE) != G_ALERTDEFAULT)
+                               return -1;
+               }
                port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
        #endif
 
@@ -334,8 +376,8 @@ gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, g
         */
        if (!keep_session || ret != 0) {
                if (session_is_connected(session))
-                       smtp_quit(session);
-               while (session_is_connected(session))
+                       smtp_quit(smtp_session);
+               while (session_is_connected(session) && !dialog->cancelled)
                        gtk_main_iteration();
                session_destroy(session);
                ac_prefs->session = NULL;