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