5681567376c501778b93dd69577a186eddb34016
[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                 smtp_quit(session);
337                 while (session_is_connected(session))
338                         gtk_main_iteration();
339                 session_destroy(session);
340                 ac_prefs->session = NULL;
341                 send_progress_dialog_destroy(dialog);
342         } else {
343                 g_free(smtp_session->from);
344                 g_free(smtp_session->send_data);
345                 g_free(smtp_session->error_msg);
346         }
347
348         statusbar_pop_all();
349         statusbar_verbosity_set(FALSE);
350         return ret;
351 }
352
353 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
354 {
355         return send_message_smtp_full(ac_prefs, to_list, fp, FALSE);
356 }
357
358 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
359 {
360         gchar buf[BUFFSIZE];
361         SMTPSession *smtp_session = SMTP_SESSION(session);
362         SendProgressDialog *dialog = (SendProgressDialog *)data;
363         gchar *state_str = NULL;
364
365         g_return_val_if_fail(dialog != NULL, -1);
366
367         switch (smtp_session->state) {
368         case SMTP_READY:
369         case SMTP_CONNECTED:
370                 return 0;
371         case SMTP_HELO:
372                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
373                 state_str = _("Authenticating");
374                 statusbar_print_all(_("Sending message..."));
375                 break;
376         case SMTP_EHLO:
377                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
378                 state_str = _("Authenticating");
379                 statusbar_print_all(_("Sending message..."));
380                 break;
381         case SMTP_AUTH:
382                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
383                 state_str = _("Authenticating");
384                 break;
385         case SMTP_FROM:
386                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
387                 state_str = _("Sending");
388                 break;
389         case SMTP_RCPT:
390                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
391                 state_str = _("Sending");
392                 break;
393         case SMTP_DATA:
394         case SMTP_EOM:
395                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
396                 state_str = _("Sending");
397                 break;
398         case SMTP_QUIT:
399                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
400                 state_str = _("Quitting");
401                 break;
402         case SMTP_ERROR:
403                 g_warning("send: error: %s\n", msg);
404                 return 0;
405         default:
406                 return 0;
407         }
408
409         progress_dialog_set_label(dialog->dialog, buf);
410         progress_dialog_list_set_status(dialog->dialog, 0, state_str);
411
412         return 0;
413 }
414
415 static gint send_send_data_progressive(Session *session, guint cur_len,
416                                        guint total_len, gpointer data)
417 {
418         gchar buf[BUFFSIZE];
419         SendProgressDialog *dialog = (SendProgressDialog *)data;
420
421         g_return_val_if_fail(dialog != NULL, -1);
422
423         if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
424             SMTP_SESSION(session)->state != SMTP_EOM)
425                 return 0;
426
427         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
428                    cur_len, total_len);
429         progress_dialog_set_label(dialog->dialog, buf);
430         progress_dialog_set_fraction
431                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
432
433         return 0;
434 }
435
436 static gint send_send_data_finished(Session *session, guint len, gpointer data)
437 {
438         SendProgressDialog *dialog = (SendProgressDialog *)data;
439
440         g_return_val_if_fail(dialog != NULL, -1);
441
442         send_send_data_progressive(session, len, len, dialog);
443         return 0;
444 }
445
446 static SendProgressDialog *send_progress_dialog_create(void)
447 {
448         SendProgressDialog *dialog;
449         ProgressDialog *progress;
450
451         dialog = g_new0(SendProgressDialog, 1);
452
453         progress = progress_dialog_create();
454         gtk_window_set_title(GTK_WINDOW(progress->window),
455                              _("Sending message"));
456         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
457                          G_CALLBACK(send_cancel_button_cb), dialog);
458         g_signal_connect(G_OBJECT(progress->window), "delete_event",
459                          G_CALLBACK(gtk_true), NULL);
460         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
461         manage_window_set_transient(GTK_WINDOW(progress->window));
462
463         progress_dialog_get_fraction(progress);
464
465         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
466                 gtk_widget_show_now(progress->window);
467         }
468         
469         dialog->dialog = progress;
470
471         return dialog;
472 }
473
474 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
475 {
476         g_return_if_fail(dialog != NULL);
477         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
478                 progress_dialog_destroy(dialog->dialog);
479         }
480         g_free(dialog);
481 }
482
483 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
484 {
485         SendProgressDialog *dialog = (SendProgressDialog *)data;
486
487         dialog->cancelled = TRUE;
488 }
489
490 static void send_put_error(Session *session)
491 {
492         gchar *msg;
493         gchar *log_msg = NULL;
494         gchar *err_msg = NULL;
495
496         msg = SMTP_SESSION(session)->error_msg;
497
498         switch (SMTP_SESSION(session)->error_val) {
499         case SM_ERROR:
500         case SM_UNRECOVERABLE:
501                 log_msg = _("Error occurred while sending the message.");
502                 if (msg)
503                         err_msg = g_strdup_printf
504                                 (_("Error occurred while sending the message:\n%s"),
505                                  msg);
506                 else
507                         err_msg = g_strdup(log_msg);
508                 break;
509         case SM_AUTHFAIL:
510                 log_msg = _("Authentication failed.");
511                 if (msg)
512                         err_msg = g_strdup_printf
513                                 (_("Authentication failed:\n%s"), msg);
514                 else
515                         err_msg = g_strdup(log_msg);
516                 break;
517         default:
518                 switch (session->state) {
519                 case SESSION_ERROR:
520                         log_msg =
521                                 _("Error occurred while sending the message.");
522                         err_msg = g_strdup(log_msg);
523                         break;
524                 case SESSION_EOF:
525                         log_msg = _("Connection closed by the remote host.");
526                         err_msg = g_strdup(log_msg);
527                         break;
528                 case SESSION_TIMEOUT:
529                         log_msg = _("Session timed out.");
530                         err_msg = g_strdup(log_msg);
531                         break;
532                 default:
533                         break;
534                 }
535                 break;
536         }
537
538         if (log_msg)
539                 log_warning("%s\n", log_msg);
540         if (err_msg) {
541                 alertpanel_error_log("%s", err_msg);
542                 g_free(err_msg);
543         }
544 }
545