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