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