/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
static gint verbose = 1;
-static void nntp_gen_send (NNTPSockInfo *sock,
+static void nntp_session_destroy(Session *session);
+
+static gint nntp_ok (SockInfo *sock,
+ gchar *argbuf);
+
+static void nntp_gen_send (SockInfo *sock,
const gchar *format,
...);
-static gint nntp_gen_recv (NNTPSockInfo *sock,
+static gint nntp_gen_recv (SockInfo *sock,
gchar *buf,
gint size);
-static gint nntp_gen_command (NNTPSockInfo *sock,
+static gint nntp_gen_command (NNTPSession *session,
gchar *argbuf,
const gchar *format,
...);
#if USE_OPENSSL
-NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf,
- SSLType ssl_type)
+Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
+ const gchar *userid, const gchar *passwd,
+ SSLType ssl_type)
#else
-NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
+Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
+ const gchar *userid, const gchar *passwd)
#endif
{
+ NNTPSession *session;
SockInfo *sock;
- NNTPSockInfo *nntp_sock;
if ((sock = sock_connect(server, port)) == NULL) {
log_warning(_("Can't connect to NNTP server: %s:%d\n"),
}
#endif
- nntp_sock = g_new0(NNTPSockInfo, 1);
- nntp_sock->sock = sock;
-
- if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
- return nntp_sock;
- else {
+ if (nntp_ok(sock, buf) != NN_SUCCESS) {
sock_close(sock);
- g_free(nntp_sock);
return NULL;
}
-}
-#if USE_OPENSSL
-NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
- const gchar *userid, const gchar *passwd,
- SSLType ssl_type)
-#else
-NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
- const gchar *userid, const gchar *passwd)
-#endif
-{
- NNTPSockInfo *sock;
+ session = g_new0(NNTPSession, 1);
+ SESSION(session)->type = SESSION_NEWS;
+ SESSION(session)->server = g_strdup(server);
+ SESSION(session)->sock = sock;
+ SESSION(session)->last_access_time = time(NULL);
+ SESSION(session)->data = NULL;
-#if USE_OPENSSL
- sock = nntp_open(server, port, buf, ssl_type);
-#else
- sock = nntp_open(server, port, buf);
-#endif
+ SESSION(session)->destroy = nntp_session_destroy;
- if (!sock) return NULL;
+ session->group = NULL;
- sock->userid = g_strdup(userid);
- sock->passwd = g_strdup(passwd);
+ if (userid && passwd) {
+ session->userid = g_strdup(userid);
+ session->passwd = g_strdup(passwd);
+ }
- return sock;
+ return SESSION(session);
}
-void nntp_close(NNTPSockInfo *sock)
+void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
+
{
- if (!sock) return;
+ if (!session) return;
+
+ nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
- sock_close(sock->sock);
- g_free(sock->userid);
- g_free(sock->passwd);
- g_free(sock);
-}
-void nntp_forceauth(NNTPSockInfo *sock, gchar *buf, const gchar *userid, const gchar *passwd)
+}
+static void nntp_session_destroy(Session *session)
{
- if (!sock) return;
-
- nntp_gen_command(sock, buf , "AUTHINFO USER %s", userid);
+ NNTPSession *nntp_session = NNTP_SESSION(session);
+ g_return_if_fail(session != NULL);
+ g_free(nntp_session->group);
+ g_free(nntp_session->userid);
+ g_free(nntp_session->passwd);
}
-gint nntp_group(NNTPSockInfo *sock, const gchar *group,
+
+gint nntp_group(NNTPSession *session, const gchar *group,
gint *num, gint *first, gint *last)
{
gint ok;
gint resp;
gchar buf[NNTPBUFSIZE];
- ok = nntp_gen_command(sock, buf, "GROUP %s", group);
+ ok = nntp_gen_command(session, buf, "GROUP %s", group);
if (ok != NN_SUCCESS) {
- ok = nntp_mode(sock, FALSE);
+ ok = nntp_mode(session, FALSE);
if (ok == NN_SUCCESS)
- ok = nntp_gen_command(sock, buf, "GROUP %s", group);
+ ok = nntp_gen_command(session, buf, "GROUP %s", group);
}
if (ok != NN_SUCCESS)
return NN_SUCCESS;
}
-gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
+gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
gchar **msgid)
{
gint ok;
gchar buf[NNTPBUFSIZE];
if (num > 0)
- ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
+ ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
else
- ok = nntp_gen_command(sock, buf, cmd);
+ ok = nntp_gen_command(session, buf, cmd);
if (ok != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
{
- return nntp_get_article(sock, "ARTICLE", num, msgid);
+ return nntp_get_article(session, "ARTICLE", num, msgid);
}
-gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
{
- return nntp_get_article(sock, "BODY", num, msgid);
+ return nntp_get_article(session, "BODY", num, msgid);
}
-gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
{
- return nntp_get_article(sock, "HEAD", num, msgid);
+ return nntp_get_article(session, "HEAD", num, msgid);
}
-gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
{
- return nntp_get_article(sock, "STAT", num, msgid);
+ return nntp_get_article(session, "STAT", num, msgid);
}
-gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
+gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
{
gint ok;
gint resp;
gchar buf[NNTPBUFSIZE];
- ok = nntp_gen_command(sock, buf, "NEXT");
+ ok = nntp_gen_command(session, buf, "NEXT");
if (ok != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
+gint nntp_xover(NNTPSession *session, gint first, gint last)
{
gint ok;
gchar buf[NNTPBUFSIZE];
- ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
+ ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
if (ok != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
+gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
{
gint ok;
gchar buf[NNTPBUFSIZE];
- ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
+ ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
+ header, first, last);
if (ok != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_list(NNTPSockInfo *sock)
+gint nntp_list(NNTPSession *session)
{
- return nntp_gen_command(sock, NULL, "LIST");
+ return nntp_gen_command(session, NULL, "LIST");
}
-gint nntp_post(NNTPSockInfo *sock, FILE *fp)
+gint nntp_post(NNTPSession *session, FILE *fp)
{
gint ok;
gchar buf[NNTPBUFSIZE];
gchar *msg;
- ok = nntp_gen_command(sock, buf, "POST");
+ ok = nntp_gen_command(session, buf, "POST");
if (ok != NN_SUCCESS)
return ok;
msg = get_outgoing_rfc2822_str(fp);
- if (sock_write_all(sock->sock, msg, strlen(msg)) < 0) {
+ if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
log_warning(_("Error occurred while posting\n"));
g_free(msg);
return NN_SOCKET;
}
g_free(msg);
- sock_write_all(sock->sock, ".\r\n", 3);
- if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+ sock_write_all(SESSION(session)->sock, ".\r\n", 3);
+ if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
return ok;
return NN_SUCCESS;
}
-gint nntp_newgroups(NNTPSockInfo *sock)
+gint nntp_newgroups(NNTPSession *session)
{
return NN_SUCCESS;
}
-gint nntp_newnews(NNTPSockInfo *sock)
+gint nntp_newnews(NNTPSession *session)
{
return NN_SUCCESS;
}
-gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
+gint nntp_mode(NNTPSession *session, gboolean stream)
{
gint ok;
- if (sock->auth_failed)
+ if (session->auth_failed)
return NN_AUTHREQ;
- ok = nntp_gen_command(sock, NULL, "MODE %s",
+ ok = nntp_gen_command(session, NULL, "MODE %s",
stream ? "STREAM" : "READER");
return ok;
}
-gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
+static gint nntp_ok(SockInfo *sock, gchar *argbuf)
{
gint ok;
gchar buf[NNTPBUFSIZE];
return ok;
}
-static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
+static void nntp_gen_send(SockInfo *sock, const gchar *format, ...)
{
gchar buf[NNTPBUFSIZE];
va_list args;
}
strcat(buf, "\r\n");
- sock_write_all(sock->sock, buf, strlen(buf));
+ sock_write_all(sock, buf, strlen(buf));
}
-static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
+static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
{
- if (sock_gets(sock->sock, buf, size) == -1)
+ if (sock_gets(sock, buf, size) == -1)
return NN_SOCKET;
strretchomp(buf);
return NN_SUCCESS;
}
-static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
+static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
const gchar *format, ...)
{
gchar buf[NNTPBUFSIZE];
va_list args;
gint ok;
+ SockInfo *sock;
va_start(args, format);
g_vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
+ sock = SESSION(session)->sock;
nntp_gen_send(sock, "%s", buf);
ok = nntp_ok(sock, argbuf);
if (ok == NN_AUTHREQ) {
- if (!sock->userid || !sock->passwd) {
- sock->auth_failed = TRUE;
+ if (!session->userid || !session->passwd) {
+ session->auth_failed = TRUE;
return ok;
}
- nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
+ nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
ok = nntp_ok(sock, NULL);
if (ok == NN_AUTHCONT) {
- nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
+ nntp_gen_send(sock, "AUTHINFO PASS %s",
+ session->passwd);
ok = nntp_ok(sock, NULL);
}
if (ok != NN_SUCCESS) {
- sock->auth_failed = TRUE;
+ session->auth_failed = TRUE;
return ok;
}
ok = nntp_ok(sock, argbuf);
} else if (ok == NN_AUTHCONT) {
- nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
+ nntp_gen_send(sock, "AUTHINFO PASS %s", session->passwd);
ok = nntp_ok(sock, NULL);
if (ok != NN_SUCCESS) {
- sock->auth_failed = TRUE;
+ session->auth_failed = TRUE;
return ok;
}
}
#endif
{
gchar buf[NNTPBUFSIZE];
- NNTPSession *session;
- NNTPSockInfo *nntp_sock;
+ Session *session;
g_return_val_if_fail(server != NULL, NULL);
log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
#if USE_OPENSSL
- if (userid && passwd)
- nntp_sock = nntp_open_auth(server, port, buf, userid, passwd,
- ssl_type);
- else
- nntp_sock = nntp_open(server, port, buf, ssl_type);
+ session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
#else
- if (userid && passwd)
- nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
- else
- nntp_sock = nntp_open(server, port, buf);
+ session = nntp_session_new(server, port, buf, userid, passwd);
#endif
- if (nntp_sock == NULL)
- return NULL;
-
- session = g_new0(NNTPSession, 1);
- session_init(SESSION(session));
- SESSION(session)->type = SESSION_NEWS;
- SESSION(session)->server = g_strdup(server);
- SESSION(session)->sock = NULL;
- SESSION(session)->data = NULL;
-
- SESSION(session)->destroy = news_session_destroy;
-
- session->nntp_sock = nntp_sock;
- session->group = NULL;
-
- return SESSION(session);
-}
-
-void news_session_destroy(Session *session)
-{
- nntp_close(NNTP_SESSION(session)->nntp_sock);
- NNTP_SESSION(session)->nntp_sock = NULL;
-
- g_free(NNTP_SESSION(session)->group);
+ return session;
}
static Session *news_session_new_for_folder(Folder *folder)
session = news_session_new(ac->nntp_server, port, userid, passwd);
#endif
if ((session != NULL) && ac->use_nntp_auth && ac->use_nntp_auth_onconnect)
- nntp_forceauth(NNTP_SESSION(session)->nntp_sock, buf, userid, passwd);
+ nntp_forceauth(NNTP_SESSION(session), buf, userid, passwd);
g_free(passwd);
return NNTP_SESSION(rfolder->session);
}
- if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
+ if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
!= NN_SUCCESS) {
log_warning("NNTP connection to %s:%d has been"
" disconnected. Reconnecting...\n",
return NULL;
}
- if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
+ if (nntp_list(session) != NN_SUCCESS) {
g_free(filename);
return NULL;
}
- if (recv_write_to_file
- (session->nntp_sock->sock, filename) < 0) {
+ if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
log_warning("can't retrieve newsgroup list\n");
session_destroy(SESSION(session));
REMOTE_FOLDER(folder)->session = NULL;
session = news_session_get(folder);
if (!session) return -1;
- ok = nntp_post(session->nntp_sock, fp);
+ ok = nntp_post(session, fp);
if (ok != NN_SUCCESS) {
log_warning("can't post article.\n");
return -1;
{
gchar *msgid;
- if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
+ if (nntp_get_article(session, cmd, num, &msgid)
!= NN_SUCCESS)
return -1;
debug_print("Message-Id = %s, num = %d\n", msgid, num);
g_free(msgid);
- if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
+ if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
log_warning("can't retrieve article %d\n", num);
return -1;
}
g_free(session->group);
session->group = NULL;
- ok = nntp_group(session->nntp_sock, group, num, first, last);
+ ok = nntp_group(session, group, num, first, last);
if (ok == NN_SUCCESS)
session->group = g_strdup(group);
#define READ_TO_LISTEND(hdr) \
while (!(buf[0] == '.' && buf[1] == '\r')) { \
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) { \
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
log_warning(_("error occurred while getting %s.\n"), hdr); \
return msginfo; \
} \
log_message(_("getting xover %d in %s...\n"),
num, item->path);
- if (nntp_xover(session->nntp_sock, num, num) != NN_SUCCESS) {
+ if (nntp_xover(session, num, num) != NN_SUCCESS) {
log_warning(_("can't get xover\n"));
return NULL;
}
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
log_warning(_("error occurred while getting xover.\n"));
return NULL;
}
msginfo->flags.tmp_flags = MSG_NEWS;
msginfo->newsgroups = g_strdup(item->path);
- if (nntp_xhdr(session->nntp_sock, "to", num, num) != NN_SUCCESS) {
+ if (nntp_xhdr(session, "to", num, num) != NN_SUCCESS) {
log_warning(_("can't get xhdr\n"));
return msginfo;
}
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
log_warning(_("error occurred while getting xhdr.\n"));
return msginfo;
}
READ_TO_LISTEND("xhdr (to)");
- if (nntp_xhdr(session->nntp_sock, "cc", num, num) != NN_SUCCESS) {
+ if (nntp_xhdr(session, "cc", num, num) != NN_SUCCESS) {
log_warning(_("can't get xhdr\n"));
return msginfo;
}
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
log_warning(_("error occurred while getting xhdr.\n"));
return msginfo;
}
log_message(_("getting xover %d - %d in %s...\n"),
begin, end, item->path);
- if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
+ if (nntp_xover(session, begin, end) != NN_SUCCESS) {
log_warning(_("can't get xover\n"));
return NULL;
}
for (;;) {
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
log_warning(_("error occurred while getting xover.\n"));
return newlist;
}
}
}
- if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
+ if (nntp_xhdr(session, "to", begin, end) != NN_SUCCESS) {
log_warning(_("can't get xhdr\n"));
return newlist;
}
llast = newlist;
for (;;) {
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
log_warning(_("error occurred while getting xhdr.\n"));
return newlist;
}
llast = llast->next;
}
- if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
+ if (nntp_xhdr(session, "cc", begin, end) != NN_SUCCESS) {
log_warning(_("can't get xhdr\n"));
return newlist;
}
llast = newlist;
for (;;) {
- if (sock_gets(session->nntp_sock->sock, buf, sizeof(buf)) < 0) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
log_warning(_("error occurred while getting xhdr.\n"));
return newlist;
}