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