0.9.7claws10
[claws.git] / src / news.c
index 94a3b3f189b5363cb56654a58f4dd01a72d33be1..24a4adb21098f512dc147a7bbef1bf979984f8e1 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 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
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <time.h>
 
 #include "intl.h"
 #include "news.h"
 #include "prefs_common.h"
 #include "prefs_account.h"
 #include "inputdialog.h"
-#include "alertpanel.h"
+#include "log.h"
+#include "progressindicator.h"
+#include "remotefolder.h"
+#if USE_OPENSSL
+#  include "ssl.h"
+#endif
 
 #define NNTP_PORT      119
+#if USE_OPENSSL
+#define NNTPS_PORT     563
+#endif
+
+typedef struct _NewsFolder     NewsFolder;
+
+#define NEWS_FOLDER(obj)       ((NewsFolder *)obj)
+
+struct _NewsFolder
+{
+       RemoteFolder rfolder;
+
+       gboolean use_auth;
+};
 
+static void news_folder_init            (Folder        *folder,
+                                         const gchar   *name,
+                                         const gchar   *path);
+
+static Folder  *news_folder_new        (const gchar    *name,
+                                        const gchar    *folder);
+static void     news_folder_destroy    (Folder         *folder);
+
+static gchar *news_fetch_msg           (Folder         *folder,
+                                        FolderItem     *item,
+                                        gint            num);
+
+
+#if USE_OPENSSL
+static Session *news_session_new        (const gchar   *server,
+                                         gushort        port,
+                                         const gchar   *userid,
+                                         const gchar   *passwd,
+                                         SSLType        ssl_type);
+#else
 static Session *news_session_new        (const gchar   *server,
                                          gushort        port,
                                          const gchar   *userid,
                                          const gchar   *passwd);
+#endif
 
 static gint news_get_article_cmd        (NNTPSession   *session,
                                          const gchar   *cmd,
@@ -61,76 +102,133 @@ static gint news_get_article_cmd   (NNTPSession   *session,
 static gint news_get_article            (NNTPSession   *session,
                                          gint           num,
                                          gchar         *filename);
-static gint news_get_header             (NNTPSession   *session,
-                                         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);
+                                         const gchar   *group,
+                                         gint          *num,
+                                         gint          *first,
+                                         gint          *last);
 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);
-
+gint news_get_num_list                  (Folder        *folder, 
+                                         FolderItem    *item,
+                                         GSList       **list,
+                                         gboolean      *old_uids_valid);
+MsgInfo *news_get_msginfo               (Folder        *folder, 
+                                         FolderItem    *item,
+                                         gint           num);
+GSList *news_get_msginfos               (Folder        *folder,
+                                         FolderItem    *item,
+                                         GSList        *msgnum_list);
+gboolean news_scan_required             (Folder        *folder,
+                                         FolderItem    *item);
+
+gint news_post_stream                   (Folder        *folder, 
+                                         FILE          *fp);
+static gchar *news_folder_get_path      (Folder        *folder);
+gchar *news_item_get_path               (Folder        *folder,
+                                         FolderItem    *item);
+
+static FolderClass news_class =
+{
+       F_NEWS,
+       "news",
+       "News",
+
+       /* Folder functions */
+       news_folder_new,
+       news_folder_destroy,
+       NULL,
+       NULL,
+
+       /* FolderItem functions */
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       news_item_get_path,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       news_get_num_list,
+       NULL,
+       NULL,
+       NULL,
+       news_scan_required,
+
+       /* Message functions */
+       news_get_msginfo,
+       news_get_msginfos,
+       news_fetch_msg,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+};
+
+FolderClass *news_get_class(void)
+{
+       return &news_class;
+}
 
-static Session *news_session_new(const gchar *server, gushort port,
-                                const gchar *userid, const gchar *passwd)
+static Folder *news_folder_new(const gchar *name, const gchar *path)
 {
-       gchar buf[NNTPBUFSIZE];
-       NNTPSession *session;
-       NNTPSockInfo *nntp_sock;
+       Folder *folder;
 
-       g_return_val_if_fail(server != NULL, NULL);
+       folder = (Folder *)g_new0(NewsFolder, 1);
+       folder->klass = &news_class;
+       news_folder_init(folder, name, path);
 
-       log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
+       return folder;
+}
 
-       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;
+static void news_folder_destroy(Folder *folder)
+{
+       gchar *dir;
 
-       session = g_new(NNTPSession, 1);
-       SESSION(session)->type      = SESSION_NEWS;
-       SESSION(session)->server    = g_strdup(server);
-       session->nntp_sock          = nntp_sock;
-       SESSION(session)->sock      = nntp_sock->sock;
-       SESSION(session)->connected = TRUE;
-       SESSION(session)->phase     = SESSION_READY;
-       SESSION(session)->data      = NULL;
-       session->group = NULL;
+       dir = news_folder_get_path(folder);
+       if (is_dir_exist(dir))
+               remove_dir_recursive(dir);
+       g_free(dir);
 
-       return SESSION(session);
+       folder_remote_folder_destroy(REMOTE_FOLDER(folder));
 }
 
-void news_session_destroy(NNTPSession *session)
+static void news_folder_init(Folder *folder, const gchar *name,
+                            const gchar *path)
 {
-       nntp_close(session->nntp_sock);
-       session->nntp_sock = NULL;
-       SESSION(session)->sock = NULL;
-
-       g_free(session->group);
+       folder_remote_folder_init(folder, name, path);
 }
 
-static gchar *news_query_password(const gchar *server, const gchar *user)
+#if USE_OPENSSL
+static Session *news_session_new(const gchar *server, gushort port,
+                                const gchar *userid, const gchar *passwd,
+                                SSLType ssl_type)
+#else
+static Session *news_session_new(const gchar *server, gushort port,
+                                const gchar *userid, const gchar *passwd)
+#endif
 {
-       gchar *message;
-       gchar *pass;
+       gchar buf[NNTPBUFSIZE];
+       Session *session;
 
-       message = g_strdup_printf(_("Input password for %s on %s:"),
-                                 user, server);
-       pass = input_dialog_with_invisible(_("Input password"), message, NULL);
-       g_free(message);
+       g_return_val_if_fail(server != NULL, NULL);
+
+       log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
 
-       return pass;
+#if USE_OPENSSL
+       session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
+#else
+       session = nntp_session_new(server, port, buf, userid, passwd);
+#endif
+
+       return session;
 }
 
 static Session *news_session_new_for_folder(Folder *folder)
@@ -139,6 +237,11 @@ static Session *news_session_new_for_folder(Folder *folder)
        PrefsAccount *ac;
        const gchar *userid = NULL;
        gchar *passwd = NULL;
+       gushort port;
+       gchar buf[NNTPBUFSIZE];
+
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(folder->account != NULL, NULL);
 
        ac = folder->account;
        if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
@@ -146,91 +249,65 @@ static Session *news_session_new_for_folder(Folder *folder)
                if (ac->passwd && ac->passwd[0])
                        passwd = g_strdup(ac->passwd);
                else
-                       passwd = news_query_password(ac->nntp_server, userid);
+                       passwd = input_dialog_query_password(ac->nntp_server,
+                                                            userid);
        }
 
-       session = news_session_new(ac->nntp_server, NNTP_PORT, userid, passwd);
+#if USE_OPENSSL
+       port = ac->set_nntpport ? ac->nntpport
+               : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
+       session = news_session_new(ac->nntp_server, port, userid, passwd,
+                                  ac->ssl_nntp);
+#else
+       port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
+       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), buf, userid, passwd);
+
        g_free(passwd);
 
        return session;
 }
 
-NNTPSession *news_session_get(Folder *folder)
+static NNTPSession *news_session_get(Folder *folder)
 {
-       NNTPSession *session;
+       RemoteFolder *rfolder = REMOTE_FOLDER(folder);
 
        g_return_val_if_fail(folder != NULL, NULL);
-       g_return_val_if_fail(folder->type == F_NEWS, NULL);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
 
-       if (!REMOTE_FOLDER(folder)->session) {
-               REMOTE_FOLDER(folder)->session =
-                       news_session_new_for_folder(folder);
-               return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
+       if (!rfolder->session) {
+               rfolder->session = news_session_new_for_folder(folder);
+               return NNTP_SESSION(rfolder->session);
        }
 
-       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, NNTP_PORT);
-               session_destroy(REMOTE_FOLDER(folder)->session);
-               REMOTE_FOLDER(folder)->session =
-                       news_session_new_for_folder(folder);
+       if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
+               rfolder->session->last_access_time = time(NULL);
+               return NNTP_SESSION(rfolder->session);
        }
 
-       return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
-}
-
-GSList *news_get_article_list(Folder *folder, FolderItem *item,
-                             gboolean use_cache)
-{
-       GSList *alist;
-       NNTPSession *session;
-
-       g_return_val_if_fail(folder != NULL, NULL);
-       g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(folder->type == F_NEWS, NULL);
-
-       session = news_session_get(folder);
-
-       if (!session) {
-               alist = procmsg_read_cache(item, FALSE);
-               item->last_num = procmsg_get_last_num_in_cache(alist);
-       } else if (use_cache) {
-               GSList *newlist;
-               gint cache_last;
-               gint first, last;
-
-               alist = procmsg_read_cache(item, FALSE);
-
-               cache_last = procmsg_get_last_num_in_cache(alist);
-               newlist = news_get_uncached_articles
-                       (session, item, cache_last, &first, &last);
-               alist = news_delete_old_article(alist, first);
-
-               alist = g_slist_concat(alist, newlist);
-               item->last_num = last;
-       } else {
-               gint last;
-
-               alist = news_get_uncached_articles
-                       (session, item, 0, NULL, &last);
-               news_delete_all_article(item);
-               item->last_num = last;
+       if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
+           != NN_SUCCESS) {
+               log_warning("NNTP connection to %s:%d has been"
+                             " disconnected. Reconnecting...\n",
+                           folder->account->nntp_server,
+                           folder->account->set_nntpport ?
+                           folder->account->nntpport : NNTP_PORT);
+               session_destroy(rfolder->session);
+               rfolder->session = news_session_new_for_folder(folder);
        }
 
-       procmsg_set_flags(alist, item);
-
-       statusbar_pop_all();
-
-       return alist;
+       if (rfolder->session)
+               rfolder->session->last_access_time = time(NULL);
+       return NNTP_SESSION(rfolder->session);
 }
 
-gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
+static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
 {
        gchar *path, *filename;
+       NNTPSession *session;
        gint ok;
 
        g_return_val_if_fail(folder != NULL, NULL);
@@ -239,34 +316,34 @@ gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
        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);
 
        if (is_file_exist(filename)) {
-               debug_print(_("article %d has been already cached.\n"), num);
+               debug_print("article %d has been already cached.\n", num);
                return filename;
        }
 
-       if (!REMOTE_FOLDER(folder)->session) {
+       session = news_session_get(folder);
+       if (!session) {
                g_free(filename);
                return NULL;
        }
 
-       ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
-                              item->path);
+       ok = news_select_group(session, item->path, NULL, NULL, NULL);
        if (ok != NN_SUCCESS) {
-               g_warning(_("can't select group %s\n"), item->path);
+               g_warning("can't select group %s\n", item->path);
                g_free(filename);
                return NULL;
        }
 
-       debug_print(_("getting article %d...\n"), num);
+       debug_print("getting article %d...\n", num);
        ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
                              num, filename);
-       statusbar_pop_all();
        if (ok < 0) {
-               g_warning(_("can't read article %d\n"), num);
+               g_warning("can't read article %d\n", num);
+               session_destroy(SESSION(session));
+               REMOTE_FOLDER(folder)->session = NULL;
                g_free(filename);
                return NULL;
        }
@@ -274,37 +351,179 @@ gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
        return filename;
 }
 
-void news_scan_group(Folder *folder, FolderItem *item)
+static NewsGroupInfo *news_group_info_new(const gchar *name,
+                                         gint first, gint last, gchar type)
+{
+       NewsGroupInfo *ginfo;
+
+       ginfo = g_new(NewsGroupInfo, 1);
+       ginfo->name = g_strdup(name);
+       ginfo->first = first;
+       ginfo->last = last;
+       ginfo->type = type;
+
+       return ginfo;
+}
+
+static void news_group_info_free(NewsGroupInfo *ginfo)
+{
+       g_free(ginfo->name);
+       g_free(ginfo);
+}
+
+static gint news_group_info_compare(NewsGroupInfo *ginfo1,
+                                   NewsGroupInfo *ginfo2)
 {
+       return g_strcasecmp(ginfo1->name, ginfo2->name);
+}
+
+GSList *news_get_group_list(Folder *folder)
+{
+       gchar *path, *filename;
+       FILE *fp;
+       GSList *list = NULL;
+       GSList *last = NULL;
+       gchar buf[NNTPBUFSIZE];
+
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
+
+       path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
+       if (!is_dir_exist(path))
+               make_dir_hier(path);
+       filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
+       g_free(path);
+
+       if ((fp = fopen(filename, "rb")) == NULL) {
+               NNTPSession *session;
+
+               session = news_session_get(folder);
+               if (!session) {
+                       g_free(filename);
+                       return NULL;
+               }
+
+               if (nntp_list(session) != NN_SUCCESS) {
+                       g_free(filename);
+                       return NULL;
+               }
+               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;
+                       g_free(filename);
+                       return NULL;
+               }
+
+               if ((fp = fopen(filename, "rb")) == NULL) {
+                       FILE_OP_ERROR(filename, "fopen");
+                       g_free(filename);
+                       return NULL;
+               }
+       }
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               gchar *p = buf;
+               gchar *name;
+               gint last_num;
+               gint first_num;
+               gchar type;
+               NewsGroupInfo *ginfo;
+
+               p = strchr(p, ' ');
+               if (!p) continue;
+               *p = '\0';
+               p++;
+               name = buf;
+
+               if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
+                       continue;
+
+               ginfo = news_group_info_new(name, first_num, last_num, type);
+
+               if (!last)
+                       last = list = g_slist_append(NULL, ginfo);
+               else {
+                       last = g_slist_append(last, ginfo);
+                       last = last->next;
+               }
+       }
+
+       fclose(fp);
+       g_free(filename);
+
+       list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
+
+       return list;
+}
+
+void news_group_list_free(GSList *group_list)
+{
+       GSList *cur;
+
+       if (!group_list) return;
+
+       for (cur = group_list; cur != NULL; cur = cur->next)
+               news_group_info_free((NewsGroupInfo *)cur->data);
+       g_slist_free(group_list);
+}
+
+void news_remove_group_list_cache(Folder *folder)
+{
+       gchar *path, *filename;
+
+       g_return_if_fail(folder != NULL);
+       g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
+
+       path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
+       filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
+       g_free(path);
+
+       if (is_file_exist(filename)) {
+               if (remove(filename) < 0)
+                       FILE_OP_ERROR(filename, "remove");
+       }
+       g_free(filename);
 }
 
 gint news_post(Folder *folder, const gchar *file)
 {
-       NNTPSession *session;
        FILE *fp;
        gint ok;
 
        g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(folder->type == F_NEWS, -1);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
        g_return_val_if_fail(file != NULL, -1);
 
-       session = news_session_get(folder);
-       if (!session) return -1;
-
-       if ((fp = fopen(file, "r")) == NULL) {
+       if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return -1;
        }
 
-       ok = nntp_post(session->nntp_sock, fp);
-       if (ok != NN_SUCCESS) {
-               log_warning(_("can't post article.\n"));
-               return -1;
-       }
+       ok = news_post_stream(folder, fp);
 
        fclose(fp);
 
-       statusbar_pop_all();
+       return ok;
+}
+
+gint news_post_stream(Folder *folder, FILE *fp)
+{
+       NNTPSession *session;
+       gint ok;
+
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
+       g_return_val_if_fail(fp != NULL, -1);
+
+       session = news_session_get(folder);
+       if (!session) return -1;
+
+       ok = nntp_post(session, fp);
+       if (ok != NN_SUCCESS) {
+               log_warning("can't post article.\n");
+               return -1;
+       }
 
        return 0;
 }
@@ -314,15 +533,15 @@ static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
 {
        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) {
-               log_warning(_("can't retrieve article %d\n"), num);
+       if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
+               log_warning("can't retrieve article %d\n", num);
                return -1;
        }
 
@@ -334,179 +553,43 @@ static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
        return news_get_article_cmd(session, "ARTICLE", num, filename);
 }
 
-static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
-{
-       return news_get_article_cmd(session, "HEAD", num, filename);
-}
-
 /**
  * news_select_group:
  * @session: Active NNTP session.
  * @group: Newsgroup name.
+ * @num: Estimated number of articles.
+ * @first: First article number.
+ * @last: Last article number.
  *
  * Select newsgroup @group with the GROUP command if it is not already
- * selected in @session.
+ * selected in @session, or article numbers need to be returned.
  *
  * Return value: NNTP result code.
  **/
-static gint news_select_group(NNTPSession *session, const gchar *group)
+static gint news_select_group(NNTPSession *session, const gchar *group,
+                             gint *num, gint *first, gint *last)
 {
        gint ok;
-       gint num, first, last;
-
-       if (session->group && g_strcasecmp(session->group, group) == 0)
-               return NN_SUCCESS;
+       gint num_, first_, last_;
+
+       if (!num || !first || !last) {
+               if (session->group && g_strcasecmp(session->group, group) == 0)
+                       return NN_SUCCESS;
+               num = &num_;
+               first = &first_;
+               last = &last_;
+       }
 
        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);
 
        return ok;
 }
 
-static GSList *news_get_uncached_articles(NNTPSession *session,
-                                         FolderItem *item, gint cache_last,
-                                         gint *rfirst, gint *rlast)
-{
-       gint ok;
-       gint num = 0, first = 0, last = 0, begin = 0, end = 0;
-       gchar buf[NNTPBUFSIZE];
-       GSList *newlist = NULL;
-       GSList *llast = NULL;
-       MsgInfo *msginfo;
-
-       if (rfirst) *rfirst = 0;
-       if (rlast)  *rlast  = 0;
-
-       g_return_val_if_fail(session != NULL, NULL);
-       g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(item->folder != NULL, NULL);
-       g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
-
-       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) {
-               log_warning(_("invalid article range: %d - %d\n"),
-                           first, last);
-               return NULL;
-       }
-       if (cache_last < first)
-               begin = first;
-       else if (last < cache_last)
-               begin = first;
-       else if (last == cache_last) {
-               debug_print(_("no new articles.\n"));
-               return NULL;
-       } else
-               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->nntp_sock, begin, end) != NN_SUCCESS) {
-               log_warning(_("can't get xover\n"));
-               return NULL;
-       }
-
-       for (;;) {
-               if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
-                       log_warning(_("error occurred while getting xover.\n"));
-                       return newlist;
-               }
-
-               if (buf[0] == '.' && buf[1] == '\r') break;
-
-               msginfo = news_parse_xover(buf);
-               if (!msginfo) {
-                       log_warning(_("invalid xover line: %s\n"), buf);
-                       continue;
-               }
-
-               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);
-               else {
-                       llast = g_slist_append(llast, msginfo);
-                       llast = llast->next;
-               }
-       }
-
-       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;
-}
-
 #define PARSE_ONE_PARAM(p, srcp) \
 { \
        p = strchr(srcp, '\t'); \
@@ -519,13 +602,12 @@ static MsgInfo *news_parse_xover(const gchar *xover_str)
 {
        MsgInfo *msginfo;
        gchar buf[NNTPBUFSIZE];
-       gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
+       gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
        gchar *p;
        gint num, size_int, line_int;
        gchar *xover_buf;
 
-       Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
-       strcpy(xover_buf, xover_str);
+       Xstrdup_a(xover_buf, xover_str, return NULL);
 
        PARSE_ONE_PARAM(subject, xover_buf);
        PARSE_ONE_PARAM(sender, subject);
@@ -534,6 +616,13 @@ static MsgInfo *news_parse_xover(const gchar *xover_str)
        PARSE_ONE_PARAM(ref, msgid);
        PARSE_ONE_PARAM(size, ref);
        PARSE_ONE_PARAM(line, size);
+       /*
+        * PARSE_ONE_PARAM(xref, line);
+        *
+         * if we parse extra headers we should first examine the
+        * LIST OVERVIEW.FMT response from the server. See
+        * RFC2980 for details
+        */
 
        tmp = strchr(line, '\t');
        if (!tmp) tmp = strchr(line, '\r');
@@ -545,7 +634,7 @@ static MsgInfo *news_parse_xover(const gchar *xover_str)
        line_int = atoi(line);
 
        /* set MsgInfo */
-       msginfo = g_new0(MsgInfo, 1);
+       msginfo = procmsg_msginfo_new();
        msginfo->msgnum = num;
        msginfo->size = size_int;
 
@@ -573,12 +662,20 @@ static MsgInfo *news_parse_xover(const gchar *xover_str)
                        msginfo->inreplyto = g_strdup(p);
        }
 
+       /*
+       msginfo->xref = g_strdup(xref);
+       p = msginfo->xref+strlen(msginfo->xref) - 1;
+       while (*p == '\r' || *p == '\n') {
+               *p = '\0';
+               p--;
+       }
+       */
+
        return msginfo;
 }
 
 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
 {
-       gchar buf[NNTPBUFSIZE];
        gchar *p;
        gchar *tmp;
        gint num;
@@ -601,159 +698,374 @@ static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
                return g_strdup(p);
 }
 
-static GSList *news_delete_old_article(GSList *alist, gint first)
+gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
 {
-       GSList *cur, *next;
-       MsgInfo *msginfo;
-       gchar *cache_file;
+       gchar * tmp;
+       FILE * tmpfp;
+       gchar buf[BUFFSIZE];
+
+       tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
+                             G_DIR_SEPARATOR, (gint)msginfo);
+       if (tmp == NULL)
+               return -1;
 
-       if (first < 2) return alist;
+       if ((tmpfp = fopen(tmp, "wb")) == NULL) {
+               FILE_OP_ERROR(tmp, "fopen");
+               return -1;
+       }
+       if (change_file_mode_rw(tmpfp, tmp) < 0) {
+               FILE_OP_ERROR(tmp, "chmod");
+               g_warning("can't change file mode\n");
+       }
+       
+       fprintf(tmpfp, "From: %s\r\n", msginfo->from);
+       fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
+       fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
+       fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
+       fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
+       fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
+       get_rfc822_date(buf, sizeof(buf));
+       fprintf(tmpfp, "Date: %s\r\n", buf);
+       fprintf(tmpfp, "\r\n");
+       fprintf(tmpfp, "removed with sylpheed\r\n");
 
-       for (cur = alist; cur != NULL; ) {
-               next = cur->next;
+       fclose(tmpfp);
 
-               msginfo = (MsgInfo *)cur->data;
-               if (msginfo && msginfo->msgnum < first) {
-                       debug_print(_("deleting article %d...\n"),
-                                   msginfo->msgnum);
+       news_post(folder, tmp);
+       remove(tmp);
 
-                       cache_file = procmsg_get_message_file_path(msginfo);
-                       if (is_file_exist(cache_file)) unlink(cache_file);
-                       g_free(cache_file);
+       g_free(tmp);
 
-                       procmsg_msginfo_free(msginfo);
-                       alist = g_slist_remove(alist, msginfo);
-               }
+       return 0;
+}
 
-               cur = next;
-       }
+static gchar *news_folder_get_path(Folder *folder)
+{
+       gchar *folder_path;
+
+        g_return_val_if_fail(folder->account != NULL, NULL);
+
+        folder_path = g_strconcat(get_news_cache_dir(),
+                                  G_DIR_SEPARATOR_S,
+                                  folder->account->nntp_server,
+                                  NULL);
+       return folder_path;
+}
 
-       return alist;
+gchar *news_item_get_path(Folder *folder, FolderItem *item)
+{
+       gchar *folder_path, *path;
+
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
+       folder_path = news_folder_get_path(folder);
+
+        g_return_val_if_fail(folder_path != NULL, NULL);
+        if (folder_path[0] == G_DIR_SEPARATOR) {
+                if (item->path)
+                        path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
+                                           item->path, NULL);
+                else
+                        path = g_strdup(folder_path);
+        } else {
+                if (item->path)
+                        path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                           folder_path, G_DIR_SEPARATOR_S,
+                                           item->path, NULL);
+                else
+                        path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
+                                           folder_path, NULL);
+        }
+        g_free(folder_path);
+
+       return path;
 }
 
-static void news_delete_all_article(FolderItem *item)
+gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
 {
-       DIR *dp;
-       struct dirent *d;
+       NNTPSession *session;
+       gint i, ok, num, first, last, nummsgs = 0;
        gchar *dir;
-       gchar *file;
 
-       dir = folder_item_get_path(item);
-       if (!is_dir_exist(dir))
-               make_dir_hier(dir);
+       g_return_val_if_fail(item != NULL, -1);
+       g_return_val_if_fail(item->folder != NULL, -1);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
 
-       if ((dp = opendir(dir)) == NULL) {
-               FILE_OP_ERROR(dir, "opendir");
-               g_free(dir);
-               return;
-       }
+       session = news_session_get(folder);
+       g_return_val_if_fail(session != NULL, -1);
 
-       debug_print(_("\tDeleting all cached articles... "));
+       *old_uids_valid = TRUE;
 
-       while ((d = readdir(dp)) != NULL) {
-               if (to_number(d->d_name) < 0) continue;
+       ok = news_select_group(session, item->path, &num, &first, &last);
+       if (ok != NN_SUCCESS) {
+               log_warning(_("can't set group: %s\n"), item->path);
+               return -1;
+       }
 
-               file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
+       if (num <= 0) {
+               remove_all_numbered_files(dir);
+               return 0;
+       }
 
-               if (is_file_exist(file)) {
-                       if (unlink(file) < 0)
-                               FILE_OP_ERROR(file, "unlink");
-               }
+       if(last < first) {
+               log_warning(_("invalid article range: %d - %d\n"),
+                           first, last);
+               return 0;
+       }
 
-               g_free(file);
+       for(i = first; i <= last; i++) {
+               *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
+               nummsgs++;
        }
 
-       closedir(dp);
+       dir = folder_item_get_path(item);
+       debug_print("removing old messages from %d to %d in %s\n", first, last, dir);
+       remove_numbered_files(dir, 1, first - 1);
        g_free(dir);
 
-       debug_print(_("done.\n"));
+       return nummsgs;
 }
 
-/*
-  news_get_group_list returns a strings list.
-  These strings are the names of the newsgroups of a server.
-  item is the FolderItem of the news server.
-  The names of the newsgroups are cached into a file so that
-  when the function is called again, there is no need to make
-  a request to the server.
- */
+#define READ_TO_LISTEND(hdr) \
+       while (!(buf[0] == '.' && buf[1] == '\r')) { \
+               if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
+                       log_warning(_("error occurred while getting %s.\n"), hdr); \
+                       return msginfo; \
+               } \
+       }
 
-GSList * news_get_group_list(FolderItem *item)
+MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
 {
-       gchar *path, *filename;
-       gint ok;
        NNTPSession *session;
-       GSList * group_list = NULL;
-       FILE * f;
+       MsgInfo *msginfo = NULL;
        gchar buf[NNTPBUFSIZE];
-       int len;
 
-       if (item == NULL)
-         return NULL;
+       session = news_session_get(folder);
+       g_return_val_if_fail(session != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
+       g_return_val_if_fail(item->folder != NULL, NULL);
+       g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
 
-       path = folder_item_get_path(item);
+       log_message(_("getting xover %d in %s...\n"),
+                   num, item->path);
+       if (nntp_xover(session, num, num) != NN_SUCCESS) {
+               log_warning(_("can't get xover\n"));
+               return NULL;
+       }
+       
+       if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+               log_warning(_("error occurred while getting xover.\n"));
+               return NULL;
+       }
+       
+       msginfo = news_parse_xover(buf);
+       if (!msginfo) {
+               log_warning(_("invalid xover line: %s\n"), buf);
+       }
 
-       if (!is_dir_exist(path))
-               make_dir_hier(path);
+       READ_TO_LISTEND("xover");
 
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
-       g_free(path);
+       if(!msginfo)
+               return NULL;
+       
+       msginfo->folder = item;
+       msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
+       msginfo->flags.tmp_flags = MSG_NEWS;
+       msginfo->newsgroups = g_strdup(item->path);
 
-       session = news_session_get(item->folder);
+       if (nntp_xhdr(session, "to", num, num) != NN_SUCCESS) {
+               log_warning(_("can't get xhdr\n"));
+               return msginfo;
+       }
 
-       if (session == NULL)
-         return NULL;
+       if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+               log_warning(_("error occurred while getting xhdr.\n"));
+               return msginfo;
+       }
 
-       if (is_file_exist(filename)) {
-               debug_print(_("group list has been already cached.\n"));
+       msginfo->to = news_parse_xhdr(buf, msginfo);
+
+       READ_TO_LISTEND("xhdr (to)");
+
+       if (nntp_xhdr(session, "cc", num, num) != NN_SUCCESS) {
+               log_warning(_("can't get xhdr\n"));
+               return msginfo;
        }
-       else {
-           ok = nntp_list(session->nntp_sock);
-           if (ok != NN_SUCCESS)
-             return NULL;
-           
-           if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
-             log_warning(_("can't retrieve group list\n"));
-             return NULL;
-           }
+
+       if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+               log_warning(_("error occurred while getting xhdr.\n"));
+               return msginfo;
        }
 
-       f = fopen(filename, "r");
-       while (fgets(buf, NNTPBUFSIZE, f)) {
-           char * s;
+       msginfo->cc = news_parse_xhdr(buf, msginfo);
+
+       READ_TO_LISTEND("xhdr (cc)");
+
+       return msginfo;
+}
+
+static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
+{
+       gchar buf[NNTPBUFSIZE];
+       GSList *newlist = NULL;
+       GSList *llast = NULL;
+       MsgInfo *msginfo;
+       guint count = 0, lines = (end - begin + 2) * 3;
 
-           len = 0;
-           while ((buf[len] != 0) && (buf[len] != ' '))
-             len++;
-           buf[len] = 0;
-           s = g_strdup(buf);
+       g_return_val_if_fail(session != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
 
-           group_list = g_slist_append(group_list, s);
+       log_message(_("getting xover %d - %d in %s...\n"),
+                   begin, end, item->path);
+       if (nntp_xover(session, begin, end) != NN_SUCCESS) {
+               log_warning(_("can't get xover\n"));
+               return NULL;
        }
-       fclose(f);
-       g_free(filename);
 
-       group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
+       for (;;) {
+               if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+                       log_warning(_("error occurred while getting xover.\n"));
+                       return newlist;
+               }
+               count++;
+               progressindicator_set_percentage
+                       (PROGRESS_TYPE_NETWORK,
+                        session->fetch_base_percentage +
+                        (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
 
-       return group_list;
-}
+               if (buf[0] == '.' && buf[1] == '\r') break;
 
-/*
-  remove the cache file of the names of the newsgroups.
- */
+               msginfo = news_parse_xover(buf);
+               if (!msginfo) {
+                       log_warning(_("invalid xover line: %s\n"), buf);
+                       continue;
+               }
+
+               msginfo->folder = item;
+               msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
+               msginfo->flags.tmp_flags = MSG_NEWS;
+               msginfo->newsgroups = g_strdup(item->path);
+
+               if (!newlist)
+                       llast = newlist = g_slist_append(newlist, msginfo);
+               else {
+                       llast = g_slist_append(llast, msginfo);
+                       llast = llast->next;
+               }
+       }
+
+       if (nntp_xhdr(session, "to", begin, end) != NN_SUCCESS) {
+               log_warning(_("can't get xhdr\n"));
+               return newlist;
+       }
+
+       llast = newlist;
 
-void news_reset_group_list(FolderItem *item)
+       for (;;) {
+               if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
+                       log_warning(_("error occurred while getting xhdr.\n"));
+                       return newlist;
+               }
+               count++;
+               progressindicator_set_percentage
+                       (PROGRESS_TYPE_NETWORK,
+                        session->fetch_base_percentage +
+                        (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
+
+               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, "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;
+               }
+               count++;
+               progressindicator_set_percentage
+                       (PROGRESS_TYPE_NETWORK,
+                        session->fetch_base_percentage +
+                        (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
+
+               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;
+}
+
+GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
 {
-       gchar *path, *filename;
+       NNTPSession *session;
+       GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
+       guint first, last, next;
+       guint tofetch, fetched;
+       
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
+       g_return_val_if_fail(msgnum_list != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
+       
+       session = news_session_get(folder);
+       g_return_val_if_fail(session != NULL, NULL);
 
-       debug_print(_("\tDeleting cached group list... "));
-       path = folder_item_get_path(item);
-       if (!is_dir_exist(path))
-               make_dir_hier(path);
+       tmp_msgnum_list = g_slist_copy(msgnum_list);
+       tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
+
+       progressindicator_start(PROGRESS_TYPE_NETWORK);
+       tofetch = g_slist_length(tmp_msgnum_list);
+       fetched = 0;
+
+       first = GPOINTER_TO_INT(tmp_msgnum_list->data);
+       last = first;
+       for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
+               next = GPOINTER_TO_INT(elem->data);
+               if(next != (last + 1)) {
+                       session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
+                       session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
+                       tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
+                       msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
+                       fetched = last - first + 1;
+                       first = next;
+               }
+               last = next;
+       }
+       session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
+       session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
+       tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
+       msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
 
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
-       g_free(path);
-       if (remove(filename) != 0)
-         log_warning(_("can't delete cached group list %s\n"), filename);
-       g_free(filename);
+       g_slist_free(tmp_msgnum_list);
+       
+       progressindicator_stop(PROGRESS_TYPE_NETWORK);
+
+       return msginfo_list;
+}
+
+gboolean news_scan_required(Folder *folder, FolderItem *item)
+{
+       return TRUE;
 }