*** empty log message ***
[gnus] / lisp / gnus-dup.el
1 ;;; gnus-dup.el --- suppression of duplicate articles in Gnus
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This package tries to mark articles as read the second time the
27 ;; user reads a copy.  This is useful if the server doesn't support
28 ;; Xref properly, or if the user reads the same group from several
29 ;; servers.
30
31 ;;; Code:
32
33 (require 'gnus)
34 (require 'gnus-art)
35
36 (defgroup gnus-duplicate nil
37   "Suppression of duplicate articles."
38   :group 'gnus)
39
40 (defcustom gnus-save-duplicate-list nil
41   "*If non-nil, save the duplicate list when shutting down Gnus.
42 If nil, duplicate suppression will only work on duplicates
43 seen in the same session."
44   :group 'gnus-duplicate
45   :type 'boolean)
46
47 (defcustom gnus-duplicate-list-length 10000
48   "*The number of Message-IDs to keep in the duplicate suppression list."
49   :group 'gnus-duplicate
50   :type 'integer)
51
52 (defcustom gnus-duplicate-file (nnheader-concat gnus-directory "suppression")
53   "*The name of the file to store the duplicate suppression list."
54   :group 'gnus-duplicate
55   :type 'file)
56
57 ;;; Internal variables
58
59 (defvar gnus-dup-list nil)
60 (defvar gnus-dup-hashtb nil)
61
62 (defvar gnus-dup-list-dirty nil)
63
64 ;;;
65 ;;; Starting and stopping
66 ;;;
67
68 (gnus-add-shutdown 'gnus-dup-close 'gnus)
69
70 (defun gnus-dup-close ()
71   "Possibly save the duplicate suppression list and shut down the subsystem."
72   (gnus-dup-save)
73   (setq gnus-dup-list nil
74         gnus-dup-hashtb nil
75         gnus-dup-list-dirty nil))
76
77 (defun gnus-dup-open ()
78   "Possibly read the duplicate suppression list and start the subsystem."
79   (if gnus-save-duplicate-list
80       (gnus-dup-read)
81     (setq gnus-dup-list nil))
82   (setq gnus-dup-hashtb (gnus-make-hashtable gnus-duplicate-list-length))
83   ;; Enter all Message-IDs into the hash table.
84   (let ((list gnus-dup-list)
85         (obarray gnus-dup-hashtb))
86     (while list
87       (intern (pop list)))))
88
89 (defun gnus-dup-read ()
90   "Read the duplicate suppression list."
91   (setq gnus-dup-list nil)
92   (when (file-exists-p gnus-duplicate-file)
93     (load gnus-duplicate-file t t t)))
94
95 (defun gnus-dup-save ()
96   "Save the duplicate suppression list."
97   (when (and gnus-save-duplicate-list
98              gnus-dup-list-dirty)
99     (nnheader-temp-write gnus-duplicate-file
100       (gnus-prin1 `(setq gnus-dup-list ',gnus-dup-list))))
101   (setq gnus-dup-list-dirty nil))
102
103 ;;;
104 ;;; Interface functions
105 ;;;
106
107 (defun gnus-dup-enter-articles ()
108   "Enter articles from the current group for future duplicate suppression."
109   (unless gnus-dup-list
110     (gnus-dup-open))
111   (setq gnus-dup-list-dirty t)          ; mark list for saving
112   (let ((data gnus-newsgroup-data)
113         datum msgid)
114     ;; Enter the Message-IDs of all read articles into the list
115     ;; and hash table.
116     (while (setq datum (pop data))
117       (when (and (not (gnus-data-pseudo-p datum))
118                  (> (gnus-data-number datum) 0)
119                  (gnus-data-read-p datum)
120                  (not (= (gnus-data-mark datum) gnus-canceled-mark))
121                  (setq msgid (mail-header-id (gnus-data-header datum)))
122                  (not (nnheader-fake-message-id-p msgid))
123                  (not (intern-soft msgid gnus-dup-hashtb)))
124         (push msgid gnus-dup-list)
125         (intern msgid gnus-dup-hashtb))))
126   ;; Chop off excess Message-IDs from the list.
127   (let ((end (nthcdr gnus-duplicate-list-length gnus-dup-list)))
128     (when end
129       (setcdr end nil))))
130
131 (defun gnus-dup-suppress-articles ()
132   "Mark duplicate articles as read."
133   (unless gnus-dup-list
134     (gnus-dup-open))
135   (gnus-message 6 "Suppressing duplicates...")
136   (let ((headers gnus-newsgroup-headers)
137         number header)
138     (while (setq header (pop headers))
139       (when (and (intern-soft (mail-header-id header) gnus-dup-hashtb)
140                  (gnus-summary-article-unread-p (mail-header-number header)))
141         (setq gnus-newsgroup-unreads
142               (delq (setq number (mail-header-number header))
143                     gnus-newsgroup-unreads))
144         (push (cons number gnus-duplicate-mark)
145               gnus-newsgroup-reads))))
146   (gnus-message 6 "Suppressing duplicates...done"))
147
148 (defun gnus-dup-unsuppress-article (article)
149   "Stop suppression of ARTICLE."
150   (let ((id (mail-header-id (gnus-data-header (gnus-data-find article)))))
151     (when id
152       (setq gnus-dup-list-dirty t)
153       (setq gnus-dup-list (delete id gnus-dup-list))
154       (unintern id gnus-dup-hashtb))))
155
156 (provide 'gnus-dup)
157
158 ;;; gnus-dup.el ends here