2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
33 #include <sys/types.h>
38 #include "send_message.h"
42 #include "prefs_common.h"
43 #include "prefs_account.h"
44 #include "procheader.h"
46 #include "progressdialog.h"
47 #include "statusbar.h"
48 #include "inputdialog.h"
49 #include "alertpanel.h"
50 #include "manage_window.h"
51 #include "logwindow.h"
57 #include "passwordstore.h"
59 typedef struct _SendProgressDialog SendProgressDialog
;
61 struct _SendProgressDialog
63 ProgressDialog
*dialog
;
68 static SendProgressDialog
*send_dialog
= NULL
;
70 static gint
send_recv_message (Session
*session
,
73 static gint
send_send_data_progressive (Session
*session
,
77 static gint
send_send_data_finished (Session
*session
,
81 static SendProgressDialog
*send_progress_dialog_create(void);
82 static void send_progress_dialog_destroy(SendProgressDialog
*dialog
);
84 static void send_showlog_button_cb (GtkWidget
*widget
,
86 static void send_cancel_button_cb (GtkWidget
*widget
,
89 static void send_put_error (Session
*session
);
92 void send_cancel(void)
95 send_cancel_button_cb(NULL
, send_dialog
);
98 gboolean
send_is_active(void)
100 return (send_dialog
!= NULL
);
103 gint
send_message(const gchar
*file
, PrefsAccount
*ac_prefs
, GSList
*to_list
)
108 cm_return_val_if_fail(file
!= NULL
, -1);
109 cm_return_val_if_fail(ac_prefs
!= NULL
, -1);
110 cm_return_val_if_fail(to_list
!= NULL
, -1);
112 if ((fp
= g_fopen(file
, "rb")) == NULL
) {
113 FILE_OP_ERROR(file
, "fopen");
118 if (ac_prefs
->use_mail_command
&& ac_prefs
->mail_command
&&
119 (*ac_prefs
->mail_command
)) {
120 val
= send_message_local(ac_prefs
->mail_command
, fp
);
125 val
= send_message_smtp(ac_prefs
, to_list
, fp
);
141 gint
send_message_local(const gchar
*command
, FILE *fp
)
147 gboolean err
= FALSE
;
149 cm_return_val_if_fail(command
!= NULL
, -1);
150 cm_return_val_if_fail(fp
!= NULL
, -1);
152 log_message(LOG_PROTOCOL
, _("Sending message using command: %s\n"), command
);
154 argv
= strsplit_with_quote(command
, " ", 0);
156 if (g_spawn_async_with_pipes(NULL
, argv
, NULL
,
160 G_SPAWN_DO_NOT_REAP_CHILD
,
163 &pid
, &child_stdin
, NULL
, NULL
,
165 g_snprintf(buf
, sizeof(buf
),
166 _("Couldn't execute command: %s"), command
);
167 log_warning(LOG_PROTOCOL
, "%s\n", buf
);
168 alertpanel_error("%s", buf
);
174 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
176 if (buf
[0] == '.' && buf
[1] == '\0') {
177 if (fd_write_all(child_stdin
, ".", 1) < 0) {
182 if (fd_write_all(child_stdin
, buf
, strlen(buf
)) < 0 ||
183 fd_write_all(child_stdin
, "\n", 1) < 0) {
189 fd_close(child_stdin
);
193 waitpid(pid
, &status
, 0);
194 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
198 g_spawn_close_pid(pid
);
201 g_snprintf(buf
, sizeof(buf
),
202 _("Error occurred while executing command: %s"),
204 log_warning(LOG_PROTOCOL
, "%s\n", buf
);
205 alertpanel_error("%s", buf
);
212 gint
send_message_smtp_full(PrefsAccount
*ac_prefs
, GSList
*to_list
, FILE *fp
, gboolean keep_session
)
215 SMTPSession
*smtp_session
;
219 gboolean was_inited
= FALSE
;
220 MsgInfo
*tmp_msginfo
= NULL
;
221 MsgFlags flags
= {0, 0};
223 gchar spec_from
[BUFFSIZE
];
225 cm_return_val_if_fail(ac_prefs
!= NULL
, -1);
226 cm_return_val_if_fail(ac_prefs
->address
!= NULL
, -1);
227 cm_return_val_if_fail(ac_prefs
->smtp_server
!= NULL
, -1);
228 cm_return_val_if_fail(to_list
!= NULL
, -1);
229 cm_return_val_if_fail(fp
!= NULL
, -1);
231 /* get the From address used, not necessarily the ac_prefs',
232 * because it's editable. */
239 tmp_msginfo
= procheader_parse_stream(fp
, flags
, TRUE
, FALSE
);
240 if (fseek(fp
, fp_pos
, SEEK_SET
) < 0) {
245 if (tmp_msginfo
&& tmp_msginfo
->extradata
&& tmp_msginfo
->extradata
->resent_from
) {
246 strncpy2(spec_from
, tmp_msginfo
->extradata
->resent_from
, BUFFSIZE
-1);
247 extract_address(spec_from
);
248 } else if (tmp_msginfo
&& tmp_msginfo
->from
) {
249 strncpy2(spec_from
, tmp_msginfo
->from
, BUFFSIZE
-1);
250 extract_address(spec_from
);
252 strncpy2(spec_from
, ac_prefs
->address
, BUFFSIZE
-1);
255 procmsg_msginfo_free(&tmp_msginfo
);
258 if (!ac_prefs
->session
) {
259 /* we can't reuse a previously initialised session */
260 session
= smtp_session_new(ac_prefs
);
261 session
->ssl_cert_auto_accept
= ac_prefs
->ssl_certs_auto_accept
;
263 smtp_session
= SMTP_SESSION(session
);
265 if (ac_prefs
->set_domain
&& ac_prefs
->domain
&& strlen(ac_prefs
->domain
)) {
266 smtp_session
->hostname
= g_strdup(ac_prefs
->domain
);
268 smtp_session
->hostname
= NULL
;
272 port
= ac_prefs
->set_smtpport
? ac_prefs
->smtpport
:
273 ac_prefs
->ssl_smtp
== SSL_TUNNEL
? SSMTP_PORT
: SMTP_PORT
;
274 session
->ssl_type
= ac_prefs
->ssl_smtp
;
275 if (ac_prefs
->ssl_smtp
!= SSL_NONE
)
276 session
->nonblocking
= ac_prefs
->use_nonblocking_ssl
;
277 if (ac_prefs
->set_gnutls_priority
&& ac_prefs
->gnutls_priority
&&
278 strlen(ac_prefs
->gnutls_priority
))
279 session
->gnutls_priority
= g_strdup(ac_prefs
->gnutls_priority
);
281 if (ac_prefs
->ssl_smtp
!= SSL_NONE
) {
282 if (alertpanel_full(_("Insecure connection"),
283 _("This connection is configured to be secured "
284 "using SSL/TLS, but SSL/TLS is not available "
285 "in this build of Claws Mail. \n\n"
286 "Do you want to continue connecting to this "
287 "server? The communication would not be "
289 GTK_STOCK_CANCEL
, _("Con_tinue connecting"),
290 NULL
, FALSE
, NULL
, ALERT_WARNING
,
291 G_ALERTDEFAULT
) != G_ALERTALTERNATE
) {
292 session_destroy(session
);
296 port
= ac_prefs
->set_smtpport
? ac_prefs
->smtpport
: SMTP_PORT
;
299 if (ac_prefs
->use_smtp_auth
) {
300 smtp_session
->forced_auth_type
= ac_prefs
->smtp_auth_type
;
301 if (ac_prefs
->smtp_userid
&& strlen(ac_prefs
->smtp_userid
)) {
302 smtp_session
->user
= g_strdup(ac_prefs
->smtp_userid
);
303 if (password_get(smtp_session
->user
,
304 ac_prefs
->smtp_server
, "smtp", port
,
305 &(smtp_session
->pass
))) {
307 } else if ((smtp_session
->pass
=
308 passwd_store_get_account(ac_prefs
->account_id
,
309 PWS_ACCOUNT_SEND
)) == NULL
) {
311 input_dialog_query_password_keep
312 (ac_prefs
->smtp_server
,
314 &(ac_prefs
->session_smtp_passwd
));
315 if (!smtp_session
->pass
) {
316 session_destroy(session
);
321 smtp_session
->user
= g_strdup(ac_prefs
->userid
);
322 if (password_get(smtp_session
->user
,
323 ac_prefs
->smtp_server
, "smtp", port
,
324 &(smtp_session
->pass
))) {
326 } else if ((smtp_session
->pass
= passwd_store_get_account(
327 ac_prefs
->account_id
, PWS_ACCOUNT_RECV
)) == NULL
) {
329 input_dialog_query_password_keep
330 (ac_prefs
->smtp_server
,
332 &(ac_prefs
->session_smtp_passwd
));
333 if (!smtp_session
->pass
) {
334 session_destroy(session
);
340 smtp_session
->user
= NULL
;
341 smtp_session
->pass
= NULL
;
344 send_dialog
= send_progress_dialog_create();
345 send_dialog
->session
= session
;
346 smtp_session
->dialog
= send_dialog
;
348 progress_dialog_list_set(send_dialog
->dialog
, 0, NULL
,
349 ac_prefs
->smtp_server
,
352 if (ac_prefs
->pop_before_smtp
353 && (ac_prefs
->protocol
== A_POP3
)
354 && (time(NULL
) - ac_prefs
->last_pop_login_time
) > (60 * ac_prefs
->pop_before_smtp_timeout
)) {
355 g_snprintf(buf
, sizeof(buf
), _("Doing POP before SMTP..."));
356 log_message(LOG_PROTOCOL
, "%s\n", buf
);
357 progress_dialog_set_label(send_dialog
->dialog
, buf
);
358 progress_dialog_list_set_status(send_dialog
->dialog
, 0, _("POP before SMTP"));
360 inc_pop_before_smtp(ac_prefs
);
363 g_snprintf(buf
, sizeof(buf
), _("Account '%s': Connecting to SMTP server: %s:%d..."),
364 ac_prefs
->account_name
, ac_prefs
->smtp_server
, port
);
365 progress_dialog_set_label(send_dialog
->dialog
, buf
);
366 log_message(LOG_PROTOCOL
, "%s\n", buf
);
368 session_set_recv_message_notify(session
, send_recv_message
, send_dialog
);
369 session_set_send_data_progressive_notify
370 (session
, send_send_data_progressive
, send_dialog
);
371 session_set_send_data_notify(session
, send_send_data_finished
, send_dialog
);
374 /* everything is ready to start at MAIL FROM:, just
375 * reinit useful variables.
377 session
= SESSION(ac_prefs
->session
);
378 ac_prefs
->session
= NULL
;
379 smtp_session
= SMTP_SESSION(session
);
380 smtp_session
->state
= SMTP_HELO
;
381 send_dialog
= (SendProgressDialog
*)smtp_session
->dialog
;
385 /* This has to be initialised for every mail sent */
386 smtp_session
->from
= g_strdup(spec_from
);
387 smtp_session
->to_list
= to_list
;
388 smtp_session
->cur_to
= to_list
;
389 smtp_session
->send_data
= (guchar
*)get_outgoing_rfc2822_str(fp
);
390 smtp_session
->send_data_len
= strlen((gchar
*)smtp_session
->send_data
);
392 session_set_timeout(session
,
393 prefs_common
.io_timeout_secs
* 1000);
394 /* connect if necessary */
395 if (!was_inited
&& session_connect(session
, ac_prefs
->smtp_server
, port
) < 0) {
396 session_destroy(session
);
397 send_progress_dialog_destroy(send_dialog
);
398 ac_prefs
->session
= NULL
;
402 debug_print("send_message_smtp(): begin event loop\n");
405 /* as the server is quiet, start sending ourselves */
406 smtp_from(smtp_session
);
409 while (session_is_running(session
) && send_dialog
->cancelled
== FALSE
410 && SMTP_SESSION(session
)->state
!= SMTP_MAIL_SENT_OK
)
411 gtk_main_iteration();
413 if (SMTP_SESSION(session
)->error_val
== SM_AUTHFAIL
) {
414 if (ac_prefs
->session_smtp_passwd
) {
415 g_free(ac_prefs
->session_smtp_passwd
);
416 ac_prefs
->session_smtp_passwd
= NULL
;
419 } else if (SMTP_SESSION(session
)->state
== SMTP_MAIL_SENT_OK
) {
420 log_message(LOG_PROTOCOL
, "%s\n", _("Mail sent successfully."));
422 } else if (session
->state
== SESSION_EOF
&&
423 SMTP_SESSION(session
)->state
== SMTP_QUIT
) {
424 /* consider EOF right after QUIT successful */
425 log_warning(LOG_PROTOCOL
, "%s\n", _("Connection closed by the remote host."));
427 } else if (session
->state
== SESSION_ERROR
||
428 session
->state
== SESSION_EOF
||
429 session
->state
== SESSION_TIMEOUT
||
430 SMTP_SESSION(session
)->state
== SMTP_ERROR
||
431 SMTP_SESSION(session
)->error_val
!= SM_OK
)
433 else if (send_dialog
->cancelled
== TRUE
)
437 manage_window_focus_in(send_dialog
->dialog
->window
, NULL
, NULL
);
438 send_put_error(session
);
439 manage_window_focus_out(send_dialog
->dialog
->window
, NULL
, NULL
);
442 /* if we should close the connection, let's do it.
443 * Close it in case of error, too, as it helps reinitializing things
446 if (!keep_session
|| ret
!= 0) {
447 if (session_is_connected(session
))
448 smtp_quit(smtp_session
);
449 while (session_is_connected(session
) && !send_dialog
->cancelled
)
450 gtk_main_iteration();
451 session_destroy(session
);
452 ac_prefs
->session
= NULL
;
453 send_progress_dialog_destroy(send_dialog
);
455 g_free(smtp_session
->from
);
456 g_free(smtp_session
->send_data
);
457 g_free(smtp_session
->error_msg
);
459 if (keep_session
&& ret
== 0 && ac_prefs
->session
== NULL
)
460 ac_prefs
->session
= SMTP_SESSION(session
);
463 statuswindow_pop_all();
464 statusbar_verbosity_set(FALSE
);
468 gint
send_message_smtp(PrefsAccount
*ac_prefs
, GSList
*to_list
, FILE *fp
)
470 return send_message_smtp_full(ac_prefs
, to_list
, fp
, FALSE
);
473 static gint
send_recv_message(Session
*session
, const gchar
*msg
, gpointer data
)
476 SMTPSession
*smtp_session
= SMTP_SESSION(session
);
477 SendProgressDialog
*dialog
= (SendProgressDialog
*)data
;
478 gchar
*state_str
= NULL
;
480 cm_return_val_if_fail(dialog
!= NULL
, -1);
482 switch (smtp_session
->state
) {
486 g_snprintf(buf
, sizeof(buf
), _("Sending HELO..."));
487 state_str
= _("Authenticating");
488 statuswindow_print_all(_("Sending message..."));
491 g_snprintf(buf
, sizeof(buf
), _("Sending EHLO..."));
492 state_str
= _("Authenticating");
493 statuswindow_print_all(_("Sending message..."));
496 g_snprintf(buf
, sizeof(buf
), _("Authenticating..."));
497 state_str
= _("Authenticating");
500 g_snprintf(buf
, sizeof(buf
), _("Sending MAIL FROM..."));
501 state_str
= _("Sending");
504 g_snprintf(buf
, sizeof(buf
), _("Sending RCPT TO..."));
505 state_str
= _("Sending");
509 g_snprintf(buf
, sizeof(buf
), _("Sending DATA..."));
510 state_str
= _("Sending");
513 g_snprintf(buf
, sizeof(buf
), _("Quitting..."));
514 state_str
= _("Quitting");
517 g_warning("send: error: %s", msg
);
523 progress_dialog_set_label(dialog
->dialog
, buf
);
524 progress_dialog_list_set_status(dialog
->dialog
, 0, state_str
);
529 static gint
send_send_data_progressive(Session
*session
, guint cur_len
,
530 guint total_len
, gpointer data
)
533 SendProgressDialog
*dialog
= (SendProgressDialog
*)data
;
534 MainWindow
*mainwin
= mainwindow_get_mainwindow();
536 cm_return_val_if_fail(dialog
!= NULL
, -1);
538 if (SMTP_SESSION(session
)->state
!= SMTP_SEND_DATA
&&
539 SMTP_SESSION(session
)->state
!= SMTP_EOM
)
542 g_snprintf(buf
, sizeof(buf
), _("Sending message (%d / %d bytes)"),
544 progress_dialog_set_label(dialog
->dialog
, buf
);
545 progress_dialog_set_fraction
546 (dialog
->dialog
, (total_len
== 0) ? 0 : (gfloat
)cur_len
/ (gfloat
)total_len
);
549 if (!gtk_widget_get_visible(mainwin
->progressbar
))
550 gtk_widget_show(mainwin
->progressbar
);
551 gtk_progress_bar_set_fraction
552 (GTK_PROGRESS_BAR(mainwin
->progressbar
),
553 (total_len
== 0) ? 0 : (gfloat
)cur_len
/ (gfloat
)total_len
);
559 static gint
send_send_data_finished(Session
*session
, guint len
, gpointer data
)
561 SendProgressDialog
*dialog
= (SendProgressDialog
*)data
;
562 MainWindow
*mainwin
= mainwindow_get_mainwindow();
564 cm_return_val_if_fail(dialog
!= NULL
, -1);
566 send_send_data_progressive(session
, len
, len
, dialog
);
568 gtk_widget_hide(mainwin
->progressbar
);
569 gtk_progress_bar_set_fraction
570 (GTK_PROGRESS_BAR(mainwin
->progressbar
),(gfloat
)0);
576 static void send_progress_dialog_size_allocate_cb(GtkWidget
*widget
,
577 GtkAllocation
*allocation
)
579 cm_return_if_fail(allocation
!= NULL
);
581 prefs_common
.sendwin_width
= allocation
->width
;
582 prefs_common
.sendwin_height
= allocation
->height
;
585 static SendProgressDialog
*send_progress_dialog_create(void)
587 SendProgressDialog
*dialog
;
588 ProgressDialog
*progress
;
589 static GdkGeometry geometry
;
591 dialog
= g_new0(SendProgressDialog
, 1);
593 progress
= progress_dialog_create();
594 gtk_window_set_title(GTK_WINDOW(progress
->window
),
595 _("Sending message"));
596 g_signal_connect(G_OBJECT(progress
->showlog_btn
), "clicked",
597 G_CALLBACK(send_showlog_button_cb
), dialog
);
598 g_signal_connect(G_OBJECT(progress
->cancel_btn
), "clicked",
599 G_CALLBACK(send_cancel_button_cb
), dialog
);
600 g_signal_connect(G_OBJECT(progress
->window
), "delete_event",
601 G_CALLBACK(gtk_true
), NULL
);
602 gtk_window_set_modal(GTK_WINDOW(progress
->window
), TRUE
);
603 g_signal_connect(G_OBJECT(progress
->window
), "size_allocate",
604 G_CALLBACK(send_progress_dialog_size_allocate_cb
), NULL
);
605 manage_window_set_transient(GTK_WINDOW(progress
->window
));
607 progress_dialog_get_fraction(progress
);
609 if (!geometry
.min_height
) {
610 geometry
.min_width
= 460;
611 geometry
.min_height
= 250;
614 gtk_window_set_geometry_hints(GTK_WINDOW(progress
->window
), NULL
, &geometry
,
616 gtk_widget_set_size_request(progress
->window
, prefs_common
.sendwin_width
,
617 prefs_common
.sendwin_height
);
619 if (!prefs_common
.send_dialog_invisible
) {
620 gtk_widget_show_now(progress
->window
);
623 dialog
->dialog
= progress
;
628 static void send_progress_dialog_destroy(SendProgressDialog
*dialog
)
630 cm_return_if_fail(dialog
!= NULL
);
631 if (!prefs_common
.send_dialog_invisible
) {
632 progress_dialog_destroy(dialog
->dialog
);
638 static void send_showlog_button_cb(GtkWidget
*widget
, gpointer data
)
640 MainWindow
*mainwin
= mainwindow_get_mainwindow();
642 log_window_show(mainwin
->logwin
);
645 static void send_cancel_button_cb(GtkWidget
*widget
, gpointer data
)
647 SendProgressDialog
*dialog
= (SendProgressDialog
*)data
;
648 statusbar_progress_all(0,0,0);
650 dialog
->cancelled
= TRUE
;
653 static void send_put_error(Session
*session
)
656 gchar
*log_msg
= NULL
;
657 gchar
*err_msg
= NULL
;
659 msg
= SMTP_SESSION(session
)->error_msg
;
661 switch (SMTP_SESSION(session
)->error_val
) {
663 case SM_UNRECOVERABLE
:
664 log_msg
= _("Error occurred while sending the message.");
666 err_msg
= g_strdup_printf
667 (_("Error occurred while sending the message:\n%s"),
670 err_msg
= g_strdup(log_msg
);
673 log_msg
= _("Authentication failed.");
675 err_msg
= g_strdup_printf
676 (_("Authentication failed:\n%s"), msg
);
678 err_msg
= g_strdup(log_msg
);
681 switch (session
->state
) {
684 _("Error occurred while sending the message.");
685 err_msg
= g_strdup(log_msg
);
688 log_msg
= _("Connection closed by the remote host.");
689 err_msg
= g_strdup(log_msg
);
691 case SESSION_TIMEOUT
:
692 log_msg
= _("Session timed out. You may be able to "
693 "recover by increasing the timeout value in "
694 "Preferences/Other/Miscellaneous.");
695 err_msg
= g_strdup(log_msg
);
704 log_error(LOG_PROTOCOL
, "%s\n", err_msg
);
708 log_warning(LOG_PROTOCOL
, "%s\n", log_msg
);