sync with Hiroyuki's 0.5.0claws2-3
[claws.git] / src / news.c
index fcf7c9581e8d212c662813bd7582759549fa7a27..973909a7fadaf548aafdf5b88a9249e439e34f85 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * 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
@@ -21,6 +21,8 @@
 #  include "config.h"
 #endif
 
+#include "defs.h"
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -28,7 +30,6 @@
 #include <dirent.h>
 #include <unistd.h>
 
-#include "defs.h"
 #include "intl.h"
 #include "news.h"
 #include "nntp.h"
 #include "inputdialog.h"
 #include "alertpanel.h"
 
+#define NNTP_PORT      119
+
+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,
@@ -58,16 +66,19 @@ static gint news_get_header          (NNTPSession   *session,
                                          gchar         *filename);
 
 static gint news_select_group           (NNTPSession   *session,
-                                         const char    *group);
+                                         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 GSList *news_delete_old_article  (GSList        *alist,
+static gchar *news_parse_xhdr           (const gchar   *xhdr_str,
+                                         MsgInfo       *msginfo);
+static GSList *news_delete_old_articles         (GSList        *alist,
+                                         FolderItem    *item,
                                          gint           first);
-static void news_delete_all_article     (FolderItem    *item);
+static void news_delete_all_articles    (FolderItem    *item);
 
 
 static Session *news_session_new(const gchar *server, gushort port,
@@ -110,20 +121,16 @@ void news_session_destroy(NNTPSession *session)
        g_free(session->group);
 }
 
-static gchar *news_query_password(const gchar *server,
-                                 const gchar *user)
+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);
+       pass = input_dialog_with_invisible(_("Input password"), message, NULL);
        g_free(message);
-/*     manage_window_focus_in(inc_dialog->mainwin->window, */
-/*                            NULL, NULL); */
+
        return pass;
 }
 
@@ -131,24 +138,26 @@ static Session *news_session_new_for_folder(Folder *folder)
 {
        Session *session;
        PrefsAccount *ac;
-       const gchar *userid;
-       gchar *passwd;
+       const gchar *userid = NULL;
+       gchar *passwd = NULL;
+
+       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]) {
                userid = ac->userid;
                if (ac->passwd && ac->passwd[0])
                        passwd = g_strdup(ac->passwd);
-               else {
+               else
                        passwd = news_query_password(ac->nntp_server, userid);
-                       if (!passwd)
-                               userid = NULL;
-               }
-       } else {
-               userid = passwd = NULL;
        }
-       session = news_session_new(ac->nntp_server, 119, userid, passwd);
+
+       session = news_session_new(ac->nntp_server,
+                                  ac->set_nntpport ? ac->nntpport : NNTP_PORT,
+                                  userid, passwd);
        g_free(passwd);
+
        return session;
 }
 
@@ -163,18 +172,24 @@ NNTPSession *news_session_get(Folder *folder)
        if (!REMOTE_FOLDER(folder)->session) {
                REMOTE_FOLDER(folder)->session =
                        news_session_new_for_folder(folder);
-       } else {
-               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_for_folder(folder);
-               }
+               statusbar_pop_all();
+               return NNTP_SESSION(REMOTE_FOLDER(folder)->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,
+                           folder->account->set_nntpport ?
+                           folder->account->nntpport : NNTP_PORT);
+               session_destroy(REMOTE_FOLDER(folder)->session);
+               REMOTE_FOLDER(folder)->session =
+                       news_session_new_for_folder(folder);
        }
 
+       statusbar_pop_all();
        return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
 }
 
@@ -203,7 +218,7 @@ GSList *news_get_article_list(Folder *folder, FolderItem *item,
                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 = news_delete_old_articles(alist, item, first);
 
                alist = g_slist_concat(alist, newlist);
                item->last_num = last;
@@ -212,7 +227,7 @@ GSList *news_get_article_list(Folder *folder, FolderItem *item,
 
                alist = news_get_uncached_articles
                        (session, item, 0, NULL, &last);
-               news_delete_all_article(item);
+               news_delete_all_articles(item);
                item->last_num = last;
        }
 
@@ -226,12 +241,16 @@ GSList *news_get_article_list(Folder *folder, FolderItem *item,
 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);
        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);
 
@@ -240,13 +259,14 @@ gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint 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);
+       statusbar_pop_all();
        if (ok != NN_SUCCESS) {
                g_warning(_("can't select group %s\n"), item->path);
                g_free(filename);
@@ -270,6 +290,89 @@ void news_scan_group(Folder *folder, FolderItem *item)
 {
 }
 
+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->type == F_NEWS, NULL);
+
+       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 ((fp = fopen(filename, "r")) == NULL) {
+               NNTPSession *session;
+
+               session = news_session_get(folder);
+               if (!session) {
+                       g_free(filename);
+                       return NULL;
+               }
+
+               if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
+                       g_free(filename);
+                       statusbar_pop_all();
+                       return NULL;
+               }
+               statusbar_pop_all();
+               if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
+                       log_warning(_("can't retrieve newsgroup list\n"));
+                       g_free(filename);
+                       return NULL;
+               }
+
+               if ((fp = fopen(filename, "r")) == NULL) {
+                       FILE_OP_ERROR(filename, "fopen");
+                       g_free(filename);
+                       return NULL;
+               }
+       }
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               gchar *p = buf;
+               while (*p != '\0' && *p != ' ') p++;
+               *p = '\0';
+               if (!last)
+                       last = list = g_slist_append(NULL, g_strdup(buf));
+               else {
+                       last = g_slist_append(last, g_strdup(buf));
+                       last = last->next;
+               }
+       }
+
+       fclose(fp);
+       g_free(filename);
+
+       list = g_slist_sort(list, (GCompareFunc)g_strcasecmp);
+
+       statusbar_pop_all();
+
+       return list;
+}
+
+void news_remove_group_list(Folder *folder)
+{
+       gchar *path, *filename;
+
+       g_return_if_fail(folder != NULL);
+       g_return_if_fail(folder->type == F_NEWS);
+
+       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;
@@ -335,26 +438,24 @@ static gint news_get_header(NNTPSession *session, gint num, gchar *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 char *group)
+static gint news_select_group(NNTPSession *session, const gchar *group)
 {
        gint ok;
        gint num, first, last;
 
-       if (g_strcasecmp(session->group, group) == 0)
+       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);
 
@@ -389,7 +490,6 @@ static GSList *news_get_uncached_articles(NNTPSession *session,
                log_warning(_("can't set group: %s\n"), item->path);
                return NULL;
        }
-
        session->group = g_strdup(item->path);
 
        /* calculate getting overview range */
@@ -409,6 +509,9 @@ static GSList *news_get_uncached_articles(NNTPSession *session,
                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;
@@ -436,6 +539,7 @@ static GSList *news_get_uncached_articles(NNTPSession *session,
 
                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);
@@ -445,8 +549,56 @@ static GSList *news_get_uncached_articles(NNTPSession *session,
                }
        }
 
-       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;
 }
 
@@ -507,6 +659,7 @@ static MsgInfo *news_parse_xover(const gchar *xover_str)
        if (*msgid != '\0')
                msginfo->msgid = g_strdup(msgid);
 
+       msginfo->references = g_strdup(ref);
        eliminate_parenthesis(ref, '(', ')');
        if ((p = strrchr(ref, '<')) != NULL) {
                extract_parenthesis(p, '<', '>');
@@ -518,26 +671,54 @@ static MsgInfo *news_parse_xover(const gchar *xover_str)
        return msginfo;
 }
 
-static GSList *news_delete_old_article(GSList *alist, gint first)
+static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
+{
+       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_articles(GSList *alist, FolderItem *item,
+                                       gint first)
 {
        GSList *cur, *next;
        MsgInfo *msginfo;
-       gchar *cache_file;
+       gchar *dir;
+
+       g_return_val_if_fail(item != NULL, alist);
+       g_return_val_if_fail(item->folder != NULL, alist);
+       g_return_val_if_fail(item->folder->type == F_NEWS, alist);
 
        if (first < 2) return alist;
 
+       debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
+
+       dir = folder_item_get_path(item);
+       remove_numbered_files(dir, 1, first - 1);
+       g_free(dir);
+
        for (cur = alist; cur != NULL; ) {
                next = cur->next;
 
                msginfo = (MsgInfo *)cur->data;
                if (msginfo && msginfo->msgnum < first) {
-                       debug_print(_("deleting article %d...\n"),
-                                   msginfo->msgnum);
-
-                       cache_file = procmsg_get_message_file_path(msginfo);
-                       if (is_file_exist(cache_file)) unlink(cache_file);
-                       g_free(cache_file);
-
                        procmsg_msginfo_free(msginfo);
                        alist = g_slist_remove(alist, msginfo);
                }
@@ -548,123 +729,21 @@ static GSList *news_delete_old_article(GSList *alist, gint first)
        return alist;
 }
 
-static void news_delete_all_article(FolderItem *item)
+static void news_delete_all_articles(FolderItem *item)
 {
-       DIR *dp;
-       struct dirent *d;
        gchar *dir;
-       gchar *file;
 
-       dir = folder_item_get_path(item);
-       if ((dp = opendir(dir)) == NULL) {
-               FILE_OP_ERROR(dir, "opendir");
-               g_free(dir);
-               return;
-       }
+       g_return_if_fail(item != NULL);
+       g_return_if_fail(item->folder != NULL);
+       g_return_if_fail(item->folder->type == F_NEWS);
 
        debug_print(_("\tDeleting all cached articles... "));
 
-       while ((d = readdir(dp)) != NULL) {
-               if (to_number(d->d_name) < 0) continue;
-
-               file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
-
-               if (is_file_exist(file)) {
-                       if (unlink(file) < 0)
-                               FILE_OP_ERROR(file, "unlink");
-               }
-
-               g_free(file);
-       }
-
-       closedir(dp);
+       dir = folder_item_get_path(item);
+       if (!is_dir_exist(dir))
+               make_dir_hier(dir);
+       remove_all_numbered_files(dir);
        g_free(dir);
 
        debug_print(_("done.\n"));
 }
-
-/*
-  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.
- */
-
-GSList * news_get_group_list(FolderItem *item)
-{
-       gchar *path, *filename;
-       gint ok;
-       NNTPSession *session;
-       GSList * group_list = NULL;
-       FILE * f;
-       gchar buf[NNTPBUFSIZE];
-       int len;
-
-       if (item == NULL)
-         return NULL;
-
-       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);
-
-       session = news_session_get(item->folder);
-
-       if (session == NULL)
-         return NULL;
-
-       if (is_file_exist(filename)) {
-               debug_print(_("group list has been already cached.\n"));
-       }
-       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;
-           }
-       }
-
-       f = fopen(filename, "r");
-       while (fgets(buf, NNTPBUFSIZE, f)) {
-           char * s;
-
-           len = 0;
-           while ((buf[len] != 0) && (buf[len] != ' '))
-             len++;
-           buf[len] = 0;
-           s = g_strdup(buf);
-
-           group_list = g_slist_append(group_list, s);
-       }
-       fclose(f);
-       g_free(filename);
-
-       group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
-
-       return group_list;
-}
-
-/*
-  remove the cache file of the names of the newsgroups.
- */
-
-void news_reset_group_list(FolderItem *item)
-{
-       gchar *path, *filename;
-
-       debug_print(_("\tDeleting cached group list... "));
-       path = folder_item_get_path(item);
-       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);
-}