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 return SESSION(session);
131 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
134 if (!session) return;
136 nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
141 static void nntp_session_destroy(Session *session)
143 NNTPSession *nntp_session = NNTP_SESSION(session);
145 g_return_if_fail(session != NULL);
147 g_free(nntp_session->group);
148 g_free(nntp_session->userid);
149 g_free(nntp_session->passwd);
152 gint nntp_group(NNTPSession *session, const gchar *group,
153 gint *num, gint *first, gint *last)
157 gchar buf[NNTPBUFSIZE];
159 ok = nntp_gen_command(session, buf, "GROUP %s", group);
161 if (ok != NN_SUCCESS && ok != NN_SOCKET && ok != NN_AUTHREQ) {
162 ok = nntp_mode(session, FALSE);
163 if (ok == NN_SUCCESS)
164 ok = nntp_gen_command(session, buf, "GROUP %s", group);
167 if (ok != NN_SUCCESS)
170 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
172 log_warning("protocol error: %s\n", buf);
179 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
183 gchar buf[NNTPBUFSIZE];
186 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
188 ok = nntp_gen_command(session, buf, cmd);
190 if (ok != NN_SUCCESS)
193 extract_parenthesis(buf, '<', '>');
194 if (buf[0] == '\0') {
195 log_warning("protocol error\n");
198 *msgid = g_strdup(buf);
203 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
205 return nntp_get_article(session, "ARTICLE", num, msgid);
208 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
210 return nntp_get_article(session, "BODY", num, msgid);
213 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
215 return nntp_get_article(session, "HEAD", num, msgid);
218 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
220 return nntp_get_article(session, "STAT", num, msgid);
223 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
227 gchar buf[NNTPBUFSIZE];
229 ok = nntp_gen_command(session, buf, "NEXT");
231 if (ok != NN_SUCCESS)
234 if (sscanf(buf, "%d %d", &resp, num) != 2) {
235 log_warning("protocol error: %s\n", buf);
239 extract_parenthesis(buf, '<', '>');
240 if (buf[0] == '\0') {
241 log_warning("protocol error\n");
244 *msgid = g_strdup(buf);
249 gint nntp_xover(NNTPSession *session, gint first, gint last)
252 gchar buf[NNTPBUFSIZE];
254 ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
255 if (ok != NN_SUCCESS)
261 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
264 gchar buf[NNTPBUFSIZE];
266 ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
267 header, first, last);
268 if (ok != NN_SUCCESS)
274 gint nntp_list(NNTPSession *session)
276 return nntp_gen_command(session, NULL, "LIST");
279 gint nntp_post(NNTPSession *session, FILE *fp)
282 gchar buf[NNTPBUFSIZE];
285 ok = nntp_gen_command(session, buf, "POST");
286 if (ok != NN_SUCCESS)
289 msg = get_outgoing_rfc2822_str(fp);
290 if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
291 log_warning("Error occurred while posting\n");
297 sock_write_all(SESSION(session)->sock, ".\r\n", 3);
298 if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
304 gint nntp_newgroups(NNTPSession *session)
309 gint nntp_newnews(NNTPSession *session)
314 gint nntp_mode(NNTPSession *session, gboolean stream)
318 ok = nntp_gen_command(session, NULL, "MODE %s",
319 stream ? "STREAM" : "READER");
324 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
327 gchar buf[NNTPBUFSIZE];
329 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
333 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
334 (buf[3] == ' ' || buf[3] == '\0')) {
338 if (!strncmp(buf, "381", 3))
342 } else if (!strncmp(buf, "480", 3))
351 static gint nntp_gen_send(SockInfo *sock, const gchar *format, ...)
353 gchar buf[NNTPBUFSIZE];
356 va_start(args, format);
357 g_vsnprintf(buf, sizeof(buf), format, args);
361 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
362 log_print("NNTP> AUTHINFO PASS ********\n");
364 log_print("NNTP> %s\n", buf);
368 if (sock_write_all(sock, buf, strlen(buf)) < 0) {
369 log_warning("Error occurred while sending command\n");
376 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
378 if (sock_gets(sock, buf, size) == -1)
384 log_print("NNTP< %s\n", buf);
389 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
390 const gchar *format, ...)
392 gchar buf[NNTPBUFSIZE];
397 va_start(args, format);
398 g_vsnprintf(buf, sizeof(buf), format, args);
401 sock = SESSION(session)->sock;
402 ok = nntp_gen_send(sock, "%s", buf);
403 if (ok != NN_SUCCESS)
405 ok = nntp_ok(sock, argbuf);
406 if (ok == NN_AUTHREQ) {
407 if (!session->userid || !session->passwd) {
408 session->auth_failed = TRUE;
412 ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
413 if (ok != NN_SUCCESS)
415 ok = nntp_ok(sock, NULL);
416 if (ok == NN_AUTHCONT) {
417 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
419 if (ok != NN_SUCCESS)
421 ok = nntp_ok(sock, NULL);
423 if (ok != NN_SUCCESS) {
424 session->auth_failed = TRUE;
428 ok = nntp_gen_send(sock, "%s", buf);
429 if (ok != NN_SUCCESS)
431 ok = nntp_ok(sock, argbuf);
433 } else if (ok == NN_AUTHCONT) {
434 ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
436 if (ok != NN_SUCCESS) {
437 session->auth_failed = TRUE;
440 ok = nntp_ok(sock, NULL);