removed local account without SMTP server - added option to use mail command
[claws.git] / src / send.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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 <stdio.h>
28 #include <string.h>
29
30 #include "intl.h"
31 #include "send.h"
32 #include "socket.h"
33 #include "smtp.h"
34 #include "prefs_account.h"
35 #include "account.h"
36 #include "compose.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "utils.h"
40
41 #define SMTP_PORT       25
42
43 static gint send_message_smtp   (GSList *to_list, const gchar *from,
44                                  const gchar *server, gushort port,
45                                  const gchar *domain, const gchar *userid,
46                                  const gchar *passwd, gboolean use_smtp_auth,
47                                  FILE *fp);
48 static SockInfo *send_smtp_open (const gchar *server, gushort port,
49                                  const gchar *domain, gboolean use_smtp_auth);
50 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
51                                       FILE * fp);
52
53 #define SEND_EXIT_IF_ERROR(f, s) \
54 { \
55         if (!(f)) { \
56                 log_warning("Error occurred while %s\n", s); \
57                 sock_close(smtp_sock); \
58                 smtp_sock = NULL; \
59                 return -1; \
60         } \
61 }
62
63 #define SEND_EXIT_IF_NOTOK(f, s) \
64 { \
65         if ((f) != SM_OK) { \
66                 log_warning("Error occurred while %s\n", s); \
67                 if (smtp_quit(smtp_sock) != SM_OK) \
68                         log_warning("Error occurred while sending QUIT\n"); \
69                 sock_close(smtp_sock); \
70                 smtp_sock = NULL; \
71                 return -1; \
72         } \
73 }
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                 {
93                         val = send_message_with_command(to_list,
94                                                         ac_prefs->mail_command,
95                                                         fp);
96                 }
97         else
98                 {
99                         port = ac_prefs->set_smtpport
100                                 ? ac_prefs->smtpport : SMTP_PORT;
101                         domain = ac_prefs->set_domain
102                                 ? ac_prefs->domain : NULL;
103                         
104                         val = send_message_smtp(to_list, ac_prefs->address,
105                                                 ac_prefs->smtp_server, port,
106                                                 domain, ac_prefs->userid,
107                                                 ac_prefs->passwd,
108                                                 ac_prefs->use_smtp_auth, fp);
109                 }
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 };
121
122 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
123                                       FILE * fp)
124 {
125         FILE * p;
126         int len;
127         gchar * cmdline;
128         GSList *cur;
129         gchar buf[BUFFSIZE];
130
131         len = strlen(mailcmd);
132         for (cur = to_list; cur != NULL; cur = cur->next)
133                 len += strlen((gchar *)cur->data) + 1;
134
135         cmdline = g_new(gchar, len + 1);
136         strcpy(cmdline, mailcmd);
137
138         for (cur = to_list; cur != NULL; cur = cur->next)
139                 {
140                         cmdline = strncat(cmdline, " ", len);
141                         cmdline = strncat(cmdline, (gchar *)cur->data, len);
142                 }
143
144         log_message(_("Using command to send mail: %s ...\n"), cmdline);
145
146         p = popen(cmdline, "w");
147         if (p != NULL)
148                 {
149                         while (fgets(buf, sizeof(buf), fp) != NULL) {
150                                 strretchomp(buf);
151                                 
152                                 /* escape when a dot appears on the top */
153                                 if (buf[0] == '.')
154                                         fputs(".", p);
155                                 
156                                 fputs(buf, p);
157                                 fputs("\n", p);
158                         }
159                 }
160         pclose(p);
161
162         log_message(_("Mail sent successfully ...\n"));
163
164         return 0;
165 }
166
167 gint send_message_queue(const gchar *file)
168 {
169         static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
170                                        {"SSV:", NULL, FALSE},
171                                        {"R:",   NULL, FALSE},
172                                        {NULL,   NULL, FALSE}};
173         FILE *fp;
174         gint val;
175         gchar *from = NULL;
176         gchar *server = NULL;
177         GSList *to_list = NULL;
178         gchar buf[BUFFSIZE];
179         gint hnum;
180
181         g_return_val_if_fail(file != NULL, -1);
182
183         if ((fp = fopen(file, "r")) == NULL) {
184                 FILE_OP_ERROR(file, "fopen");
185                 return -1;
186         }
187
188         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
189                != -1) {
190                 gchar *p = buf + strlen(qentry[hnum].name);
191
192                 switch (hnum) {
193                 case Q_SENDER:
194                         if (!from) from = g_strdup(p);
195                         break;
196                 case Q_SMTPSERVER:
197                         if (!server) server = g_strdup(p);
198                         break;
199                 case Q_RECIPIENTS:
200                         to_list = address_list_append(to_list, p);
201                         break;
202                 default:
203                 }
204         }
205
206         if (!to_list || !from || !server) {
207                 g_warning(_("Queued message header is broken.\n"));
208                 val = -1;
209         } else {
210                 gushort port;
211                 gchar *domain;
212                 PrefsAccount *ac;
213
214                 ac = account_find_from_smtp_server(from, server);
215                 if (!ac) {
216                         g_warning(_("Account not found. Using current account...\n"));
217                         ac = cur_account;
218                 }
219
220                 if (ac) {
221                         port = ac->set_smtpport ? ac->smtpport : SMTP_PORT;
222                         domain = ac->set_domain ? ac->domain : NULL;
223                         val = send_message_smtp
224                                 (to_list, from, server, port, domain,
225                                  ac->userid, ac->passwd, ac->use_smtp_auth, fp);
226                 } else {
227                         g_warning(_("Account not found.\n"));
228                         val = send_message_smtp
229                                 (to_list, from, server, SMTP_PORT, NULL,
230                                  NULL, NULL, FALSE, fp);
231                 }
232         }
233
234         slist_free_strings(to_list);
235         g_slist_free(to_list);
236         g_free(from);
237         g_free(server);
238         fclose(fp);
239
240         return val;
241 }
242
243 static gint send_message_smtp(GSList *to_list, const gchar *from,
244                               const gchar *server, gushort port,
245                               const gchar *domain, const gchar *userid,
246                               const gchar* passwd, gboolean use_smtp_auth,
247                               FILE *fp)
248 {
249         SockInfo *smtp_sock;
250         gchar buf[BUFFSIZE];
251         GSList *cur;
252
253         g_return_val_if_fail(to_list != NULL, -1);
254         g_return_val_if_fail(from != NULL, -1);
255         g_return_val_if_fail(server != NULL, -1);
256         g_return_val_if_fail(fp != NULL, -1);
257
258         log_message(_("Connecting to SMTP server: %s ...\n"), server);
259
260         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
261                                 (server, port, domain, use_smtp_auth)),
262                            "connecting to server");
263         SEND_EXIT_IF_NOTOK
264                 (smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
265                  "sending MAIL FROM");
266         for (cur = to_list; cur != NULL; cur = cur->next)
267                 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
268                                    "sending RCPT TO");
269         SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
270
271         /* send main part */
272         while (fgets(buf, sizeof(buf), fp) != NULL) {
273                 strretchomp(buf);
274
275                 /* escape when a dot appears on the top */
276                 if (buf[0] == '.')
277                         SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
278                                            "sending data");
279
280                 SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
281         }
282
283         SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
284         SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
285
286         sock_close(smtp_sock);
287         return 0;
288 }
289
290 static SockInfo *send_smtp_open(const gchar *server, gushort port,
291                            const gchar *domain, gboolean use_smtp_auth)
292 {
293         SockInfo *sock;
294         gint val;
295
296         g_return_val_if_fail(server != NULL, NULL);
297
298         if ((sock = sock_connect(server, port)) == NULL) {
299                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
300                             server, port);
301                 return NULL;
302         }
303
304         if (smtp_ok(sock) == SM_OK) {
305                 val = smtp_helo(sock, domain ? domain : get_domain_name(),
306                                 use_smtp_auth);
307                 if (val == SM_OK) return sock;
308         }
309
310         log_warning(_("Error occurred while sending HELO\n"));
311         sock_close(sock);
312
313         return NULL;
314 }