sync with 0.7.6cvs15
[claws.git] / src / news.c
index 397852851347be512e8217ebdc641812dc289fd8..68dcf6226e422c4456137b0c94b785e7a0c6d347 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-2002 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 "alertpanel.h"
 
 #define NNTP_PORT      119
+#if USE_SSL
+#define NNTPS_PORT 563
+#endif
+
+static void news_folder_init            (Folder        *folder,
+                                         const gchar   *name,
+                                         const gchar   *path);
 
+#if USE_SSL
+static Session *news_session_new        (const gchar   *server,
+                                         gushort        port,
+                                         const gchar   *userid,
+                                         const gchar   *passwd, gboolean use_ssl);
+#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,
@@ -67,7 +81,10 @@ static gint news_get_header           (NNTPSession   *session,
                                          gchar         *filename);
 
 static gint news_select_group           (NNTPSession   *session,
-                                         const gchar   *group);
+                                         const gchar   *group,
+                                         gint          *num,
+                                         gint          *first,
+                                         gint          *last);
 static GSList *news_get_uncached_articles(NNTPSession  *session,
                                          FolderItem    *item,
                                          gint           cache_last,
@@ -81,11 +98,45 @@ static GSList *news_delete_old_articles      (GSList        *alist,
                                          gint           first);
 static void news_delete_all_articles    (FolderItem    *item);
 
-static void news_group_list_free(GSList * list);
+static gint news_remove_msg             (Folder        *folder, 
+                                         FolderItem    *item, 
+                                         gint           num);
+
+Folder *news_folder_new(const gchar *name, const gchar *path)
+{
+       Folder *folder;
+
+       folder = (Folder *)g_new0(NewsFolder, 1);
+       news_folder_init(folder, name, path);
+
+       return folder;
+}
+
+void news_folder_destroy(NewsFolder *folder)
+{
+       folder_remote_folder_destroy(REMOTE_FOLDER(folder));
+}
+
+static void news_folder_init(Folder *folder, const gchar *name,
+                            const gchar *path)
+{
+       folder_remote_folder_init(folder, name, path);
 
+       folder->type = F_NEWS;
 
+       folder->get_msg_list = news_get_article_list;
+       folder->fetch_msg    = news_fetch_msg;
+       folder->scan         = news_scan_group;
+       folder->remove_msg   = news_remove_msg;
+}
+
+#if USE_SSL
+static Session *news_session_new(const gchar *server, gushort port,
+                                const gchar *userid, const gchar *passwd, gboolean use_ssl)
+#else
 static Session *news_session_new(const gchar *server, gushort port,
                                 const gchar *userid, const gchar *passwd)
+#endif
 {
        gchar buf[NNTPBUFSIZE];
        NNTPSession *session;
@@ -96,9 +147,18 @@ static Session *news_session_new(const gchar *server, gushort port,
        log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
 
        if (userid && passwd)
+#if USE_SSL
+               nntp_sock = nntp_open_auth(server, port, buf, userid, passwd, use_ssl);
+#else
                nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
+#endif
        else
+#if USE_SSL
+               nntp_sock = nntp_open(server, port, buf, use_ssl);
+#else
                nntp_sock = nntp_open(server, port, buf);
+#endif
+
        if (nntp_sock == NULL)
                return NULL;
 
@@ -112,7 +172,6 @@ static Session *news_session_new(const gchar *server, gushort port,
        SESSION(session)->last_access_time = time(NULL);
        SESSION(session)->data             = NULL;
        session->group = NULL;
-       session->group_list = NULL;
 
        return SESSION(session);
 }
@@ -123,47 +182,49 @@ void news_session_destroy(NNTPSession *session)
        session->nntp_sock = NULL;
        SESSION(session)->sock = NULL;
 
-       news_group_list_free(session->group_list);
-       session->group_list = 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;
+       int port;
 
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
 
        ac = folder->account;
+
+#if USE_SSL
+       if (ac->set_nntpport) {
+                port = ac->nntpport;
+       } else {
+                port = ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
+       }
+#else
+       port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
+#endif
        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);
+                       passwd = input_dialog_query_password(ac->nntp_server,
+                                                            userid);
        }
 
+#if USE_SSL
        session = news_session_new(ac->nntp_server,
-                                  ac->set_nntpport ? ac->nntpport : NNTP_PORT,
+                                  port, 
+                                  userid, passwd, ac->ssl_nntp);
+#else
+       session = news_session_new(ac->nntp_server,
+                                  port, 
                                   userid, passwd);
+#endif
        g_free(passwd);
 
        return session;
@@ -171,40 +232,39 @@ static Session *news_session_new_for_folder(Folder *folder)
 
 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->account != NULL, NULL);
 
-       if (!REMOTE_FOLDER(folder)->session) {
-               REMOTE_FOLDER(folder)->session =
-                       news_session_new_for_folder(folder);
+       if (!rfolder->session) {
+               rfolder->session = news_session_new_for_folder(folder);
                statusbar_pop_all();
-               return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
+               return NNTP_SESSION(rfolder->session);
        }
 
-       session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
-
-       if (time(NULL) - SESSION(session)->last_access_time < SESSION_TIMEOUT) {
-               SESSION(session)->last_access_time = time(NULL);
+       if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
+               rfolder->session->last_access_time = time(NULL);
                statusbar_pop_all();
-               return session;
+               return NNTP_SESSION(rfolder->session);
        }
 
-       if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
+       if (nntp_mode(NNTP_SESSION(rfolder->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);
+               session_destroy(rfolder->session);
+               rfolder->session = news_session_new_for_folder(folder);
        }
 
+       if (rfolder->session)
+               rfolder->session->last_access_time = time(NULL);
        statusbar_pop_all();
-       return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
+       return NNTP_SESSION(rfolder->session);
 }
 
 GSList *news_get_article_list(Folder *folder, FolderItem *item,
@@ -278,7 +338,7 @@ gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
                return NULL;
        }
 
-       ok = news_select_group(session, item->path);
+       ok = news_select_group(session, item->path, NULL, NULL, NULL);
        statusbar_pop_all();
        if (ok != NN_SUCCESS) {
                g_warning(_("can't select group %s\n"), item->path);
@@ -299,43 +359,92 @@ gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
        return filename;
 }
 
-void news_scan_group(Folder *folder, FolderItem *item)
+gint news_scan_group(Folder *folder, FolderItem *item)
 {
-}
+       NNTPSession *session;
+       gint num = 0, first = 0, last = 0;
+       gint new = 0, unread = 0, total = 0;
+       gint min = 0, max = 0;
+       gchar *path;
+       gint ok;
 
-static struct NNTPGroupInfo * group_info_new(gchar * name,
-                                            gint first, gint last,
-                                            gchar type)
-{
-       struct NNTPGroupInfo * info;
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(item != NULL, -1);
 
-       info = g_new(struct NNTPGroupInfo, 1);
-       if (info == NULL)
-               return NULL;
-       info->name = g_strdup(name);
-       info->first = first;
-       info->last = last;
-       info->type = type;
+       session = news_session_get(folder);
+       if (!session) return -1;
+
+       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;
+       }
+
+       if (num == 0) {
+               item->new = item->unread = item->total = item->last_num = 0;
+               return 0;
+       }
+
+       path = folder_item_get_path(item);
+       if (path && is_dir_exist(path)) {
+               procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
+                                    first);
+       }
+       g_free(path);
+
+       if (max < first || last < min)
+               new = unread = total = num;
+       else {
+               if (min < first)
+                       min = first;
+               else if (first < min) {
+                       num -= min - first;
+                       first = min;
+               }
+
+               if (last < max)
+                       max = last;
+               else if (max < last) {
+                       new += last - max;
+                       unread += last - max;
+               }
+
+               if (new > num) new = num;
+               if (unread > num) unread = num;
+       }
 
-       return info;
+       item->new = new;
+       item->unread = unread;
+       item->total = num;
+       item->last_num = last;
+
+       return 0;
 }
 
-static void group_info_free(struct NNTPGroupInfo * info)
+static NewsGroupInfo *news_group_info_new(const gchar *name,
+                                         gint first, gint last, gchar type)
 {
-  g_free(info->name);
-  g_free(info);
+       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_list_free(GSList * list)
+static void news_group_info_free(NewsGroupInfo *ginfo)
 {
-  g_slist_foreach(list, (GFunc) group_info_free, NULL);
-  g_slist_free(list);
+       g_free(ginfo->name);
+       g_free(ginfo);
 }
 
-gint news_group_info_compare(struct NNTPGroupInfo * info1,
-                            struct NNTPGroupInfo * info2)
+static gint news_group_info_compare(NewsGroupInfo *ginfo1,
+                                   NewsGroupInfo *ginfo2)
 {
-  return g_strcasecmp(info1->name, info2->name);
+       return g_strcasecmp(ginfo1->name, ginfo2->name);
 }
 
 GSList *news_get_group_list(Folder *folder)
@@ -345,7 +454,6 @@ GSList *news_get_group_list(Folder *folder)
        GSList *list = NULL;
        GSList *last = NULL;
        gchar buf[NNTPBUFSIZE];
-       NNTPSession *session;
 
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(folder->type == F_NEWS, NULL);
@@ -356,18 +464,15 @@ GSList *news_get_group_list(Folder *folder)
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
        g_free(path);
 
-       session = news_session_get(folder);
-       if (!session) {
-               g_free(filename);
-               return NULL;
-       }
+       if ((fp = fopen(filename, "rb")) == NULL) {
+               NNTPSession *session;
 
-       if (session->group_list) {
-               g_free(filename);
-               return session->group_list;
-       }
+               session = news_session_get(folder);
+               if (!session) {
+                       g_free(filename);
+                       return NULL;
+               }
 
-       if ((fp = fopen(filename, "r")) == NULL) {
                if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
                        g_free(filename);
                        statusbar_pop_all();
@@ -376,11 +481,13 @@ GSList *news_get_group_list(Folder *folder)
                statusbar_pop_all();
                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, "r")) == NULL) {
+               if ((fp = fopen(filename, "rb")) == NULL) {
                        FILE_OP_ERROR(filename, "fopen");
                        g_free(filename);
                        return NULL;
@@ -388,48 +495,28 @@ GSList *news_get_group_list(Folder *folder)
        }
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
-               gchar * p;
-               gchar * cur;
-               gchar * name;
-               gint last_article;
-               gint first_article;
+               gchar *p = buf;
+               gchar *name;
+               gint last_num;
+               gint first_num;
                gchar type;
-               struct NNTPGroupInfo * info;
+               NewsGroupInfo *ginfo;
 
-               cur = buf;
-               p = strchr(cur, ' ');
-               if (p == NULL)
-                       continue;
-               * p = 0;
-
-               name = cur;
-               
-               cur = p + 1;
-               p = strchr(cur, ' ');
-               if (p == NULL)
-                       continue;
-               * p = 0;
-               last_article = atoi(cur);
+               p = strchr(p, ' ');
+               if (!p) continue;
+               *p = '\0';
+               p++;
+               name = buf;
 
-               cur = p + 1;
-               p = strchr(cur, ' ');
-               if (p == NULL)
+               if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
                        continue;
-               * p = 0;
-               first_article = atoi(cur);
-
-               cur = p + 1;
-               type = * cur;
 
-               info = group_info_new(name, first_article,
-                                     last_article, type);
-               if (info == NULL)
-                       continue;
+               ginfo = news_group_info_new(name, first_num, last_num, type);
 
                if (!last)
-                       last = list = g_slist_append(NULL, info);
+                       last = list = g_slist_append(NULL, ginfo);
                else {
-                       last = g_slist_append(last, info);
+                       last = g_slist_append(last, ginfo);
                        last = last->next;
                }
        }
@@ -437,30 +524,31 @@ GSList *news_get_group_list(Folder *folder)
        fclose(fp);
        g_free(filename);
 
-       list = g_slist_sort(list, (GCompareFunc) news_group_info_compare);
-
-       session->group_list = list;
+       list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
 
        statusbar_pop_all();
 
        return list;
 }
 
-void news_cancel_group_list_cache(Folder *folder)
+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;
-       NNTPSession *session;
 
        g_return_if_fail(folder != NULL);
        g_return_if_fail(folder->type == F_NEWS);
 
-       session = news_session_get(folder);
-       if (!session)
-               return;
-
-       news_group_list_free(session->group_list);
-       session->group_list = 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);
@@ -485,7 +573,7 @@ gint news_post(Folder *folder, const gchar *file)
        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;
        }
@@ -523,6 +611,28 @@ static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
        return 0;
 }
 
+static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
+{
+       MsgInfo * msginfo;
+       gchar * filename;
+       MsgFlags msgflags = { 0, 0 };
+       gint r;
+
+       filename = folder_item_fetch_msg(item, num);
+       if (filename == NULL)
+               return -1;
+
+       msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
+       if (msginfo == NULL)
+               return -1;
+
+       r = news_cancel_article(folder, msginfo);
+
+       procmsg_msginfo_free(msginfo);
+
+       return r;
+}
+
 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
 {
        return news_get_article_cmd(session, "ARTICLE", num, filename);
@@ -537,24 +647,33 @@ static gint news_get_header(NNTPSession *session, gint num, gchar *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->nntp_sock, group, num, first, last);
        if (ok == NN_SUCCESS)
                session->group = g_strdup(group);
 
@@ -580,16 +699,11 @@ static GSList *news_get_uncached_articles(NNTPSession *session,
        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);
+       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 NULL;
        }
-       session->group = g_strdup(item->path);
 
        /* calculate getting overview range */
        if (first > last) {
@@ -714,7 +828,7 @@ 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;
@@ -728,8 +842,9 @@ 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);
 
-       tmp = strchr(line, '\t');
+       tmp = strchr(xref, '\t');
        if (!tmp) tmp = strchr(line, '\r');
        if (!tmp) tmp = strchr(line, '\n');
        if (tmp) *tmp = '\0';
@@ -767,6 +882,13 @@ 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;
 }
 
@@ -844,3 +966,44 @@ static void news_delete_all_articles(FolderItem *item)
 
        debug_print(_("done.\n"));
 }
+
+gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
+{
+       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 ((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");
+
+       fclose(tmpfp);
+
+       news_post(folder, tmp);
+       remove(tmp);
+
+       g_free(tmp);
+
+       return 0;
+}