2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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.
36 static gint verbose = 1;
38 static void nntp_gen_send (NNTPSockInfo *sock,
41 static gint nntp_gen_recv (NNTPSockInfo *sock,
44 static gint nntp_gen_command (NNTPSockInfo *sock,
50 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf,
53 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
57 NNTPSockInfo *nntp_sock;
59 if ((sock = sock_connect(server, port)) == NULL) {
60 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
66 if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
72 nntp_sock = g_new0(NNTPSockInfo, 1);
73 nntp_sock->sock = sock;
75 if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
85 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
86 const gchar *userid, const gchar *passwd,
89 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
90 const gchar *userid, const gchar *passwd)
96 sock = nntp_open(server, port, buf, ssl_type);
98 sock = nntp_open(server, port, buf);
101 if (!sock) return NULL;
103 sock->userid = g_strdup(userid);
104 sock->passwd = g_strdup(passwd);
109 void nntp_close(NNTPSockInfo *sock)
113 sock_close(sock->sock);
114 g_free(sock->userid);
115 g_free(sock->passwd);
119 gint nntp_group(NNTPSockInfo *sock, const gchar *group,
120 gint *num, gint *first, gint *last)
124 gchar buf[NNTPBUFSIZE];
126 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
128 if (ok != NN_SUCCESS) {
129 ok = nntp_mode(sock, FALSE);
130 if (ok == NN_SUCCESS)
131 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
134 if (ok != NN_SUCCESS)
137 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
139 log_warning(_("protocol error: %s\n"), buf);
146 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
150 gchar buf[NNTPBUFSIZE];
153 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
155 ok = nntp_gen_command(sock, buf, cmd);
157 if (ok != NN_SUCCESS)
160 extract_parenthesis(buf, '<', '>');
161 if (buf[0] == '\0') {
162 log_warning(_("protocol error\n"));
165 *msgid = g_strdup(buf);
170 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
172 return nntp_get_article(sock, "ARTICLE", num, msgid);
175 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
177 return nntp_get_article(sock, "BODY", num, msgid);
180 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
182 return nntp_get_article(sock, "HEAD", num, msgid);
185 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
187 return nntp_get_article(sock, "STAT", num, msgid);
190 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
194 gchar buf[NNTPBUFSIZE];
196 ok = nntp_gen_command(sock, buf, "NEXT");
198 if (ok != NN_SUCCESS)
201 if (sscanf(buf, "%d %d", &resp, num) != 2) {
202 log_warning(_("protocol error: %s\n"), buf);
206 extract_parenthesis(buf, '<', '>');
207 if (buf[0] == '\0') {
208 log_warning(_("protocol error\n"));
211 *msgid = g_strdup(buf);
216 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
219 gchar buf[NNTPBUFSIZE];
221 ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
222 if (ok != NN_SUCCESS)
228 gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
231 gchar buf[NNTPBUFSIZE];
233 ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
234 if (ok != NN_SUCCESS)
240 gint nntp_list(NNTPSockInfo *sock)
242 return nntp_gen_command(sock, NULL, "LIST");
245 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
248 gchar buf[NNTPBUFSIZE];
250 ok = nntp_gen_command(sock, buf, "POST");
251 if (ok != NN_SUCCESS)
254 while (fgets(buf, sizeof(buf), fp) != NULL) {
257 if (sock_write(sock->sock, ".", 1) < 0) {
258 log_warning(_("Error occurred while posting\n"));
263 if (sock_puts(sock->sock, buf) < 0) {
264 log_warning(_("Error occurred while posting\n"));
269 sock_write(sock->sock, ".\r\n", 3);
270 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
276 gint nntp_newgroups(NNTPSockInfo *sock)
281 gint nntp_newnews(NNTPSockInfo *sock)
286 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
290 if (sock->auth_failed)
293 ok = nntp_gen_command(sock, NULL, "MODE %s",
294 stream ? "STREAM" : "READER");
299 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
302 gchar buf[NNTPBUFSIZE];
304 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
308 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
309 (buf[3] == ' ' || buf[3] == '\0')) {
313 if (!strncmp(buf, "381", 3))
317 } else if (!strncmp(buf, "480", 3))
326 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
328 gchar buf[NNTPBUFSIZE];
331 va_start(args, format);
332 g_vsnprintf(buf, sizeof(buf), format, args);
336 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
337 log_print("NNTP> AUTHINFO PASS ********\n");
339 log_print("NNTP> %s\n", buf);
343 sock_write(sock->sock, buf, strlen(buf));
346 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
348 if (sock_gets(sock->sock, buf, size) == -1)
354 log_print("NNTP< %s\n", buf);
359 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
360 const gchar *format, ...)
362 gchar buf[NNTPBUFSIZE];
366 va_start(args, format);
367 g_vsnprintf(buf, sizeof(buf), format, args);
370 nntp_gen_send(sock, "%s", buf);
371 ok = nntp_ok(sock, argbuf);
372 if (ok == NN_AUTHREQ) {
373 if (!sock->userid || !sock->passwd) {
374 sock->auth_failed = TRUE;
378 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
379 ok = nntp_ok(sock, NULL);
380 if (ok == NN_AUTHCONT) {
381 nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
382 ok = nntp_ok(sock, NULL);
384 if (ok != NN_SUCCESS) {
385 sock->auth_failed = TRUE;
389 nntp_gen_send(sock, "%s", buf);
390 ok = nntp_ok(sock, argbuf);