2006-09-27 [wwp] 2.5.2cvs9
[claws.git] / src / send_message.c
index 8b6781124fb708471995b4a9df9cfaddc507ffc8..393515410eaa2b08127405c98dee877ae4099005 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-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
  *
  * 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 "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.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>
+#ifndef G_OS_WIN32
+#include <sys/wait.h>
+#endif
 
-#include "intl.h"
 #include "send_message.h"
 #include "session.h"
 #include "ssl.h"
@@ -46,6 +49,7 @@
 #include "inputdialog.h"
 #include "alertpanel.h"
 #include "manage_window.h"
+#include "socket.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "inc.h"
@@ -59,13 +63,6 @@ struct _SendProgressDialog
        Session *session;
        gboolean cancelled;
 };
-#if 0
-static gint send_message_local         (const gchar            *command,
-                                        FILE                   *fp);
-static gint send_message_smtp          (PrefsAccount           *ac_prefs,
-                                        GSList                 *to_list,
-                                        FILE                   *fp);
-#endif
 
 static gint send_recv_message          (Session                *session,
                                         const gchar            *msg,
@@ -96,20 +93,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;
        }
 }
@@ -122,176 +122,90 @@ enum
        Q_ACCOUNT_ID = 3
 };
 
-#if 0
-gint send_message_queue(const gchar *file)
+gint send_message_local(const gchar *command, FILE *fp)
 {
-       static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
-                                      {"SSV:", NULL, FALSE},
-                                      {"R:",   NULL, FALSE},
-                                      {"AID:", NULL, FALSE},
-                                      {NULL,   NULL, FALSE}};
-       FILE *fp;
-       gint val = 0;
-       gchar *from = NULL;
-       gchar *server = NULL;
-       GSList *to_list = NULL;
+       gchar **argv;
+       GPid pid;
+       gint child_stdin;
        gchar buf[BUFFSIZE];
-       gint hnum;
-       glong fpos;
-       PrefsAccount *ac = NULL, *mailac = NULL, *newsac = NULL;
+       gboolean err = FALSE;
+       gint status;
 
-       g_return_val_if_fail(file != NULL, -1);
-
-       if ((fp = fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return -1;
-       }
+       g_return_val_if_fail(command != NULL, -1);
+       g_return_val_if_fail(fp != NULL, -1);
 
-       while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
-              != -1) {
-               gchar *p;
+       log_message(_("Sending message using command: %s\n"), command);
 
-               p = buf + strlen(qentry[hnum].name);
+       argv = strsplit_with_quote(command, " ", 0);
 
-               switch (hnum) {
-               case Q_SENDER:
-                       if (!from) from = g_strdup(p);
-                       break;
-               case Q_SMTPSERVER:
-                       if (!server) server = g_strdup(p);
-                       break;
-               case Q_RECIPIENTS:
-                       to_list = address_list_append(to_list, p);
-                       break;
-               case Q_ACCOUNT_ID:
-                       ac = account_find_from_id(atoi(p));
-                       break;
-               default:
-                       break;
-               }
+       if (g_spawn_async_with_pipes(NULL, argv, NULL,
+#ifdef G_OS_WIN32
+                                     0,
+#else
+                                    G_SPAWN_DO_NOT_REAP_CHILD,
+#endif
+                                     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);
 
-       if (((!ac || (ac && ac->protocol != A_NNTP)) && !to_list) || !from) {
-               g_warning("Queued message header is broken.\n");
-               val = -1;
-       } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
-               val = send_message_local(prefs_common.extsend_cmd, fp);
-       } else {
-               if (ac && ac->protocol == A_NNTP) {
-                       newsac = ac;
-
-                       /* search mail account */
-                       mailac = account_find_from_address(from);
-                       if (!mailac) {
-                               if (cur_account &&
-                                   cur_account->protocol != A_NNTP)
-                                       mailac = cur_account;
-                               else {
-                                       mailac = account_get_default();
-                                       if (mailac->protocol == A_NNTP)
-                                               mailac = NULL;
-                               }
-                       }
-               } else if (ac) {
-                       mailac = ac;
-               } else {
-                       ac = account_find_from_smtp_server(from, server);
-                       if (!ac) {
-                               g_warning("Account not found. "
-                                         "Using current account...\n");
-                               ac = cur_account;
-                               if (ac && ac->protocol != A_NNTP)
-                                       mailac = ac;
-                       }
-               }
-
-               fpos = ftell(fp);
-               if (to_list) {
-                       if (mailac)
-                               val = send_message_smtp(mailac, to_list, fp);
-                       else {
-                               PrefsAccount tmp_ac;
-
-                               g_warning("Account not found.\n");
-
-                               memset(&tmp_ac, 0, sizeof(PrefsAccount));
-                               tmp_ac.address = from;
-                               tmp_ac.smtp_server = server;
-                               tmp_ac.smtpport = SMTP_PORT;
-                               val = send_message_smtp(&tmp_ac, to_list, fp);
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               strretchomp(buf);
+               if (buf[0] == '.' && buf[1] == '\0') {
+                       if (fd_write_all(child_stdin, ".", 1) < 0) {
+                               err = TRUE;
+                               break;
                        }
                }
-
-               if (val == 0 && newsac) {
-                       fseek(fp, fpos, SEEK_SET);
-                       val = news_post_stream(FOLDER(newsac->folder), fp);
-                       if (val < 0)
-                               alertpanel_error_log(_("Error occurred while posting the message to %s ."),
-                                                newsac->nntp_server);
+               if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
+                   fd_write_all(child_stdin, "\n", 1) < 0) {
+                       err = TRUE;
+                       break;
                }
        }
 
-       slist_free_strings(to_list);
-       g_slist_free(to_list);
-       g_free(from);
-       g_free(server);
-       fclose(fp);
+       fd_close(child_stdin);
 
-       return val;
-}
+#ifndef G_OS_WIN32
+       waitpid(pid, &status, 0);
+       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               err = TRUE;
 #endif
 
-gint send_message_local(const gchar *command, FILE *fp)
-{
-       FILE *pipefp;
-       gchar buf[BUFFSIZE];
-       int r;
-       sigset_t osig, mask;
-
-       g_return_val_if_fail(command != NULL, -1);
-       g_return_val_if_fail(fp != NULL, -1);
+       g_spawn_close_pid(pid);
 
-       pipefp = popen(command, "w");
-       if (!pipefp) {
-               g_warning("Can't execute external command: %s\n", command);
-               return -1;
-       }
-
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               strretchomp(buf);
-               fputs(buf, pipefp);
-               fputc('\n', pipefp);
-       }
-
-       /* 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);
-
-       sigprocmask(SIG_SETMASK, &osig, NULL);
-       if (r != 0) {
-               g_warning("external command `%s' failed with code `%i'\n", command, r);
+       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;
        }
 
        return 0;
 }
 
-gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
+gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
 {
        Session *session;
        SMTPSession *smtp_session;
-       gushort port;
+       gushort port = 0;
        SendProgressDialog *dialog;
-       GtkCList *clist;
-       const gchar *text[3];
        gchar buf[BUFFSIZE];
        gint ret = 0;
+       gboolean was_inited = FALSE;
+       MsgInfo *tmp_msginfo = NULL;
+       MsgFlags flags = {0, 0};
+       long fp_pos = 0;
+       gchar spec_from[BUFFSIZE];
 
        g_return_val_if_fail(ac_prefs != NULL, -1);
        g_return_val_if_fail(ac_prefs->address != NULL, -1);
@@ -299,121 +213,195 @@ gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
        g_return_val_if_fail(to_list != NULL, -1);
        g_return_val_if_fail(fp != NULL, -1);
 
-       session = smtp_session_new();
-       smtp_session = SMTP_SESSION(session);
-
-       smtp_session->hostname =
-               ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
-
-       if (ac_prefs->use_smtp_auth) {
-               smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
-
-               if (ac_prefs->smtp_userid) {
-                       smtp_session->user = g_strdup(ac_prefs->smtp_userid);
-                       if (ac_prefs->smtp_passwd)
-                               smtp_session->pass =
-                                       g_strdup(ac_prefs->smtp_passwd);
-                       else if (ac_prefs->tmp_smtp_pass)
-                               smtp_session->pass =
-                                       g_strdup(ac_prefs->tmp_smtp_pass);
-                       else {
-                               smtp_session->pass =
-                                       input_dialog_query_password
-                                               (ac_prefs->smtp_server,
-                                                smtp_session->user);
-                               if (!smtp_session->pass)
-                                       smtp_session->pass = g_strdup("");
-                               ac_prefs->tmp_smtp_pass =
-                                       g_strdup(smtp_session->pass);
-                       }
-               } else {
-                       smtp_session->user = g_strdup(ac_prefs->userid);
-                       if (ac_prefs->passwd)
-                               smtp_session->pass = g_strdup(ac_prefs->passwd);
-                       else if (ac_prefs->tmp_pass)
-                               smtp_session->pass =
-                                       g_strdup(ac_prefs->tmp_pass);
-                       else {
-                               smtp_session->pass =
-                                       input_dialog_query_password
-                                               (ac_prefs->smtp_server,
-                                                smtp_session->user);
-                               if (!smtp_session->pass)
-                                       smtp_session->pass = g_strdup("");
-                               ac_prefs->tmp_pass =
-                                       g_strdup(smtp_session->pass);
-                       }
-               }
+       /* get the From address used, not necessarily the ac_prefs',
+        * because it's editable. */
+
+       fp_pos = ftell(fp);
+       tmp_msginfo = procheader_parse_stream(fp, flags, FALSE, FALSE);
+       fseek(fp, fp_pos, SEEK_SET);
+       
+       if (tmp_msginfo && tmp_msginfo->from) {
+               strncpy2(spec_from, tmp_msginfo->from, BUFFSIZE-1);
+               extract_address(spec_from);
        } else {
-               smtp_session->user = NULL;
-               smtp_session->pass = NULL;
+               strncpy2(spec_from, ac_prefs->address, BUFFSIZE-1);
+       }
+       if (tmp_msginfo) {
+               procmsg_msginfo_free(tmp_msginfo);
        }
 
-       smtp_session->from = g_strdup(ac_prefs->address);
-       smtp_session->to_list = to_list;
-       smtp_session->cur_to = to_list;
-       smtp_session->send_data = get_outgoing_rfc2822_str(fp);
-       smtp_session->send_data_len = strlen(smtp_session->send_data);
-
-#if USE_OPENSSL
-       port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
-               ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
-       session->ssl_type = ac_prefs->ssl_smtp;
-       if (ac_prefs->ssl_smtp != SSL_NONE)
-               session->nonblocking = ac_prefs->use_nonblocking_ssl;
-#else
-       port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
-#endif
+       if (!ac_prefs->session) {
+               /* we can't reuse a previously initialised session */
+               session = smtp_session_new();
+               smtp_session = SMTP_SESSION(session);
 
-       dialog = send_progress_dialog_create();
-       dialog->session = session;
+               if (ac_prefs->set_domain && ac_prefs->domain && strlen(ac_prefs->domain)) {
+                       smtp_session->hostname = g_strdup(ac_prefs->domain);
+               } else {
+                       smtp_session->hostname = NULL;
+               }
 
-       text[0] = NULL;
-       text[1] = ac_prefs->smtp_server;
-       text[2] = _("Connecting");
-       clist = GTK_CLIST(dialog->dialog->clist);
-       gtk_clist_append(clist, (gchar **)text);
+               if (ac_prefs->use_smtp_auth) {
+                       smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
+                       if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
+                               smtp_session->user = g_strdup(ac_prefs->smtp_userid);
+                               if (ac_prefs->smtp_passwd)
+                                       smtp_session->pass =
+                                               g_strdup(ac_prefs->smtp_passwd);
+                               else if (ac_prefs->tmp_smtp_pass)
+                                       smtp_session->pass =
+                                               g_strdup(ac_prefs->tmp_smtp_pass);
+                               else {
+                                       smtp_session->pass =
+                                               input_dialog_query_password
+                                                       (ac_prefs->smtp_server,
+                                                        smtp_session->user);
+                                       if (!smtp_session->pass) {
+                                               session_destroy(session);
+                                               return -1;
+                                       }
+                                       ac_prefs->tmp_smtp_pass =
+                                               g_strdup(smtp_session->pass);
+                               }
+                       } else {
+                               smtp_session->user = g_strdup(ac_prefs->userid);
+                               if (ac_prefs->passwd)
+                                       smtp_session->pass = g_strdup(ac_prefs->passwd);
+                               else if (ac_prefs->tmp_pass)
+                                       smtp_session->pass =
+                                               g_strdup(ac_prefs->tmp_pass);
+                               else {
+                                       smtp_session->pass =
+                                               input_dialog_query_password
+                                                       (ac_prefs->smtp_server,
+                                                        smtp_session->user);
+                                       if (!smtp_session->pass) {
+                                               session_destroy(session);
+                                               return -1;
+                                       }
+                                       ac_prefs->tmp_pass =
+                                               g_strdup(smtp_session->pass);
+                               }
+                       }
+               } else {
+                       smtp_session->user = NULL;
+                       smtp_session->pass = NULL;
+               }
 
-       if (ac_prefs->pop_before_smtp
-           && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
-           && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
-               g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
-               log_message(buf);
+       #if USE_OPENSSL
+               port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
+                       ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
+               session->ssl_type = ac_prefs->ssl_smtp;
+               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."),
+                                 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
+                                 NULL, FALSE, NULL, ALERT_WARNING,
+                                 G_ALERTDEFAULT) != G_ALERTALTERNATE) {
+                               session_destroy(session);
+                               return -1;
+                       }
+               }
+               port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
+       #endif
+
+               dialog = send_progress_dialog_create();
+               dialog->session = session;
+               smtp_session->dialog = dialog;
+
+               progress_dialog_list_set(dialog->dialog, 0, NULL, 
+                                        ac_prefs->smtp_server, 
+                                        _("Connecting"));
+
+               if (ac_prefs->pop_before_smtp
+                   && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
+                   && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
+                       g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
+                       log_message(buf);
+                       progress_dialog_set_label(dialog->dialog, buf);
+                       progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
+                       GTK_EVENTS_FLUSH();
+                       inc_pop_before_smtp(ac_prefs);
+               }
+
+               g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
+                          ac_prefs->smtp_server);
                progress_dialog_set_label(dialog->dialog, buf);
-               gtk_clist_set_text(clist, 0, 2, _("POP before SMTP"));
-               GTK_EVENTS_FLUSH();
-               inc_pop_before_smtp(ac_prefs);
+               log_message("%s\n", buf);
+
+               session_set_recv_message_notify(session, send_recv_message, dialog);
+               session_set_send_data_progressive_notify
+                       (session, send_send_data_progressive, dialog);
+               session_set_send_data_notify(session, send_send_data_finished, dialog);
+
+       } else {
+               /* everything is ready to start at MAIL FROM:, just
+                * reinit useful variables. 
+                */
+               session = SESSION(ac_prefs->session);
+               ac_prefs->session = NULL;
+               smtp_session = SMTP_SESSION(session);
+               smtp_session->state = SMTP_HELO;
+               dialog = (SendProgressDialog *)smtp_session->dialog;
+               was_inited = TRUE;
        }
-       
-       g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
-                  ac_prefs->smtp_server);
-       progress_dialog_set_label(dialog->dialog, buf);
-       log_message("%s\n", buf);
 
-       session_set_recv_message_notify(session, send_recv_message, dialog);
-       session_set_send_data_progressive_notify
-               (session, send_send_data_progressive, dialog);
-       session_set_send_data_notify(session, send_send_data_finished, dialog);
+       /* This has to be initialised for every mail sent */
+       smtp_session->from = g_strdup(spec_from);
+       smtp_session->to_list = to_list;
+       smtp_session->cur_to = to_list;
+       smtp_session->send_data = (guchar *)get_outgoing_rfc2822_str(fp);
+       smtp_session->send_data_len = strlen((gchar *)smtp_session->send_data);
 
-       if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
+       session_set_timeout(session,
+                           prefs_common.io_timeout_secs * 1000);
+       /* connect if necessary */
+       if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
                session_destroy(session);
                send_progress_dialog_destroy(dialog);
+               ac_prefs->session = NULL;
                return -1;
        }
 
        debug_print("send_message_smtp(): begin event loop\n");
 
-       while (session_is_connected(session) && dialog->cancelled == FALSE)
+       if (was_inited) {
+               /* as the server is quiet, start sending ourselves */
+               smtp_from(smtp_session);
+       }
+
+       while (session_is_connected(session) && dialog->cancelled == FALSE
+               && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
                gtk_main_iteration();
 
        if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
                if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
                        g_free(ac_prefs->tmp_smtp_pass);
                        ac_prefs->tmp_smtp_pass = NULL;
+               } else if (ac_prefs->tmp_pass) {
+                       g_free(ac_prefs->tmp_pass);
+                       ac_prefs->tmp_pass = NULL;
                }
                ret = -1;
+       } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
+               log_message("%s\n", _("Mail sent successfully."));
+               ret = 0;
+       } else if (session->state == SESSION_EOF &&
+                  SMTP_SESSION(session)->state == SMTP_QUIT) {
+               /* consider EOF right after QUIT successful */
+               log_warning("%s\n", _("Connection closed by the remote host."));
+               ret = 0;
        } else if (session->state == SESSION_ERROR ||
                   session->state == SESSION_EOF ||
+                  session->state == SESSION_TIMEOUT ||
                   SMTP_SESSION(session)->state == SMTP_ERROR ||
                   SMTP_SESSION(session)->error_val != SM_OK)
                ret = -1;
@@ -426,14 +414,37 @@ gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
                manage_window_focus_out(dialog->dialog->window, NULL, NULL);
        }
 
-       session_destroy(session);
-       send_progress_dialog_destroy(dialog);
+       /* if we should close the connection, let's do it.
+        * Close it in case of error, too, as it helps reinitializing things
+        * easier.
+        */
+       if (!keep_session || ret != 0) {
+               if (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;
+               send_progress_dialog_destroy(dialog);
+       } else {
+               g_free(smtp_session->from);
+               g_free(smtp_session->send_data);
+               g_free(smtp_session->error_msg);
+       }
+       if (keep_session && ret == 0 && ac_prefs->session == NULL)
+               ac_prefs->session = SMTP_SESSION(session);
+
 
        statusbar_pop_all();
        statusbar_verbosity_set(FALSE);
        return ret;
 }
 
+gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
+{
+       return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
+}
+
 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
 {
        gchar buf[BUFFSIZE];
@@ -486,7 +497,7 @@ static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
        }
 
        progress_dialog_set_label(dialog->dialog, buf);
-       gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
+       progress_dialog_list_set_status(dialog->dialog, 0, state_str);
 
        return 0;
 }
@@ -506,8 +517,8 @@ static gint send_send_data_progressive(Session *session, guint cur_len,
        g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
                   cur_len, total_len);
        progress_dialog_set_label(dialog->dialog, buf);
-       progress_dialog_set_percentage
-               (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
+       progress_dialog_set_fraction
+               (dialog->dialog, (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
 
        return 0;
 }
@@ -522,10 +533,20 @@ static gint send_send_data_finished(Session *session, guint len, gpointer data)
        return 0;
 }
 
+static void send_progress_dialog_size_allocate_cb(GtkWidget *widget,
+                                        GtkAllocation *allocation)
+{
+       g_return_if_fail(allocation != NULL);
+
+       prefs_common.sendwin_width = allocation->width;
+       prefs_common.sendwin_height = allocation->height;
+}
+
 static SendProgressDialog *send_progress_dialog_create(void)
 {
        SendProgressDialog *dialog;
        ProgressDialog *progress;
+       static GdkGeometry geometry;
 
        dialog = g_new0(SendProgressDialog, 1);
 
@@ -537,9 +558,21 @@ static SendProgressDialog *send_progress_dialog_create(void)
        g_signal_connect(G_OBJECT(progress->window), "delete_event",
                         G_CALLBACK(gtk_true), NULL);
        gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
+       g_signal_connect(G_OBJECT(progress->window), "size_allocate",
+                        G_CALLBACK(send_progress_dialog_size_allocate_cb), NULL);
        manage_window_set_transient(GTK_WINDOW(progress->window));
 
-       progress_dialog_set_value(progress, 0.0);
+       progress_dialog_get_fraction(progress);
+
+       if (!geometry.min_height) {
+               geometry.min_width = 460;
+               geometry.min_height = 250;
+       }
+
+       gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
+                                     GDK_HINT_MIN_SIZE);
+       gtk_widget_set_size_request(progress->window, prefs_common.sendwin_width,
+                                   prefs_common.sendwin_height);
 
        if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
                gtk_widget_show_now(progress->window);
@@ -594,13 +627,22 @@ static void send_put_error(Session *session)
                        err_msg = g_strdup(log_msg);
                break;
        default:
-               if (session->state == SESSION_ERROR) {
+               switch (session->state) {
+               case SESSION_ERROR:
                        log_msg =
                                _("Error occurred while sending the message.");
                        err_msg = g_strdup(log_msg);
-               } else if (session->state == SESSION_EOF) {
+                       break;
+               case SESSION_EOF:
                        log_msg = _("Connection closed by the remote host.");
                        err_msg = g_strdup(log_msg);
+                       break;
+               case SESSION_TIMEOUT:
+                       log_msg = _("Session timed out.");
+                       err_msg = g_strdup(log_msg);
+                       break;
+               default:
+                       break;
                }
                break;
        }
@@ -608,7 +650,7 @@ static void send_put_error(Session *session)
        if (log_msg)
                log_warning("%s\n", log_msg);
        if (err_msg) {
-               alertpanel_error_log("%s", err_msg);
+               log_error("%s", err_msg);
                g_free(err_msg);
        }
 }