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