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