1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
32 (defvar gnus-global-score-files nil
33 "*List of global score files and directories.
34 Set this variable if you want to use people's score files. One entry
35 for each score file or each score file directory. Gnus will decide
36 by itself what score files are applicable to which group.
38 Say you want to use the single score file
39 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
40 score files in the \"/ftp.some-where:/pub/score\" directory.
42 (setq gnus-global-score-files
43 '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
44 \"/ftp.some-where:/pub/score\"))")
46 (defvar gnus-score-file-single-match-alist nil
47 "*Alist mapping regexps to lists of score files.
48 Each element of this alist should be of the form
49 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
51 If the name of a group is matched by REGEXP, the corresponding scorefiles
52 will be used for that group.
53 The first match found is used, subsequent matching entries are ignored (to
54 use multiple matches, see gnus-score-file-multiple-match-alist).
56 These score files are loaded in addition to any files returned by
57 gnus-score-find-score-files-function (which see).")
59 (defvar gnus-score-file-multiple-match-alist nil
60 "*Alist mapping regexps to lists of score files.
61 Each element of this alist should be of the form
62 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
64 If the name of a group is matched by REGEXP, the corresponding scorefiles
65 will be used for that group.
66 If multiple REGEXPs match a group, the score files corresponding to each
67 match will be used (for only one match to be used, see
68 gnus-score-file-single-match-alist).
70 These score files are loaded in addition to any files returned by
71 gnus-score-find-score-files-function (which see).")
73 (defvar gnus-score-file-suffix "SCORE"
74 "*Suffix of the score files.")
76 (defvar gnus-adaptive-file-suffix "ADAPT"
77 "*Suffix of the adaptive score files.")
79 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
80 "*Function used to find score files.
81 The function will be called with the group name as the argument, and
82 should return a list of score files to apply to that group. The score
83 files do not actually have to exist.
85 Predefined values are:
87 gnus-score-find-single: Only apply the group's own score file.
88 gnus-score-find-hierarchical: Also apply score files from parent groups.
89 gnus-score-find-bnews: Apply score files whose names matches.
91 See the documentation to these functions for more information.
93 This variable can also be a list of functions to be called. Each
94 function should either return a list of score files, or a list of
97 (defvar gnus-score-interactive-default-score 1000
98 "*Scoring commands will raise/lower the score with this number as the default.")
100 (defvar gnus-score-expiry-days 7
101 "*Number of days before unused score file entries are expired.
102 If this variable is nil, no score file entries will be expired.")
104 (defvar gnus-update-score-entry-dates t
105 "*In non-nil, update matching score entry dates.
106 If this variable is nil, then score entries that provide matches
107 will be expired along with non-matching score entries.")
109 (defvar gnus-orphan-score nil
110 "*All orphans get this score added. Set in the score file.")
112 (defvar gnus-default-adaptive-score-alist
113 '((gnus-kill-file-mark)
115 (gnus-read-mark (from 3) (subject 30))
116 (gnus-catchup-mark (subject -10))
117 (gnus-killed-mark (from -1) (subject -20))
118 (gnus-del-mark (from -2) (subject -15)))
119 "*Alist of marks and scores.")
121 (defvar gnus-score-mimic-keymap nil
122 "*Have the score entry functions pretend that they are a keymap.")
124 (defvar gnus-score-exact-adapt-limit 10
125 "*Number that says how long a match has to be before using substring matching.
126 When doing adaptive scoring, one normally uses fuzzy or substring
127 matching. However, if the header one matches is short, the possibility
128 for false positives is great, so if the length of the match is less
129 than this variable, exact matching will be used.
131 If this variable is nil, exact matching will always be used.")
133 (defvar gnus-score-uncacheable-files "ADAPT$"
134 "*All score files that match this regexp will not be cached.")
136 (defvar gnus-score-default-header nil
137 "Default header when entering new scores.
139 Should be one of the following symbols.
152 If nil, the user will be asked for a header.")
154 (defvar gnus-score-default-type nil
155 "Default match type when entering new scores.
157 Should be one of the following symbols.
167 >: greater than number
170 If nil, the user will be asked for a match type.")
172 (defvar gnus-score-default-fold nil
173 "Use case folding for new score file entries iff not nil.")
175 (defvar gnus-score-default-duration nil
176 "Default duration of effect when entering new scores.
178 Should be one of the following symbols.
184 If nil, the user will be asked for a duration.")
186 (defvar gnus-score-after-write-file-function nil
187 "*Function called with the name of the score file just written to disk.")
191 ;; Internal variables.
193 (defvar gnus-internal-global-score-files nil)
194 (defvar gnus-score-file-list nil)
196 (defvar gnus-short-name-score-file-cache nil)
198 (defvar gnus-score-help-winconf nil)
199 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
200 (defvar gnus-score-trace nil)
201 (defvar gnus-score-edit-buffer nil)
203 (defvar gnus-score-alist nil
204 "Alist containing score information.
205 The keys can be symbols or strings. The following symbols are defined.
207 touched: If this alist has been modified.
208 mark: Automatically mark articles below this.
209 expunge: Automatically expunge articles below this.
210 files: List of other score files to load when loading this one.
211 eval: Sexp to be evaluated when the score file is loaded.
213 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
214 where HEADER is the header being scored, MATCH is the string we are
215 looking for, TYPE is a flag indicating whether it should use regexp or
216 substring matching, SCORE is the score to add and DATE is the date
217 of the last successful match.")
219 (defvar gnus-score-cache nil)
220 (defvar gnus-scores-articles nil)
221 (defvar gnus-header-index nil)
222 (defvar gnus-score-index nil)
225 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap))
227 ;;; Summary mode score maps.
230 (gnus-summary-score-map "V" gnus-summary-mode-map)
231 "s" gnus-summary-set-score
232 "a" gnus-summary-score-entry
233 "S" gnus-summary-current-score
234 "c" gnus-score-change-score-file
235 "m" gnus-score-set-mark-below
236 "x" gnus-score-set-expunge-below
237 "R" gnus-summary-rescore
238 "e" gnus-score-edit-alist
239 "f" gnus-score-edit-file
240 "t" gnus-score-find-trace
241 "C" gnus-score-customize)
243 ;; Summary score file commands
245 ;; Much modification of the kill (ahem, score) code and lots of the
246 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
248 (defun gnus-summary-lower-score (&optional score)
249 "Make a score entry based on the current article.
250 The user will be prompted for header to score on, match type,
251 permanence, and the string to be used. The numerical prefix will be
254 (gnus-summary-increase-score (- (gnus-score-default score))))
256 (defvar gnus-score-default-header nil
257 "*The default header to score on when entering a score rule interactively.")
259 (defvar gnus-score-default-type nil
260 "*The default score type to use when entering a score rule interactively.")
262 (defvar gnus-score-default-duration nil
263 "*The default score duration to use on when entering a score rule interactively.")
265 (defun gnus-score-kill-help-buffer ()
266 (when (get-buffer "*Score Help*")
267 (kill-buffer "*Score Help*")
268 (and gnus-score-help-winconf
269 (set-window-configuration gnus-score-help-winconf))))
271 (defun gnus-summary-increase-score (&optional score)
272 "Make a score entry based on the current article.
273 The user will be prompted for header to score on, match type,
274 permanence, and the string to be used. The numerical prefix will be
277 (gnus-set-global-variables)
278 (let* ((nscore (gnus-score-default score))
279 (prefix (if (< nscore 0) ?L ?I))
280 (increase (> nscore 0))
282 '((?a "from" nil nil string)
283 (?s "subject" nil nil string)
284 (?b "body" "" nil body-string)
285 (?h "head" "" nil body-string)
286 (?i "message-id" nil t string)
287 (?t "references" "message-id" nil string)
288 (?x "xref" nil nil string)
289 (?l "lines" nil nil number)
290 (?d "date" nil nil date)
291 (?f "followup" nil nil string)
292 (?T "thread" nil nil string)))
294 '((?s s "substring" string)
295 (?e e "exact string" string)
296 (?f f "fuzzy string" string)
297 (?r r "regexp string" string)
298 (?s s "substring" body-string)
299 (?r s "regexp string" body-string)
300 (?b before "before date" date)
301 (?a at "at date" date)
302 (?n now "this date" date)
303 (?< < "less than number" number)
304 (?> > "greater than number" number)
305 (?= = "equal to number" number)))
307 (list (list ?t (current-time-string) "temporary")
308 '(?p perm "permanent") '(?i now "immediate")))
309 (mimic gnus-score-mimic-keymap)
310 (hchar (and gnus-score-default-header
311 (aref (symbol-name gnus-score-default-header) 0)))
312 (tchar (and gnus-score-default-type
313 (aref (symbol-name gnus-score-default-type) 0)))
314 (pchar (and gnus-score-default-duration
315 (aref (symbol-name gnus-score-default-duration) 0)))
316 entry temporary end type match)
318 ;; First we read the header to score.
323 (message "%c-" prefix))
324 (message "%s header (%s?): " (if increase "Increase" "Lower")
325 (mapconcat (lambda (s) (char-to-string (car s)))
327 (setq hchar (read-char))
328 (when (or (= hchar ??) (= hchar ?\C-h))
330 (gnus-score-insert-help "Match on header" char-to-header 1)))
332 (gnus-score-kill-help-buffer)
333 (unless (setq entry (assq (downcase hchar) char-to-header))
334 (if mimic (error "%c %c" prefix hchar) (error "")))
336 (when (/= (downcase hchar) hchar)
337 ;; This was a majuscle, so we end reading and set the defaults.
338 (if mimic (message "%c %c" prefix hchar) (message ""))
339 (setq tchar (or gnus-score-default-type ?s)
340 pchar (or gnus-score-default-duration ?t)))
342 ;; We continue reading - the type.
346 (sit-for 1) (message "%c %c-" prefix hchar))
347 (message "%s header '%s' with match type (%s?): "
348 (if increase "Increase" "Lower")
350 (mapconcat (lambda (s)
351 (if (eq (nth 4 entry)
353 (char-to-string (car s))
356 (setq tchar (read-char))
357 (when (or (= tchar ??) (= tchar ?\C-h))
359 (gnus-score-insert-help "Match type" char-to-type 2)))
361 (gnus-score-kill-help-buffer)
362 (unless (setq type (nth 1 (assq (downcase tchar) char-to-type)))
363 (if mimic (error "%c %c" prefix hchar) (error "")))
365 (when (/= (downcase tchar) tchar)
366 ;; It was a majuscle, so we end reading and the the default.
367 (if mimic (message "%c %c %c" prefix hchar tchar)
369 (setq pchar (or gnus-score-default-duration ?p)))
371 ;; We continue reading.
375 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
376 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
377 (mapconcat (lambda (s) (char-to-string (car s)))
379 (setq pchar (read-char))
380 (when (or (= pchar ??) (= pchar ?\C-h))
382 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
384 (gnus-score-kill-help-buffer)
385 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
387 (unless (setq temporary (cadr (assq pchar char-to-perm)))
389 (error "%c %c %c %c" prefix hchar tchar pchar)
392 ;; We have all the data, so we enter this score.
393 (setq match (if (string= (nth 2 entry) "") ""
394 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))))
396 ;; Modify the match, perhaps.
398 ((equal (nth 1 entry) "xref")
399 (when (string-match "^Xref: *" match)
400 (setq match (substring match (match-end 0))))
401 (when (string-match "^[^:]* +" match)
402 (setq match (substring match (match-end 0))))))
404 (when (memq type '(r R regexp Regexp))
405 (setq match (regexp-quote match)))
407 (gnus-summary-score-entry
408 (nth 1 entry) ; Header
411 (if (eq 's score) nil score) ; Score
412 (if (eq 'perm temporary) ; Temp
415 (not (nth 3 entry))) ; Prompt
418 (defun gnus-score-insert-help (string alist idx)
419 (setq gnus-score-help-winconf (current-window-configuration))
421 (set-buffer (get-buffer-create "*Score Help*"))
422 (buffer-disable-undo (current-buffer))
423 (delete-windows-on (current-buffer))
425 (insert string ":\n\n")
430 ;; find the longest string to display
432 (setq n (length (nth idx (car list))))
435 (setq list (cdr list)))
436 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
437 (setq n (/ (window-width) max)) ; items per line
438 (setq width (/ (window-width) n)) ; width of each item
439 ;; insert `n' items, each in a field of width `width'
444 (delete-char -1) ; the `\n' takes a char
446 (setq pad (- width 3))
447 (setq format (concat "%c: %-" (int-to-string pad) "s"))
448 (insert (format format (caar alist) (nth idx (car alist))))
449 (setq alist (cdr alist))
451 ;; display ourselves in a small window at the bottom
452 (gnus-appt-select-lowest-window)
454 (pop-to-buffer "*Score Help*")
455 (shrink-window-if-larger-than-buffer)
456 (select-window (get-buffer-window gnus-summary-buffer))))
458 (defun gnus-summary-header (header &optional no-err)
459 ;; Return HEADER for current articles, or error.
460 (let ((article (gnus-summary-article-number))
463 (if (and (setq headers (gnus-summary-article-header article))
465 (aref headers (nth 1 (assoc header gnus-header-index)))
468 (error "Pseudo-articles can't be scored")))
470 (error "No article on current line")
473 (defsubst gnus-score-get (symbol &optional alist)
474 ;; Get SYMBOL's definition in ALIST.
480 (gnus-score-file-name gnus-newsgroup-name))
481 gnus-score-alist)))))
483 (defun gnus-summary-score-entry
484 (header match type score date &optional prompt silent)
485 "Enter score file entry.
486 HEADER is the header being scored.
487 MATCH is the string we are looking for.
488 TYPE is the match type: substring, regexp, exact, fuzzy.
489 SCORE is the score to add.
490 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
491 If optional argument `PROMPT' is non-nil, allow user to edit match.
492 If optional argument `SILENT' is nil, show effect of score entry."
494 (list (completing-read "Header: "
496 (lambda (x) (fboundp (nth 2 x)))
498 (read-string "Match: ")
499 (if (y-or-n-p "Use regexp match? ") 'r 's)
500 (and current-prefix-arg
501 (prefix-numeric-value current-prefix-arg))
502 (cond ((not (y-or-n-p "Add to score file? "))
504 ((y-or-n-p "Expire kill? ")
505 (current-time-string))
507 ;; Regexp is the default type.
508 (if (eq type t) (setq type 'r))
509 ;; Simplify matches...
510 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
511 (setq match (if match (gnus-simplify-subject-re match) "")))
513 (setq match (gnus-simplify-subject-fuzzy match))))
514 (let ((score (gnus-score-default score))
515 (header (downcase header)))
516 (and prompt (setq match (read-string
517 (format "Match %s on %s, %s: "
518 (cond ((eq date 'now)
524 (if (< score 0) "lower" "raise"))
526 (int-to-string match)
529 ;; Score the current buffer.
530 (and (>= (nth 1 (assoc header gnus-header-index)) 0)
531 (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-string)
533 (gnus-summary-score-effect header match type score))
535 ;; If this is an integer comparison, we transform from string to int.
536 (and (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
537 (setq match (string-to-int match)))
539 (unless (eq date 'now)
540 ;; Add the score entry to the score file.
541 (when (= score gnus-score-interactive-default-score)
545 (list match score (and date (gnus-day-number date)) type))
547 (list match score (gnus-day-number date)))
552 (old (gnus-score-get header))
554 ;; We see whether we can collapse some score entries.
555 ;; This isn't quite correct, because there may be more elements
556 ;; later on with the same key that have matching elems... Hm.
558 (setq elem (assoc match old))
559 (eq (nth 3 elem) (nth 3 new))
560 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
561 (and (not (nth 2 elem)) (not (nth 2 new)))))
562 ;; Yup, we just add this new score to the old elem.
563 (setcar (cdr elem) (+ (or (nth 1 elem)
564 gnus-score-interactive-default-score)
566 gnus-score-interactive-default-score)))
567 ;; Nope, we have to add a new elem.
568 (gnus-score-set header (if old (cons new old) (list new))))
569 (gnus-score-set 'touched '(t))
572 (defun gnus-summary-score-effect (header match type score)
573 "Simulate the effect of a score file entry.
574 HEADER is the header being scored.
575 MATCH is the string we are looking for.
576 TYPE is a flag indicating if it is a regexp or substring.
577 SCORE is the score to add."
578 (interactive (list (completing-read "Header: "
580 (lambda (x) (fboundp (nth 2 x)))
582 (read-string "Match: ")
583 (y-or-n-p "Use regexp match? ")
584 (prefix-numeric-value current-prefix-arg)))
586 (or (and (stringp match) (> (length match) 0))
588 (goto-char (point-min))
589 (let ((regexp (cond ((eq type 'f)
590 (gnus-simplify-subject-fuzzy match))
594 (concat "\\`" (regexp-quote match) "\\'"))
596 (regexp-quote match)))))
598 (let ((content (gnus-summary-header header 'noerr))
599 (case-fold-search t))
602 (string-equal (gnus-simplify-subject-fuzzy content)
604 (string-match regexp content))
605 (gnus-summary-raise-score score))))
606 (beginning-of-line 2)))))
608 (defun gnus-summary-score-crossposting (score date)
609 ;; Enter score file entry for current crossposting.
610 ;; SCORE is the score to add.
611 ;; DATE is the expire date.
612 (let ((xref (gnus-summary-header "xref"))
615 (or xref (error "This article is not crossposted"))
616 (while (string-match " \\([^ \t]+\\):" xref start)
617 (setq start (match-end 0))
620 (substring xref (match-beginning 1) (match-end 1)))
621 gnus-newsgroup-name))
622 (gnus-summary-score-entry
623 "xref" (concat " " group ":") nil score date t)))))
630 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
632 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
633 (defun gnus-score-set-mark-below (score)
634 "Automatically mark articles with score below SCORE as read."
636 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
637 (string-to-int (read-string "Mark below: ")))))
638 (setq score (or score gnus-summary-default-score 0))
639 (gnus-score-set 'mark (list score))
640 (gnus-score-set 'touched '(t))
641 (setq gnus-summary-mark-below score)
642 (gnus-score-update-lines))
644 (defun gnus-score-update-lines ()
645 "Update all lines in the summary buffer."
647 (goto-char (point-min))
649 (gnus-summary-update-line)
652 (defun gnus-score-update-all-lines ()
653 "Update all lines in the summary buffer, even the hidden ones."
655 (goto-char (point-min))
658 (when (gnus-summary-show-thread)
659 (push (point) hidden))
660 (gnus-summary-update-line)
662 ;; Re-hide the hidden threads.
664 (goto-char (pop hidden))
665 (gnus-summary-hide-thread)))))
667 (defun gnus-score-set-expunge-below (score)
668 "Automatically expunge articles with score below SCORE."
670 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
671 (string-to-int (read-string "Expunge below: ")))))
672 (setq score (or score gnus-summary-default-score 0))
673 (gnus-score-set 'expunge (list score))
674 (gnus-score-set 'touched '(t)))
676 (defun gnus-score-followup-article (&optional score)
677 "Add SCORE to all followups to the article in the current buffer."
679 (setq score (gnus-score-default score))
682 (goto-char (point-min))
683 (let ((id (mail-fetch-field "message-id")))
685 (gnus-summary-score-entry
686 "references" (concat id "[ \t]*$") 'r
687 score (current-time-string) nil t))))))
689 (defun gnus-score-followup-thread (&optional score)
690 "Add SCORE to all later articles in the thread the current buffer is part of."
692 (setq score (gnus-score-default score))
695 (goto-char (point-min))
696 (let ((id (mail-fetch-field "message-id")))
698 (gnus-summary-score-entry
700 score (current-time-string)))))))
702 (defun gnus-score-set (symbol value &optional alist)
703 ;; Set SYMBOL to VALUE in ALIST.
708 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
710 (entry (assoc symbol alist)))
711 (cond ((gnus-score-get 'read-only alist)
712 ;; This is a read-only score file, so we do nothing.
715 (setcdr entry value))
717 (error "Empty alist"))
720 (cons (cons symbol value) (cdr alist)))))))
722 (defun gnus-score-change-score-file (file)
723 "Change current score alist."
725 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
726 (gnus-score-load-file file)
727 (gnus-set-mode-line 'summary))
729 (defun gnus-score-edit-alist (file)
730 "Edit the current score alist."
731 (interactive (list gnus-current-score-file))
732 (let ((winconf (current-window-configuration)))
733 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
734 (gnus-make-directory (file-name-directory file))
735 (setq gnus-score-edit-buffer (find-file-noselect file))
736 (gnus-configure-windows 'edit-score)
738 (make-local-variable 'gnus-prev-winconf)
739 (setq gnus-prev-winconf winconf))
741 4 (substitute-command-keys
742 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
744 (defun gnus-score-edit-file (file)
747 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
748 (gnus-make-directory (file-name-directory file))
749 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
750 (let ((winconf (current-window-configuration)))
751 (setq gnus-score-edit-buffer (find-file-noselect file))
752 (gnus-configure-windows 'edit-score)
754 (make-local-variable 'gnus-prev-winconf)
755 (setq gnus-prev-winconf winconf))
757 4 (substitute-command-keys
758 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
760 (defun gnus-score-load-file (file)
761 ;; Load score file FILE. Returns a list a retrieved score-alists.
762 (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
763 (let* ((file (expand-file-name
764 (or (and (string-match
765 (concat "^" (expand-file-name
766 gnus-kill-files-directory))
767 (expand-file-name file))
769 (concat gnus-kill-files-directory file))))
770 (cached (assoc file gnus-score-cache))
771 (global (member file gnus-internal-global-score-files))
774 ;; The score file was already loaded.
775 (setq alist (cdr cached))
776 ;; We load the score file.
777 (setq gnus-score-alist nil)
778 (setq alist (gnus-score-load-score-alist file))
779 ;; We add '(touched) to the alist to signify that it hasn't been
781 (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
782 ;; If it is a global score file, we make it read-only.
784 (not (assq 'read-only alist))
785 (setq alist (cons (list 'read-only t) alist)))
786 (setq gnus-score-cache
787 (cons (cons file alist) gnus-score-cache)))
788 ;; If there are actual scores in the alist, we add it to the
789 ;; return value of this function.
790 (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
791 (setq lists (list alist)))
792 ;; Treat the other possible atoms in the score alist.
793 (let ((mark (car (gnus-score-get 'mark alist)))
794 (expunge (car (gnus-score-get 'expunge alist)))
795 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
796 (files (gnus-score-get 'files alist))
797 (exclude-files (gnus-score-get 'exclude-files alist))
798 (orphan (car (gnus-score-get 'orphan alist)))
799 (adapt (gnus-score-get 'adapt alist))
800 (thread-mark-and-expunge
801 (car (gnus-score-get 'thread-mark-and-expunge alist)))
802 (adapt-file (car (gnus-score-get 'adapt-file)))
803 (local (gnus-score-get 'local alist))
804 (eval (car (gnus-score-get 'eval alist))))
805 ;; We do not respect eval and files atoms from global score
807 (and files (not global)
808 (setq lists (apply 'append lists
809 (mapcar (lambda (file)
810 (gnus-score-load-file file))
811 (if adapt-file (cons adapt-file files)
813 (and eval (not global) (eval eval))
814 ;; We then expand any exclude-file directives.
815 (setq gnus-scores-exclude-files
819 (expand-file-name sfile (file-name-directory file)))
820 exclude-files) gnus-scores-exclude-files))
824 (set-buffer gnus-summary-buffer)
826 (and (consp (car local))
827 (symbolp (caar local))
829 (make-local-variable (caar local))
830 (set (caar local) (nth 1 (car local)))))
831 (setq local (cdr local)))))
832 (if orphan (setq gnus-orphan-score orphan))
833 (setq gnus-adaptive-score-alist
834 (cond ((equal adapt '(t))
835 (setq gnus-newsgroup-adaptive t)
836 gnus-default-adaptive-score-alist)
837 ((equal adapt '(ignore))
838 (setq gnus-newsgroup-adaptive nil))
840 (setq gnus-newsgroup-adaptive t)
843 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
844 gnus-default-adaptive-score-alist)))
845 (setq gnus-thread-expunge-below
846 (or thread-mark-and-expunge gnus-thread-expunge-below))
847 (setq gnus-summary-mark-below
848 (or mark mark-and-expunge gnus-summary-mark-below))
849 (setq gnus-summary-expunge-below
850 (or expunge mark-and-expunge gnus-summary-expunge-below))
851 (setq gnus-newsgroup-adaptive-score-file
852 (or adapt-file gnus-newsgroup-adaptive-score-file)))
853 (setq gnus-current-score-file file)
854 (setq gnus-score-alist alist)
857 (defun gnus-score-load (file)
859 (let ((cache (assoc file gnus-score-cache)))
861 (setq gnus-score-alist (cdr cache))
862 (setq gnus-score-alist nil)
863 (gnus-score-load-score-alist file)
865 (setq gnus-score-alist (copy-alist '((touched nil)))))
866 (setq gnus-score-cache
867 (cons (cons file gnus-score-alist) gnus-score-cache)))))
869 (defun gnus-score-remove-from-cache (file)
870 (setq gnus-score-cache
871 (delq (assoc file gnus-score-cache) gnus-score-cache)))
873 (defun gnus-score-load-score-alist (file)
875 (if (not (file-readable-p file))
876 (setq gnus-score-alist nil)
878 (gnus-set-work-buffer)
879 (insert-file-contents file)
880 (goto-char (point-min))
881 ;; Only do the loading if the score file isn't empty.
882 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
885 (read (current-buffer))
888 (gnus-message 3 "Problem with score file %s" file)
892 (if (eq (car alist) 'setq)
893 ;; This is an old-style score file.
894 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
895 (setq gnus-score-alist alist))
896 ;; Check the syntax of the score file.
897 (setq gnus-score-alist
898 (gnus-score-check-syntax gnus-score-alist file)))))
900 (defun gnus-score-check-syntax (alist file)
901 "Check the syntax of the score ALIST."
906 (gnus-message 1 "Score file is not a list: %s" file)
912 (while (and a (not err))
916 ((not (listp (car a)))
917 (format "Illegal score element %s in %s" (car a) file))
920 ((not (listp (setq sr (cdar a))))
921 (format "Illegal header match %s in %s" (nth 1 (car a)) file))
924 (while (and sr (not err))
929 ((if (member (downcase type) '("lines" "chars"))
930 (not (numberp (car s)))
931 (not (stringp (car s))))
932 (format "Illegal match %s in %s" (car s) file))
933 ((and (cadr s) (not (integerp (cadr s))))
934 (format "Non-integer score %s in %s" (cadr s) file))
935 ((and (caddr s) (not (integerp (caddr s))))
936 (format "Non-integer date %s in %s" (caddr s) file))
937 ((and (cadddr s) (not (symbolp (cadddr s))))
938 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
949 (defun gnus-score-transform-old-to-new (alist)
950 (let* ((alist (nth 2 alist))
952 (if (eq (car alist) 'quote)
953 (setq alist (nth 1 alist)))
955 (setq entry (car alist))
956 (if (stringp (car entry))
957 (let ((scor (cdr entry)))
958 (setq out (cons entry out))
961 (list (caar scor) (nth 2 (car scor))
962 (and (nth 3 (car scor))
963 (gnus-day-number (nth 3 (car scor))))
964 (if (nth 1 (car scor)) 'r 's)))
965 (setq scor (cdr scor))))
966 (setq out (cons (if (not (listp (cdr entry)))
967 (list (car entry) (cdr entry))
970 (setq alist (cdr alist)))
971 (cons (list 'touched t) (nreverse out))))
973 (defun gnus-score-save ()
974 ;; Save all score information.
975 (let ((cache gnus-score-cache))
977 (setq gnus-score-alist nil)
978 (set-buffer (get-buffer-create "*Score*"))
979 (buffer-disable-undo (current-buffer))
980 (let (entry score file)
982 (setq entry (car cache)
986 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
987 (gnus-score-get 'read-only score)
988 (and (file-exists-p file)
989 (not (file-writable-p file))))
991 (setq score (setcdr entry (delq (assq 'touched score) score)))
993 (let (emacs-lisp-mode-hook)
994 (if (string-match (concat gnus-adaptive-file-suffix "$") file)
995 ;; This is an adaptive score file, so we do not run
996 ;; it through `pp'. These files can get huge, and
997 ;; are not meant to be edited by human hands.
998 (insert (format "%S" score))
999 ;; This is a normal score file, so we print it very
1001 (pp score (current-buffer))))
1002 (if (not (gnus-make-directory (file-name-directory file)))
1004 ;; If the score file is empty, we delete it.
1005 (if (zerop (buffer-size))
1007 ;; There are scores, so we write the file.
1008 (when (file-writable-p file)
1009 (write-region (point-min) (point-max) file nil 'silent)
1010 (and gnus-score-after-write-file-function
1011 (funcall gnus-score-after-write-file-function file)))))
1012 (and gnus-score-uncacheable-files
1013 (string-match gnus-score-uncacheable-files file)
1014 (gnus-score-remove-from-cache file)))))
1015 (kill-buffer (current-buffer)))))
1017 (defun gnus-score-headers (score-files &optional trace)
1018 ;; Score `gnus-newsgroup-headers'.
1020 ;; PLM: probably this is not the best place to clear orphan-score
1021 (setq gnus-orphan-score nil)
1022 (setq gnus-scores-articles nil)
1023 (setq gnus-scores-exclude-files nil)
1024 ;; Load the score files.
1026 (if (stringp (car score-files))
1027 ;; It is a string, which means that it's a score file name,
1028 ;; so we load the score file and add the score alist to
1029 ;; the list of alists.
1030 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1031 ;; It is an alist, so we just add it to the list directly.
1032 (setq scores (nconc (car score-files) scores)))
1033 (setq score-files (cdr score-files)))
1034 ;; Prune the score files that are to be excluded, if any.
1035 (when gnus-scores-exclude-files
1039 (and (setq c (rassq (car s) gnus-score-cache))
1040 (member (car c) gnus-scores-exclude-files)
1041 (setq scores (delq (car s) scores)))
1048 (when (and gnus-summary-default-score
1050 (> (length gnus-newsgroup-headers)
1051 (length gnus-newsgroup-scored)))
1052 (let* ((entries gnus-header-index)
1053 (now (gnus-day-number (current-time-string)))
1054 (expire (and gnus-score-expiry-days
1055 (- now gnus-score-expiry-days)))
1056 (headers gnus-newsgroup-headers)
1057 (current-score-file gnus-current-score-file)
1059 (gnus-message 5 "Scoring...")
1060 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1061 (while (setq header (pop headers))
1062 ;; WARNING: The assq makes the function O(N*S) while it could
1063 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1064 ;; and S is (length gnus-newsgroup-scored).
1065 (or (assq (mail-header-number header) gnus-newsgroup-scored)
1066 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1067 (cons (cons header (or gnus-summary-default-score 0))
1068 gnus-scores-articles))))
1071 (set-buffer (get-buffer-create "*Headers*"))
1072 (buffer-disable-undo (current-buffer))
1074 ;; Set the global variant of this variable.
1075 (setq gnus-current-score-file current-score-file)
1077 (when gnus-orphan-score
1078 (setq gnus-score-index
1079 (nth 1 (assoc "references" gnus-header-index)))
1080 (gnus-score-orphans gnus-orphan-score))
1081 ;; Run each header through the score process.
1083 (setq entry (car entries)
1084 header (downcase (nth 0 entry))
1085 entries (cdr entries))
1086 (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
1087 (when (< 0 (apply 'max (mapcar
1089 (length (gnus-score-get header score)))
1091 ;; Call the scoring function for this type of "header".
1092 (when (setq new (funcall (nth 2 entry) scores header
1095 ;; Remove the buffer.
1096 (kill-buffer (current-buffer)))
1098 ;; Add articles to `gnus-newsgroup-scored'.
1099 (while gnus-scores-articles
1100 (or (= gnus-summary-default-score (cdar gnus-scores-articles))
1101 (setq gnus-newsgroup-scored
1102 (cons (cons (mail-header-number
1103 (caar gnus-scores-articles))
1104 (cdar gnus-scores-articles))
1105 gnus-newsgroup-scored)))
1106 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1108 (gnus-message 5 "Scoring...done"))))))
1111 (defun gnus-get-new-thread-ids (articles)
1112 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
1113 (refind gnus-score-index)
1114 id-list art this tref)
1116 (setq art (car articles)
1117 this (aref (car art) index)
1118 tref (aref (car art) refind)
1119 articles (cdr articles))
1120 (if (string-equal tref "") ;no references line
1121 (setq id-list (cons this id-list))))
1124 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
1125 (defun gnus-score-orphans (score)
1126 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
1127 alike articles art arts this last this-id)
1129 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1130 articles gnus-scores-articles)
1132 ;;more or less the same as in gnus-score-string
1135 (setq art (car articles)
1136 this (aref (car art) gnus-score-index)
1137 articles (cdr articles))
1138 ;;completely skip if this is empty (not a child, so not an orphan)
1139 (if (not (string= this ""))
1140 (if (equal last this)
1141 ;; O(N*H) cons-cells used here, where H is the number of
1143 (setq alike (cons art alike))
1146 ;; Insert the line, with a text property on the
1147 ;; terminating newline referring to the articles with
1150 (put-text-property (1- (point)) (point) 'articles alike)))
1151 (setq alike (list art)
1153 (and last ; Bwadr, duplicate code.
1156 (put-text-property (1- (point)) (point) 'articles alike)))
1158 ;; PLM: now delete those lines that contain an entry from new-thread-ids
1159 (while new-thread-ids
1160 (setq this-id (car new-thread-ids)
1161 new-thread-ids (cdr new-thread-ids))
1162 (goto-char (point-min))
1163 (while (search-forward this-id nil t)
1164 ;; found a match. remove this line
1168 ;; now for each line: update its articles with score by moving to
1169 ;; every end-of-line in the buffer and read the articles property
1170 (goto-char (point-min))
1173 (setq arts (get-text-property (point) 'articles))
1175 (setq art (car arts)
1177 (setcdr art (+ score (cdr art))))
1181 (defun gnus-score-integer (scores header now expire &optional trace)
1182 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1187 (setq alist (car scores)
1189 entries (assoc header alist))
1190 (while (cdr entries) ;First entry is the header index.
1191 (let* ((rest (cdr entries))
1193 (match (nth 0 kill))
1194 (type (or (nth 3 kill) '>))
1195 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1198 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1199 (eq type '>=) (eq type '=))
1201 (error "Illegal match type: %s" type)))
1202 (articles gnus-scores-articles))
1203 ;; Instead of doing all the clever stuff that
1204 ;; `gnus-score-string' does to minimize searches and stuff,
1205 ;; I will assume that people generally will put so few
1206 ;; matches on numbers that any cleverness will take more
1207 ;; time than one would gain.
1209 (and (funcall match-func
1210 (or (aref (caar articles) gnus-score-index) 0)
1213 (and trace (setq gnus-score-trace
1216 (car-safe (rassq alist gnus-score-cache))
1220 (setcdr (car articles) (+ score (cdar articles)))))
1221 (setq articles (cdr articles)))
1222 ;; Update expire date
1223 (cond ((null date)) ;Permanent entry.
1224 ((and found gnus-update-score-entry-dates) ;Match, update date.
1225 (gnus-score-set 'touched '(t) alist)
1226 (setcar (nthcdr 2 kill) now))
1227 ((and expire (< 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)))))
1234 (defun gnus-score-date (scores header now expire &optional trace)
1235 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1240 (setq alist (car scores)
1242 entries (assoc header alist))
1243 (while (cdr entries) ;First entry is the header index.
1244 (let* ((rest (cdr entries))
1246 (match (timezone-make-date-sortable (nth 0 kill)))
1247 (type (or (nth 3 kill) 'before))
1248 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1252 (cond ((eq type 'after) 'string<)
1253 ((eq type 'before) 'gnus-string>)
1254 ((eq type 'at) 'string=)
1255 (t (error "Illegal match type: %s" type))))
1256 (articles gnus-scores-articles)
1258 ;; Instead of doing all the clever stuff that
1259 ;; `gnus-score-string' does to minimize searches and stuff,
1260 ;; I will assume that people generally will put so few
1261 ;; matches on numbers that any cleverness will take more
1262 ;; time than one would gain.
1265 (setq l (aref (caar articles) gnus-score-index))
1266 (funcall match-func match (timezone-make-date-sortable l))
1268 (and trace (setq gnus-score-trace
1271 (car-safe (rassq alist gnus-score-cache))
1275 (setcdr (car articles) (+ score (cdar articles)))))
1276 (setq articles (cdr articles)))
1277 ;; Update expire date
1278 (cond ((null date)) ;Permanent entry.
1279 ((and found gnus-update-score-entry-dates) ;Match, update date.
1280 (gnus-score-set 'touched '(t) alist)
1281 (setcar (nthcdr 2 kill) now))
1282 ((and expire (< date expire)) ;Old entry, remove.
1283 (gnus-score-set 'touched '(t) alist)
1284 (setcdr entries (cdr rest))
1285 (setq rest entries)))
1286 (setq entries rest)))))
1289 (defun gnus-score-body (scores header now expire &optional trace)
1291 (set-buffer nntp-server-buffer)
1293 (let* ((buffer-read-only nil)
1294 (articles gnus-scores-articles)
1295 (last (mail-header-number (caar gnus-scores-articles)))
1297 (request-func (cond ((string= "head" (downcase header))
1299 ((string= "body" (downcase header))
1301 (t 'gnus-request-article)))
1302 entries alist ofunc article)
1303 ;; Not all backends support partial fetching. In that case,
1304 ;; we just fetch the entire article.
1305 (or (gnus-check-backend-function
1306 (and (string-match "^gnus-" (symbol-name request-func))
1307 (intern (substring (symbol-name request-func)
1309 gnus-newsgroup-name)
1311 (setq ofunc request-func)
1312 (setq request-func 'gnus-request-article)))
1314 (setq article (mail-header-number (caar articles)))
1315 (gnus-message 7 "Scoring on article %s of %s..." article last)
1316 (if (not (funcall request-func article gnus-newsgroup-name))
1319 (goto-char (point-min))
1320 ;; If just parts of the article is to be searched, but the
1321 ;; backend didn't support partial fetching, we just narrow
1322 ;; to the relevant parts.
1324 (if (eq ofunc 'gnus-request-head)
1327 (or (search-forward "\n\n" nil t) (point-max)))
1329 (or (search-forward "\n\n" nil t) (point))
1331 (setq scores all-scores)
1334 (setq alist (car scores)
1336 entries (assoc header alist))
1337 (while (cdr entries) ;First entry is the header index.
1338 (let* ((rest (cdr entries))
1340 (match (nth 0 kill))
1341 (type (or (nth 3 kill) 's))
1342 (score (or (nth 1 kill)
1343 gnus-score-interactive-default-score))
1347 (not (or (eq type 'R) (eq type 'S)
1348 (eq type 'Regexp) (eq type 'String))))
1350 (cond ((or (eq type 'r) (eq type 'R)
1351 (eq type 'regexp) (eq type 'Regexp))
1353 ((or (eq type 's) (eq type 'S)
1354 (eq type 'string) (eq type 'String))
1357 (error "Illegal match type: %s" type)))))
1358 (goto-char (point-min))
1359 (if (funcall search-func match nil t)
1360 ;; Found a match, update scores.
1362 (setcdr (car articles) (+ score (cdar articles)))
1364 (and trace (setq gnus-score-trace
1368 (rassq alist gnus-score-cache))
1370 gnus-score-trace)))))
1371 ;; Update expire date
1373 ((null date)) ;Permanent entry.
1374 ((and found gnus-update-score-entry-dates) ;Match, update date.
1375 (gnus-score-set 'touched '(t) alist)
1376 (setcar (nthcdr 2 kill) now))
1377 ((and expire (< date expire)) ;Old entry, remove.
1378 (gnus-score-set 'touched '(t) alist)
1379 (setcdr entries (cdr rest))
1380 (setq rest entries)))
1381 (setq entries rest)))))
1382 (setq articles (cdr articles))))))
1385 (defun gnus-score-followup (scores header now expire &optional trace thread)
1386 ;; Insert the unique article headers in the buffer.
1387 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1388 (current-score-file gnus-current-score-file)
1390 ;; gnus-score-index is used as a free variable.
1391 alike last this art entries alist articles
1394 ;; Change score file to the adaptive score file. All entries that
1395 ;; this function makes will be put into this file.
1397 (set-buffer gnus-summary-buffer)
1398 (gnus-score-load-file
1399 (or gnus-newsgroup-adaptive-score-file
1400 (gnus-score-file-name
1401 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1403 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1404 articles gnus-scores-articles)
1408 (setq art (car articles)
1409 this (aref (car art) gnus-score-index)
1410 articles (cdr articles))
1411 (if (equal last this)
1412 (setq alike (cons art alike))
1416 (put-text-property (1- (point)) (point) 'articles alike)))
1417 (setq alike (list art)
1419 (and last ; Bwadr, duplicate code.
1422 (put-text-property (1- (point)) (point) 'articles alike)))
1426 (setq alist (car scores)
1428 entries (assoc header alist))
1429 (while (cdr entries) ;First entry is the header index.
1430 (let* ((rest (cdr entries))
1432 (match (nth 0 kill))
1433 (type (or (nth 3 kill) 's))
1434 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1437 (mt (aref (symbol-name type) 0))
1439 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1442 (cond ((= dmt ?r) 're-search-forward)
1443 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1444 (t (error "Illegal match type: %s" type))))
1446 (goto-char (point-min))
1448 (while (funcall search-func match nil t)
1449 (and (= (progn (beginning-of-line) (point))
1450 (match-beginning 0))
1451 (= (progn (end-of-line) (point))
1454 (setq found (setq arts (get-text-property
1455 (point) 'articles)))
1456 ;; Found a match, update scores.
1458 (setq art (car arts)
1460 (gnus-score-add-followups
1461 (car art) score all-scores thread)))))
1462 (while (funcall search-func match nil t)
1464 (setq found (setq arts (get-text-property (point) 'articles)))
1465 ;; Found a match, update scores.
1466 (while (setq art (pop arts))
1467 (when (setq new (gnus-score-add-followups
1468 (car art) score all-scores thread))
1470 ;; Update expire date
1471 (cond ((null date)) ;Permanent entry.
1472 ((and found gnus-update-score-entry-dates) ;Match, update date.
1473 (gnus-score-set 'touched '(t) alist)
1474 (setcar (nthcdr 2 kill) now))
1475 ((and expire (< date expire)) ;Old entry, remove.
1476 (gnus-score-set 'touched '(t) alist)
1477 (setcdr entries (cdr rest))
1478 (setq rest entries)))
1479 (setq entries rest))))
1480 ;; We change the score file back to the previous one.
1482 (set-buffer gnus-summary-buffer)
1483 (gnus-score-load-file current-score-file))
1484 (list (cons "references" news))))
1486 (defun gnus-score-add-followups (header score scores &optional thread)
1487 "Add a score entry to the adapt file."
1489 (set-buffer gnus-summary-buffer)
1490 (let* ((id (mail-header-id header))
1491 (scores (car scores))
1493 ;; Don't enter a score if there already is one.
1494 (while (setq entry (pop scores))
1495 (and (equal "references" (car entry))
1496 (or (null (nth 3 (cadr entry)))
1497 (eq 's (nth 3 (cadr entry))))
1501 (gnus-summary-score-entry
1502 (if thread "thread" "references")
1503 id 's score (current-time-string) nil t)))))
1505 (defun gnus-score-string (score-list header now expire &optional trace)
1506 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1507 ;; Update matching entries to NOW and remove unmatched entries older
1510 ;; Insert the unique article headers in the buffer.
1511 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1512 ;; gnus-score-index is used as a free variable.
1513 alike last this art entries alist articles scores fuzzy)
1515 ;; Sorting the articles costs os O(N*log N) but will allow us to
1516 ;; only match with each unique header. Thus the actual matching
1517 ;; will be O(M*U) where M is the number of strings to match with,
1518 ;; and U is the number of unique headers. It is assumed (but
1519 ;; untested) this will be a net win because of the large constant
1520 ;; factor involved with string matching.
1521 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1522 articles gnus-scores-articles)
1526 (setq art (car articles)
1527 this (aref (car art) gnus-score-index)
1528 articles (cdr articles))
1529 (if (equal last this)
1530 ;; O(N*H) cons-cells used here, where H is the number of
1532 (setq alike (cons art alike))
1535 ;; Insert the line, with a text property on the
1536 ;; terminating newline referring to the articles with
1539 (put-text-property (1- (point)) (point) 'articles alike)))
1540 (setq alike (list art)
1542 (and last ; Bwadr, duplicate code.
1545 (put-text-property (1- (point)) (point) 'articles alike)))
1547 ;; Find ordinary matches.
1548 (setq scores score-list)
1550 (setq alist (car scores)
1552 entries (assoc header alist))
1553 (while (cdr entries) ;First entry is the header index.
1554 (let* ((rest (cdr entries))
1556 (match (nth 0 kill))
1557 (type (or (nth 3 kill) 's))
1558 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1561 (mt (aref (symbol-name type) 0))
1563 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1566 (cond ((= dmt ?r) 're-search-forward)
1567 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1568 (t (error "Illegal match type: %s" type))))
1572 ;; Do non-fuzzy matching.
1573 (goto-char (point-min))
1575 ;; Do exact matching.
1576 (while (and (not (eobp))
1577 (funcall search-func match nil t))
1578 (and (= (progn (beginning-of-line) (point))
1579 (match-beginning 0))
1580 (= (progn (end-of-line) (point))
1583 (setq found (setq arts (get-text-property
1584 (point) 'articles)))
1585 ;; Found a match, update scores.
1588 (setq art (car arts)
1590 (setcdr art (+ score (cdr art)))
1591 (setq gnus-score-trace
1595 (rassq alist gnus-score-cache))
1599 (setq art (car arts)
1601 (setcdr art (+ score (cdr art)))))))
1603 ;; Do regexp and substring matching.
1604 (and (string= match "") (setq match "\n"))
1605 (while (and (not (eobp))
1606 (funcall search-func match nil t))
1607 (goto-char (match-beginning 0))
1609 (setq found (setq arts (get-text-property (point) 'articles)))
1610 ;; Found a match, update scores.
1613 (setq art (pop arts))
1614 (setcdr art (+ score (cdr art)))
1616 (car-safe (rassq alist gnus-score-cache))
1620 (setq art (pop arts))
1621 (setcdr art (+ score (cdr art)))))
1623 ;; Update expire date
1625 ((null date)) ;Permanent entry.
1626 ((and found gnus-update-score-entry-dates) ;Match, update date.
1627 (gnus-score-set 'touched '(t) alist)
1628 (setcar (nthcdr 2 kill) now))
1629 ((and expire (< date expire)) ;Old entry, remove.
1630 (gnus-score-set 'touched '(t) alist)
1631 (setcdr entries (cdr rest))
1632 (setq rest entries))))
1633 (setq entries rest))))
1635 ;; Find fuzzy matches.
1637 (setq scores score-list)
1638 (gnus-simplify-buffer-fuzzy)
1640 (setq alist (car scores)
1642 entries (assoc header alist))
1643 (while (cdr entries) ;First entry is the header index.
1644 (let* ((rest (cdr entries))
1646 (match (nth 0 kill))
1647 (type (or (nth 3 kill) 's))
1648 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1651 (mt (aref (symbol-name type) 0))
1652 (case-fold-search (not (= mt ?F)))
1656 (goto-char (point-min))
1657 (while (and (not (eobp))
1658 (search-forward match nil t))
1659 (when (and (= (progn (beginning-of-line) (point))
1660 (match-beginning 0))
1661 (= (progn (end-of-line) (point))
1663 (setq found (setq arts (get-text-property
1664 (point) 'articles)))
1665 ;; Found a match, update scores.
1668 (setq art (pop arts))
1669 (setcdr art (+ score (cdr art)))
1671 (car-safe (rassq alist gnus-score-cache))
1675 (setq art (pop arts))
1676 (setcdr art (+ score (cdr art))))))
1678 ;; Update expire date
1681 ((null date)) ;Permanent entry.
1682 ((and found gnus-update-score-entry-dates) ;Match, update date.
1683 (gnus-score-set 'touched '(t) alist)
1684 (setcar (nthcdr 2 kill) now))
1685 ((and expire (< date expire)) ;Old entry, remove.
1686 (gnus-score-set 'touched '(t) alist)
1687 (setcdr entries (cdr rest))
1688 (setq rest entries)))))
1689 (setq entries rest))))))
1692 (defun gnus-score-string< (a1 a2)
1693 ;; Compare headers in articles A2 and A2.
1694 ;; The header index used is the free variable `gnus-score-index'.
1695 (string-lessp (aref (car a1) gnus-score-index)
1696 (aref (car a2) gnus-score-index)))
1698 (defun gnus-score-build-cons (article)
1699 ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1700 (cons (mail-header-number (car article)) (cdr article)))
1702 (defconst gnus-header-index
1703 ;; Name to index alist.
1704 '(("number" 0 gnus-score-integer)
1705 ("subject" 1 gnus-score-string)
1706 ("from" 2 gnus-score-string)
1707 ("date" 3 gnus-score-date)
1708 ("message-id" 4 gnus-score-string)
1709 ("references" 5 gnus-score-string)
1710 ("chars" 6 gnus-score-integer)
1711 ("lines" 7 gnus-score-integer)
1712 ("xref" 8 gnus-score-string)
1713 ("head" -1 gnus-score-body)
1714 ("body" -1 gnus-score-body)
1715 ("all" -1 gnus-score-body)
1716 ("followup" 2 gnus-score-followup)
1717 ("thread" 5 gnus-score-thread)))
1719 (defun gnus-current-score-file-nondirectory (&optional score-file)
1720 (let ((score-file (or score-file gnus-current-score-file)))
1722 (gnus-short-group-name (file-name-nondirectory score-file))
1725 (defun gnus-score-adaptive ()
1727 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1729 (date (current-time-string))
1730 (data gnus-newsgroup-data)
1732 ;; First we transform the adaptive rule alist into something
1733 ;; that's faster to process.
1735 (setq elem (car malist))
1736 (if (symbolp (car elem))
1737 (setcar elem (symbol-value (car elem))))
1738 (setq elem (cdr elem))
1741 (cons (if (eq (caar elem) 'followup)
1743 (symbol-name (caar elem)))
1748 (concat "mail-header-"
1749 (if (eq (caar elem) 'followup)
1751 (downcase (symbol-name (caar elem))))))
1753 (setq elem (cdr elem)))
1754 (setq malist (cdr malist)))
1755 ;; We change the score file to the adaptive score file.
1756 (gnus-score-load-file
1757 (or gnus-newsgroup-adaptive-score-file
1758 (gnus-score-file-name
1759 gnus-newsgroup-name gnus-adaptive-file-suffix)))
1760 ;; The we score away.
1762 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
1764 (gnus-data-pseudo-p (car data)))
1766 (when (setq headers (gnus-data-header (car data)))
1768 (setq match (funcall (caar elem) headers))
1769 (gnus-summary-score-entry
1770 (nth 1 (car elem)) match
1774 ((equal (nth 1 (car elem)) "date")
1777 ;; Whether we use substring or exact matches are controlled
1779 (if (or (not gnus-score-exact-adapt-limit)
1780 (< (length match) gnus-score-exact-adapt-limit))
1782 (if (equal (nth 1 (car elem)) "subject")
1784 (nth 2 (car elem)) date nil t)
1785 (setq elem (cdr elem)))))
1786 (setq data (cdr data))))))
1792 (defvar gnus-score-mode-hook nil
1793 "*Hook run in score mode buffers.")
1795 (defvar gnus-score-menu-hook nil
1796 "*Hook run after creating the score mode menu.")
1798 (defvar gnus-score-mode-map nil)
1799 (unless gnus-score-mode-map
1800 (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1803 "\C-c\C-c" gnus-score-edit-done
1804 "\C-c\C-d" gnus-score-edit-insert-date
1805 "\C-c\C-p" gnus-score-pretty-print))
1807 (defun gnus-score-mode ()
1808 "Mode for editing score files.
1809 This mode is an extended emacs-lisp mode.
1811 \\{gnus-score-mode-map}"
1813 (kill-all-local-variables)
1814 (use-local-map gnus-score-mode-map)
1815 (when (and menu-bar-mode
1816 (gnus-visual-p 'score-menu 'menu))
1817 (gnus-score-make-menu-bar))
1818 (set-syntax-table emacs-lisp-mode-syntax-table)
1819 (setq major-mode 'gnus-score-mode)
1820 (setq mode-name "Score")
1821 (lisp-mode-variables nil)
1822 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1824 (defun gnus-score-make-menu-bar ()
1825 (unless (boundp 'gnus-score-menu)
1827 gnus-score-menu gnus-score-mode-map ""
1829 ["Exit" gnus-score-edit-done t]
1830 ["Insert date" gnus-score-edit-insert-date t]
1831 ["Format" gnus-score-pretty-print t]
1833 (run-hooks 'gnus-score-menu-hook)))
1835 (defun gnus-score-edit-insert-date ()
1836 "Insert date in numerical format."
1838 (insert (int-to-string (gnus-day-number (current-time-string)))))
1840 (defun gnus-score-pretty-print ()
1841 "Format the current score file."
1843 (goto-char (point-min))
1844 (let ((form (read (current-buffer))))
1846 (pp form (current-buffer)))
1847 (goto-char (point-min)))
1849 (defun gnus-score-edit-done ()
1850 "Save the score file and return to the summary buffer."
1852 (let ((bufnam (buffer-file-name (current-buffer)))
1853 (winconf gnus-prev-winconf))
1854 (gnus-make-directory (file-name-directory (buffer-file-name)))
1856 (kill-buffer (current-buffer))
1857 (and winconf (set-window-configuration winconf))
1858 (gnus-score-remove-from-cache bufnam)
1859 (gnus-score-load-file bufnam)))
1861 (defun gnus-score-find-trace ()
1862 "Find all score rules that applies to the current article."
1864 (let ((gnus-newsgroup-headers
1865 (list (gnus-summary-article-header)))
1866 (gnus-newsgroup-scored nil)
1867 (buf (current-buffer))
1869 (setq gnus-score-trace nil)
1870 (gnus-possibly-score-headers 'trace)
1871 (or (setq trace gnus-score-trace)
1872 (error "No score rules apply to the current article."))
1873 (pop-to-buffer "*Gnus Scores*")
1874 (gnus-add-current-to-buffer-list)
1877 (insert (format "%S -> %s\n" (cdar trace)
1878 (file-name-nondirectory (caar trace))))
1879 (setq trace (cdr trace)))
1880 (goto-char (point-min))
1881 (pop-to-buffer buf)))
1883 (defun gnus-summary-rescore ()
1884 "Redo the entire scoring process in the current summary."
1886 (setq gnus-newsgroup-scored nil)
1887 (setq gnus-score-cache nil)
1888 (setq gnus-newsgroup-scored nil)
1889 (gnus-possibly-score-headers)
1890 (gnus-score-update-all-lines))
1892 (defun gnus-score-flush-cache ()
1893 "Flush the cache of score files."
1895 (setq gnus-score-cache nil
1896 gnus-short-name-score-file-cache nil)
1897 (gnus-message 6 "The score cache is now flushed"))
1899 (defun gnus-score-close ()
1900 "Clear all internal score variables."
1901 (setq gnus-score-cache nil
1902 gnus-internal-global-score-files nil))
1904 ;; Summary score marking commands.
1906 (defun gnus-summary-raise-same-subject-and-select (score)
1907 "Raise articles which has the same subject with SCORE and select the next."
1909 (let ((subject (gnus-summary-article-subject)))
1910 (gnus-summary-raise-score score)
1911 (while (gnus-summary-find-subject subject)
1912 (gnus-summary-raise-score score))
1913 (gnus-summary-next-article t)))
1915 (defun gnus-summary-raise-same-subject (score)
1916 "Raise articles which has the same subject with SCORE."
1918 (let ((subject (gnus-summary-article-subject)))
1919 (gnus-summary-raise-score score)
1920 (while (gnus-summary-find-subject subject)
1921 (gnus-summary-raise-score score))
1922 (gnus-summary-next-subject 1 t)))
1924 (defun gnus-score-default (level)
1925 (if level (prefix-numeric-value level)
1926 gnus-score-interactive-default-score))
1928 (defun gnus-summary-raise-thread (&optional score)
1929 "Raise the score of the articles in the current thread with SCORE."
1931 (setq score (gnus-score-default score))
1934 (let ((articles (gnus-summary-articles-in-thread)))
1936 (gnus-summary-goto-subject (car articles))
1937 (gnus-summary-raise-score score)
1938 (setq articles (cdr articles))))
1940 (let ((gnus-summary-check-current t))
1941 (or (zerop (gnus-summary-next-subject 1 t))
1943 (gnus-summary-recenter)
1944 (gnus-summary-position-point)
1945 (gnus-set-mode-line 'summary))
1947 (defun gnus-summary-lower-same-subject-and-select (score)
1948 "Raise articles which has the same subject with SCORE and select the next."
1950 (gnus-summary-raise-same-subject-and-select (- score)))
1952 (defun gnus-summary-lower-same-subject (score)
1953 "Raise articles which has the same subject with SCORE."
1955 (gnus-summary-raise-same-subject (- score)))
1957 (defun gnus-summary-lower-thread (&optional score)
1958 "Lower score of articles in the current thread with SCORE."
1960 (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
1962 ;;; Finding score files.
1964 (defun gnus-score-score-files (group)
1965 "Return a list of all possible score files."
1966 ;; Search and set any global score files.
1967 (and gnus-global-score-files
1968 (or gnus-internal-global-score-files
1969 (gnus-score-search-global-directories gnus-global-score-files)))
1970 ;; Fix the kill-file dir variable.
1971 (setq gnus-kill-files-directory
1972 (file-name-as-directory
1973 (or gnus-kill-files-directory "~/News/")))
1974 ;; If we can't read it, there are no score files.
1975 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
1976 (setq gnus-score-file-list nil)
1977 (if (not (gnus-use-long-file-name 'not-score))
1978 ;; We do not use long file names, so we have to do some
1979 ;; directory traversing.
1980 (setq gnus-score-file-list
1982 (or gnus-short-name-score-file-cache
1983 (setq gnus-short-name-score-file-cache
1984 (gnus-score-score-files-1
1985 gnus-kill-files-directory)))))
1986 ;; We want long file names.
1987 (when (or (not gnus-score-file-list)
1988 (not (car gnus-score-file-list))
1989 (gnus-file-newer-than gnus-kill-files-directory
1990 (car gnus-score-file-list)))
1991 (setq gnus-score-file-list
1992 (cons (nth 5 (file-attributes gnus-kill-files-directory))
1995 gnus-kill-files-directory t
1996 (gnus-score-file-regexp)))))))
1997 (cdr gnus-score-file-list)))
1999 (defun gnus-score-score-files-1 (dir)
2000 "Return all possible score files under DIR."
2001 (let ((files (directory-files (expand-file-name dir) t nil t))
2002 (regexp (gnus-score-file-regexp))
2004 (while (setq file (pop files))
2006 ;; Ignore "." and "..".
2007 ((string-match "/\\.\\.?\\'" file)
2009 ;; Recurse down directories.
2010 ((file-directory-p file)
2011 (setq out (nconc (gnus-score-score-files-1 file) out)))
2012 ;; Add files to the list of score files.
2013 ((string-match regexp file)
2017 (defun gnus-score-file-regexp ()
2018 "Return a regexp that match all score files."
2019 (concat "\\(" gnus-score-file-suffix
2020 "\\|" gnus-adaptive-file-suffix "\\)\\'"))
2022 (defun gnus-score-find-bnews (group)
2023 "Return a list of score files for GROUP.
2024 The score files are those files in the ~/News/ directory which matches
2025 GROUP using BNews sys file syntax."
2026 (let* ((sfiles (append (gnus-score-score-files group)
2027 gnus-internal-global-score-files))
2028 (kill-dir (file-name-as-directory
2029 (expand-file-name gnus-kill-files-directory)))
2030 (klen (length kill-dir))
2031 (score-regexp (gnus-score-file-regexp))
2032 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2033 ofiles not-match regexp)
2035 (set-buffer (get-buffer-create "*gnus score files*"))
2036 (buffer-disable-undo (current-buffer))
2037 ;; Go through all score file names and create regexp with them
2041 (insert (car sfiles))
2042 (goto-char (point-min))
2043 ;; First remove the suffix itself.
2044 (when (re-search-forward (concat "." score-regexp) nil t)
2045 (replace-match "" t t)
2046 (goto-char (point-min))
2047 (if (looking-at (regexp-quote kill-dir))
2048 ;; If the file name was just "SCORE", `klen' is one character
2050 (delete-char (min (1- (point-max)) klen))
2051 (goto-char (point-max))
2052 (search-backward "/")
2053 (delete-region (1+ (point)) (point-min)))
2054 ;; If short file names were used, we have to translate slashes.
2055 (goto-char (point-min))
2056 (let ((regexp (concat
2057 "[/:" (if trans (char-to-string trans) "") "]")))
2058 (while (re-search-forward regexp nil t)
2059 (replace-match "." t t)))
2060 ;; Cludge to get rid of "nntp+" problems.
2061 (goto-char (point-min))
2062 (and (looking-at "nn[a-z]+\\+")
2064 (search-forward "+")
2067 ;; Translate "all" to ".*".
2068 (while (search-forward "all" nil t)
2069 (replace-match ".*" t t))
2070 (goto-char (point-min))
2071 ;; Deal with "not."s.
2072 (if (looking-at "not.")
2075 (setq regexp (buffer-substring 5 (point-max))))
2076 (setq regexp (buffer-substring 1 (point-max)))
2077 (setq not-match nil))
2078 ;; Finally - if this resulting regexp matches the group name,
2079 ;; we add this score file to the list of score files
2080 ;; applicable to this group.
2081 (if (or (and not-match
2082 (not (string-match regexp group)))
2083 (and (not not-match)
2084 (string-match regexp group)))
2085 (setq ofiles (cons (car sfiles) ofiles))))
2086 (setq sfiles (cdr sfiles)))
2087 (kill-buffer (current-buffer))
2088 ;; Slight kludge here - the last score file returned should be
2089 ;; the local score file, whether it exists or not. This is so
2090 ;; that any score commands the user enters will go to the right
2091 ;; file, and not end up in some global score file.
2092 (let ((localscore (gnus-score-file-name group)))
2093 (setq ofiles (cons localscore (delete localscore ofiles))))
2094 (nreverse ofiles))))
2096 (defun gnus-score-find-single (group)
2097 "Return list containing the score file for GROUP."
2098 (list (or gnus-newsgroup-adaptive-score-file
2099 (gnus-score-file-name group gnus-adaptive-file-suffix))
2100 (gnus-score-file-name group)))
2102 (defun gnus-score-find-hierarchical (group)
2103 "Return list of score files for GROUP.
2104 This includes the score file for the group and all its parents."
2105 (let ((all (copy-sequence '(nil)))
2107 (while (string-match "\\." group (1+ start))
2108 (setq start (match-beginning 0))
2109 (setq all (cons (substring group 0 start) all)))
2110 (setq all (cons group all))
2112 (mapcar (lambda (newsgroup)
2113 (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
2114 (setq all (nreverse all)))
2115 (mapcar 'gnus-score-file-name all))))
2117 (defvar gnus-score-file-alist-cache nil)
2119 (defun gnus-score-find-alist (group)
2120 "Return list of score files for GROUP.
2121 The list is determined from the variable gnus-score-file-alist."
2122 (let ((alist gnus-score-file-multiple-match-alist)
2124 ;; if this group has been seen before, return the cached entry
2125 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2126 (cdr score-files) ;ensures caching groups with no matches
2127 ;; handle the multiple match alist
2129 (and (string-match (caar alist) group)
2131 (nconc score-files (copy-sequence (cdar alist)))))
2132 (setq alist (cdr alist)))
2133 (setq alist gnus-score-file-single-match-alist)
2134 ;; handle the single match alist
2136 (and (string-match (caar alist) group)
2137 ;; progn used just in case ("regexp") has no files
2138 ;; and score-files is still nil. -sj
2139 ;; this can be construed as a "stop searching here" feature :>
2140 ;; and used to simplify regexps in the single-alist
2143 (nconc score-files (copy-sequence (cdar alist))))
2145 (setq alist (cdr alist)))
2146 ;; cache the score files
2147 (setq gnus-score-file-alist-cache
2148 (cons (cons group score-files) gnus-score-file-alist-cache))
2151 (defun gnus-possibly-score-headers (&optional trace)
2152 (let ((funcs gnus-score-find-score-files-function)
2154 ;; Make sure funcs is a list.
2157 (setq funcs (list funcs)))
2158 ;; Get the initial score files for this group.
2160 (setq score-files (gnus-score-find-alist gnus-newsgroup-name)))
2161 ;; Go through all the functions for finding score files (or actual
2162 ;; scores) and add them to a list.
2164 (when (gnus-functionp (car funcs))
2166 (nconc score-files (funcall (car funcs) gnus-newsgroup-name))))
2167 (setq funcs (cdr funcs)))
2168 ;; Check whether there is a `score-file' group parameter.
2169 (let ((param-file (gnus-group-get-parameter
2170 gnus-newsgroup-name 'score-file)))
2172 (push param-file score-files)))
2173 ;; Do the scoring if there are any score files for this group.
2175 (gnus-score-headers score-files trace))))
2177 (defun gnus-score-file-name (newsgroup &optional suffix)
2178 "Return the name of a score file for NEWSGROUP."
2179 (let ((suffix (or suffix gnus-score-file-suffix)))
2180 (nnheader-translate-file-chars
2182 ((or (null newsgroup)
2183 (string-equal newsgroup ""))
2184 ;; The global score file is placed at top of the directory.
2186 suffix (or gnus-kill-files-directory "~/News")))
2187 ((gnus-use-long-file-name 'not-score)
2188 ;; Append ".SCORE" to newsgroup name.
2189 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2191 (or gnus-kill-files-directory "~/News")))
2193 ;; Place "SCORE" under the hierarchical directory.
2194 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2196 (or gnus-kill-files-directory "~/News")))))))
2198 (defun gnus-score-search-global-directories (files)
2199 "Scan all global score directories for score files."
2200 ;; Set the variable `gnus-internal-global-score-files' to all
2201 ;; available global score files.
2202 (interactive (list gnus-global-score-files))
2205 (if (string-match "/$" (car files))
2206 (setq out (nconc (directory-files
2208 (concat (gnus-score-file-regexp) "$"))))
2209 (setq out (cons (car files) out)))
2210 (setq files (cdr files)))
2211 (setq gnus-internal-global-score-files out)))
2213 (defun gnus-score-default-fold-toggle ()
2214 "Toggle folding for new score file entries."
2216 (setq gnus-score-default-fold (not gnus-score-default-fold))
2217 (if gnus-score-default-fold
2218 (message "New score file entries will be case insensitive.")
2219 (message "New score file entries will be case sensitive.")))
2221 (provide 'gnus-score)
2223 ;;; gnus-score.el ends here