sync with 0.7.4cvs39
[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                 ok = nntp_gen_command(sock, buf, "MODE READER");
126                 if (ok == NN_SUCCESS)
127                         ok = nntp_gen_command(sock, buf, "GROUP %s", group);
128         }       
129         if (ok != NN_SUCCESS)
130                 return ok;
131
132         if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
133             != 4) {
134                 log_warning(_("protocol error: %s\n"), buf);
135                 return NN_PROTOCOL;
136         }
137
138         return NN_SUCCESS;
139 }
140
141 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
142                       gchar **msgid)
143 {
144         gint ok;
145         gchar buf[NNTPBUFSIZE];
146
147         if (num > 0)
148                 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
149         else
150                 ok = nntp_gen_command(sock, buf, cmd);
151
152         if (ok != NN_SUCCESS)
153                 return ok;
154
155         extract_parenthesis(buf, '<', '>');
156         if (buf[0] == '\0') {
157                 log_warning(_("protocol error\n"));
158                 return NN_PROTOCOL;
159         }
160         *msgid = g_strdup(buf);
161
162         return NN_SUCCESS;
163 }
164
165 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
166 {
167         return nntp_get_article(sock, "ARTICLE", num, msgid);
168 }
169
170 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
171 {
172         return nntp_get_article(sock, "BODY", num, msgid);
173 }
174
175 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
176 {
177         return nntp_get_article(sock, "HEAD", num, msgid);
178 }
179
180 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
181 {
182         return nntp_get_article(sock, "STAT", num, msgid);
183 }
184
185 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
186 {
187         gint ok;
188         gint resp;
189         gchar buf[NNTPBUFSIZE];
190
191         ok = nntp_gen_command(sock, buf, "NEXT");
192
193         if (ok != NN_SUCCESS)
194                 return ok;
195
196         if (sscanf(buf, "%d %d", &resp, num) != 2) {
197                 log_warning(_("protocol error: %s\n"), buf);
198                 return NN_PROTOCOL;
199         }
200
201         extract_parenthesis(buf, '<', '>');
202         if (buf[0] == '\0') {
203                 log_warning(_("protocol error\n"));
204                 return NN_PROTOCOL;
205         }
206         *msgid = g_strdup(buf);
207
208         return NN_SUCCESS;
209 }
210
211 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
212 {
213         gint ok;
214         gchar buf[NNTPBUFSIZE];
215
216         ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
217         if (ok != NN_SUCCESS)
218                 return ok;
219
220         return NN_SUCCESS;
221 }
222
223 gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
224 {
225         gint ok;
226         gchar buf[NNTPBUFSIZE];
227
228         ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
229         if (ok != NN_SUCCESS)
230                 return ok;
231
232         return NN_SUCCESS;
233 }
234
235 gint nntp_list(NNTPSockInfo *sock)
236 {
237         return nntp_gen_command(sock, NULL, "LIST");
238 }
239
240 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
241 {
242         gint ok;
243         gchar buf[NNTPBUFSIZE];
244
245         ok = nntp_gen_command(sock, buf, "POST");
246         if (ok != NN_SUCCESS)
247                 return ok;
248
249         while (fgets(buf, sizeof(buf), fp) != NULL) {
250                 strretchomp(buf);
251                 if (buf[0] == '.') {
252                         if (sock_write(sock->sock, ".", 1) < 0) {
253                                 log_warning(_("Error occurred while posting\n"));
254                                 return NN_SOCKET;
255                         }
256                 }
257
258                 if (sock_puts(sock->sock, buf) < 0) {
259                         log_warning(_("Error occurred while posting\n"));
260                         return NN_SOCKET;
261                 }
262         }
263
264         sock_write(sock->sock, ".\r\n", 3);
265         if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
266                 return ok;
267
268         return NN_SUCCESS;
269 }
270
271 gint nntp_newgroups(NNTPSockInfo *sock)
272 {
273         return NN_SUCCESS;
274 }
275
276 gint nntp_newnews(NNTPSockInfo *sock)
277 {
278         return NN_SUCCESS;
279 }
280
281 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
282 {
283         gint ok;
284
285         if (sock->auth_failed)
286                 return NN_AUTHREQ;
287
288         ok = nntp_gen_command(sock, NULL, "MODE %s",
289                               stream ? "STREAM" : "READER");
290
291         return ok;
292 }
293
294 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
295 {
296         gint ok;
297         gchar buf[NNTPBUFSIZE];
298
299         if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
300                 if (strlen(buf) < 3)
301                         return NN_ERROR;
302
303                 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
304                     (buf[3] == ' ' || buf[3] == '\0')) {
305                         if (argbuf)
306                                 strcpy(argbuf, buf);
307
308                         if (!strncmp(buf, "381", 3))
309                                 return NN_AUTHCONT;
310
311                         return NN_SUCCESS;
312                 } else if (!strncmp(buf, "480", 3))
313                         return NN_AUTHREQ;
314                 else
315                         return NN_ERROR;
316         }
317
318         return ok;
319 }
320
321 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
322 {
323         gchar buf[NNTPBUFSIZE];
324         va_list args;
325
326         va_start(args, format);
327         g_vsnprintf(buf, sizeof(buf), format, args);
328         va_end(args);
329
330         if (verbose) {
331                 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
332                         log_print("NNTP> AUTHINFO PASS ********\n");
333                 else
334                         log_print("NNTP> %s\n", buf);
335         }
336
337         strcat(buf, "\r\n");
338         sock_write(sock->sock, buf, strlen(buf));
339 }
340
341 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
342 {
343         if (sock_gets(sock->sock, buf, size) == -1)
344                 return NN_SOCKET;
345
346         strretchomp(buf);
347
348         if (verbose)
349                 log_print("NNTP< %s\n", buf);
350
351         return NN_SUCCESS;
352 }
353
354 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
355                              const gchar *format, ...)
356 {
357         gchar buf[NNTPBUFSIZE];
358         va_list args;
359         gint ok;
360
361         va_start(args, format);
362         g_vsnprintf(buf, sizeof(buf), format, args);
363         va_end(args);
364
365         nntp_gen_send(sock, "%s", buf);
366         ok = nntp_ok(sock, argbuf);
367         if (ok == NN_AUTHREQ) {
368                 if (!sock->userid || !sock->passwd) {
369                         sock->auth_failed = TRUE;
370                         return ok;
371                 }
372
373                 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
374                 ok = nntp_ok(sock, NULL);
375                 if (ok == NN_AUTHCONT) {
376                         nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
377                         ok = nntp_ok(sock, NULL);
378                 }
379                 if (ok != NN_SUCCESS) {
380                         sock->auth_failed = TRUE;
381                         return ok;
382                 }
383
384                 nntp_gen_send(sock, "%s", buf);
385                 ok = nntp_ok(sock, argbuf);
386         }
387
388         return ok;
389 }