2005-06-01 [colin] 1.9.11cvs31
[claws.git] / src / send_message.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 <gtk/gtkclist.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <signal.h>
35
36 #include "send_message.h"
37 #include "session.h"
38 #include "ssl.h"
39 #include "smtp.h"
40 #include "prefs_common.h"
41 #include "prefs_account.h"
42 #include "procheader.h"
43 #include "account.h"
44 #include "progressdialog.h"
45 #include "statusbar.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
48 #include "manage_window.h"
49 #include "socket.h"
50 #include "utils.h"
51 #include "gtkutils.h"
52 #include "inc.h"
53 #include "log.h"
54
55 typedef struct _SendProgressDialog      SendProgressDialog;
56
57 struct _SendProgressDialog
58 {
59         ProgressDialog *dialog;
60         Session *session;
61         gboolean cancelled;
62 };
63
64 static gint send_recv_message           (Session                *session,
65                                          const gchar            *msg,
66                                          gpointer                data);
67 static gint send_send_data_progressive  (Session                *session,
68                                          guint                   cur_len,
69                                          guint                   total_len,
70                                          gpointer                data);
71 static gint send_send_data_finished     (Session                *session,
72                                          guint                   len,
73                                          gpointer                data);
74
75 static SendProgressDialog *send_progress_dialog_create(void);
76 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
77
78 static void send_cancel_button_cb       (GtkWidget      *widget,
79                                          gpointer        data);
80
81 static void send_put_error              (Session        *session);
82
83
84 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
85 {
86         FILE *fp;
87         gint val;
88
89         g_return_val_if_fail(file != NULL, -1);
90         g_return_val_if_fail(ac_prefs != NULL, -1);
91         g_return_val_if_fail(to_list != NULL, -1);
92
93         if ((fp = fopen(file, "rb")) == NULL) {
94                 FILE_OP_ERROR(file, "fopen");
95                 return -1;
96         }
97
98         if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
99             (*ac_prefs->mail_command)) {
100                 val = send_message_local(ac_prefs->mail_command, fp);
101                 fclose(fp);
102                 return val;
103         } else {
104                 val = send_message_smtp(ac_prefs, to_list, fp);
105                 
106                 fclose(fp);
107                 return val;
108         }
109 }
110
111 enum
112 {
113         Q_SENDER     = 0,
114         Q_SMTPSERVER = 1,
115         Q_RECIPIENTS = 2,
116         Q_ACCOUNT_ID = 3
117 };
118
119 gint send_message_local(const gchar *command, FILE *fp)
120 {
121         gchar **argv;
122         GPid pid;
123         gint child_stdin;
124         gchar buf[BUFFSIZE];
125         gboolean err = FALSE;
126
127         g_return_val_if_fail(command != NULL, -1);
128         g_return_val_if_fail(fp != NULL, -1);
129
130         log_message(_("Sending message using command: %s\n"), command);
131
132         argv = strsplit_with_quote(command, " ", 0);
133
134         if (g_spawn_async_with_pipes(NULL, argv, NULL, 0, NULL, NULL, &pid,
135                                      &child_stdin, NULL, NULL, NULL) == FALSE) {
136                 g_snprintf(buf, sizeof(buf),
137                            _("Can't execute command: %s"), command);
138                 log_warning("%s\n", buf);
139                 alertpanel_error("%s", buf);
140                 g_strfreev(argv);
141                 return -1;
142         }
143         g_strfreev(argv);
144
145         while (fgets(buf, sizeof(buf), fp) != NULL) {
146                 strretchomp(buf);
147                 if (buf[0] == '.' && buf[1] == '\0') {
148                         if (fd_write_all(child_stdin, ".", 1) < 0) {
149                                 err = TRUE;
150                                 break;
151                         }
152                 }
153                 if (fd_write_all(child_stdin, buf, strlen(buf)) < 0 ||
154                     fd_write_all(child_stdin, "\n", 1) < 0) {
155                         err = TRUE;
156                         break;
157                 }
158         }
159
160         fd_close(child_stdin);
161         g_spawn_close_pid(pid);
162
163         if (err) {
164                 g_snprintf(buf, sizeof(buf),
165                            _("Error occurred while executing command: %s"),
166                            command);
167                 log_warning("%s\n", buf);
168                 alertpanel_error("%s", buf);
169                 return -1;
170         }
171
172         return 0;
173 }
174
175 gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session)
176 {
177         Session *session;
178         SMTPSession *smtp_session;
179         gushort port = 0;
180         SendProgressDialog *dialog;
181         gchar buf[BUFFSIZE];
182         gint ret = 0;
183         gboolean was_inited = FALSE;
184
185         g_return_val_if_fail(ac_prefs != NULL, -1);
186         g_return_val_if_fail(ac_prefs->address != NULL, -1);
187         g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
188         g_return_val_if_fail(to_list != NULL, -1);
189         g_return_val_if_fail(fp != NULL, -1);
190
191         if (!ac_prefs->session) {
192                 /* we can't reuse a previously initialised session */
193                 session = smtp_session_new();
194                 smtp_session = SMTP_SESSION(session);
195
196                 smtp_session->hostname =
197                         ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
198
199                 if (ac_prefs->use_smtp_auth) {
200                         smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
201                         if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
202                                 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
203                                 if (ac_prefs->smtp_passwd)
204                                         smtp_session->pass =
205                                                 g_strdup(ac_prefs->smtp_passwd);
206                                 else if (ac_prefs->tmp_smtp_pass)
207                                         smtp_session->pass =
208                                                 g_strdup(ac_prefs->tmp_smtp_pass);
209                                 else {
210                                         smtp_session->pass =
211                                                 input_dialog_query_password
212                                                         (ac_prefs->smtp_server,
213                                                          smtp_session->user);
214                                         if (!smtp_session->pass)
215                                                 smtp_session->pass = g_strdup("");
216                                         ac_prefs->tmp_smtp_pass =
217                                                 g_strdup(smtp_session->pass);
218                                 }
219                         } else {
220                                 smtp_session->user = g_strdup(ac_prefs->userid);
221                                 if (ac_prefs->passwd)
222                                         smtp_session->pass = g_strdup(ac_prefs->passwd);
223                                 else if (ac_prefs->tmp_pass)
224                                         smtp_session->pass =
225                                                 g_strdup(ac_prefs->tmp_pass);
226                                 else {
227                                         smtp_session->pass =
228                                                 input_dialog_query_password
229                                                         (ac_prefs->smtp_server,
230                                                          smtp_session->user);
231                                         if (!smtp_session->pass)
232                                                 smtp_session->pass = g_strdup("");
233                                         ac_prefs->tmp_pass =
234                                                 g_strdup(smtp_session->pass);
235                                 }
236                         }
237                 } else {
238                         smtp_session->user = NULL;
239                         smtp_session->pass = NULL;
240                 }
241
242         #if USE_OPENSSL
243                 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
244                         ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
245                 session->ssl_type = ac_prefs->ssl_smtp;
246                 if (ac_prefs->ssl_smtp != SSL_NONE)
247                         session->nonblocking = ac_prefs->use_nonblocking_ssl;
248         #else
249                 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
250         #endif
251
252                 dialog = send_progress_dialog_create();
253                 dialog->session = session;
254                 smtp_session->dialog = dialog;
255
256                 progress_dialog_list_set(dialog->dialog, 0, NULL, 
257                                          ac_prefs->smtp_server, 
258                                          _("Connecting"));
259
260                 if (ac_prefs->pop_before_smtp
261                     && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
262                     && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
263                         g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
264                         log_message(buf);
265                         progress_dialog_set_label(dialog->dialog, buf);
266                         progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
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_list_set_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_fraction
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_get_fraction(progress);
481
482         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
483                 gtk_widget_show_now(progress->window);
484         }
485         
486         dialog->dialog = progress;
487
488         return dialog;
489 }
490
491 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
492 {
493         g_return_if_fail(dialog != NULL);
494         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
495                 progress_dialog_destroy(dialog->dialog);
496         }
497         g_free(dialog);
498 }
499
500 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
501 {
502         SendProgressDialog *dialog = (SendProgressDialog *)data;
503
504         dialog->cancelled = TRUE;
505 }
506
507 static void send_put_error(Session *session)
508 {
509         gchar *msg;
510         gchar *log_msg = NULL;
511         gchar *err_msg = NULL;
512
513         msg = SMTP_SESSION(session)->error_msg;
514
515         switch (SMTP_SESSION(session)->error_val) {
516         case SM_ERROR:
517         case SM_UNRECOVERABLE:
518                 log_msg = _("Error occurred while sending the message.");
519                 if (msg)
520                         err_msg = g_strdup_printf
521                                 (_("Error occurred while sending the message:\n%s"),
522                                  msg);
523                 else
524                         err_msg = g_strdup(log_msg);
525                 break;
526         case SM_AUTHFAIL:
527                 log_msg = _("Authentication failed.");
528                 if (msg)
529                         err_msg = g_strdup_printf
530                                 (_("Authentication failed:\n%s"), msg);
531                 else
532                         err_msg = g_strdup(log_msg);
533                 break;
534         default:
535                 switch (session->state) {
536                 case SESSION_ERROR:
537                         log_msg =
538                                 _("Error occurred while sending the message.");
539                         err_msg = g_strdup(log_msg);
540                         break;
541                 case SESSION_EOF:
542                         log_msg = _("Connection closed by the remote host.");
543                         err_msg = g_strdup(log_msg);
544                         break;
545                 case SESSION_TIMEOUT:
546                         log_msg = _("Session timed out.");
547                         err_msg = g_strdup(log_msg);
548                         break;
549                 default:
550                         break;
551                 }
552                 break;
553         }
554
555         if (log_msg)
556                 log_warning("%s\n", log_msg);
557         if (err_msg) {
558                 alertpanel_error_log("%s", err_msg);
559                 g_free(err_msg);
560         }
561 }
562