2a682fc0ab97fd8c08ec051d5d7fe6a8152d1d5a
[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 "inputdialog.h"
46 #include "alertpanel.h"
47 #include "manage_window.h"
48 #include "utils.h"
49 #include "gtkutils.h"
50 #include "statusbar.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 #else
369         port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
370 #endif
371
372         dialog = send_progress_dialog_create();
373         dialog->session = session;
374
375         text[0] = NULL;
376         text[1] = ac_prefs->smtp_server;
377         text[2] = _("Connecting");
378         clist = GTK_CLIST(dialog->dialog->clist);
379         gtk_clist_append(clist, (gchar **)text);
380
381         if (ac_prefs->pop_before_smtp
382             && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
383             && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
384                 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
385                 log_message(buf);
386                 progress_dialog_set_label(dialog->dialog, buf);
387                 gtk_clist_set_text(clist, 0, 2, _("POP before SMTP"));
388                 GTK_EVENTS_FLUSH();
389                 inc_pop_before_smtp(ac_prefs);
390         }
391         
392         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
393                    ac_prefs->smtp_server);
394         progress_dialog_set_label(dialog->dialog, buf);
395         log_message("%s\n", buf);
396
397         session_set_recv_message_notify(session, send_recv_message, dialog);
398         session_set_send_data_progressive_notify
399                 (session, send_send_data_progressive, dialog);
400         session_set_send_data_notify(session, send_send_data_finished, dialog);
401
402         if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
403                 session_destroy(session);
404                 send_progress_dialog_destroy(dialog);
405                 return -1;
406         }
407
408         debug_print("send_message_smtp(): begin event loop\n");
409
410         while (session->state != SESSION_DISCONNECTED &&
411                session->state != SESSION_ERROR &&
412                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                    SMTP_SESSION(session)->state == SMTP_ERROR ||
423                    SMTP_SESSION(session)->error_val != SM_OK)
424                 ret = -1;
425         else if (dialog->cancelled == TRUE)
426                 ret = -1;
427
428         if (ret == -1) {
429                 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
430                 send_put_error(session);
431                 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
432         }
433
434         session_destroy(session);
435         send_progress_dialog_destroy(dialog);
436
437         statusbar_verbosity_set(FALSE);
438         return ret;
439 }
440
441 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
442 {
443         gchar buf[BUFFSIZE];
444         SMTPSession *smtp_session = SMTP_SESSION(session);
445         SendProgressDialog *dialog = (SendProgressDialog *)data;
446         gchar *state_str = NULL;
447
448         g_return_val_if_fail(dialog != NULL, -1);
449
450         switch (smtp_session->state) {
451         case SMTP_READY:
452         case SMTP_CONNECTED:
453                 return 0;
454         case SMTP_HELO:
455                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
456                 state_str = _("Authenticating");
457                 break;
458         case SMTP_EHLO:
459                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
460                 state_str = _("Authenticating");
461                 break;
462         case SMTP_AUTH:
463                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
464                 state_str = _("Authenticating");
465                 break;
466         case SMTP_FROM:
467                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
468                 state_str = _("Sending");
469                 break;
470         case SMTP_RCPT:
471                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
472                 state_str = _("Sending");
473                 break;
474         case SMTP_DATA:
475         case SMTP_EOM:
476                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
477                 state_str = _("Sending");
478                 break;
479         case SMTP_QUIT:
480                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
481                 state_str = _("Quitting");
482                 break;
483         case SMTP_ERROR:
484                 g_warning("send: error: %s\n", msg);
485                 return 0;
486         default:
487                 return 0;
488         }
489
490         progress_dialog_set_label(dialog->dialog, buf);
491         gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
492
493         return 0;
494 }
495
496 static gint send_send_data_progressive(Session *session, guint cur_len,
497                                        guint total_len, gpointer data)
498 {
499         gchar buf[BUFFSIZE];
500         SendProgressDialog *dialog = (SendProgressDialog *)data;
501
502         g_return_val_if_fail(dialog != NULL, -1);
503
504         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
505                    cur_len, total_len);
506         progress_dialog_set_label(dialog->dialog, buf);
507         progress_dialog_set_percentage
508                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
509
510         return 0;
511 }
512
513 static gint send_send_data_finished(Session *session, guint len, gpointer data)
514 {
515         SendProgressDialog *dialog = (SendProgressDialog *)data;
516
517         g_return_val_if_fail(dialog != NULL, -1);
518
519         send_send_data_progressive(session, len, len, dialog);
520         return 0;
521 }
522
523 static SendProgressDialog *send_progress_dialog_create(void)
524 {
525         SendProgressDialog *dialog;
526         ProgressDialog *progress;
527
528         dialog = g_new0(SendProgressDialog, 1);
529
530         progress = progress_dialog_create();
531         gtk_window_set_title(GTK_WINDOW(progress->window),
532                              _("Sending message"));
533         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
534                            GTK_SIGNAL_FUNC(send_cancel_button_cb), dialog);
535         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
536                            GTK_SIGNAL_FUNC(gtk_true), NULL);
537         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
538         manage_window_set_transient(GTK_WINDOW(progress->window));
539
540         progress_dialog_set_value(progress, 0.0);
541
542         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
543                 gtk_widget_show_now(progress->window);
544         }
545         
546         dialog->dialog = progress;
547
548         return dialog;
549 }
550
551 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
552 {
553         g_return_if_fail(dialog != NULL);
554         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
555                 progress_dialog_destroy(dialog->dialog);
556         }
557         g_free(dialog);
558 }
559
560 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
561 {
562         SendProgressDialog *dialog = (SendProgressDialog *)data;
563
564         dialog->cancelled = TRUE;
565 }
566
567 static void send_put_error(Session *session)
568 {
569         gchar *msg;
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                 if (msg)
577                         alertpanel_error_log
578                                 (_("Error occurred while sending the message:\n%s"),
579                                  msg);
580                 else
581                         alertpanel_error_log
582                                 (_("Error occurred while sending the message."));
583                 break;
584         case SM_AUTHFAIL:
585                 if (msg)
586                         alertpanel_error_log
587                                 (_("Authentication failed:\n%s"), msg);
588                 else
589                         alertpanel_error_log
590                                 (_("Authentication failed."));
591         default:
592                 if (session->state == SESSION_ERROR)
593                         alertpanel_error_log
594                                 (_("Error occurred while sending the message."));
595                 break;
596         }
597 }
598