kill score / folder scoring / buf fixed for local account prefs
[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 };
59
60 static gint send_message_smtp   (GSList *to_list, const gchar *from,
61                                  const gchar *server, gushort port,
62                                  const gchar *domain, const gchar *userid,
63                                  const gchar *passwd, gboolean use_smtp_auth,
64                                  FILE *fp);
65 static SockInfo *send_smtp_open (const gchar *server, gushort port,
66                                  const gchar *domain, gboolean use_smtp_auth);
67
68 static SendProgressDialog *send_progress_dialog_create(void);
69 static void send_progress_dialog_destroy(SendProgressDialog *dialog);
70 static void send_cancel(GtkWidget *widget, gpointer data);
71
72 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
73                                       FILE * fp);
74
75 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
76 {
77         FILE *fp;
78         gint val;
79         gushort port;
80         gchar *domain;
81
82         g_return_val_if_fail(file != NULL, -1);
83         g_return_val_if_fail(ac_prefs != NULL, -1);
84         g_return_val_if_fail(to_list != NULL, -1);
85
86         if ((fp = fopen(file, "r")) == NULL) {
87                 FILE_OP_ERROR(file, "fopen");
88                 return -1;
89         }
90
91         if (ac_prefs->protocol == A_LOCAL && ac_prefs->use_mail_command) {
92                 val = send_message_with_command(to_list,
93                                                 ac_prefs->mail_command,
94                                                 fp);
95         } else {
96                 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
97                 domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
98
99                 val = send_message_smtp(to_list, ac_prefs->address,
100                                         ac_prefs->smtp_server, port,
101                                         domain, ac_prefs->userid,
102                                         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 SEND_EXIT_IF_ERROR(f, s) \
237 { \
238         if (!(f)) { \
239                 log_warning("Error occurred while %s\n", s); \
240                 sock_close(smtp_sock); \
241                 smtp_sock = NULL; \
242                 send_progress_dialog_destroy(dialog); \
243                 return -1; \
244         } \
245 }
246
247 #define SEND_EXIT_IF_NOTOK(f, s) \
248 { \
249         if ((f) != SM_OK) { \
250                 log_warning("Error occurred while %s\n", s); \
251                 if (smtp_quit(smtp_sock) != SM_OK) \
252                         log_warning("Error occurred while sending QUIT\n"); \
253                 sock_close(smtp_sock); \
254                 smtp_sock = NULL; \
255                 send_progress_dialog_destroy(dialog); \
256                 return -1; \
257         } \
258 }
259
260 static gint send_message_smtp(GSList *to_list, const gchar *from,
261                               const gchar *server, gushort port,
262                               const gchar *domain, const gchar *userid,
263                               const gchar *passwd, gboolean use_smtp_auth,
264                               FILE *fp)
265 {
266         SockInfo *smtp_sock;
267         SendProgressDialog *dialog;
268         GtkCList *clist;
269         const gchar *text[3];
270         gchar buf[BUFFSIZE];
271         gchar str[BUFFSIZE];
272         GSList *cur;
273         gint size;
274         gint bytes = 0;
275         struct timeval tv_prev, tv_cur;
276
277         g_return_val_if_fail(to_list != NULL, -1);
278         g_return_val_if_fail(from != NULL, -1);
279         g_return_val_if_fail(server != NULL, -1);
280         g_return_val_if_fail(fp != NULL, -1);
281
282         size = get_left_file_size(fp);
283         if (size < 0) return -1;
284
285         dialog = send_progress_dialog_create();
286
287         text[0] = NULL;
288         text[1] = server;
289         text[2] = _("Standby");
290         clist = GTK_CLIST(dialog->dialog->clist);
291         gtk_clist_append(clist, (gchar **)text);
292
293         g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
294                    server);
295         log_message("%s\n", buf);
296         progress_dialog_set_label(dialog->dialog, buf);
297         gtk_clist_set_text(clist, 0, 2, _("Connecting"));
298         GTK_EVENTS_FLUSH();
299
300         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
301                                 (server, port, domain, use_smtp_auth)),
302                            "connecting to server");
303
304         progress_dialog_set_label(dialog->dialog, _("Sending MAIL FROM..."));
305         gtk_clist_set_text(clist, 0, 2, _("Sending"));
306         GTK_EVENTS_FLUSH();
307
308         SEND_EXIT_IF_NOTOK
309                 (smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
310                  "sending MAIL FROM");
311
312         progress_dialog_set_label(dialog->dialog, _("Sending RCPT TO..."));
313         GTK_EVENTS_FLUSH();
314
315         for (cur = to_list; cur != NULL; cur = cur->next)
316                 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
317                                    "sending RCPT TO");
318
319         progress_dialog_set_label(dialog->dialog, _("Sending DATA..."));
320         GTK_EVENTS_FLUSH();
321
322         SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
323
324         gettimeofday(&tv_prev, NULL);
325
326         /* send main part */
327         while (fgets(buf, sizeof(buf), fp) != NULL) {
328                 bytes += strlen(buf);
329                 strretchomp(buf);
330
331                 gettimeofday(&tv_cur, NULL);
332                 if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
333                     tv_cur.tv_usec - tv_prev.tv_usec > 10000) {
334                         g_snprintf(str, sizeof(str),
335                                    _("Sending message (%d / %d bytes)"),
336                                    bytes, size);
337                         progress_dialog_set_label(dialog->dialog, str);
338                         progress_dialog_set_percentage
339                                 (dialog->dialog, (gfloat)bytes / (gfloat)size);
340                         GTK_EVENTS_FLUSH();
341                         gettimeofday(&tv_prev, NULL);
342                 }
343
344                 /* escape when a dot appears on the top */
345                 if (buf[0] == '.')
346                         SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
347                                            "sending data");
348
349                 SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
350         }
351
352         progress_dialog_set_label(dialog->dialog, _("Quitting..."));
353         GTK_EVENTS_FLUSH();
354
355         SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
356         SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
357
358         sock_close(smtp_sock);
359         send_progress_dialog_destroy(dialog);
360
361         return 0;
362 }
363
364 static SockInfo *send_smtp_open(const gchar *server, gushort port,
365                            const gchar *domain, gboolean use_smtp_auth)
366 {
367         SockInfo *sock;
368         gint val;
369
370         g_return_val_if_fail(server != NULL, NULL);
371
372         if ((sock = sock_connect(server, port)) == NULL) {
373                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
374                             server, port);
375                 return NULL;
376         }
377
378         if (smtp_ok(sock) == SM_OK) {
379                 val = smtp_helo(sock, domain ? domain : get_domain_name(),
380                                 use_smtp_auth);
381                 if (val == SM_OK) return sock;
382         }
383
384         log_warning(_("Error occurred while sending HELO\n"));
385         sock_close(sock);
386
387         return NULL;
388 }
389
390
391 static SendProgressDialog *send_progress_dialog_create(void)
392 {
393         SendProgressDialog *dialog;
394         ProgressDialog *progress;
395
396         dialog = g_new0(SendProgressDialog, 1);
397
398         progress = progress_dialog_create();
399         gtk_window_set_title(GTK_WINDOW(progress->window),
400                              _("Sending message"));
401         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
402                            GTK_SIGNAL_FUNC(send_cancel), dialog);
403         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
404                            GTK_SIGNAL_FUNC(gtk_true), NULL);
405         manage_window_set_transient(GTK_WINDOW(progress->window));
406
407         progress_dialog_set_value(progress, 0.0);
408
409         gtk_widget_show_now(progress->window);
410
411         dialog->dialog = progress;
412         dialog->queue_list = NULL;
413
414         return dialog;
415 }
416
417 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
418 {
419         g_return_if_fail(dialog != NULL);
420
421         progress_dialog_destroy(dialog->dialog);
422         g_free(dialog);
423 }
424
425 static void send_cancel(GtkWidget *widget, gpointer data)
426 {
427         SendProgressDialog *dialog = data;
428
429         g_print("cancelled\n");
430 }