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