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);
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) {
100 session->userid = g_strdup(userid);
101 session->passwd = g_strdup(passwd);
104 return SESSION(session);
107 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
110 if (!session) return;
112 nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
117 static void nntp_session_destroy(Session *session)
119 NNTPSession *nntp_session = NNTP_SESSION(session);
121 g_return_if_fail(session != NULL);
123 g_free(nntp_session->group);
124 g_free(nntp_session->userid);
125 g_free(nntp_session->passwd);
128 gint nntp_group(NNTPSession *session, const gchar *group,
129 gint *num, gint *first, gint *last)
133 gchar buf[NNTPBUFSIZE];
135 ok = nntp_gen_command(session, buf, "GROUP %s", group);
137 if (ok != NN_SUCCESS) {
138 ok = nntp_mode(session, FALSE);
139 if (ok == NN_SUCCESS)
140 ok = nntp_gen_command(session, buf, "GROUP %s", group);
143 if (ok != NN_SUCCESS)
146 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
148 log_warning(_("protocol error: %s\n"), buf);
155 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
159 gchar buf[NNTPBUFSIZE];
162 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
164 ok = nntp_gen_command(session, buf, cmd);
166 if (ok != NN_SUCCESS)
169 extract_parenthesis(buf, '<', '>');
170 if (buf[0] == '\0') {
171 log_warning(_("protocol error\n"));
174 *msgid = g_strdup(buf);
179 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
181 return nntp_get_article(session, "ARTICLE", num, msgid);
184 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
186 return nntp_get_article(session, "BODY", num, msgid);
189 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
191 return nntp_get_article(session, "HEAD", num, msgid);
194 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
196 return nntp_get_article(session, "STAT", num, msgid);
199 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
203 gchar buf[NNTPBUFSIZE];
205 ok = nntp_gen_command(session, buf, "NEXT");
207 if (ok != NN_SUCCESS)
210 if (sscanf(buf, "%d %d", &resp, num) != 2) {
211 log_warning(_("protocol error: %s\n"), buf);
215 extract_parenthesis(buf, '<', '>');
216 if (buf[0] == '\0') {
217 log_warning(_("protocol error\n"));
220 *msgid = g_strdup(buf);
225 gint nntp_xover(NNTPSession *session, gint first, gint last)
228 gchar buf[NNTPBUFSIZE];
230 ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
231 if (ok != NN_SUCCESS)
237 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
240 gchar buf[NNTPBUFSIZE];
242 ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
243 header, first, last);
244 if (ok != NN_SUCCESS)
250 gint nntp_list(NNTPSession *session)
252 return nntp_gen_command(session, NULL, "LIST");
255 gint nntp_post(NNTPSession *session, FILE *fp)
258 gchar buf[NNTPBUFSIZE];
261 ok = nntp_gen_command(session, buf, "POST");
262 if (ok != NN_SUCCESS)
265 msg = get_outgoing_rfc2822_str(fp);
266 if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
267 log_warning(_("Error occurred while posting\n"));
273 sock_write_all(SESSION(session)->sock, ".\r\n", 3);
274 if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
280 gint nntp_newgroups(NNTPSession *session)
285 gint nntp_newnews(NNTPSession *session)
290 gint nntp_mode(NNTPSession *session, gboolean stream)
294 if (session->auth_failed)
297 ok = nntp_gen_command(session, NULL, "MODE %s",
298 stream ? "STREAM" : "READER");
303 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
306 gchar buf[NNTPBUFSIZE];
308 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
312 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
313 (buf[3] == ' ' || buf[3] == '\0')) {
317 if (!strncmp(buf, "381", 3))
321 } else if (!strncmp(buf, "480", 3))
330 static void nntp_gen_send(SockInfo *sock, const gchar *format, ...)
332 gchar buf[NNTPBUFSIZE];
335 va_start(args, format);
336 g_vsnprintf(buf, sizeof(buf), format, args);
340 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
341 log_print("NNTP> AUTHINFO PASS ********\n");
343 log_print("NNTP> %s\n", buf);
347 sock_write_all(sock, buf, strlen(buf));
350 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
352 if (sock_gets(sock, buf, size) == -1)
358 log_print("NNTP< %s\n", buf);
363 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
364 const gchar *format, ...)
366 gchar buf[NNTPBUFSIZE];
371 va_start(args, format);
372 g_vsnprintf(buf, sizeof(buf), format, args);
375 sock = SESSION(session)->sock;
376 nntp_gen_send(sock, "%s", buf);
377 ok = nntp_ok(sock, argbuf);
378 if (ok == NN_AUTHREQ) {
379 if (!session->userid || !session->passwd) {
380 session->auth_failed = TRUE;
384 nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
385 ok = nntp_ok(sock, NULL);
386 if (ok == NN_AUTHCONT) {
387 nntp_gen_send(sock, "AUTHINFO PASS %s",
389 ok = nntp_ok(sock, NULL);
391 if (ok != NN_SUCCESS) {
392 session->auth_failed = TRUE;
396 nntp_gen_send(sock, "%s", buf);
397 ok = nntp_ok(sock, argbuf);
399 } else if (ok == NN_AUTHCONT) {
400 nntp_gen_send(sock, "AUTHINFO PASS %s", session->passwd);
401 ok = nntp_ok(sock, NULL);
403 if (ok != NN_SUCCESS) {
404 session->auth_failed = TRUE;