sync with 0.9.3cvs12
[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->state != SESSION_DISCONNECTED &&
413                session->state != SESSION_ERROR &&
414                dialog->cancelled == FALSE)
415                 gtk_main_iteration();
416
417         if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
418                 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
419                         g_free(ac_prefs->tmp_smtp_pass);
420                         ac_prefs->tmp_smtp_pass = NULL;
421                 }
422                 ret = -1;
423         } else if (session->state == SESSION_ERROR ||
424                    SMTP_SESSION(session)->state == SMTP_ERROR ||
425                    SMTP_SESSION(session)->error_val != SM_OK)
426                 ret = -1;
427         else if (dialog->cancelled == TRUE)
428                 ret = -1;
429
430         if (ret == -1) {
431                 manage_window_focus_in(dialog->dialog->window, NULL, NULL);
432                 send_put_error(session);
433                 manage_window_focus_out(dialog->dialog->window, NULL, NULL);
434         }
435
436         session_destroy(session);
437         send_progress_dialog_destroy(dialog);
438
439         statusbar_pop_all();
440         statusbar_verbosity_set(FALSE);
441         return ret;
442 }
443
444 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
445 {
446         gchar buf[BUFFSIZE];
447         SMTPSession *smtp_session = SMTP_SESSION(session);
448         SendProgressDialog *dialog = (SendProgressDialog *)data;
449         gchar *state_str = NULL;
450
451         g_return_val_if_fail(dialog != NULL, -1);
452
453         switch (smtp_session->state) {
454         case SMTP_READY:
455         case SMTP_CONNECTED:
456                 return 0;
457         case SMTP_HELO:
458                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
459                 state_str = _("Authenticating");
460                 statusbar_print_all(_("Sending message..."));
461                 break;
462         case SMTP_EHLO:
463                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
464                 state_str = _("Authenticating");
465                 statusbar_print_all(_("Sending message..."));
466                 break;
467         case SMTP_AUTH:
468                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
469                 state_str = _("Authenticating");
470                 break;
471         case SMTP_FROM:
472                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
473                 state_str = _("Sending");
474                 break;
475         case SMTP_RCPT:
476                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
477                 state_str = _("Sending");
478                 break;
479         case SMTP_DATA:
480         case SMTP_EOM:
481                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
482                 state_str = _("Sending");
483                 break;
484         case SMTP_QUIT:
485                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
486                 state_str = _("Quitting");
487                 break;
488         case SMTP_ERROR:
489                 g_warning("send: error: %s\n", msg);
490                 return 0;
491         default:
492                 return 0;
493         }
494
495         progress_dialog_set_label(dialog->dialog, buf);
496         gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
497
498         return 0;
499 }
500
501 static gint send_send_data_progressive(Session *session, guint cur_len,
502                                        guint total_len, gpointer data)
503 {
504         gchar buf[BUFFSIZE];
505         SendProgressDialog *dialog = (SendProgressDialog *)data;
506
507         g_return_val_if_fail(dialog != NULL, -1);
508
509         if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
510             SMTP_SESSION(session)->state != SMTP_EOM)
511                 return 0;
512
513         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
514                    cur_len, total_len);
515         progress_dialog_set_label(dialog->dialog, buf);
516         progress_dialog_set_percentage
517                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
518
519         return 0;
520 }
521
522 static gint send_send_data_finished(Session *session, guint len, gpointer data)
523 {
524         SendProgressDialog *dialog = (SendProgressDialog *)data;
525
526         g_return_val_if_fail(dialog != NULL, -1);
527
528         send_send_data_progressive(session, len, len, dialog);
529         return 0;
530 }
531
532 static SendProgressDialog *send_progress_dialog_create(void)
533 {
534         SendProgressDialog *dialog;
535         ProgressDialog *progress;
536
537         dialog = g_new0(SendProgressDialog, 1);
538
539         progress = progress_dialog_create();
540         gtk_window_set_title(GTK_WINDOW(progress->window),
541                              _("Sending message"));
542         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
543                            GTK_SIGNAL_FUNC(send_cancel_button_cb), dialog);
544         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
545                            GTK_SIGNAL_FUNC(gtk_true), NULL);
546         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
547         manage_window_set_transient(GTK_WINDOW(progress->window));
548
549         progress_dialog_set_value(progress, 0.0);
550
551         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
552                 gtk_widget_show_now(progress->window);
553         }
554         
555         dialog->dialog = progress;
556
557         return dialog;
558 }
559
560 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
561 {
562         g_return_if_fail(dialog != NULL);
563         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
564                 progress_dialog_destroy(dialog->dialog);
565         }
566         g_free(dialog);
567 }
568
569 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
570 {
571         SendProgressDialog *dialog = (SendProgressDialog *)data;
572
573         dialog->cancelled = TRUE;
574 }
575
576 static void send_put_error(Session *session)
577 {
578         gchar *msg;
579
580         msg = SMTP_SESSION(session)->error_msg;
581
582         switch (SMTP_SESSION(session)->error_val) {
583         case SM_ERROR:
584         case SM_UNRECOVERABLE:
585                 if (msg)
586                         alertpanel_error_log
587                                 (_("Error occurred while sending the message:\n%s"),
588                                  msg);
589                 else
590                         alertpanel_error_log
591                                 (_("Error occurred while sending the message."));
592                 break;
593         case SM_AUTHFAIL:
594                 if (msg)
595                         alertpanel_error_log
596                                 (_("Authentication failed:\n%s"), msg);
597                 else
598                         alertpanel_error_log
599                                 (_("Authentication failed."));
600         default:
601                 if (session->state == SESSION_ERROR)
602                         alertpanel_error_log
603                                 (_("Error occurred while sending the message."));
604                 break;
605         }
606 }
607