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