1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: news registry
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is the gnus-registry.el package, which works with all
26 ;; Gnus backends, not just nnmail. The major issue is that it
27 ;; doesn't go across backends, so for instance if an article is in
28 ;; nnml:sys and you see a reference to it in nnimap splitting, the
29 ;; article will end up in nnimap:sys
31 ;; gnus-registry.el intercepts article respooling, moving, deleting,
32 ;; and copying for all backends. If it doesn't work correctly for
33 ;; you, submit a bug report and I'll be glad to fix it. It needs
34 ;; better documentation in the manual (also on my to-do list).
36 ;; If you want to track recipients (and you should to make the
37 ;; gnus-registry splitting work better), you need the To and Cc
38 ;; headers collected by Gnus. Note that in more recent Gnus versions
39 ;; this is already the case: look at `gnus-extra-headers' to be sure.
41 ;; ;;; you may also want Gcc Newsgroups Keywords X-Face
42 ;; (add-to-list 'gnus-extra-headers 'To)
43 ;; (add-to-list 'gnus-extra-headers 'Cc)
44 ;; (setq nnmail-extra-headers gnus-extra-headers)
46 ;; Put this in your startup file (~/.gnus.el for instance) or use Customize:
48 ;; (setq gnus-registry-max-entries 2500
49 ;; gnus-registry-track-extra '(sender subject recipient))
51 ;; (gnus-registry-initialize)
53 ;; Then use this in your fancy-split:
55 ;; (: gnus-registry-split-fancy-with-parent)
57 ;; You should also consider using the nnregistry backend to look up
58 ;; articles. See the Gnus manual for more information.
60 ;; Finally, you can put %uM in your summary line format to show the
61 ;; registry marks if you do this:
63 ;; show the marks as single characters (see the :char property in
64 ;; `gnus-registry-marks'):
65 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
67 ;; show the marks by name (see `gnus-registry-marks'):
68 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
72 ;; - get the correct group on spool actions
74 ;; - articles that are spooled to a different backend should be moved
79 (eval-when-compile (require 'cl
))
82 (when (null (ignore-errors (require 'ert
)))
83 (defmacro* ert-deftest
(name () &body docstring-keys-and-body
))))
96 (defvar gnus-adaptive-word-syntax-table
)
98 (defvar gnus-registry-dirty t
99 "Boolean set to t when the registry is modified")
101 (defgroup gnus-registry nil
106 (defvar gnus-registry-marks
109 :image
"summary_important")
112 :image
"summary_work")
115 :image
"summary_personal")
118 :image
"summary_todo")
121 :image
"summary_later"))
123 "List of registry marks and their options.
125 `gnus-registry-mark-article' will offer symbols from this list
128 Each entry must have a character to be useful for summary mode
129 line display and for keyboard shortcuts.
131 Each entry must have an image string to be useful for visual
134 (defcustom gnus-registry-default-mark
'To-Do
135 "The default mark. Should be a valid key for `gnus-registry-marks'."
136 :group
'gnus-registry
139 (defcustom gnus-registry-unfollowed-addresses
140 (list (regexp-quote user-mail-address
))
141 "List of addresses that gnus-registry-split-fancy-with-parent won't trace.
142 The addresses are matched, they don't have to be fully qualified.
143 In the messages, these addresses can be the sender or the
145 :group
'gnus-registry
146 :type
'(repeat regexp
))
148 (defcustom gnus-registry-unfollowed-groups
149 '("delayed$" "drafts$" "queue$" "INBOX$" "^nnmairix:" "archive")
150 "List of groups that gnus-registry-split-fancy-with-parent won't return.
151 The group names are matched, they don't have to be fully
152 qualified. This parameter tells the Gnus registry 'never split a
153 message into a group that matches one of these, regardless of
156 nnmairix groups are specifically excluded because they are ephemeral."
157 :group
'gnus-registry
158 :type
'(repeat regexp
))
160 (defcustom gnus-registry-install
'ask
161 "Whether the registry should be installed."
162 :group
'gnus-registry
163 :type
'(choice (const :tag
"Never Install" nil
)
164 (const :tag
"Always Install" t
)
165 (const :tag
"Ask Me" ask
)))
167 (defvar gnus-registry-enabled nil
)
169 (defvar gnus-summary-misc-menu
) ;; Avoid byte compiler warning.
171 (defvar gnus-registry-misc-menus nil
) ; ugly way to keep the menus
173 (make-obsolete-variable 'gnus-registry-clean-empty nil
"23.4")
174 (make-obsolete-variable 'gnus-registry-use-long-group-names nil
"23.4")
175 (make-obsolete-variable 'gnus-registry-max-track-groups nil
"23.4")
176 (make-obsolete-variable 'gnus-registry-entry-caching nil
"23.4")
177 (make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil
"23.4")
179 (defcustom gnus-registry-track-extra
'(subject sender recipient
)
180 "Whether the registry should track extra data about a message.
181 The subject, recipients (To: and Cc:), and Sender (From:) headers
182 are tracked this way by default."
183 :group
'gnus-registry
185 '(set :tag
"Tracking choices"
186 (const :tag
"Track by subject (Subject: header)" subject
)
187 (const :tag
"Track by recipient (To: and Cc: headers)" recipient
)
188 (const :tag
"Track by sender (From: header)" sender
)))
190 (defcustom gnus-registry-split-strategy nil
191 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
193 Given a set of unique found groups G and counts for each element
194 of G, and a key K (typically 'sender or 'subject):
196 When nil, if G has only one element, use it. Otherwise give up.
197 This is the fastest but also least useful strategy.
199 When 'majority, use the majority by count. So if there is a
200 group with the most articles counted by K, use that. Ties are
201 resolved in no particular order, simply the first one found wins.
202 This is the slowest strategy but also the most accurate one.
204 When 'first, the first element of G wins. This is fast and
205 should be OK if your senders and subjects don't \"bleed\" across
207 :group
'gnus-registry
209 '(choice :tag
"Splitting strategy"
210 (const :tag
"Only use single choices, discard multiple matches" nil
)
211 (const :tag
"Majority of matches wins" majority
)
212 (const :tag
"First found wins" first
)))
214 (defcustom gnus-registry-minimum-subject-length
5
215 "The minimum length of a subject before it's considered trackable."
216 :group
'gnus-registry
219 (defcustom gnus-registry-extra-entries-precious
'(mark)
220 "What extra keys are precious, meaning entries with them won't get pruned.
221 By default, 'mark is included, so articles with marks are
224 Before you save the Gnus registry, it's pruned. Any entries with
225 keys in this list will not be pruned. All other entries go to
227 :group
'gnus-registry
228 :type
'(repeat symbol
))
230 (defcustom gnus-registry-cache-file
232 (or gnus-dribble-directory gnus-home-directory
"~/")
233 ".gnus.registry.eioio")
234 "File where the Gnus registry will be stored."
235 :group
'gnus-registry
238 (defcustom gnus-registry-max-entries nil
239 "Maximum number of entries in the registry, nil for unlimited."
240 :group
'gnus-registry
241 :type
'(radio (const :format
"Unlimited " nil
)
242 (integer :format
"Maximum number: %v")))
244 (defcustom gnus-registry-max-pruned-entries nil
245 "Maximum number of pruned entries in the registry, nil for unlimited."
246 :group
'gnus-registry
247 :type
'(radio (const :format
"Unlimited " nil
)
248 (integer :format
"Maximum number: %v")))
250 (defun gnus-registry-fixup-registry (db)
252 (let ((old (oref db
:tracked
)))
254 (append gnus-registry-extra-entries-precious
257 (or gnus-registry-max-entries
258 most-positive-fixnum
))
259 (oset db
:prune-factor
262 (or gnus-registry-max-pruned-entries
263 most-positive-fixnum
))
265 (append gnus-registry-track-extra
266 '(mark group keyword
)))
267 (when (not (equal old
(oref db
:tracked
)))
268 (gnus-message 4 "Reindexing the Gnus registry (tracked change)")
269 (registry-reindex db
))))
272 (defun gnus-registry-make-db (&optional file
)
273 (interactive "fGnus registry persistence file: \n")
274 (gnus-registry-fixup-registry
277 :file
(or file gnus-registry-cache-file
)
278 ;; these parameters are set in `gnus-registry-fixup-registry'
279 :max-hard most-positive-fixnum
280 :max-soft most-positive-fixnum
284 (defvar gnus-registry-db
(gnus-registry-make-db)
285 "*The article registry by Message ID. See `registry-db'")
287 ;; top-level registry data management
288 (defun gnus-registry-remake-db (&optional forsure
)
289 "Remake the registry database after customization.
290 This is not required after changing `gnus-registry-cache-file'."
291 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
293 (gnus-message 4 "Remaking the Gnus registry")
294 (setq gnus-registry-db
(gnus-registry-make-db))))
296 (defun gnus-registry-read ()
297 "Read the registry cache file."
299 (let ((file gnus-registry-cache-file
))
302 (gnus-message 5 "Reading Gnus registry from %s..." file
)
303 (setq gnus-registry-db
(gnus-registry-fixup-registry
304 (eieio-persistent-read file
)))
305 (gnus-message 5 "Reading Gnus registry from %s...done" file
))
309 "The Gnus registry could not be loaded from %s, creating a new one"
311 (gnus-registry-remake-db t
)))))
313 (defun gnus-registry-save (&optional file db
)
314 "Save the registry cache file."
316 (let ((file (or file gnus-registry-cache-file
))
317 (db (or db gnus-registry-db
)))
318 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
319 (registry-size db
) file
)
321 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
322 (eieio-persistent-save db file
)
323 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
324 (registry-size db
) file
)))
326 (defun gnus-registry-remove-ignored ()
328 (let* ((db gnus-registry-db
)
329 (grouphashtb (registry-lookup-secondary db
'group
))
330 (old-size (registry-size db
)))
331 (registry-reindex db
)
332 (loop for k being the hash-keys of grouphashtb
333 using
(hash-values v
)
334 when
(gnus-registry-ignore-group-p k
)
335 do
(registry-delete db v nil
))
336 (registry-reindex db
)
337 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
338 (- old-size
(registry-size db
)))))
340 ;; article move/copy/spool/delete actions
341 (defun gnus-registry-action (action data-header from
&optional to method
)
342 (let* ((id (mail-header-id data-header
))
343 (subject (mail-header-subject data-header
))
344 (extra (mail-header-extra data-header
))
345 (recipients (gnus-registry-sort-addresses
346 (or (cdr-safe (assq 'Cc extra
)) "")
347 (or (cdr-safe (assq 'To extra
)) "")))
348 (sender (nth 0 (gnus-registry-extract-addresses
349 (mail-header-from data-header
))))
350 (from (gnus-group-guess-full-name-from-command-method from
))
351 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
))
352 (to-name (if to to
"the Bit Bucket")))
353 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
354 id
(if method
"respooling" "going") from to
)
356 (gnus-registry-handle-action
358 ;; unless copying, remove the old "from" group
359 (if (not (equal 'copy action
)) from nil
)
360 to subject sender recipients
)))
362 (defun gnus-registry-spool-action (id group
&optional subject sender recipients
)
363 (let ((to (gnus-group-guess-full-name-from-command-method group
))
364 (recipients (or recipients
365 (gnus-registry-sort-addresses
366 (or (message-fetch-field "cc") "")
367 (or (message-fetch-field "to") ""))))
368 (subject (or subject
(message-fetch-field "subject")))
369 (sender (or sender
(message-fetch-field "from"))))
370 (when (and (stringp id
) (string-match "\r$" id
))
371 (setq id
(substring id
0 -
1)))
372 (gnus-message 7 "Gnus registry: article %s spooled to %s"
375 (gnus-registry-handle-action id nil to subject sender recipients
)))
377 (defun gnus-registry-handle-action (id from to subject sender
378 &optional recipients
)
381 "gnus-registry-handle-action %S" (list id from to subject sender recipients
))
382 (let ((db gnus-registry-db
)
383 ;; if the group is ignored, set the destination to nil (same as delete)
384 (to (if (gnus-registry-ignore-group-p to
) nil to
))
386 (entry (gnus-registry-get-or-make-entry id
))
387 (subject (gnus-string-remove-all-properties
388 (gnus-registry-simplify-subject subject
)))
389 (sender (gnus-string-remove-all-properties sender
)))
391 ;; this could be done by calling `gnus-registry-set-id-key'
392 ;; several times but it's better to bunch the transactions
395 (registry-delete db
(list id
) nil
)
397 (setq entry
(cons (delete from
(assoc 'group entry
))
398 (assq-delete-all 'group entry
))))
400 (dolist (kv `((group ,to
)
402 (recipient ,@recipients
)
405 (let ((new (or (assq (first kv
) entry
)
407 (dolist (toadd (cdr kv
))
408 (add-to-list 'new toadd t
))
409 (setq entry
(cons new
410 (assq-delete-all (first kv
) entry
))))))
411 (gnus-message 10 "Gnus registry: new entry for %s is %S"
414 (gnus-registry-insert db id entry
)))
416 ;; Function for nn{mail|imap}-split-fancy: look up all references in
417 ;; the cache and if a match is found, return that group.
418 (defun gnus-registry-split-fancy-with-parent ()
419 "Split this message into the same group as its parent. The parent
420 is obtained from the registry. This function can be used as an entry
421 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
422 this: (: gnus-registry-split-fancy-with-parent)
424 This function tracks ALL backends, unlike
425 `nnmail-split-fancy-with-parent' which tracks only nnmail
428 For a message to be split, it looks for the parent message in the
429 References or In-Reply-To header and then looks in the registry
430 to see which group that message was put in. This group is
431 returned, unless `gnus-registry-follow-group-p' return nil for
434 See the Info node `(gnus)Fancy Mail Splitting' for more details."
435 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
436 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
437 ;; now, if reply-to is valid, append it to the References
439 (concat refstr
" " reply-to
)
441 (references (and refstr
(gnus-extract-references refstr
)))
442 ;; these may not be used, but the code is cleaner having them up here
443 (sender (gnus-string-remove-all-properties
444 (message-fetch-field "from")))
445 (recipients (gnus-registry-sort-addresses
446 (or (message-fetch-field "cc") "")
447 (or (message-fetch-field "to") "")))
448 (subject (gnus-string-remove-all-properties
449 (gnus-registry-simplify-subject
450 (message-fetch-field "subject"))))
452 (nnmail-split-fancy-with-parent-ignore-groups
453 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
454 nnmail-split-fancy-with-parent-ignore-groups
455 (list nnmail-split-fancy-with-parent-ignore-groups
))))
456 (gnus-registry--split-fancy-with-parent-internal
457 :references references
460 :recipients recipients
462 :log-agent
"Gnus registry fancy splitting with parent")))
464 (defun* gnus-registry--split-fancy-with-parent-internal
466 &key references refstr sender subject recipients log-agent
470 "gnus-registry--split-fancy-with-parent-internal %S" spec
)
471 (let ((db gnus-registry-db
)
473 ;; this is a big chain of statements. it uses
474 ;; gnus-registry-post-process-groups to filter the results after
476 ;; the references string must be valid and parse to valid references
480 "%s is tracing references %s"
482 (dolist (reference (nreverse references
))
483 (gnus-message 9 "%s is looking up %s" log-agent reference
)
484 (loop for group in
(gnus-registry-get-id-key reference
'group
)
485 when
(gnus-registry-follow-group-p group
)
488 (gnus-message 7 "%s traced %s to %s" log-agent reference group
)
489 (push group found
))))
490 ;; filter the found groups and return them
491 ;; the found groups are the full groups
492 (setq found
(gnus-registry-post-process-groups
493 "references" refstr found
)))
495 ;; else: there were no matches, now try the extra tracking by subject
496 (when (and (null found
)
497 (memq 'subject gnus-registry-track-extra
)
499 (< gnus-registry-minimum-subject-length
(length subject
)))
504 (gnus-registry-get-id-key reference
'group
))
505 (registry-lookup-secondary-value db
'subject subject
)))))
507 (loop for group in groups
508 when
(gnus-registry-follow-group-p group
)
510 ;; warn more if gnus-registry-track-extra
511 (if gnus-registry-track-extra
7 9)
512 "%s (extra tracking) traced subject '%s' to %s"
513 log-agent subject group
)
515 ;; filter the found groups and return them
516 ;; the found groups are NOT the full groups
517 (setq found
(gnus-registry-post-process-groups
518 "subject" subject found
))))
520 ;; else: there were no matches, try the extra tracking by sender
521 (when (and (null found
)
522 (memq 'sender gnus-registry-track-extra
)
524 (not (gnus-grep-in-list
526 gnus-registry-unfollowed-addresses
)))
531 (gnus-registry-get-id-key reference
'group
))
532 (registry-lookup-secondary-value db
'sender sender
)))))
534 (loop for group in groups
535 when
(gnus-registry-follow-group-p group
)
537 ;; warn more if gnus-registry-track-extra
538 (if gnus-registry-track-extra
7 9)
539 "%s (extra tracking) traced sender '%s' to %s"
540 log-agent sender group
)
543 ;; filter the found groups and return them
544 ;; the found groups are NOT the full groups
545 (setq found
(gnus-registry-post-process-groups
546 "sender" sender found
)))
548 ;; else: there were no matches, try the extra tracking by recipient
549 (when (and (null found
)
550 (memq 'recipient gnus-registry-track-extra
)
552 (dolist (recp recipients
)
553 (when (and (null found
)
554 (not (gnus-grep-in-list
556 gnus-registry-unfollowed-addresses
)))
557 (let ((groups (apply 'append
560 (gnus-registry-get-id-key reference
'group
))
561 (registry-lookup-secondary-value
562 db
'recipient recp
)))))
564 (loop for group in groups
565 when
(gnus-registry-follow-group-p group
)
567 ;; warn more if gnus-registry-track-extra
568 (if gnus-registry-track-extra
7 9)
569 "%s (extra tracking) traced recipient '%s' to %s"
570 log-agent recp group
)
571 and collect group
)))))
573 ;; filter the found groups and return them
574 ;; the found groups are NOT the full groups
575 (setq found
(gnus-registry-post-process-groups
576 "recipients" (mapconcat 'identity recipients
", ") found
)))
578 ;; after the (cond) we extract the actual value safely
581 (defun gnus-registry-post-process-groups (mode key groups
)
582 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
584 MODE can be 'subject' or 'sender' for example. The KEY is the
585 value by which MODE was searched.
587 Transforms each group name to the equivalent short name.
589 Checks if the current Gnus method (from `gnus-command-method' or
590 from `gnus-newsgroup-name') is the same as the group's method.
591 Foreign methods are not supported so they are rejected.
593 Reduces the list to a single group, or complains if that's not
594 possible. Uses `gnus-registry-split-strategy'."
595 (let ((log-agent "gnus-registry-post-process-group")
596 (desc (format "%d groups" (length groups
)))
598 ;; the strategy can be nil, in which case chosen is nil
600 (case gnus-registry-split-strategy
601 ;; default, take only one-element lists into chosen
603 (and (= (length groups
) 1)
610 (let ((freq (make-hash-table
613 (mapc (lambda (x) (let ((x (gnus-group-short-name x
)))
614 (puthash x
(1+ (gethash x freq
0)) freq
)))
616 (setq desc
(format "%d groups, %d unique"
618 (hash-table-count freq
)))
622 (> (gethash (gnus-group-short-name a
) freq
0)
623 (gethash (gnus-group-short-name b
) freq
0)))))))))
628 "%s: strategy %s on %s produced %s"
629 log-agent gnus-registry-split-strategy desc chosen
)
632 "%s: strategy %s on %s did not produce an answer"
634 (or gnus-registry-split-strategy
"default")
637 (setq groups
(and chosen
(list chosen
)))
639 (dolist (group groups
)
640 (let ((m1 (gnus-find-method-for-group group
))
641 (m2 (or gnus-command-method
642 (gnus-find-method-for-group gnus-newsgroup-name
)))
643 (short-name (gnus-group-short-name group
)))
644 (if (gnus-methods-equal-p m1 m2
)
646 ;; this is REALLY just for debugging
647 (when (not (equal group short-name
))
650 "%s: stripped group %s to %s"
651 log-agent group short-name
))
652 (add-to-list 'out short-name
))
656 "%s: ignored foreign group %s"
659 (setq out
(delq nil out
))
662 ((= (length out
) 1) out
)
666 "%s: no matches for %s '%s'."
671 "%s: too many extra matches (%s) for %s '%s'. Returning none."
672 log-agent out mode key
)
675 (defun gnus-registry-follow-group-p (group)
676 "Determines if a group name should be followed.
677 Consults `gnus-registry-unfollowed-groups' and
678 `nnmail-split-fancy-with-parent-ignore-groups'."
680 (not (or (gnus-grep-in-list
682 gnus-registry-unfollowed-groups
)
685 nnmail-split-fancy-with-parent-ignore-groups
)))))
687 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
688 ;; group/topic parameter and an associated variable!
690 ;; we do special logic for ignoring to accept regular expressions and
691 ;; nnmail-split-fancy-with-parent-ignore-groups as well
692 (defun gnus-registry-ignore-group-p (group)
693 "Determines if a group name should be ignored.
694 Consults `gnus-registry-ignored-groups' and
695 `nnmail-split-fancy-with-parent-ignore-groups'."
697 (or (gnus-grep-in-list
699 (delq nil
(mapcar (lambda (g)
702 ((and (listp g
) (nth 1 g
))
704 (t nil
))) gnus-registry-ignored-groups
)))
705 ;; only use `gnus-parameter-registry-ignore' if
706 ;; `gnus-registry-ignored-groups' is a list of lists
707 ;; (it can be a list of regexes)
708 (and (listp (nth 0 gnus-registry-ignored-groups
))
709 (get-buffer "*Group*") ; in automatic tests this is false
710 (gnus-parameter-registry-ignore group
))
713 nnmail-split-fancy-with-parent-ignore-groups
))))
715 (defun gnus-registry-wash-for-keywords (&optional force
)
716 "Get the keywords of the current article.
717 Overrides existing keywords with FORCE set non-nil."
719 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
721 (if (or (not (gnus-registry-get-id-key id
'keyword
))
723 (with-current-buffer gnus-article-buffer
725 (save-window-excursion
727 (narrow-to-region (point) (point-max))
728 (with-syntax-table gnus-adaptive-word-syntax-table
729 (while (re-search-forward "\\b\\w+\\b" nil t
)
730 (setq word
(gnus-string-remove-all-properties
731 (downcase (buffer-substring
732 (match-beginning 0) (match-end 0)))))
733 (if (> (length word
) 2)
734 (push word words
))))))
735 (gnus-registry-set-id-key id
'keyword words
)))))
737 (defun gnus-registry-keywords ()
738 (let ((table (registry-lookup-secondary gnus-registry-db
'keyword
)))
739 (when table
(maphash (lambda (k v
) k
) table
))))
741 (defun gnus-registry-find-keywords (keyword)
743 (completing-read "Keyword: " (gnus-registry-keywords) nil t
)))
744 (registry-lookup-secondary-value gnus-registry-db
'keyword keyword
))
746 (defun gnus-registry-register-message-ids ()
747 "Register the Message-ID of every article in the group"
748 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
749 (dolist (article gnus-newsgroup-articles
)
750 (let* ((id (gnus-registry-fetch-message-id-fast article
))
751 (groups (gnus-registry-get-id-key id
'group
)))
752 (unless (member gnus-newsgroup-name groups
)
753 (gnus-message 9 "Registry: Registering article %d with group %s"
754 article gnus-newsgroup-name
)
755 (gnus-registry-handle-action id nil gnus-newsgroup-name
756 (gnus-registry-fetch-simplified-message-subject-fast article
)
757 (gnus-registry-fetch-sender-fast article
)
758 (gnus-registry-fetch-recipients-fast article
)))))))
760 ;; message field fetchers
761 (defun gnus-registry-fetch-message-id-fast (article)
762 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
763 (if (and (numberp article
)
764 (assoc article
(gnus-data-list nil
)))
765 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
768 (defun gnus-registry-extract-addresses (text)
769 "Extract all the addresses in a normalized way from TEXT.
770 Returns an unsorted list of strings in the name <address> format.
771 Addresses without a name will say \"noname\"."
772 (mapcar (lambda (add)
773 (gnus-string-remove-all-properties
774 (let* ((name (or (nth 0 add
) "noname"))
776 (addr (if (bufferp addr
)
777 (with-current-buffer addr
780 (format "%s <%s>" name addr
))))
781 (mail-extract-address-components text t
)))
783 (defun gnus-registry-sort-addresses (&rest addresses
)
784 "Return a normalized and sorted list of ADDRESSES."
785 (sort (apply 'nconc
(mapcar 'gnus-registry-extract-addresses addresses
))
788 (defun gnus-registry-simplify-subject (subject)
789 (if (stringp subject
)
790 (gnus-simplify-subject subject
)
793 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
794 "Fetch the Subject quickly, using the internal gnus-data-list function"
795 (if (and (numberp article
)
796 (assoc article
(gnus-data-list nil
)))
797 (gnus-string-remove-all-properties
798 (gnus-registry-simplify-subject
799 (mail-header-subject (gnus-data-header
800 (assoc article
(gnus-data-list nil
))))))
803 (defun gnus-registry-fetch-sender-fast (article)
804 (gnus-registry-fetch-header-fast "from" article
))
806 (defun gnus-registry-fetch-recipients-fast (article)
807 (gnus-registry-sort-addresses
808 (or (ignore-errors (gnus-registry-fetch-header-fast "Cc" article
)) "")
809 (or (ignore-errors (gnus-registry-fetch-header-fast "To" article
)) "")))
811 (defun gnus-registry-fetch-header-fast (article header
)
812 "Fetch the HEADER quickly, using the internal gnus-data-list function"
813 (if (and (numberp article
)
814 (assoc article
(gnus-data-list nil
)))
815 (gnus-string-remove-all-properties
816 (cdr (assq header
(gnus-data-header
817 (assoc article
(gnus-data-list nil
))))))
820 ;; registry marks glue
821 (defun gnus-registry-do-marks (type function
)
822 "For each known mark, call FUNCTION for each cell of type TYPE.
824 FUNCTION should take two parameters, a mark symbol and the cell value."
825 (dolist (mark-info gnus-registry-marks
)
826 (let* ((mark (car-safe mark-info
))
827 (data (cdr-safe mark-info
))
828 (cell-data (plist-get data type
)))
830 (funcall function mark cell-data
)))))
832 ;;; this is ugly code, but I don't know how to do it better
833 (defun gnus-registry-install-shortcuts ()
834 "Install the keyboard shortcuts and menus for the registry.
835 Uses `gnus-registry-marks' to find what shortcuts to install."
837 (setq gnus-registry-misc-menus nil
)
838 (gnus-registry-do-marks
841 (let ((function-format
842 (format "gnus-registry-%%s-article-%s-mark" mark
)))
844 ;;; The following generates these functions:
845 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
846 ;;; "Apply the Important mark to process-marked ARTICLES."
847 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
848 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
849 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
850 ;;; "Apply the Important mark to process-marked ARTICLES."
851 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
852 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
854 (dolist (remove '(t nil
))
855 (let* ((variant-name (if remove
"remove" "set"))
856 (function-name (format function-format variant-name
))
857 (shortcut (format "%c" data
))
858 (shortcut (if remove
(upcase shortcut
) shortcut
)))
859 (unintern function-name obarray
)
863 ,(intern function-name
)
864 ;; parameter definition
868 "%s the %s mark over process-marked ARTICLES."
869 (upcase-initials variant-name
)
871 ;; interactive definition
873 (gnus-summary-work-articles current-prefix-arg
))
876 ;; if this is called and the user doesn't want the
877 ;; registry enabled, we'll ask anyhow
878 (unless gnus-registry-install
879 (let ((gnus-registry-install 'ask
))
880 (gnus-registry-install-p)))
882 ;; now the user is asked if gnus-registry-install is 'ask
883 (when (gnus-registry-install-p)
884 (gnus-registry-set-article-mark-internal
885 ;; all this just to get the mark, I must be doing it wrong
886 (intern ,(symbol-name mark
))
890 "Applying mark %s to %d articles"
891 ,(symbol-name mark
) (length articles
))
892 (dolist (article articles
)
893 (gnus-summary-update-article
895 (assoc article
(gnus-data-list nil
)))))))
896 (push (intern function-name
) keys-plist
)
897 (push shortcut keys-plist
)
898 (push (vector (format "%s %s"
899 (upcase-initials variant-name
)
901 (intern function-name
) t
)
902 gnus-registry-misc-menus
)
905 "Defined mark handling function %s"
908 '(gnus-registry-mark-map "M" gnus-summary-mark-map
)
910 (add-hook 'gnus-summary-menu-hook
913 gnus-summary-misc-menu
915 (cons "Registry Marks" gnus-registry-misc-menus
))))))
917 (make-obsolete 'gnus-registry-user-format-function-M
918 'gnus-registry-article-marks-to-chars
"24.1") ?
920 (defalias 'gnus-registry-user-format-function-M
921 'gnus-registry-article-marks-to-chars
)
924 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
925 (defun gnus-registry-article-marks-to-chars (headers)
926 "Show the marks for an article by the :char property"
927 (let* ((id (mail-header-message-id headers
))
928 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
929 (mapconcat (lambda (mark)
932 (assoc mark gnus-registry-marks
))
937 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
938 (defun gnus-registry-article-marks-to-names (headers)
939 "Show the marks for an article by name"
940 (let* ((id (mail-header-message-id headers
))
941 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
942 (mapconcat (lambda (mark) (symbol-name mark
)) marks
",")))
944 (defun gnus-registry-read-mark ()
945 "Read a mark name from the user with completion."
946 (let ((mark (gnus-completing-read
948 (mapcar 'symbol-name
(mapcar 'car gnus-registry-marks
))
950 (symbol-name gnus-registry-default-mark
))))
954 (defun gnus-registry-set-article-mark (&rest articles
)
955 "Apply a mark to process-marked ARTICLES."
956 (interactive (gnus-summary-work-articles current-prefix-arg
))
957 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
960 (defun gnus-registry-remove-article-mark (&rest articles
)
961 "Remove a mark from process-marked ARTICLES."
962 (interactive (gnus-summary-work-articles current-prefix-arg
))
963 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
966 (defun gnus-registry-set-article-mark-internal (mark
970 "Apply or remove MARK across a list of ARTICLES."
971 (let ((article-id-list
972 (mapcar 'gnus-registry-fetch-message-id-fast articles
)))
973 (dolist (id article-id-list
)
974 (let* ((marks (delq mark
(gnus-registry-get-id-key id
'mark
)))
975 (marks (if remove marks
(cons mark marks
))))
977 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
978 (if remove
"Removing" "Adding")
980 (gnus-registry-set-id-key id
'mark marks
)))))
982 (defun gnus-registry-get-article-marks (&rest articles
)
983 "Get the Gnus registry marks for ARTICLES and show them if interactive.
984 Uses process/prefix conventions. For multiple articles,
985 only the last one's marks are returned."
986 (interactive (gnus-summary-work-articles 1))
987 (let* ((article (last articles
))
988 (id (gnus-registry-fetch-message-id-fast article
))
989 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
990 (when (interactive-p)
991 (gnus-message 1 "Marks are %S" marks
))
994 (defun gnus-registry-group-count (id)
995 "Get the number of groups of a message, based on the message ID."
996 (length (gnus-registry-get-id-key id
'group
)))
998 (defun gnus-registry-get-or-make-entry (id)
999 (let* ((db gnus-registry-db
)
1000 ;; safe if not found
1001 (entries (registry-lookup db
(list id
))))
1003 (when (null entries
)
1004 (gnus-registry-insert db id
(list (list 'creation-time
(current-time))
1005 '(group) '(sender) '(subject)))
1006 (setq entries
(registry-lookup db
(list id
))))
1008 (nth 1 (assoc id entries
))))
1010 (defun gnus-registry-delete-entries (idlist)
1011 (registry-delete gnus-registry-db idlist nil
))
1013 (defun gnus-registry-get-id-key (id key
)
1014 (cdr-safe (assq key
(gnus-registry-get-or-make-entry id
))))
1016 (defun gnus-registry-set-id-key (id key vals
)
1017 (let* ((db gnus-registry-db
)
1018 (entry (gnus-registry-get-or-make-entry id
)))
1019 (registry-delete db
(list id
) nil
)
1020 (setq entry
(cons (cons key vals
) (assq-delete-all key entry
)))
1021 (gnus-registry-insert db id entry
)
1024 (defun gnus-registry-insert (db id entry
)
1025 "Just like `registry-insert' but tries to prune on error."
1026 (when (registry-full db
)
1027 (message "Trying to prune the registry because it's full")
1028 (registry-prune db
))
1029 (registry-insert db id entry
)
1032 (defun gnus-registry-import-eld (file)
1033 (interactive "fOld registry file to import? ")
1035 ;; (setq gnus-registry-alist '(
1036 ;; ("<messageID>" ((marks nil)
1037 ;; (mtime 19365 1776 440496)
1038 ;; (sender . "root (Cron Daemon)")
1039 ;; (subject . "Cron"))
1040 ;; "cron" "nnml+private:cron")
1042 (when (boundp 'gnus-registry-alist
)
1043 (let* ((old (symbol-value 'gnus-registry-alist
))
1045 (expected (length old
))
1047 (while (car-safe old
)
1049 ;; don't use progress reporters for backwards compatibility
1050 (when (and (< 0 expected
)
1051 (= 0 (mod count
100)))
1052 (message "importing: %d of %d (%.2f%%)"
1053 count expected
(/ (* 100 count
) expected
)))
1054 (setq entry
(car-safe old
)
1056 (let* ((id (car-safe entry
))
1057 (new-entry (gnus-registry-get-or-make-entry id
))
1058 (rest (cdr-safe entry
))
1059 (groups (loop for p in rest
1063 ;; remove all the strings from the entry
1065 (if (stringp elem
) (setq rest
(delq elem rest
))))
1066 (gnus-registry-set-id-key id
'group groups
)
1067 ;; just use the first extra element
1068 (setq rest
(car-safe rest
))
1069 (while (car-safe rest
)
1070 (setq extra-cell
(car-safe rest
)
1071 key
(car-safe extra-cell
)
1072 val
(cdr-safe extra-cell
)
1073 rest
(cdr-safe rest
))
1074 (when (and val
(atom val
))
1075 (setq val
(list val
)))
1076 (gnus-registry-set-id-key id key val
))))
1077 (message "Import done, collected %d entries" count
))))
1079 (ert-deftest gnus-registry-misc-test
()
1080 (should-error (gnus-registry-extract-addresses '("" "")))
1082 (should (equal '("Ted Zlatanov <tzz@lifelogs.com>"
1083 "noname <ed@you.me>"
1084 "noname <cyd@stupidchicken.com>"
1085 "noname <tzz@lifelogs.com>")
1086 (gnus-registry-extract-addresses
1087 (concat "Ted Zlatanov <tzz@lifelogs.com>, "
1088 "ed <ed@you.me>, " ; "ed" is not a valid name here
1089 "cyd@stupidchicken.com, "
1090 "tzz@lifelogs.com")))))
1092 (ert-deftest gnus-registry-usage-test
()
1094 (tempfile (make-temp-file "gnus-registry-persist"))
1095 (db (gnus-registry-make-db tempfile
))
1096 (gnus-registry-db db
)
1098 (message "Adding %d keys to the test Gnus registry" n
)
1100 (let ((id (number-to-string i
)))
1101 (gnus-registry-handle-action id
1102 (if (>= 50 i
) "fromgroup" nil
)
1105 (format "subject %d" (mod i
10)))
1107 (format "sender %d" (mod i
10))))))
1108 (message "Testing Gnus registry size is %d" n
)
1109 (should (= n
(registry-size db
)))
1110 (message "Looking up individual keys (registry-lookup)")
1111 (should (equal (loop for e
1113 (registry-lookup db
'("20" "83" "72")))
1114 collect
(assq 'subject e
)
1115 collect
(assq 'sender e
)
1116 collect
(assq 'group e
))
1117 '((subject "subject 0") (sender "sender 0") (group "togroup")
1118 (subject) (sender) (group "togroup")
1119 (subject) (sender "sender 2") (group "togroup"))))
1121 (message "Looking up individual keys (gnus-registry-id-key)")
1122 (should (equal (gnus-registry-get-id-key "34" 'group
) '("togroup")))
1123 (should (equal (gnus-registry-get-id-key "34" 'subject
) '("subject 4")))
1124 (message "Trying to insert a duplicate key")
1125 (should-error (gnus-registry-insert db
"55" '()))
1126 (message "Looking up individual keys (gnus-registry-get-or-make-entry)")
1127 (should (gnus-registry-get-or-make-entry "22"))
1128 (message "Saving the Gnus registry to %s" tempfile
)
1129 (should (gnus-registry-save tempfile db
))
1130 (setq size
(nth 7 (file-attributes tempfile
)))
1131 (message "Saving the Gnus registry to %s: size %d" tempfile size
)
1134 (insert-file-contents-literally tempfile
)
1135 (should (looking-at (concat ";; Object "
1137 "\n;; EIEIO PERSISTENT OBJECT"))))
1138 (message "Reading Gnus registry back")
1139 (setq back
(eieio-persistent-read tempfile
))
1141 (message "Read Gnus registry back: %d keys, expected %d==%d"
1142 (registry-size back
) n
(registry-size db
))
1143 (should (= (registry-size back
) n
))
1144 (should (= (registry-size back
) (registry-size db
)))
1145 (delete-file tempfile
)
1146 (message "Pruning Gnus registry to 0 by setting :max-soft")
1147 (oset db
:max-soft
0)
1149 (should (= (registry-size db
) 0)))
1150 (message "Done with Gnus registry usage testing."))
1153 (defun gnus-registry-initialize ()
1154 "Initialize the Gnus registry."
1156 (gnus-message 5 "Initializing the registry")
1157 (gnus-registry-install-hooks)
1158 (gnus-registry-install-shortcuts)
1159 (gnus-registry-read))
1162 (defun gnus-registry-install-hooks ()
1163 "Install the registry hooks."
1165 (setq gnus-registry-enabled t
)
1166 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1167 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1168 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1169 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1171 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1172 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
1174 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1176 (defun gnus-registry-unload-hook ()
1177 "Uninstall the registry hooks."
1179 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1180 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1181 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1182 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1184 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1185 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
1187 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
)
1188 (setq gnus-registry-enabled nil
))
1190 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1192 (defun gnus-registry-install-p ()
1193 "If the registry is not already enabled, and `gnus-registry-install' is t,
1194 the registry is enabled. If `gnus-registry-install' is `ask',
1195 the user is asked first. Returns non-nil iff the registry is enabled."
1197 (unless gnus-registry-enabled
1198 (when (if (eq gnus-registry-install
'ask
)
1200 (concat "Enable the Gnus registry? "
1201 "See the variable `gnus-registry-install' "
1202 "to get rid of this query permanently. "))
1203 gnus-registry-install
)
1204 (gnus-registry-initialize)))
1205 gnus-registry-enabled
)
1207 ;; TODO: a few things
1209 (provide 'gnus-registry
)
1211 ;;; gnus-registry.el ends here