2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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 void 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);
86 SESSION(session)->type = SESSION_NEWS;
87 SESSION(session)->server = g_strdup(server);
88 SESSION(session)->sock = sock;
89 SESSION(session)->last_access_time = time(NULL);
90 SESSION(session)->data = NULL;
92 SESSION(session)->destroy = nntp_session_destroy;
94 session->group = NULL;
96 if (userid && passwd) {
97 session->userid = g_strdup(userid);
98 session->passwd = g_strdup(passwd);
101 return SESSION(session);
104 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
107 if (!session) return;
109 nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
114 static void nntp_session_destroy(Session *session)
116 NNTPSession *nntp_session = NNTP_SESSION(session);
118 g_return_if_fail(session != NULL);
120 g_free(nntp_session->group);
121 g_free(nntp_session->userid);
122 g_free(nntp_session->passwd);
125 gint nntp_group(NNTPSession *session, const gchar *group,
126 gint *num, gint *first, gint *last)
130 gchar buf[NNTPBUFSIZE];
132 ok = nntp_gen_command(session, buf, "GROUP %s", group);
134 if (ok != NN_SUCCESS) {
135 ok = nntp_mode(session, FALSE);
136 if (ok == NN_SUCCESS)
137 ok = nntp_gen_command(session, buf, "GROUP %s", group);
140 if (ok != NN_SUCCESS)
143 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
145 log_warning(_("protocol error: %s\n"), buf);
152 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
156 gchar buf[NNTPBUFSIZE];
159 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
161 ok = nntp_gen_command(session, buf, cmd);
163 if (ok != NN_SUCCESS)
166 extract_parenthesis(buf, '<', '>');
167 if (buf[0] == '\0') {
168 log_warning(_("protocol error\n"));
171 *msgid = g_strdup(buf);
176 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
178 return nntp_get_article(session, "ARTICLE", num, msgid);
181 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
183 return nntp_get_article(session, "BODY", num, msgid);
186 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
188 return nntp_get_article(session, "HEAD", num, msgid);
191 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
193 return nntp_get_article(session, "STAT", num, msgid);
196 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
200 gchar buf[NNTPBUFSIZE];
202 ok = nntp_gen_command(session, buf, "NEXT");
204 if (ok != NN_SUCCESS)
207 if (sscanf(buf, "%d %d", &resp, num) != 2) {
208 log_warning(_("protocol error: %s\n"), buf);
212 extract_parenthesis(buf, '<', '>');
213 if (buf[0] == '\0') {
214 log_warning(_("protocol error\n"));
217 *msgid = g_strdup(buf);
222 gint nntp_xover(NNTPSession *session, gint first, gint last)
225 gchar buf[NNTPBUFSIZE];
227 ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
228 if (ok != NN_SUCCESS)
234 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
237 gchar buf[NNTPBUFSIZE];
239 ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
240 header, first, last);
241 if (ok != NN_SUCCESS)
247 gint nntp_list(NNTPSession *session)
249 return nntp_gen_command(session, NULL, "LIST");
252 gint nntp_post(NNTPSession *session, FILE *fp)
255 gchar buf[NNTPBUFSIZE];
258 ok = nntp_gen_command(session, buf, "POST");
259 if (ok != NN_SUCCESS)
262 msg = get_outgoing_rfc2822_str(fp);
263 if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
264 log_warning(_("Error occurred while posting\n"));
270 sock_write_all(SESSION(session)->sock, ".\r\n", 3);
271 if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
277 gint nntp_newgroups(NNTPSession *session)
282 gint nntp_newnews(NNTPSession *session)
287 gint nntp_mode(NNTPSession *session, gboolean stream)
291 if (session->auth_failed)
294 ok = nntp_gen_command(session, NULL, "MODE %s",
295 stream ? "STREAM" : "READER");
300 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
303 gchar buf[NNTPBUFSIZE];
305 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
309 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
310 (buf[3] == ' ' || buf[3] == '\0')) {
314 if (!strncmp(buf, "381", 3))
318 } else if (!strncmp(buf, "480", 3))
327 static void nntp_gen_send(SockInfo *sock, const gchar *format, ...)
329 gchar buf[NNTPBUFSIZE];
332 va_start(args, format);
333 g_vsnprintf(buf, sizeof(buf), format, args);
337 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
338 log_print("NNTP> AUTHINFO PASS ********\n");
340 log_print("NNTP> %s\n", buf);
344 sock_write_all(sock, buf, strlen(buf));
347 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
349 if (sock_gets(sock, buf, size) == -1)
355 log_print("NNTP< %s\n", buf);
360 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
361 const gchar *format, ...)
363 gchar buf[NNTPBUFSIZE];
368 va_start(args, format);
369 g_vsnprintf(buf, sizeof(buf), format, args);
372 sock = SESSION(session)->sock;
373 nntp_gen_send(sock, "%s", buf);
374 ok = nntp_ok(sock, argbuf);
375 if (ok == NN_AUTHREQ) {
376 if (!session->userid || !session->passwd) {
377 session->auth_failed = TRUE;
381 nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
382 ok = nntp_ok(sock, NULL);
383 if (ok == NN_AUTHCONT) {
384 nntp_gen_send(sock, "AUTHINFO PASS %s",
386 ok = nntp_ok(sock, NULL);
388 if (ok != NN_SUCCESS) {
389 session->auth_failed = TRUE;
393 nntp_gen_send(sock, "%s", buf);
394 ok = nntp_ok(sock, argbuf);
396 } else if (ok == NN_AUTHCONT) {
397 nntp_gen_send(sock, "AUTHINFO PASS %s", session->passwd);
398 ok = nntp_ok(sock, NULL);
400 if (ok != NN_SUCCESS) {
401 session->auth_failed = TRUE;