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