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