2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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.
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 static gint verbose = 1;
39 static void nntp_session_destroy(Session *session);
41 static gint nntp_ok (SockInfo *sock,
44 static gint nntp_gen_send (SockInfo *sock,
47 static gint nntp_gen_recv (SockInfo *sock,
50 static gint nntp_gen_command (NNTPSession *session,
56 Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
57 const gchar *userid, const gchar *passwd,
60 Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
61 const gchar *userid, const gchar *passwd)
67 if ((sock = sock_connect(server, port)) == NULL) {
68 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
74 if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
80 if (nntp_ok(sock, buf) != NN_SUCCESS) {
85 session = g_new0(NNTPSession, 1);
87 session_init(SESSION(session));
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;
95 SESSION(session)->destroy = nntp_session_destroy;
97 session->group = NULL;
99 if (userid && passwd) {
102 session->userid = g_strdup(userid);
103 session->passwd = g_strdup(passwd);
105 ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
106 if (ok != NN_SUCCESS) {
107 session_destroy(SESSION(session));
110 ok = nntp_ok(sock, NULL);
111 if (ok == NN_AUTHCONT) {
112 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
114 if (ok != NN_SUCCESS) {
115 session_destroy(SESSION(session));
118 ok = nntp_ok(sock, NULL);
119 if (ok != NN_SUCCESS)
120 session->auth_failed = TRUE;
122 if (ok == NN_SOCKET) {
123 session_destroy(SESSION(session));
128 session_set_access_time(SESSION(session));
130 return SESSION(session);
133 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
136 if (!session) return;
138 nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
143 static void nntp_session_destroy(Session *session)
145 NNTPSession *nntp_session = NNTP_SESSION(session);
147 g_return_if_fail(session != NULL);
149 g_free(nntp_session->group);
150 g_free(nntp_session->userid);
151 g_free(nntp_session->passwd);
154 gint nntp_group(NNTPSession *session, const gchar *group,
155 gint *num, gint *first, gint *last)
159 gchar buf[NNTPBUFSIZE];
161 ok = nntp_gen_command(session, buf, "GROUP %s", group);
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);
169 if (ok != NN_SUCCESS)
172 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
174 log_warning(_("protocol error: %s\n"), buf);
181 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
185 gchar buf[NNTPBUFSIZE];
188 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
190 ok = nntp_gen_command(session, buf, cmd);
192 if (ok != NN_SUCCESS)
195 extract_parenthesis(buf, '<', '>');
196 if (buf[0] == '\0') {
197 log_warning(_("protocol error\n"));
198 *msgid = g_strdup("0");
200 *msgid = g_strdup(buf);
205 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
207 return nntp_get_article(session, "ARTICLE", num, msgid);
210 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
212 return nntp_get_article(session, "BODY", num, msgid);
215 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
217 return nntp_get_article(session, "HEAD", num, msgid);
220 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
222 return nntp_get_article(session, "STAT", num, msgid);
225 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
229 gchar buf[NNTPBUFSIZE];
231 ok = nntp_gen_command(session, buf, "NEXT");
233 if (ok != NN_SUCCESS)
236 if (sscanf(buf, "%d %d", &resp, num) != 2) {
237 log_warning(_("protocol error: %s\n"), buf);
241 extract_parenthesis(buf, '<', '>');
242 if (buf[0] == '\0') {
243 log_warning(_("protocol error\n"));
246 *msgid = g_strdup(buf);
251 gint nntp_xover(NNTPSession *session, gint first, gint last)
254 gchar buf[NNTPBUFSIZE];
256 ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
257 if (ok != NN_SUCCESS)
263 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
266 gchar buf[NNTPBUFSIZE];
268 ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
269 header, first, last);
270 if (ok != NN_SUCCESS)
276 gint nntp_list(NNTPSession *session)
278 return nntp_gen_command(session, NULL, "LIST");
281 gint nntp_post(NNTPSession *session, FILE *fp)
284 gchar buf[NNTPBUFSIZE];
287 ok = nntp_gen_command(session, buf, "POST");
288 if (ok != NN_SUCCESS)
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"));
299 sock_write_all(SESSION(session)->sock, ".\r\n", 3);
300 if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
303 session_set_access_time(SESSION(session));
308 gint nntp_newgroups(NNTPSession *session)
313 gint nntp_newnews(NNTPSession *session)
318 gint nntp_mode(NNTPSession *session, gboolean stream)
322 ok = nntp_gen_command(session, NULL, "MODE %s",
323 stream ? "STREAM" : "READER");
328 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
331 gchar buf[NNTPBUFSIZE];
333 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
337 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
338 (buf[3] == ' ' || buf[3] == '\0')) {
342 if (!strncmp(buf, "381", 3))
346 } else if (!strncmp(buf, "480", 3))
355 static gint nntp_gen_send(SockInfo *sock, const gchar *format, ...)
357 gchar buf[NNTPBUFSIZE];
360 va_start(args, format);
361 g_vsnprintf(buf, sizeof(buf), format, args);
365 if (!g_ascii_strncasecmp(buf, "AUTHINFO PASS", 13))
366 log_print("NNTP> AUTHINFO PASS ********\n");
368 log_print("NNTP> %s\n", buf);
372 if (sock_write_all(sock, buf, strlen(buf)) < 0) {
373 log_warning(_("Error occurred while sending command\n"));
380 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
382 if (sock_gets(sock, buf, size) == -1)
388 log_print("NNTP< %s\n", buf);
393 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
394 const gchar *format, ...)
396 gchar buf[NNTPBUFSIZE];
401 va_start(args, format);
402 g_vsnprintf(buf, sizeof(buf), format, args);
405 sock = SESSION(session)->sock;
406 ok = nntp_gen_send(sock, "%s", buf);
407 if (ok != NN_SUCCESS)
409 ok = nntp_ok(sock, argbuf);
410 if (ok == NN_AUTHREQ) {
411 if (!session->userid || !session->passwd) {
412 session->auth_failed = TRUE;
416 ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
417 if (ok != NN_SUCCESS)
419 ok = nntp_ok(sock, NULL);
420 if (ok == NN_AUTHCONT) {
421 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
423 if (ok != NN_SUCCESS)
425 ok = nntp_ok(sock, NULL);
427 if (ok != NN_SUCCESS) {
428 session->auth_failed = TRUE;
432 ok = nntp_gen_send(sock, "%s", buf);
433 if (ok != NN_SUCCESS)
435 ok = nntp_ok(sock, argbuf);
437 } else if (ok == NN_AUTHCONT) {
438 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
440 if (ok != NN_SUCCESS) {
441 session->auth_failed = TRUE;
444 ok = nntp_ok(sock, NULL);
447 session_set_access_time(SESSION(session));