1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
31 "*Function used to find SCORE files.
32 The function will be called with the group name as the argument, and
33 should return a list of score files to apply to that group. The score
34 files do not actually have to exist.
36 Predefined values are:
38 gnus-score-find-single: Only apply the group's own SCORE file.
39 gnus-score-find-hierarchical: Also apply SCORE files from parent groups.
40 gnus-score-find-bnews: Apply SCORE files whose names matches.
42 See the documentation to these functions for more information.
44 This variable can also be a list of functions to be called. Each
45 function should either return a list of score files, or a list of
48 (defvar gnus-adaptive-file-suffix "ADAPT"
49 "*Suffix of the adaptive score files.")
51 (defvar gnus-score-expiry-days 7
52 "*Number of days before unused score file entries are expired.")
54 (defvar gnus-orphan-score nil
55 "*All orphans get this score added. Set in the score file.")
57 (defvar gnus-default-adaptive-score-alist
59 (gnus-ticked-mark (from 4))
60 (gnus-dormant-mark (from 5))
61 (gnus-del-mark (from -4) (subject -1))
62 (gnus-read-mark (from 4) (subject 2))
63 (gnus-expirable-mark (from -1) (subject -1))
64 (gnus-killed-mark (from -1) (subject -3))
66 (gnus-catchup-mark (from -1) (subject -1)))
67 "*Alist of marks and scores.")
69 (defvar gnus-score-mimic-keymap nil
70 "*Have the score entry functions pretend that they are a keymap.")
72 (defvar gnus-score-exact-adapt-limit nil
73 "*Number that says how long a match has to be before using substring matching.
74 When doing adaptive scoring, one normally uses substring matching.
75 However, if the header one matches is short, the possibility for false
76 positives is great, so if the length of the match is less than this
77 variable, exact matching will be used.
79 If this variable is nil, which it is by default, exact matching will
84 ;; Internal variables.
86 (defvar gnus-internal-global-score-files nil)
87 (defvar gnus-score-file-list nil)
88 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
90 (defvar gnus-score-trace nil)
92 (defvar gnus-score-alist nil
93 "Alist containing score information.
94 The keys can be symbols or strings. The following symbols are defined.
96 touched: If this alist has been modified.
97 mark: Automatically mark articles below this.
98 expunge: Automatically expunge articles below this.
99 files: List of other SCORE files to load when loading this one.
100 eval: Sexp to be evaluated when the score file is loaded.
102 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
103 where HEADER is the header being scored, MATCH is the string we are
104 looking for, TYPE is a flag indicating whether it should use regexp or
105 substring matching, SCORE is the score to add and DATE is the date
106 of the last succesful match.")
108 (defvar gnus-score-cache nil)
109 (defvar gnus-scores-articles nil)
110 (defvar gnus-scores-exclude-files nil)
111 (defvar gnus-header-index nil)
112 (defvar gnus-score-index nil)
114 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
116 ;;; Summary mode score maps.
118 (defvar gnus-summary-score-map nil)
120 (define-prefix-command 'gnus-summary-score-map)
121 (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
122 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
123 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
124 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
125 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
126 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
127 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
128 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
129 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
130 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
134 ;; Summary score file commands
136 ;; Much modification of the kill (ahem, score) code and lots of the
137 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
139 (defun gnus-summary-lower-score (score)
141 (gnus-summary-increase-score (- (gnus-score-default score))))
143 (defun gnus-summary-increase-score (score)
145 (let* ((nscore (gnus-score-default score))
146 (prefix (if (< nscore 0) ?L ?I))
147 (increase (> nscore 0))
153 (?i "message-id" nil t)
154 (?t "references" "message-id" t)
160 '((?e e) (?f f) (?s nil) (?r r) (?b before)
161 (?a at) (?n now) (?< <) (?> >) (?= =)))
163 (list (list ?t (current-time-string)) '(?p nil) '(?i now)))
164 (mimic gnus-score-mimic-keymap)
165 hchar entry temporary tchar pchar end type)
166 ;; First we read the header to score.
168 (message "%c-" prefix)
169 (message "%s header (%s): " (if increase "Increase" "Lower")
170 (mapconcat (lambda (s) (char-to-string (car s)))
172 (setq hchar (read-char))
173 (or (setq entry (assq (downcase hchar) char-to-header))
177 (if mimic (message "%c %c" prefix hchar) (message ""))))
178 (if (or end (/= (downcase hchar) hchar))
180 ;; This was a majuscle, so we end reading and set the defaults.
181 (if mimic (message "%c %c" prefix hchar) (message ""))
183 temporary (current-time-string)))
184 ;; We continue reading - the type.
186 (message "%c %c-" prefix hchar)
187 (message "%s header '%s' with match type (%s): "
188 (if increase "Increase" "Lower")
190 (mapconcat (lambda (s) (char-to-string (car s)))
192 (setq tchar (read-char))
193 (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
196 (if mimic (message "%c %c" prefix hchar) (message ""))
198 (if (or end (/= (downcase tchar) tchar))
200 ;; It was a majuscle, so we end reading and the the default.
201 (if mimic (message "%c %c %c" prefix hchar tchar)
203 (setq temporary (current-time-string)))
204 ;; We continue reading.
206 (message "%c %c %c-" prefix hchar tchar)
207 (message "%s permanence (%s): " (if increase "Increase" "Lower")
208 (mapconcat (lambda (s) (char-to-string (car s)))
210 (setq pchar (read-char))
211 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
213 (if (setq temporary (nth 1 (assq pchar char-to-perm)))
218 (message "%c %c %c %c" prefix hchar tchar pchar)
220 ;; We have all the data, so we enter this score.
223 (gnus-summary-score-entry
224 (nth 1 entry) ; Header
225 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))) ; Match
229 (not (nth 3 entry))) ; Prompt
232 (defun gnus-summary-header (header)
233 ;; Return HEADER for current articles, or error.
234 (let ((article (gnus-summary-article-number)))
236 (aref (gnus-get-header-by-number article)
237 (nth 1 (assoc header gnus-header-index)))
238 (error "No article on current line"))))
240 (defun gnus-summary-score-entry
241 (header match type score date &optional prompt silent)
242 "Enter score file entry.
243 HEADER is the header being scored.
244 MATCH is the string we are looking for.
245 TYPE is a flag indicating if it is a regexp or substring.
246 SCORE is the score to add.
247 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
248 If optional argument `PROMPT' is non-nil, allow user to edit match.
249 If optional argument `SILENT' is nil, show effect of score entry."
251 (list (completing-read "Header: "
253 (lambda (x) (fboundp (nth 2 x)))
255 (read-string "Match: ")
256 (if (y-or-n-p "Use regexp match? ") 'r 's)
257 (and current-prefix-arg
258 (prefix-numeric-value current-prefix-arg))
259 (cond ((not (y-or-n-p "Add to SCORE file? "))
261 ((y-or-n-p "Expire kill? ")
262 (current-time-string))
264 (let ((score (gnus-score-default score))
265 (header (downcase header)))
266 (and prompt (setq match (read-string
267 (format "Match %s on %s, %s: "
268 (cond ((eq date 'now)
274 (if (< score 0) "lower" "raise"))
276 (and (>= (nth 1 (assoc header gnus-header-index)) 0)
278 (gnus-summary-score-effect header match type score))
281 (and (= score gnus-score-interactive-default-score)
283 (let ((new (cond ((eq type 'f)
284 (list (gnus-simplify-subject-fuzzy match)
285 score (and date (gnus-day-number date)) type))
287 (list match score (and date (gnus-day-number date)) type))
289 (list match score (gnus-day-number date)))
294 (old (gnus-score-get header))
296 ;; We see whether we can collapse some score entries.
297 ;; This isn't quite correct, because there may be more elements
298 ;; later on with the same key that have matching elems... Hm.
300 (setq elem (assoc match old))
301 (eq (nth 3 elem) (nth 3 new))
302 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
303 (and (not (nth 2 elem)) (not (nth 2 new)))))
304 ;; Yup, we just add this new score to the old elem.
305 (setcar (cdr elem) (+ (or (nth 1 elem)
306 gnus-score-interactive-default-score)
308 gnus-score-interactive-default-score)))
309 ;; Nope, we have to add a new elem.
310 (gnus-score-set header (if old (cons new old) (list new)))))
311 (gnus-score-set 'touched '(t)))))
313 (defun gnus-summary-score-effect (header match type score)
314 "Simulate the effect of a score file entry.
315 HEADER is the header being scored.
316 MATCH is the string we are looking for.
317 TYPE is a flag indicating if it is a regexp or substring.
318 SCORE is the score to add."
319 (interactive (list (completing-read "Header: "
321 (lambda (x) (fboundp (nth 2 x)))
323 (read-string "Match: ")
324 (y-or-n-p "Use regexp match? ")
325 (prefix-numeric-value current-prefix-arg)))
327 (or (and (stringp match) (> (length match) 0))
329 (goto-char (point-min))
330 (let ((regexp (cond ((eq type 'f)
331 (gnus-simplify-subject-fuzzy match))
333 (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
335 (let ((content (gnus-summary-header header))
336 (case-fold-search t))
339 (string-equal (gnus-simplify-subject-fuzzy content)
341 (string-match regexp content))
342 (gnus-summary-raise-score score))))
343 (beginning-of-line 2)))))
345 (defun gnus-summary-score-crossposting (score date)
346 ;; Enter score file entry for current crossposting.
347 ;; SCORE is the score to add.
348 ;; DATE is the expire date.
349 (let ((xref (gnus-summary-header "xref"))
352 (or xref (error "This article is not crossposted"))
353 (while (string-match " \\([^ \t]+\\):" xref start)
354 (setq start (match-end 0))
357 (substring xref (match-beginning 1) (match-end 1)))
358 gnus-newsgroup-name))
359 (gnus-summary-score-entry
360 "xref" (concat " " group ":") nil score date t)))))
367 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
369 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
370 (defun gnus-score-set-mark-below (score)
371 "Automatically mark articles with score below SCORE as read."
373 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
374 (string-to-int (read-string "Mark below: ")))))
375 (setq score (or score gnus-summary-default-score 0))
376 (gnus-score-set 'mark (list score))
377 (gnus-score-set 'touched '(t))
378 (setq gnus-summary-mark-below score)
379 (gnus-summary-update-lines))
381 (defun gnus-score-set-expunge-below (score)
382 "Automatically expunge articles with score below SCORE."
384 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
385 (string-to-int (read-string "Expunge below: ")))))
386 (setq score (or score gnus-summary-default-score 0))
387 (gnus-score-set 'expunge (list score))
388 (gnus-score-set 'touched '(t)))
390 (defun gnus-score-set (symbol value &optional alist)
391 ;; Set SYMBOL to VALUE in ALIST.
396 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
398 (entry (assoc symbol alist)))
399 (cond ((gnus-score-get 'read-only alist)
400 ;; This is a read-only score file, so we do nothing.
403 (setcdr entry value))
405 (error "Empty alist"))
408 (cons (cons symbol value) (cdr alist)))))))
410 (defun gnus-score-get (symbol &optional alist)
411 ;; Get SYMBOL's definition in ALIST.
417 (gnus-score-file-name gnus-newsgroup-name))
418 gnus-score-alist)))))
420 (defun gnus-score-change-score-file (file)
421 "Change current score alist."
422 (interactive (list (completing-read "Score file: " gnus-score-cache)))
423 (gnus-score-load-file file)
424 (gnus-set-mode-line 'summary))
426 (defun gnus-score-edit-alist (file)
427 "Edit the current score alist."
428 (interactive (list gnus-current-score-file))
429 (let ((winconf (current-window-configuration)))
430 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
431 (gnus-configure-windows 'article)
432 (pop-to-buffer (find-file-noselect file))
434 (make-local-variable 'gnus-prev-winconf)
435 (setq gnus-prev-winconf winconf))
437 4 (substitute-command-keys
438 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
440 (defun gnus-score-edit-file (file)
443 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
444 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
445 (let ((winconf (current-window-configuration)))
446 (gnus-configure-windows 'article)
447 (pop-to-buffer (find-file-noselect file))
449 (make-local-variable 'gnus-prev-winconf)
450 (setq gnus-prev-winconf winconf))
452 4 (substitute-command-keys
453 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
455 (defun gnus-score-load-file (file)
456 ;; Load score file FILE. Returns a list a retrieved score-alists.
457 (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
458 (let* ((file (expand-file-name
459 (or (and (string-match
460 (concat "^" (expand-file-name
461 gnus-kill-files-directory))
462 (expand-file-name file))
464 (concat gnus-kill-files-directory file))))
465 (cached (assoc file gnus-score-cache))
466 (global (member file gnus-internal-global-score-files))
469 ;; The score file was already loaded.
470 (setq alist (cdr cached))
471 ;; We load the score file.
472 (setq gnus-score-alist nil)
473 (setq alist (gnus-score-load-score-alist file))
474 ;; We add '(touched) to the alist to signify that it hasn't been
476 (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
477 ;; If it is a global score file, we make it read-only.
479 (not (assq 'read-only alist))
480 (setq alist (cons (list 'read-only t) alist)))
482 (setq gnus-score-cache
483 (cons (cons file alist) gnus-score-cache)))
484 ;; If there are actual scores in the alist, we add it to the
485 ;; return value of this function.
486 (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
487 (setq lists (list alist)))
488 ;; Treat the other possible atoms in the score alist.
489 (let ((mark (car (gnus-score-get 'mark alist)))
490 (expunge (car (gnus-score-get 'expunge alist)))
492 (car (gnus-score-get 'mark-and-expunge alist)))
493 (read-only (gnus-score-get 'read-only alist))
494 (files (gnus-score-get 'files alist))
495 (exclude-files (gnus-score-get 'exclude-files alist))
496 (orphan (car (gnus-score-get 'orphan alist)))
497 (adapt (gnus-score-get 'adapt alist))
498 (eval (car (gnus-score-get 'eval alist))))
499 ;; We do not respect eval and files atoms from global score
501 (and files (not global)
502 (setq lists (apply 'append lists
503 (mapcar (lambda (file)
504 (gnus-score-load-file file))
506 (and eval (not global) (eval eval))
507 (setq gnus-scores-exclude-files exclude-files)
508 (if orphan (setq gnus-orphan-score orphan))
509 (setq gnus-adaptive-score-alist
510 (cond ((equal adapt '(t))
511 (setq gnus-newsgroup-adaptive t)
512 gnus-default-adaptive-score-alist)
513 ((equal adapt '(ignore))
514 (setq gnus-newsgroup-adaptive nil))
516 (setq gnus-newsgroup-adaptive t)
519 (setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
520 gnus-default-adaptive-score-alist)))
521 (setq gnus-summary-mark-below
522 (or mark mark-and-expunge gnus-summary-mark-below))
523 (setq gnus-summary-expunge-below
524 (or expunge mark-and-expunge gnus-summary-expunge-below)))
525 (setq gnus-current-score-file file)
526 (setq gnus-score-alist alist)
529 (defun gnus-score-load (file)
531 (let ((cache (assoc file gnus-score-cache)))
533 (setq gnus-score-alist (cdr cache))
534 (setq gnus-score-alist nil)
535 (gnus-score-load-score-alist file)
537 (setq gnus-score-alist (copy-alist '((touched nil)))))
538 (setq gnus-score-cache
539 (cons (cons file gnus-score-alist) gnus-score-cache)))))
541 (defun gnus-score-remove-from-cache (file)
542 (setq gnus-score-cache
543 (delq (assoc file gnus-score-cache) gnus-score-cache)))
545 (defun gnus-score-load-score-alist (file)
547 (if (file-readable-p file)
550 (gnus-set-work-buffer)
551 (insert-file-contents file)
552 (goto-char (point-min))
553 ;; Only do the loading if the score file isn't empty.
554 (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
557 (read (current-buffer))
560 (gnus-message 3 "Problem with score file %s" file)
564 (if (eq (car alist) 'setq)
565 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
566 (setq gnus-score-alist alist))
567 (setq gnus-score-alist
568 (gnus-score-check-syntax gnus-score-alist file)))
569 (setq gnus-score-alist nil))))
571 (defun gnus-score-check-syntax (alist file)
576 (gnus-message 1 "Score file is not a list: %s" file)
582 (while (and a (not err))
583 (cond ((not (listp (car a)))
584 (gnus-message 3 "Illegal score element %s in %s" (car a) file)
586 ((and (stringp (car (car a)))
587 (not (listp (nth 1 (car a)))))
588 (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
598 (defun gnus-score-transform-old-to-new (alist)
599 (let* ((alist (nth 2 alist))
601 (if (eq (car alist) 'quote)
602 (setq alist (nth 1 alist)))
604 (setq entry (car alist))
605 (if (stringp (car entry))
606 (let ((scor (cdr entry)))
607 (setq out (cons entry out))
610 (list (car (car scor)) (nth 2 (car scor))
611 (and (nth 3 (car scor))
612 (gnus-day-number (nth 3 (car scor))))
613 (if (nth 1 (car scor)) 'r 's)))
614 (setq scor (cdr scor))))
615 (setq out (cons (if (not (listp (cdr entry)))
616 (list (car entry) (cdr entry))
619 (setq alist (cdr alist)))
620 (cons (list 'touched t) (nreverse out))))
622 (defun gnus-score-save ()
623 ;; Save all SCORE information.
624 (let ((cache gnus-score-cache))
626 (setq gnus-score-alist nil)
627 (set-buffer (get-buffer-create "*Score*"))
628 (buffer-disable-undo (current-buffer))
629 (let (entry score file)
631 (setq entry (car cache)
635 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
636 (gnus-score-get 'read-only score)
637 (and (file-exists-p file)
638 (not (file-writable-p file))))
640 (setq score (setcdr entry (delq (assq 'touched score) score)))
642 (let (emacs-lisp-mode-hook)
643 (if (string-match (concat gnus-adaptive-file-suffix "$") file)
644 ;; This is an adaptive score file, so we do not run
645 ;; it through `pp'. These files can get huge, and
646 ;; are not meant to be edited by human hands.
647 (insert (format "%S" score))
648 ;; This is a normal score file, so we print it very
650 (pp score (current-buffer))))
651 (gnus-make-directory (file-name-directory file))
652 (write-region (point-min) (point-max) file nil 'silent))))
653 (kill-buffer (current-buffer)))))
655 (defun gnus-score-headers (score-files &optional trace)
656 ;; Score `gnus-newsgroup-headers'.
658 ;; PLM: probably this is not the best place to clear orphan-score
659 (setq gnus-orphan-score nil)
660 ;; Load the SCORE files.
662 (if (stringp (car score-files))
663 ;; It is a string, which means that it's a score file name,
664 ;; so we load the score file and add the score alist to
665 ;; the list of alists.
666 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
667 ;; It is an alist, so we just add it to the list directly.
668 (setq scores (nconc (car score-files) scores)))
669 (setq score-files (cdr score-files)))
670 ;; Prune the score files that are to be excluded, if any.
671 (if (not gnus-scores-exclude-files)
676 (and (setq c (rassq (car s) gnus-score-cache))
677 (member (car c) gnus-scores-exclude-files)
678 (setq scores (delq (car s) scores)))
680 (if (not (and gnus-summary-default-score
682 (> (length gnus-newsgroup-headers)
683 (length gnus-newsgroup-scored))))
685 (let* ((entries gnus-header-index)
686 (now (gnus-day-number (current-time-string)))
687 (expire (- now gnus-score-expiry-days))
688 (headers gnus-newsgroup-headers)
689 (gnus-current-score-file gnus-current-score-file)
691 (gnus-message 5 "Scoring...")
692 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
694 (setq header (car headers)
695 headers (cdr headers))
696 ;; WARNING: The assq makes the function O(N*S) while it could
697 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
698 ;; and S is (length gnus-newsgroup-scored).
699 (or (assq (header-number header) gnus-newsgroup-scored)
700 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
701 (cons (cons header (or gnus-summary-default-score 0))
702 gnus-scores-articles))))
705 (set-buffer (get-buffer-create "*Headers*"))
706 (buffer-disable-undo (current-buffer))
708 (if gnus-orphan-score
710 (setq gnus-score-index
711 (nth 1 (assoc "references" gnus-header-index)))
712 (gnus-score-orphans gnus-orphan-score)))
713 ;; Run each header through the score process.
715 (setq entry (car entries)
717 entries (cdr entries))
718 (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
719 (if (< 0 (apply 'max (mapcar
721 (length (gnus-score-get header score)))
723 (funcall (nth 2 entry) scores header now expire trace)))
724 ;; Remove the buffer.
725 (kill-buffer (current-buffer)))
727 ;; Add articles to `gnus-newsgroup-scored'.
728 (while gnus-scores-articles
729 (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
730 (setq gnus-newsgroup-scored
731 (cons (cons (header-number
732 (car (car gnus-scores-articles)))
733 (cdr (car gnus-scores-articles)))
734 gnus-newsgroup-scored)))
735 (setq gnus-scores-articles (cdr gnus-scores-articles)))
737 (gnus-message 5 "Scoring...done")))))
740 (defun gnus-get-new-thread-ids (articles)
741 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
742 (refind gnus-score-index)
743 id-list art this tref)
745 (setq art (car articles)
746 this (aref (car art) index)
747 tref (aref (car art) refind)
748 articles (cdr articles))
749 (if (string-equal tref "") ;no references line
750 (setq id-list (cons this id-list))))
753 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
754 (defun gnus-score-orphans (score)
755 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
756 (index (nth 1 (assoc "references" gnus-header-index)))
757 alike articles art arts this last this-id)
759 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
760 articles gnus-scores-articles)
762 ;;more or less the same as in gnus-score-string
765 (setq art (car articles)
766 this (aref (car art) gnus-score-index)
767 articles (cdr articles))
768 ;;completely skip if this is empty (not a child, so not an orphan)
769 (if (not (string= this ""))
770 (if (equal last this)
771 ;; O(N*H) cons-cells used here, where H is the number of
773 (setq alike (cons art alike))
776 ;; Insert the line, with a text property on the
777 ;; terminating newline refering to the articles with
780 (put-text-property (1- (point)) (point) 'articles alike)))
781 (setq alike (list art)
783 (and last ; Bwadr, duplicate code.
786 (put-text-property (1- (point)) (point) 'articles alike)))
788 ;; PLM: now delete those lines that contain an entry from new-thread-ids
789 (while new-thread-ids
790 (setq this-id (car new-thread-ids)
791 new-thread-ids (cdr new-thread-ids))
792 (goto-char (point-min))
793 (while (search-forward this-id nil t)
794 ;; found a match. remove this line
798 ;; now for each line: update its articles with score by moving to
799 ;; every end-of-line in the buffer and read the articles property
800 (goto-char (point-min))
803 (setq arts (get-text-property (point) 'articles))
807 (setcdr art (+ score (cdr art))))
811 (defun gnus-score-integer (scores header now expire &optional trace)
812 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
813 alike last this art entries alist articles)
817 (setq alist (car scores)
819 entries (assoc header alist))
820 (while (cdr entries) ;First entry is the header index.
821 (let* ((rest (cdr entries))
824 (type (or (nth 3 kill) '>))
825 (score (or (nth 1 kill) gnus-score-interactive-default-score))
828 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
829 (eq type '>=) (eq type '=))
831 (error "Illegal match type: %s" type)))
832 (articles gnus-scores-articles)
834 ;; Instead of doing all the clever stuff that
835 ;; `gnus-score-string' does to minimize searches and stuff,
836 ;; I will assume that people generally will put so few
837 ;; matches on numbers that any cleverness will take more
838 ;; time than one would gain.
840 (and (funcall match-func
841 (or (aref (car (car articles)) gnus-score-index) 0)
844 (and trace (setq gnus-score-trace
845 (cons (cons (car (car articles)) kill)
848 (setcdr (car articles) (+ score (cdr (car articles))))))
849 (setq articles (cdr articles)))
850 ;; Update expire date
851 (cond ((null date)) ;Permanent entry.
852 (found ;Match, update date.
853 (gnus-score-set 'touched '(t) alist)
854 (setcar (nthcdr 2 kill) now))
855 ((< date expire) ;Old entry, remove.
856 (gnus-score-set 'touched '(t) alist)
857 (setcdr entries (cdr rest))
858 (setq rest entries)))
859 (setq entries rest))))))
861 (defun gnus-score-date (scores header now expire &optional trace)
862 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
863 alike last this art entries alist articles)
867 (setq alist (car scores)
869 entries (assoc header alist))
870 (while (cdr entries) ;First entry is the header index.
871 (let* ((rest (cdr entries))
873 (match (timezone-make-date-sortable (nth 0 kill)))
874 (type (or (nth 3 kill) 'before))
875 (score (or (nth 1 kill) gnus-score-interactive-default-score))
879 (cond ((eq type 'after) 'string<)
880 ((eq type 'before) 'gnus-string>)
881 ((eq type 'at) 'string=)
882 (t (error "Illegal match type: %s" type))))
883 (articles gnus-scores-articles)
885 ;; Instead of doing all the clever stuff that
886 ;; `gnus-score-string' does to minimize searches and stuff,
887 ;; I will assume that people generally will put so few
888 ;; matches on numbers that any cleverness will take more
889 ;; time than one would gain.
892 (setq l (aref (car (car articles)) gnus-score-index))
893 (funcall match-func match (timezone-make-date-sortable l))
895 (and trace (setq gnus-score-trace
896 (cons (cons (car (car articles)) kill)
899 (setcdr (car articles) (+ score (cdr (car articles))))))
900 (setq articles (cdr articles)))
901 ;; Update expire date
902 (cond ((null date)) ;Permanent entry.
903 (found ;Match, update date.
904 (gnus-score-set 'touched '(t) alist)
905 (setcar (nthcdr 2 kill) now))
906 ((< date expire) ;Old entry, remove.
907 (gnus-score-set 'touched '(t) alist)
908 (setcdr entries (cdr rest))
909 (setq rest entries)))
910 (setq entries rest))))))
912 (defun gnus-score-body (scores header now expire &optional trace)
914 (set-buffer nntp-server-buffer)
916 (let* ((buffer-read-only nil)
917 (articles gnus-scores-articles)
919 (request-func (cond ((string= "head" (downcase header))
921 ((string= "body" (downcase header))
923 (t 'gnus-request-article)))
924 alike last this art entries alist ofunc article)
925 ;; Not all backends support partial fetching. In that case,
926 ;; we just fetch the entire article.
927 (or (gnus-check-backend-function request-func gnus-newsgroup-name)
929 (setq ofunc request-func)
930 (setq request-func 'gnus-request-article)))
932 (setq article (header-number (car (car articles))))
933 (gnus-message 7 "Scoring on article %s..." article)
934 (if (not (funcall request-func article gnus-newsgroup-name))
937 (goto-char (point-min))
938 ;; If just parts of the article is to be searched, but the
939 ;; backend didn't support partial fetching, we just narrow
940 ;; to the relevant parts.
942 (if (eq ofunc 'gnus-request-head)
945 (or (search-forward "\n\n" nil t) (point-max)))
947 (or (search-forward "\n\n" nil t) (point))
949 (setq scores all-scores)
952 (setq alist (car scores)
954 entries (assoc header alist))
955 (while (cdr entries) ;First entry is the header index.
956 (let* ((rest (cdr entries))
959 (type (or (nth 3 kill) 's))
960 (score (or (nth 1 kill)
961 gnus-score-interactive-default-score))
965 (not (or (eq type 'R) (eq type 'S)
966 (eq type 'Regexp) (eq type 'String))))
968 (cond ((or (eq type 'r) (eq type 'R)
969 (eq type 'regexp) (eq type 'Regexp))
971 ((or (eq type 's) (eq type 'S)
972 (eq type 'string) (eq type 'String))
975 (error "Illegal match type: %s" type))))
977 (goto-char (point-min))
978 (if (funcall search-func match nil t)
979 ;; Found a match, update scores.
981 (setcdr (car articles) (+ score (cdr (car articles))))
983 (and trace (setq gnus-score-trace
984 (cons (cons (car (car articles)) kill)
985 gnus-score-trace)))))
986 ;; Update expire date
987 (cond ((null date)) ;Permanent entry.
988 (found ;Match, update date.
989 (gnus-score-set 'touched '(t) alist)
990 (setcar (nthcdr 2 kill) now))
991 ((< date expire) ;Old entry, remove.
992 (gnus-score-set 'touched '(t) alist)
993 (setcdr entries (cdr rest))
994 (setq rest entries)))
995 (setq entries rest)))))
996 (setq articles (cdr articles)))))))
1000 (defun gnus-score-followup (scores header now expire)
1001 ;; Insert the unique article headers in the buffer.
1002 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1003 (current-score-file gnus-current-score-file)
1004 ;; gnus-score-index is used as a free variable.
1005 alike last this art entries alist articles)
1007 ;; Change score file to the adaptive score file. All entries that
1008 ;; this function makes will be put into this file.
1009 (gnus-score-load-file (gnus-score-file-name
1010 gnus-newsgroup-name gnus-adaptive-file-suffix))
1012 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1013 articles gnus-scores-articles)
1017 (setq art (car articles)
1018 this (aref (car art) gnus-score-index)
1019 articles (cdr articles))
1020 (if (equal last this)
1021 (setq alike (cons art alike))
1025 (put-text-property (1- (point)) (point) 'articles alike)))
1026 (setq alike (list art)
1028 (and last ; Bwadr, duplicate code.
1031 (put-text-property (1- (point)) (point) 'articles alike)))
1035 (setq alist (car scores)
1037 entries (assoc header alist))
1038 (while (cdr entries) ;First entry is the header index.
1039 (let* ((rest (cdr entries))
1041 (match (nth 0 kill))
1042 (type (or (nth 3 kill) 's))
1043 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1046 (mt (aref (symbol-name type) 0))
1048 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1051 (cond ((= dmt ?r) 're-search-forward)
1052 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1053 (t (error "Illegal match type: %s" type))))
1055 (goto-char (point-min))
1057 (while (funcall search-func match nil t)
1058 (and (= (progn (beginning-of-line) (point))
1059 (match-beginning 0))
1060 (= (progn (end-of-line) (point))
1063 (setq found (setq arts (get-text-property
1064 (point) 'articles)))
1065 ;; Found a match, update scores.
1067 (setq art (car arts)
1069 (gnus-score-add-followups (car art) score)))))
1070 (while (funcall search-func match nil t)
1072 (setq found (setq arts (get-text-property (point) 'articles)))
1073 ;; Found a match, update scores.
1075 (setq art (car arts)
1077 (gnus-score-add-followups (car art) score))))
1078 ;; Update expire date
1079 (cond ((null date)) ;Permanent entry.
1080 (found ;Match, update date.
1081 (gnus-score-set 'touched '(t) alist)
1082 (setcar (nthcdr 2 kill) now))
1083 ((< date expire) ;Old entry, remove.
1084 (gnus-score-set 'touched '(t) alist)
1085 (setcdr entries (cdr rest))
1086 (setq rest entries)))
1087 (setq entries rest))))
1088 ;; We change the score file back to the previous one.
1089 (gnus-score-load-file current-score-file)))
1091 (defun gnus-score-add-followups (header score)
1093 (set-buffer gnus-summary-buffer)
1094 (let ((id (header-id header))
1095 (score gnus-score-alist)
1097 ;; Don't enter a score if there already is one.
1099 (and (equal "references" (car (car score)))
1100 (or (null (nth 3 (car score)))
1101 (eq 's (nth 3 (car score))))
1103 (or (assoc id (car score))
1106 (setq score (cdr score)))
1108 (gnus-summary-score-entry
1109 "references" id 's score (current-time-string) nil t)))))
1112 (defun gnus-score-string (score-list header now expire &optional trace)
1113 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1114 ;; Update matches entries to NOW and remove unmatched entried older
1117 ;; Insert the unique article headers in the buffer.
1118 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1119 ;; gnus-score-index is used as a free variable.
1120 alike last this art entries alist articles scores fuzzy)
1122 ;; Sorting the articles costs os O(N*log N) but will allow us to
1123 ;; only match with each unique header. Thus the actual matching
1124 ;; will be O(M*U) where M is the number of strings to match with,
1125 ;; and U is the number of unique headers. It is assumed (but
1126 ;; untested) this will be a net win because of the large constant
1127 ;; factor involved with string matching.
1128 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1129 articles gnus-scores-articles)
1133 (setq art (car articles)
1134 this (aref (car art) gnus-score-index)
1135 articles (cdr articles))
1136 (if (equal last this)
1137 ;; O(N*H) cons-cells used here, where H is the number of
1139 (setq alike (cons art alike))
1142 ;; Insert the line, with a text property on the
1143 ;; terminating newline refering to the articles with
1146 (put-text-property (1- (point)) (point) 'articles alike)))
1147 (setq alike (list art)
1149 (and last ; Bwadr, duplicate code.
1152 (put-text-property (1- (point)) (point) 'articles alike)))
1154 ;; Find ordinary matches.
1155 (setq scores score-list)
1157 (setq alist (car scores)
1159 entries (assoc header alist))
1160 (while (cdr entries) ;First entry is the header index.
1161 (let* ((rest (cdr entries))
1163 (match (nth 0 kill))
1164 (type (or (nth 3 kill) 's))
1165 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1168 (mt (aref (symbol-name type) 0))
1170 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1173 (cond ((= dmt ?r) 're-search-forward)
1174 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1175 (t (error "Illegal match type: %s" type))))
1179 (goto-char (point-min))
1181 (while (and (not (eobp))
1182 (funcall search-func match nil t))
1183 (and (= (progn (beginning-of-line) (point))
1184 (match-beginning 0))
1185 (= (progn (end-of-line) (point))
1188 (setq found (setq arts (get-text-property
1189 (point) 'articles)))
1190 ;; Found a match, update scores.
1193 (setq art (car arts)
1195 (setcdr art (+ score (cdr art)))
1196 (setq gnus-score-trace
1197 (cons (cons (header-number
1201 (setq art (car arts)
1203 (setcdr art (+ score (cdr art)))))))
1205 (and (string= match "") (setq match "\n"))
1206 (while (funcall search-func match nil t)
1208 (setq found (setq arts (get-text-property (point) 'articles)))
1209 ;; Found a match, update scores.
1212 (setq art (car arts)
1214 (setcdr art (+ score (cdr art)))
1215 (setq gnus-score-trace
1216 (cons (cons (header-number (car art)) kill)
1219 (setq art (car arts)
1221 (setcdr art (+ score (cdr art)))))))
1222 ;; Update expire date
1223 (cond ((null date)) ;Permanent entry.
1224 (found ;Match, update date.
1225 (gnus-score-set 'touched '(t) alist)
1226 (setcar (nthcdr 2 kill) now))
1227 ((< date expire) ;Old entry, remove.
1228 (gnus-score-set 'touched '(t) alist)
1229 (setcdr entries (cdr rest))
1230 (setq rest entries))))
1231 (setq entries rest))))
1233 ;; Find fuzzy matches.
1234 (setq scores (and fuzzy score-list))
1235 (if fuzzy (gnus-simplify-buffer-fuzzy))
1237 (setq alist (car scores)
1239 entries (assoc header alist))
1240 (while (cdr entries) ;First entry is the header index.
1241 (let* ((rest (cdr entries))
1243 (match (nth 0 kill))
1244 (type (or (nth 3 kill) 's))
1245 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1248 (mt (aref (symbol-name type) 0))
1250 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1253 (cond ((= dmt ?r) 're-search-forward)
1254 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1255 (t (error "Illegal match type: %s" type))))
1259 (goto-char (point-min))
1260 (while (and (not (eobp))
1261 (funcall search-func match nil t))
1262 (and (= (progn (beginning-of-line) (point))
1263 (match-beginning 0))
1264 (= (progn (end-of-line) (point))
1267 (setq found (setq arts (get-text-property
1268 (point) 'articles)))
1269 ;; Found a match, update scores.
1271 (setq art (car arts)
1273 (setcdr art (+ score (cdr art))))))
1275 ;; Update expire date
1276 (cond ((null date)) ;Permanent entry.
1277 (found ;Match, update date.
1278 (gnus-score-set 'touched '(t) alist)
1279 (setcar (nthcdr 2 kill) now))
1280 ((< date expire) ;Old entry, remove.
1281 (gnus-score-set 'touched '(t) alist)
1282 (setcdr entries (cdr rest))
1283 (setq rest entries))))
1284 (setq entries rest))))))
1286 (defun gnus-score-string< (a1 a2)
1287 ;; Compare headers in articles A2 and A2.
1288 ;; The header index used is the free variable `gnus-score-index'.
1289 (string-lessp (aref (car a1) gnus-score-index)
1290 (aref (car a2) gnus-score-index)))
1292 (defun gnus-score-build-cons (article)
1293 ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1294 (cons (header-number (car article)) (cdr article)))
1296 (defconst gnus-header-index
1297 ;; Name to index alist.
1298 '(("number" 0 gnus-score-integer)
1299 ("subject" 1 gnus-score-string)
1300 ("from" 2 gnus-score-string)
1301 ("date" 3 gnus-score-date)
1302 ("message-id" 4 gnus-score-string)
1303 ("references" 5 gnus-score-string)
1304 ("chars" 6 gnus-score-integer)
1305 ("lines" 7 gnus-score-integer)
1306 ("xref" 8 gnus-score-string)
1307 ("head" -1 gnus-score-body)
1308 ("body" -1 gnus-score-body)
1309 ("all" -1 gnus-score-body)
1310 ("followup" 2 gnus-score-followup)))
1312 (defun gnus-current-score-file-nondirectory (&optional score-file)
1313 (let ((score-file (or score-file gnus-current-score-file)))
1315 (gnus-short-group-name (file-name-nondirectory score-file))
1318 (defun gnus-score-adaptive ()
1320 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1322 (date (current-time-string))
1324 ;; First we transform the adaptive rule alist into something
1325 ;; that's faster to process.
1327 (setq elem (car malist))
1328 (if (symbolp (car elem))
1329 (setcar elem (symbol-value (car elem))))
1330 (setq elem (cdr elem))
1333 (cons (symbol-name (car (car elem))) (cdr (car elem))))
1336 (concat "gnus-header-"
1337 (downcase (symbol-name (car (car elem)))))))
1338 (setq elem (cdr elem)))
1339 (setq malist (cdr malist)))
1340 ;; We change the score file to the adaptive score file.
1341 (gnus-score-load-file (gnus-score-file-name
1342 gnus-newsgroup-name gnus-adaptive-file-suffix))
1343 ;; The we score away.
1344 (goto-char (point-min))
1346 (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1348 (get-text-property (point) 'gnus-pseudo))
1350 (setq headers (gnus-get-header-by-number
1351 (gnus-summary-article-number)))
1353 (setq match (funcall (car (car elem)) headers))
1354 (gnus-summary-score-entry
1355 (nth 1 (car elem)) match
1356 ;; Whether we use regexp or exact matches are controlled
1358 (if (or (not gnus-score-exact-adapt-limit)
1359 (< (length match) gnus-score-exact-adapt-limit))
1361 (nth 2 (car elem)) date nil t)
1362 (setq elem (cdr elem))))
1363 (forward-line 1)))))
1365 (defun gnus-score-remove-lines-adaptive (marks)
1367 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1369 (date (current-time-string))
1371 ;; First we transform the adaptive rule alist into something
1372 ;; that's faster to process.
1374 (setq elem (car malist))
1375 (if (symbolp (car elem))
1376 (setcar elem (symbol-value (car elem))))
1377 (setq elem (cdr elem))
1380 (cons (symbol-name (car (car elem))) (cdr (car elem))))
1383 (concat "gnus-header-"
1384 (downcase (symbol-name (car (car elem)))))))
1385 (setq elem (cdr elem)))
1386 (setq malist (cdr malist)))
1387 ;; The we score away.
1388 (goto-char (point-min))
1389 ;; We change the score file to the adaptive score file.
1390 (gnus-score-load-file (gnus-score-file-name
1391 gnus-newsgroup-name gnus-adaptive-file-suffix))
1392 (while (re-search-forward marks nil t)
1394 (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1396 (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
1398 (setq headers (gnus-get-header-by-number
1399 (gnus-summary-article-number)))
1401 (setq match (funcall (car (car elem)) headers))
1402 (gnus-summary-score-entry
1403 (nth 1 (car elem)) match
1404 (if (or (not gnus-score-exact-adapt-limit)
1405 (< (length match) gnus-score-exact-adapt-limit))
1407 (nth 2 (car elem)) date nil t)
1408 (setq elem (cdr elem))))
1409 (delete-region (point) (progn (forward-line 1) (point)))))))
1415 (defvar gnus-score-mode-map nil)
1416 (defvar gnus-score-mode-hook nil)
1418 (if gnus-score-mode-map
1420 (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1421 (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1422 (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1424 (defun gnus-score-mode ()
1425 "Mode for editing score files.
1426 This mode is an extended emacs-lisp mode.
1428 \\{gnus-score-mode-map}"
1430 (kill-all-local-variables)
1431 (use-local-map gnus-score-mode-map)
1432 (set-syntax-table emacs-lisp-mode-syntax-table)
1433 (setq major-mode 'gnus-score-mode)
1434 (setq mode-name "Score")
1435 (lisp-mode-variables nil)
1436 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1438 (defun gnus-score-edit-insert-date ()
1439 "Insert date in numerical format."
1441 (insert (int-to-string (gnus-day-number (current-time-string)))))
1443 (defun gnus-score-edit-done ()
1444 "Save the score file and return to the summary buffer."
1446 (let ((bufnam (buffer-file-name (current-buffer)))
1447 (winconf gnus-prev-winconf))
1449 (kill-buffer (current-buffer))
1450 (gnus-score-remove-from-cache bufnam)
1451 (gnus-score-load-file bufnam)
1452 (and winconf (set-window-configuration winconf))))
1454 ;;; Finding score files.
1456 (defvar gnus-global-score-files nil
1457 "*List of global score files and directories.
1458 Set this variable if you want to use people's score files. One entry
1459 for each score file or each score file directory. Gnus will decide
1460 by itself what score files are applicable to which group.
1462 Say you want to use the single score file
1463 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
1464 score files in the \"/ftp.some-where:/pub/score\" directory.
1466 (setq gnus-global-score-files
1467 '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
1468 \"/ftp.some-where:/pub/score\"))")
1470 (defun gnus-score-score-files (group)
1471 "Return a list of all possible score files."
1472 ;; Search and set any global score files.
1473 (and gnus-global-score-files
1474 (or gnus-internal-global-score-files
1475 (gnus-score-search-global-directories gnus-global-score-files)))
1476 ;; Fix the kill-file dir variable.
1477 (setq gnus-kill-files-directory
1478 (file-name-as-directory
1479 (or gnus-kill-files-directory "~/News/")))
1480 ;; If er can't read it, there's no score files.
1481 (if (not (file-readable-p (expand-file-name gnus-kill-files-directory)))
1482 (setq gnus-score-file-list nil)
1483 (if (gnus-use-long-file-name 'not-score)
1484 ;; We want long file names.
1485 (if (or (not gnus-score-file-list)
1486 (gnus-file-newer-than gnus-kill-files-directory
1487 (car gnus-score-file-list)))
1488 (setq gnus-score-file-list
1489 (cons (nth 5 (file-attributes gnus-kill-files-directory))
1492 gnus-kill-files-directory t
1493 (gnus-score-file-regexp))))))
1494 ;; We do not use long file names, so we have to do some
1495 ;; directory traversing.
1496 (let ((dir (expand-file-name
1497 (concat gnus-kill-files-directory
1498 (gnus-replace-chars-in-string group ?. ?/))))
1499 (mdir (length (expand-file-name gnus-kill-files-directory)))
1500 (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
1503 (setq suffix (car suffixes)
1504 suffixes (cdr suffixes))
1505 (if (file-exists-p (concat dir "/" suffix))
1506 (setq files (list (concat dir "/" suffix))))
1507 (while (>= (1+ (length dir)) mdir)
1508 (and (file-exists-p (concat dir "/all/" suffix))
1509 (setq files (cons (concat dir "/all/" suffix) files)))
1510 (string-match "/[^/]*$" dir)
1511 (setq dir (substring dir 0 (match-beginning 0)))))
1512 (setq gnus-score-file-list
1513 (cons nil (nreverse files)))))
1514 (cdr gnus-score-file-list)))
1516 (defun gnus-score-file-regexp ()
1517 (concat "\\(" gnus-score-file-suffix
1518 "\\|" gnus-adaptive-file-suffix "\\)$"))
1520 (defun gnus-score-find-bnews (group)
1521 "Return a list of score files for GROUP.
1522 The score files are those files in the ~/News directory which matches
1523 GROUP using BNews sys file syntax."
1524 (let* ((sfiles (append (gnus-score-score-files group)
1525 gnus-internal-global-score-files))
1526 (kill-dir (file-name-as-directory
1527 (expand-file-name gnus-kill-files-directory)))
1528 (klen (length kill-dir))
1529 ofiles not-match regexp)
1531 (set-buffer (get-buffer-create "*gnus score files*"))
1532 (buffer-disable-undo (current-buffer))
1533 ;; Go through all score file names and create regexp with them
1537 (insert (car sfiles))
1538 (goto-char (point-min))
1539 ;; First remove the suffix itself.
1540 (re-search-forward (concat "." (gnus-score-file-regexp)))
1541 (replace-match "" t t)
1542 (goto-char (point-min))
1543 (if (looking-at (regexp-quote kill-dir))
1544 ;; If the file name was just "SCORE", `klen' is one character
1546 (delete-char (min (1- (point-max)) klen))
1547 (goto-char (point-max))
1548 (search-backward "/")
1549 (delete-region (1+ (point)) (point-min)))
1550 ;; Translate "all" to ".*".
1551 (while (search-forward "all" nil t)
1552 (replace-match ".*" t t))
1553 (goto-char (point-min))
1554 ;; Deal with "not."s.
1555 (if (looking-at "not.")
1558 (setq regexp (buffer-substring 5 (point-max))))
1559 (setq regexp (buffer-substring 1 (point-max)))
1560 (setq not-match nil))
1561 ;; Finally - if this resulting regexp matches the group name,
1562 ;; we add this score file to the list of score files
1563 ;; applicable to this group.
1564 (if (or (and not-match
1565 (not (string-match regexp group)))
1566 (and (not not-match)
1567 (string-match regexp group)))
1568 (setq ofiles (cons (car sfiles) ofiles)))
1569 (setq sfiles (cdr sfiles)))
1570 (kill-buffer (current-buffer))
1571 ;; Slight kludge here - the last score file returned should be
1572 ;; the local score file, whether it exists or not. This is so
1573 ;; that any score commands the user enters will go to the right
1574 ;; file, and not end up in some global score file.
1577 (if (gnus-use-long-file-name 'not-score)
1578 (concat gnus-kill-files-directory group "."
1579 gnus-score-file-suffix)
1580 (concat gnus-kill-files-directory
1581 (gnus-replace-chars-in-string group ?. ?/)
1582 "/" gnus-score-file-suffix)))))
1583 (and (member localscore ofiles)
1584 (delete localscore ofiles))
1585 (setq ofiles (cons localscore ofiles)))
1586 (nreverse ofiles))))
1588 (defun gnus-score-find-single (group)
1589 "Return list containing the score file for GROUP."
1590 (list (gnus-score-file-name group)))
1592 (defun gnus-score-find-hierarchical (group)
1593 "Return list of score files for GROUP.
1594 This includes the score file for the group and all its parents."
1595 (let ((all (copy-sequence '(nil)))
1597 (while (string-match "\\." group (1+ start))
1598 (setq start (match-beginning 0))
1599 (setq all (cons (substring group 0 start) all)))
1600 (setq all (cons group all))
1601 (mapcar 'gnus-score-file-name (nreverse all))))
1603 (defun gnus-possibly-score-headers (&optional trace)
1604 (let ((func gnus-score-find-score-files-function)
1606 (and func (not (listp func))
1607 (setq func (list func)))
1608 ;; Go through all the functions for finding score files (or actual
1609 ;; scores) and add them to a list.
1611 (and (symbolp (car func))
1612 (fboundp (car func))
1614 (nconc score-files (funcall (car func) gnus-newsgroup-name))))
1615 (setq func (cdr func)))
1616 (if score-files (gnus-score-headers score-files trace))))
1618 (defun gnus-score-file-name (newsgroup &optional suffix)
1619 "Return the name of a score file for NEWSGROUP."
1620 (let ((suffix (or suffix gnus-score-file-suffix)))
1621 (cond ((or (null newsgroup)
1622 (string-equal newsgroup ""))
1623 ;; The global score file is placed at top of the directory.
1625 suffix (or gnus-kill-files-directory "~/News")))
1626 ((gnus-use-long-file-name 'not-score)
1627 ;; Append ".SCORE" to newsgroup name.
1628 (expand-file-name (concat newsgroup "." suffix)
1629 (or gnus-kill-files-directory "~/News")))
1631 ;; Place "SCORE" under the hierarchical directory.
1632 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
1634 (or gnus-kill-files-directory "~/News"))))))
1636 (defun gnus-score-search-global-directories (files)
1637 "Scan all global score directories for score files."
1638 ;; Set the variable `gnus-internal-global-score-files' to all
1639 ;; available global score files.
1640 (interactive (list gnus-global-score-files))
1643 (if (string-match "/$" (car files))
1644 (setq out (nconc (directory-files
1646 (concat (gnus-score-file-regexp) "$"))))
1647 (setq out (cons (car files) out)))
1648 (setq files (cdr files)))
1649 (setq gnus-internal-global-score-files out)))
1651 (defun gnus-score-find-trace ()
1652 "Find all score rules applied to this article."
1654 (let ((gnus-newsgroup-headers
1655 (list (gnus-get-header-by-number (gnus-summary-article-number))))
1656 (gnus-newsgroup-scored nil)
1657 (buf (current-buffer))
1659 (setq gnus-score-trace nil)
1660 (gnus-possibly-score-headers 'trace)
1661 (pop-to-buffer "*Gnus Scores*")
1663 (setq trace gnus-score-trace)
1665 (error "No score rules apply to the current article."))
1667 (insert (format "%S\n" (cdr (car trace))))
1668 (setq trace (cdr trace)))
1669 (goto-char (point-min))
1670 (pop-to-buffer buf)))
1673 (provide 'gnus-score)
1675 ;;; gnus-score.el ends here