1 ;;; gnus-registry.el --- article registry for Gnus
3 ;; Copyright (C) 2002-2011 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-summary-misc-menu
) ;; Avoid byte compiler warning.
169 (defvar gnus-registry-misc-menus nil
) ; ugly way to keep the menus
171 (make-obsolete-variable 'gnus-registry-clean-empty nil
"23.4")
172 (make-obsolete-variable 'gnus-registry-use-long-group-names nil
"23.4")
173 (make-obsolete-variable 'gnus-registry-max-track-groups nil
"23.4")
174 (make-obsolete-variable 'gnus-registry-entry-caching nil
"23.4")
175 (make-obsolete-variable 'gnus-registry-trim-articles-without-groups nil
"23.4")
177 (defcustom gnus-registry-track-extra
'(subject sender recipient
)
178 "Whether the registry should track extra data about a message.
179 The subject, recipients (To: and Cc:), and Sender (From:) headers
180 are tracked this way by default."
181 :group
'gnus-registry
183 '(set :tag
"Tracking choices"
184 (const :tag
"Track by subject (Subject: header)" subject
)
185 (const :tag
"Track by recipient (To: and Cc: headers)" recipient
)
186 (const :tag
"Track by sender (From: header)" sender
)))
188 (defcustom gnus-registry-split-strategy nil
189 "The splitting strategy applied to the keys in `gnus-registry-track-extra'.
191 Given a set of unique found groups G and counts for each element
192 of G, and a key K (typically 'sender or 'subject):
194 When nil, if G has only one element, use it. Otherwise give up.
195 This is the fastest but also least useful strategy.
197 When 'majority, use the majority by count. So if there is a
198 group with the most articles counted by K, use that. Ties are
199 resolved in no particular order, simply the first one found wins.
200 This is the slowest strategy but also the most accurate one.
202 When 'first, the first element of G wins. This is fast and
203 should be OK if your senders and subjects don't \"bleed\" across
205 :group
'gnus-registry
207 '(choice :tag
"Splitting strategy"
208 (const :tag
"Only use single choices, discard multiple matches" nil
)
209 (const :tag
"Majority of matches wins" majority
)
210 (const :tag
"First found wins" first
)))
212 (defcustom gnus-registry-minimum-subject-length
5
213 "The minimum length of a subject before it's considered trackable."
214 :group
'gnus-registry
217 (defcustom gnus-registry-extra-entries-precious
'(mark)
218 "What extra keys are precious, meaning entries with them won't get pruned.
219 By default, 'mark is included, so articles with marks are
222 Before you save the Gnus registry, it's pruned. Any entries with
223 keys in this list will not be pruned. All other entries go to
225 :group
'gnus-registry
226 :type
'(repeat symbol
))
228 (defcustom gnus-registry-cache-file
230 (or gnus-dribble-directory gnus-home-directory
"~/")
231 ".gnus.registry.eioio")
232 "File where the Gnus registry will be stored."
233 :group
'gnus-registry
236 (defcustom gnus-registry-max-entries nil
237 "Maximum number of entries in the registry, nil for unlimited."
238 :group
'gnus-registry
239 :type
'(radio (const :format
"Unlimited " nil
)
240 (integer :format
"Maximum number: %v")))
242 (defcustom gnus-registry-max-pruned-entries nil
243 "Maximum number of pruned entries in the registry, nil for unlimited."
244 :group
'gnus-registry
245 :type
'(radio (const :format
"Unlimited " nil
)
246 (integer :format
"Maximum number: %v")))
248 (defun gnus-registry-fixup-registry (db)
250 (let ((old (oref db
:tracked
)))
252 (append gnus-registry-extra-entries-precious
255 (or gnus-registry-max-entries
256 most-positive-fixnum
))
257 (oset db
:prune-factor
260 (or gnus-registry-max-pruned-entries
261 most-positive-fixnum
))
263 (append gnus-registry-track-extra
264 '(mark group keyword
)))
265 (when (not (equal old
(oref db
:tracked
)))
266 (gnus-message 4 "Reindexing the Gnus registry (tracked change)")
267 (registry-reindex db
))))
270 (defun gnus-registry-make-db (&optional file
)
271 (interactive "fGnus registry persistence file: \n")
272 (gnus-registry-fixup-registry
275 :file
(or file gnus-registry-cache-file
)
276 ;; these parameters are set in `gnus-registry-fixup-registry'
277 :max-hard most-positive-fixnum
278 :max-soft most-positive-fixnum
282 (defvar gnus-registry-db
(gnus-registry-make-db)
283 "*The article registry by Message ID. See `registry-db'")
285 ;; top-level registry data management
286 (defun gnus-registry-remake-db (&optional forsure
)
287 "Remake the registry database after customization.
288 This is not required after changing `gnus-registry-cache-file'."
289 (interactive (list (y-or-n-p "Remake and CLEAR the Gnus registry? ")))
291 (gnus-message 4 "Remaking the Gnus registry")
292 (setq gnus-registry-db
(gnus-registry-make-db))))
294 (defun gnus-registry-read ()
295 "Read the registry cache file."
297 (let ((file gnus-registry-cache-file
))
300 (gnus-message 5 "Reading Gnus registry from %s..." file
)
301 (setq gnus-registry-db
(gnus-registry-fixup-registry
302 (eieio-persistent-read file
)))
303 (gnus-message 5 "Reading Gnus registry from %s...done" file
))
307 "The Gnus registry could not be loaded from %s, creating a new one"
309 (gnus-registry-remake-db t
)))))
311 (defun gnus-registry-save (&optional file db
)
312 "Save the registry cache file."
314 (let ((file (or file gnus-registry-cache-file
))
315 (db (or db gnus-registry-db
)))
316 (gnus-message 5 "Saving Gnus registry (%d entries) to %s..."
317 (registry-size db
) file
)
319 ;; TODO: call (gnus-string-remove-all-properties v) on all elements?
320 (eieio-persistent-save db file
)
321 (gnus-message 5 "Saving Gnus registry (size %d) to %s...done"
322 (registry-size db
) file
)))
324 (defun gnus-registry-remove-ignored ()
326 (let* ((db gnus-registry-db
)
327 (grouphashtb (registry-lookup-secondary db
'group
))
328 (old-size (registry-size db
)))
329 (registry-reindex db
)
330 (loop for k being the hash-keys of grouphashtb
331 using
(hash-values v
)
332 when
(gnus-registry-ignore-group-p k
)
333 do
(registry-delete db v nil
))
334 (registry-reindex db
)
335 (gnus-message 4 "Removed %d ignored entries from the Gnus registry"
336 (- old-size
(registry-size db
)))))
338 ;; article move/copy/spool/delete actions
339 (defun gnus-registry-action (action data-header from
&optional to method
)
340 (let* ((id (mail-header-id data-header
))
341 (subject (mail-header-subject data-header
))
342 (extra (mail-header-extra data-header
))
343 (recipients (gnus-registry-sort-addresses
344 (or (cdr-safe (assq 'Cc extra
)) "")
345 (or (cdr-safe (assq 'To extra
)) "")))
346 (sender (nth 0 (gnus-registry-extract-addresses
347 (mail-header-from data-header
))))
348 (from (gnus-group-guess-full-name-from-command-method from
))
349 (to (if to
(gnus-group-guess-full-name-from-command-method to
) nil
))
350 (to-name (if to to
"the Bit Bucket")))
351 (gnus-message 7 "Gnus registry: article %s %s from %s to %s"
352 id
(if method
"respooling" "going") from to
)
354 (gnus-registry-handle-action
356 ;; unless copying, remove the old "from" group
357 (if (not (equal 'copy action
)) from nil
)
358 to subject sender recipients
)))
360 (defun gnus-registry-spool-action (id group
&optional subject sender recipients
)
361 (let ((to (gnus-group-guess-full-name-from-command-method group
))
362 (recipients (or recipients
363 (gnus-registry-sort-addresses
364 (or (message-fetch-field "cc") "")
365 (or (message-fetch-field "to") ""))))
366 (subject (or subject
(message-fetch-field "subject")))
367 (sender (or sender
(message-fetch-field "from"))))
368 (when (and (stringp id
) (string-match "\r$" id
))
369 (setq id
(substring id
0 -
1)))
370 (gnus-message 7 "Gnus registry: article %s spooled to %s"
373 (gnus-registry-handle-action id nil to subject sender recipients
)))
375 (defun gnus-registry-handle-action (id from to subject sender
376 &optional recipients
)
379 "gnus-registry-handle-action %S" (list id from to subject sender recipients
))
380 (let ((db gnus-registry-db
)
381 ;; if the group is ignored, set the destination to nil (same as delete)
382 (to (if (gnus-registry-ignore-group-p to
) nil to
))
384 (entry (gnus-registry-get-or-make-entry id
))
385 (subject (gnus-string-remove-all-properties
386 (gnus-registry-simplify-subject subject
)))
387 (sender (gnus-string-remove-all-properties sender
)))
389 ;; this could be done by calling `gnus-registry-set-id-key'
390 ;; several times but it's better to bunch the transactions
393 (registry-delete db
(list id
) nil
)
395 (setq entry
(cons (delete from
(assoc 'group entry
))
396 (assq-delete-all 'group entry
))))
398 (dolist (kv `((group ,to
)
400 (recipient ,@recipients
)
403 (let ((new (or (assq (first kv
) entry
)
405 (dolist (toadd (cdr kv
))
406 (add-to-list 'new toadd t
))
407 (setq entry
(cons new
408 (assq-delete-all (first kv
) entry
))))))
409 (gnus-message 10 "Gnus registry: new entry for %s is %S"
412 (gnus-registry-insert db id entry
)))
414 ;; Function for nn{mail|imap}-split-fancy: look up all references in
415 ;; the cache and if a match is found, return that group.
416 (defun gnus-registry-split-fancy-with-parent ()
417 "Split this message into the same group as its parent. The parent
418 is obtained from the registry. This function can be used as an entry
419 in `nnmail-split-fancy' or `nnimap-split-fancy', for example like
420 this: (: gnus-registry-split-fancy-with-parent)
422 This function tracks ALL backends, unlike
423 `nnmail-split-fancy-with-parent' which tracks only nnmail
426 For a message to be split, it looks for the parent message in the
427 References or In-Reply-To header and then looks in the registry
428 to see which group that message was put in. This group is
429 returned, unless `gnus-registry-follow-group-p' return nil for
432 See the Info node `(gnus)Fancy Mail Splitting' for more details."
433 (let* ((refstr (or (message-fetch-field "references") "")) ; guaranteed
434 (reply-to (message-fetch-field "in-reply-to")) ; may be nil
435 ;; now, if reply-to is valid, append it to the References
437 (concat refstr
" " reply-to
)
439 (references (and refstr
(gnus-extract-references refstr
)))
440 ;; these may not be used, but the code is cleaner having them up here
441 (sender (gnus-string-remove-all-properties
442 (message-fetch-field "from")))
443 (recipients (gnus-registry-sort-addresses
444 (or (message-fetch-field "cc") "")
445 (or (message-fetch-field "to") "")))
446 (subject (gnus-string-remove-all-properties
447 (gnus-registry-simplify-subject
448 (message-fetch-field "subject"))))
450 (nnmail-split-fancy-with-parent-ignore-groups
451 (if (listp nnmail-split-fancy-with-parent-ignore-groups
)
452 nnmail-split-fancy-with-parent-ignore-groups
453 (list nnmail-split-fancy-with-parent-ignore-groups
))))
454 (gnus-registry--split-fancy-with-parent-internal
455 :references references
458 :recipients recipients
460 :log-agent
"Gnus registry fancy splitting with parent")))
462 (defun* gnus-registry--split-fancy-with-parent-internal
464 &key references refstr sender subject recipients log-agent
468 "gnus-registry--split-fancy-with-parent-internal %S" spec
)
469 (let ((db gnus-registry-db
)
471 ;; this is a big chain of statements. it uses
472 ;; gnus-registry-post-process-groups to filter the results after
474 ;; the references string must be valid and parse to valid references
478 "%s is tracing references %s"
480 (dolist (reference (nreverse references
))
481 (gnus-message 9 "%s is looking up %s" log-agent reference
)
482 (loop for group in
(gnus-registry-get-id-key reference
'group
)
483 when
(gnus-registry-follow-group-p group
)
486 (gnus-message 7 "%s traced %s to %s" log-agent reference group
)
487 (push group found
))))
488 ;; filter the found groups and return them
489 ;; the found groups are the full groups
490 (setq found
(gnus-registry-post-process-groups
491 "references" refstr found
)))
493 ;; else: there were no matches, now try the extra tracking by subject
494 (when (and (null found
)
495 (memq 'subject gnus-registry-track-extra
)
497 (< gnus-registry-minimum-subject-length
(length subject
)))
502 (gnus-registry-get-id-key reference
'group
))
503 (registry-lookup-secondary-value db
'subject subject
)))))
505 (loop for group in groups
506 when
(gnus-registry-follow-group-p group
)
508 ;; warn more if gnus-registry-track-extra
509 (if gnus-registry-track-extra
7 9)
510 "%s (extra tracking) traced subject '%s' to %s"
511 log-agent subject group
)
513 ;; filter the found groups and return them
514 ;; the found groups are NOT the full groups
515 (setq found
(gnus-registry-post-process-groups
516 "subject" subject found
))))
518 ;; else: there were no matches, try the extra tracking by sender
519 (when (and (null found
)
520 (memq 'sender gnus-registry-track-extra
)
522 (not (gnus-grep-in-list
524 gnus-registry-unfollowed-addresses
)))
529 (gnus-registry-get-id-key reference
'group
))
530 (registry-lookup-secondary-value db
'sender sender
)))))
532 (loop for group in groups
533 when
(gnus-registry-follow-group-p group
)
535 ;; warn more if gnus-registry-track-extra
536 (if gnus-registry-track-extra
7 9)
537 "%s (extra tracking) traced sender '%s' to %s"
538 log-agent sender group
)
541 ;; filter the found groups and return them
542 ;; the found groups are NOT the full groups
543 (setq found
(gnus-registry-post-process-groups
544 "sender" sender found
)))
546 ;; else: there were no matches, try the extra tracking by recipient
547 (when (and (null found
)
548 (memq 'recipient gnus-registry-track-extra
)
550 (dolist (recp recipients
)
551 (when (and (null found
)
552 (not (gnus-grep-in-list
554 gnus-registry-unfollowed-addresses
)))
555 (let ((groups (apply 'append
558 (gnus-registry-get-id-key reference
'group
))
559 (registry-lookup-secondary-value
560 db
'recipient recp
)))))
562 (loop for group in groups
563 when
(gnus-registry-follow-group-p group
)
565 ;; warn more if gnus-registry-track-extra
566 (if gnus-registry-track-extra
7 9)
567 "%s (extra tracking) traced recipient '%s' to %s"
568 log-agent recp group
)
569 and collect group
)))))
571 ;; filter the found groups and return them
572 ;; the found groups are NOT the full groups
573 (setq found
(gnus-registry-post-process-groups
574 "recipients" (mapconcat 'identity recipients
", ") found
)))
576 ;; after the (cond) we extract the actual value safely
579 (defun gnus-registry-post-process-groups (mode key groups
)
580 "Inspects GROUPS found by MODE for KEY to determine which ones to follow.
582 MODE can be 'subject' or 'sender' for example. The KEY is the
583 value by which MODE was searched.
585 Transforms each group name to the equivalent short name.
587 Checks if the current Gnus method (from `gnus-command-method' or
588 from `gnus-newsgroup-name') is the same as the group's method.
589 Foreign methods are not supported so they are rejected.
591 Reduces the list to a single group, or complains if that's not
592 possible. Uses `gnus-registry-split-strategy'."
593 (let ((log-agent "gnus-registry-post-process-group")
594 (desc (format "%d groups" (length groups
)))
596 ;; the strategy can be nil, in which case chosen is nil
598 (case gnus-registry-split-strategy
599 ;; default, take only one-element lists into chosen
601 (and (= (length groups
) 1)
608 (let ((freq (make-hash-table
611 (mapc (lambda (x) (let ((x (gnus-group-short-name x
)))
612 (puthash x
(1+ (gethash x freq
0)) freq
)))
614 (setq desc
(format "%d groups, %d unique"
616 (hash-table-count freq
)))
620 (> (gethash (gnus-group-short-name a
) freq
0)
621 (gethash (gnus-group-short-name b
) freq
0)))))))))
626 "%s: strategy %s on %s produced %s"
627 log-agent gnus-registry-split-strategy desc chosen
)
630 "%s: strategy %s on %s did not produce an answer"
632 (or gnus-registry-split-strategy
"default")
635 (setq groups
(and chosen
(list chosen
)))
637 (dolist (group groups
)
638 (let ((m1 (gnus-find-method-for-group group
))
639 (m2 (or gnus-command-method
640 (gnus-find-method-for-group gnus-newsgroup-name
)))
641 (short-name (gnus-group-short-name group
)))
642 (if (gnus-methods-equal-p m1 m2
)
644 ;; this is REALLY just for debugging
645 (when (not (equal group short-name
))
648 "%s: stripped group %s to %s"
649 log-agent group short-name
))
650 (add-to-list 'out short-name
))
654 "%s: ignored foreign group %s"
657 (setq out
(delq nil out
))
660 ((= (length out
) 1) out
)
664 "%s: no matches for %s '%s'."
669 "%s: too many extra matches (%s) for %s '%s'. Returning none."
670 log-agent out mode key
)
673 (defun gnus-registry-follow-group-p (group)
674 "Determines if a group name should be followed.
675 Consults `gnus-registry-unfollowed-groups' and
676 `nnmail-split-fancy-with-parent-ignore-groups'."
678 (not (or (gnus-grep-in-list
680 gnus-registry-unfollowed-groups
)
683 nnmail-split-fancy-with-parent-ignore-groups
)))))
685 ;; note that gnus-registry-ignored-groups is defined in gnus.el as a
686 ;; group/topic parameter and an associated variable!
688 ;; we do special logic for ignoring to accept regular expressions and
689 ;; nnmail-split-fancy-with-parent-ignore-groups as well
690 (defun gnus-registry-ignore-group-p (group)
691 "Determines if a group name should be ignored.
692 Consults `gnus-registry-ignored-groups' and
693 `nnmail-split-fancy-with-parent-ignore-groups'."
695 (or (gnus-grep-in-list
697 (delq nil
(mapcar (lambda (g)
700 ((and (listp g
) (nth 1 g
))
702 (t nil
))) gnus-registry-ignored-groups
)))
703 ;; only use `gnus-parameter-registry-ignore' if
704 ;; `gnus-registry-ignored-groups' is a list of lists
705 ;; (it can be a list of regexes)
706 (and (listp (nth 0 gnus-registry-ignored-groups
))
707 (get-buffer "*Group*") ; in automatic tests this is false
708 (gnus-parameter-registry-ignore group
))
711 nnmail-split-fancy-with-parent-ignore-groups
))))
713 (defun gnus-registry-wash-for-keywords (&optional force
)
714 "Get the keywords of the current article.
715 Overrides existing keywords with FORCE set non-nil."
717 (let ((id (gnus-registry-fetch-message-id-fast gnus-current-article
))
719 (if (or (not (gnus-registry-get-id-key id
'keyword
))
721 (with-current-buffer gnus-article-buffer
723 (save-window-excursion
725 (narrow-to-region (point) (point-max))
726 (with-syntax-table gnus-adaptive-word-syntax-table
727 (while (re-search-forward "\\b\\w+\\b" nil t
)
728 (setq word
(gnus-string-remove-all-properties
729 (downcase (buffer-substring
730 (match-beginning 0) (match-end 0)))))
731 (if (> (length word
) 2)
732 (push word words
))))))
733 (gnus-registry-set-id-key id
'keyword words
)))))
735 (defun gnus-registry-keywords ()
736 (let ((table (registry-lookup-secondary gnus-registry-db
'keyword
)))
737 (when table
(maphash (lambda (k v
) k
) table
))))
739 (defun gnus-registry-find-keywords (keyword)
741 (completing-read "Keyword: " (gnus-registry-keywords) nil t
)))
742 (registry-lookup-secondary-value gnus-registry-db
'keyword keyword
))
744 (defun gnus-registry-register-message-ids ()
745 "Register the Message-ID of every article in the group"
746 (unless (gnus-parameter-registry-ignore gnus-newsgroup-name
)
747 (dolist (article gnus-newsgroup-articles
)
748 (let* ((id (gnus-registry-fetch-message-id-fast article
))
749 (groups (gnus-registry-get-id-key id
'group
)))
750 (unless (member gnus-newsgroup-name groups
)
751 (gnus-message 9 "Registry: Registering article %d with group %s"
752 article gnus-newsgroup-name
)
753 (gnus-registry-handle-action id nil gnus-newsgroup-name
754 (gnus-registry-fetch-simplified-message-subject-fast article
)
755 (gnus-registry-fetch-sender-fast article
)
756 (gnus-registry-fetch-recipients-fast article
)))))))
758 ;; message field fetchers
759 (defun gnus-registry-fetch-message-id-fast (article)
760 "Fetch the Message-ID quickly, using the internal gnus-data-list function"
761 (if (and (numberp article
)
762 (assoc article
(gnus-data-list nil
)))
763 (mail-header-id (gnus-data-header (assoc article
(gnus-data-list nil
))))
766 (defun gnus-registry-extract-addresses (text)
767 "Extract all the addresses in a normalized way from TEXT.
768 Returns an unsorted list of strings in the name <address> format.
769 Addresses without a name will say \"noname\"."
770 (mapcar (lambda (add)
771 (gnus-string-remove-all-properties
772 (let* ((name (or (nth 0 add
) "noname"))
774 (addr (if (bufferp addr
)
775 (with-current-buffer addr
778 (format "%s <%s>" name addr
))))
779 (mail-extract-address-components text t
)))
781 (defun gnus-registry-sort-addresses (&rest addresses
)
782 "Return a normalized and sorted list of ADDRESSES."
783 (sort (apply 'nconc
(mapcar 'gnus-registry-extract-addresses addresses
))
786 (defun gnus-registry-simplify-subject (subject)
787 (if (stringp subject
)
788 (gnus-simplify-subject subject
)
791 (defun gnus-registry-fetch-simplified-message-subject-fast (article)
792 "Fetch the Subject quickly, using the internal gnus-data-list function"
793 (if (and (numberp article
)
794 (assoc article
(gnus-data-list nil
)))
795 (gnus-string-remove-all-properties
796 (gnus-registry-simplify-subject
797 (mail-header-subject (gnus-data-header
798 (assoc article
(gnus-data-list nil
))))))
801 (defun gnus-registry-fetch-sender-fast (article)
802 (gnus-registry-fetch-header-fast "from" article
))
804 (defun gnus-registry-fetch-recipients-fast (article)
805 (gnus-registry-sort-addresses
806 (or (ignore-errors (gnus-registry-fetch-header-fast "Cc" article
)) "")
807 (or (ignore-errors (gnus-registry-fetch-header-fast "To" article
)) "")))
809 (defun gnus-registry-fetch-header-fast (article header
)
810 "Fetch the HEADER quickly, using the internal gnus-data-list function"
811 (if (and (numberp article
)
812 (assoc article
(gnus-data-list nil
)))
813 (gnus-string-remove-all-properties
814 (cdr (assq header
(gnus-data-header
815 (assoc article
(gnus-data-list nil
))))))
818 ;; registry marks glue
819 (defun gnus-registry-do-marks (type function
)
820 "For each known mark, call FUNCTION for each cell of type TYPE.
822 FUNCTION should take two parameters, a mark symbol and the cell value."
823 (dolist (mark-info gnus-registry-marks
)
824 (let* ((mark (car-safe mark-info
))
825 (data (cdr-safe mark-info
))
826 (cell-data (plist-get data type
)))
828 (funcall function mark cell-data
)))))
830 ;;; this is ugly code, but I don't know how to do it better
831 (defun gnus-registry-install-shortcuts ()
832 "Install the keyboard shortcuts and menus for the registry.
833 Uses `gnus-registry-marks' to find what shortcuts to install."
835 (setq gnus-registry-misc-menus nil
)
836 (gnus-registry-do-marks
839 (let ((function-format
840 (format "gnus-registry-%%s-article-%s-mark" mark
)))
842 ;;; The following generates these functions:
843 ;;; (defun gnus-registry-set-article-Important-mark (&rest articles)
844 ;;; "Apply the Important mark to process-marked ARTICLES."
845 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
846 ;;; (gnus-registry-set-article-mark-internal 'Important articles nil t))
847 ;;; (defun gnus-registry-remove-article-Important-mark (&rest articles)
848 ;;; "Apply the Important mark to process-marked ARTICLES."
849 ;;; (interactive (gnus-summary-work-articles current-prefix-arg))
850 ;;; (gnus-registry-set-article-mark-internal 'Important articles t t))
852 (dolist (remove '(t nil
))
853 (let* ((variant-name (if remove
"remove" "set"))
854 (function-name (format function-format variant-name
))
855 (shortcut (format "%c" data
))
856 (shortcut (if remove
(upcase shortcut
) shortcut
)))
857 (unintern function-name obarray
)
861 ,(intern function-name
)
862 ;; parameter definition
866 "%s the %s mark over process-marked ARTICLES."
867 (upcase-initials variant-name
)
869 ;; interactive definition
871 (gnus-summary-work-articles current-prefix-arg
))
874 ;; if this is called and the user doesn't want the
875 ;; registry enabled, we'll ask anyhow
876 (when (eq gnus-registry-install nil
)
877 (setq gnus-registry-install
'ask
))
879 ;; now the user is asked if gnus-registry-install is 'ask
880 (when (gnus-registry-install-p)
881 (gnus-registry-set-article-mark-internal
882 ;; all this just to get the mark, I must be doing it wrong
883 (intern ,(symbol-name mark
))
887 "Applying mark %s to %d articles"
888 ,(symbol-name mark
) (length articles
))
889 (dolist (article articles
)
890 (gnus-summary-update-article
892 (assoc article
(gnus-data-list nil
)))))))
893 (push (intern function-name
) keys-plist
)
894 (push shortcut keys-plist
)
895 (push (vector (format "%s %s"
896 (upcase-initials variant-name
)
898 (intern function-name
) t
)
899 gnus-registry-misc-menus
)
902 "Defined mark handling function %s"
905 '(gnus-registry-mark-map "M" gnus-summary-mark-map
)
907 (add-hook 'gnus-summary-menu-hook
910 gnus-summary-misc-menu
912 (cons "Registry Marks" gnus-registry-misc-menus
))))))
914 (make-obsolete 'gnus-registry-user-format-function-M
915 'gnus-registry-article-marks-to-chars
"24.1") ?
918 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-chars)
919 (defun gnus-registry-article-marks-to-chars (headers)
920 "Show the marks for an article by the :char property"
921 (let* ((id (mail-header-message-id headers
))
922 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
923 (mapconcat (lambda (mark)
926 (assoc mark gnus-registry-marks
))
931 ;; (defalias 'gnus-user-format-function-M 'gnus-registry-article-marks-to-names)
932 (defun gnus-registry-article-marks-to-names (headers)
933 "Show the marks for an article by name"
934 (let* ((id (mail-header-message-id headers
))
935 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
936 (mapconcat (lambda (mark) (symbol-name mark
)) marks
",")))
938 (defun gnus-registry-read-mark ()
939 "Read a mark name from the user with completion."
940 (let ((mark (gnus-completing-read
942 (mapcar 'symbol-name
(mapcar 'car gnus-registry-marks
))
944 (symbol-name gnus-registry-default-mark
))))
948 (defun gnus-registry-set-article-mark (&rest articles
)
949 "Apply a mark to process-marked ARTICLES."
950 (interactive (gnus-summary-work-articles current-prefix-arg
))
951 (gnus-registry-set-article-mark-internal (gnus-registry-read-mark)
954 (defun gnus-registry-remove-article-mark (&rest articles
)
955 "Remove a mark from 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-set-article-mark-internal (mark
964 "Apply or remove MARK across a list of ARTICLES."
965 (let ((article-id-list
966 (mapcar 'gnus-registry-fetch-message-id-fast articles
)))
967 (dolist (id article-id-list
)
968 (let* ((marks (delq mark
(gnus-registry-get-id-key id
'mark
)))
969 (marks (if remove marks
(cons mark marks
))))
971 (gnus-message 1 "%s mark %s with message ID %s, resulting in %S"
972 (if remove
"Removing" "Adding")
974 (gnus-registry-set-id-key id
'mark marks
)))))
976 (defun gnus-registry-get-article-marks (&rest articles
)
977 "Get the Gnus registry marks for ARTICLES and show them if interactive.
978 Uses process/prefix conventions. For multiple articles,
979 only the last one's marks are returned."
980 (interactive (gnus-summary-work-articles 1))
981 (let* ((article (last articles
))
982 (id (gnus-registry-fetch-message-id-fast article
))
983 (marks (when id
(gnus-registry-get-id-key id
'mark
))))
984 (when (interactive-p)
985 (gnus-message 1 "Marks are %S" marks
))
988 (defun gnus-registry-group-count (id)
989 "Get the number of groups of a message, based on the message ID."
990 (length (gnus-registry-get-id-key id
'group
)))
992 (defun gnus-registry-get-or-make-entry (id)
993 (let* ((db gnus-registry-db
)
995 (entries (registry-lookup db
(list id
))))
998 (gnus-registry-insert db id
(list (list 'creation-time
(current-time))
999 '(group) '(sender) '(subject)))
1000 (setq entries
(registry-lookup db
(list id
))))
1002 (nth 1 (assoc id entries
))))
1004 (defun gnus-registry-delete-entries (idlist)
1005 (registry-delete gnus-registry-db idlist nil
))
1007 (defun gnus-registry-get-id-key (id key
)
1008 (cdr-safe (assq key
(gnus-registry-get-or-make-entry id
))))
1010 (defun gnus-registry-set-id-key (id key vals
)
1011 (let* ((db gnus-registry-db
)
1012 (entry (gnus-registry-get-or-make-entry id
)))
1013 (registry-delete db
(list id
) nil
)
1014 (setq entry
(cons (cons key vals
) (assq-delete-all key entry
)))
1015 (gnus-registry-insert db id entry
)
1018 (defun gnus-registry-insert (db id entry
)
1019 "Just like `registry-insert' but tries to prune on error."
1020 (when (registry-full db
)
1021 (message "Trying to prune the registry because it's full")
1022 (registry-prune db
))
1023 (registry-insert db id entry
)
1026 (defun gnus-registry-import-eld (file)
1027 (interactive "fOld registry file to import? ")
1029 ;; (setq gnus-registry-alist '(
1030 ;; ("<messageID>" ((marks nil)
1031 ;; (mtime 19365 1776 440496)
1032 ;; (sender . "root (Cron Daemon)")
1033 ;; (subject . "Cron"))
1034 ;; "cron" "nnml+private:cron")
1036 (when (boundp 'gnus-registry-alist
)
1037 (let* ((old (symbol-value 'gnus-registry-alist
))
1039 (expected (length old
))
1041 (while (car-safe old
)
1043 ;; don't use progress reporters for backwards compatibility
1044 (when (and (< 0 expected
)
1045 (= 0 (mod count
100)))
1046 (message "importing: %d of %d (%.2f%%)"
1047 count expected
(/ (* 100 count
) expected
)))
1048 (setq entry
(car-safe old
)
1050 (let* ((id (car-safe entry
))
1051 (new-entry (gnus-registry-get-or-make-entry id
))
1052 (rest (cdr-safe entry
))
1053 (groups (loop for p in rest
1057 ;; remove all the strings from the entry
1059 (if (stringp elem
) (setq rest
(delq elem rest
))))
1060 (gnus-registry-set-id-key id
'group groups
)
1061 ;; just use the first extra element
1062 (setq rest
(car-safe rest
))
1063 (while (car-safe rest
)
1064 (setq extra-cell
(car-safe rest
)
1065 key
(car-safe extra-cell
)
1066 val
(cdr-safe extra-cell
)
1067 rest
(cdr-safe rest
))
1068 (when (and val
(atom val
))
1069 (setq val
(list val
)))
1070 (gnus-registry-set-id-key id key val
))))
1071 (message "Import done, collected %d entries" count
))))
1073 (ert-deftest gnus-registry-misc-test
()
1074 (should-error (gnus-registry-extract-addresses '("" "")))
1076 (should (equal '("Ted Zlatanov <tzz@lifelogs.com>"
1077 "noname <ed@you.me>"
1078 "noname <cyd@stupidchicken.com>"
1079 "noname <tzz@lifelogs.com>")
1080 (gnus-registry-extract-addresses
1081 (concat "Ted Zlatanov <tzz@lifelogs.com>, "
1082 "ed <ed@you.me>, " ; "ed" is not a valid name here
1083 "cyd@stupidchicken.com, "
1084 "tzz@lifelogs.com")))))
1086 (ert-deftest gnus-registry-usage-test
()
1088 (tempfile (make-temp-file "gnus-registry-persist"))
1089 (db (gnus-registry-make-db tempfile
))
1090 (gnus-registry-db db
)
1092 (message "Adding %d keys to the test Gnus registry" n
)
1094 (let ((id (number-to-string i
)))
1095 (gnus-registry-handle-action id
1096 (if (>= 50 i
) "fromgroup" nil
)
1099 (format "subject %d" (mod i
10)))
1101 (format "sender %d" (mod i
10))))))
1102 (message "Testing Gnus registry size is %d" n
)
1103 (should (= n
(registry-size db
)))
1104 (message "Looking up individual keys (registry-lookup)")
1105 (should (equal (loop for e
1107 (registry-lookup db
'("20" "83" "72")))
1108 collect
(assq 'subject e
)
1109 collect
(assq 'sender e
)
1110 collect
(assq 'group e
))
1111 '((subject "subject 0") (sender "sender 0") (group "togroup")
1112 (subject) (sender) (group "togroup")
1113 (subject) (sender "sender 2") (group "togroup"))))
1115 (message "Looking up individual keys (gnus-registry-id-key)")
1116 (should (equal (gnus-registry-get-id-key "34" 'group
) '("togroup")))
1117 (should (equal (gnus-registry-get-id-key "34" 'subject
) '("subject 4")))
1118 (message "Trying to insert a duplicate key")
1119 (should-error (gnus-registry-insert db
"55" '()))
1120 (message "Looking up individual keys (gnus-registry-get-or-make-entry)")
1121 (should (gnus-registry-get-or-make-entry "22"))
1122 (message "Saving the Gnus registry to %s" tempfile
)
1123 (should (gnus-registry-save tempfile db
))
1124 (setq size
(nth 7 (file-attributes tempfile
)))
1125 (message "Saving the Gnus registry to %s: size %d" tempfile size
)
1128 (insert-file-contents-literally tempfile
)
1129 (should (looking-at (concat ";; Object "
1131 "\n;; EIEIO PERSISTENT OBJECT"))))
1132 (message "Reading Gnus registry back")
1133 (setq back
(eieio-persistent-read tempfile
))
1135 (message "Read Gnus registry back: %d keys, expected %d==%d"
1136 (registry-size back
) n
(registry-size db
))
1137 (should (= (registry-size back
) n
))
1138 (should (= (registry-size back
) (registry-size db
)))
1139 (delete-file tempfile
)
1140 (message "Pruning Gnus registry to 0 by setting :max-soft")
1141 (oset db
:max-soft
0)
1143 (should (= (registry-size db
) 0)))
1144 (message "Done with Gnus registry usage testing."))
1147 (defun gnus-registry-initialize ()
1148 "Initialize the Gnus registry."
1150 (gnus-message 5 "Initializing the registry")
1151 (setq gnus-registry-install t
) ; in case it was 'ask or nil
1152 (gnus-registry-install-hooks)
1153 (gnus-registry-install-shortcuts)
1154 (gnus-registry-read))
1157 (defun gnus-registry-install-hooks ()
1158 "Install the registry hooks."
1160 (add-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1161 (add-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1162 (add-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1163 (add-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1165 (add-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1166 (add-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
1168 (add-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1170 (defun gnus-registry-unload-hook ()
1171 "Uninstall the registry hooks."
1173 (remove-hook 'gnus-summary-article-move-hook
'gnus-registry-action
)
1174 (remove-hook 'gnus-summary-article-delete-hook
'gnus-registry-action
)
1175 (remove-hook 'gnus-summary-article-expire-hook
'gnus-registry-action
)
1176 (remove-hook 'nnmail-spool-hook
'gnus-registry-spool-action
)
1178 (remove-hook 'gnus-save-newsrc-hook
'gnus-registry-save
)
1179 (remove-hook 'gnus-read-newsrc-el-hook
'gnus-registry-read
)
1181 (remove-hook 'gnus-summary-prepare-hook
'gnus-registry-register-message-ids
))
1183 (add-hook 'gnus-registry-unload-hook
'gnus-registry-unload-hook
)
1185 (defun gnus-registry-install-p ()
1187 (when (eq gnus-registry-install
'ask
)
1188 (setq gnus-registry-install
1190 (concat "Enable the Gnus registry? "
1191 "See the variable `gnus-registry-install' "
1192 "to get rid of this query permanently. ")))
1193 (when gnus-registry-install
1194 ;; we just set gnus-registry-install to t, so initialize the registry!
1195 (gnus-registry-initialize)))
1196 ;;; we could call it here: (customize-variable 'gnus-registry-install)
1197 gnus-registry-install
)
1199 ;; TODO: a few things
1201 (provide 'gnus-registry
)
1203 ;;; gnus-registry.el ends here