/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 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
# include "config.h"
#endif
+#include "defs.h"
+
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
-#include "defs.h"
#include "intl.h"
#include "news.h"
#include "nntp.h"
#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
+#include "prefs_account.h"
+#include "inputdialog.h"
+#include "alertpanel.h"
+
+static Session *news_session_new (const gchar *server,
+ gushort port,
+ const gchar *userid,
+ const gchar *passwd);
static gint news_get_article_cmd (NNTPSession *session,
const gchar *cmd,
gint num,
gchar *filename);
+static gint news_select_group (NNTPSession *session,
+ const gchar *group);
static GSList *news_get_uncached_articles(NNTPSession *session,
FolderItem *item,
gint cache_last,
gint *rfirst,
gint *rlast);
static MsgInfo *news_parse_xover (const gchar *xover_str);
+static gchar *news_parse_xhdr (const gchar *xhdr_str,
+ MsgInfo *msginfo);
static GSList *news_delete_old_article (GSList *alist,
gint first);
static void news_delete_all_article (FolderItem *item);
-Session *news_session_new(const gchar *server, gushort port)
+static Session *news_session_new(const gchar *server, gushort port,
+ const gchar *userid, const gchar *passwd)
{
gchar buf[NNTPBUFSIZE];
NNTPSession *session;
- gint nntp_sock;
+ NNTPSockInfo *nntp_sock;
g_return_val_if_fail(server != NULL, NULL);
log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
- if ((nntp_sock = nntp_open(server, port, buf)) < 0)
+ if (userid && passwd)
+ nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
+ else
+ nntp_sock = nntp_open(server, port, buf);
+ if (nntp_sock == NULL)
return NULL;
session = g_new(NNTPSession, 1);
SESSION(session)->type = SESSION_NEWS;
SESSION(session)->server = g_strdup(server);
- SESSION(session)->sock = nntp_sock;
+ session->nntp_sock = nntp_sock;
+ SESSION(session)->sock = nntp_sock->sock;
SESSION(session)->connected = TRUE;
SESSION(session)->phase = SESSION_READY;
SESSION(session)->data = NULL;
void news_session_destroy(NNTPSession *session)
{
- close(SESSION(session)->sock);
+ nntp_close(session->nntp_sock);
+ session->nntp_sock = NULL;
+ SESSION(session)->sock = NULL;
g_free(session->group);
}
+static gchar *news_query_password(const gchar *server, const gchar *user)
+{
+ gchar *message;
+ gchar *pass;
+
+ message = g_strdup_printf(_("Input password for %s on %s:"),
+ user, server);
+ pass = input_dialog_with_invisible(_("Input password"), message, NULL);
+ g_free(message);
+
+ return pass;
+}
+
+static Session *news_session_new_for_folder(Folder *folder)
+{
+ Session *session;
+ PrefsAccount *ac;
+ const gchar *userid = NULL;
+ gchar *passwd = NULL;
+
+ ac = folder->account;
+ if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
+ userid = ac->userid;
+ if (ac->passwd && ac->passwd[0])
+ passwd = g_strdup(ac->passwd);
+ else
+ passwd = news_query_password(ac->nntp_server, userid);
+ }
+
+ session = news_session_new(ac->nntp_server, 119, userid, passwd);
+ g_free(passwd);
+
+ return session;
+}
+
NNTPSession *news_session_get(Folder *folder)
{
+ NNTPSession *session;
+
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(folder->type == F_NEWS, NULL);
g_return_val_if_fail(folder->account != NULL, NULL);
if (!REMOTE_FOLDER(folder)->session) {
REMOTE_FOLDER(folder)->session =
- news_session_new(folder->account->nntp_server, 119);
+ news_session_new_for_folder(folder);
} else {
- if (nntp_mode(REMOTE_FOLDER(folder)->session->sock, FALSE)
- != NN_SUCCESS) {
+ session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
+ if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
log_warning(_("NNTP connection to %s:%d has been"
" disconnected. Reconnecting...\n"),
folder->account->nntp_server, 119);
session_destroy(REMOTE_FOLDER(folder)->session);
REMOTE_FOLDER(folder)->session =
- news_session_new(folder->account->nntp_server,
- 119);
+ news_session_new_for_folder(folder);
}
}
g_return_val_if_fail(item != NULL, NULL);
path = folder_item_get_path(item);
+ if (!is_dir_exist(path))
+ make_dir_hier(path);
+
filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
g_free(path);
return NULL;
}
+ ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
+ item->path);
+ if (ok != NN_SUCCESS) {
+ g_warning(_("can't select group %s\n"), item->path);
+ g_free(filename);
+ return NULL;
+ }
+
debug_print(_("getting article %d...\n"), num);
ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
num, filename);
return -1;
}
- ok = nntp_post(SESSION(session)->sock, fp);
+ ok = nntp_post(session->nntp_sock, fp);
if (ok != NN_SUCCESS) {
log_warning(_("can't post article.\n"));
return -1;
{
gchar *msgid;
- if (nntp_get_article(SESSION(session)->sock, cmd, num, &msgid)
+ if (nntp_get_article(session->nntp_sock, 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(session)->sock, filename) < 0) {
+ if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
log_warning(_("can't retrieve article %d\n"), num);
return -1;
}
return news_get_article_cmd(session, "HEAD", num, filename);
}
+/**
+ * news_select_group:
+ * @session: Active NNTP session.
+ * @group: Newsgroup name.
+ *
+ * Select newsgroup @group with the GROUP command if it is not already
+ * selected in @session.
+ *
+ * Return value: NNTP result code.
+ **/
+static gint news_select_group(NNTPSession *session, const gchar *group)
+{
+ gint ok;
+ gint num, first, last;
+
+ if (session->group && g_strcasecmp(session->group, group) == 0)
+ return NN_SUCCESS;
+
+ g_free(session->group);
+ session->group = NULL;
+
+ ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
+ if (ok == NN_SUCCESS)
+ session->group = g_strdup(group);
+
+ return ok;
+}
+
static GSList *news_get_uncached_articles(NNTPSession *session,
FolderItem *item, gint cache_last,
gint *rfirst, gint *rlast)
g_return_val_if_fail(item->folder != NULL, NULL);
g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
- ok = nntp_group(SESSION(session)->sock, item->path,
+ g_free(session->group);
+ session->group = NULL;
+
+ ok = nntp_group(session->nntp_sock, item->path,
&num, &first, &last);
if (ok != NN_SUCCESS) {
log_warning(_("can't set group: %s\n"), item->path);
return NULL;
}
+ session->group = g_strdup(item->path);
/* calculate getting overview range */
if (first > last) {
begin = cache_last + 1;
end = last;
+ if (rfirst) *rfirst = first;
+ if (rlast) *rlast = last;
+
if (prefs_common.max_articles > 0 &&
end - begin + 1 > prefs_common.max_articles)
begin = end - prefs_common.max_articles + 1;
log_message(_("getting xover %d - %d in %s...\n"),
begin, end, item->path);
- if (nntp_xover(SESSION(session)->sock, begin, end) != NN_SUCCESS) {
+ if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
log_warning(_("can't get xover\n"));
return NULL;
}
for (;;) {
- if (sock_read(SESSION(session)->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;
}
msginfo->folder = item;
msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
+ msginfo->newsgroups = g_strdup(item->path);
if (!newlist)
llast = newlist = g_slist_append(newlist, msginfo);
}
}
- if (rfirst) *rfirst = first;
- if (rlast) *rlast = last;
+ if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
+ log_warning(_("can't get xhdr\n"));
+ return newlist;
+ }
+
+ llast = newlist;
+
+ for (;;) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+ log_warning(_("error occurred while getting xhdr.\n"));
+ return newlist;
+ }
+
+ if (buf[0] == '.' && buf[1] == '\r') break;
+ if (!llast) {
+ g_warning("llast == NULL\n");
+ continue;
+ }
+
+ msginfo = (MsgInfo *)llast->data;
+ msginfo->to = news_parse_xhdr(buf, msginfo);
+
+ llast = llast->next;
+ }
+
+ if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
+ log_warning(_("can't get xhdr\n"));
+ return newlist;
+ }
+
+ llast = newlist;
+
+ for (;;) {
+ if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+ log_warning(_("error occurred while getting xhdr.\n"));
+ return newlist;
+ }
+
+ if (buf[0] == '.' && buf[1] == '\r') break;
+ if (!llast) {
+ g_warning("llast == NULL\n");
+ continue;
+ }
+
+ msginfo = (MsgInfo *)llast->data;
+ msginfo->cc = news_parse_xhdr(buf, msginfo);
+
+ llast = llast->next;
+ }
+
return newlist;
}
if (*msgid != '\0')
msginfo->msgid = g_strdup(msgid);
+ msginfo->references = g_strdup(ref);
eliminate_parenthesis(ref, '(', ')');
if ((p = strrchr(ref, '<')) != NULL) {
extract_parenthesis(p, '<', '>');
return msginfo;
}
+static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
+{
+ gchar buf[NNTPBUFSIZE];
+ gchar *p;
+ gchar *tmp;
+ gint num;
+
+ p = strchr(xhdr_str, ' ');
+ if (!p)
+ return NULL;
+ else
+ p++;
+
+ num = atoi(xhdr_str);
+ if (msginfo->msgnum != num) return NULL;
+
+ tmp = strchr(p, '\r');
+ if (!tmp) tmp = strchr(p, '\n');
+
+ if (tmp)
+ return g_strndup(p, tmp - p);
+ else
+ return g_strdup(p);
+}
+
static GSList *news_delete_old_article(GSList *alist, gint first)
{
GSList *cur, *next;
gchar *file;
dir = folder_item_get_path(item);
+ if (!is_dir_exist(dir))
+ make_dir_hier(dir);
+
if ((dp = opendir(dir)) == NULL) {
FILE_OP_ERROR(dir, "opendir");
g_free(dir);
if (is_file_exist(filename)) {
debug_print(_("group list has been already cached.\n"));
}
- else
- {
- ok = nntp_list(SESSION(session)->sock);
+ else {
+ ok = nntp_list(session->nntp_sock);
if (ok != NN_SUCCESS)
return NULL;
- if (recv_write_to_file(SESSION(session)->sock, filename)
- < 0) {
+ if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
log_warning(_("can't retrieve group list\n"));
return NULL;
}
- }
+ }
f = fopen(filename, "r");
- while (fgets(buf, NNTPBUFSIZE, f))
- {
+ while (fgets(buf, NNTPBUFSIZE, f)) {
char * s;
len = 0;
s = g_strdup(buf);
group_list = g_slist_append(group_list, s);
- }
+ }
fclose(f);
g_free(filename);
debug_print(_("\tDeleting cached group list... "));
path = folder_item_get_path(item);
+ if (!is_dir_exist(path))
+ make_dir_hier(path);
+
filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
g_free(path);
if (remove(filename) != 0)