3d9e0b692728d540939b631745a66cf4f8d296a1
[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 gint send_smtp_open      (const gchar *server, gushort port,
49                                  const gchar *domain, gboolean use_smtp_auth);
50
51 #define SEND_EXIT_IF_ERROR(f, s) \
52 { \
53         if ((f) < 0) { \
54                 log_warning("Error occurred while %s\n", s); \
55                 if (smtp_sock > 0) sock_close(smtp_sock); \
56                 return -1; \
57         } \
58 }
59
60 #define SEND_EXIT_IF_NOTOK(f, s) \
61 { \
62         if ((f) != SM_OK) { \
63                 log_warning("Error occurred while %s\n", s); \
64                 if (smtp_quit(smtp_sock) != SM_OK) \
65                         log_warning("Error occurred while sending QUIT\n"); \
66                 sock_close(smtp_sock); \
67                 return -1; \
68         } \
69 }
70
71 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
72 {
73         FILE *fp;
74         gint val;
75         gushort port;
76         gchar *domain;
77
78         g_return_val_if_fail(file != NULL, -1);
79         g_return_val_if_fail(ac_prefs != NULL, -1);
80         g_return_val_if_fail(to_list != NULL, -1);
81
82         if ((fp = fopen(file, "r")) == NULL) {
83                 FILE_OP_ERROR(file, "fopen");
84                 return -1;
85         }
86
87         port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
88         domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
89
90         val = send_message_smtp(to_list, ac_prefs->address,
91                                 ac_prefs->smtp_server, port, domain,
92                                 ac_prefs->userid, ac_prefs->passwd,
93                                 ac_prefs->use_smtp_auth, fp);
94
95         fclose(fp);
96         return val;
97 }
98
99 enum
100 {
101         Q_SENDER     = 0,
102         Q_SMTPSERVER = 1,
103         Q_RECIPIENTS = 2
104 };
105
106 gint send_message_queue(const gchar *file)
107 {
108         static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
109                                        {"SSV:", NULL, FALSE},
110                                        {"R:",   NULL, FALSE},
111                                        {NULL,   NULL, FALSE}};
112         FILE *fp;
113         gint val;
114         gchar *from = NULL;
115         gchar *server = NULL;
116         GSList *to_list = NULL;
117         gchar buf[BUFFSIZE];
118         gint hnum;
119
120         g_return_val_if_fail(file != NULL, -1);
121
122         if ((fp = fopen(file, "r")) == NULL) {
123                 FILE_OP_ERROR(file, "fopen");
124                 return -1;
125         }
126
127         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
128                != -1) {
129                 gchar *p = buf + strlen(qentry[hnum].name);
130
131                 switch (hnum) {
132                 case Q_SENDER:
133                         if (!from) from = g_strdup(p);
134                         break;
135                 case Q_SMTPSERVER:
136                         if (!server) server = g_strdup(p);
137                         break;
138                 case Q_RECIPIENTS:
139                         to_list = address_list_append(to_list, p);
140                         break;
141                 default:
142                 }
143         }
144
145         if (!to_list || !from || !server) {
146                 g_warning(_("Queued message header is broken.\n"));
147                 val = -1;
148         } else {
149                 gushort port;
150                 gchar *domain;
151                 PrefsAccount *ac;
152
153                 ac = account_find_from_smtp_server(from, server);
154                 if (!ac) {
155                         g_warning(_("Account not found. Using current account...\n"));
156                         ac = cur_account;
157                 }
158
159                 if (ac) {
160                         port = ac->set_smtpport ? ac->smtpport : SMTP_PORT;
161                         domain = ac->set_domain ? ac->domain : NULL;
162                         val = send_message_smtp
163                                 (to_list, from, server, port, domain,
164                                  ac->userid, ac->passwd, ac->use_smtp_auth, fp);
165                 } else {
166                         g_warning(_("Account not found.\n"));
167                         val = send_message_smtp
168                                 (to_list, from, server, SMTP_PORT, NULL,
169                                  NULL, NULL, FALSE, fp);
170                 }
171         }
172
173         slist_free_strings(to_list);
174         g_slist_free(to_list);
175         g_free(from);
176         g_free(server);
177         fclose(fp);
178
179         return val;
180 }
181
182 static gint send_message_smtp(GSList *to_list, const gchar *from,
183                               const gchar *server, gushort port,
184                               const gchar *domain, const gchar *userid,
185                               const gchar* passwd, gboolean use_smtp_auth,
186                               FILE *fp)
187 {
188         gint smtp_sock;
189         gchar buf[BUFFSIZE];
190         GSList *cur;
191
192         g_return_val_if_fail(to_list != NULL, -1);
193         g_return_val_if_fail(from != NULL, -1);
194         g_return_val_if_fail(server != NULL, -1);
195         g_return_val_if_fail(fp != NULL, -1);
196
197         log_message(_("Connecting to SMTP server: %s ...\n"), server);
198
199         SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
200                                 (server, port, domain, use_smtp_auth)),
201                            "connecting to server");
202         SEND_EXIT_IF_NOTOK
203                 (smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
204                  "sending MAIL FROM");
205         for (cur = to_list; cur != NULL; cur = cur->next)
206                 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
207                                    "sending RCPT TO");
208         SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
209
210         /* send main part */
211         while (fgets(buf, sizeof(buf), fp) != NULL) {
212                 strretchomp(buf);
213
214                 /* escape when a dot appears on the top */
215                 if (buf[0] == '.')
216                         SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
217                                            "sending data");
218
219                 SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
220         }
221
222         SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
223         SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
224
225         sock_close(smtp_sock);
226         return 0;
227 }
228
229 static gint send_smtp_open(const gchar *server, gushort port,
230                            const gchar *domain, gboolean use_smtp_auth)
231 {
232         SockInfo *sockinfo;
233         gint sock;
234         gint val;
235
236         g_return_val_if_fail(server != NULL, -1);
237
238         if ((sockinfo = sock_connect(server, port)) == NULL) {
239                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
240                             server, port);
241                 return -1;
242         }
243         sock = sockinfo->sock;
244         sock_sockinfo_free(sockinfo);
245
246         if (smtp_ok(sock) == SM_OK) {
247                 val = smtp_helo(sock, domain ? domain : get_domain_name(),
248                                 use_smtp_auth);
249                 if (val == SM_OK) return sock;
250         }
251
252         log_warning(_("Error occurred while sending HELO\n"));
253         sock_close(sock);
254
255         return -1;
256 }