2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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.
34 static gint verbose = 1;
36 static void nntp_gen_send (NNTPSockInfo *sock,
39 static gint nntp_gen_recv (NNTPSockInfo *sock,
42 static gint nntp_gen_command (NNTPSockInfo *sock,
48 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf, gboolean use_ssl)
50 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
54 NNTPSockInfo *nntp_sock;
56 if ((sock = sock_connect(server, port)) == NULL) {
57 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
63 if (use_ssl && !ssl_init_socket(sock)) {
69 nntp_sock = g_new0(NNTPSockInfo, 1);
70 nntp_sock->sock = sock;
72 if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
82 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
83 const gchar *userid, const gchar *passwd, gboolean use_ssl)
85 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
86 const gchar *userid, const gchar *passwd)
92 sock = nntp_open(server, port, buf, use_ssl);
94 sock = nntp_open(server, port, buf);
97 if (!sock) return NULL;
99 sock->userid = g_strdup(userid);
100 sock->passwd = g_strdup(passwd);
105 void nntp_close(NNTPSockInfo *sock)
109 sock_close(sock->sock);
110 g_free(sock->userid);
111 g_free(sock->passwd);
115 gint nntp_group(NNTPSockInfo *sock, const gchar *group,
116 gint *num, gint *first, gint *last)
120 gchar buf[NNTPBUFSIZE];
122 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
124 if (ok != NN_SUCCESS)
127 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
129 log_warning(_("protocol error: %s\n"), buf);
136 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
140 gchar buf[NNTPBUFSIZE];
143 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
145 ok = nntp_gen_command(sock, buf, cmd);
147 if (ok != NN_SUCCESS)
150 extract_parenthesis(buf, '<', '>');
151 if (buf[0] == '\0') {
152 log_warning(_("protocol error\n"));
155 *msgid = g_strdup(buf);
160 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
162 return nntp_get_article(sock, "ARTICLE", num, msgid);
165 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
167 return nntp_get_article(sock, "BODY", num, msgid);
170 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
172 return nntp_get_article(sock, "HEAD", num, msgid);
175 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
177 return nntp_get_article(sock, "STAT", num, msgid);
180 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
184 gchar buf[NNTPBUFSIZE];
186 ok = nntp_gen_command(sock, buf, "NEXT");
188 if (ok != NN_SUCCESS)
191 if (sscanf(buf, "%d %d", &resp, num) != 2) {
192 log_warning(_("protocol error: %s\n"), buf);
196 extract_parenthesis(buf, '<', '>');
197 if (buf[0] == '\0') {
198 log_warning(_("protocol error\n"));
201 *msgid = g_strdup(buf);
206 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
209 gchar buf[NNTPBUFSIZE];
211 ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
212 if (ok != NN_SUCCESS)
218 gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
221 gchar buf[NNTPBUFSIZE];
223 ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
224 if (ok != NN_SUCCESS)
230 gint nntp_list(NNTPSockInfo *sock)
232 return nntp_gen_command(sock, NULL, "LIST");
235 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
238 gchar buf[NNTPBUFSIZE];
240 ok = nntp_gen_command(sock, buf, "POST");
241 if (ok != NN_SUCCESS)
244 while (fgets(buf, sizeof(buf), fp) != NULL) {
247 if (sock_write(sock->sock, ".", 1) < 0) {
248 log_warning(_("Error occurred while posting\n"));
253 if (sock_puts(sock->sock, buf) < 0) {
254 log_warning(_("Error occurred while posting\n"));
259 sock_write(sock->sock, ".\r\n", 3);
260 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
266 gint nntp_newgroups(NNTPSockInfo *sock)
271 gint nntp_newnews(NNTPSockInfo *sock)
276 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
280 if (sock->auth_failed)
283 ok = nntp_gen_command(sock, NULL, "MODE %s",
284 stream ? "STREAM" : "READER");
289 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
292 gchar buf[NNTPBUFSIZE];
294 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
298 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
299 (buf[3] == ' ' || buf[3] == '\0')) {
303 if (!strncmp(buf, "381", 3))
307 } else if (!strncmp(buf, "480", 3))
316 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
318 gchar buf[NNTPBUFSIZE];
321 va_start(args, format);
322 g_vsnprintf(buf, sizeof(buf), format, args);
326 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
327 log_print("NNTP> AUTHINFO PASS ********\n");
329 log_print("NNTP> %s\n", buf);
333 sock_write(sock->sock, buf, strlen(buf));
336 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
338 if (sock_gets(sock->sock, buf, size) == -1)
344 log_print("NNTP< %s\n", buf);
349 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
350 const gchar *format, ...)
352 gchar buf[NNTPBUFSIZE];
356 va_start(args, format);
357 g_vsnprintf(buf, sizeof(buf), format, args);
360 nntp_gen_send(sock, "%s", buf);
361 ok = nntp_ok(sock, argbuf);
362 if (ok == NN_AUTHREQ) {
363 if (!sock->userid || !sock->passwd) {
364 sock->auth_failed = TRUE;
368 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
369 ok = nntp_ok(sock, NULL);
370 if (ok == NN_AUTHCONT) {
371 nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
372 ok = nntp_ok(sock, NULL);
374 if (ok != NN_SUCCESS) {
375 sock->auth_failed = TRUE;
379 nntp_gen_send(sock, "%s", buf);
380 ok = nntp_ok(sock, argbuf);