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