* erc-stamp.el (erc-echo-timestamp):
[emacs.git] / lisp / gnus / gnus-registry.el
blobd45cc6c5d6d777144349235c17dcb40a2fdcd8d2
1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news
9 ;; This file is part of GNU Emacs.
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 3, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This is the gnus-registry.el package, which works with all
29 ;; backends, not just nnmail (e.g. NNTP). The major issue is that it
30 ;; doesn't go across backends, so for instance if an article is in
31 ;; nnml:sys and you see a reference to it in nnimap splitting, the
32 ;; article will end up in nnimap:sys
34 ;; gnus-registry.el intercepts article respooling, moving, deleting,
35 ;; and copying for all backends. If it doesn't work correctly for
36 ;; you, submit a bug report and I'll be glad to fix it. It needs
37 ;; documentation in the manual (also on my to-do list).
39 ;; Put this in your startup file (~/.gnus.el for instance)
41 ;; (setq gnus-registry-max-entries 2500
42 ;; gnus-registry-use-long-group-names t)
44 ;; (gnus-registry-initialize)
46 ;; Then use this in your fancy-split:
48 ;; (: gnus-registry-split-fancy-with-parent)
50 ;; TODO:
52 ;; - get the correct group on spool actions
54 ;; - articles that are spooled to a different backend should be handled
56 ;;; Code:
58 (eval-when-compile (require 'cl))
60 (require 'gnus)
61 (require 'gnus-int)
62 (require 'gnus-sum)
63 (require 'gnus-util)
64 (require 'nnmail)
66 (defvar gnus-adaptive-word-syntax-table)
68 (defvar gnus-registry-dirty t
69 "Boolean set to t when the registry is modified")
71 (defgroup gnus-registry nil
72 "The Gnus registry."
73 :version "22.1"
74 :group 'gnus)
76 (defvar gnus-registry-hashtb (make-hash-table
77 :size 256
78 :test 'equal)
79 "*The article registry by Message ID.")
81 (defcustom gnus-registry-unfollowed-groups '("delayed$" "drafts$" "queue$" "INBOX$")
82 "List of groups that gnus-registry-split-fancy-with-parent won't return.
83 The group names are matched, they don't have to be fully
84 qualified. This parameter tells the Registry 'never split a
85 message into a group that matches one of these, regardless of
86 references.'"
87 :group 'gnus-registry
88 :type '(repeat regexp))
90 (defcustom gnus-registry-install nil
91 "Whether the registry should be installed."
92 :group 'gnus-registry
93 :type 'boolean)
95 (defcustom gnus-registry-clean-empty t
96 "Whether the empty registry entries should be deleted.
97 Registry entries are considered empty when they have no groups
98 and no extra data."
99 :group 'gnus-registry
100 :type 'boolean)
102 (defcustom gnus-registry-use-long-group-names nil
103 "Whether the registry should use long group names (BUGGY)."
104 :group 'gnus-registry
105 :type 'boolean)
107 (defcustom gnus-registry-track-extra nil
108 "Whether the registry should track extra data about a message.
109 The Subject and Sender (From:) headers are currently tracked this
110 way."
111 :group 'gnus-registry
112 :type
113 '(set :tag "Tracking choices"
114 (const :tag "Track by subject (Subject: header)" subject)
115 (const :tag "Track by sender (From: header)" sender)))
117 (defcustom gnus-registry-entry-caching t
118 "Whether the registry should cache extra information."
119 :group 'gnus-registry
120 :type 'boolean)
122 (defcustom gnus-registry-minimum-subject-length 5
123 "The minimum length of a subject before it's considered trackable."
124 :group 'gnus-registry
125 :type 'integer)
127 (defcustom gnus-registry-trim-articles-without-groups t
128 "Whether the registry should clean out message IDs without groups."
129 :group 'gnus-registry
130 :type 'boolean)
132 (defcustom gnus-registry-cache-file
133 (nnheader-concat
134 (or gnus-dribble-directory gnus-home-directory "~/")
135 ".gnus.registry.eld")
136 "File where the Gnus registry will be stored."
137 :group 'gnus-registry
138 :type 'file)
140 (defcustom gnus-registry-max-entries nil
141 "Maximum number of entries in the registry, nil for unlimited."
142 :group 'gnus-registry
143 :type '(radio (const :format "Unlimited " nil)
144 (integer :format "Maximum number: %v")))
146 (defun gnus-registry-track-subject-p ()
147 (memq 'subject gnus-registry-track-extra))
149 (defun gnus-registry-track-sender-p ()
150 (memq 'sender gnus-registry-track-extra))
152 (defun gnus-registry-cache-read ()
153 "Read the registry cache file."
154 (interactive)
155 (let ((file gnus-registry-cache-file))
156 (when (file-exists-p file)
157 (gnus-message 5 "Reading %s..." file)
158 (gnus-load file)
159 (gnus-message 5 "Reading %s...done" file))))
161 ;; FIXME: Get rid of duplicated code, cf. `gnus-save-newsrc-file' in
162 ;; `gnus-start.el'. --rsteib
163 (defun gnus-registry-cache-save ()
164 "Save the registry cache file."
165 (interactive)
166 (let ((file gnus-registry-cache-file))
167 (save-excursion
168 (set-buffer (gnus-get-buffer-create " *Gnus-registry-cache*"))
169 (make-local-variable 'version-control)
170 (setq version-control gnus-backup-startup-file)
171 (setq buffer-file-name file)
172 (setq default-directory (file-name-directory buffer-file-name))
173 (buffer-disable-undo)
174 (erase-buffer)
175 (gnus-message 5 "Saving %s..." file)
176 (if gnus-save-startup-file-via-temp-buffer
177 (let ((coding-system-for-write gnus-ding-file-coding-system)
178 (standard-output (current-buffer)))
179 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist)
180 (gnus-registry-cache-whitespace file)
181 (save-buffer))
182 (let ((coding-system-for-write gnus-ding-file-coding-system)
183 (version-control gnus-backup-startup-file)
184 (startup-file file)
185 (working-dir (file-name-directory file))
186 working-file
187 (i -1))
188 ;; Generate the name of a non-existent file.
189 (while (progn (setq working-file
190 (format
191 (if (and (eq system-type 'ms-dos)
192 (not (gnus-long-file-names)))
193 "%s#%d.tm#" ; MSDOS limits files to 8+3
194 (if (memq system-type '(vax-vms axp-vms))
195 "%s$tmp$%d"
196 "%s#tmp#%d"))
197 working-dir (setq i (1+ i))))
198 (file-exists-p working-file)))
200 (unwind-protect
201 (progn
202 (gnus-with-output-to-file working-file
203 (gnus-gnus-to-quick-newsrc-format t "gnus registry startup file" 'gnus-registry-alist))
205 ;; These bindings will mislead the current buffer
206 ;; into thinking that it is visiting the startup
207 ;; file.
208 (let ((buffer-backed-up nil)
209 (buffer-file-name startup-file)
210 (file-precious-flag t)
211 (setmodes (file-modes startup-file)))
212 ;; Backup the current version of the startup file.
213 (backup-buffer)
215 ;; Replace the existing startup file with the temp file.
216 (rename-file working-file startup-file t)
217 (gnus-set-file-modes startup-file setmodes)))
218 (condition-case nil
219 (delete-file working-file)
220 (file-error nil)))))
222 (gnus-kill-buffer (current-buffer))
223 (gnus-message 5 "Saving %s...done" file))))
225 ;; Idea from Dan Christensen <jdc@chow.mat.jhu.edu>
226 ;; Save the gnus-registry file with extra line breaks.
227 (defun gnus-registry-cache-whitespace (filename)
228 (gnus-message 7 "Adding whitespace to %s" filename)
229 (save-excursion
230 (goto-char (point-min))
231 (while (re-search-forward "^(\\|(\\\"" nil t)
232 (replace-match "\n\\&" t))
233 (goto-char (point-min))
234 (while (re-search-forward " $" nil t)
235 (replace-match "" t t))))
237 (defun gnus-registry-save (&optional force)
238 (when (or gnus-registry-dirty force)
239 (let ((caching gnus-registry-entry-caching))
240 ;; turn off entry caching, so mtime doesn't get recorded
241 (setq gnus-registry-entry-caching nil)
242 ;; remove entry caches
243 (maphash
244 (lambda (key value)
245 (if (hash-table-p value)
246 (remhash key gnus-registry-hashtb)))
247 gnus-registry-hashtb)
248 ;; remove empty entries
249 (when gnus-registry-clean-empty
250 (gnus-registry-clean-empty-function))
251 ;; now trim and clean text properties from the registry appropriately
252 (setq gnus-registry-alist
253 (gnus-registry-remove-alist-text-properties
254 (gnus-registry-trim
255 (gnus-hashtable-to-alist
256 gnus-registry-hashtb))))
257 ;; really save
258 (gnus-registry-cache-save)
259 (setq gnus-registry-entry-caching caching)
260 (setq gnus-registry-dirty nil))))
262 (defun gnus-registry-clean-empty-function ()
263 "Remove all empty entries from the registry. Returns count thereof."
264 (let ((count 0))
266 (maphash
267 (lambda (key value)
268 (when (stringp key)
269 (dolist (group (gnus-registry-fetch-groups key))
270 (when (gnus-parameter-registry-ignore group)
271 (gnus-message
273 "gnus-registry: deleted ignored group %s from key %s"
274 group key)
275 (gnus-registry-delete-group key group)))
277 (unless (gnus-registry-group-count key)
278 (gnus-registry-delete-id key))
280 (unless (or
281 (gnus-registry-fetch-group key)
282 ;; TODO: look for specific extra data here!
283 ;; in this example, we look for 'label
284 (gnus-registry-fetch-extra key 'label))
285 (incf count)
286 (gnus-registry-delete-id key))
288 (unless (stringp key)
289 (gnus-message
291 "gnus-registry key %s was not a string, removing"
292 key)
293 (gnus-registry-delete-id key))))
295 gnus-registry-hashtb)
296 count))
298 (defun gnus-registry-read ()
299 (gnus-registry-cache-read)
300 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
301 (setq gnus-registry-dirty nil))
303 (defun gnus-registry-remove-alist-text-properties (v)
304 "Remove text properties from all strings in alist."
305 (if (stringp v)
306 (gnus-string-remove-all-properties v)
307 (if (and (listp v) (listp (cdr v)))
308 (mapcar 'gnus-registry-remove-alist-text-properties v)
309 (if (and (listp v) (stringp (cdr v)))
310 (cons (gnus-registry-remove-alist-text-properties (car v))
311 (gnus-registry-remove-alist-text-properties (cdr v)))
312 v))))
314 (defun gnus-registry-trim (alist)
315 "Trim alist to size, using gnus-registry-max-entries.
316 Also, drop all gnus-registry-ignored-groups matches."
317 (if (null gnus-registry-max-entries)
318 alist ; just return the alist
319 ;; else, when given max-entries, trim the alist
320 (let* ((timehash (make-hash-table
321 :size 4096
322 :test 'equal))
323 (trim-length (- (length alist) gnus-registry-max-entries))
324 (trim-length (if (natnump trim-length) trim-length 0)))
325 (maphash
326 (lambda (key value)
327 (puthash key (gnus-registry-fetch-extra key 'mtime) timehash))
328 gnus-registry-hashtb)
330 ;; we use the return value of this setq, which is the trimmed alist
331 (setq alist
332 (nthcdr
333 trim-length
334 (sort alist
335 (lambda (a b)
336 (time-less-p
337 (or (cdr (gethash (car a) timehash)) '(0 0 0))
338 (or (cdr (gethash (car b) timehash)) '(0 0 0))))))))))
340 (defun gnus-registry-action (action data-header from &optional to method)
341 (let* ((id (mail-header-id data-header))
342 (subject (gnus-string-remove-all-properties
343 (gnus-registry-simplify-subject
344 (mail-header-subject data-header))))
345 (sender (gnus-string-remove-all-properties (mail-header-from data-header)))
346 (from (gnus-group-guess-full-name-from-command-method from))
347 (to (if to (gnus-group-guess-full-name-from-command-method to) nil))
348 (to-name (if to to "the Bit Bucket"))
349 (old-entry (gethash id gnus-registry-hashtb)))
350 (gnus-message 7 "Registry: article %s %s from %s to %s"
352 (if method "respooling" "going")
353 from
356 ;; All except copy will need a delete
357 (gnus-registry-delete-group id from)
359 (when (equal 'copy action)
360 (gnus-registry-add-group id from subject sender)) ; undo the delete
362 (gnus-registry-add-group id to subject sender)))
364 (defun gnus-registry-spool-action (id group &optional subject sender)
365 (let ((group (gnus-group-guess-full-name-from-command-method group)))
366 (when (and (stringp id) (string-match "\r$" id))
367 (setq id (substring id 0 -1)))
368 (gnus-message 7 "Registry: article %s spooled to %s"
370 group)
371 (gnus-registry-add-group id group subject sender)))
373 ;; Function for nn{mail|imap}-split-fancy: look up all references in
374 ;; the cache and if a match is found, return that group.
375 (defun gnus-registry-split-fancy-with-parent ()
376 "Split this message into the same group as its parent. The parent
377 is obtained from the registry. This function can be used as an entry
378 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
379 this: (: gnus-registry-split-fancy-with-parent)
381 This function tracks ALL backends, unlike
382 `nnmail-split-fancy-with-parent' which tracks only nnmail
383 messages.
385 For a message to be split, it looks for the parent message in the
386 References or In-Reply-To header and then looks in the registry
387 to see which group that message was put in. This group is
388 returned, unless it matches one of the entries in
389 gnus-registry-unfollowed-groups or
390 nnmail-split-fancy-with-parent-ignore-groups.
392 See the Info node `(gnus)Fancy Mail Splitting' for more details."
393 (let* ((refstr (or (message-fetch-field "references") "")) ; guarantee string
394 (reply-to (message-fetch-field "in-reply-to")) ; grab reply-to
395 ;; now, if reply-to is valid, append it to the References
396 (refstr (if reply-to
397 (concat refstr " " reply-to)
398 refstr))
399 (nnmail-split-fancy-with-parent-ignore-groups
400 (if (listp nnmail-split-fancy-with-parent-ignore-groups)
401 nnmail-split-fancy-with-parent-ignore-groups
402 (list nnmail-split-fancy-with-parent-ignore-groups)))
403 res)
404 ;; the references string must be valid and parse to valid references
405 (if (and refstr (gnus-extract-references refstr))
406 (dolist (reference (nreverse (gnus-extract-references refstr)))
407 (setq res (or (gnus-registry-fetch-group reference) res))
408 (when (or (gnus-registry-grep-in-list
410 gnus-registry-unfollowed-groups)
411 (gnus-registry-grep-in-list
413 nnmail-split-fancy-with-parent-ignore-groups))
414 (setq res nil)))
416 ;; else: there were no references, now try the extra tracking
417 (let ((sender (gnus-string-remove-all-properties(message-fetch-field "from")))
418 (subject (gnus-string-remove-all-properties
419 (gnus-registry-simplify-subject
420 (message-fetch-field "subject"))))
421 (single-match t))
422 (when (and single-match
423 (gnus-registry-track-sender-p)
424 sender)
425 (maphash
426 (lambda (key value)
427 (let ((this-sender (cdr
428 (gnus-registry-fetch-extra key 'sender))))
429 (when (and single-match
430 this-sender
431 (equal sender this-sender))
432 ;; too many matches, bail
433 (unless (equal res (gnus-registry-fetch-group key))
434 (setq single-match nil))
435 (setq res (gnus-registry-fetch-group key))
436 (when (and sender res)
437 (gnus-message
438 ;; raise level of messaging if gnus-registry-track-extra
439 (if gnus-registry-track-extra 7 9)
440 "%s (extra tracking) traced sender %s to group %s"
441 "gnus-registry-split-fancy-with-parent"
442 sender
443 res)))))
444 gnus-registry-hashtb))
445 (when (and single-match
446 (gnus-registry-track-subject-p)
447 subject
448 (< gnus-registry-minimum-subject-length (length subject)))
449 (maphash
450 (lambda (key value)
451 (let ((this-subject (cdr
452 (gnus-registry-fetch-extra key 'subject))))
453 (when (and single-match
454 this-subject
455 (equal subject this-subject))
456 ;; too many matches, bail
457 (unless (equal res (gnus-registry-fetch-group key))
458 (setq single-match nil))
459 (setq res (gnus-registry-fetch-group key))
460 (when (and subject res)
461 (gnus-message
462 ;; raise level of messaging if gnus-registry-track-extra
463 (if gnus-registry-track-extra 7 9)
464 "%s (extra tracking) traced subject %s to group %s"
465 "gnus-registry-split-fancy-with-parent"
466 subject
467 res)))))
468 gnus-registry-hashtb))
469 (unless single-match
470 (gnus-message
472 "gnus-registry-split-fancy-with-parent: too many extra matches for %s"
473 refstr)
474 (setq res nil))))
475 (when (and refstr res)
476 (gnus-message
478 "gnus-registry-split-fancy-with-parent traced %s to group %s"
479 refstr res))
481 (when (and res gnus-registry-use-long-group-names)
482 (let ((m1 (gnus-find-method-for-group res))
483 (m2 (or gnus-command-method
484 (gnus-find-method-for-group gnus-newsgroup-name)))
485 (short-res (gnus-group-short-name res)))
486 (if (gnus-methods-equal-p m1 m2)
487 (progn
488 (gnus-message
490 "gnus-registry-split-fancy-with-parent stripped group %s to %s"
492 short-res)
493 (setq res short-res))
494 ;; else...
495 (gnus-message
497 "gnus-registry-split-fancy-with-parent ignored foreign group %s"
498 res)
499 (setq res nil))))
500 res))
502 (defun gnus-registry-wash-for-keywords (&optional force)
503 (interactive)
504 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article))
505 word words)
506 (if (or (not (gnus-registry-fetch-extra id 'keywords))
507 force)
508 (save-excursion
509 (set-buffer gnus-article-buffer)
510 (article-goto-body)
511 (save-window-excursion
512 (save-restriction
513 (narrow-to-region (point) (point-max))
514 (with-syntax-table gnus-adaptive-word-syntax-table
515 (while (re-search-forward "\\b\\w+\\b" nil t)
516 (setq word (gnus-registry-remove-alist-text-properties
517 (downcase (buffer-substring
518 (match-beginning 0) (match-end 0)))))
519 (if (> (length word) 3)
520 (push word words))))))
521 (gnus-registry-store-extra-entry id 'keywords words)))))
523 (defun gnus-registry-find-keywords (keyword)
524 (interactive "skeyword: ")
525 (let (articles)
526 (maphash
527 (lambda (key value)
528 (when (gnus-registry-grep-in-list
529 keyword
530 (cdr (gnus-registry-fetch-extra key 'keywords)))
531 (push key articles)))
532 gnus-registry-hashtb)
533 articles))
535 (defun gnus-registry-register-message-ids ()
536 "Register the Message-ID of every article in the group"
537 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name)
538 (dolist (article gnus-newsgroup-articles)
539 (let ((id (gnus-registry-fetch-message-id-fast article)))
540 (unless (gnus-registry-fetch-group id)
541 (gnus-message 9 "Registry: Registering article %d with group %s"
542 article gnus-newsgroup-name)
543 (gnus-registry-add-group
544 (gnus-registry-fetch-message-id-fast article)
545 gnus-newsgroup-name
546 (gnus-registry-fetch-simplified-message-subject-fast article)
547 (gnus-registry-fetch-sender-fast article)))))))
549 (defun gnus-registry-fetch-message-id-fast (article)
550 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
551 (if (and (numberp article)
552 (assoc article (gnus-data-list nil)))
553 (mail-header-id (gnus-data-header (assoc article (gnus-data-list nil))))
554 nil))
556 (defun gnus-registry-simplify-subject (subject)
557 (if (stringp subject)
558 (gnus-simplify-subject subject)
559 nil))
561 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
562 "Fetch the Subject quickly, using the internal gnus-data-list function"
563 (if (and (numberp article)
564 (assoc article (gnus-data-list nil)))
565 (gnus-string-remove-all-properties
566 (gnus-registry-simplify-subject
567 (mail-header-subject (gnus-data-header
568 (assoc article (gnus-data-list nil))))))
569 nil))
571 (defun gnus-registry-fetch-sender-fast (article)
572 "Fetch the Sender quickly, using the internal gnus-data-list function"
573 (if (and (numberp article)
574 (assoc article (gnus-data-list nil)))
575 (gnus-string-remove-all-properties
576 (mail-header-from (gnus-data-header
577 (assoc article (gnus-data-list nil)))))
578 nil))
580 (defun gnus-registry-grep-in-list (word list)
581 (when word
582 (memq nil
583 (mapcar 'not
584 (mapcar
585 (lambda (x)
586 (string-match word x))
587 list)))))
589 ;;; if this extends to more than 'flags, it should be improved to be more generic.
590 (defun gnus-registry-fetch-extra-flags (id)
591 "Get the flags of a message, based on the message ID.
592 Returns a list of symbol flags or nil."
593 (car-safe (cdr (gnus-registry-fetch-extra id 'flags))))
595 (defun gnus-registry-has-extra-flag (id flag)
596 "Checks if a message has `flag', based on the message ID."
597 (memq flag (gnus-registry-fetch-extra-flags id)))
599 (defun gnus-registry-store-extra-flags (id &rest flag-list)
600 "Set the flags of a message, based on the message ID.
601 The `flag-list' can be nil, in which case no flags are left."
602 (gnus-registry-store-extra-entry id 'flags (list flag-list)))
604 (defun gnus-registry-delete-extra-flags (id &rest flag-delete-list)
605 "Delete the message flags in `flag-delete-list', based on the message ID."
606 (let ((flags (gnus-registry-fetch-extra-flags id)))
607 (when flags
608 (dolist (flag flag-delete-list)
609 (setq flags (delq flag flags))))
610 (gnus-registry-store-extra-flags id (car flags))))
612 (defun gnus-registry-delete-all-extra-flags (id)
613 "Delete all the flags for a message ID."
614 (gnus-registry-store-extra-flags id nil))
616 (defun gnus-registry-fetch-extra (id &optional entry)
617 "Get the extra data of a message, based on the message ID.
618 Returns the first place where the trail finds a nonstring."
619 (let ((entry-cache (gethash entry gnus-registry-hashtb)))
620 (if (and entry
621 (hash-table-p entry-cache)
622 (gethash id entry-cache))
623 (gethash id entry-cache)
624 ;; else, if there is no caching possible...
625 (let ((trail (gethash id gnus-registry-hashtb)))
626 (when (listp trail)
627 (dolist (crumb trail)
628 (unless (stringp crumb)
629 (return (gnus-registry-fetch-extra-entry crumb entry id)))))))))
631 (defun gnus-registry-fetch-extra-entry (alist &optional entry id)
632 "Get the extra data of a message, or a specific entry in it.
633 Update the entry cache if needed."
634 (if (and entry id)
635 (let ((entry-cache (gethash entry gnus-registry-hashtb))
636 entree)
637 (when gnus-registry-entry-caching
638 ;; create the hash table
639 (unless (hash-table-p entry-cache)
640 (setq entry-cache (make-hash-table
641 :size 4096
642 :test 'equal))
643 (puthash entry entry-cache gnus-registry-hashtb))
645 ;; get the entree from the hash table or from the alist
646 (setq entree (gethash id entry-cache)))
648 (unless entree
649 (setq entree (assq entry alist))
650 (when gnus-registry-entry-caching
651 (puthash id entree entry-cache)))
652 entree)
653 alist))
655 (defun gnus-registry-store-extra (id extra)
656 "Store the extra data of a message, based on the message ID.
657 The message must have at least one group name."
658 (when (gnus-registry-group-count id)
659 ;; we now know the trail has at least 1 group name, so it's not empty
660 (let ((trail (gethash id gnus-registry-hashtb))
661 (old-extra (gnus-registry-fetch-extra id))
662 entry-cache)
663 (dolist (crumb trail)
664 (unless (stringp crumb)
665 (dolist (entry crumb)
666 (setq entry-cache (gethash (car entry) gnus-registry-hashtb))
667 (when entry-cache
668 (remhash id entry-cache))))
669 (puthash id (cons extra (delete old-extra trail))
670 gnus-registry-hashtb)
671 (setq gnus-registry-dirty t)))))
673 (defun gnus-registry-delete-extra-entry (id key)
674 "Delete a specific entry in the extras field of the registry entry for id."
675 (gnus-registry-store-extra-entry id key nil))
677 (defun gnus-registry-store-extra-entry (id key value)
678 "Put a specific entry in the extras field of the registry entry for id."
679 (let* ((extra (gnus-registry-fetch-extra id))
680 ;; all the entries except the one for `key'
681 (the-rest (gnus-assq-delete-all key (gnus-registry-fetch-extra id)))
682 (alist (if value
683 (gnus-registry-remove-alist-text-properties
684 (cons (cons key value)
685 the-rest))
686 the-rest)))
687 (gnus-registry-store-extra id alist)))
689 (defun gnus-registry-fetch-group (id)
690 "Get the group of a message, based on the message ID.
691 Returns the first place where the trail finds a group name."
692 (when (gnus-registry-group-count id)
693 ;; we now know the trail has at least 1 group name
694 (let ((trail (gethash id gnus-registry-hashtb)))
695 (dolist (crumb trail)
696 (when (stringp crumb)
697 (return (if gnus-registry-use-long-group-names
698 crumb
699 (gnus-group-short-name crumb))))))))
701 (defun gnus-registry-fetch-groups (id)
702 "Get the groups of a message, based on the message ID."
703 (let ((trail (gethash id gnus-registry-hashtb))
704 groups)
705 (dolist (crumb trail)
706 (when (stringp crumb)
707 ;; push the group name into the list
708 (setq
709 groups
710 (cons
711 (if (or (not (stringp crumb)) gnus-registry-use-long-group-names)
712 crumb
713 (gnus-group-short-name crumb))
714 groups))))
715 ;; return the list of groups
716 groups))
718 (defun gnus-registry-group-count (id)
719 "Get the number of groups of a message, based on the message ID."
720 (let ((trail (gethash id gnus-registry-hashtb)))
721 (if (and trail (listp trail))
722 (apply '+ (mapcar (lambda (x) (if (stringp x) 1 0)) trail))
723 0)))
725 (defun gnus-registry-delete-group (id group)
726 "Delete a group for a message, based on the message ID."
727 (when (and group id)
728 (let ((trail (gethash id gnus-registry-hashtb))
729 (short-group (gnus-group-short-name group)))
730 (puthash id (if trail
731 (delete short-group (delete group trail))
732 nil)
733 gnus-registry-hashtb))
734 ;; now, clear the entry if there are no more groups
735 (when gnus-registry-trim-articles-without-groups
736 (unless (gnus-registry-group-count id)
737 (gnus-registry-delete-id id)))
738 ;; is this ID still in the registry?
739 (when (gethash id gnus-registry-hashtb)
740 (gnus-registry-store-extra-entry id 'mtime (current-time)))))
742 (defun gnus-registry-delete-id (id)
743 "Delete a message ID from the registry."
744 (when (stringp id)
745 (remhash id gnus-registry-hashtb)
746 (maphash
747 (lambda (key value)
748 (when (hash-table-p value)
749 (remhash id value)))
750 gnus-registry-hashtb)))
752 (defun gnus-registry-add-group (id group &optional subject sender)
753 "Add a group for a message, based on the message ID."
754 (when group
755 (when (and id
756 (not (string-match "totally-fudged-out-message-id" id)))
757 (let ((full-group group)
758 (group (if gnus-registry-use-long-group-names
759 group
760 (gnus-group-short-name group))))
761 (gnus-registry-delete-group id group)
763 (unless gnus-registry-use-long-group-names ;; unnecessary in this case
764 (gnus-registry-delete-group id full-group))
766 (let ((trail (gethash id gnus-registry-hashtb)))
767 (puthash id (if trail
768 (cons group trail)
769 (list group))
770 gnus-registry-hashtb)
772 (when (and (gnus-registry-track-subject-p)
773 subject)
774 (gnus-registry-store-extra-entry
776 'subject
777 (gnus-registry-simplify-subject subject)))
778 (when (and (gnus-registry-track-sender-p)
779 sender)
780 (gnus-registry-store-extra-entry
782 'sender
783 sender))
785 (gnus-registry-store-extra-entry id 'mtime (current-time)))))))
787 (defun gnus-registry-clear ()
788 "Clear the Gnus registry."
789 (interactive)
790 (setq gnus-registry-alist nil)
791 (setq gnus-registry-hashtb (gnus-alist-to-hashtable gnus-registry-alist))
792 (setq gnus-registry-dirty t))
794 ;;;###autoload
795 (defun gnus-registry-initialize ()
796 (interactive)
797 (setq gnus-registry-install t)
798 (gnus-registry-install-hooks)
799 (gnus-registry-read))
801 ;;;###autoload
802 (defun gnus-registry-install-hooks ()
803 "Install the registry hooks."
804 (interactive)
805 (add-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
806 (add-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
807 (add-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
808 (add-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
810 (add-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
811 (add-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
813 (add-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
815 (defun gnus-registry-unload-hook ()
816 "Uninstall the registry hooks."
817 (interactive)
818 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action)
819 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action)
820 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action)
821 (remove-hook 'nnmail-spool-hook 'gnus-registry-spool-action)
823 (remove-hook 'gnus-save-newsrc-hook 'gnus-registry-save)
824 (remove-hook 'gnus-read-newsrc-el-hook 'gnus-registry-read)
826 (remove-hook 'gnus-summary-prepare-hook 'gnus-registry-register-message-ids))
828 (add-hook 'gnus-registry-unload-hook 'gnus-registry-unload-hook)
830 (when gnus-registry-install
831 (gnus-registry-install-hooks)
832 (gnus-registry-read))
834 ;; TODO: a lot of things
836 (provide 'gnus-registry)
838 ;;; arch-tag: 5cba0a32-718a-4a97-8c91-0a15af21da94
839 ;;; gnus-registry.el ends here