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