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(NNTPSockInfo *sock, const gchar *format, ...);
36 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size);
37 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
38 const gchar *format, ...);
40 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
43 NNTPSockInfo *nntp_sock;
45 nntp_sock = g_new0(NNTPSockInfo, 1);
46 if ((sock = sock_connect(server, port)) == NULL) {
47 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
52 nntp_sock->sock = sock;
54 if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
63 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
64 const gchar *userid, const gchar *passwd)
68 sock = nntp_open(server, port, buf);
71 sock->userid = g_strdup(userid);
72 sock->passwd = g_strdup(passwd);
77 void nntp_close(NNTPSockInfo *sock)
82 sock_close(sock->sock);
88 gint nntp_group(NNTPSockInfo *sock, const gchar *group,
89 gint *num, gint *first, gint *last)
93 gchar buf[NNTPBUFSIZE];
95 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
100 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
102 log_warning(_("protocol error: %s\n"), buf);
109 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num, gchar **msgid)
112 gchar buf[NNTPBUFSIZE];
115 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
117 ok = nntp_gen_command(sock, buf, cmd);
119 if (ok != NN_SUCCESS)
122 extract_parenthesis(buf, '<', '>');
123 if (buf[0] == '\0') {
124 log_warning(_("protocol error\n"));
127 *msgid = g_strdup(buf);
132 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
134 return nntp_get_article(sock, "ARTICLE", num, msgid);
137 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
139 return nntp_get_article(sock, "BODY", num, msgid);
142 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
144 return nntp_get_article(sock, "HEAD", num, msgid);
147 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
149 return nntp_get_article(sock, "STAT", num, msgid);
152 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
156 gchar buf[NNTPBUFSIZE];
158 ok = nntp_gen_command(sock, buf, "NEXT");
160 if (ok != NN_SUCCESS)
163 if (sscanf(buf, "%d %d", &resp, num) != 2) {
164 log_warning(_("protocol error: %s\n"), buf);
168 extract_parenthesis(buf, '<', '>');
169 if (buf[0] == '\0') {
170 log_warning(_("protocol error\n"));
173 *msgid = g_strdup(buf);
178 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
181 gchar buf[NNTPBUFSIZE];
183 ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
184 if (ok != NN_SUCCESS)
190 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
193 gchar buf[NNTPBUFSIZE];
195 ok = nntp_gen_command(sock, buf, "POST");
196 if (ok != NN_SUCCESS)
199 while (fgets(buf, sizeof(buf), fp) != NULL) {
202 if (sock_write(sock->sock, ".", 1) < 0) {
203 log_warning(_("Error occurred while posting\n"));
208 if (sock_puts(sock->sock, buf) < 0) {
209 log_warning(_("Error occurred while posting\n"));
214 sock_write(sock->sock, ".\r\n", 3);
215 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
221 gint nntp_newgroups(NNTPSockInfo *sock)
226 gint nntp_newnews(NNTPSockInfo *sock)
231 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
235 if (sock->auth_failed)
236 return NN_AUTHREQ; /* force reconnection */
238 ok = nntp_gen_command(sock, NULL, "MODE %s",
239 stream ? "STREAM" : "READER");
244 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
247 gchar buf[NNTPBUFSIZE];
249 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
253 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
258 if (!strncmp(buf, "381 ", 4))
261 } else if (!strncmp(buf, "480 ", 4))
270 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
272 gchar buf[NNTPBUFSIZE];
275 va_start(args, format);
276 g_vsnprintf(buf, sizeof(buf), format, args);
280 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
281 log_print("NNTP> AUTHINFO PASS ***\n");
283 log_print("NNTP> %s\n", buf);
287 sock_write(sock->sock, buf, strlen(buf));
290 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
292 if (sock_gets(sock->sock, buf, size) == -1)
298 log_print("NNTP< %s\n", buf);
303 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
304 const gchar *format, ...)
306 gchar buf[NNTPBUFSIZE];
310 va_start(args, format);
311 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
314 nntp_gen_send(sock, "%s", buf);
315 ok = nntp_ok(sock, argbuf);
316 if (ok == NN_AUTHREQ) {
317 if (!sock->userid || !sock->passwd) {
318 sock->auth_failed = TRUE;
321 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
322 ok = nntp_ok(sock, NULL);
323 if (ok == NN_AUTHCONT) {
324 nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
325 ok = nntp_ok(sock, NULL);
327 if (ok != NN_SUCCESS) {
328 sock->auth_failed = TRUE;
331 nntp_gen_send(sock, "%s", buf);
332 ok = nntp_ok(sock, argbuf);
338 nntp_list sends the command "LIST" to the news server,
339 a function is needed to read the newsgroups list.
342 gint nntp_list(NNTPSockInfo *sock)
344 return nntp_gen_command(sock, NULL, "LIST");