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