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