1 ;;; gnorb-bbdb.el --- The BBDB-centric functions of gnorb
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Eric Abrahamsen <eric@ericabrahamsen.net>
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
31 (require 'gnorb-utils)
34 (defgroup gnorb-bbdb nil
35 "The BBDB bits of gnorb."
39 (defcustom gnorb-bbdb-org-tag-field 'org-tags
40 "The name (as a symbol) of the field to use for org tags."
44 (when (boundp 'bbdb-separator-alist) ;Allow compilation if BBDB is absent!
45 (unless (assoc gnorb-bbdb-org-tag-field bbdb-separator-alist)
46 (push `(,gnorb-bbdb-org-tag-field ":" ":") bbdb-separator-alist)))
48 (defcustom gnorb-bbdb-messages-field 'messages
49 "The name (as a symbol) of the field where links to recent gnus
50 messages from this record are stored.
52 \\<bbdb-mode-map>Records that do not have this field defined
53 will not collect links to messages: you have to call
54 \"\\[gnorb-bbdb-open-link]\" on the record once -- after that,
55 message links will be collected and updated automatically."
59 (defcustom gnorb-bbdb-collect-N-messages 5
60 "For records with a `gnorb-bbdb-messages-field' defined,
61 collect links to a maximum of this many messages."
65 (defcustom gnorb-bbdb-define-recent 'seen
66 "For records with a `gnorb-bbdb-message-tag-field' defined,
67 this variable controls how gnorb defines a \"recent\" message.
68 Setting it to the symbol seen will collect the messages most
69 recently opened and viewed. The symbol received means gnorb will
70 collect the most recent messages by Date header.
72 In other words, if this variable is set to 'received, and a
73 record's messages field is already full of recently-received
74 messages, opening a five-year-old message (for instance) from
75 this record will not push a link to the message into the field."
77 :type '(choice (const :tag "Most recently seen" 'seen)
78 (const :tag "Most recently received" 'received)))
80 (defcustom gnorb-bbdb-message-link-format-multi "%:count. %D: %:subject"
81 "How a single message is formatted in the list of recent messages.
82 This format string is used in multi-line record display.
84 Available information for each message includes the subject, the
85 date, and the message's count in the list, as an integer. You can
86 access subject and count using the %:subject and %:count escapes.
87 The message date can be formatted using any of the escapes
88 mentioned in the docstring of `format-time-string', which see."
92 (defcustom gnorb-bbdb-message-link-format-one "%:count"
93 "How a single message is formatted in the list of recent messages.
94 This format string is used in single-line display -- note that by
95 default, no user-created xfields are displayed in the 'one-line
96 layout found in `bbdb-layout-alist'. If you want this field to
97 appear there, put its name in the \"order\" list of the 'one-line
100 Available information for each message includes the subject, the
101 date, and the message's count in the list, as an integer. You can
102 access subject and count using the %:subject and %:count escapes.
103 The message date can be formatted using any of the escapes
104 mentioned in the docstring of `format-time-string', which see."
108 (defface gnorb-bbdb-link (org-compatible-face 'org-link nil)
109 "Custom face for displaying message links in the *BBDB* buffer.
110 Defaults to org-link."
113 (cl-defstruct gnorb-bbdb-link
114 subject date group id)
116 (defcustom gnorb-bbdb-posting-styles nil
117 "An alist of styles to use when composing messages to the BBDB
118 record(s) under point. This is entirely analogous to
119 `gnus-posting-styles', it simply works by examining record fields
120 rather than group names.
122 When composing a message to multiple contacts (using the \"*\"
123 prefix), the records will be scanned in order, with the record
124 initially under point (if any) set aside for last. That means
125 that, in the case of conflicting styles, the record under point
126 will override the others.
128 In order not to be too intrusive, this option has no effect on
129 the usual `bbdb-mail' command. Instead, the wrapper command
130 `gnorb-bbdb-mail' is provided, which consults this option and
131 then hands off to `bbdb-compose-mail'. If you'd always like to
132 use `gnorb-bbdb-mail', you can simply bind it to \"m\" in the
135 The value of the option should be a list of sexps, each one
136 matching a single field. The first element should match a field
137 name: one of the built-in fields like lastname, or an xfield.
138 Field names should be given as symbols.
140 The second element is a regexp used to match against the value of
141 the field (non-string field values will be cast to strings, if
142 possible). It can also be a cons of two strings, the first of
143 which matches the field label, the second the field value.
145 Alternately, the first element can be the name of a custom
146 function that is called with the record as its only argument, and
147 returns either t or nil. In this case, the second element of the
150 All following elements should be field setters for the message to
151 be composed, just as in `gnus-posting-styles'.
153 An example value might look like:"
156 (defvar message-mode-hook)
158 (when (fboundp 'bbdb-record-xfield-string)
159 (fset (intern (format "bbdb-read-xfield-%s"
160 gnorb-bbdb-org-tag-field))
161 (lambda (&optional init)
162 (gnorb-bbdb-read-org-tags init)))
164 (fset (intern (format "bbdb-display-%s-multi-line"
165 gnorb-bbdb-org-tag-field))
167 (gnorb-bbdb-display-org-tags record))))
169 (defun gnorb-bbdb-read-org-tags (&optional init)
170 "Read Org mode tags, with `completing-read-multiple'."
171 (if (string< "24.3" (substring emacs-version 0 4))
174 (cadr (assq gnorb-bbdb-org-tag-field
175 bbdb-separator-alist))
177 (crm-local-completion-map bbdb-crm-local-completion-map)
178 (table (cl-mapcar #'car
179 (org-global-tags-completion-table
180 (org-agenda-files))))
181 (init (if (consp init)
183 (nth 2 (assq gnorb-bbdb-org-tag-field
184 bbdb-separator-alist)))
186 (completing-read-multiple
189 (bbdb-split gnorb-bbdb-org-tag-field
190 (bbdb-read-string "Tags: " init))))
192 (defun gnorb-bbdb-display-org-tags (record)
193 "Display the Org tags associated with the record.
195 Org tags are stored in the `gnorb-bbdb-org-tags-field'."
196 (let ((full-field (assq gnorb-bbdb-org-tag-field
197 (bbdb-record-xfields record)))
198 (val (bbdb-record-xfield
200 gnorb-bbdb-org-tag-field)))
202 ;; We already know that `fmt' and `indent' are dynamically
203 ;; bound, shut up about it.
205 (bbdb-display-text (format fmt gnorb-bbdb-org-tag-field)
206 `(xfields ,full-field field-label)
209 (bbdb-display-list val gnorb-bbdb-org-tag-field "\n")
211 (bbdb-indent-string (concat val "\n") indent)))))))
214 (defun gnorb-bbdb-mail (records &optional subject n verbose)
215 "\\<bbdb-mode-map>Acts just like `bbdb-mail', except runs
216 RECORDS through `gnorb-bbdb-posting-styles', allowing
217 customization of message styles for certain records. From the
218 `bbdb-mail' docstring:
220 Compose a mail message to RECORDS (optional: using SUBJECT).
221 Interactively, use BBDB prefix \\[bbdb-do-all-records], see
222 `bbdb-do-all-records'. By default, the first mail addresses of
223 RECORDS are used. If prefix N is a number, use Nth mail address
224 of RECORDS (starting from 1). If prefix N is C-u (t
225 noninteractively) use all mail addresses of RECORDS. If VERBOSE
226 is non-nil (as in interactive calls) be verbose."
227 ;; see the function `gnus-configure-posting-styles' for tips on how
228 ;; to actually do this.
229 (interactive (list (bbdb-do-records) nil
230 (or (consp current-prefix-arg)
233 (setq records (bbdb-record-list records))
235 (user-error "No records displayed")
236 (let ((head (bbdb-current-record))
237 (to (bbdb-mail-address records n nil verbose))
238 (message-mode-hook (copy-sequence message-mode-hook)))
239 (setq records (remove head records))
240 (when gnorb-bbdb-posting-styles
241 (add-hook 'message-mode-hook
243 (gnorb-bbdb-configure-posting-styles (quote ,records))
244 (gnorb-bbdb-configure-posting-styles (list ,head)))))
245 (bbdb-compose-mail to subject))))
247 (defun gnorb-bbdb-configure-posting-styles (recs)
248 ;; My most magnificent work of copy pasta!
250 (let (field val label rec-val element filep
251 element v value results name address)
252 (dolist (style gnorb-bbdb-posting-styles)
253 (setq field (pop style)
255 (when (consp val) ;; (label value)
256 (setq label (pop val)
258 (unless (fboundp field)
259 ;; what's the record's existing value for this field?
260 (setq rec-val (bbdb-record-field r field)))
265 (not (string-match label (car a))))
266 (string-match val (bbdb-format-address-default a)))))
270 (not (string-match label (car p))))
271 (string-match val (bbdb-phone-string p)))))
274 (string-match val f)))
278 (string-match val rec-val)))
279 ;; there are matches, run through the field setters in last
280 ;; element of the sexp
281 (dolist (attribute style)
282 (setq element (pop attribute)
286 ((eq (car attribute) :file)
289 ((eq (car attribute) :value)
300 (cond ((functionp value)
303 (symbol-value value))))
306 ;; Post-processing for the signature posting-style:
307 (and (eq element 'signature) filep
308 message-signature-directory
309 ;; don't actually use the signature directory
310 ;; if message-signature-file contains a path.
311 (not (file-name-directory v))
312 (setq v (nnheader-concat message-signature-directory v)))
313 ;; Get the contents of file elems.
315 (setq v (with-temp-buffer
316 (insert-file-contents v)
320 (goto-char (point-max))
321 (if (zerop (skip-chars-backward "\n"))
324 (setq results (delq (assoc element results) results))
325 (push (cons element v) results))))
326 (setq name (assq 'name results)
327 address (assq 'address results))
328 (setq results (delq name (delq address results)))
329 (gnus-make-local-hook 'message-setup-hook)
330 (setq results (sort results (lambda (x y)
331 (string-lessp (car x) (car y)))))
332 (dolist (result results)
333 (add-hook 'message-setup-hook
335 ((eq 'eval (car result))
337 ((eq 'body (car result))
341 (insert ,(cdr result)))))
342 ((eq 'signature (car result))
343 (set (make-local-variable 'message-signature) nil)
344 (set (make-local-variable 'message-signature-file) nil)
345 (if (not (cdr result))
349 (let ((message-signature ,(cdr result)))
350 (when message-signature
351 (message-insert-signature)))))))
354 (if (symbolp (car result))
355 (capitalize (symbol-name (car result)))
359 (message-remove-header ,header)
360 (let ((value ,(cdr result)))
363 (insert ,header ": " value)
365 (insert "\n")))))))))
367 (when (or name address)
368 (add-hook 'message-setup-hook
370 (set (make-local-variable 'user-mail-address)
371 ,(or (cdr address) user-mail-address))
372 (let ((user-full-name ,(or (cdr name) (user-full-name)))
374 ,(or (cdr address) user-mail-address)))
376 (message-remove-header "From")
378 (insert "From: " (message-make-from) "\n"))))
382 (defun gnorb-bbdb-tag-agenda (records)
383 "Open an Org agenda tags view from the BBDB buffer, using the
384 value of the record's org-tags field. This shows only TODOs by
385 default; a prefix argument shows all tagged headings; a \"*\"
386 prefix operates on all currently visible records. If you want
387 both, use \"C-u\" before the \"*\"."
388 (interactive (list (bbdb-do-records)))
389 (require 'org-agenda)
390 (unless (and (eq major-mode 'bbdb-mode)
391 (equal (buffer-name) bbdb-buffer-name))
392 (error "Only works in the BBDB buffer"))
393 (setq records (bbdb-record-list records))
398 (cl-mapcan (lambda (r)
399 (bbdb-record-xfield-split r gnorb-bbdb-org-tag-field))
403 ;; C-u = all headings, not just todos
404 (org-tags-view (not (equal current-prefix-arg '(4)))
406 (error "No org-tags field present"))))
409 (defun gnorb-bbdb-mail-search (records)
410 "Initiate a mail search from the BBDB buffer.
412 Use the prefix arg to edit the search string first, and the \"*\"
413 prefix to search mails from all visible contacts. When using both
414 a prefix arg and \"*\", the prefix arg must come first."
415 (interactive (list (bbdb-do-records)))
416 (unless (and (eq major-mode 'bbdb-mode)
417 (equal (buffer-name) bbdb-buffer-name))
418 (error "Only works in the BBDB buffer"))
419 (setq records (bbdb-record-list records))
420 (require 'gnorb-gnus)
421 (let* ((backend (or (assoc gnorb-gnus-mail-search-backend
422 gnorb-gnus-mail-search-backends)
423 (error "No search backend specified")))
425 (funcall (cl-second backend)
426 (cl-mapcan 'bbdb-record-mail records))))
427 (when (equal current-prefix-arg '(4))
429 (read-from-minibuffer
430 (format "%s search string: " (cl-first backend)) search-string)))
431 (funcall (cl-third backend) search-string)
432 (delete-other-windows)))
435 (defun gnorb-bbdb-cite-contact (rec)
436 (interactive (list (gnorb-prompt-for-bbdb-record)))
437 (let ((mail-string (bbdb-dwim-mail rec)))
438 (if (called-interactively-p 'any)
442 ;;; Field containing links to recent messages
444 (when (boundp 'bbdb-xfield-label-list)
445 (add-to-list 'bbdb-xfield-label-list gnorb-bbdb-messages-field nil 'eq))
447 (defun gnorb-bbdb-display-messages (record format)
448 "Show links to the messages collected in the
449 `gnorb-bbdb-messages-field' field of a BBDB record. Each link
450 will be formatted using the format string in
451 `gnorb-bbdb-message-link-format-multi' or
452 `gnorb-bbdb-message-link-format-one', depending on the current
454 (let ((full-field (assq gnorb-bbdb-messages-field
455 (bbdb-record-xfields record)))
456 (val (bbdb-record-xfield record gnorb-bbdb-messages-field))
457 (map (make-sparse-keymap))
458 (count 1)) ; one-indexed to fit with prefix arg to `gnorb-bbdb-open-link'
459 (define-key map [mouse-1] 'gnorb-bbdb-mouse-open-link)
460 (define-key map (kbd "<RET>") 'gnorb-bbdb-RET-open-link)
462 (when (eq format 'multi)
463 (with-no-warnings ; For `fmt'
464 (bbdb-display-text (format fmt gnorb-bbdb-messages-field)
465 `(xfields ,full-field field-label)
467 (insert (cond ((and (stringp val)
469 (with-no-warnings ; For `indent'
470 (bbdb-indent-string (concat val "\n") indent)))
472 ;; Why aren't I using `bbdb-display-list' with a
473 ;; preformatted list of messages?
475 (with-no-warnings ; For `indent' again
483 (replace-regexp-in-string
484 "%:subject" (gnorb-bbdb-link-subject m)
485 (replace-regexp-in-string
486 "%:count" (number-to-string count)
487 (if (eq format 'multi)
488 gnorb-bbdb-message-link-format-multi
489 gnorb-bbdb-message-link-format-one)))
490 (gnorb-bbdb-link-date m)))
491 'face 'gnorb-bbdb-link
492 'mouse-face 'highlight
493 'gnorb-bbdb-link-count count
496 val (if (eq format 'multi)
499 (if (eq format 'multi) "\n" "")))
503 (fset (intern (format "bbdb-display-%s-multi-line"
504 gnorb-bbdb-messages-field))
506 (gnorb-bbdb-display-messages record 'multi)))
508 (fset (intern (format "bbdb-display-%s-one-line"
509 gnorb-bbdb-messages-field))
511 (gnorb-bbdb-display-messages record 'one)))
513 ;; Don't allow direct editing of this field
515 (fset (intern (format "bbdb-read-xfield-%s"
516 gnorb-bbdb-messages-field))
517 (lambda (&optional init)
518 (user-error "This field shouldn't be edited manually")))
520 ;; Open links from the *BBDB* buffer.
523 (defun gnorb-bbdb-open-link (record arg)
524 "\\<bbdb-mode-map>Call this on a BBDB record to open one of the
525 links in the message field. By default, the first link will be
526 opened. Use a prefix arg to open different links. For instance,
527 M-3 \\[gnorb-bbdb-open-link] will open the third link in the
528 list. If the %:count escape is present in the message formatting
529 string (see `gnorb-bbdb-message-link-format-multi' and
530 `gnorb-bbdb-message-link-format-one'), that's the number to use.
532 This function also needs to be called on a contact once before
533 that contact will start collecting links to messages."
535 (or (bbdb-current-record)
536 (user-error "No record under point"))
538 (unless (fboundp 'bbdb-record-xfield-string)
539 (user-error "This function only works with the git version of BBDB"))
540 (let* ((record (bbdb-current-record))
542 (if (not (memq gnorb-bbdb-messages-field
543 (mapcar 'car (bbdb-record-xfields record))))
545 (format "Start collecting message links for %s?"
546 (bbdb-record-name record)))
547 (bbdb-record-set-xfield record gnorb-bbdb-messages-field "no links yet")
548 (message "Opening messages from %s will add links to the %s field"
549 (bbdb-record-name record)
550 gnorb-bbdb-messages-field)
551 (bbdb-change-record record))
553 (bbdb-record-xfield record gnorb-bbdb-messages-field))
556 (nth (1- arg) msg-list))
559 (org-gnus-follow-link (gnorb-bbdb-link-group target-msg)
560 (gnorb-bbdb-link-id target-msg))))))
562 (defun gnorb-bbdb-mouse-open-link (event)
564 (mouse-set-point event)
565 (let ((rec (bbdb-current-record))
566 (num (get-text-property (point) 'gnorb-bbdb-link-count)))
568 (user-error "No link under point")
569 (gnorb-bbdb-open-link rec num))))
571 (defun gnorb-bbdb-RET-open-link ()
573 (let ((rec (bbdb-current-record))
574 (num (get-text-property (point) 'gnorb-bbdb-link-count)))
576 (user-error "No link under point")
577 (gnorb-bbdb-open-link rec num))))
579 (defun gnorb-bbdb-store-message-link (record)
580 "Used in the `bbdb-notice-record-hook' to possibly save a link
581 to a message into the record's `gnorb-bbdb-messages-field'."
583 (when (not (fboundp 'bbdb-record-xfield-string))
584 (user-error "This function only works with the git version of BBDB"))
585 (unless (or (not (and (memq gnorb-bbdb-messages-field
586 (mapcar 'car (bbdb-record-xfields record)))
587 (memq major-mode '(gnus-summary-mode gnus-article-mode))))
588 (with-current-buffer gnus-article-buffer
589 (not ; only store messages if the record is the sender
590 (member (nth 1 (car (bbdb-get-address-components 'sender)))
591 (bbdb-record-mail record)))))
592 (with-current-buffer gnus-summary-buffer
593 (let* ((val (bbdb-record-xfield record gnorb-bbdb-messages-field))
594 (art-no (gnus-summary-article-number))
595 (heads (gnus-summary-article-header art-no))
596 (date (apply 'encode-time
597 (parse-time-string (mail-header-date heads))))
598 (subject (mail-header-subject heads))
599 (id (mail-header-id heads))
600 (group gnus-newsgroup-name)
602 ;; check for both nnvirtual and nnir, and link to the real
603 ;; group in those cases
604 (when (eq (car (gnus-find-method-for-group group))
606 (setq group (car (nnvirtual-map-article art-no))))
607 (when (eq (car (gnus-find-method-for-group group))
609 (setq group (nnir-article-group art-no)))
610 (if (not (and date subject id group))
611 (message "Could not save a link to this message")
612 (setq link (make-gnorb-bbdb-link :subject subject :date date
613 :group group :id id))
616 (setq val (cons link (delete link val)))
617 (when (eq gnorb-bbdb-define-recent 'received)
621 (gnorb-bbdb-link-date b)
622 (gnorb-bbdb-link-date a))))))
623 (setq val (cl-subseq val 0 gnorb-bbdb-collect-N-messages))
624 (bbdb-record-set-xfield record
625 gnorb-bbdb-messages-field
627 (bbdb-change-record record))))))
629 (add-hook 'bbdb-notice-record-hook 'gnorb-bbdb-store-message-link)
631 (provide 'gnorb-bbdb)
632 ;;; gnorb-bbdb.el ends here