2007-10-01 [colin] 3.0.1cvs2-stable
[claws.git] / src / common / nntp.c
blobd7aa17da55dd419a39a5c37491d3f1f42a0ccf9b
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
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>
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
42 static gint verbose = 1;
44 static void nntp_session_destroy(Session *session);
46 static gint nntp_ok (SockInfo *sock,
47 gchar *argbuf);
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 ...);
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
69 NNTPSession *session;
70 SockInfo *sock;
72 if ((sock = sock_connect(server, port)) == NULL) {
73 log_warning(LOG_PROTOCOL, _("Can't connect to NNTP server: %s:%d\n"),
74 server, port);
75 return NULL;
78 #if USE_OPENSSL
79 if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
80 log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
81 sock_close(sock);
82 return NULL;
84 #endif
86 if (nntp_ok(sock, buf) != NN_SUCCESS) {
87 sock_close(sock);
88 return NULL;
91 session = g_new0(NNTPSession, 1);
93 session_init(SESSION(session));
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;
101 SESSION(session)->destroy = nntp_session_destroy;
103 session->group = NULL;
105 nntp_mode(session, FALSE);
107 if (userid && passwd) {
108 gint ok;
110 session->userid = g_strdup(userid);
111 session->passwd = g_strdup(passwd);
113 ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
114 if (ok != NN_SUCCESS) {
115 session_destroy(SESSION(session));
116 return NULL;
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;
126 ok = nntp_ok(sock, NULL);
127 if (ok != NN_SUCCESS)
128 session->auth_failed = TRUE;
130 if (ok == NN_SOCKET) {
131 session_destroy(SESSION(session));
132 return NULL;
136 session_set_access_time(SESSION(session));
138 return SESSION(session);
141 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
144 if (!session) return;
146 nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
151 static void nntp_session_destroy(Session *session)
153 NNTPSession *nntp_session = NNTP_SESSION(session);
155 g_return_if_fail(session != NULL);
157 g_free(nntp_session->group);
158 g_free(nntp_session->userid);
159 g_free(nntp_session->passwd);
162 gint nntp_group(NNTPSession *session, const gchar *group,
163 gint *num, gint *first, gint *last)
165 gint ok;
166 gint resp;
167 gchar buf[NNTPBUFSIZE];
169 ok = nntp_gen_command(session, buf, "GROUP %s", group);
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);
177 if (ok != NN_SUCCESS)
178 return ok;
180 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
181 != 4) {
182 log_warning(LOG_PROTOCOL, _("protocol error: %s\n"), buf);
183 return NN_PROTOCOL;
186 return NN_SUCCESS;
189 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
190 gchar **msgid)
192 gint ok;
193 gchar buf[NNTPBUFSIZE];
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);
200 if (ok != NN_SUCCESS)
201 return ok;
203 extract_parenthesis(buf, '<', '>');
204 if (buf[0] == '\0') {
205 log_warning(LOG_PROTOCOL, _("protocol error\n"));
206 *msgid = g_strdup("0");
207 } else
208 *msgid = g_strdup(buf);
210 return NN_SUCCESS;
213 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
215 return nntp_get_article(session, "ARTICLE", num, msgid);
218 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
220 return nntp_get_article(session, "BODY", num, msgid);
223 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
225 return nntp_get_article(session, "HEAD", num, msgid);
228 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
230 return nntp_get_article(session, "STAT", num, msgid);
233 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
235 gint ok;
236 gint resp;
237 gchar buf[NNTPBUFSIZE];
239 ok = nntp_gen_command(session, buf, "NEXT");
241 if (ok != NN_SUCCESS)
242 return ok;
244 if (sscanf(buf, "%d %d", &resp, num) != 2) {
245 log_warning(LOG_PROTOCOL, _("protocol error: %s\n"), buf);
246 return NN_PROTOCOL;
249 extract_parenthesis(buf, '<', '>');
250 if (buf[0] == '\0') {
251 log_warning(LOG_PROTOCOL, _("protocol error\n"));
252 return NN_PROTOCOL;
254 *msgid = g_strdup(buf);
256 return NN_SUCCESS;
259 gint nntp_xover(NNTPSession *session, gint first, gint last)
261 gint ok;
262 gchar buf[NNTPBUFSIZE];
264 ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
265 if (ok != NN_SUCCESS)
266 return ok;
268 return NN_SUCCESS;
271 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
273 gint ok;
274 gchar buf[NNTPBUFSIZE];
276 ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
277 header, first, last);
278 if (ok != NN_SUCCESS)
279 return ok;
281 return NN_SUCCESS;
284 gint nntp_list(NNTPSession *session)
286 return nntp_gen_command(session, NULL, "LIST");
289 gint nntp_post(NNTPSession *session, FILE *fp)
291 gint ok;
292 gchar buf[NNTPBUFSIZE];
293 gchar *msg;
295 ok = nntp_gen_command(session, buf, "POST");
296 if (ok != NN_SUCCESS)
297 return ok;
299 msg = get_outgoing_rfc2822_str(fp);
300 if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
301 log_warning(LOG_PROTOCOL, _("Error occurred while posting\n"));
302 g_free(msg);
303 return NN_SOCKET;
305 g_free(msg);
307 sock_write_all(SESSION(session)->sock, ".\r\n", 3);
308 if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
309 return ok;
311 session_set_access_time(SESSION(session));
313 return NN_SUCCESS;
316 gint nntp_newgroups(NNTPSession *session)
318 return NN_SUCCESS;
321 gint nntp_newnews(NNTPSession *session)
323 return NN_SUCCESS;
326 gint nntp_mode(NNTPSession *session, gboolean stream)
328 gint ok;
330 ok = nntp_gen_command(session, NULL, "MODE %s",
331 stream ? "STREAM" : "READER");
333 return ok;
336 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
338 gint ok;
339 gchar buf[NNTPBUFSIZE];
341 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
342 if (strlen(buf) < 3)
343 return NN_ERROR;
345 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
346 (buf[3] == ' ' || buf[3] == '\0')) {
347 if (argbuf)
348 strcpy(argbuf, buf);
350 if (!strncmp(buf, "381", 3))
351 return NN_AUTHCONT;
353 return NN_SUCCESS;
354 } else if (!strncmp(buf, "480", 3))
355 return NN_AUTHREQ;
356 else
357 return NN_ERROR;
360 return ok;
363 static gint nntp_gen_send(SockInfo *sock, const gchar *format, ...)
365 gchar buf[NNTPBUFSIZE];
366 va_list args;
368 va_start(args, format);
369 g_vsnprintf(buf, sizeof(buf), format, args);
370 va_end(args);
372 if (verbose) {
373 if (!g_ascii_strncasecmp(buf, "AUTHINFO PASS", 13))
374 log_print(LOG_PROTOCOL, "NNTP> AUTHINFO PASS ********\n");
375 else
376 log_print(LOG_PROTOCOL, "NNTP> %s\n", buf);
379 strcat(buf, "\r\n");
380 if (sock_write_all(sock, buf, strlen(buf)) < 0) {
381 log_warning(LOG_PROTOCOL, _("Error occurred while sending command\n"));
382 return NN_SOCKET;
385 return NN_SUCCESS;
388 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
390 if (sock_gets(sock, buf, size) == -1)
391 return NN_SOCKET;
393 strretchomp(buf);
395 if (verbose)
396 log_print(LOG_PROTOCOL, "NNTP< %s\n", buf);
398 return NN_SUCCESS;
401 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
402 const gchar *format, ...)
404 gchar buf[NNTPBUFSIZE];
405 va_list args;
406 gint ok;
407 SockInfo *sock;
409 va_start(args, format);
410 g_vsnprintf(buf, sizeof(buf), format, args);
411 va_end(args);
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;
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);
435 if (ok != NN_SUCCESS) {
436 session->auth_failed = TRUE;
437 return ok;
440 ok = nntp_gen_send(sock, "%s", buf);
441 if (ok != NN_SUCCESS)
442 return ok;
443 ok = nntp_ok(sock, argbuf);
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;
452 ok = nntp_ok(sock, NULL);
455 session_set_access_time(SESSION(session));
457 return ok;