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