fc5a42019bcfa23c0590de5906e5b91532ade935
[claws.git] / src / send_message.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <sys/types.h>
34 #ifndef G_OS_WIN32
35 #include <sys/wait.h>
36 #endif
37
38 #include "send_message.h"
39 #include "session.h"
40 #include "ssl.h"
41 #include "smtp.h"
42 #include "prefs_common.h"
43 #include "prefs_account.h"
44 #include "procheader.h"
45 #include "account.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"
52 #include "socket.h"
53 #include "utils.h"
54 #include "gtkutils.h"
55 #include "inc.h"
56 #include "log.h"
57 #include "passwordstore.h"
58
59 typedef struct _SendProgressDialog      SendProgressDialog;
60
61 struct _SendProgressDialog
62 {
63         ProgressDialog *dialog;
64         Session *session;
65         gboolean cancelled;
66 };
67
68 static SendProgressDialog *send_dialog = NULL;
69
70 static gint send_recv_message           (Session                *session,
71                                          const gchar            *msg,
72                                          gpointer                data);
73 static gint send_send_data_progressive  (Session                *session,
74                                          guint                   cur_len,
75                                          guint                   total_len,
76                                          gpointer                data);
77 static gint send_send_data_finished     (Session                *session,
78                                          guint                   len,
79                                          gpointer                data);
80
81 static SendProgressDialog *send_progress_dialog_create(void);
82 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
83
84 static void send_showlog_button_cb      (GtkWidget      *widget,
85                                          gpointer        data);
86 static void send_cancel_button_cb       (GtkWidget      *widget,
87                                          gpointer        data);
88
89 static void send_put_error              (Session        *session);
90
91
92 void send_cancel(void)
93 {
94         if (send_dialog)
95                 send_cancel_button_cb(NULL, send_dialog);
96 }
97
98 gboolean send_is_active(void)
99 {
100         return (send_dialog != NULL);
101 }
102
103 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
104 {
105         FILE *fp;
106         gint val;
107
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);
111
112         if ((fp = g_fopen(file, "rb")) == NULL) {
113                 FILE_OP_ERROR(file, "fopen");
114                 return -1;
115         }
116
117         inc_lock();
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);
121                 fclose(fp);
122                 inc_unlock();
123                 return val;
124         } else {
125                 val = send_message_smtp(ac_prefs, to_list, fp);
126                 
127                 fclose(fp);
128                 inc_unlock();
129                 return val;
130         }
131 }
132
133 enum
134 {
135         Q_SENDER     = 0,
136         Q_SMTPSERVER = 1,
137         Q_RECIPIENTS = 2,
138         Q_ACCOUNT_ID = 3
139 };
140
141 gint send_message_local(const gchar *command, FILE *fp)
142 {
143         gchar **argv;
144         GPid pid;
145         gint child_stdin;
146         gchar buf[BUFFSIZE];
147         gboolean err = FALSE;
148         gint status;
149
150         cm_return_val_if_fail(command != NULL, -1);
151         cm_return_val_if_fail(fp != NULL, -1);
152
153         log_message(LOG_PROTOCOL, _("Sending message using command: %s\n"), command);
154
155         argv = strsplit_with_quote(command, " ", 0);
156
157         if (g_spawn_async_with_pipes(NULL, argv, NULL,
158 #ifdef G_OS_WIN32
159                                      0,
160 #else
161                                      G_SPAWN_DO_NOT_REAP_CHILD,
162 #endif
163                                      NULL, NULL,
164                                      &pid, &child_stdin, NULL, NULL,
165                                      NULL) == FALSE) {
166                 g_snprintf(buf, sizeof(buf),
167                            _("Couldn't execute command: %s"), command);
168                 log_warning(LOG_PROTOCOL, "%s\n", buf);
169                 alertpanel_error("%s", buf);
170                 g_strfreev(argv);
171                 return -1;
172         }
173         g_strfreev(argv);
174
175         while (fgets(buf, sizeof(buf), fp) != NULL) {
176                 strretchomp(buf);
177                 if (buf[0] == '.' && buf[1] == '\0') {
178                         if (fd_write_all(child_stdin, ".", 1) < 0) {
179                                 err = TRUE;
180                                 break;
181                         }
182                 }
183                 if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
184                     fd_write_all(child_stdin, "\n", 1) < 0) {
185                         err = TRUE;
186                         break;
187                 }
188         }
189
190         fd_close(child_stdin);
191
192 #ifndef G_OS_WIN32
193         waitpid(pid, &status, 0);
194         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
195                 err = TRUE;
196 #endif
197
198         g_spawn_close_pid(pid);
199
200         if (err) {
201                 g_snprintf(buf, sizeof(buf),
202                            _("Error occurred while executing command: %s"),
203                            command);
204                 log_warning(LOG_PROTOCOL, "%s\n", buf);
205                 alertpanel_error("%s", buf);
206                 return -1;
207         }
208
209         return 0;
210 }
211
212 gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
213 {
214         Session *session;
215         SMTPSession *smtp_session;
216         gushort port = 0;
217         gchar buf[BUFFSIZE];
218         gint ret = 0;
219         gboolean was_inited = FALSE;
220         MsgInfo *tmp_msginfo = NULL;
221         MsgFlags flags = {0, 0};
222         long fp_pos = 0;
223         gchar spec_from[BUFFSIZE];
224
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);
230
231         /* get the From address used, not necessarily the ac_prefs',
232          * because it's editable. */
233
234         fp_pos = ftell(fp);
235         if (fp_pos < 0) {
236                 perror("ftell");
237                 return -1;
238         }
239         tmp_msginfo = procheader_parse_stream(fp, flags, TRUE, FALSE);
240         if (fseek(fp, fp_pos, SEEK_SET) < 0) {
241                 perror("fseek");
242                 return -1;
243         }
244
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);
251         } else {
252                 strncpy2(spec_from, ac_prefs->address, BUFFSIZE-1);
253         }
254         if (tmp_msginfo) {
255                 procmsg_msginfo_free(&tmp_msginfo);
256         }
257
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;
262
263                 smtp_session = SMTP_SESSION(session);
264
265                 if (ac_prefs->set_domain && ac_prefs->domain && strlen(ac_prefs->domain)) {
266                         smtp_session->hostname = g_strdup(ac_prefs->domain);
267                 } else {
268                         smtp_session->hostname = NULL;
269                 }
270
271 #ifdef USE_GNUTLS
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);
280 #else
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, but SSL is not available in this "
285                                   "build of Claws Mail. \n\n"
286                                   "Do you want to continue connecting to this "
287                                   "server? The communication would not be "
288                                   "secure."),
289                                   GTK_STOCK_CANCEL, _("Con_tinue connecting"),
290                                   NULL, FALSE, NULL, ALERT_WARNING,
291                                   G_ALERTDEFAULT) != G_ALERTALTERNATE) {
292                                 session_destroy(session);
293                                 return -1;
294                         }
295                 }
296                 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
297 #endif
298
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))) {
306                                         /* NOP */;
307                                 } else if (ac_prefs->smtp_passwd)
308                                         smtp_session->pass =
309                                                 passwd_store_get(PWS_ACCOUNT, ac_prefs->account_name,
310                                                                 PWS_ACCOUNT_SEND);
311                                 else {
312                                         smtp_session->pass =
313                                                 input_dialog_query_password_keep
314                                                         (ac_prefs->smtp_server,
315                                                          smtp_session->user,
316                                                          &(ac_prefs->session_smtp_passwd));
317                                         if (!smtp_session->pass) {
318                                                 session_destroy(session);
319                                                 return -1;
320                                         }
321                                 }
322                         } else {
323                                 smtp_session->user = g_strdup(ac_prefs->userid);
324                                 if (password_get(smtp_session->user,
325                                                         ac_prefs->smtp_server, "smtp", port,
326                                                         &(smtp_session->pass))) {
327                                         /* NOP */;
328                                 } else if (ac_prefs->passwd)
329                                         smtp_session->pass = passwd_store_get(PWS_ACCOUNT,
330                                                         ac_prefs->account_name, PWS_ACCOUNT_RECV);
331                                 else {
332                                         smtp_session->pass =
333                                                 input_dialog_query_password_keep
334                                                         (ac_prefs->smtp_server,
335                                                          smtp_session->user,
336                                                          &(ac_prefs->session_smtp_passwd));
337                                         if (!smtp_session->pass) {
338                                                 session_destroy(session);
339                                                 return -1;
340                                         }
341                                 }
342                         }
343                 } else {
344                         smtp_session->user = NULL;
345                         smtp_session->pass = NULL;
346                 }
347
348                 send_dialog = send_progress_dialog_create();
349                 send_dialog->session = session;
350                 smtp_session->dialog = send_dialog;
351
352                 progress_dialog_list_set(send_dialog->dialog, 0, NULL, 
353                                          ac_prefs->smtp_server, 
354                                          _("Connecting"));
355
356                 if (ac_prefs->pop_before_smtp
357                     && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
358                     && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
359                         g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
360                         log_message(LOG_PROTOCOL, "%s\n", buf);
361                         progress_dialog_set_label(send_dialog->dialog, buf);
362                         progress_dialog_list_set_status(send_dialog->dialog, 0, _("POP before SMTP"));
363                         GTK_EVENTS_FLUSH();
364                         inc_pop_before_smtp(ac_prefs);
365                 }
366
367                 g_snprintf(buf, sizeof(buf), _("Account '%s': Connecting to SMTP server: %s:%d..."),
368                                 ac_prefs->account_name, ac_prefs->smtp_server, port);
369                 progress_dialog_set_label(send_dialog->dialog, buf);
370                 log_message(LOG_PROTOCOL, "%s\n", buf);
371
372                 session_set_recv_message_notify(session, send_recv_message, send_dialog);
373                 session_set_send_data_progressive_notify
374                         (session, send_send_data_progressive, send_dialog);
375                 session_set_send_data_notify(session, send_send_data_finished, send_dialog);
376
377         } else {
378                 /* everything is ready to start at MAIL FROM:, just
379                  * reinit useful variables. 
380                  */
381                 session = SESSION(ac_prefs->session);
382                 ac_prefs->session = NULL;
383                 smtp_session = SMTP_SESSION(session);
384                 smtp_session->state = SMTP_HELO;
385                 send_dialog = (SendProgressDialog *)smtp_session->dialog;
386                 was_inited = TRUE;
387         }
388
389         /* This has to be initialised for every mail sent */
390         smtp_session->from = g_strdup(spec_from);
391         smtp_session->to_list = to_list;
392         smtp_session->cur_to = to_list;
393         smtp_session->send_data = (guchar *)get_outgoing_rfc2822_str(fp);
394         smtp_session->send_data_len = strlen((gchar *)smtp_session->send_data);
395
396         session_set_timeout(session,
397                             prefs_common.io_timeout_secs * 1000);
398         /* connect if necessary */
399         if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
400                 session_destroy(session);
401                 send_progress_dialog_destroy(send_dialog);
402                 ac_prefs->session = NULL;
403                 return -1;
404         }
405
406         debug_print("send_message_smtp(): begin event loop\n");
407
408         if (was_inited) {
409                 /* as the server is quiet, start sending ourselves */
410                 smtp_from(smtp_session);
411         }
412
413         while (session_is_running(session) && send_dialog->cancelled == FALSE
414                 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
415                 gtk_main_iteration();
416
417         if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
418                 if (ac_prefs->session_smtp_passwd) {
419                         g_free(ac_prefs->session_smtp_passwd);
420                         ac_prefs->session_smtp_passwd = NULL;
421                 }
422                 ret = -1;
423         } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
424                 log_message(LOG_PROTOCOL, "%s\n", _("Mail sent successfully."));
425                 ret = 0;
426         } else if (session->state == SESSION_EOF &&
427                    SMTP_SESSION(session)->state == SMTP_QUIT) {
428                 /* consider EOF right after QUIT successful */
429                 log_warning(LOG_PROTOCOL, "%s\n", _("Connection closed by the remote host."));
430                 ret = 0;
431         } else if (session->state == SESSION_ERROR ||
432                    session->state == SESSION_EOF ||
433                    session->state == SESSION_TIMEOUT ||
434                    SMTP_SESSION(session)->state == SMTP_ERROR ||
435                    SMTP_SESSION(session)->error_val != SM_OK)
436                 ret = -1;
437         else if (send_dialog->cancelled == TRUE)
438                 ret = -1;
439
440         if (ret == -1) {
441                 manage_window_focus_in(send_dialog->dialog->window, NULL, NULL);
442                 send_put_error(session);
443                 manage_window_focus_out(send_dialog->dialog->window, NULL, NULL);
444         }
445
446         /* if we should close the connection, let's do it.
447          * Close it in case of error, too, as it helps reinitializing things
448          * easier.
449          */
450         if (!keep_session || ret != 0) {
451                 if (session_is_connected(session))
452                         smtp_quit(smtp_session);
453                 while (session_is_connected(session) && !send_dialog->cancelled)
454                         gtk_main_iteration();
455                 session_destroy(session);
456                 ac_prefs->session = NULL;
457                 send_progress_dialog_destroy(send_dialog);
458         } else {
459                 g_free(smtp_session->from);
460                 g_free(smtp_session->send_data);
461                 g_free(smtp_session->error_msg);
462         }
463         if (keep_session && ret == 0 && ac_prefs->session == NULL)
464                 ac_prefs->session = SMTP_SESSION(session);
465
466
467         statuswindow_pop_all();
468         statusbar_verbosity_set(FALSE);
469         return ret;
470 }
471
472 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
473 {
474         return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
475 }
476
477 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
478 {
479         gchar buf[BUFFSIZE];
480         SMTPSession *smtp_session = SMTP_SESSION(session);
481         SendProgressDialog *dialog = (SendProgressDialog *)data;
482         gchar *state_str = NULL;
483
484         cm_return_val_if_fail(dialog != NULL, -1);
485
486         switch (smtp_session->state) {
487         case SMTP_READY:
488                 return 0;
489         case SMTP_HELO:
490                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
491                 state_str = _("Authenticating");
492                 statuswindow_print_all(_("Sending message..."));
493                 break;
494         case SMTP_EHLO:
495                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
496                 state_str = _("Authenticating");
497                 statuswindow_print_all(_("Sending message..."));
498                 break;
499         case SMTP_AUTH:
500                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
501                 state_str = _("Authenticating");
502                 break;
503         case SMTP_FROM:
504                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
505                 state_str = _("Sending");
506                 break;
507         case SMTP_RCPT:
508                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
509                 state_str = _("Sending");
510                 break;
511         case SMTP_DATA:
512         case SMTP_EOM:
513                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
514                 state_str = _("Sending");
515                 break;
516         case SMTP_QUIT:
517                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
518                 state_str = _("Quitting");
519                 break;
520         case SMTP_ERROR:
521                 g_warning("send: error: %s", msg);
522                 return 0;
523         default:
524                 return 0;
525         }
526
527         progress_dialog_set_label(dialog->dialog, buf);
528         progress_dialog_list_set_status(dialog->dialog, 0, state_str);
529
530         return 0;
531 }
532
533 static gint send_send_data_progressive(Session *session, guint cur_len,
534                                        guint total_len, gpointer data)
535 {
536         gchar buf[BUFFSIZE];
537         SendProgressDialog *dialog = (SendProgressDialog *)data;
538         MainWindow *mainwin = mainwindow_get_mainwindow();
539         
540         cm_return_val_if_fail(dialog != NULL, -1);
541
542         if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
543             SMTP_SESSION(session)->state != SMTP_EOM)
544                 return 0;
545
546         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
547                    cur_len, total_len);
548         progress_dialog_set_label(dialog->dialog, buf);
549         progress_dialog_set_fraction
550                 (dialog->dialog, (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
551
552         if (mainwin) {
553                 if (!gtk_widget_get_visible(mainwin->progressbar))      
554                         gtk_widget_show(mainwin->progressbar);
555                 gtk_progress_bar_set_fraction
556                         (GTK_PROGRESS_BAR(mainwin->progressbar),
557                          (total_len == 0) ? 0 : (gfloat)cur_len / (gfloat)total_len);
558         }
559
560         return 0;
561 }
562
563 static gint send_send_data_finished(Session *session, guint len, gpointer data)
564 {
565         SendProgressDialog *dialog = (SendProgressDialog *)data;
566         MainWindow *mainwin = mainwindow_get_mainwindow();
567
568         cm_return_val_if_fail(dialog != NULL, -1);
569
570         send_send_data_progressive(session, len, len, dialog);
571         if (mainwin) {
572                 gtk_widget_hide(mainwin->progressbar);
573                 gtk_progress_bar_set_fraction
574                         (GTK_PROGRESS_BAR(mainwin->progressbar),(gfloat)0);
575         }
576
577         return 0;
578 }
579
580 static void send_progress_dialog_size_allocate_cb(GtkWidget *widget,
581                                          GtkAllocation *allocation)
582 {
583         cm_return_if_fail(allocation != NULL);
584
585         prefs_common.sendwin_width = allocation->width;
586         prefs_common.sendwin_height = allocation->height;
587 }
588
589 static SendProgressDialog *send_progress_dialog_create(void)
590 {
591         SendProgressDialog *dialog;
592         ProgressDialog *progress;
593         static GdkGeometry geometry;
594
595         dialog = g_new0(SendProgressDialog, 1);
596
597         progress = progress_dialog_create();
598         gtk_window_set_title(GTK_WINDOW(progress->window),
599                              _("Sending message"));
600         g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
601                          G_CALLBACK(send_showlog_button_cb), dialog);
602         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
603                          G_CALLBACK(send_cancel_button_cb), dialog);
604         g_signal_connect(G_OBJECT(progress->window), "delete_event",
605                          G_CALLBACK(gtk_true), NULL);
606         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
607         g_signal_connect(G_OBJECT(progress->window), "size_allocate",
608                          G_CALLBACK(send_progress_dialog_size_allocate_cb), NULL);
609         manage_window_set_transient(GTK_WINDOW(progress->window));
610
611         progress_dialog_get_fraction(progress);
612
613         if (!geometry.min_height) {
614                 geometry.min_width = 460;
615                 geometry.min_height = 250;
616         }
617
618         gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
619                                       GDK_HINT_MIN_SIZE);
620         gtk_widget_set_size_request(progress->window, prefs_common.sendwin_width,
621                                     prefs_common.sendwin_height);
622
623         if (!prefs_common.send_dialog_invisible) {
624                 gtk_widget_show_now(progress->window);
625         }
626         
627         dialog->dialog = progress;
628
629         return dialog;
630 }
631
632 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
633 {
634         cm_return_if_fail(dialog != NULL);
635         if (!prefs_common.send_dialog_invisible) {
636                 progress_dialog_destroy(dialog->dialog);
637         }
638         g_free(dialog);
639         send_dialog = NULL;
640 }
641
642 static void send_showlog_button_cb(GtkWidget *widget, gpointer data)
643 {
644         MainWindow *mainwin = mainwindow_get_mainwindow();
645
646         log_window_show(mainwin->logwin);
647 }
648
649 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
650 {
651         SendProgressDialog *dialog = (SendProgressDialog *)data;
652         statusbar_progress_all(0,0,0);
653
654         dialog->cancelled = TRUE;
655 }
656
657 static void send_put_error(Session *session)
658 {
659         gchar *msg;
660         gchar *log_msg = NULL;
661         gchar *err_msg = NULL;
662
663         msg = SMTP_SESSION(session)->error_msg;
664
665         switch (SMTP_SESSION(session)->error_val) {
666         case SM_ERROR:
667         case SM_UNRECOVERABLE:
668                 log_msg = _("Error occurred while sending the message.");
669                 if (msg)
670                         err_msg = g_strdup_printf
671                                 (_("Error occurred while sending the message:\n%s"),
672                                  msg);
673                 else
674                         err_msg = g_strdup(log_msg);
675                 break;
676         case SM_AUTHFAIL:
677                 log_msg = _("Authentication failed.");
678                 if (msg)
679                         err_msg = g_strdup_printf
680                                 (_("Authentication failed:\n%s"), msg);
681                 else
682                         err_msg = g_strdup(log_msg);
683                 break;
684         default:
685                 switch (session->state) {
686                 case SESSION_ERROR:
687                         log_msg =
688                                 _("Error occurred while sending the message.");
689                         err_msg = g_strdup(log_msg);
690                         break;
691                 case SESSION_EOF:
692                         log_msg = _("Connection closed by the remote host.");
693                         err_msg = g_strdup(log_msg);
694                         break;
695                 case SESSION_TIMEOUT:
696                         log_msg = _("Session timed out. You may be able to "
697                                     "recover by increasing the timeout value in "
698                                     "Preferences/Other/Miscellaneous.");
699                         err_msg = g_strdup(log_msg);
700                         break;
701                 default:
702                         break;
703                 }
704                 break;
705         }
706
707         if (err_msg) {
708                 log_error(LOG_PROTOCOL, "%s\n", err_msg);
709                 g_free(err_msg);
710         } else {
711                 if (log_msg)
712                         log_warning(LOG_PROTOCOL, "%s\n", log_msg);
713         }
714 }
715