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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gi18n.h>
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 nntp_mode(session, FALSE);
101 if (userid && passwd) {
104 session->userid = g_strdup(userid);
105 session->passwd = g_strdup(passwd);
107 ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
108 if (ok != NN_SUCCESS) {
109 session_destroy(SESSION(session));
112 ok = nntp_ok(sock, NULL);
113 if (ok == NN_AUTHCONT) {
114 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
116 if (ok != NN_SUCCESS) {
117 session_destroy(SESSION(session));
120 ok = nntp_ok(sock, NULL);
121 if (ok != NN_SUCCESS)
122 session->auth_failed = TRUE;
124 if (ok == NN_SOCKET) {
125 session_destroy(SESSION(session));
130 session_set_access_time(SESSION(session));
132 return SESSION(session);
135 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
138 if (!session) return;
140 nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
145 static void nntp_session_destroy(Session *session)
147 NNTPSession *nntp_session = NNTP_SESSION(session);
149 g_return_if_fail(session != NULL);
151 g_free(nntp_session->group);
152 g_free(nntp_session->userid);
153 g_free(nntp_session->passwd);
156 gint nntp_group(NNTPSession *session, const gchar *group,
157 gint *num, gint *first, gint *last)
161 gchar buf[NNTPBUFSIZE];
163 ok = nntp_gen_command(session, buf, "GROUP %s", group);
165 if (ok != NN_SUCCESS && ok != NN_SOCKET && ok != NN_AUTHREQ) {
166 ok = nntp_mode(session, FALSE);
167 if (ok == NN_SUCCESS)
168 ok = nntp_gen_command(session, buf, "GROUP %s", group);
171 if (ok != NN_SUCCESS)
174 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
176 log_warning(_("protocol error: %s\n"), buf);
183 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
187 gchar buf[NNTPBUFSIZE];
190 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
192 ok = nntp_gen_command(session, buf, cmd);
194 if (ok != NN_SUCCESS)
197 extract_parenthesis(buf, '<', '>');
198 if (buf[0] == '\0') {
199 log_warning(_("protocol error\n"));
200 *msgid = g_strdup("0");
202 *msgid = g_strdup(buf);
207 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
209 return nntp_get_article(session, "ARTICLE", num, msgid);
212 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
214 return nntp_get_article(session, "BODY", num, msgid);
217 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
219 return nntp_get_article(session, "HEAD", num, msgid);
222 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
224 return nntp_get_article(session, "STAT", num, msgid);
227 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
231 gchar buf[NNTPBUFSIZE];
233 ok = nntp_gen_command(session, buf, "NEXT");
235 if (ok != NN_SUCCESS)
238 if (sscanf(buf, "%d %d", &resp, num) != 2) {
239 log_warning(_("protocol error: %s\n"), buf);
243 extract_parenthesis(buf, '<', '>');
244 if (buf[0] == '\0') {
245 log_warning(_("protocol error\n"));
248 *msgid = g_strdup(buf);
253 gint nntp_xover(NNTPSession *session, gint first, gint last)
256 gchar buf[NNTPBUFSIZE];
258 ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
259 if (ok != NN_SUCCESS)
265 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
268 gchar buf[NNTPBUFSIZE];
270 ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
271 header, first, last);
272 if (ok != NN_SUCCESS)
278 gint nntp_list(NNTPSession *session)
280 return nntp_gen_command(session, NULL, "LIST");
283 gint nntp_post(NNTPSession *session, FILE *fp)
286 gchar buf[NNTPBUFSIZE];
289 ok = nntp_gen_command(session, buf, "POST");
290 if (ok != NN_SUCCESS)
293 msg = get_outgoing_rfc2822_str(fp);
294 if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
295 log_warning(_("Error occurred while posting\n"));
301 sock_write_all(SESSION(session)->sock, ".\r\n", 3);
302 if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
305 session_set_access_time(SESSION(session));
310 gint nntp_newgroups(NNTPSession *session)
315 gint nntp_newnews(NNTPSession *session)
320 gint nntp_mode(NNTPSession *session, gboolean stream)
324 ok = nntp_gen_command(session, NULL, "MODE %s",
325 stream ? "STREAM" : "READER");
330 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
333 gchar buf[NNTPBUFSIZE];
335 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
339 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
340 (buf[3] == ' ' || buf[3] == '\0')) {
344 if (!strncmp(buf, "381", 3))
348 } else if (!strncmp(buf, "480", 3))
357 static gint nntp_gen_send(SockInfo *sock, const gchar *format, ...)
359 gchar buf[NNTPBUFSIZE];
362 va_start(args, format);
363 g_vsnprintf(buf, sizeof(buf), format, args);
367 if (!g_ascii_strncasecmp(buf, "AUTHINFO PASS", 13))
368 log_print("NNTP> AUTHINFO PASS ********\n");
370 log_print("NNTP> %s\n", buf);
374 if (sock_write_all(sock, buf, strlen(buf)) < 0) {
375 log_warning(_("Error occurred while sending command\n"));
382 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
384 if (sock_gets(sock, buf, size) == -1)
390 log_print("NNTP< %s\n", buf);
395 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
396 const gchar *format, ...)
398 gchar buf[NNTPBUFSIZE];
403 va_start(args, format);
404 g_vsnprintf(buf, sizeof(buf), format, args);
407 sock = SESSION(session)->sock;
408 ok = nntp_gen_send(sock, "%s", buf);
409 if (ok != NN_SUCCESS)
411 ok = nntp_ok(sock, argbuf);
412 if (ok == NN_AUTHREQ) {
413 if (!session->userid || !session->passwd) {
414 session->auth_failed = TRUE;
418 ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
419 if (ok != NN_SUCCESS)
421 ok = nntp_ok(sock, NULL);
422 if (ok == NN_AUTHCONT) {
423 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
425 if (ok != NN_SUCCESS)
427 ok = nntp_ok(sock, NULL);
429 if (ok != NN_SUCCESS) {
430 session->auth_failed = TRUE;
434 ok = nntp_gen_send(sock, "%s", buf);
435 if (ok != NN_SUCCESS)
437 ok = nntp_ok(sock, argbuf);
439 } else if (ok == NN_AUTHCONT) {
440 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
442 if (ok != NN_SUCCESS) {
443 session->auth_failed = TRUE;
446 ok = nntp_ok(sock, NULL);
449 session_set_access_time(SESSION(session));