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.
29 (eval-when-compile (require 'cl))
31 (defvar gnus-global-score-files nil
32 "*List of global score files and directories.
33 Set this variable if you want to use people's score files. One entry
34 for each score file or each score file directory. Gnus will decide
35 by itself what score files are applicable to which group.
37 Say you want to use the single score file
38 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
39 score files in the \"/ftp.some-where:/pub/score\" directory.
41 (setq gnus-global-score-files
42 '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
43 \"/ftp.some-where:/pub/score\"))")
45 (defvar gnus-score-file-single-match-alist nil
46 "*Alist mapping regexps to lists of score files.
47 Each element of this alist should be of the form
48 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
50 If the name of a group is matched by REGEXP, the corresponding scorefiles
51 will be used for that group.
52 The first match found is used, subsequent matching entries are ignored (to
53 use multiple matches, see gnus-score-file-multiple-match-alist).
55 These score files are loaded in addition to any files returned by
56 gnus-score-find-score-files-function (which see).")
58 (defvar gnus-score-file-multiple-match-alist nil
59 "*Alist mapping regexps to lists of score files.
60 Each element of this alist should be of the form
61 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
63 If the name of a group is matched by REGEXP, the corresponding scorefiles
64 will be used for that group.
65 If multiple REGEXPs match a group, the score files corresponding to each
66 match will be used (for only one match to be used, see
67 gnus-score-file-single-match-alist).
69 These score files are loaded in addition to any files returned by
70 gnus-score-find-score-files-function (which see).")
72 (defvar gnus-score-file-suffix "SCORE"
73 "*Suffix of the score files.")
75 (defvar gnus-adaptive-file-suffix "ADAPT"
76 "*Suffix of the adaptive score files.")
78 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
79 "*Function used to find score files.
80 The function will be called with the group name as the argument, and
81 should return a list of score files to apply to that group. The score
82 files do not actually have to exist.
84 Predefined values are:
86 gnus-score-find-single: Only apply the group's own score file.
87 gnus-score-find-hierarchical: Also apply score files from parent groups.
88 gnus-score-find-bnews: Apply score files whose names matches.
90 See the documentation to these functions for more information.
92 This variable can also be a list of functions to be called. Each
93 function should either return a list of score files, or a list of
96 (defvar gnus-score-interactive-default-score 1000
97 "*Scoring commands will raise/lower the score with this number as the default.")
99 (defvar gnus-score-expiry-days 7
100 "*Number of days before unused score file entries are expired.")
102 (defvar gnus-orphan-score nil
103 "*All orphans get this score added. Set in the score file.")
105 (defvar gnus-default-adaptive-score-alist
106 '((gnus-kill-file-mark)
108 (gnus-read-mark (from 3) (subject 30))
109 (gnus-catchup-mark (subject -10))
110 (gnus-killed-mark (from -1) (subject -20))
111 (gnus-del-mark (from -2) (subject -15)))
112 "*Alist of marks and scores.")
114 (defvar gnus-score-mimic-keymap nil
115 "*Have the score entry functions pretend that they are a keymap.")
117 (defvar gnus-score-exact-adapt-limit 10
118 "*Number that says how long a match has to be before using substring matching.
119 When doing adaptive scoring, one normally uses fuzzy or substring
120 matching. However, if the header one matches is short, the possibility
121 for false positives is great, so if the length of the match is less
122 than this variable, exact matching will be used.
124 If this variable is nil, exact matching will always be used.")
126 (defvar gnus-score-uncacheable-files "ADAPT$"
127 "*All score files that match this regexp will not be cached.")
131 ;; Internal variables.
133 (defvar gnus-internal-global-score-files nil)
134 (defvar gnus-score-file-list nil)
136 (defvar gnus-score-help-winconf nil)
137 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
138 (defvar gnus-score-trace nil)
139 (defvar gnus-score-edit-buffer nil)
141 (defvar gnus-score-alist nil
142 "Alist containing score information.
143 The keys can be symbols or strings. The following symbols are defined.
145 touched: If this alist has been modified.
146 mark: Automatically mark articles below this.
147 expunge: Automatically expunge articles below this.
148 files: List of other score files to load when loading this one.
149 eval: Sexp to be evaluated when the score file is loaded.
151 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
152 where HEADER is the header being scored, MATCH is the string we are
153 looking for, TYPE is a flag indicating whether it should use regexp or
154 substring matching, SCORE is the score to add and DATE is the date
155 of the last successful match.")
157 (defvar gnus-score-cache nil)
158 (defvar gnus-scores-articles nil)
159 (defvar gnus-header-index nil)
160 (defvar gnus-score-index nil)
163 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
164 (autoload 'appt-select-lowest-window "appt.el"))
166 ;;; Summary mode score maps.
168 (defvar gnus-summary-score-map nil)
170 (define-prefix-command 'gnus-summary-score-map)
171 (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
172 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
173 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
174 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
175 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
176 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
177 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
178 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
179 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
180 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
181 (define-key gnus-summary-score-map "C" 'gnus-score-customize)
185 ;; Summary score file commands
187 ;; Much modification of the kill (ahem, score) code and lots of the
188 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
190 (defun gnus-summary-lower-score (&optional score)
191 "Make a score entry based on the current article.
192 The user will be prompted for header to score on, match type,
193 permanence, and the string to be used. The numerical prefix will be
196 (gnus-summary-increase-score (- (gnus-score-default score))))
198 (defun gnus-summary-increase-score (&optional score)
199 "Make a score entry based on the current article.
200 The user will be prompted for header to score on, match type,
201 permanence, and the string to be used. The numerical prefix will be
204 (gnus-set-global-variables)
205 (let* ((nscore (gnus-score-default score))
206 (prefix (if (< nscore 0) ?L ?I))
207 (increase (> nscore 0))
209 '((?a "from" nil nil string)
210 (?s "subject" nil nil string)
211 (?b "body" "" nil body-string)
212 (?h "head" "" nil body-string)
213 (?i "message-id" nil t string)
214 (?t "references" "message-id" t string)
215 (?x "xref" nil nil string)
216 (?l "lines" nil nil number)
217 (?d "date" nil nil date)
218 (?f "followup" nil nil string)))
220 '((?s s "substring" string)
221 (?e e "exact string" string)
222 (?f f "fuzzy string" string)
223 (?r r "regexp string" string)
224 (?s s "substring" body-string)
225 (?r s "regexp string" body-string)
226 (?b before "before date" date)
227 (?a at "at date" date)
228 (?n now "this date" date)
229 (?< < "less than number" number)
230 (?> > "greater than number" number)
231 (?= = "equal to number" number)))
233 (list (list ?t (current-time-string) "temporary")
234 '(?p perm "permanent") '(?i now "immediate")))
235 (mimic gnus-score-mimic-keymap)
236 hchar entry temporary tchar pchar end type match)
237 ;; First we read the header to score.
242 (message "%c-" prefix))
243 (message "%s header (%s?): " (if increase "Increase" "Lower")
244 (mapconcat (lambda (s) (char-to-string (car s)))
246 (setq hchar (read-char))
247 (if (not (or (= hchar ??) (= hchar ?\C-h)))
250 (gnus-score-insert-help "Match on header" char-to-header 1)))
252 (and (get-buffer "*Score Help*")
254 (kill-buffer "*Score Help*")
255 (and gnus-score-help-winconf
256 (set-window-configuration gnus-score-help-winconf))))
258 (or (setq entry (assq (downcase hchar) char-to-header))
262 (if mimic (message "%c %c" prefix hchar) (message ""))))
263 (if (or end (/= (downcase hchar) hchar))
265 ;; This was a majuscle, so we end reading and set the defaults.
266 (if mimic (message "%c %c" prefix hchar) (message ""))
268 temporary (current-time-string)))
270 ;; We continue reading - the type.
275 (message "%c %c-" prefix hchar))
276 (message "%s header '%s' with match type (%s?): "
277 (if increase "Increase" "Lower")
279 (mapconcat (lambda (s)
280 (if (eq (nth 4 entry)
282 (char-to-string (car s))
285 (setq tchar (read-char))
286 (if (not (or (= tchar ??) (= tchar ?\C-h)))
289 (gnus-score-insert-help "Match type" char-to-type 2)))
291 (and (get-buffer "*Score Help*")
293 (and gnus-score-help-winconf
294 (set-window-configuration gnus-score-help-winconf))
295 (kill-buffer "*Score Help*")))
297 (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
300 (if mimic (message "%c %c" prefix hchar) (message ""))
302 (if (or end (/= (downcase tchar) tchar))
304 ;; It was a majuscle, so we end reading and the the default.
305 (if mimic (message "%c %c %c" prefix hchar tchar)
307 (setq temporary (current-time-string)))
309 ;; We continue reading.
314 (message "%c %c %c-" prefix hchar tchar))
315 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
316 (mapconcat (lambda (s) (char-to-string (car s)))
318 (setq pchar (read-char))
319 (if (not (or (= pchar ??) (= pchar ?\C-h)))
322 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
324 (and (get-buffer "*Score Help*")
326 (and gnus-score-help-winconf
327 (set-window-configuration gnus-score-help-winconf))
328 (kill-buffer "*Score Help*")))
330 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
332 (if (setq temporary (nth 1 (assq pchar char-to-perm)))
337 (message "%c %c %c %c" prefix hchar tchar pchar)
340 ;; We have all the data, so we enter this score.
343 (setq match (if (string= (nth 2 entry) "") ""
344 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))))
346 ;; Modify the match, perhaps.
348 ((equal (nth 1 entry) "xref")
349 (when (string-match "^Xref: *" match)
350 (setq match (substring match (match-end 0))))
351 (when (string-match "^[^:]* +" match)
352 (setq match (substring match (match-end 0))))))
354 (gnus-summary-score-entry
355 (nth 1 entry) ; Header
358 (if (eq 's score) nil score) ; Score
359 (if (eq 'perm temporary) ; Temp
362 (not (nth 3 entry))) ; Prompt
365 (defun gnus-score-insert-help (string alist idx)
366 (setq gnus-score-help-winconf (current-window-configuration))
368 (set-buffer (get-buffer-create "*Score Help*"))
369 (buffer-disable-undo (current-buffer))
370 (delete-windows-on (current-buffer))
372 (insert string ":\n\n")
377 ;; find the longest string to display
379 (setq n (length (nth idx (car list))))
382 (setq list (cdr list)))
383 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
384 (setq n (/ (window-width) max)) ; items per line
385 (setq width (/ (window-width) n)) ; width of each item
386 ;; insert `n' items, each in a field of width `width'
391 (delete-char -1) ; the `\n' takes a char
393 (setq pad (- width 3))
394 (setq format (concat "%c: %-" (int-to-string pad) "s"))
395 (insert (format format (car (car alist)) (nth idx (car alist))))
396 (setq alist (cdr alist))
398 ;; display ourselves in a small window at the bottom
399 (appt-select-lowest-window)
401 (pop-to-buffer "*Score Help*")
402 (shrink-window-if-larger-than-buffer)
403 (select-window (get-buffer-window gnus-summary-buffer))))
405 (defun gnus-summary-header (header &optional no-err)
406 ;; Return HEADER for current articles, or error.
407 (let ((article (gnus-summary-article-number))
410 (if (and (setq headers (gnus-summary-article-header article))
412 (aref headers (nth 1 (assoc header gnus-header-index)))
415 (error "Pseudo-articles can't be scored")))
417 (error "No article on current line")
420 (defsubst gnus-score-get (symbol &optional alist)
421 ;; Get SYMBOL's definition in ALIST.
427 (gnus-score-file-name gnus-newsgroup-name))
428 gnus-score-alist)))))
430 (defun gnus-summary-score-entry
431 (header match type score date &optional prompt silent)
432 "Enter score file entry.
433 HEADER is the header being scored.
434 MATCH is the string we are looking for.
435 TYPE is the match type: substring, regexp, exact, fuzzy.
436 SCORE is the score to add.
437 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
438 If optional argument `PROMPT' is non-nil, allow user to edit match.
439 If optional argument `SILENT' is nil, show effect of score entry."
441 (list (completing-read "Header: "
443 (lambda (x) (fboundp (nth 2 x)))
445 (read-string "Match: ")
446 (if (y-or-n-p "Use regexp match? ") 'r 's)
447 (and current-prefix-arg
448 (prefix-numeric-value current-prefix-arg))
449 (cond ((not (y-or-n-p "Add to score file? "))
451 ((y-or-n-p "Expire kill? ")
452 (current-time-string))
454 ;; Regexp is the default type.
455 (if (eq type t) (setq type 'r))
456 ;; Simplify matches...
457 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
458 (setq match (if match (gnus-simplify-subject-re match) "")))
460 (setq match (gnus-simplify-subject-fuzzy match))))
461 (let ((score (gnus-score-default score))
462 (header (downcase header)))
463 (and prompt (setq match (read-string
464 (format "Match %s on %s, %s: "
465 (cond ((eq date 'now)
471 (if (< score 0) "lower" "raise"))
473 (int-to-string match)
475 (and (>= (nth 1 (assoc header gnus-header-index)) 0)
476 (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-string)
478 (gnus-summary-score-effect header match type score))
480 ;; If this is an integer comparison, we transform from string to int.
481 (and (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
482 (setq match (string-to-int match)))
486 (and (= score gnus-score-interactive-default-score)
490 (list match score (and date (gnus-day-number date)) type))
492 (list match score (gnus-day-number date)))
497 (old (gnus-score-get header))
499 ;; We see whether we can collapse some score entries.
500 ;; This isn't quite correct, because there may be more elements
501 ;; later on with the same key that have matching elems... Hm.
503 (setq elem (assoc match old))
504 (eq (nth 3 elem) (nth 3 new))
505 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
506 (and (not (nth 2 elem)) (not (nth 2 new)))))
507 ;; Yup, we just add this new score to the old elem.
508 (setcar (cdr elem) (+ (or (nth 1 elem)
509 gnus-score-interactive-default-score)
511 gnus-score-interactive-default-score)))
512 ;; Nope, we have to add a new elem.
513 (gnus-score-set header (if old (cons new old) (list new)))))
514 (gnus-score-set 'touched '(t)))))
516 (defun gnus-summary-score-effect (header match type score)
517 "Simulate the effect of a score file entry.
518 HEADER is the header being scored.
519 MATCH is the string we are looking for.
520 TYPE is a flag indicating if it is a regexp or substring.
521 SCORE is the score to add."
522 (interactive (list (completing-read "Header: "
524 (lambda (x) (fboundp (nth 2 x)))
526 (read-string "Match: ")
527 (y-or-n-p "Use regexp match? ")
528 (prefix-numeric-value current-prefix-arg)))
530 (or (and (stringp match) (> (length match) 0))
532 (goto-char (point-min))
533 (let ((regexp (cond ((eq type 'f)
534 (gnus-simplify-subject-fuzzy match))
536 (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
538 (let ((content (gnus-summary-header header 'noerr))
539 (case-fold-search t))
542 (string-equal (gnus-simplify-subject-fuzzy content)
544 (string-match regexp content))
545 (gnus-summary-raise-score score))))
546 (beginning-of-line 2)))))
548 (defun gnus-summary-score-crossposting (score date)
549 ;; Enter score file entry for current crossposting.
550 ;; SCORE is the score to add.
551 ;; DATE is the expire date.
552 (let ((xref (gnus-summary-header "xref"))
555 (or xref (error "This article is not crossposted"))
556 (while (string-match " \\([^ \t]+\\):" xref start)
557 (setq start (match-end 0))
560 (substring xref (match-beginning 1) (match-end 1)))
561 gnus-newsgroup-name))
562 (gnus-summary-score-entry
563 "xref" (concat " " group ":") nil score date t)))))
570 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
572 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
573 (defun gnus-score-set-mark-below (score)
574 "Automatically mark articles with score below SCORE as read."
576 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
577 (string-to-int (read-string "Mark below: ")))))
578 (setq score (or score gnus-summary-default-score 0))
579 (gnus-score-set 'mark (list score))
580 (gnus-score-set 'touched '(t))
581 (setq gnus-summary-mark-below score)
582 (gnus-score-update-lines))
584 (defun gnus-score-update-lines ()
586 (goto-char (point-min))
588 (gnus-summary-update-line)
591 (defun gnus-score-set-expunge-below (score)
592 "Automatically expunge articles with score below SCORE."
594 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
595 (string-to-int (read-string "Expunge below: ")))))
596 (setq score (or score gnus-summary-default-score 0))
597 (gnus-score-set 'expunge (list score))
598 (gnus-score-set 'touched '(t)))
600 (defun gnus-score-set (symbol value &optional alist)
601 ;; Set SYMBOL to VALUE in ALIST.
606 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
608 (entry (assoc symbol alist)))
609 (cond ((gnus-score-get 'read-only alist)
610 ;; This is a read-only score file, so we do nothing.
613 (setcdr entry value))
615 (error "Empty alist"))
618 (cons (cons symbol value) (cdr alist)))))))
620 (defun gnus-score-change-score-file (file)
621 "Change current score alist."
623 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
624 (gnus-score-load-file file)
625 (gnus-set-mode-line 'summary))
627 (defun gnus-score-edit-alist (file)
628 "Edit the current score alist."
629 (interactive (list gnus-current-score-file))
630 (let ((winconf (current-window-configuration)))
631 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
632 (setq gnus-score-edit-buffer (find-file-noselect file))
633 (gnus-configure-windows 'edit-score)
635 (make-local-variable 'gnus-prev-winconf)
636 (setq gnus-prev-winconf winconf))
638 4 (substitute-command-keys
639 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
641 (defun gnus-score-edit-file (file)
644 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
645 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
646 (let ((winconf (current-window-configuration)))
647 (setq gnus-score-edit-buffer (find-file-noselect file))
648 (gnus-configure-windows 'edit-score)
650 (make-local-variable 'gnus-prev-winconf)
651 (setq gnus-prev-winconf winconf))
653 4 (substitute-command-keys
654 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
656 (defun gnus-score-load-file (file)
657 ;; Load score file FILE. Returns a list a retrieved score-alists.
658 (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
659 (let* ((file (expand-file-name
660 (or (and (string-match
661 (concat "^" (expand-file-name
662 gnus-kill-files-directory))
663 (expand-file-name file))
665 (concat gnus-kill-files-directory file))))
666 (cached (assoc file gnus-score-cache))
667 (global (member file gnus-internal-global-score-files))
670 ;; The score file was already loaded.
671 (setq alist (cdr cached))
672 ;; We load the score file.
673 (setq gnus-score-alist nil)
674 (setq alist (gnus-score-load-score-alist file))
675 ;; We add '(touched) to the alist to signify that it hasn't been
677 (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
678 ;; If it is a global score file, we make it read-only.
680 (not (assq 'read-only alist))
681 (setq alist (cons (list 'read-only t) alist)))
682 (setq gnus-score-cache
683 (cons (cons file alist) gnus-score-cache)))
684 ;; If there are actual scores in the alist, we add it to the
685 ;; return value of this function.
686 (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
687 (setq lists (list alist)))
688 ;; Treat the other possible atoms in the score alist.
689 (let ((mark (car (gnus-score-get 'mark alist)))
690 (expunge (car (gnus-score-get 'expunge alist)))
691 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
692 (files (gnus-score-get 'files alist))
693 (exclude-files (gnus-score-get 'exclude-files alist))
694 (orphan (car (gnus-score-get 'orphan alist)))
695 (adapt (gnus-score-get 'adapt alist))
696 (thread-mark-and-expunge
697 (car (gnus-score-get 'thread-mark-and-expunge alist)))
698 (adapt-file (car (gnus-score-get 'adapt-file)))
699 (local (gnus-score-get 'local alist))
700 (eval (car (gnus-score-get 'eval alist))))
701 ;; We do not respect eval and files atoms from global score
703 (and files (not global)
704 (setq lists (apply 'append lists
705 (mapcar (lambda (file)
706 (gnus-score-load-file file))
707 (if adapt-file (cons adapt-file files)
709 (and eval (not global) (eval eval))
710 ;; We then expand any exclude-file directives.
711 (setq gnus-scores-exclude-files
715 (expand-file-name sfile (file-name-directory file)))
716 exclude-files) gnus-scores-exclude-files))
720 (set-buffer gnus-summary-buffer)
722 (and (consp (car local))
723 (symbolp (car (car local)))
725 (make-local-variable (car (car local)))
726 (set (car (car local)) (nth 1 (car local)))))
727 (setq local (cdr local)))))
728 (if orphan (setq gnus-orphan-score orphan))
729 (setq gnus-adaptive-score-alist
730 (cond ((equal adapt '(t))
731 (setq gnus-newsgroup-adaptive t)
732 gnus-default-adaptive-score-alist)
733 ((equal adapt '(ignore))
734 (setq gnus-newsgroup-adaptive nil))
736 (setq gnus-newsgroup-adaptive t)
739 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
740 gnus-default-adaptive-score-alist)))
741 (setq gnus-thread-expunge-below
742 (or thread-mark-and-expunge gnus-thread-expunge-below))
743 (setq gnus-summary-mark-below
744 (or mark mark-and-expunge gnus-summary-mark-below))
745 (setq gnus-summary-expunge-below
746 (or expunge mark-and-expunge gnus-summary-expunge-below))
747 (setq gnus-newsgroup-adaptive-score-file
748 (or adapt-file gnus-newsgroup-adaptive-score-file)))
749 (setq gnus-current-score-file file)
750 (setq gnus-score-alist alist)
753 (defun gnus-score-load (file)
755 (let ((cache (assoc file gnus-score-cache)))
757 (setq gnus-score-alist (cdr cache))
758 (setq gnus-score-alist nil)
759 (gnus-score-load-score-alist file)
761 (setq gnus-score-alist (copy-alist '((touched nil)))))
762 (setq gnus-score-cache
763 (cons (cons file gnus-score-alist) gnus-score-cache)))))
765 (defun gnus-score-remove-from-cache (file)
766 (setq gnus-score-cache
767 (delq (assoc file gnus-score-cache) gnus-score-cache)))
769 (defun gnus-score-load-score-alist (file)
771 (if (file-readable-p file)
774 (gnus-set-work-buffer)
775 (insert-file-contents file)
776 (goto-char (point-min))
777 ;; Only do the loading if the score file isn't empty.
778 (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
781 (read (current-buffer))
784 (gnus-message 3 "Problem with score file %s" file)
788 (if (eq (car alist) 'setq)
789 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
790 (setq gnus-score-alist alist))
791 (setq gnus-score-alist
792 (gnus-score-check-syntax gnus-score-alist file)))
793 (setq gnus-score-alist nil))))
795 (defun gnus-score-check-syntax (alist file)
800 (gnus-message 1 "Score file is not a list: %s" file)
806 (while (and a (not err))
807 (cond ((not (listp (car a)))
808 (gnus-message 3 "Illegal score element %s in %s" (car a) file)
810 ((and (stringp (car (car a)))
811 (not (listp (nth 1 (car a)))))
812 (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
822 (defun gnus-score-transform-old-to-new (alist)
823 (let* ((alist (nth 2 alist))
825 (if (eq (car alist) 'quote)
826 (setq alist (nth 1 alist)))
828 (setq entry (car alist))
829 (if (stringp (car entry))
830 (let ((scor (cdr entry)))
831 (setq out (cons entry out))
834 (list (car (car scor)) (nth 2 (car scor))
835 (and (nth 3 (car scor))
836 (gnus-day-number (nth 3 (car scor))))
837 (if (nth 1 (car scor)) 'r 's)))
838 (setq scor (cdr scor))))
839 (setq out (cons (if (not (listp (cdr entry)))
840 (list (car entry) (cdr entry))
843 (setq alist (cdr alist)))
844 (cons (list 'touched t) (nreverse out))))
846 (defun gnus-score-save ()
847 ;; Save all score information.
848 (let ((cache gnus-score-cache))
850 (setq gnus-score-alist nil)
851 (set-buffer (get-buffer-create "*Score*"))
852 (buffer-disable-undo (current-buffer))
853 (let (entry score file)
855 (setq entry (car cache)
859 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
860 (gnus-score-get 'read-only score)
861 (and (file-exists-p file)
862 (not (file-writable-p file))))
864 (setq score (setcdr entry (delq (assq 'touched score) score)))
866 (let (emacs-lisp-mode-hook)
867 (if (string-match (concat gnus-adaptive-file-suffix "$") file)
868 ;; This is an adaptive score file, so we do not run
869 ;; it through `pp'. These files can get huge, and
870 ;; are not meant to be edited by human hands.
871 (insert (format "%S" score))
872 ;; This is a normal score file, so we print it very
874 (pp score (current-buffer))))
875 (if (not (gnus-make-directory (file-name-directory file)))
877 ;; If the score file is empty, we delete it.
878 (if (zerop (buffer-size))
880 ;; There are scores, so we write the file.
881 (and (file-writable-p file)
882 (write-region (point-min) (point-max)
884 (and gnus-score-uncacheable-files
885 (string-match gnus-score-uncacheable-files file)
886 (gnus-score-remove-from-cache file)))))
887 (kill-buffer (current-buffer)))))
889 (defun gnus-score-headers (score-files &optional trace)
890 ;; Score `gnus-newsgroup-headers'.
892 ;; PLM: probably this is not the best place to clear orphan-score
893 (setq gnus-orphan-score nil)
894 (setq gnus-scores-articles nil)
895 (setq gnus-scores-exclude-files nil)
896 ;; Load the score files.
898 (if (stringp (car score-files))
899 ;; It is a string, which means that it's a score file name,
900 ;; so we load the score file and add the score alist to
901 ;; the list of alists.
902 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
903 ;; It is an alist, so we just add it to the list directly.
904 (setq scores (nconc (car score-files) scores)))
905 (setq score-files (cdr score-files)))
906 ;; Prune the score files that are to be excluded, if any.
907 (when gnus-scores-exclude-files
911 (and (setq c (rassq (car s) gnus-score-cache))
912 (member (car c) gnus-scores-exclude-files)
913 (setq scores (delq (car s) scores)))
916 (when (and gnus-summary-default-score
918 (> (length gnus-newsgroup-headers)
919 (length gnus-newsgroup-scored)))
920 (let* ((entries gnus-header-index)
921 (now (gnus-day-number (current-time-string)))
922 (expire (- now gnus-score-expiry-days))
923 (headers gnus-newsgroup-headers)
924 (current-score-file gnus-current-score-file)
926 (gnus-message 5 "Scoring...")
927 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
929 (setq header (car headers)
930 headers (cdr headers))
931 ;; WARNING: The assq makes the function O(N*S) while it could
932 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
933 ;; and S is (length gnus-newsgroup-scored).
934 (or (assq (mail-header-number header) gnus-newsgroup-scored)
935 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
936 (cons (cons header (or gnus-summary-default-score 0))
937 gnus-scores-articles))))
940 (set-buffer (get-buffer-create "*Headers*"))
941 (buffer-disable-undo (current-buffer))
943 ;; Set the global variant of this variable.
944 (setq gnus-current-score-file current-score-file)
946 (if gnus-orphan-score
948 (setq gnus-score-index
949 (nth 1 (assoc "references" gnus-header-index)))
950 (gnus-score-orphans gnus-orphan-score)))
951 ;; Run each header through the score process.
953 (setq entry (car entries)
954 header (downcase (nth 0 entry))
955 entries (cdr entries))
956 (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
957 (if (< 0 (apply 'max (mapcar
959 (length (gnus-score-get header score)))
961 ;; Call the scoring function for this type of "header".
962 (funcall (nth 2 entry) scores header now expire trace)))
963 ;; Remove the buffer.
964 (kill-buffer (current-buffer)))
966 ;; Add articles to `gnus-newsgroup-scored'.
967 (while gnus-scores-articles
968 (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
969 (setq gnus-newsgroup-scored
970 (cons (cons (mail-header-number
971 (car (car gnus-scores-articles)))
972 (cdr (car gnus-scores-articles)))
973 gnus-newsgroup-scored)))
974 (setq gnus-scores-articles (cdr gnus-scores-articles)))
976 (gnus-message 5 "Scoring...done")))))
979 (defun gnus-get-new-thread-ids (articles)
980 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
981 (refind gnus-score-index)
982 id-list art this tref)
984 (setq art (car articles)
985 this (aref (car art) index)
986 tref (aref (car art) refind)
987 articles (cdr articles))
988 (if (string-equal tref "") ;no references line
989 (setq id-list (cons this id-list))))
992 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
993 (defun gnus-score-orphans (score)
994 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
995 alike articles art arts this last this-id)
997 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
998 articles gnus-scores-articles)
1000 ;;more or less the same as in gnus-score-string
1003 (setq art (car articles)
1004 this (aref (car art) gnus-score-index)
1005 articles (cdr articles))
1006 ;;completely skip if this is empty (not a child, so not an orphan)
1007 (if (not (string= this ""))
1008 (if (equal last this)
1009 ;; O(N*H) cons-cells used here, where H is the number of
1011 (setq alike (cons art alike))
1014 ;; Insert the line, with a text property on the
1015 ;; terminating newline refering to the articles with
1018 (put-text-property (1- (point)) (point) 'articles alike)))
1019 (setq alike (list art)
1021 (and last ; Bwadr, duplicate code.
1024 (put-text-property (1- (point)) (point) 'articles alike)))
1026 ;; PLM: now delete those lines that contain an entry from new-thread-ids
1027 (while new-thread-ids
1028 (setq this-id (car new-thread-ids)
1029 new-thread-ids (cdr new-thread-ids))
1030 (goto-char (point-min))
1031 (while (search-forward this-id nil t)
1032 ;; found a match. remove this line
1036 ;; now for each line: update its articles with score by moving to
1037 ;; every end-of-line in the buffer and read the articles property
1038 (goto-char (point-min))
1041 (setq arts (get-text-property (point) 'articles))
1043 (setq art (car arts)
1045 (setcdr art (+ score (cdr art))))
1049 (defun gnus-score-integer (scores header now expire &optional trace)
1050 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1055 (setq alist (car scores)
1057 entries (assoc header alist))
1058 (while (cdr entries) ;First entry is the header index.
1059 (let* ((rest (cdr entries))
1061 (match (nth 0 kill))
1062 (type (or (nth 3 kill) '>))
1063 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1066 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1067 (eq type '>=) (eq type '=))
1069 (error "Illegal match type: %s" type)))
1070 (articles gnus-scores-articles))
1071 ;; Instead of doing all the clever stuff that
1072 ;; `gnus-score-string' does to minimize searches and stuff,
1073 ;; I will assume that people generally will put so few
1074 ;; matches on numbers that any cleverness will take more
1075 ;; time than one would gain.
1077 (and (funcall match-func
1078 (or (aref (car (car articles)) gnus-score-index) 0)
1081 (and trace (setq gnus-score-trace
1084 (car-safe (rassq alist gnus-score-cache))
1088 (setcdr (car articles) (+ score (cdr (car articles))))))
1089 (setq articles (cdr articles)))
1090 ;; Update expire date
1091 (cond ((null date)) ;Permanent entry.
1092 (found ;Match, update date.
1093 (gnus-score-set 'touched '(t) alist)
1094 (setcar (nthcdr 2 kill) now))
1095 ((< date expire) ;Old entry, remove.
1096 (gnus-score-set 'touched '(t) alist)
1097 (setcdr entries (cdr rest))
1098 (setq rest entries)))
1099 (setq entries rest))))))
1101 (defun gnus-score-date (scores header now expire &optional trace)
1102 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1107 (setq alist (car scores)
1109 entries (assoc header alist))
1110 (while (cdr entries) ;First entry is the header index.
1111 (let* ((rest (cdr entries))
1113 (match (timezone-make-date-sortable (nth 0 kill)))
1114 (type (or (nth 3 kill) 'before))
1115 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1119 (cond ((eq type 'after) 'string<)
1120 ((eq type 'before) 'gnus-string>)
1121 ((eq type 'at) 'string=)
1122 (t (error "Illegal match type: %s" type))))
1123 (articles gnus-scores-articles)
1125 ;; Instead of doing all the clever stuff that
1126 ;; `gnus-score-string' does to minimize searches and stuff,
1127 ;; I will assume that people generally will put so few
1128 ;; matches on numbers that any cleverness will take more
1129 ;; time than one would gain.
1132 (setq l (aref (car (car articles)) gnus-score-index))
1133 (funcall match-func match (timezone-make-date-sortable l))
1135 (and trace (setq gnus-score-trace
1138 (car-safe (rassq alist gnus-score-cache))
1142 (setcdr (car articles) (+ score (cdr (car articles))))))
1143 (setq articles (cdr articles)))
1144 ;; Update expire date
1145 (cond ((null date)) ;Permanent entry.
1146 (found ;Match, update date.
1147 (gnus-score-set 'touched '(t) alist)
1148 (setcar (nthcdr 2 kill) now))
1149 ((< date expire) ;Old entry, remove.
1150 (gnus-score-set 'touched '(t) alist)
1151 (setcdr entries (cdr rest))
1152 (setq rest entries)))
1153 (setq entries rest))))))
1155 (defun gnus-score-body (scores header now expire &optional trace)
1157 (set-buffer nntp-server-buffer)
1159 (let* ((buffer-read-only nil)
1160 (articles gnus-scores-articles)
1161 (last (mail-header-number (car (car gnus-scores-articles))))
1163 (request-func (cond ((string= "head" (downcase header))
1165 ((string= "body" (downcase header))
1167 (t 'gnus-request-article)))
1168 entries alist ofunc article)
1169 ;; Not all backends support partial fetching. In that case,
1170 ;; we just fetch the entire article.
1171 (or (gnus-check-backend-function
1172 (and (string-match "^gnus-" (symbol-name request-func))
1173 (intern (substring (symbol-name request-func)
1175 gnus-newsgroup-name)
1177 (setq ofunc request-func)
1178 (setq request-func 'gnus-request-article)))
1180 (setq article (mail-header-number (car (car articles))))
1181 (gnus-message 7 "Scoring on article %s of %s..." article last)
1182 (if (not (funcall request-func article gnus-newsgroup-name))
1185 (goto-char (point-min))
1186 ;; If just parts of the article is to be searched, but the
1187 ;; backend didn't support partial fetching, we just narrow
1188 ;; to the relevant parts.
1190 (if (eq ofunc 'gnus-request-head)
1193 (or (search-forward "\n\n" nil t) (point-max)))
1195 (or (search-forward "\n\n" nil t) (point))
1197 (setq scores all-scores)
1200 (setq alist (car scores)
1202 entries (assoc header alist))
1203 (while (cdr entries) ;First entry is the header index.
1204 (let* ((rest (cdr entries))
1206 (match (nth 0 kill))
1207 (type (or (nth 3 kill) 's))
1208 (score (or (nth 1 kill)
1209 gnus-score-interactive-default-score))
1213 (not (or (eq type 'R) (eq type 'S)
1214 (eq type 'Regexp) (eq type 'String))))
1216 (cond ((or (eq type 'r) (eq type 'R)
1217 (eq type 'regexp) (eq type 'Regexp))
1219 ((or (eq type 's) (eq type 'S)
1220 (eq type 'string) (eq type 'String))
1223 (error "Illegal match type: %s" type)))))
1224 (goto-char (point-min))
1225 (if (funcall search-func match nil t)
1226 ;; Found a match, update scores.
1228 (setcdr (car articles) (+ score (cdr (car articles))))
1230 (and trace (setq gnus-score-trace
1234 (rassq alist gnus-score-cache))
1236 gnus-score-trace)))))
1237 ;; Update expire date
1238 (cond ((null date)) ;Permanent entry.
1239 (found ;Match, update date.
1240 (gnus-score-set 'touched '(t) alist)
1241 (setcar (nthcdr 2 kill) now))
1242 ((< date expire) ;Old entry, remove.
1243 (gnus-score-set 'touched '(t) alist)
1244 (setcdr entries (cdr rest))
1245 (setq rest entries)))
1246 (setq entries rest)))))
1247 (setq articles (cdr articles)))))))
1249 (defun gnus-score-followup (scores header now expire &optional trace)
1250 ;; Insert the unique article headers in the buffer.
1251 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1252 (current-score-file gnus-current-score-file)
1254 ;; gnus-score-index is used as a free variable.
1255 alike last this art entries alist articles)
1257 ;; Change score file to the adaptive score file. All entries that
1258 ;; this function makes will be put into this file.
1259 (gnus-score-load-file (gnus-score-file-name
1260 gnus-newsgroup-name gnus-adaptive-file-suffix))
1262 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1263 articles gnus-scores-articles)
1267 (setq art (car articles)
1268 this (aref (car art) gnus-score-index)
1269 articles (cdr articles))
1270 (if (equal last this)
1271 (setq alike (cons art alike))
1275 (put-text-property (1- (point)) (point) 'articles alike)))
1276 (setq alike (list art)
1278 (and last ; Bwadr, duplicate code.
1281 (put-text-property (1- (point)) (point) 'articles alike)))
1285 (setq alist (car scores)
1287 entries (assoc header alist))
1288 (while (cdr entries) ;First entry is the header index.
1289 (let* ((rest (cdr entries))
1291 (match (nth 0 kill))
1292 (type (or (nth 3 kill) 's))
1293 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1296 (mt (aref (symbol-name type) 0))
1298 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1301 (cond ((= dmt ?r) 're-search-forward)
1302 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1303 (t (error "Illegal match type: %s" type))))
1305 (goto-char (point-min))
1307 (while (funcall search-func match nil t)
1308 (and (= (progn (beginning-of-line) (point))
1309 (match-beginning 0))
1310 (= (progn (end-of-line) (point))
1313 (setq found (setq arts (get-text-property
1314 (point) 'articles)))
1315 ;; Found a match, update scores.
1317 (setq art (car arts)
1319 (gnus-score-add-followups
1320 (car art) score all-scores)))))
1321 (while (funcall search-func match nil t)
1323 (setq found (setq arts (get-text-property (point) 'articles)))
1324 ;; Found a match, update scores.
1326 (setq art (car arts)
1328 (gnus-score-add-followups (car art) score all-scores))))
1329 ;; Update expire date
1330 (cond ((null date)) ;Permanent entry.
1331 (found ;Match, update date.
1332 (gnus-score-set 'touched '(t) alist)
1333 (setcar (nthcdr 2 kill) now))
1334 ((< date expire) ;Old entry, remove.
1335 (gnus-score-set 'touched '(t) alist)
1336 (setcdr entries (cdr rest))
1337 (setq rest entries)))
1338 (setq entries rest))))
1339 ;; We change the score file back to the previous one.
1340 (gnus-score-load-file current-score-file)))
1342 (defun gnus-score-add-followups (header score scores)
1344 (set-buffer gnus-summary-buffer)
1345 (let* ((id (mail-header-id header))
1346 (scores (car scores))
1348 ;; Don't enter a score if there already is one.
1350 (setq entry (car scores))
1351 (and (equal "references" (car entry))
1352 (or (null (nth 3 (car (cdr entry))))
1353 (eq 's (nth 3 (car (cdr entry)))))
1355 (if (assoc id entry)
1357 (setq scores (cdr scores)))
1359 (gnus-summary-score-entry
1360 "references" id 's score (current-time-string) nil t)))))
1363 (defun gnus-score-string (score-list header now expire &optional trace)
1364 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1365 ;; Update matches entries to NOW and remove unmatched entried older
1368 ;; Insert the unique article headers in the buffer.
1369 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1370 ;; gnus-score-index is used as a free variable.
1371 alike last this art entries alist articles scores fuzzy)
1373 ;; Sorting the articles costs os O(N*log N) but will allow us to
1374 ;; only match with each unique header. Thus the actual matching
1375 ;; will be O(M*U) where M is the number of strings to match with,
1376 ;; and U is the number of unique headers. It is assumed (but
1377 ;; untested) this will be a net win because of the large constant
1378 ;; factor involved with string matching.
1379 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1380 articles gnus-scores-articles)
1384 (setq art (car articles)
1385 this (aref (car art) gnus-score-index)
1386 articles (cdr articles))
1387 (if (equal last this)
1388 ;; O(N*H) cons-cells used here, where H is the number of
1390 (setq alike (cons art alike))
1393 ;; Insert the line, with a text property on the
1394 ;; terminating newline refering to the articles with
1397 (put-text-property (1- (point)) (point) 'articles alike)))
1398 (setq alike (list art)
1400 (and last ; Bwadr, duplicate code.
1403 (put-text-property (1- (point)) (point) 'articles alike)))
1405 ;; Find ordinary matches.
1406 (setq scores score-list)
1408 (setq alist (car scores)
1410 entries (assoc header alist))
1411 (while (cdr entries) ;First entry is the header index.
1412 (let* ((rest (cdr entries))
1414 (match (nth 0 kill))
1415 (type (or (nth 3 kill) 's))
1416 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1419 (mt (aref (symbol-name type) 0))
1421 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1424 (cond ((= dmt ?r) 're-search-forward)
1425 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1426 (t (error "Illegal match type: %s" type))))
1430 ;; Do non-fuzzy matching.
1431 (goto-char (point-min))
1433 ;; Do exact matching.
1434 (while (and (not (eobp))
1435 (funcall search-func match nil t))
1436 (and (= (progn (beginning-of-line) (point))
1437 (match-beginning 0))
1438 (= (progn (end-of-line) (point))
1441 (setq found (setq arts (get-text-property
1442 (point) 'articles)))
1443 ;; Found a match, update scores.
1446 (setq art (car arts)
1448 (setcdr art (+ score (cdr art)))
1449 (setq gnus-score-trace
1453 (rassq alist gnus-score-cache))
1457 (setq art (car arts)
1459 (setcdr art (+ score (cdr art)))))))
1461 ;; Do regexp and substring matching.
1462 (and (string= match "") (setq match "\n"))
1463 (while (and (not (eobp))
1464 (funcall search-func match nil t))
1465 (goto-char (match-beginning 0))
1467 (setq found (setq arts (get-text-property (point) 'articles)))
1468 ;; Found a match, update scores.
1471 (setq art (pop arts))
1472 (setcdr art (+ score (cdr art)))
1474 (car-safe (rassq alist gnus-score-cache))
1478 (setq art (pop arts))
1479 (setcdr art (+ score (cdr art)))))
1481 ;; Update expire date
1482 (cond ((null date)) ;Permanent entry.
1483 (found ;Match, update date.
1484 (gnus-score-set 'touched '(t) alist)
1485 (setcar (nthcdr 2 kill) now))
1486 ((< date expire) ;Old entry, remove.
1487 (gnus-score-set 'touched '(t) alist)
1488 (setcdr entries (cdr rest))
1489 (setq rest entries))))
1490 (setq entries rest))))
1492 ;; Find fuzzy matches.
1494 (setq scores score-list)
1495 (gnus-simplify-buffer-fuzzy)
1497 (setq alist (car scores)
1499 entries (assoc header alist))
1500 (while (cdr entries) ;First entry is the header index.
1501 (let* ((rest (cdr entries))
1503 (match (nth 0 kill))
1504 (type (or (nth 3 kill) 's))
1505 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1508 (mt (aref (symbol-name type) 0))
1509 (case-fold-search (not (= mt ?F)))
1513 (goto-char (point-min))
1514 (while (and (not (eobp))
1515 (search-forward match nil t))
1516 (when (and (= (progn (beginning-of-line) (point))
1517 (match-beginning 0))
1518 (= (progn (end-of-line) (point))
1520 (setq found (setq arts (get-text-property
1521 (point) 'articles)))
1522 ;; Found a match, update scores.
1525 (setq art (pop arts))
1526 (setcdr art (+ score (cdr art)))
1528 (car-safe (rassq alist gnus-score-cache))
1532 (setq art (pop arts))
1533 (setcdr art (+ score (cdr art))))))
1535 ;; Update expire date
1537 (cond ((null date)) ;Permanent entry.
1538 (found ;Match, update date.
1539 (gnus-score-set 'touched '(t) alist)
1540 (setcar (nthcdr 2 kill) now))
1541 ((< date expire) ;Old entry, remove.
1542 (gnus-score-set 'touched '(t) alist)
1543 (setcdr entries (cdr rest))
1544 (setq rest entries)))))
1545 (setq entries rest)))))))
1547 (defun gnus-score-string< (a1 a2)
1548 ;; Compare headers in articles A2 and A2.
1549 ;; The header index used is the free variable `gnus-score-index'.
1550 (string-lessp (aref (car a1) gnus-score-index)
1551 (aref (car a2) gnus-score-index)))
1553 (defun gnus-score-build-cons (article)
1554 ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1555 (cons (mail-header-number (car article)) (cdr article)))
1557 (defconst gnus-header-index
1558 ;; Name to index alist.
1559 '(("number" 0 gnus-score-integer)
1560 ("subject" 1 gnus-score-string)
1561 ("from" 2 gnus-score-string)
1562 ("date" 3 gnus-score-date)
1563 ("message-id" 4 gnus-score-string)
1564 ("references" 5 gnus-score-string)
1565 ("chars" 6 gnus-score-integer)
1566 ("lines" 7 gnus-score-integer)
1567 ("xref" 8 gnus-score-string)
1568 ("head" -1 gnus-score-body)
1569 ("body" -1 gnus-score-body)
1570 ("all" -1 gnus-score-body)
1571 ("followup" 2 gnus-score-followup)))
1573 (defun gnus-current-score-file-nondirectory (&optional score-file)
1574 (let ((score-file (or score-file gnus-current-score-file)))
1576 (gnus-short-group-name (file-name-nondirectory score-file))
1579 (defun gnus-score-adaptive ()
1581 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1583 (date (current-time-string))
1584 (data gnus-newsgroup-data)
1586 ;; First we transform the adaptive rule alist into something
1587 ;; that's faster to process.
1589 (setq elem (car malist))
1590 (if (symbolp (car elem))
1591 (setcar elem (symbol-value (car elem))))
1592 (setq elem (cdr elem))
1595 (cons (if (eq (car (car elem)) 'followup)
1597 (symbol-name (car (car elem))))
1601 (concat "gnus-header-"
1602 (if (eq (car (car elem)) 'followup)
1604 (downcase (symbol-name (car (car elem))))))))
1605 (setq elem (cdr elem)))
1606 (setq malist (cdr malist)))
1607 ;; We change the score file to the adaptive score file.
1608 (gnus-score-load-file
1609 (or gnus-newsgroup-adaptive-score-file
1610 (gnus-score-file-name
1611 gnus-newsgroup-name gnus-adaptive-file-suffix)))
1612 ;; The we score away.
1614 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
1616 (gnus-data-pseudo-p (car data)))
1618 (when (setq headers (gnus-data-header (car data)))
1620 (setq match (funcall (car (car elem)) headers))
1621 (gnus-summary-score-entry
1622 (nth 1 (car elem)) match
1626 ((equal (nth 1 (car elem)) "date")
1629 ;; Whether we use substring or exact matches are controlled
1631 (if (or (not gnus-score-exact-adapt-limit)
1632 (< (length match) gnus-score-exact-adapt-limit))
1634 (if (equal (nth 1 (car elem)) "subject")
1636 (nth 2 (car elem)) date nil t)
1637 (setq elem (cdr elem)))))
1638 (setq data (cdr data))))))
1644 (defvar gnus-score-mode-map nil)
1645 (defvar gnus-score-mode-hook nil)
1647 (if gnus-score-mode-map
1649 (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1650 (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1651 (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1653 (defun gnus-score-mode ()
1654 "Mode for editing score files.
1655 This mode is an extended emacs-lisp mode.
1657 \\{gnus-score-mode-map}"
1659 (kill-all-local-variables)
1660 (use-local-map gnus-score-mode-map)
1661 (set-syntax-table emacs-lisp-mode-syntax-table)
1662 (setq major-mode 'gnus-score-mode)
1663 (setq mode-name "Score")
1664 (lisp-mode-variables nil)
1665 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1667 (defun gnus-score-edit-insert-date ()
1668 "Insert date in numerical format."
1670 (insert (int-to-string (gnus-day-number (current-time-string)))))
1672 (defun gnus-score-edit-done ()
1673 "Save the score file and return to the summary buffer."
1675 (let ((bufnam (buffer-file-name (current-buffer)))
1676 (winconf gnus-prev-winconf))
1677 (gnus-make-directory (file-name-directory (buffer-file-name)))
1679 (kill-buffer (current-buffer))
1680 (gnus-score-remove-from-cache bufnam)
1681 (gnus-score-load-file bufnam)
1682 (and winconf (set-window-configuration winconf))))
1684 (defun gnus-score-find-trace ()
1685 "Find all score rules applied to this article."
1687 (let ((gnus-newsgroup-headers
1688 (list (gnus-summary-article-header)))
1689 (gnus-newsgroup-scored nil)
1690 (buf (current-buffer))
1692 (setq gnus-score-trace nil)
1693 (gnus-possibly-score-headers 'trace)
1694 (or (setq trace gnus-score-trace)
1695 (error "No score rules apply to the current article."))
1696 (pop-to-buffer "*Gnus Scores*")
1697 (gnus-add-current-to-buffer-list)
1700 (insert (format "%S -> %s\n" (cdr (car trace))
1701 (file-name-nondirectory (car (car trace)))))
1702 (setq trace (cdr trace)))
1703 (goto-char (point-min))
1704 (pop-to-buffer buf)))
1706 (defun gnus-score-flush-cache ()
1707 "Flush the cache of score files."
1709 (setq gnus-score-cache nil)
1710 (gnus-message 6 "The score cache is now flushed"))
1712 (defun gnus-score-close ()
1713 "Clear all internal score variables."
1714 (setq gnus-score-cache nil
1715 gnus-internal-global-score-files nil))
1717 ;; Summary score marking commands.
1719 (defun gnus-summary-raise-same-subject-and-select (score)
1720 "Raise articles which has the same subject with SCORE and select the next."
1722 (let ((subject (gnus-summary-article-subject)))
1723 (gnus-summary-raise-score score)
1724 (while (gnus-summary-find-subject subject)
1725 (gnus-summary-raise-score score))
1726 (gnus-summary-next-article t)))
1728 (defun gnus-summary-raise-same-subject (score)
1729 "Raise articles which has the same subject with SCORE."
1731 (let ((subject (gnus-summary-article-subject)))
1732 (gnus-summary-raise-score score)
1733 (while (gnus-summary-find-subject subject)
1734 (gnus-summary-raise-score score))
1735 (gnus-summary-next-subject 1 t)))
1737 (defun gnus-score-default (level)
1738 (if level (prefix-numeric-value level)
1739 gnus-score-interactive-default-score))
1741 (defun gnus-summary-raise-thread (&optional score)
1742 "Raise the score of the articles in the current thread with SCORE."
1744 (setq score (gnus-score-default score))
1747 (let ((articles (gnus-summary-articles-in-thread)))
1749 (gnus-summary-goto-subject (car articles))
1750 (gnus-summary-raise-score score)
1751 (setq articles (cdr articles))))
1753 (let ((gnus-summary-check-current t))
1754 (or (zerop (gnus-summary-next-subject 1 t))
1756 (gnus-summary-recenter)
1757 (gnus-summary-position-point)
1758 (gnus-set-mode-line 'summary))
1760 (defun gnus-summary-lower-same-subject-and-select (score)
1761 "Raise articles which has the same subject with SCORE and select the next."
1763 (gnus-summary-raise-same-subject-and-select (- score)))
1765 (defun gnus-summary-lower-same-subject (score)
1766 "Raise articles which has the same subject with SCORE."
1768 (gnus-summary-raise-same-subject (- score)))
1770 (defun gnus-summary-lower-thread (&optional score)
1771 "Lower score of articles in the current thread with SCORE."
1773 (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
1775 ;;; Finding score files.
1777 (defun gnus-score-score-files (group)
1778 "Return a list of all possible score files."
1779 ;; Search and set any global score files.
1780 (and gnus-global-score-files
1781 (or gnus-internal-global-score-files
1782 (gnus-score-search-global-directories gnus-global-score-files)))
1783 ;; Fix the kill-file dir variable.
1784 (setq gnus-kill-files-directory
1785 (file-name-as-directory
1786 (or gnus-kill-files-directory "~/News/")))
1787 ;; If we can't read it, there are no score files.
1788 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
1789 (setq gnus-score-file-list nil)
1790 (if (gnus-use-long-file-name 'not-score)
1791 ;; We want long file names.
1792 (if (or (not gnus-score-file-list)
1793 (not (car gnus-score-file-list))
1794 (gnus-file-newer-than gnus-kill-files-directory
1795 (car gnus-score-file-list)))
1796 (setq gnus-score-file-list
1797 (cons (nth 5 (file-attributes gnus-kill-files-directory))
1800 gnus-kill-files-directory t
1801 (gnus-score-file-regexp))))))
1802 ;; We do not use long file names, so we have to do some
1803 ;; directory traversing.
1804 (let ((mdir (length (expand-file-name gnus-kill-files-directory)))
1805 (suffixes (list gnus-score-file-suffix gnus-adaptive-file-suffix))
1808 (setq dir (expand-file-name
1809 (concat gnus-kill-files-directory
1810 (gnus-replace-chars-in-string group ?. ?/))))
1811 (setq dir (gnus-replace-chars-in-string dir ?: ?/))
1812 (setq suffix (car suffixes)
1813 suffixes (cdr suffixes))
1814 (if (file-exists-p (concat dir "/" suffix))
1815 (setq files (cons (concat dir "/" suffix) files)))
1816 (while (>= (1+ (length dir)) mdir)
1817 (and (file-exists-p (concat dir "/all/" suffix))
1818 (setq files (cons (concat dir "/all/" suffix) files)))
1819 (string-match "/[^/]*$" dir)
1820 (setq dir (substring dir 0 (match-beginning 0)))))
1821 (setq gnus-score-file-list
1822 (cons nil (nreverse files)))))
1823 (cdr gnus-score-file-list)))
1825 (defun gnus-score-file-regexp ()
1826 (concat "\\(" gnus-score-file-suffix
1827 "\\|" gnus-adaptive-file-suffix "\\)$"))
1829 (defun gnus-score-find-bnews (group)
1830 "Return a list of score files for GROUP.
1831 The score files are those files in the ~/News directory which matches
1832 GROUP using BNews sys file syntax."
1833 (let* ((sfiles (append (gnus-score-score-files group)
1834 gnus-internal-global-score-files))
1835 (kill-dir (file-name-as-directory
1836 (expand-file-name gnus-kill-files-directory)))
1837 (klen (length kill-dir))
1838 (score-regexp (gnus-score-file-regexp))
1839 ofiles not-match regexp)
1841 (set-buffer (get-buffer-create "*gnus score files*"))
1842 (buffer-disable-undo (current-buffer))
1843 ;; Go through all score file names and create regexp with them
1847 (insert (car sfiles))
1848 (goto-char (point-min))
1849 ;; First remove the suffix itself.
1850 (when (re-search-forward (concat "." score-regexp) nil t)
1851 (replace-match "" t t)
1852 (goto-char (point-min))
1853 (if (looking-at (regexp-quote kill-dir))
1854 ;; If the file name was just "SCORE", `klen' is one character
1856 (delete-char (min (1- (point-max)) klen))
1857 (goto-char (point-max))
1858 (search-backward "/")
1859 (delete-region (1+ (point)) (point-min)))
1860 ;; If short file names were used, we have to translate slashes.
1861 (goto-char (point-min))
1862 (while (re-search-forward "[/:]" nil t)
1863 (replace-match "." t t))
1864 ;; Cludge to get rid of "nntp+" problems.
1865 (goto-char (point-min))
1866 (and (looking-at "nn[a-z]+\\+")
1868 (search-forward "+")
1871 ;; Translate "all" to ".*".
1872 (while (search-forward "all" nil t)
1873 (replace-match ".*" t t))
1874 (goto-char (point-min))
1875 ;; Deal with "not."s.
1876 (if (looking-at "not.")
1879 (setq regexp (buffer-substring 5 (point-max))))
1880 (setq regexp (buffer-substring 1 (point-max)))
1881 (setq not-match nil))
1882 ;; Finally - if this resulting regexp matches the group name,
1883 ;; we add this score file to the list of score files
1884 ;; applicable to this group.
1885 (if (or (and not-match
1886 (not (string-match regexp group)))
1887 (and (not not-match)
1888 (string-match regexp group)))
1889 (setq ofiles (cons (car sfiles) ofiles))))
1890 (setq sfiles (cdr sfiles)))
1891 (kill-buffer (current-buffer))
1892 ;; Slight kludge here - the last score file returned should be
1893 ;; the local score file, whether it exists or not. This is so
1894 ;; that any score commands the user enters will go to the right
1895 ;; file, and not end up in some global score file.
1896 (let ((localscore (gnus-score-file-name group)))
1897 (setq ofiles (cons localscore (delete localscore ofiles))))
1898 (nreverse ofiles))))
1900 (defun gnus-score-find-single (group)
1901 "Return list containing the score file for GROUP."
1902 (list (gnus-score-file-name group gnus-adaptive-file-suffix)
1903 (gnus-score-file-name group)))
1905 (defun gnus-score-find-hierarchical (group)
1906 "Return list of score files for GROUP.
1907 This includes the score file for the group and all its parents."
1908 (let ((all (copy-sequence '(nil)))
1910 (while (string-match "\\." group (1+ start))
1911 (setq start (match-beginning 0))
1912 (setq all (cons (substring group 0 start) all)))
1913 (setq all (cons group all))
1915 (mapcar (lambda (newsgroup)
1916 (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
1917 (setq all (nreverse all)))
1918 (mapcar 'gnus-score-file-name all))))
1920 (defvar gnus-score-file-alist-cache nil)
1922 (defun gnus-score-find-alist (group)
1923 "Return list of score files for GROUP.
1924 The list is determined from the variable gnus-score-file-alist."
1925 (let ((alist gnus-score-file-multiple-match-alist)
1927 ;; if this group has been seen before, return the cached entry
1928 (if (setq score-files (assoc group gnus-score-file-alist-cache))
1929 (cdr score-files) ;ensures caching groups with no matches
1930 ;; handle the multiple match alist
1932 (and (string-match (car (car alist)) group)
1934 (nconc score-files (copy-sequence (cdr (car alist))))))
1935 (setq alist (cdr alist)))
1936 (setq alist gnus-score-file-single-match-alist)
1937 ;; handle the single match alist
1939 (and (string-match (car (car alist)) group)
1940 ;; progn used just in case ("regexp") has no files
1941 ;; and score-files is still nil. -sj
1942 ;; this can be construed as a "stop searching here" feature :>
1943 ;; and used to simplify regexps in the single-alist
1946 (nconc score-files (copy-sequence (cdr (car alist)))))
1948 (setq alist (cdr alist)))
1949 ;; cache the score files
1950 (setq gnus-score-file-alist-cache
1951 (cons (cons group score-files) gnus-score-file-alist-cache))
1954 (defun gnus-possibly-score-headers (&optional trace)
1955 (let ((func gnus-score-find-score-files-function)
1959 (setq func (list func)))
1960 ;; Go through all the functions for finding score files (or actual
1961 ;; scores) and add them to a list.
1962 (and func (setq score-files (gnus-score-find-alist gnus-newsgroup-name)))
1964 (and (symbolp (car func))
1965 (fboundp (car func))
1967 (nconc score-files (funcall (car func) gnus-newsgroup-name))))
1968 (setq func (cdr func)))
1969 (if score-files (gnus-score-headers score-files trace))))
1971 (defun gnus-score-file-name (newsgroup &optional suffix)
1972 "Return the name of a score file for NEWSGROUP."
1973 (let ((suffix (or suffix gnus-score-file-suffix)))
1975 ((or (null newsgroup)
1976 (string-equal newsgroup ""))
1977 ;; The global score file is placed at top of the directory.
1979 suffix (or gnus-kill-files-directory "~/News")))
1980 ((gnus-use-long-file-name 'not-score)
1981 ;; Append ".SCORE" to newsgroup name.
1982 (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
1984 (or gnus-kill-files-directory "~/News")))
1986 ;; Place "SCORE" under the hierarchical directory.
1987 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
1989 (or gnus-kill-files-directory "~/News"))))))
1991 (defun gnus-score-search-global-directories (files)
1992 "Scan all global score directories for score files."
1993 ;; Set the variable `gnus-internal-global-score-files' to all
1994 ;; available global score files.
1995 (interactive (list gnus-global-score-files))
1998 (if (string-match "/$" (car files))
1999 (setq out (nconc (directory-files
2001 (concat (gnus-score-file-regexp) "$"))))
2002 (setq out (cons (car files) out)))
2003 (setq files (cdr files)))
2004 (setq gnus-internal-global-score-files out)))
2006 (provide 'gnus-score)
2008 ;;; gnus-score.el ends here