2005-01-29 [paul] 1.0.0cvs23.2
[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 <gtk/gtkmain.h>
28 #include <gtk/gtksignal.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkclist.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <signal.h>
34
35 #include "intl.h"
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 #if 0
63 static gint send_message_local          (const gchar            *command,
64                                          FILE                   *fp);
65 static gint send_message_smtp           (PrefsAccount           *ac_prefs,
66                                          GSList                 *to_list,
67                                          FILE                   *fp);
68 #endif
69
70 static gint send_recv_message           (Session                *session,
71                                          const gchar            *msg,
72                                          gpointer                data);
73 static gint send_send_data_progressive  (Session                *session,
74                                          guint                   cur_len,
75                                          guint                   total_len,
76                                          gpointer                data);
77 static gint send_send_data_finished     (Session                *session,
78                                          guint                   len,
79                                          gpointer                data);
80
81 static SendProgressDialog *send_progress_dialog_create(void);
82 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
83
84 static void send_cancel_button_cb       (GtkWidget      *widget,
85                                          gpointer        data);
86
87 static void send_put_error              (Session        *session);
88
89
90 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
91 {
92         FILE *fp;
93         gint val;
94
95         g_return_val_if_fail(file != NULL, -1);
96         g_return_val_if_fail(ac_prefs != NULL, -1);
97         g_return_val_if_fail(to_list != NULL, -1);
98
99         if ((fp = fopen(file, "rb")) == NULL) {
100                 FILE_OP_ERROR(file, "fopen");
101                 return -1;
102         }
103
104         if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
105             (*ac_prefs->mail_command)) {
106                 val = send_message_local(ac_prefs->mail_command, fp);
107                 fclose(fp);
108                 return val;
109         } else {
110                 val = send_message_smtp(ac_prefs, to_list, fp);
111                 
112                 fclose(fp);
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 #if 0
126 gint send_message_queue(const gchar *file)
127 {
128         static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
129                                        {"SSV:", NULL, FALSE},
130                                        {"R:",   NULL, FALSE},
131                                        {"AID:", NULL, FALSE},
132                                        {NULL,   NULL, FALSE}};
133         FILE *fp;
134         gint val = 0;
135         gchar *from = NULL;
136         gchar *server = NULL;
137         GSList *to_list = NULL;
138         gchar buf[BUFFSIZE];
139         gint hnum;
140         glong fpos;
141         PrefsAccount *ac = NULL, *mailac = NULL, *newsac = NULL;
142
143         g_return_val_if_fail(file != NULL, -1);
144
145         if ((fp = fopen(file, "rb")) == NULL) {
146                 FILE_OP_ERROR(file, "fopen");
147                 return -1;
148         }
149
150         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
151                != -1) {
152                 gchar *p;
153
154                 p = buf + strlen(qentry[hnum].name);
155
156                 switch (hnum) {
157                 case Q_SENDER:
158                         if (!from) from = g_strdup(p);
159                         break;
160                 case Q_SMTPSERVER:
161                         if (!server) server = g_strdup(p);
162                         break;
163                 case Q_RECIPIENTS:
164                         to_list = address_list_append(to_list, p);
165                         break;
166                 case Q_ACCOUNT_ID:
167                         ac = account_find_from_id(atoi(p));
168                         break;
169                 default:
170                         break;
171                 }
172         }
173
174         if (((!ac || (ac && ac->protocol != A_NNTP)) && !to_list) || !from) {
175                 g_warning("Queued message header is broken.\n");
176                 val = -1;
177         } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
178                 val = send_message_local(prefs_common.extsend_cmd, fp);
179         } else {
180                 if (ac && ac->protocol == A_NNTP) {
181                         newsac = ac;
182
183                         /* search mail account */
184                         mailac = account_find_from_address(from);
185                         if (!mailac) {
186                                 if (cur_account &&
187                                     cur_account->protocol != A_NNTP)
188                                         mailac = cur_account;
189                                 else {
190                                         mailac = account_get_default();
191                                         if (mailac->protocol == A_NNTP)
192                                                 mailac = NULL;
193                                 }
194                         }
195                 } else if (ac) {
196                         mailac = ac;
197                 } else {
198                         ac = account_find_from_smtp_server(from, server);
199                         if (!ac) {
200                                 g_warning("Account not found. "
201                                           "Using current account...\n");
202                                 ac = cur_account;
203                                 if (ac && ac->protocol != A_NNTP)
204                                         mailac = ac;
205                         }
206                 }
207
208                 fpos = ftell(fp);
209                 if (to_list) {
210                         if (mailac)
211                                 val = send_message_smtp(mailac, to_list, fp);
212                         else {
213                                 PrefsAccount tmp_ac;
214
215                                 g_warning("Account not found.\n");
216
217                                 memset(&tmp_ac, 0, sizeof(PrefsAccount));
218                                 tmp_ac.address = from;
219                                 tmp_ac.smtp_server = server;
220                                 tmp_ac.smtpport = SMTP_PORT;
221                                 val = send_message_smtp(&tmp_ac, to_list, fp);
222                         }
223                 }
224
225                 if (val == 0 && newsac) {
226                         fseek(fp, fpos, SEEK_SET);
227                         val = news_post_stream(FOLDER(newsac->folder), fp);
228                         if (val < 0)
229                                 alertpanel_error_log(_("Error occurred while posting the message to %s ."),
230                                                  newsac->nntp_server);
231                 }
232         }
233
234         slist_free_strings(to_list);
235         g_slist_free(to_list);
236         g_free(from);
237         g_free(server);
238         fclose(fp);
239
240         return val;
241 }
242 #endif
243
244 gint send_message_local(const gchar *command, FILE *fp)
245 {
246         FILE *pipefp;
247         gchar buf[BUFFSIZE];
248         int r;
249         sigset_t osig, mask;
250
251         g_return_val_if_fail(command != NULL, -1);
252         g_return_val_if_fail(fp != NULL, -1);
253
254         pipefp = popen(command, "w");
255         if (!pipefp) {
256                 g_warning("Can't execute external command: %s\n", command);
257                 return -1;
258         }
259
260         while (fgets(buf, sizeof(buf), fp) != NULL) {
261                 strretchomp(buf);
262                 fputs(buf, pipefp);
263                 fputc('\n', pipefp);
264         }
265
266         /* we need to block SIGCHLD, otherwise pspell's handler will wait()
267          * the pipecommand away and pclose will return -1 because of its
268          * failed wait4().
269          */
270         sigemptyset(&mask);
271         sigaddset(&mask, SIGCHLD);
272         sigprocmask(SIG_BLOCK, &mask, &osig);
273         
274         r = pclose(pipefp);
275
276         sigprocmask(SIG_SETMASK, &osig, NULL);
277         if (r != 0) {
278                 g_warning("external command `%s' failed with code `%i'\n", command, r);
279                 return -1;
280         }
281
282         return 0;
283 }
284
285 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
286 {
287         Session *session;
288         SMTPSession *smtp_session;
289         gushort port;
290         SendProgressDialog *dialog;
291         gchar buf[BUFFSIZE];
292         gint ret = 0;
293
294         g_return_val_if_fail(ac_prefs != NULL, -1);
295         g_return_val_if_fail(ac_prefs->address != NULL, -1);
296         g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
297         g_return_val_if_fail(to_list != NULL, -1);
298         g_return_val_if_fail(fp != NULL, -1);
299
300         session = smtp_session_new();
301         smtp_session = SMTP_SESSION(session);
302
303         smtp_session->hostname =
304                 ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
305
306         if (ac_prefs->use_smtp_auth) {
307                 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
308                 if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) {
309                         smtp_session->user = g_strdup(ac_prefs->smtp_userid);
310                         if (ac_prefs->smtp_passwd)
311                                 smtp_session->pass =
312                                         g_strdup(ac_prefs->smtp_passwd);
313                         else if (ac_prefs->tmp_smtp_pass)
314                                 smtp_session->pass =
315                                         g_strdup(ac_prefs->tmp_smtp_pass);
316                         else {
317                                 smtp_session->pass =
318                                         input_dialog_query_password
319                                                 (ac_prefs->smtp_server,
320                                                  smtp_session->user);
321                                 if (!smtp_session->pass)
322                                         smtp_session->pass = g_strdup("");
323                                 ac_prefs->tmp_smtp_pass =
324                                         g_strdup(smtp_session->pass);
325                         }
326                 } else {
327                         smtp_session->user = g_strdup(ac_prefs->userid);
328                         if (ac_prefs->passwd)
329                                 smtp_session->pass = g_strdup(ac_prefs->passwd);
330                         else if (ac_prefs->tmp_pass)
331                                 smtp_session->pass =
332                                         g_strdup(ac_prefs->tmp_pass);
333                         else {
334                                 smtp_session->pass =
335                                         input_dialog_query_password
336                                                 (ac_prefs->smtp_server,
337                                                  smtp_session->user);
338                                 if (!smtp_session->pass)
339                                         smtp_session->pass = g_strdup("");
340                                 ac_prefs->tmp_pass =
341                                         g_strdup(smtp_session->pass);
342                         }
343                 }
344         } else {
345                 smtp_session->user = NULL;
346                 smtp_session->pass = NULL;
347         }
348
349         smtp_session->from = g_strdup(ac_prefs->address);
350         smtp_session->to_list = to_list;
351         smtp_session->cur_to = to_list;
352         smtp_session->send_data = get_outgoing_rfc2822_str(fp);
353         smtp_session->send_data_len = strlen(smtp_session->send_data);
354
355 #if USE_OPENSSL
356         port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
357                 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
358         session->ssl_type = ac_prefs->ssl_smtp;
359         if (ac_prefs->ssl_smtp != SSL_NONE)
360                 session->nonblocking = ac_prefs->use_nonblocking_ssl;
361 #else
362         port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
363 #endif
364
365         dialog = send_progress_dialog_create();
366         dialog->session = session;
367
368         progress_dialog_list_set(dialog->dialog, 0, NULL, 
369                                  ac_prefs->smtp_server, 
370                                  _("Connecting"));
371
372         if (ac_prefs->pop_before_smtp
373             && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
374             && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
375                 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
376                 log_message(buf);
377                 progress_dialog_set_label(dialog->dialog, buf);
378                 progress_dialog_list_set_status(dialog->dialog, 0, _("POP before SMTP"));
379                 GTK_EVENTS_FLUSH();
380                 inc_pop_before_smtp(ac_prefs);
381         }
382         
383         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
384                    ac_prefs->smtp_server);
385         progress_dialog_set_label(dialog->dialog, buf);
386         log_message("%s\n", buf);
387
388         session_set_recv_message_notify(session, send_recv_message, dialog);
389         session_set_send_data_progressive_notify
390                 (session, send_send_data_progressive, dialog);
391         session_set_send_data_notify(session, send_send_data_finished, dialog);
392
393         if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
394                 session_destroy(session);
395                 send_progress_dialog_destroy(dialog);
396                 return -1;
397         }
398
399         debug_print("send_message_smtp(): begin event loop\n");
400
401         while (session_is_connected(session) && dialog->cancelled == FALSE)
402                 gtk_main_iteration();
403
404         if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
405                 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
406                         g_free(ac_prefs->tmp_smtp_pass);
407                         ac_prefs->tmp_smtp_pass = NULL;
408                 }
409                 ret = -1;
410         } else if (session->state == SESSION_ERROR ||
411                    session->state == SESSION_EOF ||
412                    session->state == SESSION_TIMEOUT ||
413                    SMTP_SESSION(session)->state == SMTP_ERROR ||
414                    SMTP_SESSION(session)->error_val != SM_OK)
415                 ret = -1;
416         else if (dialog->cancelled == TRUE)
417                 ret = -1;
418
419         if (ret == -1) {
420                 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
421                 send_put_error(session);
422                 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
423         }
424
425         session_destroy(session);
426         send_progress_dialog_destroy(dialog);
427
428         statusbar_pop_all();
429         statusbar_verbosity_set(FALSE);
430         return ret;
431 }
432
433 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
434 {
435         gchar buf[BUFFSIZE];
436         SMTPSession *smtp_session = SMTP_SESSION(session);
437         SendProgressDialog *dialog = (SendProgressDialog *)data;
438         gchar *state_str = NULL;
439
440         g_return_val_if_fail(dialog != NULL, -1);
441
442         switch (smtp_session->state) {
443         case SMTP_READY:
444         case SMTP_CONNECTED:
445                 return 0;
446         case SMTP_HELO:
447                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
448                 state_str = _("Authenticating");
449                 statusbar_print_all(_("Sending message..."));
450                 break;
451         case SMTP_EHLO:
452                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
453                 state_str = _("Authenticating");
454                 statusbar_print_all(_("Sending message..."));
455                 break;
456         case SMTP_AUTH:
457                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
458                 state_str = _("Authenticating");
459                 break;
460         case SMTP_FROM:
461                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
462                 state_str = _("Sending");
463                 break;
464         case SMTP_RCPT:
465                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
466                 state_str = _("Sending");
467                 break;
468         case SMTP_DATA:
469         case SMTP_EOM:
470                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
471                 state_str = _("Sending");
472                 break;
473         case SMTP_QUIT:
474                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
475                 state_str = _("Quitting");
476                 break;
477         case SMTP_ERROR:
478                 g_warning("send: error: %s\n", msg);
479                 return 0;
480         default:
481                 return 0;
482         }
483
484         progress_dialog_set_label(dialog->dialog, buf);
485         progress_dialog_list_set_status(dialog->dialog, 0, state_str);
486
487         return 0;
488 }
489
490 static gint send_send_data_progressive(Session *session, guint cur_len,
491                                        guint total_len, gpointer data)
492 {
493         gchar buf[BUFFSIZE];
494         SendProgressDialog *dialog = (SendProgressDialog *)data;
495
496         g_return_val_if_fail(dialog != NULL, -1);
497
498         if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
499             SMTP_SESSION(session)->state != SMTP_EOM)
500                 return 0;
501
502         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
503                    cur_len, total_len);
504         progress_dialog_set_label(dialog->dialog, buf);
505         progress_dialog_set_fraction
506                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
507
508         return 0;
509 }
510
511 static gint send_send_data_finished(Session *session, guint len, gpointer data)
512 {
513         SendProgressDialog *dialog = (SendProgressDialog *)data;
514
515         g_return_val_if_fail(dialog != NULL, -1);
516
517         send_send_data_progressive(session, len, len, dialog);
518         return 0;
519 }
520
521 static SendProgressDialog *send_progress_dialog_create(void)
522 {
523         SendProgressDialog *dialog;
524         ProgressDialog *progress;
525
526         dialog = g_new0(SendProgressDialog, 1);
527
528         progress = progress_dialog_create();
529         gtk_window_set_title(GTK_WINDOW(progress->window),
530                              _("Sending message"));
531         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
532                          G_CALLBACK(send_cancel_button_cb), dialog);
533         g_signal_connect(G_OBJECT(progress->window), "delete_event",
534                          G_CALLBACK(gtk_true), NULL);
535         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
536         manage_window_set_transient(GTK_WINDOW(progress->window));
537
538         progress_dialog_get_fraction(progress);
539
540         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
541                 gtk_widget_show_now(progress->window);
542         }
543         
544         dialog->dialog = progress;
545
546         return dialog;
547 }
548
549 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
550 {
551         g_return_if_fail(dialog != NULL);
552         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
553                 progress_dialog_destroy(dialog->dialog);
554         }
555         g_free(dialog);
556 }
557
558 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
559 {
560         SendProgressDialog *dialog = (SendProgressDialog *)data;
561
562         dialog->cancelled = TRUE;
563 }
564
565 static void send_put_error(Session *session)
566 {
567         gchar *msg;
568         gchar *log_msg = NULL;
569         gchar *err_msg = NULL;
570
571         msg = SMTP_SESSION(session)->error_msg;
572
573         switch (SMTP_SESSION(session)->error_val) {
574         case SM_ERROR:
575         case SM_UNRECOVERABLE:
576                 log_msg = _("Error occurred while sending the message.");
577                 if (msg)
578                         err_msg = g_strdup_printf
579                                 (_("Error occurred while sending the message:\n%s"),
580                                  msg);
581                 else
582                         err_msg = g_strdup(log_msg);
583                 break;
584         case SM_AUTHFAIL:
585                 log_msg = _("Authentication failed.");
586                 if (msg)
587                         err_msg = g_strdup_printf
588                                 (_("Authentication failed:\n%s"), msg);
589                 else
590                         err_msg = g_strdup(log_msg);
591                 break;
592         default:
593                 switch (session->state) {
594                 case SESSION_ERROR:
595                         log_msg =
596                                 _("Error occurred while sending the message.");
597                         err_msg = g_strdup(log_msg);
598                         break;
599                 case SESSION_EOF:
600                         log_msg = _("Connection closed by the remote host.");
601                         err_msg = g_strdup(log_msg);
602                         break;
603                 case SESSION_TIMEOUT:
604                         log_msg = _("Session timed out.");
605                         err_msg = g_strdup(log_msg);
606                         break;
607                 default:
608                         break;
609                 }
610                 break;
611         }
612
613         if (log_msg)
614                 log_warning("%s\n", log_msg);
615         if (err_msg) {
616                 alertpanel_error_log("%s", err_msg);
617                 g_free(err_msg);
618         }
619 }
620