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