* pop3.el (pop3-md5): Treat a given string as binary.
[elisp/gnus.git-] / lisp / nnkiboze.el
1 ;;; nnkiboze.el --- select virtual news access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; The other access methods (nntp, nnspool, etc) are general news
29 ;; access methods.  This module relies on Gnus and can't be used
30 ;; separately.
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'gnus-clfns))
36
37 (require 'nntp)
38 (require 'nnheader)
39 (require 'gnus)
40 (require 'gnus-score)
41 (require 'nnoo)
42
43 (nnoo-declare nnkiboze)
44 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
45   "nnkiboze will put its files in this directory.")
46
47 (defvoo nnkiboze-level 9
48   "The maximum level to be searched for articles.")
49
50 (defvoo nnkiboze-remove-read-articles t
51   "If non-nil, nnkiboze will remove read articles from the kiboze group.")
52
53 (defvoo nnkiboze-ephemeral nil
54   "If non-nil, don't store any data anywhere.")
55
56 (defvoo nnkiboze-scores nil
57   "Score rules for generating the nnkiboze group.")
58
59 (defvoo nnkiboze-regexp nil
60   "Regexp for matching component groups.")
61
62 (defvoo nnkiboze-file-coding-system nnheader-text-coding-system
63   "Coding system for nnkiboze files.")
64
65 \f
66
67 (defconst nnkiboze-version "nnkiboze 1.0")
68
69 (defvoo nnkiboze-current-group nil)
70 (defvoo nnkiboze-status-string "")
71
72 (defvoo nnkiboze-headers nil)
73
74 \f
75
76 ;;; Interface functions.
77
78 (nnoo-define-basics nnkiboze)
79
80 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
81   (nnkiboze-possibly-change-group group)
82   (unless gnus-nov-is-evil
83     (if (stringp (car articles))
84         'headers
85       (let ((nov (nnkiboze-nov-file-name)))
86         (when (file-exists-p nov)
87           (save-excursion
88             (set-buffer nntp-server-buffer)
89             (erase-buffer)
90             (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
91               (nnheader-insert-file-contents nov))
92             (nnheader-nov-delete-outside-range
93              (car articles) (car (last articles)))
94             'nov))))))
95
96 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
97   (nnkiboze-possibly-change-group newsgroup)
98   (if (not (numberp article))
99       ;; This is a real kludge.  It might not work at times, but it
100       ;; does no harm I think.  The only alternative is to offer no
101       ;; article fetching by message-id at all.
102       (nntp-request-article article newsgroup gnus-nntp-server buffer)
103     (let* ((header (gnus-summary-article-header article))
104            (xref (mail-header-xref header))
105            num group)
106       (unless xref
107         (error "nnkiboze: No xref"))
108       (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
109         (error "nnkiboze: Malformed xref"))
110       (setq num (string-to-int (match-string 2 xref))
111             group (match-string 1 xref))
112       (or (with-current-buffer buffer
113             (or (gnus-cache-request-article num group)
114                 (gnus-agent-request-article num group)))
115           (gnus-request-article num group buffer)))))
116
117 (deffoo nnkiboze-request-scan (&optional group server)
118   (nnkiboze-generate-group (concat "nnkiboze:" group)))
119
120 (deffoo nnkiboze-request-group (group &optional server dont-check)
121   "Make GROUP the current newsgroup."
122   (nnkiboze-possibly-change-group group)
123   (if dont-check
124       t
125     (let ((nov-file (nnkiboze-nov-file-name))
126           beg end total)
127       (save-excursion
128         (set-buffer nntp-server-buffer)
129         (erase-buffer)
130         (unless (file-exists-p nov-file)
131           (nnkiboze-request-scan group))
132         (if (not (file-exists-p nov-file))
133             (nnheader-report 'nnkiboze "Can't select group %s" group)
134           (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
135             (nnheader-insert-file-contents nov-file))
136           (if (zerop (buffer-size))
137               (nnheader-insert "211 0 0 0 %s\n" group)
138             (goto-char (point-min))
139             (when (looking-at "[0-9]+")
140               (setq beg (read (current-buffer))))
141             (goto-char (point-max))
142             (when (re-search-backward "^[0-9]" nil t)
143               (setq end (read (current-buffer))))
144             (setq total (count-lines (point-min) (point-max)))
145             (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
146
147 (deffoo nnkiboze-close-group (group &optional server)
148   (nnkiboze-possibly-change-group group)
149   ;; Remove NOV lines of articles that are marked as read.
150   (when (and (file-exists-p (nnkiboze-nov-file-name))
151              nnkiboze-remove-read-articles)
152     (let ((coding-system-for-write nnkiboze-file-coding-system)
153           (output-coding-system nnkiboze-file-coding-system))
154       (with-temp-file (nnkiboze-nov-file-name)
155         (let ((cur (current-buffer))
156               (nnheader-file-coding-system nnkiboze-file-coding-system))
157           (nnheader-insert-file-contents (nnkiboze-nov-file-name))
158           (goto-char (point-min))
159           (while (not (eobp))
160             (if (not (gnus-article-read-p (read cur)))
161                 (forward-line 1)
162               (gnus-delete-line))))))
163     (setq nnkiboze-current-group nil)))
164
165 (deffoo nnkiboze-open-server (server &optional defs)
166   (unless (assq 'nnkiboze-regexp defs)
167     (push `(nnkiboze-regexp ,server)
168           defs))
169   (nnoo-change-server 'nnkiboze server defs))
170
171 (deffoo nnkiboze-request-delete-group (group &optional force server)
172   (nnkiboze-possibly-change-group group)
173   (when force
174     (let ((files (nconc
175                   (nnkiboze-score-file group)
176                   (list (nnkiboze-nov-file-name)
177                         (nnkiboze-nov-file-name ".newsrc")))))
178       (while files
179         (and (file-exists-p (car files))
180              (file-writable-p (car files))
181              (delete-file (car files)))
182         (setq files (cdr files)))))
183   (setq nnkiboze-current-group nil)
184   t)
185
186 (nnoo-define-skeleton nnkiboze)
187
188 \f
189 ;;; Internal functions.
190
191 (defun nnkiboze-possibly-change-group (group)
192   (setq nnkiboze-current-group group))
193
194 (defun nnkiboze-prefixed-name (group)
195   (gnus-group-prefixed-name group '(nnkiboze "")))
196
197 ;;;###autoload
198 (defun nnkiboze-generate-groups ()
199   "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
200 Finds out what articles are to be part of the nnkiboze groups."
201   (interactive)
202   (let ((nnmail-spool-file nil)
203         (mail-sources nil)
204         (gnus-use-dribble-file nil)
205         (gnus-read-active-file t)
206         (gnus-expert-user t))
207     (gnus))
208   (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
209          (newsrc (cdr gnus-newsrc-alist))
210          gnus-newsrc-hashtb info)
211     (gnus-make-hashtable-from-newsrc-alist)
212     ;; We have copied all the newsrc alist info over to local copies
213     ;; so that we can mess all we want with these lists.
214     (while (setq info (pop newsrc))
215       (when (string-match "nnkiboze" (gnus-info-group info))
216         ;; For each kiboze group, we call this function to generate
217         ;; it.
218         (nnkiboze-generate-group (gnus-info-group info) t))))
219   (save-excursion
220     (set-buffer gnus-group-buffer)
221     (gnus-group-list-groups)))
222
223 (defun nnkiboze-score-file (group)
224   (list (expand-file-name
225          (concat (file-name-as-directory gnus-kill-files-directory)
226                  (nnheader-translate-file-chars
227                   (concat (nnkiboze-prefixed-name nnkiboze-current-group)
228                           "." gnus-score-file-suffix))))))
229
230 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
231   (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
232          (newsrc-file (concat nnkiboze-directory
233                               (nnheader-translate-file-chars
234                                (concat group ".newsrc"))))
235          (nov-file (concat nnkiboze-directory
236                            (nnheader-translate-file-chars
237                             (concat group ".nov"))))
238          method nnkiboze-newsrc gname newsrc active
239          ginfo lowest glevel orig-info nov-buffer
240          ;; Bind various things to nil to make group entry faster.
241          (gnus-expert-user t)
242          (gnus-large-newsgroup nil)
243          (gnus-score-find-score-files-function 'nnkiboze-score-file)
244          ;; Use only nnkiboze-score-file!
245          (gnus-score-use-all-scores nil)
246          (gnus-use-scoring t)
247          (gnus-verbose (min gnus-verbose 3))
248          gnus-select-group-hook gnus-summary-prepare-hook
249          gnus-thread-sort-functions gnus-show-threads
250          gnus-visual gnus-suppress-duplicates num-unread)
251     (unless info
252       (error "No such group: %s" group))
253     ;; Load the kiboze newsrc file for this group.
254     (when (file-exists-p newsrc-file)
255       (load newsrc-file))
256     (let ((coding-system-for-write nnkiboze-file-coding-system)
257           (output-coding-system nnkiboze-file-coding-system))
258       (gnus-make-directory (file-name-directory nov-file))
259       (with-temp-file nov-file
260         (when (file-exists-p nov-file)
261           (nnheader-insert-file-contents nov-file))
262         (setq nov-buffer (current-buffer))
263         ;; Go through the active hashtb and add new all groups that match the
264         ;; kiboze regexp.
265         (mapatoms
266          (lambda (group)
267            (and (string-match nnkiboze-regexp
268                               (setq gname (symbol-name group))) ; Match
269                 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
270                 (numberp (car (symbol-value group))) ; It is active
271                 (or (> nnkiboze-level 7)
272                     (and (setq glevel (nth 1 (nth 2 (gnus-gethash
273                                                      gname gnus-newsrc-hashtb))))
274                          (>= nnkiboze-level glevel)))
275                 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
276                 (push (cons gname (1- (car (symbol-value group))))
277                       nnkiboze-newsrc)))
278          gnus-active-hashtb)
279         ;; `newsrc' is set to the list of groups that possibly are
280         ;; component groups to this kiboze group.  This list has elements
281         ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
282         ;; number that has been kibozed in GROUP in this kiboze group.
283         (setq newsrc nnkiboze-newsrc)
284         (while newsrc
285           (if (not (setq active (gnus-gethash
286                                  (caar newsrc) gnus-active-hashtb)))
287               ;; This group isn't active after all, so we remove it from
288               ;; the list of component groups.
289               (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
290             (setq lowest (cdar newsrc))
291             ;; Ok, we have a valid component group, so we jump to it.
292             (switch-to-buffer gnus-group-buffer)
293             (gnus-group-jump-to-group (caar newsrc))
294             (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
295             (setq ginfo (gnus-get-info (gnus-group-group-name))
296                   orig-info (gnus-copy-sequence ginfo)
297                   num-unread (car (gnus-gethash (caar newsrc)
298                                                 gnus-newsrc-hashtb)))
299             (unwind-protect
300                 (progn
301                   ;; We set all list of article marks to nil.  Since we operate
302                   ;; on copies of the real lists, we can destroy anything we
303                   ;; want here.
304                   (when (nth 3 ginfo)
305                     (setcar (nthcdr 3 ginfo) nil))
306                   ;; We set the list of read articles to be what we expect for
307                   ;; this kiboze group -- either nil or `(1 . LOWEST)'.
308                   (when ginfo
309                     (setcar (nthcdr 2 ginfo)
310                             (and (not (= lowest 1)) (cons 1 lowest))))
311                   (when (and (or (not ginfo)
312                                  (> (length (gnus-list-of-unread-articles
313                                              (car ginfo)))
314                                     0))
315                              (progn
316                                (ignore-errors
317                                  (gnus-group-select-group nil))
318                                (eq major-mode 'gnus-summary-mode)))
319                     ;; We are now in the group where we want to be.
320                     (setq method (gnus-find-method-for-group
321                                   gnus-newsgroup-name))
322                     (when (eq method gnus-select-method)
323                       (setq method nil))
324                     ;; We go through the list of scored articles.
325                     (while gnus-newsgroup-scored
326                       (when (> (caar gnus-newsgroup-scored) lowest)
327                         ;; If it has a good score, then we enter this article
328                         ;; into the kiboze group.
329                         (nnkiboze-enter-nov
330                          nov-buffer
331                          (gnus-summary-article-header
332                           (caar gnus-newsgroup-scored))
333                          gnus-newsgroup-name))
334                       (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
335                     ;; That's it.  We exit this group.
336                     (when (eq major-mode 'gnus-summary-mode)
337                       (kill-buffer (current-buffer)))))
338               ;; Restore the proper info.
339               (when ginfo
340                 (setcdr ginfo (cdr orig-info)))
341               (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
342                       num-unread)))
343           (setcdr (car newsrc) (car active))
344           (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
345           (setq newsrc (cdr newsrc)))))
346     ;; We save the kiboze newsrc for this group.
347     (gnus-make-directory (file-name-directory newsrc-file))
348     (with-temp-file newsrc-file
349       (insert "(setq nnkiboze-newsrc '")
350       (gnus-prin1 nnkiboze-newsrc)
351       (insert ")\n")))
352   (unless inhibit-list-groups
353     (save-excursion
354       (set-buffer gnus-group-buffer)
355       (gnus-group-list-groups)))
356   t)
357
358 (defun nnkiboze-enter-nov (buffer header group)
359   (save-excursion
360     (set-buffer buffer)
361     (goto-char (point-max))
362     (let ((prefix (gnus-group-real-prefix group))
363           (oheader (copy-sequence header))
364           article)
365       (if (zerop (forward-line -1))
366           (progn
367             (setq article (1+ (read (current-buffer))))
368             (forward-line 1))
369         (setq article 1))
370       (mail-header-set-number oheader article)
371       (with-temp-buffer
372         (insert (or (mail-header-xref oheader) ""))
373         (goto-char (point-min))
374         (if (re-search-forward " [^ ]+:[0-9]+" nil t)
375             (goto-char (match-beginning 0))
376           (or (eobp) (forward-char 1)))
377         ;; The first Xref has to be the group this article
378         ;; really came for - this is the article nnkiboze
379         ;; will request when it is asked for the article.
380         (insert " " group ":"
381                 (int-to-string (mail-header-number header)) " ")
382         (while (re-search-forward " [^ ]+:[0-9]+" nil t)
383           (goto-char (1+ (match-beginning 0)))
384           (insert prefix))
385         (mail-header-set-xref oheader (buffer-string)))
386       (nnheader-insert-nov oheader))))
387
388 (defun nnkiboze-nov-file-name (&optional suffix)
389   (concat (file-name-as-directory nnkiboze-directory)
390           (nnheader-translate-file-chars
391            (concat (nnkiboze-prefixed-name nnkiboze-current-group)
392                    (or suffix ".nov")))))
393
394 (provide 'nnkiboze)
395
396 ;;; nnkiboze.el ends here