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