enable NNTP over SSL
[claws.git] / src / nntp.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 <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "intl.h"
29 #include "nntp.h"
30 #include "socket.h"
31 #include "ssl.h"
32 #include "utils.h"
33
34 static gint verbose = 1;
35
36 static void nntp_gen_send       (NNTPSockInfo   *sock,
37                                  const gchar    *format,
38                                  ...);
39 static gint nntp_gen_recv       (NNTPSockInfo   *sock,
40                                  gchar          *buf,
41                                  gint            size);
42 static gint nntp_gen_command    (NNTPSockInfo   *sock,
43                                  gchar          *argbuf,
44                                  const gchar    *format,
45                                  ...);
46
47 #if USE_SSL
48 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf, gboolean use_ssl)
49 #else
50 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
51 #endif
52 {
53         SockInfo *sock;
54         NNTPSockInfo *nntp_sock;
55
56         if ((sock = sock_connect(server, port)) == NULL) {
57                 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
58                             server, port);
59                 return NULL;
60         }
61
62 #if USE_SSL
63         if (use_ssl && !ssl_init_socket(sock)) {
64                  sock_close(sock);
65                  return NULL;
66         }
67 #endif
68         
69         nntp_sock = g_new0(NNTPSockInfo, 1);
70         nntp_sock->sock = sock;
71
72         if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
73                 return nntp_sock;
74         else {
75                 sock_close(sock);
76                 g_free(nntp_sock);
77                 return NULL;
78         }
79 }
80
81 #if USE_SSL
82 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
83                              const gchar *userid, const gchar *passwd, gboolean use_ssl)
84 #else
85 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
86                              const gchar *userid, const gchar *passwd)
87 #endif
88 {
89         NNTPSockInfo *sock;
90
91 #if USE_SSL
92         sock = nntp_open(server, port, buf, use_ssl);
93 #else
94         sock = nntp_open(server, port, buf);
95 #endif
96
97         if (!sock) return NULL;
98
99         sock->userid = g_strdup(userid);
100         sock->passwd = g_strdup(passwd);
101
102         return sock;
103 }
104
105 void nntp_close(NNTPSockInfo *sock)
106 {
107         if (!sock) return;
108
109         sock_close(sock->sock);
110         g_free(sock->userid);
111         g_free(sock->passwd);
112         g_free(sock);
113 }
114
115 gint nntp_group(NNTPSockInfo *sock, const gchar *group,
116                 gint *num, gint *first, gint *last)
117 {
118         gint ok;
119         gint resp;
120         gchar buf[NNTPBUFSIZE];
121
122         ok = nntp_gen_command(sock, buf, "GROUP %s", group);
123
124         if (ok != NN_SUCCESS)
125                 return ok;
126
127         if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
128             != 4) {
129                 log_warning(_("protocol error: %s\n"), buf);
130                 return NN_PROTOCOL;
131         }
132
133         return NN_SUCCESS;
134 }
135
136 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
137                       gchar **msgid)
138 {
139         gint ok;
140         gchar buf[NNTPBUFSIZE];
141
142         if (num > 0)
143                 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
144         else
145                 ok = nntp_gen_command(sock, buf, cmd);
146
147         if (ok != NN_SUCCESS)
148                 return ok;
149
150         extract_parenthesis(buf, '<', '>');
151         if (buf[0] == '\0') {
152                 log_warning(_("protocol error\n"));
153                 return NN_PROTOCOL;
154         }
155         *msgid = g_strdup(buf);
156
157         return NN_SUCCESS;
158 }
159
160 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
161 {
162         return nntp_get_article(sock, "ARTICLE", num, msgid);
163 }
164
165 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
166 {
167         return nntp_get_article(sock, "BODY", num, msgid);
168 }
169
170 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
171 {
172         return nntp_get_article(sock, "HEAD", num, msgid);
173 }
174
175 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
176 {
177         return nntp_get_article(sock, "STAT", num, msgid);
178 }
179
180 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
181 {
182         gint ok;
183         gint resp;
184         gchar buf[NNTPBUFSIZE];
185
186         ok = nntp_gen_command(sock, buf, "NEXT");
187
188         if (ok != NN_SUCCESS)
189                 return ok;
190
191         if (sscanf(buf, "%d %d", &resp, num) != 2) {
192                 log_warning(_("protocol error: %s\n"), buf);
193                 return NN_PROTOCOL;
194         }
195
196         extract_parenthesis(buf, '<', '>');
197         if (buf[0] == '\0') {
198                 log_warning(_("protocol error\n"));
199                 return NN_PROTOCOL;
200         }
201         *msgid = g_strdup(buf);
202
203         return NN_SUCCESS;
204 }
205
206 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
207 {
208         gint ok;
209         gchar buf[NNTPBUFSIZE];
210
211         ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
212         if (ok != NN_SUCCESS)
213                 return ok;
214
215         return NN_SUCCESS;
216 }
217
218 gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
219 {
220         gint ok;
221         gchar buf[NNTPBUFSIZE];
222
223         ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
224         if (ok != NN_SUCCESS)
225                 return ok;
226
227         return NN_SUCCESS;
228 }
229
230 gint nntp_list(NNTPSockInfo *sock)
231 {
232         return nntp_gen_command(sock, NULL, "LIST");
233 }
234
235 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
236 {
237         gint ok;
238         gchar buf[NNTPBUFSIZE];
239
240         ok = nntp_gen_command(sock, buf, "POST");
241         if (ok != NN_SUCCESS)
242                 return ok;
243
244         while (fgets(buf, sizeof(buf), fp) != NULL) {
245                 strretchomp(buf);
246                 if (buf[0] == '.') {
247                         if (sock_write(sock->sock, ".", 1) < 0) {
248                                 log_warning(_("Error occurred while posting\n"));
249                                 return NN_SOCKET;
250                         }
251                 }
252
253                 if (sock_puts(sock->sock, buf) < 0) {
254                         log_warning(_("Error occurred while posting\n"));
255                         return NN_SOCKET;
256                 }
257         }
258
259         sock_write(sock->sock, ".\r\n", 3);
260         if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
261                 return ok;
262
263         return NN_SUCCESS;
264 }
265
266 gint nntp_newgroups(NNTPSockInfo *sock)
267 {
268         return NN_SUCCESS;
269 }
270
271 gint nntp_newnews(NNTPSockInfo *sock)
272 {
273         return NN_SUCCESS;
274 }
275
276 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
277 {
278         gint ok;
279
280         if (sock->auth_failed)
281                 return NN_AUTHREQ;
282
283         ok = nntp_gen_command(sock, NULL, "MODE %s",
284                               stream ? "STREAM" : "READER");
285
286         return ok;
287 }
288
289 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
290 {
291         gint ok;
292         gchar buf[NNTPBUFSIZE];
293
294         if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
295                 if (strlen(buf) < 3)
296                         return NN_ERROR;
297
298                 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
299                     (buf[3] == ' ' || buf[3] == '\0')) {
300                         if (argbuf)
301                                 strcpy(argbuf, buf);
302
303                         if (!strncmp(buf, "381", 3))
304                                 return NN_AUTHCONT;
305
306                         return NN_SUCCESS;
307                 } else if (!strncmp(buf, "480", 3))
308                         return NN_AUTHREQ;
309                 else
310                         return NN_ERROR;
311         }
312
313         return ok;
314 }
315
316 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
317 {
318         gchar buf[NNTPBUFSIZE];
319         va_list args;
320
321         va_start(args, format);
322         g_vsnprintf(buf, sizeof(buf), format, args);
323         va_end(args);
324
325         if (verbose) {
326                 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
327                         log_print("NNTP> AUTHINFO PASS ********\n");
328                 else
329                         log_print("NNTP> %s\n", buf);
330         }
331
332         strcat(buf, "\r\n");
333         sock_write(sock->sock, buf, strlen(buf));
334 }
335
336 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
337 {
338         if (sock_gets(sock->sock, buf, size) == -1)
339                 return NN_SOCKET;
340
341         strretchomp(buf);
342
343         if (verbose)
344                 log_print("NNTP< %s\n", buf);
345
346         return NN_SUCCESS;
347 }
348
349 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
350                              const gchar *format, ...)
351 {
352         gchar buf[NNTPBUFSIZE];
353         va_list args;
354         gint ok;
355
356         va_start(args, format);
357         g_vsnprintf(buf, sizeof(buf), format, args);
358         va_end(args);
359
360         nntp_gen_send(sock, "%s", buf);
361         ok = nntp_ok(sock, argbuf);
362         if (ok == NN_AUTHREQ) {
363                 if (!sock->userid || !sock->passwd) {
364                         sock->auth_failed = TRUE;
365                         return ok;
366                 }
367
368                 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
369                 ok = nntp_ok(sock, NULL);
370                 if (ok == NN_AUTHCONT) {
371                         nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
372                         ok = nntp_ok(sock, NULL);
373                 }
374                 if (ok != NN_SUCCESS) {
375                         sock->auth_failed = TRUE;
376                         return ok;
377                 }
378
379                 nntp_gen_send(sock, "%s", buf);
380                 ok = nntp_ok(sock, argbuf);
381         }
382
383         return ok;
384 }