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