4503eee07cf1321ffebdc60f9f8d71a163d4e37f
[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                 if (ac_prefs->smtp_userid) {
309                         smtp_session->user = g_strdup(ac_prefs->smtp_userid);
310                         if (ac_prefs->smtp_passwd)
311                                 smtp_session->pass =
312                                         g_strdup(ac_prefs->smtp_passwd);
313                         else if (ac_prefs->tmp_smtp_pass)
314                                 smtp_session->pass =
315                                         g_strdup(ac_prefs->tmp_smtp_pass);
316                         else {
317                                 smtp_session->pass =
318                                         input_dialog_query_password
319                                                 (ac_prefs->smtp_server,
320                                                  smtp_session->user);
321                                 if (!smtp_session->pass)
322                                         smtp_session->pass = g_strdup("");
323                                 ac_prefs->tmp_smtp_pass =
324                                         g_strdup(smtp_session->pass);
325                         }
326                 } else {
327                         smtp_session->user = g_strdup(ac_prefs->userid);
328                         if (ac_prefs->passwd)
329                                 smtp_session->pass = g_strdup(ac_prefs->passwd);
330                         else if (ac_prefs->tmp_pass)
331                                 smtp_session->pass =
332                                         g_strdup(ac_prefs->tmp_pass);
333                         else {
334                                 smtp_session->pass =
335                                         input_dialog_query_password
336                                                 (ac_prefs->smtp_server,
337                                                  smtp_session->user);
338                                 if (!smtp_session->pass)
339                                         smtp_session->pass = g_strdup("");
340                                 ac_prefs->tmp_pass =
341                                         g_strdup(smtp_session->pass);
342                         }
343                 }
344         } else {
345                 smtp_session->user = NULL;
346                 smtp_session->pass = NULL;
347         }
348
349         smtp_session->from = g_strdup(ac_prefs->address);
350         smtp_session->to_list = to_list;
351         smtp_session->cur_to = to_list;
352         smtp_session->send_data = get_outgoing_rfc2822_str(fp);
353         smtp_session->send_data_len = strlen(smtp_session->send_data);
354
355 #if USE_OPENSSL
356         port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
357                 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
358         session->ssl_type = ac_prefs->ssl_smtp;
359 #else
360         port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
361 #endif
362
363         dialog = send_progress_dialog_create();
364         dialog->session = session;
365
366         text[0] = NULL;
367         text[1] = ac_prefs->smtp_server;
368         text[2] = _("Connecting");
369         clist = GTK_CLIST(dialog->dialog->clist);
370         gtk_clist_append(clist, (gchar **)text);
371
372         if (ac_prefs->pop_before_smtp
373             && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
374             && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
375                 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
376                 log_message(buf);
377                 progress_dialog_set_label(dialog->dialog, buf);
378                 gtk_clist_set_text(clist, 0, 2, _("POP before SMTP"));
379                 GTK_EVENTS_FLUSH();
380                 inc_pop_before_smtp(ac_prefs);
381         }
382         
383         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
384                    ac_prefs->smtp_server);
385         progress_dialog_set_label(dialog->dialog, buf);
386         log_message("%s\n", buf);
387
388         session_set_recv_message_notify(session, send_recv_message, dialog);
389         session_set_send_data_progressive_notify
390                 (session, send_send_data_progressive, dialog);
391         session_set_send_data_notify(session, send_send_data_finished, dialog);
392
393         if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
394                 session_destroy(session);
395                 send_progress_dialog_destroy(dialog);
396                 return -1;
397         }
398
399         g_print("parent: begin event loop\n");
400
401         while (session->state != SESSION_DISCONNECTED &&
402                session->state != SESSION_ERROR)
403                 gtk_main_iteration();
404
405         if (SMTP_SESSION(session)->state == SMTP_AUTH_FAILED) {
406                 g_free(ac_prefs->tmp_smtp_pass);
407                 ac_prefs->tmp_smtp_pass = NULL;
408                 ret = -1;
409         } else if ((session->state == SESSION_ERROR) ||
410                    (SMTP_SESSION(session)->state == SMTP_ERROR))
411                 ret = -1;
412         else if (dialog->cancelled == TRUE)
413                 ret = -1;
414
415         session_destroy(session);
416         send_progress_dialog_destroy(dialog);
417
418         statusbar_verbosity_set(FALSE);
419         return ret;
420 }
421
422 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
423 {
424         SMTPSession *smtp_session = SMTP_SESSION(session);
425         SendProgressDialog *dialog = (SendProgressDialog *)data;
426         gchar buf[BUFFSIZE];
427         gchar *state_str = NULL;
428
429         switch (smtp_session->state) {
430         case SMTP_READY:
431         case SMTP_CONNECTED:
432                 return 0;
433         case SMTP_HELO:
434                 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
435                 state_str = _("Authenticating");
436                 break;
437         case SMTP_EHLO:
438                 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
439                 state_str = _("Authenticating");
440                 break;
441         case SMTP_AUTH:
442                 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
443                 state_str = _("Authenticating");
444                 break;
445         case SMTP_FROM:
446                 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
447                 state_str = _("Sending");
448                 break;
449         case SMTP_RCPT:
450                 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
451                 state_str = _("Sending");
452                 break;
453         case SMTP_DATA:
454         case SMTP_EOM:
455                 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
456                 state_str = _("Sending");
457                 break;
458         case SMTP_QUIT:
459                 g_snprintf(buf, sizeof(buf), _("Quitting..."));
460                 state_str = _("Quitting");
461                 break;
462         case SMTP_ERROR:
463         case SMTP_AUTH_FAILED:
464                 g_warning("send: error: %s\n", msg);
465                 return 0;
466         default:
467                 return 0;
468         }
469
470         progress_dialog_set_label(dialog->dialog, buf);
471         gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
472
473         return 0;
474 }
475
476 static gint send_send_data_progressive(Session *session, guint cur_len,
477                                        guint total_len, gpointer data)
478 {
479         SendProgressDialog *dialog = (SendProgressDialog *)data;
480         gchar buf[BUFFSIZE];
481
482         g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
483                    cur_len, total_len);
484         progress_dialog_set_label(dialog->dialog, buf);
485         progress_dialog_set_percentage
486                 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
487
488         return 0;
489 }
490
491 static gint send_send_data_finished(Session *session, guint len, gpointer data)
492 {
493         send_send_data_progressive(session, len, len, data);
494         return 0;
495 }
496
497 static SendProgressDialog *send_progress_dialog_create(void)
498 {
499         SendProgressDialog *dialog;
500         ProgressDialog *progress;
501
502         dialog = g_new0(SendProgressDialog, 1);
503
504         progress = progress_dialog_create();
505         gtk_window_set_title(GTK_WINDOW(progress->window),
506                              _("Sending message"));
507         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
508                            GTK_SIGNAL_FUNC(send_cancel_button_cb), dialog);
509         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
510                            GTK_SIGNAL_FUNC(gtk_true), NULL);
511         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
512         manage_window_set_transient(GTK_WINDOW(progress->window));
513
514         progress_dialog_set_value(progress, 0.0);
515
516         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
517                 gtk_widget_show_now(progress->window);
518         }
519         
520         dialog->dialog = progress;
521
522         return dialog;
523 }
524
525 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
526 {
527         g_return_if_fail(dialog != NULL);
528         if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
529                 progress_dialog_destroy(dialog->dialog);
530         }
531         g_free(dialog);
532 }
533
534 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
535 {
536         SendProgressDialog *dialog = (SendProgressDialog *)data;
537         Session *session = dialog->session;
538
539         session->state = SESSION_DISCONNECTED;
540         dialog->cancelled = TRUE;
541 }