2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkwindow.h>
34 #include <sys/types.h>
39 #include "send_message.h"
43 #include "prefs_common.h"
44 #include "prefs_account.h"
45 #include "procheader.h"
47 #include "progressdialog.h"
48 #include "statusbar.h"
49 #include "inputdialog.h"
50 #include "alertpanel.h"
51 #include "manage_window.h"
58 typedef struct _SendProgressDialog SendProgressDialog;
60 struct _SendProgressDialog
62 ProgressDialog *dialog;
67 static gint send_recv_message (Session *session,
70 static gint send_send_data_progressive (Session *session,
74 static gint send_send_data_finished (Session *session,
78 static SendProgressDialog *send_progress_dialog_create(void);
79 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
81 static void send_cancel_button_cb (GtkWidget *widget,
84 static void send_put_error (Session *session);
87 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
92 g_return_val_if_fail(file != NULL, -1);
93 g_return_val_if_fail(ac_prefs != NULL, -1);
94 g_return_val_if_fail(to_list != NULL, -1);
96 if ((fp = g_fopen(file, "rb")) == NULL) {
97 FILE_OP_ERROR(file, "fopen");
102 if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
103 (*ac_prefs->mail_command)) {
104 val = send_message_local(ac_prefs->mail_command, fp);
109 val = send_message_smtp(ac_prefs, to_list, fp);
125 gint send_message_local(const gchar *command, FILE *fp)
131 gboolean err = FALSE;
134 g_return_val_if_fail(command != NULL, -1);
135 g_return_val_if_fail(fp != NULL, -1);
137 log_message(_("Sending message using command: %s\n"), command);
139 argv = strsplit_with_quote(command, " ", 0);
141 if (g_spawn_async_with_pipes(NULL, argv, NULL,
145 G_SPAWN_DO_NOT_REAP_CHILD,
148 &pid, &child_stdin, NULL, NULL,
150 g_snprintf(buf, sizeof(buf),
151 _("Can't execute command: %s"), command);
152 log_warning("%s\n", buf);
153 alertpanel_error("%s", buf);
159 while (fgets(buf, sizeof(buf), fp) != NULL) {
161 if (buf[0] == '.' && buf[1] == '\0') {
162 if (fd_write_all(child_stdin, ".", 1) < 0) {
167 if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
168 fd_write_all(child_stdin, "\n", 1) < 0) {
174 fd_close(child_stdin);
177 waitpid(pid, &status, 0);
178 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
182 g_spawn_close_pid(pid);
185 g_snprintf(buf, sizeof(buf),
186 _("Error occurred while executing command: %s"),
188 log_warning("%s\n", buf);
189 alertpanel_error("%s", buf);
196 gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
199 SMTPSession *smtp_session;
201 SendProgressDialog *dialog;
204 gboolean was_inited = FALSE;
206 g_return_val_if_fail(ac_prefs != NULL, -1);
207 g_return_val_if_fail(ac_prefs->address != NULL, -1);
208 g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
209 g_return_val_if_fail(to_list != NULL, -1);
210 g_return_val_if_fail(fp != NULL, -1);
212 if (!ac_prefs->session) {
213 /* we can't reuse a previously initialised session */
214 session = smtp_session_new();
215 smtp_session = SMTP_SESSION(session);
217 if (ac_prefs->set_domain && ac_prefs->domain && strlen(ac_prefs->domain)) {
218 smtp_session->hostname = g_strdup(ac_prefs->domain);
220 smtp_session->hostname = NULL;
223 if (ac_prefs->use_smtp_auth) {
224 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
225 if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
226 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
227 if (ac_prefs->smtp_passwd)
229 g_strdup(ac_prefs->smtp_passwd);
230 else if (ac_prefs->tmp_smtp_pass)
232 g_strdup(ac_prefs->tmp_smtp_pass);
235 input_dialog_query_password
236 (ac_prefs->smtp_server,
238 if (!smtp_session->pass) {
239 session_destroy(session);
242 ac_prefs->tmp_smtp_pass =
243 g_strdup(smtp_session->pass);
246 smtp_session->user = g_strdup(ac_prefs->userid);
247 if (ac_prefs->passwd)
248 smtp_session->pass = g_strdup(ac_prefs->passwd);
249 else if (ac_prefs->tmp_pass)
251 g_strdup(ac_prefs->tmp_pass);
254 input_dialog_query_password
255 (ac_prefs->smtp_server,
257 if (!smtp_session->pass) {
258 session_destroy(session);
262 g_strdup(smtp_session->pass);
266 smtp_session->user = NULL;
267 smtp_session->pass = NULL;
271 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
272 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
273 session->ssl_type = ac_prefs->ssl_smtp;
274 if (ac_prefs->ssl_smtp != SSL_NONE)
275 session->nonblocking = ac_prefs->use_nonblocking_ssl;
277 if (ac_prefs->ssl_smtp != SSL_NONE) {
278 if (alertpanel_full(_("Insecure connection"),
279 _("This connection is configured to be secured "
280 "using SSL, but SSL is not available in this "
281 "build of Sylpheed-Claws. \n\n"
282 "Do you want to continue connecting to this "
283 "server? The communication would not be "
285 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
286 NULL, FALSE, NULL, ALERT_WARNING,
287 G_ALERTDEFAULT) != G_ALERTALTERNATE) {
288 session_destroy(session);
292 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
295 dialog = send_progress_dialog_create();
296 dialog->session = session;
297 smtp_session->dialog = dialog;
299 progress_dialog_list_set(dialog->dialog, 0, NULL,
300 ac_prefs->smtp_server,
303 if (ac_prefs->pop_before_smtp
304 && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
305 && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
306 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
308 progress_dialog_set_label(dialog->dialog, buf);
309 progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
311 inc_pop_before_smtp(ac_prefs);
314 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
315 ac_prefs->smtp_server);
316 progress_dialog_set_label(dialog->dialog, buf);
317 log_message("%s\n", buf);
319 session_set_recv_message_notify(session, send_recv_message, dialog);
320 session_set_send_data_progressive_notify
321 (session, send_send_data_progressive, dialog);
322 session_set_send_data_notify(session, send_send_data_finished, dialog);
325 /* everything is ready to start at MAIL FROM:, just
326 * reinit useful variables.
328 session = SESSION(ac_prefs->session);
329 ac_prefs->session = NULL;
330 smtp_session = SMTP_SESSION(session);
331 smtp_session->state = SMTP_HELO;
332 dialog = (SendProgressDialog *)smtp_session->dialog;
336 /* This has to be initialised for every mail sent */
337 smtp_session->from = g_strdup(ac_prefs->address);
338 smtp_session->to_list = to_list;
339 smtp_session->cur_to = to_list;
340 smtp_session->send_data = get_outgoing_rfc2822_str(fp);
341 smtp_session->send_data_len = strlen(smtp_session->send_data);
343 session_set_timeout(session,
344 prefs_common.io_timeout_secs * 1000);
345 /* connect if necessary */
346 if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
347 session_destroy(session);
348 send_progress_dialog_destroy(dialog);
349 ac_prefs->session = NULL;
353 debug_print("send_message_smtp(): begin event loop\n");
356 /* as the server is quiet, start sending ourselves */
357 smtp_from(smtp_session);
360 while (session_is_connected(session) && dialog->cancelled == FALSE
361 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
362 gtk_main_iteration();
364 if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
365 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
366 g_free(ac_prefs->tmp_smtp_pass);
367 ac_prefs->tmp_smtp_pass = NULL;
368 } else if (ac_prefs->tmp_pass) {
369 g_free(ac_prefs->tmp_pass);
370 ac_prefs->tmp_pass = NULL;
373 } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
374 log_message("%s\n", _("Mail sent successfully."));
376 } else if (session->state == SESSION_EOF &&
377 SMTP_SESSION(session)->state == SMTP_QUIT) {
378 /* consider EOF right after QUIT successful */
379 log_warning("%s\n", _("Connection closed by the remote host."));
381 } else if (session->state == SESSION_ERROR ||
382 session->state == SESSION_EOF ||
383 session->state == SESSION_TIMEOUT ||
384 SMTP_SESSION(session)->state == SMTP_ERROR ||
385 SMTP_SESSION(session)->error_val != SM_OK)
387 else if (dialog->cancelled == TRUE)
391 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
392 send_put_error(session);
393 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
396 /* if we should close the connection, let's do it.
397 * Close it in case of error, too, as it helps reinitializing things
400 if (!keep_session || ret != 0) {
401 if (session_is_connected(session))
402 smtp_quit(smtp_session);
403 while (session_is_connected(session) && !dialog->cancelled)
404 gtk_main_iteration();
405 session_destroy(session);
406 ac_prefs->session = NULL;
407 send_progress_dialog_destroy(dialog);
409 g_free(smtp_session->from);
410 g_free(smtp_session->send_data);
411 g_free(smtp_session->error_msg);
413 if (keep_session && ret == 0 && ac_prefs->session == NULL)
414 ac_prefs->session = SMTP_SESSION(session);
418 statusbar_verbosity_set(FALSE);
422 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
424 return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
427 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
430 SMTPSession *smtp_session = SMTP_SESSION(session);
431 SendProgressDialog *dialog = (SendProgressDialog *)data;
432 gchar *state_str = NULL;
434 g_return_val_if_fail(dialog != NULL, -1);
436 switch (smtp_session->state) {
441 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
442 state_str = _("Authenticating");
443 statusbar_print_all(_("Sending message..."));
446 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
447 state_str = _("Authenticating");
448 statusbar_print_all(_("Sending message..."));
451 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
452 state_str = _("Authenticating");
455 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
456 state_str = _("Sending");
459 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
460 state_str = _("Sending");
464 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
465 state_str = _("Sending");
468 g_snprintf(buf, sizeof(buf), _("Quitting..."));
469 state_str = _("Quitting");
472 g_warning("send: error: %s\n", msg);
478 progress_dialog_set_label(dialog->dialog, buf);
479 progress_dialog_list_set_status(dialog->dialog, 0, state_str);
484 static gint send_send_data_progressive(Session *session, guint cur_len,
485 guint total_len, gpointer data)
488 SendProgressDialog *dialog = (SendProgressDialog *)data;
490 g_return_val_if_fail(dialog != NULL, -1);
492 if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
493 SMTP_SESSION(session)->state != SMTP_EOM)
496 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
498 progress_dialog_set_label(dialog->dialog, buf);
499 progress_dialog_set_fraction
500 (dialog->dialog, (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
505 static gint send_send_data_finished(Session *session, guint len, gpointer data)
507 SendProgressDialog *dialog = (SendProgressDialog *)data;
509 g_return_val_if_fail(dialog != NULL, -1);
511 send_send_data_progressive(session, len, len, dialog);
515 static SendProgressDialog *send_progress_dialog_create(void)
517 SendProgressDialog *dialog;
518 ProgressDialog *progress;
520 dialog = g_new0(SendProgressDialog, 1);
522 progress = progress_dialog_create();
523 gtk_window_set_title(GTK_WINDOW(progress->window),
524 _("Sending message"));
525 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
526 G_CALLBACK(send_cancel_button_cb), dialog);
527 g_signal_connect(G_OBJECT(progress->window), "delete_event",
528 G_CALLBACK(gtk_true), NULL);
529 gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
530 manage_window_set_transient(GTK_WINDOW(progress->window));
532 progress_dialog_get_fraction(progress);
534 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
535 gtk_widget_show_now(progress->window);
538 dialog->dialog = progress;
543 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
545 g_return_if_fail(dialog != NULL);
546 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
547 progress_dialog_destroy(dialog->dialog);
552 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
554 SendProgressDialog *dialog = (SendProgressDialog *)data;
556 dialog->cancelled = TRUE;
559 static void send_put_error(Session *session)
562 gchar *log_msg = NULL;
563 gchar *err_msg = NULL;
565 msg = SMTP_SESSION(session)->error_msg;
567 switch (SMTP_SESSION(session)->error_val) {
569 case SM_UNRECOVERABLE:
570 log_msg = _("Error occurred while sending the message.");
572 err_msg = g_strdup_printf
573 (_("Error occurred while sending the message:\n%s"),
576 err_msg = g_strdup(log_msg);
579 log_msg = _("Authentication failed.");
581 err_msg = g_strdup_printf
582 (_("Authentication failed:\n%s"), msg);
584 err_msg = g_strdup(log_msg);
587 switch (session->state) {
590 _("Error occurred while sending the message.");
591 err_msg = g_strdup(log_msg);
594 log_msg = _("Connection closed by the remote host.");
595 err_msg = g_strdup(log_msg);
597 case SESSION_TIMEOUT:
598 log_msg = _("Session timed out.");
599 err_msg = g_strdup(log_msg);
608 log_warning("%s\n", log_msg);
610 alertpanel_error_log("%s", err_msg);