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