2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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.
33 static gint verbose = 1;
35 static void nntp_gen_send(gint sock, const gchar *format, ...);
36 static gint nntp_gen_recv(gint sock, gchar *buf, gint size);
38 gint nntp_open(const gchar *server, gushort port, gchar *buf)
43 if ((sockinfo = sock_connect(server, port)) == NULL) {
44 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
48 sock = sockinfo->sock;
49 sock_sockinfo_free(sockinfo);
51 if (nntp_ok(sock, buf) == NN_SUCCESS)
59 gint nntp_group(gint sock, const gchar *group,
60 gint *num, gint *first, gint *last)
64 gchar buf[NNTPBUFSIZE];
66 nntp_gen_send(sock, "GROUP %s", group);
68 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
71 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
73 log_warning(_("protocol error: %s\n"), buf);
80 gint nntp_get_article(gint sock, const gchar *cmd, gint num, gchar **msgid)
83 gchar buf[NNTPBUFSIZE];
86 nntp_gen_send(sock, "%s %d", cmd, num);
88 nntp_gen_send(sock, cmd);
90 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
93 extract_parenthesis(buf, '<', '>');
95 log_warning(_("protocol error\n"));
98 *msgid = g_strdup(buf);
103 gint nntp_article(gint sock, gint num, gchar **msgid)
105 return nntp_get_article(sock, "ARTICLE", num, msgid);
108 gint nntp_body(gint sock, gint num, gchar **msgid)
110 return nntp_get_article(sock, "BODY", num, msgid);
113 gint nntp_head(gint sock, gint num, gchar **msgid)
115 return nntp_get_article(sock, "HEAD", num, msgid);
118 gint nntp_stat(gint sock, gint num, gchar **msgid)
120 return nntp_get_article(sock, "STAT", num, msgid);
123 gint nntp_next(gint sock, gint *num, gchar **msgid)
127 gchar buf[NNTPBUFSIZE];
129 nntp_gen_send(sock, "NEXT");
131 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
134 if (sscanf(buf, "%d %d", &resp, num) != 2) {
135 log_warning(_("protocol error: %s\n"), buf);
139 extract_parenthesis(buf, '<', '>');
140 if (buf[0] == '\0') {
141 log_warning(_("protocol error\n"));
144 *msgid = g_strdup(buf);
149 gint nntp_xover(gint sock, gint first, gint last)
152 gchar buf[NNTPBUFSIZE];
154 nntp_gen_send(sock, "XOVER %d-%d", first, last);
155 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
161 gint nntp_post(gint sock, FILE *fp)
164 gchar buf[NNTPBUFSIZE];
166 nntp_gen_send(sock, "POST");
167 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
170 while (fgets(buf, sizeof(buf), fp) != NULL) {
173 if (sock_write(sock, ".", 1) < 0) {
174 log_warning(_("Error occurred while posting\n"));
179 if (sock_puts(sock, buf) < 0) {
180 log_warning(_("Error occurred while posting\n"));
185 sock_write(sock, ".\r\n", 3);
186 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
192 gint nntp_newgroups(gint sock)
197 gint nntp_newnews(gint sock)
202 gint nntp_mode(gint sock, gboolean stream)
206 nntp_gen_send(sock, "MODE %s", stream ? "STREAM" : "READER");
207 ok = nntp_ok(sock, NULL);
212 gint nntp_authinfo_user(gint sock, const gchar *user)
215 gchar buf[NNTPBUFSIZE];
217 nntp_gen_send(sock, "AUTHINFO USER %s", user);
218 ok = nntp_ok(sock, buf);
223 gint nntp_authinfo_pass(gint sock, const gchar *pass)
226 gchar buf[NNTPBUFSIZE];
228 nntp_gen_send(sock, "AUTHINFO PASS %s", pass);
229 ok = nntp_ok(sock, buf);
234 gint nntp_ok(gint sock, gchar *argbuf)
237 gchar buf[NNTPBUFSIZE];
239 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
243 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
248 if (!strncmp(buf, "381 ", 4))
251 } else if (!strncmp(buf, "480 ", 4))
260 static void nntp_gen_send(gint sock, const gchar *format, ...)
262 gchar buf[NNTPBUFSIZE];
265 va_start(args, format);
266 g_vsnprintf(buf, sizeof(buf), format, args);
270 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
271 log_print("NNTP> AUTHINFO PASS ***\n");
273 log_print("NNTP> %s\n", buf);
277 sock_write(sock, buf, strlen(buf));
280 static gint nntp_gen_recv(gint sock, gchar *buf, gint size)
282 if (sock_read(sock, buf, size) == -1)
288 log_print("NNTP< %s\n", buf);
294 nntp_list sends the command "LIST" to the news server,
295 a function is needed to read the newsgroups list.
298 gint nntp_list(gint sock)
300 GList * result = NULL;
304 gchar buf[NNTPBUFSIZE];
306 nntp_gen_send(sock, "LIST");
308 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
312 log_print("NNTP< %s\n", buf);