2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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., 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>
37 #include "send_message.h"
41 #include "prefs_common.h"
42 #include "prefs_account.h"
43 #include "procheader.h"
45 #include "progressdialog.h"
46 #include "statusbar.h"
47 #include "inputdialog.h"
48 #include "alertpanel.h"
49 #include "manage_window.h"
56 typedef struct _SendProgressDialog SendProgressDialog;
58 struct _SendProgressDialog
60 ProgressDialog *dialog;
65 static gint send_recv_message (Session *session,
68 static gint send_send_data_progressive (Session *session,
72 static gint send_send_data_finished (Session *session,
76 static SendProgressDialog *send_progress_dialog_create(void);
77 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
79 static void send_cancel_button_cb (GtkWidget *widget,
82 static void send_put_error (Session *session);
85 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
90 g_return_val_if_fail(file != NULL, -1);
91 g_return_val_if_fail(ac_prefs != NULL, -1);
92 g_return_val_if_fail(to_list != NULL, -1);
94 if ((fp = g_fopen(file, "rb")) == NULL) {
95 FILE_OP_ERROR(file, "fopen");
99 if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
100 (*ac_prefs->mail_command)) {
101 val = send_message_local(ac_prefs->mail_command, fp);
105 val = send_message_smtp(ac_prefs, to_list, fp);
120 gint send_message_local(const gchar *command, FILE *fp)
126 gboolean err = FALSE;
129 g_return_val_if_fail(command != NULL, -1);
130 g_return_val_if_fail(fp != NULL, -1);
132 log_message(_("Sending message using command: %s\n"), command);
134 argv = strsplit_with_quote(command, " ", 0);
136 if (g_spawn_async_with_pipes(NULL, argv, NULL,
137 G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
138 &pid, &child_stdin, NULL, NULL,
140 g_snprintf(buf, sizeof(buf),
141 _("Can't execute command: %s"), command);
142 log_warning("%s\n", buf);
143 alertpanel_error("%s", buf);
149 while (fgets(buf, sizeof(buf), fp) != NULL) {
151 if (buf[0] == '.' && buf[1] == '\0') {
152 if (fd_write_all(child_stdin, ".", 1) < 0) {
157 if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
158 fd_write_all(child_stdin, "\n", 1) < 0) {
164 fd_close(child_stdin);
166 waitpid(pid, &status, 0);
167 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
170 g_spawn_close_pid(pid);
173 g_snprintf(buf, sizeof(buf),
174 _("Error occurred while executing command: %s"),
176 log_warning("%s\n", buf);
177 alertpanel_error("%s", buf);
184 gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
187 SMTPSession *smtp_session;
189 SendProgressDialog *dialog;
192 gboolean was_inited = FALSE;
194 g_return_val_if_fail(ac_prefs != NULL, -1);
195 g_return_val_if_fail(ac_prefs->address != NULL, -1);
196 g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
197 g_return_val_if_fail(to_list != NULL, -1);
198 g_return_val_if_fail(fp != NULL, -1);
200 if (!ac_prefs->session) {
201 /* we can't reuse a previously initialised session */
202 session = smtp_session_new();
203 smtp_session = SMTP_SESSION(session);
205 smtp_session->hostname =
206 ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
208 if (ac_prefs->use_smtp_auth) {
209 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
210 if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
211 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
212 if (ac_prefs->smtp_passwd)
214 g_strdup(ac_prefs->smtp_passwd);
215 else if (ac_prefs->tmp_smtp_pass)
217 g_strdup(ac_prefs->tmp_smtp_pass);
220 input_dialog_query_password
221 (ac_prefs->smtp_server,
223 if (!smtp_session->pass)
224 smtp_session->pass = g_strdup("");
225 ac_prefs->tmp_smtp_pass =
226 g_strdup(smtp_session->pass);
229 smtp_session->user = g_strdup(ac_prefs->userid);
230 if (ac_prefs->passwd)
231 smtp_session->pass = g_strdup(ac_prefs->passwd);
232 else if (ac_prefs->tmp_pass)
234 g_strdup(ac_prefs->tmp_pass);
237 input_dialog_query_password
238 (ac_prefs->smtp_server,
240 if (!smtp_session->pass)
241 smtp_session->pass = g_strdup("");
243 g_strdup(smtp_session->pass);
247 smtp_session->user = NULL;
248 smtp_session->pass = NULL;
252 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
253 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
254 session->ssl_type = ac_prefs->ssl_smtp;
255 if (ac_prefs->ssl_smtp != SSL_NONE)
256 session->nonblocking = ac_prefs->use_nonblocking_ssl;
258 if (ac_prefs->ssl_smtp != SSL_NONE) {
259 if (alertpanel_full(_("Insecure connection"),
260 _("This connection is configured to be secured "
261 "using SSL, but SSL is not available in this "
262 "build of Sylpheed-Claws. \n\n"
263 "Do you want to continue connecting to this "
264 "server? The communication would not be "
266 _("Con_tinue connecting"),
267 GTK_STOCK_CANCEL, NULL,
268 FALSE, NULL, ALERT_WARNING,
269 G_ALERTALTERNATE) != G_ALERTDEFAULT)
272 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
275 dialog = send_progress_dialog_create();
276 dialog->session = session;
277 smtp_session->dialog = dialog;
279 progress_dialog_list_set(dialog->dialog, 0, NULL,
280 ac_prefs->smtp_server,
283 if (ac_prefs->pop_before_smtp
284 && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
285 && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
286 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
288 progress_dialog_set_label(dialog->dialog, buf);
289 progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
291 inc_pop_before_smtp(ac_prefs);
294 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
295 ac_prefs->smtp_server);
296 progress_dialog_set_label(dialog->dialog, buf);
297 log_message("%s\n", buf);
299 session_set_recv_message_notify(session, send_recv_message, dialog);
300 session_set_send_data_progressive_notify
301 (session, send_send_data_progressive, dialog);
302 session_set_send_data_notify(session, send_send_data_finished, dialog);
304 ac_prefs->session = SMTP_SESSION(session);
306 /* everything is ready to start at MAIL FROM:, just
307 * reinit useful variables.
309 session = SESSION(ac_prefs->session);
310 smtp_session = SMTP_SESSION(session);
311 smtp_session->state = SMTP_HELO;
312 dialog = (SendProgressDialog *)smtp_session->dialog;
316 /* This has to be initialised for every mail sent */
317 smtp_session->from = g_strdup(ac_prefs->address);
318 smtp_session->to_list = to_list;
319 smtp_session->cur_to = to_list;
320 smtp_session->send_data = get_outgoing_rfc2822_str(fp);
321 smtp_session->send_data_len = strlen(smtp_session->send_data);
323 /* connect if necessary */
324 if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
325 session_destroy(session);
326 send_progress_dialog_destroy(dialog);
330 debug_print("send_message_smtp(): begin event loop\n");
333 /* as the server is quiet, start sending ourselves */
334 smtp_from(smtp_session);
337 while (session_is_connected(session) && dialog->cancelled == FALSE
338 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
339 gtk_main_iteration();
341 if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
342 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
343 g_free(ac_prefs->tmp_smtp_pass);
344 ac_prefs->tmp_smtp_pass = NULL;
347 } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
348 log_message("%s\n", _("Mail sent successfully."));
350 } else if (session->state == SESSION_EOF &&
351 SMTP_SESSION(session)->state == SMTP_QUIT) {
352 /* consider EOF right after QUIT successful */
353 log_warning("%s\n", _("Connection closed by the remote host."));
355 } else if (session->state == SESSION_ERROR ||
356 session->state == SESSION_EOF ||
357 session->state == SESSION_TIMEOUT ||
358 SMTP_SESSION(session)->state == SMTP_ERROR ||
359 SMTP_SESSION(session)->error_val != SM_OK)
361 else if (dialog->cancelled == TRUE)
365 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
366 send_put_error(session);
367 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
370 /* if we should close the connection, let's do it.
371 * Close it in case of error, too, as it helps reinitializing things
374 if (!keep_session || ret != 0) {
375 if (session_is_connected(session))
376 smtp_quit(smtp_session);
377 while (session_is_connected(session) && !dialog->cancelled)
378 gtk_main_iteration();
379 session_destroy(session);
380 ac_prefs->session = NULL;
381 send_progress_dialog_destroy(dialog);
383 g_free(smtp_session->from);
384 g_free(smtp_session->send_data);
385 g_free(smtp_session->error_msg);
389 statusbar_verbosity_set(FALSE);
393 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
395 return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
398 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
401 SMTPSession *smtp_session = SMTP_SESSION(session);
402 SendProgressDialog *dialog = (SendProgressDialog *)data;
403 gchar *state_str = NULL;
405 g_return_val_if_fail(dialog != NULL, -1);
407 switch (smtp_session->state) {
412 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
413 state_str = _("Authenticating");
414 statusbar_print_all(_("Sending message..."));
417 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
418 state_str = _("Authenticating");
419 statusbar_print_all(_("Sending message..."));
422 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
423 state_str = _("Authenticating");
426 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
427 state_str = _("Sending");
430 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
431 state_str = _("Sending");
435 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
436 state_str = _("Sending");
439 g_snprintf(buf, sizeof(buf), _("Quitting..."));
440 state_str = _("Quitting");
443 g_warning("send: error: %s\n", msg);
449 progress_dialog_set_label(dialog->dialog, buf);
450 progress_dialog_list_set_status(dialog->dialog, 0, state_str);
455 static gint send_send_data_progressive(Session *session, guint cur_len,
456 guint total_len, gpointer data)
459 SendProgressDialog *dialog = (SendProgressDialog *)data;
461 g_return_val_if_fail(dialog != NULL, -1);
463 if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
464 SMTP_SESSION(session)->state != SMTP_EOM)
467 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
469 progress_dialog_set_label(dialog->dialog, buf);
470 progress_dialog_set_fraction
471 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
476 static gint send_send_data_finished(Session *session, guint len, gpointer data)
478 SendProgressDialog *dialog = (SendProgressDialog *)data;
480 g_return_val_if_fail(dialog != NULL, -1);
482 send_send_data_progressive(session, len, len, dialog);
486 static SendProgressDialog *send_progress_dialog_create(void)
488 SendProgressDialog *dialog;
489 ProgressDialog *progress;
491 dialog = g_new0(SendProgressDialog, 1);
493 progress = progress_dialog_create();
494 gtk_window_set_title(GTK_WINDOW(progress->window),
495 _("Sending message"));
496 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
497 G_CALLBACK(send_cancel_button_cb), dialog);
498 g_signal_connect(G_OBJECT(progress->window), "delete_event",
499 G_CALLBACK(gtk_true), NULL);
500 gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
501 manage_window_set_transient(GTK_WINDOW(progress->window));
503 progress_dialog_get_fraction(progress);
505 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
506 gtk_widget_show_now(progress->window);
509 dialog->dialog = progress;
514 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
516 g_return_if_fail(dialog != NULL);
517 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
518 progress_dialog_destroy(dialog->dialog);
523 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
525 SendProgressDialog *dialog = (SendProgressDialog *)data;
527 dialog->cancelled = TRUE;
530 static void send_put_error(Session *session)
533 gchar *log_msg = NULL;
534 gchar *err_msg = NULL;
536 msg = SMTP_SESSION(session)->error_msg;
538 switch (SMTP_SESSION(session)->error_val) {
540 case SM_UNRECOVERABLE:
541 log_msg = _("Error occurred while sending the message.");
543 err_msg = g_strdup_printf
544 (_("Error occurred while sending the message:\n%s"),
547 err_msg = g_strdup(log_msg);
550 log_msg = _("Authentication failed.");
552 err_msg = g_strdup_printf
553 (_("Authentication failed:\n%s"), msg);
555 err_msg = g_strdup(log_msg);
558 switch (session->state) {
561 _("Error occurred while sending the message.");
562 err_msg = g_strdup(log_msg);
565 log_msg = _("Connection closed by the remote host.");
566 err_msg = g_strdup(log_msg);
568 case SESSION_TIMEOUT:
569 log_msg = _("Session timed out.");
570 err_msg = g_strdup(log_msg);
579 log_warning("%s\n", log_msg);
581 alertpanel_error_log("%s", err_msg);