*** empty log message ***
[gnus] / lisp / gnus-score.el
1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29
30 (defvar gnus-score-expiry-days 7
31   "*Number of days before unused score file entries are expired.")
32
33 (defvar gnus-orphan-score nil
34   "*All orphans get this score added. Set in the score file.")
35
36 (defvar gnus-default-adaptive-score-alist
37   '((gnus-unread-mark)
38     (gnus-ticked-mark (from 4))
39     (gnus-dormant-mark (from 5))
40     (gnus-del-mark (from -4) (subject -1))
41     (gnus-read-mark (from 4) (subject 2))
42     (gnus-expirable-mark (from -1) (subject -1))
43     (gnus-killed-mark (from -1) (subject -3))
44     (gnus-kill-file-mark)
45     (gnus-catchup-mark (from -1) (subject -1)))
46   "*Alist of marks and scores.")
47
48 (defvar gnus-score-mimic-keymap nil
49   "*Have the score entry functions pretend that they are a keymap.")
50
51 (defvar gnus-score-exact-adapt-limit nil
52   "*Number that says how long a match has to be before using substring matching.
53 When doing adaptive scoring, one normally uses substring matching.
54 However, if the header one matches is short, the possibility for false
55 positives is great, so if the length of the match is less than this
56 variable, exact matching will be used.
57
58 If this variable is nil, which it is by default, exact matching will
59 always be used.")
60
61 \f
62
63 ;; Internal variables.
64
65 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
66
67 (defvar gnus-score-trace nil)
68
69 (defvar gnus-score-alist nil
70   "Alist containing score information.
71 The keys can be symbols or strings.  The following symbols are defined. 
72
73 touched: If this alist has been modified.
74 mark:    Automatically mark articles below this.
75 expunge: Automatically expunge articles below this.
76 files:   List of other SCORE files to load when loading this one.
77 eval:    Sexp to be evaluated when the score file is loaded.
78
79 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...) 
80 where HEADER is the header being scored, MATCH is the string we are
81 looking for, TYPE is a flag indicating whether it should use regexp or
82 substring matching, SCORE is the score to add and DATE is the date
83 of the last succesful match.")
84
85 (defvar gnus-score-cache nil)
86 (defvar gnus-scores-articles nil)
87 (defvar gnus-scores-exclude-files nil)
88 (defvar gnus-header-index nil)
89 (defvar gnus-score-index nil)
90
91 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
92
93 ;;; Summary mode score maps.
94
95 (defvar gnus-summary-score-map nil)
96
97 (define-prefix-command 'gnus-summary-score-map)
98 (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
99 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
100 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
101 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
102 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
103 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
104 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
105 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
106 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
107 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
108 (define-key gnus-summary-score-map "C" 'gnus-score-customize)
109
110
111
112 ;; Summary score file commands
113
114 ;; Much modification of the kill (ahem, score) code and lots of the
115 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
116
117 (defun gnus-summary-lower-score (score)
118   (interactive "P")
119   (gnus-summary-increase-score (- (gnus-score-default score))))
120
121 (defun gnus-summary-increase-score (score)
122   (interactive "P")
123   (gnus-set-global-variables)
124   (let* ((nscore (gnus-score-default score))
125          (prefix (if (< nscore 0) ?L ?I))
126          (increase (> nscore 0))
127          (char-to-header 
128           '((?a "from")
129             (?s "subject")
130             (?b "body" "")
131             (?h "head" "")
132             (?i "message-id" nil t)
133             (?t "references" "message-id" t)
134             (?x "xref")
135             (?l "lines")
136             (?d "date")
137             (?f "followup")))
138          (char-to-type
139           '((?s s "substring") (?e e "exact string") (?f f "fuzzy string")
140             (?r r "regexp string")
141             (?b before "before date") (?a at "at date") (?n now "this date")
142             (?< < "less than number") (?> > "greater than number") 
143             (?= = "equal to number")))
144          (char-to-perm
145           (list (list ?t (current-time-string) "temporary") 
146                 '(?p perm "permanent") '(?i now "immediate")))
147          (mimic gnus-score-mimic-keymap)
148          hchar entry temporary tchar pchar end type)
149     ;; First we read the header to score.
150     (while (not hchar)
151       (if mimic
152           (message "%c-" prefix)
153         (message "%s header (%s?): " (if increase "Increase" "Lower")
154                  (mapconcat (lambda (s) (char-to-string (car s)))
155                             char-to-header "")))
156       (setq hchar (read-char))
157       (if (not (= hchar ??))
158           ()
159         (setq hchar nil)
160         (gnus-score-insert-help "Match on header" char-to-header 1)))
161
162     (and (get-buffer "*Score Help*")
163          (progn
164            (delete-windows-on "*Score Help*")
165            (kill-buffer "*Score Help*")))
166
167     (or (setq entry (assq (downcase hchar) char-to-header))
168         (progn
169           (ding)
170           (setq end t)
171           (if mimic (message "%c %c" prefix hchar) (message ""))))
172     (if (or end (/= (downcase hchar) hchar))
173         (progn
174           ;; This was a majuscle, so we end reading and set the defaults.
175           (if mimic (message "%c %c" prefix hchar) (message ""))
176           (setq type nil
177                 temporary (current-time-string)))
178
179       ;; We continue reading - the type.
180       (while (not tchar)
181         (if mimic
182             (message "%c %c-" prefix hchar)
183           (message "%s header '%s' with match type (%s?): "
184                    (if increase "Increase" "Lower")
185                    (nth 1 entry)
186                    (mapconcat (lambda (s) (char-to-string (car s)))
187                               char-to-type "")))
188         (setq tchar (read-char))
189         (if (not (= tchar ??))
190             ()
191           (setq tchar nil)
192           (gnus-score-insert-help "Match type" char-to-type 2)))
193
194       (and (get-buffer "*Score Help*")
195            (progn
196              (delete-windows-on "*Score Help*")
197              (kill-buffer "*Score Help*")))
198       
199       (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
200           (progn
201             (ding)
202             (if mimic (message "%c %c" prefix hchar) (message ""))
203             (setq end t)))
204       (if (or end (/= (downcase tchar) tchar))
205           (progn
206             ;; It was a majuscle, so we end reading and the the default.
207             (if mimic (message "%c %c %c" prefix hchar tchar)
208               (message ""))
209             (setq temporary (current-time-string)))
210
211         ;; We continue reading.
212         (while (not pchar)
213           (if mimic
214               (message "%c %c %c-" prefix hchar tchar)
215             (message "%s permanence (%s?): " (if increase "Increase" "Lower")
216                      (mapconcat (lambda (s) (char-to-string (car s)))
217                                 char-to-perm "")))
218           (setq pchar (read-char))
219           (if (not (= pchar ??))
220               ()
221             (setq pchar nil)
222             (gnus-score-insert-help "Match permanence" char-to-perm 2)))
223
224         (and (get-buffer "*Score Help*")
225              (progn
226                (delete-windows-on "*Score Help*")
227                (kill-buffer "*Score Help*")))
228
229         (if mimic (message "%c %c %c" prefix hchar tchar pchar)
230           (message ""))
231         (if (setq temporary (nth 1 (assq pchar char-to-perm)))
232             ()
233           (ding)
234           (setq end t)
235           (if mimic 
236               (message "%c %c %c %c" prefix hchar tchar pchar)
237             (message "")))))
238     ;; We have all the data, so we enter this score.
239     (if end
240         ()
241       (gnus-summary-score-entry
242        (nth 1 entry)                    ; Header
243        (gnus-summary-header (or (nth 2 entry) (nth 1 entry))) ; Match
244        type                             ; Type
245        (if (eq 's score) nil score)     ; Score
246        (if (eq 'perm temporary)         ; Temp
247            nil
248          temporary)
249        (not (nth 3 entry)))             ; Prompt
250       )))
251
252 (defun gnus-score-insert-help (string alist idx)
253   (save-excursion
254     (pop-to-buffer "*Score Help*")
255     (buffer-disable-undo (current-buffer))
256     (erase-buffer)
257     (insert string ":\n\n")
258     (while alist
259       (insert (format " %c: %s\n" (car (car alist)) (nth idx (car alist))))
260       (setq alist (cdr alist)))))
261
262 (defun gnus-summary-header (header)
263   ;; Return HEADER for current articles, or error.
264   (let ((article (gnus-summary-article-number)))
265     (if article
266         (aref (gnus-get-header-by-number article)
267               (nth 1 (assoc header gnus-header-index)))
268       (error "No article on current line"))))
269
270 (defun gnus-summary-score-entry 
271   (header match type score date &optional prompt silent)
272   "Enter score file entry.
273 HEADER is the header being scored.
274 MATCH is the string we are looking for.
275 TYPE is a flag indicating if it is a regexp or substring.
276 SCORE is the score to add.
277 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
278 If optional argument `PROMPT' is non-nil, allow user to edit match.
279 If optional argument `SILENT' is nil, show effect of score entry."
280   (interactive
281    (list (completing-read "Header: "
282                           gnus-header-index
283                           (lambda (x) (fboundp (nth 2 x)))
284                           t)
285          (read-string "Match: ")
286          (if (y-or-n-p "Use regexp match? ") 'r 's)
287          (and current-prefix-arg
288              (prefix-numeric-value current-prefix-arg))
289          (cond ((not (y-or-n-p "Add to SCORE file? "))
290                 'now)
291                ((y-or-n-p "Expire kill? ")
292                 (current-time-string))
293                (t nil))))
294   (let ((score (gnus-score-default score))
295         (header (downcase header)))
296     (and prompt (setq match (read-string 
297                              (format "Match %s on %s, %s: " 
298                                      (cond ((eq date 'now)
299                                             "now")
300                                            ((stringp date)
301                                             "temp")
302                                            (t "permanent"))
303                                      header
304                                      (if (< score 0) "lower" "raise"))
305                              match)))
306     (and (>= (nth 1 (assoc header gnus-header-index)) 0)
307          (not silent)
308          (gnus-summary-score-effect header match type score))
309     (if (eq date 'now)
310         ()
311       (and (= score gnus-score-interactive-default-score)
312            (setq score nil))
313       (let ((new (cond ((eq type 'f)
314                     (list (gnus-simplify-subject-fuzzy match)
315                           score (and date (gnus-day-number date)) type))
316                    (type
317                     (list match score (and date (gnus-day-number date)) type))
318                    (date
319                     (list match score (gnus-day-number date)))
320                    (score
321                     (list match score))
322                    (t
323                     (list match))))
324             (old (gnus-score-get header))
325             elem)
326         ;; We see whether we can collapse some score entries.
327         ;; This isn't quite correct, because there may be more elements
328         ;; later on with the same key that have matching elems... Hm.
329         (if (and old
330                  (setq elem (assoc match old))
331                  (eq (nth 3 elem) (nth 3 new))
332                  (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
333                      (and (not (nth 2 elem)) (not (nth 2 new)))))
334             ;; Yup, we just add this new score to the old elem.
335             (setcar (cdr elem) (+ (or (nth 1 elem) 
336                                       gnus-score-interactive-default-score)
337                                   (or (nth 1 new)
338                                       gnus-score-interactive-default-score)))
339           ;; Nope, we have to add a new elem.
340           (gnus-score-set header (if old (cons new old) (list new)))))
341       (gnus-score-set 'touched '(t)))))
342
343 (defun gnus-summary-score-effect (header match type score)
344   "Simulate the effect of a score file entry.
345 HEADER is the header being scored.
346 MATCH is the string we are looking for.
347 TYPE is a flag indicating if it is a regexp or substring.
348 SCORE is the score to add."
349   (interactive (list (completing-read "Header: "
350                                       gnus-header-index
351                                       (lambda (x) (fboundp (nth 2 x)))
352                                       t)
353                      (read-string "Match: ")
354                      (y-or-n-p "Use regexp match? ")
355                      (prefix-numeric-value current-prefix-arg)))
356   (save-excursion
357     (or (and (stringp match) (> (length match) 0))
358       (error "No match"))
359     (goto-char (point-min))
360     (let ((regexp (cond ((eq type 'f)
361                          (gnus-simplify-subject-fuzzy match))
362                         (type match)
363                         (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
364       (while (not (eobp))
365         (let ((content (gnus-summary-header header))
366               (case-fold-search t))
367           (and content
368                (if (if (eq type 'f)
369                        (string-equal (gnus-simplify-subject-fuzzy content)
370                                      regexp)
371                      (string-match regexp content))
372                    (gnus-summary-raise-score score))))
373         (beginning-of-line 2)))))
374
375 (defun gnus-summary-score-crossposting (score date)
376    ;; Enter score file entry for current crossposting.
377    ;; SCORE is the score to add.
378    ;; DATE is the expire date.
379    (let ((xref (gnus-summary-header "xref"))
380          (start 0)
381          group)
382      (or xref (error "This article is not crossposted"))
383      (while (string-match " \\([^ \t]+\\):" xref start)
384        (setq start (match-end 0))
385        (if (not (string= 
386                  (setq group 
387                        (substring xref (match-beginning 1) (match-end 1)))
388                  gnus-newsgroup-name))
389            (gnus-summary-score-entry
390             "xref" (concat " " group ":") nil score date t)))))
391
392 \f
393 ;;;
394 ;;; Gnus Score Files
395 ;;;
396
397 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
398
399 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
400 (defun gnus-score-set-mark-below (score)
401   "Automatically mark articles with score below SCORE as read."
402   (interactive 
403    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
404              (string-to-int (read-string "Mark below: ")))))
405   (setq score (or score gnus-summary-default-score 0))
406   (gnus-score-set 'mark (list score))
407   (gnus-score-set 'touched '(t))
408   (setq gnus-summary-mark-below score)
409   (gnus-summary-update-lines))
410
411 (defun gnus-score-set-expunge-below (score)
412   "Automatically expunge articles with score below SCORE."
413   (interactive 
414    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
415              (string-to-int (read-string "Expunge below: ")))))
416   (setq score (or score gnus-summary-default-score 0))
417   (gnus-score-set 'expunge (list score))
418   (gnus-score-set 'touched '(t)))
419
420 (defun gnus-score-set (symbol value &optional alist)
421   ;; Set SYMBOL to VALUE in ALIST.
422   (let* ((alist 
423           (or alist 
424               gnus-score-alist
425               (progn
426                 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
427                 gnus-score-alist)))
428          (entry (assoc symbol alist)))
429     (cond ((gnus-score-get 'read-only alist)
430            ;; This is a read-only score file, so we do nothing.
431            )
432           (entry
433            (setcdr entry value))
434           ((null alist)
435            (error "Empty alist"))
436           (t
437            (setcdr alist
438                    (cons (cons symbol value) (cdr alist)))))))
439
440 (defun gnus-score-get (symbol &optional alist)
441   ;; Get SYMBOL's definition in ALIST.
442   (cdr (assoc symbol 
443               (or alist 
444                   gnus-score-alist
445                   (progn
446                     (gnus-score-load 
447                      (gnus-score-file-name gnus-newsgroup-name))
448                     gnus-score-alist)))))
449
450 (defun gnus-score-change-score-file (file)
451   "Change current score alist."
452   (interactive (list (completing-read "Score file: " gnus-score-cache)))
453   (gnus-score-load-file file)
454   (gnus-set-mode-line 'summary))
455
456 (defun gnus-score-edit-alist (file)
457   "Edit the current score alist."
458   (interactive (list gnus-current-score-file))
459   (let ((winconf (current-window-configuration)))
460     (and (buffer-name gnus-summary-buffer) (gnus-score-save))
461     (gnus-configure-windows 'article)
462     (pop-to-buffer (find-file-noselect file))
463     (gnus-score-mode)
464     (make-local-variable 'gnus-prev-winconf)
465     (setq gnus-prev-winconf winconf))
466   (gnus-message 
467    4 (substitute-command-keys 
468       "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
469   
470 (defun gnus-score-edit-file (file)
471   "Edit a score file."
472   (interactive 
473    (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
474   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
475   (let ((winconf (current-window-configuration)))
476     (gnus-configure-windows 'article)
477     (pop-to-buffer (find-file-noselect file))
478     (gnus-score-mode)
479     (make-local-variable 'gnus-prev-winconf)
480     (setq gnus-prev-winconf winconf))
481   (gnus-message 
482    4 (substitute-command-keys 
483       "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
484   
485 (defun gnus-score-load-file (file)
486   ;; Load score file FILE.  Returns a list a retrieved score-alists.
487   (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
488   (let* ((file (expand-file-name 
489                 (or (and (string-match
490                           (concat "^" (expand-file-name
491                                        gnus-kill-files-directory)) 
492                           (expand-file-name file))
493                          file)
494                     (concat gnus-kill-files-directory file))))
495          (cached (assoc file gnus-score-cache))
496          (global (member file gnus-internal-global-score-files))
497          lists alist)
498     (if cached
499         ;; The score file was already loaded.
500         (setq alist (cdr cached))
501       ;; We load the score file.
502       (setq gnus-score-alist nil)
503       (setq alist (gnus-score-load-score-alist file))
504       ;; We add '(touched) to the alist to signify that it hasn't been
505       ;; touched (yet). 
506       (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
507       ;; If it is a global score file, we make it read-only.
508       (and global
509            (not (assq 'read-only alist))
510            (setq alist (cons (list 'read-only t) alist)))
511       ;; Update cache.
512       (setq gnus-score-cache
513             (cons (cons file alist) gnus-score-cache)))
514     ;; If there are actual scores in the alist, we add it to the
515     ;; return value of this function.
516     (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
517         (setq lists (list alist)))
518     ;; Treat the other possible atoms in the score alist.
519     (let ((mark (car (gnus-score-get 'mark alist)))
520           (expunge (car (gnus-score-get 'expunge alist)))
521           (mark-and-expunge 
522            (car (gnus-score-get 'mark-and-expunge alist)))
523           (read-only (gnus-score-get 'read-only alist))
524           (files (gnus-score-get 'files alist))
525           (exclude-files (gnus-score-get 'exclude-files alist))
526           (orphan (car (gnus-score-get 'orphan alist)))
527           (adapt (gnus-score-get 'adapt alist))
528           (local (gnus-score-get 'local alist))
529           (eval (car (gnus-score-get 'eval alist))))
530       ;; We do not respect eval and files atoms from global score
531       ;; files. 
532       (and files (not global)
533            (setq lists (apply 'append lists
534                               (mapcar (lambda (file)
535                                         (gnus-score-load-file file)) 
536                                       files))))
537       (and eval (not global) (eval eval))
538       (setq gnus-scores-exclude-files exclude-files)
539       (if (not local)
540           ()
541         (save-excursion
542           (set-buffer gnus-summary-buffer)
543           (while local
544             (and (consp (car local))
545                  (symbolp (car (car local)))
546                  (progn
547                    (make-local-variable (car (car local)))
548                    (set (car (car local)) (nth 1 (car local)))))
549             (setq local (cdr local)))))
550       (if orphan (setq gnus-orphan-score orphan))
551       (setq gnus-adaptive-score-alist
552             (cond ((equal adapt '(t))
553                    (setq gnus-newsgroup-adaptive t)
554                    gnus-default-adaptive-score-alist)
555                   ((equal adapt '(ignore))
556                    (setq gnus-newsgroup-adaptive nil))
557                   ((consp adapt)
558                    (setq gnus-newsgroup-adaptive t)
559                    adapt)
560                   (t
561                    (setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
562                    gnus-default-adaptive-score-alist)))
563       (setq gnus-summary-mark-below 
564             (or mark mark-and-expunge gnus-summary-mark-below))
565       (setq gnus-summary-expunge-below 
566             (or expunge mark-and-expunge gnus-summary-expunge-below)))
567     (setq gnus-current-score-file file)
568     (setq gnus-score-alist alist)
569     lists))
570
571 (defun gnus-score-load (file)
572   ;; Load score FILE.
573   (let ((cache (assoc file gnus-score-cache)))
574     (if cache
575         (setq gnus-score-alist (cdr cache))
576       (setq gnus-score-alist nil)
577       (gnus-score-load-score-alist file)
578       (or gnus-score-alist
579           (setq gnus-score-alist (copy-alist '((touched nil)))))
580       (setq gnus-score-cache
581             (cons (cons file gnus-score-alist) gnus-score-cache)))))
582
583 (defun gnus-score-remove-from-cache (file)
584   (setq gnus-score-cache 
585         (delq (assoc file gnus-score-cache) gnus-score-cache)))
586
587 (defun gnus-score-load-score-alist (file)
588   (let (alist)
589     (if (file-readable-p file)
590         (progn
591           (save-excursion
592             (gnus-set-work-buffer)
593             (insert-file-contents file)
594             (goto-char (point-min))
595             ;; Only do the loading if the score file isn't empty.
596             (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
597                 (setq alist
598                       (condition-case ()
599                           (read (current-buffer))
600                         (error 
601                          (progn
602                            (gnus-message 3 "Problem with score file %s" file)
603                            (ding) 
604                            (sit-for 2)
605                            nil))))))
606           (if (eq (car alist) 'setq)
607               (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
608             (setq gnus-score-alist alist))
609           (setq gnus-score-alist
610                 (gnus-score-check-syntax gnus-score-alist file)))
611       (setq gnus-score-alist nil))))
612
613 (defun gnus-score-check-syntax (alist file)
614   (cond 
615    ((null alist)
616     nil)
617    ((not (consp alist))
618     (gnus-message 1 "Score file is not a list: %s" file)
619     (ding)
620     nil)
621    (t
622     (let ((a alist)
623           err)
624       (while (and a (not err))
625         (cond ((not (listp (car a)))
626                (gnus-message 3 "Illegal score element %s in %s" (car a) file)
627                (setq err t))
628               ((and (stringp (car (car a)))
629                     (not (listp (nth 1 (car a)))))
630                (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
631                (setq err t))
632               (t
633                (setq a (cdr a)))))
634       (if err
635           (progn
636             (ding)
637             nil)
638         alist)))))    
639
640 (defun gnus-score-transform-old-to-new (alist)
641   (let* ((alist (nth 2 alist))
642          out entry)
643     (if (eq (car alist) 'quote)
644         (setq alist (nth 1 alist)))
645     (while alist
646       (setq entry (car alist))
647       (if (stringp (car entry))
648           (let ((scor (cdr entry)))
649             (setq out (cons entry out))
650             (while scor
651               (setcar scor
652                       (list (car (car scor)) (nth 2 (car scor))
653                             (and (nth 3 (car scor))
654                                  (gnus-day-number (nth 3 (car scor))))
655                             (if (nth 1 (car scor)) 'r 's)))
656               (setq scor (cdr scor))))
657         (setq out (cons (if (not (listp (cdr entry))) 
658                             (list (car entry) (cdr entry))
659                           entry)
660                         out)))
661       (setq alist (cdr alist)))
662     (cons (list 'touched t) (nreverse out))))
663   
664 (defun gnus-score-save ()
665   ;; Save all SCORE information.
666   (let ((cache gnus-score-cache))
667     (save-excursion
668       (setq gnus-score-alist nil)
669       (set-buffer (get-buffer-create "*Score*"))
670       (buffer-disable-undo (current-buffer))
671       (let (entry score file)
672         (while cache
673           (setq entry (car cache)
674                 cache (cdr cache)
675                 file (car entry)
676                 score (cdr entry))
677           (if (or (not (equal (gnus-score-get 'touched score) '(t)))
678                   (gnus-score-get 'read-only score)
679                   (and (file-exists-p file)
680                        (not (file-writable-p file))))
681               ()
682             (setq score (setcdr entry (delq (assq 'touched score) score)))
683             (erase-buffer)
684             (let (emacs-lisp-mode-hook)
685               (if (string-match (concat gnus-adaptive-file-suffix "$") file)
686                   ;; This is an adaptive score file, so we do not run
687                   ;; it through `pp'.  These files can get huge, and
688                   ;; are not meant to be edited by human hands.
689                   (insert (format "%S" score))
690                 ;; This is a normal score file, so we print it very
691                 ;; prettily. 
692                 (pp score (current-buffer))))
693             (gnus-make-directory (file-name-directory file))
694             (write-region (point-min) (point-max) file nil 'silent))))
695       (kill-buffer (current-buffer)))))
696   
697 (defun gnus-score-headers (score-files &optional trace)
698   ;; Score `gnus-newsgroup-headers'.
699   (let (scores)
700     ;; PLM: probably this is not the best place to clear orphan-score
701     (setq gnus-orphan-score nil)
702     ;; Load the SCORE files.
703     (while score-files
704       (if (stringp (car score-files))
705           ;; It is a string, which means that it's a score file name,
706           ;; so we load the score file and add the score alist to
707           ;; the list of alists.
708           (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
709         ;; It is an alist, so we just add it to the list directly.
710         (setq scores (nconc (car score-files) scores)))
711       (setq score-files (cdr score-files)))
712     ;; Prune the score files that are to be excluded, if any.
713     (if (not gnus-scores-exclude-files)
714         ()
715       (let ((s scores)
716             c)
717         (while s
718           (and (setq c (rassq (car s) gnus-score-cache))
719                (member (car c) gnus-scores-exclude-files)
720                (setq scores (delq (car s) scores)))
721           (setq s (cdr s)))))
722     (if (not (and gnus-summary-default-score
723                   scores
724                   (> (length gnus-newsgroup-headers)
725                      (length gnus-newsgroup-scored))))
726         ()
727       (let* ((entries gnus-header-index)
728              (now (gnus-day-number (current-time-string)))
729              (expire (- now gnus-score-expiry-days))
730              (headers gnus-newsgroup-headers)
731              (current-score-file gnus-current-score-file)
732              entry header)
733         (gnus-message 5 "Scoring...")
734         ;; Create articles, an alist of the form `(HEADER . SCORE)'.
735         (while headers
736           (setq header (car headers)
737                 headers (cdr headers))
738           ;; WARNING: The assq makes the function O(N*S) while it could
739           ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
740           ;; and S is (length gnus-newsgroup-scored).
741           (or (assq (header-number header) gnus-newsgroup-scored)
742               (setq gnus-scores-articles       ;Total of 2 * N cons-cells used.
743                     (cons (cons header (or gnus-summary-default-score 0))
744                           gnus-scores-articles))))
745
746         (save-excursion
747           (set-buffer (get-buffer-create "*Headers*"))
748           (buffer-disable-undo (current-buffer))
749
750           ;; Set the global variant of this variable.
751           (setq gnus-current-score-file current-score-file)
752           ;; score orphans
753           (if gnus-orphan-score 
754               (progn
755                 (setq gnus-score-index 
756                       (nth 1 (assoc "references" gnus-header-index)))
757                 (gnus-score-orphans gnus-orphan-score)))
758           ;; Run each header through the score process.
759           (while entries
760             (setq entry (car entries)
761                   header (nth 0 entry)
762                   entries (cdr entries))
763             (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
764             (if (< 0 (apply 'max (mapcar
765                                   (lambda (score)
766                                     (length (gnus-score-get header score)))
767                                   scores)))
768                 (funcall (nth 2 entry) scores header now expire trace)))
769           ;; Remove the buffer.
770           (kill-buffer (current-buffer)))
771
772         ;; Add articles to `gnus-newsgroup-scored'.
773         (while gnus-scores-articles
774           (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
775               (setq gnus-newsgroup-scored
776                     (cons (cons (header-number 
777                                  (car (car gnus-scores-articles)))
778                                 (cdr (car gnus-scores-articles)))
779                           gnus-newsgroup-scored)))
780           (setq gnus-scores-articles (cdr gnus-scores-articles)))
781
782         (gnus-message 5 "Scoring...done")))))
783
784
785 (defun gnus-get-new-thread-ids (articles)
786   (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
787         (refind gnus-score-index)
788         id-list art this tref)
789     (while articles
790       (setq art (car articles)
791             this (aref (car art) index)
792             tref (aref (car art) refind)
793             articles (cdr articles))
794       (if (string-equal tref "")        ;no references line
795           (setq id-list (cons this id-list))))
796     id-list))
797
798 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
799 (defun gnus-score-orphans (score)
800   (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
801         (index (nth 1 (assoc "references" gnus-header-index)))
802         alike articles art arts this last this-id)
803     
804     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
805           articles gnus-scores-articles)
806
807     ;;more or less the same as in gnus-score-string
808     (erase-buffer)
809     (while articles
810       (setq art (car articles)
811             this (aref (car art) gnus-score-index)
812             articles (cdr articles))
813       ;;completely skip if this is empty (not a child, so not an orphan)
814       (if (not (string= this ""))
815           (if (equal last this)
816               ;; O(N*H) cons-cells used here, where H is the number of
817               ;; headers.
818               (setq alike (cons art alike))
819             (if last
820                 (progn
821                   ;; Insert the line, with a text property on the
822                   ;; terminating newline refering to the articles with
823                   ;; this line.
824                   (insert last ?\n)
825                   (put-text-property (1- (point)) (point) 'articles alike)))
826             (setq alike (list art)
827                   last this))))
828     (and last                           ; Bwadr, duplicate code.
829          (progn
830            (insert last ?\n)                    
831            (put-text-property (1- (point)) (point) 'articles alike)))
832
833     ;; PLM: now delete those lines that contain an entry from new-thread-ids
834     (while new-thread-ids
835       (setq this-id (car new-thread-ids)
836             new-thread-ids (cdr new-thread-ids))
837       (goto-char (point-min))
838       (while (search-forward this-id nil t)
839         ;; found a match. remove this line
840         (beginning-of-line)
841         (kill-line 1)))
842
843     ;; now for each line: update its articles with score by moving to
844     ;; every end-of-line in the buffer and read the articles property
845     (goto-char (point-min))
846     (while (eq 0 (progn
847                    (end-of-line)
848                    (setq arts (get-text-property (point) 'articles))
849                    (while arts
850                      (setq art (car arts)
851                            arts (cdr arts))
852                      (setcdr art (+ score (cdr art))))
853                    (forward-line))))))
854              
855
856 (defun gnus-score-integer (scores header now expire &optional trace)
857   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
858         alike last this art entries alist articles)
859
860     ;; Find matches.
861     (while scores
862       (setq alist (car scores)
863             scores (cdr scores)
864             entries (assoc header alist))
865       (while (cdr entries)              ;First entry is the header index.
866         (let* ((rest (cdr entries))             
867                (kill (car rest))
868                (match (nth 0 kill))
869                (type (or (nth 3 kill) '>))
870                (score (or (nth 1 kill) gnus-score-interactive-default-score))
871                (date (nth 2 kill))
872                (found nil)
873                (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
874                                    (eq type '>=) (eq type '=))
875                                type
876                              (error "Illegal match type: %s" type)))
877                (articles gnus-scores-articles)
878                arts art)
879           ;; Instead of doing all the clever stuff that
880           ;; `gnus-score-string' does to minimize searches and stuff,
881           ;; I will assume that people generally will put so few
882           ;; matches on numbers that any cleverness will take more
883           ;; time than one would gain.
884           (while articles
885             (and (funcall match-func 
886                           (or (aref (car (car articles)) gnus-score-index) 0)
887                           match)
888                  (progn
889                    (and trace (setq gnus-score-trace 
890                                     (cons (cons (car (car articles)) kill)
891                                           gnus-score-trace)))
892                    (setq found t)
893                    (setcdr (car articles) (+ score (cdr (car articles))))))
894             (setq articles (cdr articles)))
895           ;; Update expire date
896           (cond ((null date))           ;Permanent entry.
897                 (found                  ;Match, update date.
898                  (gnus-score-set 'touched '(t) alist)
899                  (setcar (nthcdr 2 kill) now))
900                 ((< date expire) ;Old entry, remove.
901                  (gnus-score-set 'touched '(t) alist)
902                  (setcdr entries (cdr rest))
903                  (setq rest entries)))
904           (setq entries rest))))))
905
906 (defun gnus-score-date (scores header now expire &optional trace)
907   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
908         alike last this art entries alist articles)
909
910     ;; Find matches.
911     (while scores
912       (setq alist (car scores)
913             scores (cdr scores)
914             entries (assoc header alist))
915       (while (cdr entries)              ;First entry is the header index.
916         (let* ((rest (cdr entries))             
917                (kill (car rest))
918                (match (timezone-make-date-sortable (nth 0 kill)))
919                (type (or (nth 3 kill) 'before))
920                (score (or (nth 1 kill) gnus-score-interactive-default-score))
921                (date (nth 2 kill))
922                (found nil)
923                (match-func 
924                 (cond ((eq type 'after) 'string<)
925                       ((eq type 'before) 'gnus-string>)
926                       ((eq type 'at) 'string=)
927                       (t (error "Illegal match type: %s" type))))
928                (articles gnus-scores-articles)
929                arts art l)
930           ;; Instead of doing all the clever stuff that
931           ;; `gnus-score-string' does to minimize searches and stuff,
932           ;; I will assume that people generally will put so few
933           ;; matches on numbers that any cleverness will take more
934           ;; time than one would gain.
935           (while articles
936             (and
937              (setq l (aref (car (car articles)) gnus-score-index))
938              (funcall match-func match (timezone-make-date-sortable l))
939              (progn
940                (and trace (setq gnus-score-trace 
941                                 (cons (cons (car (car articles)) kill)
942                                       gnus-score-trace)))
943                (setq found t)
944                (setcdr (car articles) (+ score (cdr (car articles))))))
945             (setq articles (cdr articles)))
946           ;; Update expire date
947           (cond ((null date))           ;Permanent entry.
948                 (found                  ;Match, update date.
949                  (gnus-score-set 'touched '(t) alist)
950                  (setcar (nthcdr 2 kill) now))
951                 ((< date expire) ;Old entry, remove.
952                  (gnus-score-set 'touched '(t) alist)
953                  (setcdr entries (cdr rest))
954                  (setq rest entries)))
955           (setq entries rest))))))
956
957 (defun gnus-score-body (scores header now expire &optional trace)
958   (save-excursion
959     (set-buffer nntp-server-buffer)
960     (save-restriction
961       (let* ((buffer-read-only nil)
962              (articles gnus-scores-articles)
963              (all-scores scores)
964              (request-func (cond ((string= "head" (downcase header))
965                                   'gnus-request-head)
966                                  ((string= "body" (downcase header))
967                                   'gnus-request-body)
968                                  (t 'gnus-request-article)))
969              alike last this art entries alist ofunc article)
970         ;; Not all backends support partial fetching.  In that case,
971         ;; we just fetch the entire article.
972         (or (gnus-check-backend-function request-func gnus-newsgroup-name)
973             (progn
974               (setq ofunc request-func)
975               (setq request-func 'gnus-request-article)))
976         (while articles
977           (setq article (header-number (car (car articles))))
978           (gnus-message 7 "Scoring on article %s..." article)
979           (if (not (funcall request-func article gnus-newsgroup-name))
980               ()
981             (widen)
982             (goto-char (point-min))
983             ;; If just parts of the article is to be searched, but the
984             ;; backend didn't support partial fetching, we just narrow
985             ;; to the relevant parts.
986             (if ofunc
987                 (if (eq ofunc 'gnus-request-head)
988                     (narrow-to-region
989                      (point)
990                      (or (search-forward "\n\n" nil t) (point-max)))
991                   (narrow-to-region
992                    (or (search-forward "\n\n" nil t) (point))
993                    (point-max))))
994             (setq scores all-scores)
995             ;; Find matches.
996             (while scores
997               (setq alist (car scores)
998                     scores (cdr scores)
999                     entries (assoc header alist))
1000               (while (cdr entries)      ;First entry is the header index.
1001                 (let* ((rest (cdr entries))             
1002                        (kill (car rest))
1003                        (match (nth 0 kill))
1004                        (type (or (nth 3 kill) 's))
1005                        (score (or (nth 1 kill) 
1006                                   gnus-score-interactive-default-score))
1007                        (date (nth 2 kill))
1008                        (found nil)
1009                        (case-fold-search 
1010                         (not (or (eq type 'R) (eq type 'S)
1011                                  (eq type 'Regexp) (eq type 'String))))
1012                        (search-func 
1013                         (cond ((or (eq type 'r) (eq type 'R)
1014                                    (eq type 'regexp) (eq type 'Regexp))
1015                                're-search-forward)
1016                               ((or (eq type 's) (eq type 'S)
1017                                    (eq type 'string) (eq type 'String))
1018                                'search-forward)
1019                               (t
1020                                (error "Illegal match type: %s" type))))
1021                        arts art)
1022                   (goto-char (point-min))
1023                   (if (funcall search-func match nil t)
1024                       ;; Found a match, update scores.
1025                       (progn
1026                         (setcdr (car articles) (+ score (cdr (car articles))))
1027                         (setq found t)
1028                         (and trace (setq gnus-score-trace 
1029                                          (cons (cons (car (car articles)) kill)
1030                                                gnus-score-trace)))))
1031                   ;; Update expire date
1032                   (cond ((null date))   ;Permanent entry.
1033                         (found          ;Match, update date.
1034                          (gnus-score-set 'touched '(t) alist)
1035                          (setcar (nthcdr 2 kill) now))
1036                         ((< date expire) ;Old entry, remove.
1037                          (gnus-score-set 'touched '(t) alist)
1038                          (setcdr entries (cdr rest))
1039                          (setq rest entries)))
1040                   (setq entries rest)))))
1041           (setq articles (cdr articles)))))))
1042
1043
1044
1045 (defun gnus-score-followup (scores header now expire &optional trace)
1046   ;; Insert the unique article headers in the buffer.
1047   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1048         (current-score-file gnus-current-score-file)
1049         ;; gnus-score-index is used as a free variable.
1050         alike last this art entries alist articles)
1051
1052     ;; Change score file to the adaptive score file.  All entries that
1053     ;; this function makes will be put into this file.
1054     (gnus-score-load-file (gnus-score-file-name 
1055                            gnus-newsgroup-name gnus-adaptive-file-suffix))
1056
1057     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1058           articles gnus-scores-articles)
1059
1060     (erase-buffer)
1061     (while articles
1062       (setq art (car articles)
1063             this (aref (car art) gnus-score-index)
1064             articles (cdr articles))
1065       (if (equal last this)
1066           (setq alike (cons art alike))
1067         (if last
1068             (progn
1069               (insert last ?\n)
1070               (put-text-property (1- (point)) (point) 'articles alike)))
1071         (setq alike (list art)
1072               last this)))
1073     (and last                           ; Bwadr, duplicate code.
1074          (progn
1075            (insert last ?\n)                    
1076            (put-text-property (1- (point)) (point) 'articles alike)))
1077   
1078     ;; Find matches.
1079     (while scores
1080       (setq alist (car scores)
1081             scores (cdr scores)
1082             entries (assoc header alist))
1083       (while (cdr entries)              ;First entry is the header index.
1084         (let* ((rest (cdr entries))             
1085                (kill (car rest))
1086                (match (nth 0 kill))
1087                (type (or (nth 3 kill) 's))
1088                (score (or (nth 1 kill) gnus-score-interactive-default-score))
1089                (date (nth 2 kill))
1090                (found nil)
1091                (mt (aref (symbol-name type) 0))
1092                (case-fold-search 
1093                 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1094                (dmt (downcase mt))
1095                (search-func 
1096                 (cond ((= dmt ?r) 're-search-forward)
1097                       ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1098                       (t (error "Illegal match type: %s" type))))
1099                arts art)
1100           (goto-char (point-min))
1101           (if (= dmt ?e)
1102               (while (funcall search-func match nil t)
1103                 (and (= (progn (beginning-of-line) (point))
1104                         (match-beginning 0))
1105                      (= (progn (end-of-line) (point))
1106                         (match-end 0))
1107                      (progn
1108                        (setq found (setq arts (get-text-property 
1109                                                (point) 'articles)))
1110                        ;; Found a match, update scores.
1111                        (while arts
1112                          (setq art (car arts)
1113                                arts (cdr arts))
1114                          (gnus-score-add-followups (car art) score)))))
1115             (while (funcall search-func match nil t)
1116               (end-of-line)
1117               (setq found (setq arts (get-text-property (point) 'articles)))
1118               ;; Found a match, update scores.
1119               (while arts
1120                 (setq art (car arts)
1121                       arts (cdr arts))
1122                 (gnus-score-add-followups (car art) score))))
1123           ;; Update expire date
1124           (cond ((null date))           ;Permanent entry.
1125                 (found                  ;Match, update date.
1126                  (gnus-score-set 'touched '(t) alist)
1127                  (setcar (nthcdr 2 kill) now))
1128                 ((< date expire) ;Old entry, remove.
1129                  (gnus-score-set 'touched '(t) alist)
1130                  (setcdr entries (cdr rest))
1131                  (setq rest entries)))
1132           (setq entries rest))))
1133     ;; We change the score file back to the previous one.
1134     (gnus-score-load-file current-score-file)))
1135
1136 (defun gnus-score-add-followups (header score)
1137   (save-excursion
1138     (set-buffer gnus-summary-buffer)
1139     (let ((id (header-id header))
1140           (score gnus-score-alist)
1141           dont)
1142       ;; Don't enter a score if there already is one.
1143       (while score
1144         (and (equal "references" (car (car score)))
1145              (or (null (nth 3 (car score)))
1146                  (eq 's (nth 3 (car score))))
1147              (progn
1148                (or (assoc id (car score))
1149                    (setq dont t))
1150                (setq score nil)))
1151         (setq score (cdr score)))
1152       (or dont
1153           (gnus-summary-score-entry 
1154            "references" id 's score (current-time-string) nil t)))))
1155
1156
1157 (defun gnus-score-string (score-list header now expire &optional trace)
1158   ;; Score ARTICLES according to HEADER in SCORE-LIST.
1159   ;; Update matches entries to NOW and remove unmatched entried older
1160   ;; than EXPIRE.
1161   
1162   ;; Insert the unique article headers in the buffer.
1163   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1164         ;; gnus-score-index is used as a free variable.
1165         alike last this art entries alist articles scores fuzzy)
1166
1167     ;; Sorting the articles costs os O(N*log N) but will allow us to
1168     ;; only match with each unique header.  Thus the actual matching
1169     ;; will be O(M*U) where M is the number of strings to match with,
1170     ;; and U is the number of unique headers.  It is assumed (but
1171     ;; untested) this will be a net win because of the large constant
1172     ;; factor involved with string matching.
1173     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1174           articles gnus-scores-articles)
1175
1176     (erase-buffer)
1177     (while articles
1178       (setq art (car articles)
1179             this (aref (car art) gnus-score-index)
1180             articles (cdr articles))
1181       (if (equal last this)
1182           ;; O(N*H) cons-cells used here, where H is the number of
1183           ;; headers.
1184           (setq alike (cons art alike))
1185         (if last
1186             (progn
1187               ;; Insert the line, with a text property on the
1188               ;; terminating newline refering to the articles with
1189               ;; this line.
1190               (insert last ?\n)
1191               (put-text-property (1- (point)) (point) 'articles alike)))
1192         (setq alike (list art)
1193               last this)))
1194     (and last                           ; Bwadr, duplicate code.
1195          (progn
1196            (insert last ?\n)                    
1197            (put-text-property (1- (point)) (point) 'articles alike)))
1198   
1199     ;; Find ordinary matches.
1200     (setq scores score-list) 
1201     (while scores
1202       (setq alist (car scores)
1203             scores (cdr scores)
1204             entries (assoc header alist))
1205       (while (cdr entries)              ;First entry is the header index.
1206         (let* ((rest (cdr entries))             
1207                (kill (car rest))
1208                (match (nth 0 kill))
1209                (type (or (nth 3 kill) 's))
1210                (score (or (nth 1 kill) gnus-score-interactive-default-score))
1211                (date (nth 2 kill))
1212                (found nil)
1213                (mt (aref (symbol-name type) 0))
1214                (case-fold-search 
1215                 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1216                (dmt (downcase mt))
1217                (search-func 
1218                 (cond ((= dmt ?r) 're-search-forward)
1219                       ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1220                       (t (error "Illegal match type: %s" type))))
1221                arts art)
1222           (if (= dmt ?f)
1223               (setq fuzzy t)
1224             (goto-char (point-min))
1225             (if (= dmt ?e)
1226                 (while (and (not (eobp)) 
1227                             (funcall search-func match nil t))
1228                   (and (= (progn (beginning-of-line) (point))
1229                           (match-beginning 0))
1230                        (= (progn (end-of-line) (point))
1231                           (match-end 0))
1232                        (progn
1233                          (setq found (setq arts (get-text-property 
1234                                                  (point) 'articles)))
1235                          ;; Found a match, update scores.
1236                          (if trace
1237                              (while arts
1238                                (setq art (car arts)
1239                                      arts (cdr arts))
1240                                (setcdr art (+ score (cdr art)))
1241                                (setq gnus-score-trace 
1242                                      (cons (cons (header-number
1243                                                   (car art)) kill)
1244                                            gnus-score-trace)))
1245                            (while arts
1246                              (setq art (car arts)
1247                                    arts (cdr arts))
1248                              (setcdr art (+ score (cdr art)))))))
1249                   (forward-line 1))
1250               (and (string= match "") (setq match "\n"))
1251               (while (funcall search-func match nil t)
1252                 (end-of-line)
1253                 (setq found (setq arts (get-text-property (point) 'articles)))
1254                 ;; Found a match, update scores.
1255                 (if trace
1256                     (while arts
1257                       (setq art (car arts)
1258                             arts (cdr arts))
1259                       (setcdr art (+ score (cdr art)))
1260                       (setq gnus-score-trace 
1261                             (cons (cons (header-number (car art)) kill)
1262                                   gnus-score-trace)))
1263                   (while arts
1264                     (setq art (car arts)
1265                           arts (cdr arts))
1266                     (setcdr art (+ score (cdr art)))))))
1267             ;; Update expire date
1268             (cond ((null date))         ;Permanent entry.
1269                   (found                ;Match, update date.
1270                    (gnus-score-set 'touched '(t) alist)
1271                    (setcar (nthcdr 2 kill) now))
1272                   ((< date expire)      ;Old entry, remove.
1273                    (gnus-score-set 'touched '(t) alist)
1274                    (setcdr entries (cdr rest))
1275                    (setq rest entries))))
1276           (setq entries rest))))
1277   
1278     ;; Find fuzzy matches.
1279     (setq scores (and fuzzy score-list))
1280     (if fuzzy (gnus-simplify-buffer-fuzzy))
1281     (while scores
1282       (setq alist (car scores)
1283             scores (cdr scores)
1284             entries (assoc header alist))
1285       (while (cdr entries)              ;First entry is the header index.
1286         (let* ((rest (cdr entries))             
1287                (kill (car rest))
1288                (match (nth 0 kill))
1289                (type (or (nth 3 kill) 's))
1290                (score (or (nth 1 kill) gnus-score-interactive-default-score))
1291                (date (nth 2 kill))
1292                (found nil)
1293                (mt (aref (symbol-name type) 0))
1294                (case-fold-search 
1295                 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1296                (dmt (downcase mt))
1297                (search-func 
1298                 (cond ((= dmt ?r) 're-search-forward)
1299                       ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1300                       (t (error "Illegal match type: %s" type))))
1301                arts art)
1302           (if (/= dmt ?f)
1303               ()
1304             (goto-char (point-min))
1305             (while (and (not (eobp)) 
1306                         (funcall search-func match nil t))
1307               (and (= (progn (beginning-of-line) (point))
1308                       (match-beginning 0))
1309                    (= (progn (end-of-line) (point))
1310                       (match-end 0))
1311                    (progn
1312                      (setq found (setq arts (get-text-property 
1313                                              (point) 'articles)))
1314                      ;; Found a match, update scores.
1315                      (while arts
1316                        (setq art (car arts)
1317                              arts (cdr arts))
1318                        (setcdr art (+ score (cdr art))))))
1319               (forward-line 1))
1320             ;; Update expire date
1321             (cond ((null date))         ;Permanent entry.
1322                   (found                ;Match, update date.
1323                    (gnus-score-set 'touched '(t) alist)
1324                    (setcar (nthcdr 2 kill) now))
1325                   ((< date expire)      ;Old entry, remove.
1326                    (gnus-score-set 'touched '(t) alist)
1327                    (setcdr entries (cdr rest))
1328                    (setq rest entries))))
1329           (setq entries rest))))))
1330
1331 (defun gnus-score-string< (a1 a2)
1332   ;; Compare headers in articles A2 and A2.
1333   ;; The header index used is the free variable `gnus-score-index'.
1334   (string-lessp (aref (car a1) gnus-score-index)
1335                 (aref (car a2) gnus-score-index)))
1336
1337 (defun gnus-score-build-cons (article)
1338   ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1339   (cons (header-number (car article)) (cdr article)))
1340
1341 (defconst gnus-header-index
1342   ;; Name to index alist.
1343   '(("number" 0 gnus-score-integer)
1344     ("subject" 1 gnus-score-string)
1345     ("from" 2 gnus-score-string)
1346     ("date" 3 gnus-score-date)
1347     ("message-id" 4 gnus-score-string) 
1348     ("references" 5 gnus-score-string) 
1349     ("chars" 6 gnus-score-integer) 
1350     ("lines" 7 gnus-score-integer) 
1351     ("xref" 8 gnus-score-string)
1352     ("head" -1 gnus-score-body)
1353     ("body" -1 gnus-score-body)
1354     ("all" -1 gnus-score-body)
1355     ("followup" 2 gnus-score-followup)))
1356
1357 (defun gnus-current-score-file-nondirectory (&optional score-file)
1358   (let ((score-file (or score-file gnus-current-score-file)))
1359     (if score-file 
1360         (gnus-short-group-name (file-name-nondirectory score-file))
1361       "none")))
1362
1363 (defun gnus-score-adaptive ()
1364   (save-excursion
1365     (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1366            (alist malist)
1367            (date (current-time-string)) 
1368            elem headers match)
1369       ;; First we transform the adaptive rule alist into something
1370       ;; that's faster to process.
1371       (while malist
1372         (setq elem (car malist))
1373         (if (symbolp (car elem))
1374             (setcar elem (symbol-value (car elem))))
1375         (setq elem (cdr elem))
1376         (while elem
1377           (setcdr (car elem) 
1378                   (cons (symbol-name (car (car elem))) (cdr (car elem))))
1379           (setcar (car elem) 
1380                   (intern 
1381                    (concat "gnus-header-" 
1382                            (downcase (symbol-name (car (car elem)))))))
1383           (setq elem (cdr elem)))
1384         (setq malist (cdr malist)))
1385       ;; We change the score file to the adaptive score file.
1386       (gnus-score-load-file (gnus-score-file-name 
1387                              gnus-newsgroup-name gnus-adaptive-file-suffix))
1388       ;; The we score away.
1389       (goto-char (point-min))
1390       (while (not (eobp))
1391         (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1392         (if (or (not elem)
1393                 (get-text-property (point) 'gnus-pseudo))
1394             ()
1395           (setq headers (gnus-get-header-by-number 
1396                          (gnus-summary-article-number)))
1397           (while (and elem headers)
1398             (setq match (funcall (car (car elem)) headers))
1399             (gnus-summary-score-entry 
1400              (nth 1 (car elem)) match
1401              ;; Whether we use regexp or exact matches are controlled
1402              ;; here.  
1403              (if (or (not gnus-score-exact-adapt-limit)
1404                      (< (length match) gnus-score-exact-adapt-limit))
1405                  'e 's) 
1406              (nth 2 (car elem)) date nil t)
1407             (setq elem (cdr elem))))
1408         (forward-line 1)))))
1409
1410 (defun gnus-score-remove-lines-adaptive (marks)
1411   (save-excursion
1412     (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1413            (alist malist)
1414            (date (current-time-string)) 
1415            (cur-score gnus-current-score-file)
1416            elem headers match)
1417       ;; First we transform the adaptive rule alist into something
1418       ;; that's faster to process.
1419       (while malist
1420         (setq elem (car malist))
1421         (if (symbolp (car elem))
1422             (setcar elem (symbol-value (car elem))))
1423         (setq elem (cdr elem))
1424         (while elem
1425           (setcdr (car elem) 
1426                   (cons (symbol-name (car (car elem))) (cdr (car elem))))
1427           (setcar (car elem) 
1428                   (intern 
1429                    (concat "gnus-header-" 
1430                            (downcase (symbol-name (car (car elem)))))))
1431           (setq elem (cdr elem)))
1432         (setq malist (cdr malist)))
1433       ;; The we score away.
1434       (goto-char (point-min))
1435       ;; We change the score file to the adaptive score file.
1436       (gnus-score-load-file (gnus-score-file-name 
1437                              gnus-newsgroup-name gnus-adaptive-file-suffix))
1438       (while (re-search-forward marks nil t)
1439         (beginning-of-line)
1440         (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1441         (if (or (not elem)
1442                 (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
1443             ()
1444           (setq headers (gnus-get-header-by-number 
1445                          (gnus-summary-article-number)))
1446           (while elem
1447             (setq match (funcall (car (car elem)) headers))
1448             (gnus-summary-score-entry 
1449              (nth 1 (car elem)) match
1450              (if (or (not gnus-score-exact-adapt-limit)
1451                      (< (length match) gnus-score-exact-adapt-limit))
1452                  'e 's) 
1453              (nth 2 (car elem)) date nil t)
1454             (setq elem (cdr elem))))
1455         (delete-region (point) (progn (forward-line 1) (point))))
1456       ;; Switch back to the old score file.
1457       (gnus-score-load-file cur-score))))
1458
1459 ;;;
1460 ;;; Score mode.
1461 ;;;
1462
1463 (defvar gnus-score-mode-map nil)
1464 (defvar gnus-score-mode-hook nil)
1465
1466 (if gnus-score-mode-map
1467     ()
1468   (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1469   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1470   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1471
1472 (defun gnus-score-mode ()
1473   "Mode for editing score files.
1474 This mode is an extended emacs-lisp mode.
1475
1476 \\{gnus-score-mode-map}"
1477   (interactive)
1478   (kill-all-local-variables)
1479   (use-local-map gnus-score-mode-map)
1480   (set-syntax-table emacs-lisp-mode-syntax-table)
1481   (setq major-mode 'gnus-score-mode)
1482   (setq mode-name "Score")
1483   (lisp-mode-variables nil)
1484   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1485
1486 (defun gnus-score-edit-insert-date ()
1487   "Insert date in numerical format."
1488   (interactive)
1489   (insert (int-to-string (gnus-day-number (current-time-string)))))
1490
1491 (defun gnus-score-edit-done ()
1492   "Save the score file and return to the summary buffer."
1493   (interactive)
1494   (let ((bufnam (buffer-file-name (current-buffer)))
1495         (winconf gnus-prev-winconf))
1496     (save-buffer)
1497     (kill-buffer (current-buffer))
1498     (gnus-score-remove-from-cache bufnam)
1499     (gnus-score-load-file bufnam)
1500     (and winconf (set-window-configuration winconf))))
1501
1502 (defun gnus-score-find-trace ()
1503   "Find all score rules applied to this article."
1504   (interactive)
1505   (let ((gnus-newsgroup-headers
1506          (list (gnus-get-header-by-number (gnus-summary-article-number))))
1507         (gnus-newsgroup-scored nil)
1508         (buf (current-buffer))
1509         trace)
1510     (setq gnus-score-trace nil)
1511     (gnus-possibly-score-headers 'trace)
1512     (pop-to-buffer "*Gnus Scores*")
1513     (erase-buffer)
1514     (setq trace gnus-score-trace)
1515     (or trace
1516         (error "No score rules apply to the current article."))
1517     (while trace
1518       (insert (format "%S\n" (cdr (car trace))))
1519       (setq trace (cdr trace)))
1520     (goto-char (point-min))
1521     (pop-to-buffer buf)))
1522   
1523
1524 (provide 'gnus-score)
1525
1526 ;;; gnus-score.el ends here