52b2726a57798dfbf992a841685dfdf65b0ab0c8
[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         GtkCList *clist;
292         const gchar *text[3];
293         gchar buf[BUFFSIZE];
294         gint ret = 0;
295
296         g_return_val_if_fail(ac_prefs != NULL, -1);
297         g_return_val_if_fail(ac_prefs->address != NULL, -1);
298         g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
299         g_return_val_if_fail(to_list != NULL, -1);
300         g_return_val_if_fail(fp != NULL, -1);
301
302         session = smtp_session_new();
303         smtp_session = SMTP_SESSION(session);
304
305         smtp_session->hostname =
306                 ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
307
308         if (ac_prefs->use_smtp_auth) {
309                 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
310
311                 if (ac_prefs->smtp_userid) {
312                         smtp_session->user = g_strdup(ac_prefs->smtp_userid);
313                         if (ac_prefs->smtp_passwd)
314                                 smtp_session->pass =
315                                         g_strdup(ac_prefs->smtp_passwd);
316                         else if (ac_prefs->tmp_smtp_pass)
317                                 smtp_session->pass =
318                                         g_strdup(ac_prefs->tmp_smtp_pass);
319                         else {
320                                 smtp_session->pass =
321                                         input_dialog_query_password
322                                                 (ac_prefs->smtp_server,
323                                                  smtp_session->user);
324                                 if (!smtp_session->pass)
325                                         smtp_session->pass = g_strdup("");
326                                 ac_prefs->tmp_smtp_pass =
327                                         g_strdup(smtp_session->pass);
328                         }
329                 } else {
330                         smtp_session->user = g_strdup(ac_prefs->userid);
331                         if (ac_prefs->passwd)
332                                 smtp_session->pass = g_strdup(ac_prefs->passwd);
333                         else if (ac_prefs->tmp_pass)
334                                 smtp_session->pass =
335                                         g_strdup(ac_prefs->tmp_pass);
336                         else {
337                                 smtp_session->pass =
338                                         input_dialog_query_password
339                                                 (ac_prefs->smtp_server,
340                                                  smtp_session->user);
341                                 if (!smtp_session->pass)
342                                         smtp_session->pass = g_strdup("");
343                                 ac_prefs->tmp_pass =
344                                         g_strdup(smtp_session->pass);
345                         }
346                 }
347         } else {
348                 smtp_session->user = NULL;
349                 smtp_session->pass = NULL;
350         }
351
352         smtp_session->from = g_strdup(ac_prefs->address);
353         smtp_session->to_list = to_list;
354         smtp_session->cur_to = to_list;
355         smtp_session->send_data = get_outgoing_rfc2822_str(fp);
356         smtp_session->send_data_len = strlen(smtp_session->send_data);
357
358 #if USE_OPENSSL
359         port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
360                 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
361         session->ssl_type = ac_prefs->ssl_smtp;
362         if (ac_prefs->ssl_smtp != SSL_NONE)
363                 session->nonblocking = ac_prefs->use_nonblocking_ssl;
364 #else
365         port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
366 #endif
367
368         dialog = send_progress_dialog_create();
369         dialog->session = session;
370
371         text[0] = NULL;
372         text[1] = ac_prefs->smtp_server;
373         text[2] = _("Connecting");
374         clist = GTK_CLIST(dialog->dialog->clist);
375         gtk_clist_append(clist, (gchar **)text);
376
377         if (ac_prefs->pop_before_smtp
378             && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
379             && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
380                 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
381                 log_message(buf);
382                 progress_dialog_set_label(dialog->dialog, buf);
383                 gtk_clist_set_text(clist, 0, 2, _("POP before SMTP"));
384                 GTK_EVENTS_FLUSH();
385                 inc_pop_before_smtp(ac_prefs);
386         }
387         
388         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
389                    ac_prefs->smtp_server);
390         progress_dialog_set_label(dialog->dialog, buf);
391         log_message("%s\n", buf);
392
393         session_set_recv_message_notify(session, send_recv_message, dialog);
394         session_set_send_data_progressive_notify
395                 (session, send_send_data_progressive, dialog);
396         session_set_send_data_notify(session, send_send_data_finished, dialog);
397
398         if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
399                 session_destroy(session);
400                 send_progress_dialog_destroy(dialog);
401                 return -1;
402         }
403
404         debug_print("send_message_smtp(): begin event loop\n");
405
406         while (session_is_connected(session) && dialog->cancelled == FALSE)
407                 gtk_main_iteration();
408
409         if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
410                 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
411                         g_free(ac_prefs->tmp_smtp_pass);
412                         ac_prefs->tmp_smtp_pass = NULL;
413                 }
414                 ret = -1;
415         } else if (session->state == SESSION_ERROR ||
416                    session->state == SESSION_EOF ||
417                    SMTP_SESSION(session)->state == SMTP_ERROR ||
418                    SMTP_SESSION(session)->error_val != SM_OK)
419                 ret = -1;
420         else if (dialog->cancelled == TRUE)
421                 ret = -1;
422
423         if (ret == -1) {
424                 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
425                 send_put_error(session);
426                 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
427         }
428
429         session_destroy(session);
430         send_progress_dialog_destroy(dialog);
431
432         statusbar_pop_all();
433         statusbar_verbosity_set(FALSE);
434         return ret;
435 }
436
437 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
438 {
439         gchar buf[BUFFSIZE];
440         SMTPSession *smtp_session = SMTP_SESSION(session);
441         SendProgressDialog *dialog = (SendProgressDialog *)data;
442         gchar *state_str = NULL;
443
444         g_return_val_if_fail(dialog != NULL, -1);
445
446         switch (smtp_session->state) {
447         case SMTP_READY:
448         case SMTP_CONNECTED:
449                 return 0;
450         case SMTP_HELO:
451                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
452                 state_str = _("Authenticating");
453                 statusbar_print_all(_("Sending message..."));
454                 break;
455         case SMTP_EHLO:
456                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
457                 state_str = _("Authenticating");
458                 statusbar_print_all(_("Sending message..."));
459                 break;
460         case SMTP_AUTH:
461                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
462                 state_str = _("Authenticating");
463                 break;
464         case SMTP_FROM:
465                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
466                 state_str = _("Sending");
467                 break;
468         case SMTP_RCPT:
469                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
470                 state_str = _("Sending");
471                 break;
472         case SMTP_DATA:
473         case SMTP_EOM:
474                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
475                 state_str = _("Sending");
476                 break;
477         case SMTP_QUIT:
478                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
479                 state_str = _("Quitting");
480                 break;
481         case SMTP_ERROR:
482                 g_warning("send: error: %s\n", msg);
483                 return 0;
484         default:
485                 return 0;
486         }
487
488         progress_dialog_set_label(dialog->dialog, buf);
489         gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
490
491         return 0;
492 }
493
494 static gint send_send_data_progressive(Session *session, guint cur_len,
495                                        guint total_len, gpointer data)
496 {
497         gchar buf[BUFFSIZE];
498         SendProgressDialog *dialog = (SendProgressDialog *)data;
499
500         g_return_val_if_fail(dialog != NULL, -1);
501
502         if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
503             SMTP_SESSION(session)->state != SMTP_EOM)
504                 return 0;
505
506         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
507                    cur_len, total_len);
508         progress_dialog_set_label(dialog->dialog, buf);
509         progress_dialog_set_percentage
510                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
511
512         return 0;
513 }
514
515 static gint send_send_data_finished(Session *session, guint len, gpointer data)
516 {
517         SendProgressDialog *dialog = (SendProgressDialog *)data;
518
519         g_return_val_if_fail(dialog != NULL, -1);
520
521         send_send_data_progressive(session, len, len, dialog);
522         return 0;
523 }
524
525 static SendProgressDialog *send_progress_dialog_create(void)
526 {
527         SendProgressDialog *dialog;
528         ProgressDialog *progress;
529
530         dialog = g_new0(SendProgressDialog, 1);
531
532         progress = progress_dialog_create();
533         gtk_window_set_title(GTK_WINDOW(progress->window),
534                              _("Sending message"));
535         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
536                            GTK_SIGNAL_FUNC(send_cancel_button_cb), dialog);
537         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
538                            GTK_SIGNAL_FUNC(gtk_true), NULL);
539         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
540         manage_window_set_transient(GTK_WINDOW(progress->window));
541
542         progress_dialog_set_value(progress, 0.0);
543
544         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
545                 gtk_widget_show_now(progress->window);
546         }
547         
548         dialog->dialog = progress;
549
550         return dialog;
551 }
552
553 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
554 {
555         g_return_if_fail(dialog != NULL);
556         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
557                 progress_dialog_destroy(dialog->dialog);
558         }
559         g_free(dialog);
560 }
561
562 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
563 {
564         SendProgressDialog *dialog = (SendProgressDialog *)data;
565
566         dialog->cancelled = TRUE;
567 }
568
569 static void send_put_error(Session *session)
570 {
571         gchar *msg;
572         gchar *log_msg = NULL;
573         gchar *err_msg = NULL;
574
575         msg = SMTP_SESSION(session)->error_msg;
576
577         switch (SMTP_SESSION(session)->error_val) {
578         case SM_ERROR:
579         case SM_UNRECOVERABLE:
580                 log_msg = _("Error occurred while sending the message.");
581                 if (msg)
582                         err_msg = g_strdup_printf
583                                 (_("Error occurred while sending the message:\n%s"),
584                                  msg);
585                 else
586                         err_msg = g_strdup(log_msg);
587                 break;
588         case SM_AUTHFAIL:
589                 log_msg = _("Authentication failed.");
590                 if (msg)
591                         err_msg = g_strdup_printf
592                                 (_("Authentication failed:\n%s"), msg);
593                 else
594                         err_msg = g_strdup(log_msg);
595                 break;
596         default:
597                 if (session->state == SESSION_ERROR) {
598                         log_msg =
599                                 _("Error occurred while sending the message.");
600                         err_msg = g_strdup(log_msg);
601                 } else if (session->state == SESSION_EOF) {
602                         log_msg = _("Connection closed by the remote host.");
603                         err_msg = g_strdup(log_msg);
604                 }
605                 break;
606         }
607
608         if (log_msg)
609                 log_warning("%s\n", log_msg);
610         if (err_msg) {
611                 alertpanel_error_log(err_msg);
612                 g_free(err_msg);
613         }
614 }
615