sync with sylpheed 0.6.4cvs18
[claws.git] / src / send.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 <sys/time.h>
34 #include <unistd.h>
35
36 #include "intl.h"
37 #include "send.h"
38 #include "socket.h"
39 #include "ssl.h"
40 #include "smtp.h"
41 #include "esmtp.h"
42 #include "prefs_common.h"
43 #include "prefs_account.h"
44 #include "account.h"
45 #include "compose.h"
46 #include "progressdialog.h"
47 #include "inputdialog.h"
48 #include "manage_window.h"
49 #include "procmsg.h"
50 #include "procheader.h"
51 #include "utils.h"
52 #include "gtkutils.h"
53
54 #define SMTP_PORT       25
55 #if USE_SSL
56 #define SSMTP_PORT      465
57 #endif
58
59 typedef struct _SendProgressDialog      SendProgressDialog;
60
61 struct _SendProgressDialog
62 {
63         ProgressDialog *dialog;
64         GList *queue_list;
65         gboolean cancelled;
66 };
67
68 static gint send_message_local  (const gchar *command, FILE *fp);
69
70 static gint send_message_smtp   (PrefsAccount *ac_prefs, GSList *to_list,
71                                  FILE *fp);
72
73 #if USE_SSL
74 static SockInfo *send_smtp_open (const gchar *server, gushort port,
75                                  const gchar *domain, gboolean use_smtp_auth,
76                                  SSLSMTPType ssl_type);
77 #else
78 static SockInfo *send_smtp_open (const gchar *server, gushort port,
79                                  const gchar *domain, gboolean use_smtp_auth);
80 #endif
81
82 static gint send_message_data   (SendProgressDialog *dialog, SockInfo *sock,
83                                  FILE *fp, gint size);
84
85 static SendProgressDialog *send_progress_dialog_create(void);
86 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
87 static void send_cancel(GtkWidget *widget, gpointer data);
88
89 static gchar *send_query_password(const gchar *server, const gchar *user);
90
91
92 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
93 {
94         FILE *fp;
95         gint val;
96
97         g_return_val_if_fail(file != NULL, -1);
98         g_return_val_if_fail(ac_prefs != NULL, -1);
99         g_return_val_if_fail(to_list != NULL, -1);
100
101         if ((fp = fopen(file, "r")) == NULL) {
102                 FILE_OP_ERROR(file, "fopen");
103                 return -1;
104         }
105
106         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
112         val = send_message_smtp(ac_prefs, to_list, fp);
113
114         fclose(fp);
115         return val;
116 }
117
118 enum
119 {
120         Q_SENDER     = 0,
121         Q_SMTPSERVER = 1,
122         Q_RECIPIENTS = 2,
123         Q_ACCOUNT_ID = 3
124 };
125
126 gint send_message_queue(const gchar *file)
127 {
128         static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
129                                        {"SSV:", NULL, FALSE},
130                                        {"R:",   NULL, FALSE},
131                                        {"AID:", NULL, FALSE},
132                                        {NULL,   NULL, FALSE}};
133         FILE *fp;
134         gint val;
135         gchar *from = NULL;
136         gchar *server = NULL;
137         GSList *to_list = NULL;
138         gchar buf[BUFFSIZE];
139         gint hnum;
140         PrefsAccount *ac = NULL;
141
142         g_return_val_if_fail(file != NULL, -1);
143
144         if ((fp = fopen(file, "r")) == NULL) {
145                 FILE_OP_ERROR(file, "fopen");
146                 return -1;
147         }
148
149         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
150                != -1) {
151                 gchar *p = buf + strlen(qentry[hnum].name);
152
153                 switch (hnum) {
154                 case Q_SENDER:
155                         if (!from) from = g_strdup(p);
156                         break;
157                 case Q_SMTPSERVER:
158                         if (!server) server = g_strdup(p);
159                         break;
160                 case Q_RECIPIENTS:
161                         to_list = address_list_append(to_list, p);
162                         break;
163                 case Q_ACCOUNT_ID:
164                         ac = account_find_from_id(atoi(p));
165                         break;
166                 default:
167                 }
168         }
169
170         if (!to_list || !from) {
171                 g_warning(_("Queued message header is broken.\n"));
172                 val = -1;
173         } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
174                 val = send_message_local(prefs_common.extsend_cmd, fp);
175         } else {
176                 if (!ac) {
177                         ac = account_find_from_smtp_server(from, server);
178                         if (!ac) {
179                                 g_warning(_("Account not found. "
180                                             "Using current account...\n"));
181                                 ac = cur_account;
182                         }
183                 }
184
185                 if (ac)
186                         val = send_message_smtp(ac, to_list, fp);
187                 else {
188                         PrefsAccount tmp_ac;
189
190                         g_warning(_("Account not found.\n"));
191
192                         memset(&tmp_ac, 0, sizeof(PrefsAccount));
193                         tmp_ac.address = from;
194                         tmp_ac.smtp_server = server;
195                         tmp_ac.smtpport = SMTP_PORT;
196                         val = send_message_smtp(&tmp_ac, to_list, fp);
197                 }
198         }
199
200         slist_free_strings(to_list);
201         g_slist_free(to_list);
202         g_free(from);
203         g_free(server);
204         fclose(fp);
205
206         return val;
207 }
208
209 static gint send_message_local(const gchar *command, FILE *fp)
210 {
211         FILE *pipefp;
212         gchar buf[BUFFSIZE];
213
214         g_return_val_if_fail(command != NULL, -1);
215         g_return_val_if_fail(fp != NULL, -1);
216
217         pipefp = popen(command, "w");
218         if (!pipefp) {
219                 g_warning(_("Can't execute external command: %s\n"), command);
220                 return -1;
221         }
222
223         while (fgets(buf, sizeof(buf), fp) != NULL) {
224                 strretchomp(buf);
225                 /* escape when a dot appears on the top */
226                 if (buf[0] == '.')
227                         fputc('.', pipefp);
228                 fputs(buf, pipefp);
229                 fputc('\n', pipefp);
230         }
231
232         pclose(pipefp);
233
234         return 0;
235 }
236
237 #define EXIT_IF_CANCELLED() \
238 { \
239         if (dialog->cancelled) { \
240                 sock_close(smtp_sock); \
241                 send_progress_dialog_destroy(dialog); \
242                 return -1; \
243         } \
244 }
245
246 #define SEND_EXIT_IF_ERROR(f, s) \
247 { \
248         EXIT_IF_CANCELLED(); \
249         if (!(f)) { \
250                 log_warning("Error occurred while %s\n", s); \
251                 sock_close(smtp_sock); \
252                 send_progress_dialog_destroy(dialog); \
253                 return -1; \
254         } \
255 }
256
257 #define SEND_EXIT_IF_NOTOK(f, s) \
258 { \
259         gint ok; \
260  \
261         EXIT_IF_CANCELLED(); \
262         if ((ok = (f)) != SM_OK) { \
263                 log_warning("Error occurred while %s\n", s); \
264                 if (ok == SM_AUTHFAIL) { \
265                         log_warning("SMTP AUTH failed\n"); \
266                         if (ac_prefs->tmp_pass) { \
267                                 g_free(ac_prefs->tmp_pass); \
268                                 ac_prefs->tmp_pass = NULL; \
269                         } \
270                 } \
271                 if (smtp_quit(smtp_sock) != SM_OK) \
272                         log_warning("Error occurred while sending QUIT\n"); \
273                 sock_close(smtp_sock); \
274                 send_progress_dialog_destroy(dialog); \
275                 return -1; \
276         } \
277 }
278
279 static gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list,
280                               FILE *fp)
281 {
282         SockInfo *smtp_sock = NULL;
283         SendProgressDialog *dialog;
284         GtkCList *clist;
285         const gchar *text[3];
286         gchar buf[BUFFSIZE];
287         gushort port;
288         gchar *domain;
289         gchar *pass = NULL;
290         GSList *cur;
291         gint size;
292
293         g_return_val_if_fail(ac_prefs != NULL, -1);
294         g_return_val_if_fail(ac_prefs->address != NULL, -1);
295         g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
296         g_return_val_if_fail(to_list != NULL, -1);
297         g_return_val_if_fail(fp != NULL, -1);
298
299         size = get_left_file_size(fp);
300         if (size < 0) return -1;
301
302 #if USE_SSL
303         port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
304                 ac_prefs->ssl_smtp == SSL_SMTP_TUNNEL ? SSMTP_PORT : SMTP_PORT;
305 #else
306         port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
307 #endif
308         domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
309
310         if (ac_prefs->use_smtp_auth) {
311                 if (ac_prefs->passwd)
312                         pass = ac_prefs->passwd;
313                 else if (ac_prefs->tmp_pass)
314                         pass = ac_prefs->tmp_pass;
315                 else {
316                         pass = send_query_password(ac_prefs->smtp_server,
317                                                    ac_prefs->userid);
318                         if (!pass) pass = g_strdup("");
319                         ac_prefs->tmp_pass = pass;
320                 }
321         }
322
323         dialog = send_progress_dialog_create();
324
325         text[0] = NULL;
326         text[1] = ac_prefs->smtp_server;
327         text[2] = _("Standby");
328         clist = GTK_CLIST(dialog->dialog->clist);
329         gtk_clist_append(clist, (gchar **)text);
330
331         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
332                    ac_prefs->smtp_server);
333         log_message("%s\n", buf);
334         progress_dialog_set_label(dialog->dialog, buf);
335         gtk_clist_set_text(clist, 0, 2, _("Connecting"));
336         GTK_EVENTS_FLUSH();
337
338 #if USE_SSL
339         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
340                                 (ac_prefs->smtp_server, port, domain,
341                                  ac_prefs->use_smtp_auth, ac_prefs->ssl_smtp)),
342                            "connecting to server");
343 #else
344         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
345                                 (ac_prefs->smtp_server, port, domain,
346                                  ac_prefs->use_smtp_auth)),
347                            "connecting to server");
348 #endif
349
350         progress_dialog_set_label(dialog->dialog, _("Sending MAIL FROM..."));
351         gtk_clist_set_text(clist, 0, 2, _("Sending"));
352         GTK_EVENTS_FLUSH();
353
354         SEND_EXIT_IF_NOTOK
355                 (smtp_from(smtp_sock, ac_prefs->address, ac_prefs->userid,
356                            pass, ac_prefs->use_smtp_auth),
357                  "sending MAIL FROM");
358
359         progress_dialog_set_label(dialog->dialog, _("Sending RCPT TO..."));
360         GTK_EVENTS_FLUSH();
361
362         for (cur = to_list; cur != NULL; cur = cur->next)
363                 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
364                                    "sending RCPT TO");
365
366         progress_dialog_set_label(dialog->dialog, _("Sending DATA..."));
367         GTK_EVENTS_FLUSH();
368
369         SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
370
371         /* send main part */
372         SEND_EXIT_IF_ERROR(send_message_data(dialog, smtp_sock, fp, size) == 0,
373                            "sending data");
374
375         progress_dialog_set_label(dialog->dialog, _("Quitting..."));
376         GTK_EVENTS_FLUSH();
377
378         SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
379         SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
380
381         sock_close(smtp_sock);
382         send_progress_dialog_destroy(dialog);
383
384         return 0;
385 }
386
387 #undef EXIT_IF_CANCELLED
388 #undef SEND_EXIT_IF_ERROR
389 #undef SEND_EXIT_IF_NOTOK
390
391 #define EXIT_IF_CANCELLED() \
392 { \
393         if (dialog->cancelled) return -1; \
394 }
395
396 #define SEND_EXIT_IF_ERROR(f) \
397 { \
398         EXIT_IF_CANCELLED(); \
399         if ((f) <= 0) return -1; \
400 }
401
402 #define SEND_DIALOG_UPDATE() \
403 { \
404         gettimeofday(&tv_cur, NULL); \
405         if (tv_cur.tv_sec - tv_prev.tv_sec > 0 || \
406             tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) { \
407                 g_snprintf(str, sizeof(str), \
408                            _("Sending message (%d / %d bytes)"), \
409                            bytes, size); \
410                 progress_dialog_set_label(dialog->dialog, str); \
411                 progress_dialog_set_percentage \
412                         (dialog->dialog, (gfloat)bytes / (gfloat)size); \
413                 GTK_EVENTS_FLUSH(); \
414                 gettimeofday(&tv_prev, NULL); \
415         } \
416 }
417
418 static gint send_message_data(SendProgressDialog *dialog, SockInfo *sock,
419                               FILE *fp, gint size)
420 {
421         gchar buf[BUFFSIZE];
422         gchar str[BUFFSIZE];
423         gint bytes = 0;
424         struct timeval tv_prev, tv_cur;
425
426         gettimeofday(&tv_prev, NULL);
427
428         /* output header part */
429         while (fgets(buf, sizeof(buf), fp) != NULL) {
430                 bytes += strlen(buf);
431                 strretchomp(buf);
432
433                 SEND_DIALOG_UPDATE();
434
435                 if (!g_strncasecmp(buf, "Bcc:", 4)) {
436                         gint next;
437
438                         for (;;) {
439                                 next = fgetc(fp);
440                                 if (next == EOF)
441                                         break;
442                                 else if (next != ' ' && next != '\t') {
443                                         ungetc(next, fp);
444                                         break;
445                                 }
446                                 if (fgets(buf, sizeof(buf), fp) == NULL)
447                                         break;
448                                 else
449                                         bytes += strlen(buf);
450                         }
451                 } else {
452                         SEND_EXIT_IF_ERROR(sock_puts(sock, buf));
453                         if (buf[0] == '\0')
454                                 break;
455                 }
456         }
457
458         /* output body part */
459         while (fgets(buf, sizeof(buf), fp) != NULL) {
460                 bytes += strlen(buf);
461                 strretchomp(buf);
462
463                 SEND_DIALOG_UPDATE();
464
465                 /* escape when a dot appears on the top */
466                 if (buf[0] == '.')
467                         SEND_EXIT_IF_ERROR(sock_write(sock, ".", 1));
468
469                 SEND_EXIT_IF_ERROR(sock_puts(sock, buf));
470         }
471
472         g_snprintf(str, sizeof(str), _("Sending message (%d / %d bytes)"),
473                    bytes, size);
474         progress_dialog_set_label(dialog->dialog, str);
475         progress_dialog_set_percentage
476                 (dialog->dialog, (gfloat)bytes / (gfloat)size);
477         GTK_EVENTS_FLUSH();
478
479         return 0;
480 }
481
482 #undef EXIT_IF_CANCELLED
483 #undef SEND_EXIT_IF_ERROR
484 #undef SEND_DIALOG_UPDATE
485
486 #if USE_SSL
487 static SockInfo *send_smtp_open(const gchar *server, gushort port,
488                                 const gchar *domain, gboolean use_smtp_auth,
489                                 SSLSMTPType ssl_type)
490 #else
491 static SockInfo *send_smtp_open(const gchar *server, gushort port,
492                                 const gchar *domain, gboolean use_smtp_auth)
493 #endif
494 {
495         SockInfo *sock;
496         gint val;
497
498         g_return_val_if_fail(server != NULL, NULL);
499
500         if ((sock = sock_connect(server, port)) == NULL) {
501                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
502                             server, port);
503                 return NULL;
504         }
505
506 #if USE_SSL
507         if (ssl_type == SSL_SMTP_TUNNEL && !ssl_init_socket(sock)) {
508                 log_warning(_("SSL connection failed"));
509                 sock_close(sock);
510                 return NULL;
511         }
512 #endif
513
514         if (smtp_ok(sock) != SM_OK) {
515                 log_warning(_("Error occurred while connecting to %s:%d\n"),
516                             server, port);
517                 sock_close(sock);
518                 return NULL;
519         }
520
521 #if USE_SSL
522         val = smtp_helo(sock, domain ? domain : get_domain_name(),
523                         use_smtp_auth || ssl_type == SSL_SMTP_STARTTLS);
524 #else
525         val = smtp_helo(sock, domain ? domain : get_domain_name(),
526                         use_smtp_auth);
527 #endif
528
529         if (val != SM_OK) {
530                 log_warning(_("Error occurred while sending HELO\n"));
531                 sock_close(sock);
532                 return NULL;
533         }
534
535 #if USE_SSL
536         if (ssl_type == SSL_SMTP_STARTTLS) {
537                 val = esmtp_starttls(sock);
538                 if (val != SM_OK) {
539                         log_warning(_("Error occurred while sending STARTTLS\n"));
540                         sock_close(sock);
541                         return NULL;
542                 }
543                 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
544                         sock_close(sock);
545                         return NULL;
546                 }
547                 val = esmtp_ehlo(sock, domain ? domain : get_domain_name());
548                 if (val != SM_OK) {
549                         log_warning(_("Error occurred while sending EHLO\n"));
550                         sock_close(sock);
551                         return NULL;
552                 }
553         }
554 #endif
555
556         return sock;
557 }
558
559
560 static SendProgressDialog *send_progress_dialog_create(void)
561 {
562         SendProgressDialog *dialog;
563         ProgressDialog *progress;
564
565         dialog = g_new0(SendProgressDialog, 1);
566
567         progress = progress_dialog_create();
568         gtk_window_set_title(GTK_WINDOW(progress->window),
569                              _("Sending message"));
570         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
571                            GTK_SIGNAL_FUNC(send_cancel), dialog);
572         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
573                            GTK_SIGNAL_FUNC(gtk_true), NULL);
574         gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
575         manage_window_set_transient(GTK_WINDOW(progress->window));
576
577         progress_dialog_set_value(progress, 0.0);
578
579         gtk_widget_show_now(progress->window);
580
581         dialog->dialog = progress;
582         dialog->queue_list = NULL;
583         dialog->cancelled = FALSE;
584
585         return dialog;
586 }
587
588 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
589 {
590         g_return_if_fail(dialog != NULL);
591
592         progress_dialog_destroy(dialog->dialog);
593         g_free(dialog);
594 }
595
596 static void send_cancel(GtkWidget *widget, gpointer data)
597 {
598         SendProgressDialog *dialog = data;
599
600         dialog->cancelled = TRUE;
601 }
602
603 static gchar *send_query_password(const gchar *server, const gchar *user)
604 {
605         gchar *message;
606         gchar *pass;
607
608         message = g_strdup_printf(_("Input password for %s on %s:"),
609                                   user, server);
610         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
611         g_free(message);
612
613         return pass;
614 }