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