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, SSLType ssl_type)
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 (ssl_type && !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, SSLType ssl_type)
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, ssl_type);
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) {
125 ok = nntp_gen_command(sock, buf, "MODE READER");
126 if (ok == NN_SUCCESS)
127 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
129 if (ok != NN_SUCCESS)
132 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
134 log_warning(_("protocol error: %s\n"), buf);
141 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
145 gchar buf[NNTPBUFSIZE];
148 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
150 ok = nntp_gen_command(sock, buf, cmd);
152 if (ok != NN_SUCCESS)
155 extract_parenthesis(buf, '<', '>');
156 if (buf[0] == '\0') {
157 log_warning(_("protocol error\n"));
160 *msgid = g_strdup(buf);
165 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
167 return nntp_get_article(sock, "ARTICLE", num, msgid);
170 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
172 return nntp_get_article(sock, "BODY", num, msgid);
175 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
177 return nntp_get_article(sock, "HEAD", num, msgid);
180 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
182 return nntp_get_article(sock, "STAT", num, msgid);
185 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
189 gchar buf[NNTPBUFSIZE];
191 ok = nntp_gen_command(sock, buf, "NEXT");
193 if (ok != NN_SUCCESS)
196 if (sscanf(buf, "%d %d", &resp, num) != 2) {
197 log_warning(_("protocol error: %s\n"), buf);
201 extract_parenthesis(buf, '<', '>');
202 if (buf[0] == '\0') {
203 log_warning(_("protocol error\n"));
206 *msgid = g_strdup(buf);
211 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
214 gchar buf[NNTPBUFSIZE];
216 ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
217 if (ok != NN_SUCCESS)
223 gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
226 gchar buf[NNTPBUFSIZE];
228 ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
229 if (ok != NN_SUCCESS)
235 gint nntp_list(NNTPSockInfo *sock)
237 return nntp_gen_command(sock, NULL, "LIST");
240 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
243 gchar buf[NNTPBUFSIZE];
245 ok = nntp_gen_command(sock, buf, "POST");
246 if (ok != NN_SUCCESS)
249 while (fgets(buf, sizeof(buf), fp) != NULL) {
252 if (sock_write(sock->sock, ".", 1) < 0) {
253 log_warning(_("Error occurred while posting\n"));
258 if (sock_puts(sock->sock, buf) < 0) {
259 log_warning(_("Error occurred while posting\n"));
264 sock_write(sock->sock, ".\r\n", 3);
265 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
271 gint nntp_newgroups(NNTPSockInfo *sock)
276 gint nntp_newnews(NNTPSockInfo *sock)
281 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
285 if (sock->auth_failed)
288 ok = nntp_gen_command(sock, NULL, "MODE %s",
289 stream ? "STREAM" : "READER");
294 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
297 gchar buf[NNTPBUFSIZE];
299 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
303 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
304 (buf[3] == ' ' || buf[3] == '\0')) {
308 if (!strncmp(buf, "381", 3))
312 } else if (!strncmp(buf, "480", 3))
321 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
323 gchar buf[NNTPBUFSIZE];
326 va_start(args, format);
327 g_vsnprintf(buf, sizeof(buf), format, args);
331 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
332 log_print("NNTP> AUTHINFO PASS ********\n");
334 log_print("NNTP> %s\n", buf);
338 sock_write(sock->sock, buf, strlen(buf));
341 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
343 if (sock_gets(sock->sock, buf, size) == -1)
349 log_print("NNTP< %s\n", buf);
354 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
355 const gchar *format, ...)
357 gchar buf[NNTPBUFSIZE];
361 va_start(args, format);
362 g_vsnprintf(buf, sizeof(buf), format, args);
365 nntp_gen_send(sock, "%s", buf);
366 ok = nntp_ok(sock, argbuf);
367 if (ok == NN_AUTHREQ) {
368 if (!sock->userid || !sock->passwd) {
369 sock->auth_failed = TRUE;
373 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
374 ok = nntp_ok(sock, NULL);
375 if (ok == NN_AUTHCONT) {
376 nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
377 ok = nntp_ok(sock, NULL);
379 if (ok != NN_SUCCESS) {
380 sock->auth_failed = TRUE;
384 nntp_gen_send(sock, "%s", buf);
385 ok = nntp_ok(sock, argbuf);