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.
37 static gint verbose = 1;
39 static void nntp_gen_send (NNTPSockInfo *sock,
42 static gint nntp_gen_recv (NNTPSockInfo *sock,
45 static gint nntp_gen_command (NNTPSockInfo *sock,
51 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf,
54 NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
58 NNTPSockInfo *nntp_sock;
60 if ((sock = sock_connect(server, port)) == NULL) {
61 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
67 if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
73 nntp_sock = g_new0(NNTPSockInfo, 1);
74 nntp_sock->sock = sock;
76 if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
86 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
87 const gchar *userid, const gchar *passwd,
90 NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
91 const gchar *userid, const gchar *passwd)
97 sock = nntp_open(server, port, buf, ssl_type);
99 sock = nntp_open(server, port, buf);
102 if (!sock) return NULL;
104 sock->userid = g_strdup(userid);
105 sock->passwd = g_strdup(passwd);
110 void nntp_close(NNTPSockInfo *sock)
114 sock_close(sock->sock);
115 g_free(sock->userid);
116 g_free(sock->passwd);
120 void nntp_forceauth(NNTPSockInfo *sock, gchar *buf, const gchar *userid, const gchar *passwd)
125 nntp_gen_command(sock, buf , "AUTHINFO USER %s", userid);
129 gint nntp_group(NNTPSockInfo *sock, const gchar *group,
130 gint *num, gint *first, gint *last)
134 gchar buf[NNTPBUFSIZE];
136 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
138 if (ok != NN_SUCCESS) {
139 ok = nntp_mode(sock, FALSE);
140 if (ok == NN_SUCCESS)
141 ok = nntp_gen_command(sock, buf, "GROUP %s", group);
144 if (ok != NN_SUCCESS)
147 if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
149 log_warning(_("protocol error: %s\n"), buf);
156 gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
160 gchar buf[NNTPBUFSIZE];
163 ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
165 ok = nntp_gen_command(sock, buf, cmd);
167 if (ok != NN_SUCCESS)
170 extract_parenthesis(buf, '<', '>');
171 if (buf[0] == '\0') {
172 log_warning(_("protocol error\n"));
175 *msgid = g_strdup(buf);
180 gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
182 return nntp_get_article(sock, "ARTICLE", num, msgid);
185 gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
187 return nntp_get_article(sock, "BODY", num, msgid);
190 gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
192 return nntp_get_article(sock, "HEAD", num, msgid);
195 gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
197 return nntp_get_article(sock, "STAT", num, msgid);
200 gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
204 gchar buf[NNTPBUFSIZE];
206 ok = nntp_gen_command(sock, buf, "NEXT");
208 if (ok != NN_SUCCESS)
211 if (sscanf(buf, "%d %d", &resp, num) != 2) {
212 log_warning(_("protocol error: %s\n"), buf);
216 extract_parenthesis(buf, '<', '>');
217 if (buf[0] == '\0') {
218 log_warning(_("protocol error\n"));
221 *msgid = g_strdup(buf);
226 gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
229 gchar buf[NNTPBUFSIZE];
231 ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
232 if (ok != NN_SUCCESS)
238 gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
241 gchar buf[NNTPBUFSIZE];
243 ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
244 if (ok != NN_SUCCESS)
250 gint nntp_list(NNTPSockInfo *sock)
252 return nntp_gen_command(sock, NULL, "LIST");
255 gint nntp_post(NNTPSockInfo *sock, FILE *fp)
258 gchar buf[NNTPBUFSIZE];
261 ok = nntp_gen_command(sock, buf, "POST");
262 if (ok != NN_SUCCESS)
265 msg = get_outgoing_rfc2822_str(fp);
266 if (sock_write_all(sock->sock, msg, strlen(msg)) < 0) {
267 log_warning(_("Error occurred while posting\n"));
273 sock_write_all(sock->sock, ".\r\n", 3);
274 if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
280 gint nntp_newgroups(NNTPSockInfo *sock)
285 gint nntp_newnews(NNTPSockInfo *sock)
290 gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
294 if (sock->auth_failed)
297 ok = nntp_gen_command(sock, NULL, "MODE %s",
298 stream ? "STREAM" : "READER");
303 gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
306 gchar buf[NNTPBUFSIZE];
308 if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
312 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
313 (buf[3] == ' ' || buf[3] == '\0')) {
317 if (!strncmp(buf, "381", 3))
321 } else if (!strncmp(buf, "480", 3))
330 static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
332 gchar buf[NNTPBUFSIZE];
335 va_start(args, format);
336 g_vsnprintf(buf, sizeof(buf), format, args);
340 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
341 log_print("NNTP> AUTHINFO PASS ********\n");
343 log_print("NNTP> %s\n", buf);
347 sock_write_all(sock->sock, buf, strlen(buf));
350 static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
352 if (sock_gets(sock->sock, buf, size) == -1)
358 log_print("NNTP< %s\n", buf);
363 static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
364 const gchar *format, ...)
366 gchar buf[NNTPBUFSIZE];
370 va_start(args, format);
371 g_vsnprintf(buf, sizeof(buf), format, args);
374 nntp_gen_send(sock, "%s", buf);
375 ok = nntp_ok(sock, argbuf);
376 if (ok == NN_AUTHREQ) {
377 if (!sock->userid || !sock->passwd) {
378 sock->auth_failed = TRUE;
382 nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
383 ok = nntp_ok(sock, NULL);
384 if (ok == NN_AUTHCONT) {
385 nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
386 ok = nntp_ok(sock, NULL);
388 if (ok != NN_SUCCESS) {
389 sock->auth_failed = TRUE;
393 nntp_gen_send(sock, "%s", buf);
394 ok = nntp_ok(sock, argbuf);
396 } else if (ok == NN_AUTHCONT) {
397 nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
398 ok = nntp_ok(sock, NULL);
400 if (ok != NN_SUCCESS) {
401 sock->auth_failed = TRUE;