2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
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(LOG_PROTOCOL, _("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 _("Couldn't execute command: %s"), command);
152 log_warning(LOG_PROTOCOL, "%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(LOG_PROTOCOL, "%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;
205 MsgInfo *tmp_msginfo = NULL;
206 MsgFlags flags = {0, 0};
208 gchar spec_from[BUFFSIZE];
210 g_return_val_if_fail(ac_prefs != NULL, -1);
211 g_return_val_if_fail(ac_prefs->address != NULL, -1);
212 g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
213 g_return_val_if_fail(to_list != NULL, -1);
214 g_return_val_if_fail(fp != NULL, -1);
216 /* get the From address used, not necessarily the ac_prefs',
217 * because it's editable. */
220 tmp_msginfo = procheader_parse_stream(fp, flags, FALSE, FALSE);
221 fseek(fp, fp_pos, SEEK_SET);
223 if (tmp_msginfo && tmp_msginfo->from) {
224 strncpy2(spec_from, tmp_msginfo->from, BUFFSIZE-1);
225 extract_address(spec_from);
227 strncpy2(spec_from, ac_prefs->address, BUFFSIZE-1);
230 procmsg_msginfo_free(tmp_msginfo);
233 if (!ac_prefs->session) {
234 /* we can't reuse a previously initialised session */
235 session = smtp_session_new();
236 smtp_session = SMTP_SESSION(session);
238 if (ac_prefs->set_domain && ac_prefs->domain && strlen(ac_prefs->domain)) {
239 smtp_session->hostname = g_strdup(ac_prefs->domain);
241 smtp_session->hostname = NULL;
244 if (ac_prefs->use_smtp_auth) {
245 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
246 if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
247 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
248 if (ac_prefs->smtp_passwd)
250 g_strdup(ac_prefs->smtp_passwd);
251 else if (ac_prefs->tmp_smtp_pass)
253 g_strdup(ac_prefs->tmp_smtp_pass);
256 input_dialog_query_password
257 (ac_prefs->smtp_server,
259 if (!smtp_session->pass) {
260 session_destroy(session);
263 ac_prefs->tmp_smtp_pass =
264 g_strdup(smtp_session->pass);
267 smtp_session->user = g_strdup(ac_prefs->userid);
268 if (ac_prefs->passwd)
269 smtp_session->pass = g_strdup(ac_prefs->passwd);
270 else if (ac_prefs->tmp_pass)
272 g_strdup(ac_prefs->tmp_pass);
275 input_dialog_query_password
276 (ac_prefs->smtp_server,
278 if (!smtp_session->pass) {
279 session_destroy(session);
283 g_strdup(smtp_session->pass);
287 smtp_session->user = NULL;
288 smtp_session->pass = NULL;
292 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
293 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
294 session->ssl_type = ac_prefs->ssl_smtp;
295 if (ac_prefs->ssl_smtp != SSL_NONE)
296 session->nonblocking = ac_prefs->use_nonblocking_ssl;
298 if (ac_prefs->ssl_smtp != SSL_NONE) {
299 if (alertpanel_full(_("Insecure connection"),
300 _("This connection is configured to be secured "
301 "using SSL, but SSL is not available in this "
302 "build of Claws Mail. \n\n"
303 "Do you want to continue connecting to this "
304 "server? The communication would not be "
306 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
307 NULL, FALSE, NULL, ALERT_WARNING,
308 G_ALERTDEFAULT) != G_ALERTALTERNATE) {
309 session_destroy(session);
313 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
316 dialog = send_progress_dialog_create();
317 dialog->session = session;
318 smtp_session->dialog = dialog;
320 progress_dialog_list_set(dialog->dialog, 0, NULL,
321 ac_prefs->smtp_server,
324 if (ac_prefs->pop_before_smtp
325 && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
326 && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
327 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
328 log_message(LOG_PROTOCOL, buf);
329 progress_dialog_set_label(dialog->dialog, buf);
330 progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
332 inc_pop_before_smtp(ac_prefs);
335 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
336 ac_prefs->smtp_server);
337 progress_dialog_set_label(dialog->dialog, buf);
338 log_message(LOG_PROTOCOL, "%s\n", buf);
340 session_set_recv_message_notify(session, send_recv_message, dialog);
341 session_set_send_data_progressive_notify
342 (session, send_send_data_progressive, dialog);
343 session_set_send_data_notify(session, send_send_data_finished, dialog);
346 /* everything is ready to start at MAIL FROM:, just
347 * reinit useful variables.
349 session = SESSION(ac_prefs->session);
350 ac_prefs->session = NULL;
351 smtp_session = SMTP_SESSION(session);
352 smtp_session->state = SMTP_HELO;
353 dialog = (SendProgressDialog *)smtp_session->dialog;
357 /* This has to be initialised for every mail sent */
358 smtp_session->from = g_strdup(spec_from);
359 smtp_session->to_list = to_list;
360 smtp_session->cur_to = to_list;
361 smtp_session->send_data = (guchar *)get_outgoing_rfc2822_str(fp);
362 smtp_session->send_data_len = strlen((gchar *)smtp_session->send_data);
364 session_set_timeout(session,
365 prefs_common.io_timeout_secs * 1000);
366 /* connect if necessary */
367 if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
368 session_destroy(session);
369 send_progress_dialog_destroy(dialog);
370 ac_prefs->session = NULL;
374 debug_print("send_message_smtp(): begin event loop\n");
377 /* as the server is quiet, start sending ourselves */
378 smtp_from(smtp_session);
381 while (session_is_connected(session) && dialog->cancelled == FALSE
382 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
383 gtk_main_iteration();
385 if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
386 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
387 g_free(ac_prefs->tmp_smtp_pass);
388 ac_prefs->tmp_smtp_pass = NULL;
389 } else if (ac_prefs->tmp_pass) {
390 g_free(ac_prefs->tmp_pass);
391 ac_prefs->tmp_pass = NULL;
394 } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
395 log_message(LOG_PROTOCOL, "%s\n", _("Mail sent successfully."));
397 } else if (session->state == SESSION_EOF &&
398 SMTP_SESSION(session)->state == SMTP_QUIT) {
399 /* consider EOF right after QUIT successful */
400 log_warning(LOG_PROTOCOL, "%s\n", _("Connection closed by the remote host."));
402 } else if (session->state == SESSION_ERROR ||
403 session->state == SESSION_EOF ||
404 session->state == SESSION_TIMEOUT ||
405 SMTP_SESSION(session)->state == SMTP_ERROR ||
406 SMTP_SESSION(session)->error_val != SM_OK)
408 else if (dialog->cancelled == TRUE)
412 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
413 send_put_error(session);
414 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
417 /* if we should close the connection, let's do it.
418 * Close it in case of error, too, as it helps reinitializing things
421 if (!keep_session || ret != 0) {
422 if (session_is_connected(session))
423 smtp_quit(smtp_session);
424 while (session_is_connected(session) && !dialog->cancelled)
425 gtk_main_iteration();
426 session_destroy(session);
427 ac_prefs->session = NULL;
428 send_progress_dialog_destroy(dialog);
430 g_free(smtp_session->from);
431 g_free(smtp_session->send_data);
432 g_free(smtp_session->error_msg);
434 if (keep_session && ret == 0 && ac_prefs->session == NULL)
435 ac_prefs->session = SMTP_SESSION(session);
439 statusbar_verbosity_set(FALSE);
443 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
445 return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
448 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
451 SMTPSession *smtp_session = SMTP_SESSION(session);
452 SendProgressDialog *dialog = (SendProgressDialog *)data;
453 gchar *state_str = NULL;
455 g_return_val_if_fail(dialog != NULL, -1);
457 switch (smtp_session->state) {
462 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
463 state_str = _("Authenticating");
464 statusbar_print_all(_("Sending message..."));
467 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
468 state_str = _("Authenticating");
469 statusbar_print_all(_("Sending message..."));
472 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
473 state_str = _("Authenticating");
476 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
477 state_str = _("Sending");
480 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
481 state_str = _("Sending");
485 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
486 state_str = _("Sending");
489 g_snprintf(buf, sizeof(buf), _("Quitting..."));
490 state_str = _("Quitting");
493 g_warning("send: error: %s\n", msg);
499 progress_dialog_set_label(dialog->dialog, buf);
500 progress_dialog_list_set_status(dialog->dialog, 0, state_str);
505 static gint send_send_data_progressive(Session *session, guint cur_len,
506 guint total_len, gpointer data)
509 SendProgressDialog *dialog = (SendProgressDialog *)data;
511 g_return_val_if_fail(dialog != NULL, -1);
513 if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
514 SMTP_SESSION(session)->state != SMTP_EOM)
517 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
519 progress_dialog_set_label(dialog->dialog, buf);
520 progress_dialog_set_fraction
521 (dialog->dialog, (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
526 static gint send_send_data_finished(Session *session, guint len, gpointer data)
528 SendProgressDialog *dialog = (SendProgressDialog *)data;
530 g_return_val_if_fail(dialog != NULL, -1);
532 send_send_data_progressive(session, len, len, dialog);
536 static void send_progress_dialog_size_allocate_cb(GtkWidget *widget,
537 GtkAllocation *allocation)
539 g_return_if_fail(allocation != NULL);
541 prefs_common.sendwin_width = allocation->width;
542 prefs_common.sendwin_height = allocation->height;
545 static SendProgressDialog *send_progress_dialog_create(void)
547 SendProgressDialog *dialog;
548 ProgressDialog *progress;
549 static GdkGeometry geometry;
551 dialog = g_new0(SendProgressDialog, 1);
553 progress = progress_dialog_create();
554 gtk_window_set_title(GTK_WINDOW(progress->window),
555 _("Sending message"));
556 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
557 G_CALLBACK(send_cancel_button_cb), dialog);
558 g_signal_connect(G_OBJECT(progress->window), "delete_event",
559 G_CALLBACK(gtk_true), NULL);
560 gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
561 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
562 G_CALLBACK(send_progress_dialog_size_allocate_cb), NULL);
563 manage_window_set_transient(GTK_WINDOW(progress->window));
565 progress_dialog_get_fraction(progress);
567 if (!geometry.min_height) {
568 geometry.min_width = 460;
569 geometry.min_height = 250;
572 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
574 gtk_widget_set_size_request(progress->window, prefs_common.sendwin_width,
575 prefs_common.sendwin_height);
577 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
578 gtk_widget_show_now(progress->window);
581 dialog->dialog = progress;
586 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
588 g_return_if_fail(dialog != NULL);
589 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
590 progress_dialog_destroy(dialog->dialog);
595 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
597 SendProgressDialog *dialog = (SendProgressDialog *)data;
599 dialog->cancelled = TRUE;
602 static void send_put_error(Session *session)
605 gchar *log_msg = NULL;
606 gchar *err_msg = NULL;
608 msg = SMTP_SESSION(session)->error_msg;
610 switch (SMTP_SESSION(session)->error_val) {
612 case SM_UNRECOVERABLE:
613 log_msg = _("Error occurred while sending the message.");
615 err_msg = g_strdup_printf
616 (_("Error occurred while sending the message:\n%s"),
619 err_msg = g_strdup(log_msg);
622 log_msg = _("Authentication failed.");
624 err_msg = g_strdup_printf
625 (_("Authentication failed:\n%s"), msg);
627 err_msg = g_strdup(log_msg);
630 switch (session->state) {
633 _("Error occurred while sending the message.");
634 err_msg = g_strdup(log_msg);
637 log_msg = _("Connection closed by the remote host.");
638 err_msg = g_strdup(log_msg);
640 case SESSION_TIMEOUT:
641 log_msg = _("Session timed out.");
642 err_msg = g_strdup(log_msg);
651 log_error(LOG_PROTOCOL, "%s\n", err_msg);
655 log_warning(LOG_PROTOCOL, "%s\n", log_msg);