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