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