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