SMTP over SSL (untested, feedback requested)
[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 "smtp.h"
40 #include "prefs_account.h"
41 #include "account.h"
42 #include "compose.h"
43 #include "progressdialog.h"
44 #include "manage_window.h"
45 #include "procmsg.h"
46 #include "procheader.h"
47 #include "utils.h"
48 #include "gtkutils.h"
49
50 #define SMTP_PORT       25
51 #if USE_SSL
52 #define SSMTP_PORT      465
53 #endif
54
55 typedef struct _SendProgressDialog      SendProgressDialog;
56
57 struct _SendProgressDialog
58 {
59         ProgressDialog *dialog;
60         GList *queue_list;
61         gboolean cancelled;
62 };
63
64 #if !USE_SSL
65 static gint send_message_smtp   (GSList *to_list, const gchar *from,
66                                  const gchar *server, gushort port,
67                                  const gchar *domain, const gchar *userid,
68                                  const gchar *passwd, gboolean use_smtp_auth,
69                                  FILE *fp);
70
71 static SockInfo *send_smtp_open (const gchar *server, gushort port,
72                                  const gchar *domain, gboolean use_smtp_auth);
73 #else
74 static gint send_message_smtp   (GSList *to_list, const gchar *from,
75                                  const gchar *server, gushort port,
76                                  const gchar *domain, const gchar *userid,
77                                  const gchar *passwd, gboolean use_smtp_auth,
78                                  FILE *fp, gboolean use_ssl);
79
80 static SockInfo *send_smtp_open (const gchar *server, gushort port,
81                                  const gchar *domain, gboolean use_smtp_auth, gboolean use_ssl);
82 #endif
83
84 static SendProgressDialog *send_progress_dialog_create(void);
85 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
86 static void send_cancel(GtkWidget *widget, gpointer data);
87
88 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
89                                       FILE * fp);
90
91 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
92 {
93         FILE *fp;
94         gint val;
95         gushort port;
96         gchar *domain;
97
98         g_return_val_if_fail(file != NULL, -1);
99         g_return_val_if_fail(ac_prefs != NULL, -1);
100         g_return_val_if_fail(to_list != NULL, -1);
101
102         if ((fp = fopen(file, "r")) == NULL) {
103                 FILE_OP_ERROR(file, "fopen");
104                 return -1;
105         }
106
107         if (ac_prefs->protocol == A_LOCAL && ac_prefs->use_mail_command) {
108                 val = send_message_with_command(to_list,
109                                                 ac_prefs->mail_command,
110                                                 fp);
111         } else {
112 #if USE_SSL
113                 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : (ac_prefs->smtp_ssl ? SSMTP_PORT : SMTP_PORT);
114 #else
115                 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
116 #endif
117                 domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
118
119                 val = send_message_smtp(to_list, ac_prefs->address,
120                                         ac_prefs->smtp_server, port, domain,
121                                         ac_prefs->userid, ac_prefs->passwd,
122                                         ac_prefs->use_smtp_auth, fp
123 #if USE_SSL
124                                         , ac_prefs->smtp_ssl
125 #endif
126                                         );
127         }
128
129         fclose(fp);
130         return val;
131 }
132
133 enum
134 {
135         Q_SENDER     = 0,
136         Q_SMTPSERVER = 1,
137         Q_RECIPIENTS = 2
138 };
139
140 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
141                                       FILE * fp)
142 {
143         FILE * p;
144         int len;
145         gchar * cmdline;
146         GSList *cur;
147         gchar buf[BUFFSIZE];
148
149         len = strlen(mailcmd);
150         for (cur = to_list; cur != NULL; cur = cur->next)
151                 len += strlen((gchar *)cur->data) + 1;
152
153         cmdline = g_new(gchar, len + 1);
154         strcpy(cmdline, mailcmd);
155
156         for (cur = to_list; cur != NULL; cur = cur->next) {
157                 cmdline = strncat(cmdline, " ", len);
158                 cmdline = strncat(cmdline, (gchar *)cur->data, len);
159         }
160
161         log_message(_("Using command to send mail: %s ...\n"), cmdline);
162
163         p = popen(cmdline, "w");
164         if (p != NULL) {
165                 while (fgets(buf, sizeof(buf), fp) != NULL) {
166                         strretchomp(buf);
167
168                         /* escape when a dot appears on the top */
169                         if (buf[0] == '.')
170                                 fputs(".", p);
171
172                         fputs(buf, p);
173                         fputs("\n", p);
174                 }
175                 pclose(p);
176         }
177
178         log_message(_("Mail sent successfully ...\n"));
179
180         return 0;
181 }
182
183 gint send_message_queue(const gchar *file)
184 {
185         static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
186                                        {"SSV:", NULL, FALSE},
187                                        {"R:",   NULL, FALSE},
188                                        {NULL,   NULL, FALSE}};
189         FILE *fp;
190         gint val;
191         gchar *from = NULL;
192         gchar *server = NULL;
193         GSList *to_list = NULL;
194         gchar buf[BUFFSIZE];
195         gint hnum;
196
197         g_return_val_if_fail(file != NULL, -1);
198
199         if ((fp = fopen(file, "r")) == NULL) {
200                 FILE_OP_ERROR(file, "fopen");
201                 return -1;
202         }
203
204         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
205                != -1) {
206                 gchar *p = buf + strlen(qentry[hnum].name);
207
208                 switch (hnum) {
209                 case Q_SENDER:
210                         if (!from) from = g_strdup(p);
211                         break;
212                 case Q_SMTPSERVER:
213                         if (!server) server = g_strdup(p);
214                         break;
215                 case Q_RECIPIENTS:
216                         to_list = address_list_append(to_list, p);
217                         break;
218                 default:
219                 }
220         }
221
222         if (!to_list || !from || !server) {
223                 g_warning(_("Queued message header is broken.\n"));
224                 val = -1;
225         } else {
226                 gushort port;
227                 gchar *domain;
228                 PrefsAccount *ac;
229
230                 ac = account_find_from_smtp_server(from, server);
231                 if (!ac) {
232                         g_warning(_("Account not found. Using current account...\n"));
233                         ac = cur_account;
234                 }
235
236                 if (ac) {
237 #if !USE_SSL
238                         port = ac->set_smtpport ? ac->smtpport : SMTP_PORT;
239 #else
240                         port = ac->set_smtpport ? ac->smtpport : (ac->smtp_ssl ? SSMTP_PORT : SMTP_PORT);
241 #endif
242                         domain = ac->set_domain ? ac->domain : NULL;
243 #if !USE_SSL
244                         val = send_message_smtp
245                                 (to_list, from, server, port, domain,
246                                  ac->userid, ac->passwd, ac->use_smtp_auth, fp);
247 #else
248                         val = send_message_smtp
249                                 (to_list, from, server, port, domain,
250                                  ac->userid, ac->passwd, ac->use_smtp_auth, fp, ac->smtp_ssl);
251 #endif
252                 } else {
253                         g_warning(_("Account not found.\n"));
254 #if !USE_SSL
255                         val = send_message_smtp
256                                 (to_list, from, server, SMTP_PORT, NULL,
257                                  NULL, NULL, FALSE, fp);
258 #else
259                         val = send_message_smtp
260                                 (to_list, from, server, SMTP_PORT, NULL,
261                                  NULL, NULL, FALSE, fp, FALSE);
262 #endif
263                 }
264         }
265
266         slist_free_strings(to_list);
267         g_slist_free(to_list);
268         g_free(from);
269         g_free(server);
270         fclose(fp);
271
272         return val;
273 }
274
275 #define EXIT_IF_CANCELLED() \
276 { \
277         if (dialog->cancelled) { \
278                 sock_close(smtp_sock); \
279                 send_progress_dialog_destroy(dialog); \
280                 return -1; \
281         } \
282 }
283
284 #define SEND_EXIT_IF_ERROR(f, s) \
285 { \
286         EXIT_IF_CANCELLED(); \
287         if (!(f)) { \
288                 log_warning("Error occurred while %s\n", s); \
289                 sock_close(smtp_sock); \
290                 send_progress_dialog_destroy(dialog); \
291                 return -1; \
292         } \
293 }
294
295 #define SEND_EXIT_IF_NOTOK(f, s) \
296 { \
297         EXIT_IF_CANCELLED(); \
298         if ((f) != SM_OK) { \
299                 log_warning("Error occurred while %s\n", s); \
300                 if (smtp_quit(smtp_sock) != SM_OK) \
301                         log_warning("Error occurred while sending QUIT\n"); \
302                 sock_close(smtp_sock); \
303                 send_progress_dialog_destroy(dialog); \
304                 return -1; \
305         } \
306 }
307
308 #if !USE_SSL
309 static gint send_message_smtp(GSList *to_list, const gchar *from,
310                               const gchar *server, gushort port,
311                               const gchar *domain, const gchar *userid,
312                               const gchar *passwd, gboolean use_smtp_auth,
313                               FILE *fp)
314 #else
315 static gint send_message_smtp(GSList *to_list, const gchar *from,
316                               const gchar *server, gushort port,
317                               const gchar *domain, const gchar *userid,
318                               const gchar *passwd, gboolean use_smtp_auth,
319                               FILE *fp, gboolean use_ssl)
320 #endif
321 {
322         SockInfo *smtp_sock = NULL;
323         SendProgressDialog *dialog;
324         GtkCList *clist;
325         const gchar *text[3];
326         gchar buf[BUFFSIZE];
327         gchar str[BUFFSIZE];
328         GSList *cur;
329         gint size;
330         gint bytes = 0;
331         struct timeval tv_prev, tv_cur;
332
333         g_return_val_if_fail(to_list != NULL, -1);
334         g_return_val_if_fail(from != NULL, -1);
335         g_return_val_if_fail(server != NULL, -1);
336         g_return_val_if_fail(fp != NULL, -1);
337
338         size = get_left_file_size(fp);
339         if (size < 0) return -1;
340
341         dialog = send_progress_dialog_create();
342
343         text[0] = NULL;
344         text[1] = server;
345         text[2] = _("Standby");
346         clist = GTK_CLIST(dialog->dialog->clist);
347         gtk_clist_append(clist, (gchar **)text);
348
349         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
350                    server);
351         log_message("%s\n", buf);
352         progress_dialog_set_label(dialog->dialog, buf);
353         gtk_clist_set_text(clist, 0, 2, _("Connecting"));
354         GTK_EVENTS_FLUSH();
355
356 #if !USE_SSL
357         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
358                                 (server, port, domain, use_smtp_auth)),
359                            "connecting to server");
360 #else
361         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
362                                 (server, port, domain, use_smtp_auth, use_ssl)),
363                            "connecting to server");
364 #endif
365
366         progress_dialog_set_label(dialog->dialog, _("Sending MAIL FROM..."));
367         gtk_clist_set_text(clist, 0, 2, _("Sending"));
368         GTK_EVENTS_FLUSH();
369
370         SEND_EXIT_IF_NOTOK
371                 (smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
372                  "sending MAIL FROM");
373
374         progress_dialog_set_label(dialog->dialog, _("Sending RCPT TO..."));
375         GTK_EVENTS_FLUSH();
376
377         for (cur = to_list; cur != NULL; cur = cur->next)
378                 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
379                                    "sending RCPT TO");
380
381         progress_dialog_set_label(dialog->dialog, _("Sending DATA..."));
382         GTK_EVENTS_FLUSH();
383
384         SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
385
386         gettimeofday(&tv_prev, NULL);
387
388         /* send main part */
389         while (fgets(buf, sizeof(buf), fp) != NULL) {
390                 bytes += strlen(buf);
391                 strretchomp(buf);
392
393                 gettimeofday(&tv_cur, NULL);
394                 if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
395                     tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) {
396                         g_snprintf(str, sizeof(str),
397                                    _("Sending message (%d / %d bytes)"),
398                                    bytes, size);
399                         progress_dialog_set_label(dialog->dialog, str);
400                         progress_dialog_set_percentage
401                                 (dialog->dialog, (gfloat)bytes / (gfloat)size);
402                         GTK_EVENTS_FLUSH();
403                         gettimeofday(&tv_prev, NULL);
404                 }
405
406                 /* escape when a dot appears on the top */
407                 if (buf[0] == '.')
408                         SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
409                                            "sending data");
410
411                 SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
412         }
413
414         progress_dialog_set_label(dialog->dialog, _("Quitting..."));
415         GTK_EVENTS_FLUSH();
416
417         SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
418         SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
419
420 #if USE_SSL
421         ssl_done_socket(smtp_sock);
422 #endif
423
424         sock_close(smtp_sock);
425         send_progress_dialog_destroy(dialog);
426
427         return 0;
428 }
429
430 #if !USE_SSL
431 static SockInfo *send_smtp_open(const gchar *server, gushort port,
432                            const gchar *domain, gboolean use_smtp_auth)
433 #else
434 static SockInfo *send_smtp_open(const gchar *server, gushort port,
435                            const gchar *domain, gboolean use_smtp_auth, gboolean use_ssl)
436 #endif
437 {
438         SockInfo *sock;
439         gint val;
440
441         g_return_val_if_fail(server != NULL, NULL);
442
443         if ((sock = sock_connect(server, port)) == NULL) {
444                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
445                             server, port);
446                 return NULL;
447         }
448
449 #if USE_SSL
450         if(use_ssl && !ssl_init_socket(sock)) {
451                 log_warning(_("SSL connection failed"));
452                 sock_close(sock);
453                 return NULL;
454         }
455 #endif
456
457         if (smtp_ok(sock) == SM_OK) {
458                 val = smtp_helo(sock, domain ? domain : get_domain_name(),
459                                 use_smtp_auth);
460                 if (val == SM_OK) return sock;
461         }
462
463         log_warning(_("Error occurred while sending HELO\n"));
464         sock_close(sock);
465
466         return NULL;
467 }
468
469
470 static SendProgressDialog *send_progress_dialog_create(void)
471 {
472         SendProgressDialog *dialog;
473         ProgressDialog *progress;
474
475         dialog = g_new0(SendProgressDialog, 1);
476
477         progress = progress_dialog_create();
478         gtk_window_set_title(GTK_WINDOW(progress->window),
479                              _("Sending message"));
480         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
481                            GTK_SIGNAL_FUNC(send_cancel), dialog);
482         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
483                            GTK_SIGNAL_FUNC(gtk_true), NULL);
484         manage_window_set_transient(GTK_WINDOW(progress->window));
485
486         progress_dialog_set_value(progress, 0.0);
487
488         gtk_widget_show_now(progress->window);
489
490         dialog->dialog = progress;
491         dialog->queue_list = NULL;
492         dialog->cancelled = FALSE;
493
494         return dialog;
495 }
496
497 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
498 {
499         g_return_if_fail(dialog != NULL);
500
501         progress_dialog_destroy(dialog->dialog);
502         g_free(dialog);
503 }
504
505 static void send_cancel(GtkWidget *widget, gpointer data)
506 {
507         SendProgressDialog *dialog = data;
508
509         dialog->cancelled = TRUE;
510 }