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