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