2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkclist.h>
36 #include "send_message.h"
40 #include "prefs_common.h"
41 #include "prefs_account.h"
42 #include "procheader.h"
44 #include "progressdialog.h"
45 #include "statusbar.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
48 #include "manage_window.h"
55 typedef struct _SendProgressDialog SendProgressDialog;
57 struct _SendProgressDialog
59 ProgressDialog *dialog;
64 static gint send_recv_message (Session *session,
67 static gint send_send_data_progressive (Session *session,
71 static gint send_send_data_finished (Session *session,
75 static SendProgressDialog *send_progress_dialog_create(void);
76 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
78 static void send_cancel_button_cb (GtkWidget *widget,
81 static void send_put_error (Session *session);
84 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
89 g_return_val_if_fail(file != NULL, -1);
90 g_return_val_if_fail(ac_prefs != NULL, -1);
91 g_return_val_if_fail(to_list != NULL, -1);
93 if ((fp = fopen(file, "rb")) == NULL) {
94 FILE_OP_ERROR(file, "fopen");
98 if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
99 (*ac_prefs->mail_command)) {
100 val = send_message_local(ac_prefs->mail_command, fp);
104 val = send_message_smtp(ac_prefs, to_list, fp);
119 gint send_message_local(const gchar *command, FILE *fp)
125 gboolean err = FALSE;
127 g_return_val_if_fail(command != NULL, -1);
128 g_return_val_if_fail(fp != NULL, -1);
130 log_message(_("Sending message using command: %s\n"), command);
132 argv = strsplit_with_quote(command, " ", 0);
134 if (g_spawn_async_with_pipes(NULL, argv, NULL, 0, NULL, NULL, &pid,
135 &child_stdin, NULL, NULL, NULL) == FALSE) {
136 g_snprintf(buf, sizeof(buf),
137 _("Can't execute command: %s"), command);
138 log_warning("%s\n", buf);
139 alertpanel_error("%s", buf);
145 while (fgets(buf, sizeof(buf), fp) != NULL) {
147 if (buf[0] == '.' && buf[1] == '\0') {
148 if (fd_write_all(child_stdin, ".", 1) < 0) {
153 if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
154 fd_write_all(child_stdin, "\n", 1) < 0) {
160 fd_close(child_stdin);
161 g_spawn_close_pid(pid);
164 g_snprintf(buf, sizeof(buf),
165 _("Error occurred while executing command: %s"),
167 log_warning("%s\n", buf);
168 alertpanel_error("%s", buf);
175 gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
178 SMTPSession *smtp_session;
180 SendProgressDialog *dialog;
183 gboolean was_inited = FALSE;
185 g_return_val_if_fail(ac_prefs != NULL, -1);
186 g_return_val_if_fail(ac_prefs->address != NULL, -1);
187 g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
188 g_return_val_if_fail(to_list != NULL, -1);
189 g_return_val_if_fail(fp != NULL, -1);
191 if (!ac_prefs->session) {
192 /* we can't reuse a previously initialised session */
193 session = smtp_session_new();
194 smtp_session = SMTP_SESSION(session);
196 smtp_session->hostname =
197 ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
199 if (ac_prefs->use_smtp_auth) {
200 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
201 if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
202 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
203 if (ac_prefs->smtp_passwd)
205 g_strdup(ac_prefs->smtp_passwd);
206 else if (ac_prefs->tmp_smtp_pass)
208 g_strdup(ac_prefs->tmp_smtp_pass);
211 input_dialog_query_password
212 (ac_prefs->smtp_server,
214 if (!smtp_session->pass)
215 smtp_session->pass = g_strdup("");
216 ac_prefs->tmp_smtp_pass =
217 g_strdup(smtp_session->pass);
220 smtp_session->user = g_strdup(ac_prefs->userid);
221 if (ac_prefs->passwd)
222 smtp_session->pass = g_strdup(ac_prefs->passwd);
223 else if (ac_prefs->tmp_pass)
225 g_strdup(ac_prefs->tmp_pass);
228 input_dialog_query_password
229 (ac_prefs->smtp_server,
231 if (!smtp_session->pass)
232 smtp_session->pass = g_strdup("");
234 g_strdup(smtp_session->pass);
238 smtp_session->user = NULL;
239 smtp_session->pass = NULL;
243 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
244 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
245 session->ssl_type = ac_prefs->ssl_smtp;
246 if (ac_prefs->ssl_smtp != SSL_NONE)
247 session->nonblocking = ac_prefs->use_nonblocking_ssl;
249 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
252 dialog = send_progress_dialog_create();
253 dialog->session = session;
254 smtp_session->dialog = dialog;
256 progress_dialog_list_set(dialog->dialog, 0, NULL,
257 ac_prefs->smtp_server,
260 if (ac_prefs->pop_before_smtp
261 && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
262 && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
263 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
265 progress_dialog_set_label(dialog->dialog, buf);
266 progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
268 inc_pop_before_smtp(ac_prefs);
271 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
272 ac_prefs->smtp_server);
273 progress_dialog_set_label(dialog->dialog, buf);
274 log_message("%s\n", buf);
276 session_set_recv_message_notify(session, send_recv_message, dialog);
277 session_set_send_data_progressive_notify
278 (session, send_send_data_progressive, dialog);
279 session_set_send_data_notify(session, send_send_data_finished, dialog);
281 ac_prefs->session = SMTP_SESSION(session);
283 /* everything is ready to start at MAIL FROM:, just
284 * reinit useful variables.
286 session = SESSION(ac_prefs->session);
287 smtp_session = SMTP_SESSION(session);
288 smtp_session->state = SMTP_HELO;
289 dialog = (SendProgressDialog *)smtp_session->dialog;
293 /* This has to be initialised for every mail sent */
294 smtp_session->from = g_strdup(ac_prefs->address);
295 smtp_session->to_list = to_list;
296 smtp_session->cur_to = to_list;
297 smtp_session->send_data = get_outgoing_rfc2822_str(fp);
298 smtp_session->send_data_len = strlen(smtp_session->send_data);
300 /* connect if necessary */
301 if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
302 session_destroy(session);
303 send_progress_dialog_destroy(dialog);
307 debug_print("send_message_smtp(): begin event loop\n");
310 /* as the server is quiet, start sending ourselves */
311 smtp_from(smtp_session);
314 while (session_is_connected(session) && dialog->cancelled == FALSE
315 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
316 gtk_main_iteration();
318 if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
319 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
320 g_free(ac_prefs->tmp_smtp_pass);
321 ac_prefs->tmp_smtp_pass = NULL;
324 } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
325 log_message("%s\n", _("Mail sent successfully."));
327 } else if (session->state == SESSION_EOF &&
328 SMTP_SESSION(session)->state == SMTP_QUIT) {
329 /* consider EOF right after QUIT successful */
330 log_warning("%s\n", _("Connection closed by the remote host."));
332 } else if (session->state == SESSION_ERROR ||
333 session->state == SESSION_EOF ||
334 session->state == SESSION_TIMEOUT ||
335 SMTP_SESSION(session)->state == SMTP_ERROR ||
336 SMTP_SESSION(session)->error_val != SM_OK)
338 else if (dialog->cancelled == TRUE)
342 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
343 send_put_error(session);
344 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
347 /* if we should close the connection, let's do it.
348 * Close it in case of error, too, as it helps reinitializing things
351 if (!keep_session || ret != 0) {
352 if (session_is_connected(session))
354 while (session_is_connected(session))
355 gtk_main_iteration();
356 session_destroy(session);
357 ac_prefs->session = NULL;
358 send_progress_dialog_destroy(dialog);
360 g_free(smtp_session->from);
361 g_free(smtp_session->send_data);
362 g_free(smtp_session->error_msg);
366 statusbar_verbosity_set(FALSE);
370 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
372 return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
375 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
378 SMTPSession *smtp_session = SMTP_SESSION(session);
379 SendProgressDialog *dialog = (SendProgressDialog *)data;
380 gchar *state_str = NULL;
382 g_return_val_if_fail(dialog != NULL, -1);
384 switch (smtp_session->state) {
389 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
390 state_str = _("Authenticating");
391 statusbar_print_all(_("Sending message..."));
394 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
395 state_str = _("Authenticating");
396 statusbar_print_all(_("Sending message..."));
399 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
400 state_str = _("Authenticating");
403 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
404 state_str = _("Sending");
407 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
408 state_str = _("Sending");
412 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
413 state_str = _("Sending");
416 g_snprintf(buf, sizeof(buf), _("Quitting..."));
417 state_str = _("Quitting");
420 g_warning("send: error: %s\n", msg);
426 progress_dialog_set_label(dialog->dialog, buf);
427 progress_dialog_list_set_status(dialog->dialog, 0, state_str);
432 static gint send_send_data_progressive(Session *session, guint cur_len,
433 guint total_len, gpointer data)
436 SendProgressDialog *dialog = (SendProgressDialog *)data;
438 g_return_val_if_fail(dialog != NULL, -1);
440 if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
441 SMTP_SESSION(session)->state != SMTP_EOM)
444 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
446 progress_dialog_set_label(dialog->dialog, buf);
447 progress_dialog_set_fraction
448 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
453 static gint send_send_data_finished(Session *session, guint len, gpointer data)
455 SendProgressDialog *dialog = (SendProgressDialog *)data;
457 g_return_val_if_fail(dialog != NULL, -1);
459 send_send_data_progressive(session, len, len, dialog);
463 static SendProgressDialog *send_progress_dialog_create(void)
465 SendProgressDialog *dialog;
466 ProgressDialog *progress;
468 dialog = g_new0(SendProgressDialog, 1);
470 progress = progress_dialog_create();
471 gtk_window_set_title(GTK_WINDOW(progress->window),
472 _("Sending message"));
473 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
474 G_CALLBACK(send_cancel_button_cb), dialog);
475 g_signal_connect(G_OBJECT(progress->window), "delete_event",
476 G_CALLBACK(gtk_true), NULL);
477 gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
478 manage_window_set_transient(GTK_WINDOW(progress->window));
480 progress_dialog_get_fraction(progress);
482 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
483 gtk_widget_show_now(progress->window);
486 dialog->dialog = progress;
491 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
493 g_return_if_fail(dialog != NULL);
494 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
495 progress_dialog_destroy(dialog->dialog);
500 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
502 SendProgressDialog *dialog = (SendProgressDialog *)data;
504 dialog->cancelled = TRUE;
507 static void send_put_error(Session *session)
510 gchar *log_msg = NULL;
511 gchar *err_msg = NULL;
513 msg = SMTP_SESSION(session)->error_msg;
515 switch (SMTP_SESSION(session)->error_val) {
517 case SM_UNRECOVERABLE:
518 log_msg = _("Error occurred while sending the message.");
520 err_msg = g_strdup_printf
521 (_("Error occurred while sending the message:\n%s"),
524 err_msg = g_strdup(log_msg);
527 log_msg = _("Authentication failed.");
529 err_msg = g_strdup_printf
530 (_("Authentication failed:\n%s"), msg);
532 err_msg = g_strdup(log_msg);
535 switch (session->state) {
538 _("Error occurred while sending the message.");
539 err_msg = g_strdup(log_msg);
542 log_msg = _("Connection closed by the remote host.");
543 err_msg = g_strdup(log_msg);
545 case SESSION_TIMEOUT:
546 log_msg = _("Session timed out.");
547 err_msg = g_strdup(log_msg);
556 log_warning("%s\n", log_msg);
558 alertpanel_error_log("%s", err_msg);