2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 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>
32 #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 cm_return_val_if_fail(file != NULL, -1);
91 cm_return_val_if_fail(ac_prefs != NULL, -1);
92 cm_return_val_if_fail(to_list != NULL, -1);
94 if ((fp = g_fopen(file, "rb")) == NULL) {
95 FILE_OP_ERROR(file, "fopen");
100 if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
101 (*ac_prefs->mail_command)) {
102 val = send_message_local(ac_prefs->mail_command, fp);
107 val = send_message_smtp(ac_prefs, to_list, fp);
123 gint send_message_local(const gchar *command, FILE *fp)
129 gboolean err = FALSE;
132 cm_return_val_if_fail(command != NULL, -1);
133 cm_return_val_if_fail(fp != NULL, -1);
135 log_message(LOG_PROTOCOL, _("Sending message using command: %s\n"), command);
137 argv = strsplit_with_quote(command, " ", 0);
139 if (g_spawn_async_with_pipes(NULL, argv, NULL,
143 G_SPAWN_DO_NOT_REAP_CHILD,
146 &pid, &child_stdin, NULL, NULL,
148 g_snprintf(buf, sizeof(buf),
149 _("Couldn't execute command: %s"), command);
150 log_warning(LOG_PROTOCOL, "%s\n", buf);
151 alertpanel_error("%s", buf);
157 while (fgets(buf, sizeof(buf), fp) != NULL) {
159 if (buf[0] == '.' && buf[1] == '\0') {
160 if (fd_write_all(child_stdin, ".", 1) < 0) {
165 if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
166 fd_write_all(child_stdin, "\n", 1) < 0) {
172 fd_close(child_stdin);
175 waitpid(pid, &status, 0);
176 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
180 g_spawn_close_pid(pid);
183 g_snprintf(buf, sizeof(buf),
184 _("Error occurred while executing command: %s"),
186 log_warning(LOG_PROTOCOL, "%s\n", buf);
187 alertpanel_error("%s", buf);
194 gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
197 SMTPSession *smtp_session;
199 SendProgressDialog *dialog;
202 gboolean was_inited = FALSE;
203 MsgInfo *tmp_msginfo = NULL;
204 MsgFlags flags = {0, 0};
206 gchar spec_from[BUFFSIZE];
208 cm_return_val_if_fail(ac_prefs != NULL, -1);
209 cm_return_val_if_fail(ac_prefs->address != NULL, -1);
210 cm_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
211 cm_return_val_if_fail(to_list != NULL, -1);
212 cm_return_val_if_fail(fp != NULL, -1);
214 /* get the From address used, not necessarily the ac_prefs',
215 * because it's editable. */
218 tmp_msginfo = procheader_parse_stream(fp, flags, FALSE, FALSE);
219 fseek(fp, fp_pos, SEEK_SET);
221 if (tmp_msginfo && tmp_msginfo->from) {
222 strncpy2(spec_from, tmp_msginfo->from, BUFFSIZE-1);
223 extract_address(spec_from);
225 strncpy2(spec_from, ac_prefs->address, BUFFSIZE-1);
228 procmsg_msginfo_free(tmp_msginfo);
231 if (!ac_prefs->session) {
232 /* we can't reuse a previously initialised session */
233 session = smtp_session_new(ac_prefs);
234 smtp_session = SMTP_SESSION(session);
236 if (ac_prefs->set_domain && ac_prefs->domain && strlen(ac_prefs->domain)) {
237 smtp_session->hostname = g_strdup(ac_prefs->domain);
239 smtp_session->hostname = NULL;
242 if (ac_prefs->use_smtp_auth) {
243 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
244 if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
245 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
246 if (ac_prefs->smtp_passwd)
248 g_strdup(ac_prefs->smtp_passwd);
251 input_dialog_query_password_keep
252 (ac_prefs->smtp_server,
254 &(ac_prefs->session_smtp_passwd));
255 if (!smtp_session->pass) {
256 session_destroy(session);
261 smtp_session->user = g_strdup(ac_prefs->userid);
262 if (ac_prefs->passwd)
263 smtp_session->pass = g_strdup(ac_prefs->passwd);
266 input_dialog_query_password_keep
267 (ac_prefs->smtp_server,
269 &(ac_prefs->session_smtp_passwd));
270 if (!smtp_session->pass) {
271 session_destroy(session);
277 smtp_session->user = NULL;
278 smtp_session->pass = NULL;
282 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
283 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
284 session->ssl_type = ac_prefs->ssl_smtp;
285 if (ac_prefs->ssl_smtp != SSL_NONE)
286 session->nonblocking = ac_prefs->use_nonblocking_ssl;
288 if (ac_prefs->ssl_smtp != SSL_NONE) {
289 if (alertpanel_full(_("Insecure connection"),
290 _("This connection is configured to be secured "
291 "using SSL, but SSL is not available in this "
292 "build of Claws Mail. \n\n"
293 "Do you want to continue connecting to this "
294 "server? The communication would not be "
296 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
297 NULL, FALSE, NULL, ALERT_WARNING,
298 G_ALERTDEFAULT) != G_ALERTALTERNATE) {
299 session_destroy(session);
303 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
306 dialog = send_progress_dialog_create();
307 dialog->session = session;
308 smtp_session->dialog = dialog;
310 progress_dialog_list_set(dialog->dialog, 0, NULL,
311 ac_prefs->smtp_server,
314 if (ac_prefs->pop_before_smtp
315 && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
316 && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
317 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
318 log_message(LOG_PROTOCOL, "%s\n", buf);
319 progress_dialog_set_label(dialog->dialog, buf);
320 progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
322 inc_pop_before_smtp(ac_prefs);
325 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
326 ac_prefs->smtp_server);
327 progress_dialog_set_label(dialog->dialog, buf);
328 log_message(LOG_PROTOCOL, "%s\n", buf);
330 session_set_recv_message_notify(session, send_recv_message, dialog);
331 session_set_send_data_progressive_notify
332 (session, send_send_data_progressive, dialog);
333 session_set_send_data_notify(session, send_send_data_finished, dialog);
336 /* everything is ready to start at MAIL FROM:, just
337 * reinit useful variables.
339 session = SESSION(ac_prefs->session);
340 ac_prefs->session = NULL;
341 smtp_session = SMTP_SESSION(session);
342 smtp_session->state = SMTP_HELO;
343 dialog = (SendProgressDialog *)smtp_session->dialog;
347 /* This has to be initialised for every mail sent */
348 smtp_session->from = g_strdup(spec_from);
349 smtp_session->to_list = to_list;
350 smtp_session->cur_to = to_list;
351 smtp_session->send_data = (guchar *)get_outgoing_rfc2822_str(fp);
352 smtp_session->send_data_len = strlen((gchar *)smtp_session->send_data);
354 session_set_timeout(session,
355 prefs_common.io_timeout_secs * 1000);
356 /* connect if necessary */
357 if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
358 session_destroy(session);
359 send_progress_dialog_destroy(dialog);
360 ac_prefs->session = NULL;
364 debug_print("send_message_smtp(): begin event loop\n");
367 /* as the server is quiet, start sending ourselves */
368 smtp_from(smtp_session);
371 while (session_is_connected(session) && dialog->cancelled == FALSE
372 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
373 gtk_main_iteration();
375 if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
376 if (ac_prefs->session_smtp_passwd) {
377 g_free(ac_prefs->session_smtp_passwd);
378 ac_prefs->session_smtp_passwd = NULL;
381 } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
382 log_message(LOG_PROTOCOL, "%s\n", _("Mail sent successfully."));
384 } else if (session->state == SESSION_EOF &&
385 SMTP_SESSION(session)->state == SMTP_QUIT) {
386 /* consider EOF right after QUIT successful */
387 log_warning(LOG_PROTOCOL, "%s\n", _("Connection closed by the remote host."));
389 } else if (session->state == SESSION_ERROR ||
390 session->state == SESSION_EOF ||
391 session->state == SESSION_TIMEOUT ||
392 SMTP_SESSION(session)->state == SMTP_ERROR ||
393 SMTP_SESSION(session)->error_val != SM_OK)
395 else if (dialog->cancelled == TRUE)
399 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
400 send_put_error(session);
401 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
404 /* if we should close the connection, let's do it.
405 * Close it in case of error, too, as it helps reinitializing things
408 if (!keep_session || ret != 0) {
409 if (session_is_connected(session))
410 smtp_quit(smtp_session);
411 while (session_is_connected(session) && !dialog->cancelled)
412 gtk_main_iteration();
413 session_destroy(session);
414 ac_prefs->session = NULL;
415 send_progress_dialog_destroy(dialog);
417 g_free(smtp_session->from);
418 g_free(smtp_session->send_data);
419 g_free(smtp_session->error_msg);
421 if (keep_session && ret == 0 && ac_prefs->session == NULL)
422 ac_prefs->session = SMTP_SESSION(session);
425 statuswindow_pop_all();
426 statusbar_verbosity_set(FALSE);
430 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
432 return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
435 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
438 SMTPSession *smtp_session = SMTP_SESSION(session);
439 SendProgressDialog *dialog = (SendProgressDialog *)data;
440 gchar *state_str = NULL;
442 cm_return_val_if_fail(dialog != NULL, -1);
444 switch (smtp_session->state) {
449 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
450 state_str = _("Authenticating");
451 statuswindow_print_all(_("Sending message..."));
454 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
455 state_str = _("Authenticating");
456 statuswindow_print_all(_("Sending message..."));
459 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
460 state_str = _("Authenticating");
463 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
464 state_str = _("Sending");
467 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
468 state_str = _("Sending");
472 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
473 state_str = _("Sending");
476 g_snprintf(buf, sizeof(buf), _("Quitting..."));
477 state_str = _("Quitting");
480 g_warning("send: error: %s\n", msg);
486 progress_dialog_set_label(dialog->dialog, buf);
487 progress_dialog_list_set_status(dialog->dialog, 0, state_str);
492 static gint send_send_data_progressive(Session *session, guint cur_len,
493 guint total_len, gpointer data)
496 SendProgressDialog *dialog = (SendProgressDialog *)data;
497 MainWindow *mainwin = mainwindow_get_mainwindow();
499 cm_return_val_if_fail(dialog != NULL, -1);
501 if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
502 SMTP_SESSION(session)->state != SMTP_EOM)
505 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
507 progress_dialog_set_label(dialog->dialog, buf);
508 progress_dialog_set_fraction
509 (dialog->dialog, (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
512 if (!gtkut_widget_get_visible(mainwin->progressbar))
513 gtk_widget_show(mainwin->progressbar);
514 gtk_progress_bar_set_fraction
515 (GTK_PROGRESS_BAR(mainwin->progressbar),
516 (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
522 static gint send_send_data_finished(Session *session, guint len, gpointer data)
524 SendProgressDialog *dialog = (SendProgressDialog *)data;
525 MainWindow *mainwin = mainwindow_get_mainwindow();
527 cm_return_val_if_fail(dialog != NULL, -1);
529 send_send_data_progressive(session, len, len, dialog);
531 gtk_widget_hide(mainwin->progressbar);
532 gtk_progress_bar_set_fraction
533 (GTK_PROGRESS_BAR(mainwin->progressbar),(gfloat)0);
539 static void send_progress_dialog_size_allocate_cb(GtkWidget *widget,
540 GtkAllocation *allocation)
542 cm_return_if_fail(allocation != NULL);
544 prefs_common.sendwin_width = allocation->width;
545 prefs_common.sendwin_height = allocation->height;
548 static SendProgressDialog *send_progress_dialog_create(void)
550 SendProgressDialog *dialog;
551 ProgressDialog *progress;
552 static GdkGeometry geometry;
554 dialog = g_new0(SendProgressDialog, 1);
556 progress = progress_dialog_create();
557 gtk_window_set_title(GTK_WINDOW(progress->window),
558 _("Sending message"));
559 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
560 G_CALLBACK(send_cancel_button_cb), dialog);
561 g_signal_connect(G_OBJECT(progress->window), "delete_event",
562 G_CALLBACK(gtk_true), NULL);
563 gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
564 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
565 G_CALLBACK(send_progress_dialog_size_allocate_cb), NULL);
566 manage_window_set_transient(GTK_WINDOW(progress->window));
568 progress_dialog_get_fraction(progress);
570 if (!geometry.min_height) {
571 geometry.min_width = 460;
572 geometry.min_height = 250;
575 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
577 gtk_widget_set_size_request(progress->window, prefs_common.sendwin_width,
578 prefs_common.sendwin_height);
580 if (!prefs_common.send_dialog_invisible) {
581 gtk_widget_show_now(progress->window);
584 dialog->dialog = progress;
589 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
591 cm_return_if_fail(dialog != NULL);
592 if (!prefs_common.send_dialog_invisible) {
593 progress_dialog_destroy(dialog->dialog);
598 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
600 SendProgressDialog *dialog = (SendProgressDialog *)data;
601 statusbar_progress_all(0,0,0);
603 dialog->cancelled = TRUE;
606 static void send_put_error(Session *session)
609 gchar *log_msg = NULL;
610 gchar *err_msg = NULL;
612 msg = SMTP_SESSION(session)->error_msg;
614 switch (SMTP_SESSION(session)->error_val) {
616 case SM_UNRECOVERABLE:
617 log_msg = _("Error occurred while sending the message.");
619 err_msg = g_strdup_printf
620 (_("Error occurred while sending the message:\n%s"),
623 err_msg = g_strdup(log_msg);
626 log_msg = _("Authentication failed.");
628 err_msg = g_strdup_printf
629 (_("Authentication failed:\n%s"), msg);
631 err_msg = g_strdup(log_msg);
634 switch (session->state) {
637 _("Error occurred while sending the message.");
638 err_msg = g_strdup(log_msg);
641 log_msg = _("Connection closed by the remote host.");
642 err_msg = g_strdup(log_msg);
644 case SESSION_TIMEOUT:
645 log_msg = _("Session timed out. You may be able to "
646 "recover by increasing the timeout value in "
647 "Preferences/Other/Miscellaneous.");
648 err_msg = g_strdup(log_msg);
657 log_error(LOG_PROTOCOL, "%s\n", err_msg);
661 log_warning(LOG_PROTOCOL, "%s\n", log_msg);