2005-06-15 [paul] 1.9.11cvs71
[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 <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_list_set(dialog->dialog, 0, NULL, 
256                                          ac_prefs->smtp_server, 
257                                          _("Connecting"));
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(buf);
264                         progress_dialog_set_label(dialog->dialog, buf);
265                         progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
266                         GTK_EVENTS_FLUSH();
267                         inc_pop_before_smtp(ac_prefs);
268                 }
269
270                 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
271                            ac_prefs->smtp_server);
272                 progress_dialog_set_label(dialog->dialog, buf);
273                 log_message("%s\n", buf);
274
275                 session_set_recv_message_notify(session, send_recv_message, dialog);
276                 session_set_send_data_progressive_notify
277                         (session, send_send_data_progressive, dialog);
278                 session_set_send_data_notify(session, send_send_data_finished, dialog);
279
280                 ac_prefs->session = SMTP_SESSION(session);
281         } else {
282                 /* everything is ready to start at MAIL FROM:, just
283                  * reinit useful variables. 
284                  */
285                 session = SESSION(ac_prefs->session);
286                 smtp_session = SMTP_SESSION(session);
287                 smtp_session->state = SMTP_HELO;
288                 dialog = (SendProgressDialog *)smtp_session->dialog;
289                 was_inited = TRUE;
290         }
291
292         /* This has to be initialised for every mail sent */
293         smtp_session->from = g_strdup(ac_prefs->address);
294         smtp_session->to_list = to_list;
295         smtp_session->cur_to = to_list;
296         smtp_session->send_data = get_outgoing_rfc2822_str(fp);
297         smtp_session->send_data_len = strlen(smtp_session->send_data);
298
299         /* connect if necessary */
300         if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) {
301                 session_destroy(session);
302                 send_progress_dialog_destroy(dialog);
303                 return -1;
304         }
305
306         debug_print("send_message_smtp(): begin event loop\n");
307
308         if (was_inited) {
309                 /* as the server is quiet, start sending ourselves */
310                 smtp_from(smtp_session);
311         }
312
313         while (session_is_connected(session) && dialog->cancelled == FALSE
314                 && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK)
315                 gtk_main_iteration();
316
317         if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
318                 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
319                         g_free(ac_prefs->tmp_smtp_pass);
320                         ac_prefs->tmp_smtp_pass = NULL;
321                 }
322                 ret = -1;
323         } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) {
324                 log_message("%s\n", _("Mail sent successfully."));
325                 ret = 0;
326         } else if (session->state == SESSION_EOF &&
327                    SMTP_SESSION(session)->state == SMTP_QUIT) {
328                 /* consider EOF right after QUIT successful */
329                 log_warning("%s\n", _("Connection closed by the remote host."));
330                 ret = 0;
331         } else if (session->state == SESSION_ERROR ||
332                    session->state == SESSION_EOF ||
333                    session->state == SESSION_TIMEOUT ||
334                    SMTP_SESSION(session)->state == SMTP_ERROR ||
335                    SMTP_SESSION(session)->error_val != SM_OK)
336                 ret = -1;
337         else if (dialog->cancelled == TRUE)
338                 ret = -1;
339
340         if (ret == -1) {
341                 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
342                 send_put_error(session);
343                 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
344         }
345
346         /* if we should close the connection, let's do it.
347          * Close it in case of error, too, as it helps reinitializing things
348          * easier.
349          */
350         if (!keep_session || ret != 0) {
351                 if (session_is_connected(session))
352                         smtp_quit(smtp_session);
353                 while (session_is_connected(session))
354                         gtk_main_iteration();
355                 session_destroy(session);
356                 ac_prefs->session = NULL;
357                 send_progress_dialog_destroy(dialog);
358         } else {
359                 g_free(smtp_session->from);
360                 g_free(smtp_session->send_data);
361                 g_free(smtp_session->error_msg);
362         }
363
364         statusbar_pop_all();
365         statusbar_verbosity_set(FALSE);
366         return ret;
367 }
368
369 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
370 {
371         return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
372 }
373
374 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
375 {
376         gchar buf[BUFFSIZE];
377         SMTPSession *smtp_session = SMTP_SESSION(session);
378         SendProgressDialog *dialog = (SendProgressDialog *)data;
379         gchar *state_str = NULL;
380
381         g_return_val_if_fail(dialog != NULL, -1);
382
383         switch (smtp_session->state) {
384         case SMTP_READY:
385         case SMTP_CONNECTED:
386                 return 0;
387         case SMTP_HELO:
388                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
389                 state_str = _("Authenticating");
390                 statusbar_print_all(_("Sending message..."));
391                 break;
392         case SMTP_EHLO:
393                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
394                 state_str = _("Authenticating");
395                 statusbar_print_all(_("Sending message..."));
396                 break;
397         case SMTP_AUTH:
398                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
399                 state_str = _("Authenticating");
400                 break;
401         case SMTP_FROM:
402                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
403                 state_str = _("Sending");
404                 break;
405         case SMTP_RCPT:
406                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
407                 state_str = _("Sending");
408                 break;
409         case SMTP_DATA:
410         case SMTP_EOM:
411                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
412                 state_str = _("Sending");
413                 break;
414         case SMTP_QUIT:
415                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
416                 state_str = _("Quitting");
417                 break;
418         case SMTP_ERROR:
419                 g_warning("send: error: %s\n", msg);
420                 return 0;
421         default:
422                 return 0;
423         }
424
425         progress_dialog_set_label(dialog->dialog, buf);
426         progress_dialog_list_set_status(dialog->dialog, 0, state_str);
427
428         return 0;
429 }
430
431 static gint send_send_data_progressive(Session *session, guint cur_len,
432                                        guint total_len, gpointer data)
433 {
434         gchar buf[BUFFSIZE];
435         SendProgressDialog *dialog = (SendProgressDialog *)data;
436
437         g_return_val_if_fail(dialog != NULL, -1);
438
439         if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
440             SMTP_SESSION(session)->state != SMTP_EOM)
441                 return 0;
442
443         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
444                    cur_len, total_len);
445         progress_dialog_set_label(dialog->dialog, buf);
446         progress_dialog_set_fraction
447                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
448
449         return 0;
450 }
451
452 static gint send_send_data_finished(Session *session, guint len, gpointer data)
453 {
454         SendProgressDialog *dialog = (SendProgressDialog *)data;
455
456         g_return_val_if_fail(dialog != NULL, -1);
457
458         send_send_data_progressive(session, len, len, dialog);
459         return 0;
460 }
461
462 static SendProgressDialog *send_progress_dialog_create(void)
463 {
464         SendProgressDialog *dialog;
465         ProgressDialog *progress;
466
467         dialog = g_new0(SendProgressDialog, 1);
468
469         progress = progress_dialog_create();
470         gtk_window_set_title(GTK_WINDOW(progress->window),
471                              _("Sending message"));
472         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
473                          G_CALLBACK(send_cancel_button_cb), dialog);
474         g_signal_connect(G_OBJECT(progress->window), "delete_event",
475                          G_CALLBACK(gtk_true), NULL);
476         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
477         manage_window_set_transient(GTK_WINDOW(progress->window));
478
479         progress_dialog_get_fraction(progress);
480
481         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
482                 gtk_widget_show_now(progress->window);
483         }
484         
485         dialog->dialog = progress;
486
487         return dialog;
488 }
489
490 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
491 {
492         g_return_if_fail(dialog != NULL);
493         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
494                 progress_dialog_destroy(dialog->dialog);
495         }
496         g_free(dialog);
497 }
498
499 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
500 {
501         SendProgressDialog *dialog = (SendProgressDialog *)data;
502
503         dialog->cancelled = TRUE;
504 }
505
506 static void send_put_error(Session *session)
507 {
508         gchar *msg;
509         gchar *log_msg = NULL;
510         gchar *err_msg = NULL;
511
512         msg = SMTP_SESSION(session)->error_msg;
513
514         switch (SMTP_SESSION(session)->error_val) {
515         case SM_ERROR:
516         case SM_UNRECOVERABLE:
517                 log_msg = _("Error occurred while sending the message.");
518                 if (msg)
519                         err_msg = g_strdup_printf
520                                 (_("Error occurred while sending the message:\n%s"),
521                                  msg);
522                 else
523                         err_msg = g_strdup(log_msg);
524                 break;
525         case SM_AUTHFAIL:
526                 log_msg = _("Authentication failed.");
527                 if (msg)
528                         err_msg = g_strdup_printf
529                                 (_("Authentication failed:\n%s"), msg);
530                 else
531                         err_msg = g_strdup(log_msg);
532                 break;
533         default:
534                 switch (session->state) {
535                 case SESSION_ERROR:
536                         log_msg =
537                                 _("Error occurred while sending the message.");
538                         err_msg = g_strdup(log_msg);
539                         break;
540                 case SESSION_EOF:
541                         log_msg = _("Connection closed by the remote host.");
542                         err_msg = g_strdup(log_msg);
543                         break;
544                 case SESSION_TIMEOUT:
545                         log_msg = _("Session timed out.");
546                         err_msg = g_strdup(log_msg);
547                         break;
548                 default:
549                         break;
550                 }
551                 break;
552         }
553
554         if (log_msg)
555                 log_warning("%s\n", log_msg);
556         if (err_msg) {
557                 alertpanel_error_log("%s", err_msg);
558                 g_free(err_msg);
559         }
560 }
561