static gint verbose = 1;
-static void nntp_gen_send(gint sock, const gchar *format, ...);
-static gint nntp_gen_recv(gint sock, gchar *buf, gint size);
+static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...);
+static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size);
+static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
+ const gchar *format, ...);
-gint nntp_open(const gchar *server, gushort port, gchar *buf)
+NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
{
- SockInfo *sockinfo;
- gint sock;
+ SockInfo *sock;
+ NNTPSockInfo *nntp_sock;
- if ((sockinfo = sock_connect(server, port)) == NULL) {
+ nntp_sock = g_new0(NNTPSockInfo, 1);
+ if ((sock = sock_connect(server, port)) == NULL) {
log_warning(_("Can't connect to NNTP server: %s:%d\n"),
server, port);
- return -1;
+ g_free(nntp_sock);
+ return NULL;
}
- sock = sockinfo->sock;
- sock_sockinfo_free(sockinfo);
+ nntp_sock->sock = sock;
- if (nntp_ok(sock, buf) == NN_SUCCESS)
- return sock;
+ if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
+ return nntp_sock;
else {
sock_close(sock);
- return -1;
+ g_free(nntp_sock);
+ return NULL;
}
}
-gint nntp_group(gint sock, const gchar *group,
+NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
+ const gchar *userid, const gchar *passwd)
+{
+ NNTPSockInfo *sock;
+
+ sock = nntp_open(server, port, buf);
+ if (!sock)
+ return NULL;
+ sock->userid = g_strdup(userid);
+ sock->passwd = g_strdup(passwd);
+ return sock;
+}
+
+
+void nntp_close(NNTPSockInfo *sock)
+{
+ if (!sock)
+ return;
+
+ sock_close(sock->sock);
+ g_free(sock->passwd);
+ g_free(sock->userid);
+ g_free(sock);
+}
+
+gint nntp_group(NNTPSockInfo *sock, const gchar *group,
gint *num, gint *first, gint *last)
{
gint ok;
gint resp;
gchar buf[NNTPBUFSIZE];
- nntp_gen_send(sock, "GROUP %s", group);
+ ok = nntp_gen_command(sock, buf, "GROUP %s", group);
- if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+ if (ok != NN_SUCCESS)
return ok;
if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
return NN_SUCCESS;
}
-gint nntp_get_article(gint sock, const gchar *cmd, gint num, gchar **msgid)
+gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num, gchar **msgid)
{
gint ok;
gchar buf[NNTPBUFSIZE];
if (num > 0)
- nntp_gen_send(sock, "%s %d", cmd, num);
+ ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
else
- nntp_gen_send(sock, cmd);
+ ok = nntp_gen_command(sock, buf, cmd);
- if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+ if (ok != NN_SUCCESS)
return ok;
extract_parenthesis(buf, '<', '>');
return NN_SUCCESS;
}
-gint nntp_article(gint sock, gint num, gchar **msgid)
+gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
{
return nntp_get_article(sock, "ARTICLE", num, msgid);
}
-gint nntp_body(gint sock, gint num, gchar **msgid)
+gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
{
return nntp_get_article(sock, "BODY", num, msgid);
}
-gint nntp_head(gint sock, gint num, gchar **msgid)
+gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
{
return nntp_get_article(sock, "HEAD", num, msgid);
}
-gint nntp_stat(gint sock, gint num, gchar **msgid)
+gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
{
return nntp_get_article(sock, "STAT", num, msgid);
}
-gint nntp_next(gint sock, gint *num, gchar **msgid)
+gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
{
gint ok;
gint resp;
gchar buf[NNTPBUFSIZE];
- nntp_gen_send(sock, "NEXT");
+ ok = nntp_gen_command(sock, buf, "NEXT");
- if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+ if (ok != NN_SUCCESS)
return ok;
if (sscanf(buf, "%d %d", &resp, num) != 2) {
return NN_SUCCESS;
}
-gint nntp_xover(gint sock, gint first, gint last)
+gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
{
gint ok;
gchar buf[NNTPBUFSIZE];
- nntp_gen_send(sock, "XOVER %d-%d", first, last);
- if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+ ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
+ if (ok != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_post(gint sock, FILE *fp)
+gint nntp_post(NNTPSockInfo *sock, FILE *fp)
{
gint ok;
gchar buf[NNTPBUFSIZE];
- nntp_gen_send(sock, "POST");
- if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+ ok = nntp_gen_command(sock, buf, "POST");
+ if (ok != NN_SUCCESS)
return ok;
while (fgets(buf, sizeof(buf), fp) != NULL) {
strretchomp(buf);
if (buf[0] == '.') {
- if (sock_write(sock, ".", 1) < 0) {
+ if (sock_write(sock->sock, ".", 1) < 0) {
log_warning(_("Error occurred while posting\n"));
return NN_SOCKET;
}
}
- if (sock_puts(sock, buf) < 0) {
+ if (sock_puts(sock->sock, buf) < 0) {
log_warning(_("Error occurred while posting\n"));
return NN_SOCKET;
}
}
- sock_write(sock, ".\r\n", 3);
+ sock_write(sock->sock, ".\r\n", 3);
if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_newgroups(gint sock)
+gint nntp_newgroups(NNTPSockInfo *sock)
{
return NN_SUCCESS;
}
-gint nntp_newnews(gint sock)
+gint nntp_newnews(NNTPSockInfo *sock)
{
return NN_SUCCESS;
}
-gint nntp_mode(gint sock, gboolean stream)
+gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
{
gint ok;
- nntp_gen_send(sock, "MODE %s", stream ? "STREAM" : "READER");
- ok = nntp_ok(sock, NULL);
+ if (sock->auth_failed)
+ return NN_AUTHREQ; /* force reconnection */
+
+ ok = nntp_gen_command(sock, NULL, "MODE %s",
+ stream ? "STREAM" : "READER");
return ok;
}
-gint nntp_ok(gint sock, gchar *argbuf)
+gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
{
gint ok;
gchar buf[NNTPBUFSIZE];
if (argbuf)
strcpy(argbuf, buf);
+ if (!strncmp(buf, "381 ", 4))
+ return NN_AUTHCONT;
return NN_SUCCESS;
- } else
+ } else if (!strncmp(buf, "480 ", 4))
+ return NN_AUTHREQ;
+ else
return NN_ERROR;
}
return ok;
}
-static void nntp_gen_send(gint sock, const gchar *format, ...)
+static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
{
gchar buf[NNTPBUFSIZE];
va_list args;
g_vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
- if (verbose)
- log_print("NNTP> %s\n", buf);
+ if (verbose) {
+ if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
+ log_print("NNTP> AUTHINFO PASS ***\n");
+ else
+ log_print("NNTP> %s\n", buf);
+ }
strcat(buf, "\r\n");
- sock_write(sock, buf, strlen(buf));
+ sock_write(sock->sock, buf, strlen(buf));
}
-static gint nntp_gen_recv(gint sock, gchar *buf, gint size)
+static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
{
- if (sock_read(sock, buf, size) == -1)
+ if (sock_read(sock->sock, buf, size) == -1)
return NN_SOCKET;
strretchomp(buf);
return NN_SUCCESS;
}
+
+static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
+ const gchar *format, ...)
+{
+ gchar buf[NNTPBUFSIZE];
+ va_list args;
+ gint ok;
+
+ va_start(args, format);
+ g_vsnprintf(buf, sizeof(buf) - 2, format, args);
+ va_end(args);
+
+ nntp_gen_send(sock, "%s", buf);
+ ok = nntp_ok(sock, argbuf);
+ if (ok == NN_AUTHREQ) {
+ if (!sock->userid || !sock->passwd) {
+ sock->auth_failed = TRUE;
+ return ok;
+ }
+ nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
+ ok = nntp_ok(sock, NULL);
+ if (ok == NN_AUTHCONT) {
+ nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
+ ok = nntp_ok(sock, NULL);
+ }
+ if (ok != NN_SUCCESS) {
+ sock->auth_failed = TRUE;
+ return ok;
+ }
+ nntp_gen_send(sock, "%s", buf);
+ ok = nntp_ok(sock, argbuf);
+ }
+ return ok;
+}
+
+/*
+ nntp_list sends the command "LIST" to the news server,
+ a function is needed to read the newsgroups list.
+ */
+
+gint nntp_list(NNTPSockInfo *sock)
+{
+ return nntp_gen_command(sock, NULL, "LIST");
+}