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