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/>.
28 (require 'gnorb-utils)
31 (defgroup gnorb-bbdb nil
32 "The BBDB bits of gnorb."
36 (defcustom gnorb-bbdb-org-tag-field 'org-tags
37 "The name (as a symbol) of the field to use for org tags."
41 (when (boundp 'bbdb-separator-alist) ;Allow compilation if BBDB is absent!
42 (unless (assoc gnorb-bbdb-org-tag-field bbdb-separator-alist)
43 (push `(,gnorb-bbdb-org-tag-field ":" ":") bbdb-separator-alist)))
45 (defcustom gnorb-bbdb-messages-field 'messages
46 "The name (as a symbol) of the field where links to recent gnus
47 messages from this record are stored.
49 \\<bbdb-mode-map>Records that do not have this field defined
50 will not collect links to messages: you have to call
51 \"\\[gnorb-bbdb-open-link]\" on the record once -- after that,
52 message links will be collected and updated automatically."
56 (defcustom gnorb-bbdb-collect-N-messages 5
57 "For records with a `gnorb-bbdb-messages-field' defined,
58 collect links to a maximum of this many messages."
62 (defcustom gnorb-bbdb-define-recent 'seen
63 "For records with a `gnorb-bbdb-message-tag-field' defined,
64 this variable controls how gnorb defines a \"recent\" message.
65 Setting it to the symbol seen will collect the messages most
66 recently opened and viewed. The symbol received means gnorb will
67 collect the most recent messages by Date header.
69 In other words, if this variable is set to 'received, and a
70 record's messages field is already full of recently-received
71 messages, opening a five-year-old message (for instance) from
72 this record will not push a link to the message into the field."
74 :type '(choice (const :tag "Most recently seen" 'seen)
75 (const :tag "Most recently received" 'received)))
77 (defcustom gnorb-bbdb-message-link-format-multi "%:count. %D: %:subject"
78 "How a single message is formatted in the list of recent messages.
79 This format string is used in multi-line record display.
81 Available information for each message includes the subject, the
82 date, and the message's count in the list, as an integer. You can
83 access subject and count using the %:subject and %:count escapes.
84 The message date can be formatted using any of the escapes
85 mentioned in the docstring of `format-time-string', which see."
89 (defcustom gnorb-bbdb-message-link-format-one "%:count"
90 "How a single message is formatted in the list of recent messages.
91 This format string is used in single-line display -- note that by
92 default, no user-created xfields are displayed in the 'one-line
93 layout found in `bbdb-layout-alist'. If you want this field to
94 appear there, put its name in the \"order\" list of the 'one-line
97 Available information for each message includes the subject, the
98 date, and the message's count in the list, as an integer. You can
99 access subject and count using the %:subject and %:count escapes.
100 The message date can be formatted using any of the escapes
101 mentioned in the docstring of `format-time-string', which see."
105 (defface gnorb-bbdb-link (org-compatible-face 'org-link nil)
106 "Custom face for displaying message links in the *BBDB* buffer.
107 Defaults to org-link."
110 (cl-defstruct gnorb-bbdb-link
111 subject date group id)
113 (defcustom gnorb-bbdb-posting-styles nil
114 "An alist of styles to use when composing messages to the BBDB
115 record(s) under point. This is entirely analogous to
116 `gnus-posting-styles', it simply works by examining record fields
117 rather than group names.
119 When composing a message to multiple contacts (using the \"*\"
120 prefix), the records will be scanned in order, with the record
121 initially under point (if any) set aside for last. That means
122 that, in the case of conflicting styles, the record under point
123 will override the others.
125 In order not to be too intrusive, this option has no effect on
126 the usual `bbdb-mail' command. Instead, the wrapper command
127 `gnorb-bbdb-mail' is provided, which consults this option and
128 then hands off to `bbdb-compose-mail'. If you'd always like to
129 use `gnorb-bbdb-mail', you can simply bind it to \"m\" in the
132 The value of the option should be a list of sexps, each one
133 matching a single field. The first element should match a field
134 name: one of the built-in fields like lastname, or an xfield.
135 Field names should be given as symbols.
137 The second element is a regexp used to match against the value of
138 the field (non-string field values will be cast to strings, if
139 possible). It can also be a cons of two strings, the first of
140 which matches the field label, the second the field value.
142 Alternately, the first element can be the name of a custom
143 function that is called with the record as its only argument, and
144 returns either t or nil. In this case, the second element of the
147 All following elements should be field setters for the message to
148 be composed, just as in `gnus-posting-styles'.
150 An example value might look like:"
153 (when (fboundp 'bbdb-record-xfield-string)
154 (fset (intern (format "bbdb-read-xfield-%s"
155 gnorb-bbdb-org-tag-field))
156 (lambda (&optional init)
157 (gnorb-bbdb-read-org-tags init)))
159 (fset (intern (format "bbdb-display-%s-multi-line"
160 gnorb-bbdb-org-tag-field))
162 (gnorb-bbdb-display-org-tags record))))
164 (defun gnorb-bbdb-read-org-tags (&optional init)
165 "Read Org mode tags, with `completing-read-multiple'."
166 (if (string< "24.3" (substring emacs-version 0 4))
169 (cadr (assq gnorb-bbdb-org-tag-field
170 bbdb-separator-alist))
172 (crm-local-completion-map bbdb-crm-local-completion-map)
173 (table (cl-mapcar #'car
174 (org-global-tags-completion-table
175 (org-agenda-files))))
176 (init (if (consp init)
178 (nth 2 (assq gnorb-bbdb-org-tag-field
179 bbdb-separator-alist)))
181 (completing-read-multiple
184 (bbdb-split gnorb-bbdb-org-tag-field
185 (bbdb-read-string "Tags: " init))))
187 (defun gnorb-bbdb-display-org-tags (record)
188 "Display the Org tags associated with the record.
190 Org tags are stored in the `gnorb-bbdb-org-tags-field'."
191 (let ((full-field (assq gnorb-bbdb-org-tag-field
192 (bbdb-record-xfields record)))
193 (val (bbdb-record-xfield
195 gnorb-bbdb-org-tag-field)))
197 ;; We already know that `fmt' and `indent' are dynamically
198 ;; bound, shut up about it.
200 (bbdb-display-text (format fmt gnorb-bbdb-org-tag-field)
201 `(xfields ,full-field field-label)
204 (bbdb-display-list val gnorb-bbdb-org-tag-field "\n")
206 (bbdb-indent-string (concat val "\n") indent)))))))
208 (defvar message-mode-hook)
211 (defun gnorb-bbdb-mail (records &optional subject n verbose)
212 "\\<bbdb-mode-map>Acts just like `bbdb-mail', except runs
213 RECORDS through `gnorb-bbdb-posting-styles', allowing
214 customization of message styles for certain records. From the
215 `bbdb-mail' docstring:
217 Compose a mail message to RECORDS (optional: using SUBJECT).
218 Interactively, use BBDB prefix \\[bbdb-do-all-records], see
219 `bbdb-do-all-records'. By default, the first mail addresses of
220 RECORDS are used. If prefix N is a number, use Nth mail address
221 of RECORDS (starting from 1). If prefix N is C-u (t
222 noninteractively) use all mail addresses of RECORDS. If VERBOSE
223 is non-nil (as in interactive calls) be verbose."
224 ;; see the function `gnus-configure-posting-styles' for tips on how
225 ;; to actually do this.
226 (interactive (list (bbdb-do-records) nil
227 (or (consp current-prefix-arg)
230 (setq records (bbdb-record-list records))
232 (user-error "No records displayed")
233 (let ((head (bbdb-current-record))
234 (to (bbdb-mail-address records n nil verbose))
235 (message-mode-hook (copy-sequence message-mode-hook)))
236 (setq records (remove head records))
237 (when gnorb-bbdb-posting-styles
238 (add-hook 'message-mode-hook
240 (gnorb-bbdb-configure-posting-styles (quote ,records))
241 (gnorb-bbdb-configure-posting-styles (list ,head)))))
242 (bbdb-compose-mail to subject))))
244 (defun gnorb-bbdb-configure-posting-styles (recs)
245 ;; My most magnificent work of copy pasta!
247 (let (field val label rec-val element filep
248 element v value results name address)
249 (dolist (style gnorb-bbdb-posting-styles)
250 (setq field (pop style)
252 (when (consp val) ;; (label value)
253 (setq label (pop val)
255 (unless (fboundp field)
256 ;; what's the record's existing value for this field?
257 (setq rec-val (bbdb-record-field r field)))
262 (not (string-match label (car a))))
263 (string-match val (bbdb-format-address-default a)))))
267 (not (string-match label (car p))))
268 (string-match val (bbdb-phone-string p)))))
271 (string-match val f)))
275 (string-match val rec-val)))
276 ;; there are matches, run through the field setters in last
277 ;; element of the sexp
278 (dolist (attribute style)
279 (setq element (pop attribute)
283 ((eq (car attribute) :file)
286 ((eq (car attribute) :value)
297 (cond ((functionp value)
300 (symbol-value value))))
303 ;; Post-processing for the signature posting-style:
304 (and (eq element 'signature) filep
305 message-signature-directory
306 ;; don't actually use the signature directory
307 ;; if message-signature-file contains a path.
308 (not (file-name-directory v))
309 (setq v (nnheader-concat message-signature-directory v)))
310 ;; Get the contents of file elems.
312 (setq v (with-temp-buffer
313 (insert-file-contents v)
317 (goto-char (point-max))
318 (if (zerop (skip-chars-backward "\n"))
321 (setq results (delq (assoc element results) results))
322 (push (cons element v) results))))
323 (setq name (assq 'name results)
324 address (assq 'address results))
325 (setq results (delq name (delq address results)))
326 (gnus-make-local-hook 'message-setup-hook)
327 (setq results (sort results (lambda (x y)
328 (string-lessp (car x) (car y)))))
329 (dolist (result results)
330 (add-hook 'message-setup-hook
332 ((eq 'eval (car result))
334 ((eq 'body (car result))
338 (insert ,(cdr result)))))
339 ((eq 'signature (car result))
340 (set (make-local-variable 'message-signature) nil)
341 (set (make-local-variable 'message-signature-file) nil)
342 (if (not (cdr result))
346 (let ((message-signature ,(cdr result)))
347 (when message-signature
348 (message-insert-signature)))))))
351 (if (symbolp (car result))
352 (capitalize (symbol-name (car result)))
356 (message-remove-header ,header)
357 (let ((value ,(cdr result)))
360 (insert ,header ": " value)
362 (insert "\n")))))))))
364 (when (or name address)
365 (add-hook 'message-setup-hook
367 (set (make-local-variable 'user-mail-address)
368 ,(or (cdr address) user-mail-address))
369 (let ((user-full-name ,(or (cdr name) (user-full-name)))
371 ,(or (cdr address) user-mail-address)))
373 (message-remove-header "From")
375 (insert "From: " (message-make-from) "\n"))))
379 (defun gnorb-bbdb-tag-agenda (records)
380 "Open an Org agenda tags view from the BBDB buffer, using the
381 value of the record's org-tags field. This shows only TODOs by
382 default; a prefix argument shows all tagged headings; a \"*\"
383 prefix operates on all currently visible records. If you want
384 both, use \"C-u\" before the \"*\"."
385 (interactive (list (bbdb-do-records)))
386 (require 'org-agenda)
387 (unless (and (eq major-mode 'bbdb-mode)
388 (equal (buffer-name) bbdb-buffer-name))
389 (error "Only works in the BBDB buffer"))
390 (setq records (bbdb-record-list records))
397 (bbdb-record-xfield-split r gnorb-bbdb-org-tag-field))
401 ;; C-u = all headings, not just todos
402 (if (equal current-prefix-arg '(4))
403 (org-tags-view nil tag-string)
404 (org-tags-view t tag-string))
405 (error "No org-tags field present"))))
408 (defun gnorb-bbdb-mail-search (records)
409 "Initiate a mail search from the BBDB buffer.
411 Use the prefix arg to edit the search string first, and the \"*\"
412 prefix to search mails from all visible contacts. When using both
413 a prefix arg and \"*\", the prefix arg must come first."
414 (interactive (list (bbdb-do-records)))
415 (unless (and (eq major-mode 'bbdb-mode)
416 (equal (buffer-name) bbdb-buffer-name))
417 (error "Only works in the BBDB buffer"))
418 (setq records (bbdb-record-list records))
419 (require 'gnorb-gnus)
420 (let* ((backend (or (assoc gnorb-gnus-mail-search-backend
421 gnorb-gnus-mail-search-backends)
422 (error "No search backend specified")))
424 (funcall (cl-second backend)
425 (cl-mapcan 'bbdb-record-mail records))))
426 (when (equal current-prefix-arg '(4))
428 (read-from-minibuffer
429 (format "%s search string: " (first backend)) search-string)))
430 (funcall (cl-third backend) search-string)
431 (delete-other-windows)))
434 (defun gnorb-bbdb-cite-contact (rec)
435 (interactive (list (gnorb-prompt-for-bbdb-record)))
436 (let ((mail-string (bbdb-dwim-mail rec)))
437 (if (called-interactively-p 'any)
441 ;;; Field containing links to recent messages
442 (when (boundp 'bbdb-xfield-label-list)
443 (add-to-list 'bbdb-xfield-label-list gnorb-bbdb-messages-field nil 'eq))
445 (defun gnorb-bbdb-display-messages (record format)
446 "Show links to the messages collected in the
447 `gnorb-bbdb-messages-field' field of a BBDB record. Each link
448 will be formatted using the format string in
449 `gnorb-bbdb-message-link-format-multi' or
450 `gnorb-bbdb-message-link-format-one', depending on the current
452 (let ((full-field (assq gnorb-bbdb-messages-field
453 (bbdb-record-xfields record)))
454 (val (bbdb-record-xfield record gnorb-bbdb-messages-field))
455 (map (make-sparse-keymap))
456 (count 1)) ; one-indexed to fit with prefix arg to `gnorb-bbdb-open-link'
457 (define-key map [mouse-1] 'gnorb-bbdb-mouse-open-link)
458 (define-key map (kbd "<RET>") 'gnorb-bbdb-RET-open-link)
460 (when (eq format 'multi)
461 (with-no-warnings ; For `fmt'
462 (bbdb-display-text (format fmt gnorb-bbdb-messages-field)
463 `(xfields ,full-field field-label)
465 (insert (cond ((and (stringp val)
467 (with-no-warnings ; For `indent'
468 (bbdb-indent-string (concat val "\n") indent)))
470 ;; Why aren't I using `bbdb-display-list' with a
471 ;; preformatted list of messages?
473 (with-no-warnings ; For `indent' again
481 (replace-regexp-in-string
482 "%:subject" (gnorb-bbdb-link-subject m)
483 (replace-regexp-in-string
484 "%:count" (number-to-string count)
485 (if (eq format 'multi)
486 gnorb-bbdb-message-link-format-multi
487 gnorb-bbdb-message-link-format-one)))
488 (gnorb-bbdb-link-date m)))
489 'face 'gnorb-bbdb-link
490 'mouse-face 'highlight
491 'gnorb-bbdb-link-count count
494 val (if (eq format 'multi)
497 (if (eq format 'multi) "\n" "")))
501 (fset (intern (format "bbdb-display-%s-multi-line"
502 gnorb-bbdb-messages-field))
504 (gnorb-bbdb-display-messages record 'multi)))
506 (fset (intern (format "bbdb-display-%s-one-line"
507 gnorb-bbdb-messages-field))
509 (gnorb-bbdb-display-messages record 'one)))
511 ;; Don't allow direct editing of this field
513 (fset (intern (format "bbdb-read-xfield-%s"
514 gnorb-bbdb-messages-field))
515 (lambda (&optional init)
516 (user-error "This field shouldn't be edited manually")))
518 ;; Open links from the *BBDB* buffer.
521 (defun gnorb-bbdb-open-link (record arg)
522 "\\<bbdb-mode-map>Call this on a BBDB record to open one of the
523 links in the message field. By default, the first link will be
524 opened. Use a prefix arg to open different links. For instance,
525 M-3 \\[gnorb-bbdb-open-link] will open the third link in the
526 list. If the %:count escape is present in the message formatting
527 string (see `gnorb-bbdb-message-link-format-multi' and
528 `gnorb-bbdb-message-link-format-one'), that's the number to use.
530 This function also needs to be called on a contact once before
531 that contact will start collecting links to messages."
533 (or (bbdb-current-record)
534 (user-error "No record under point"))
536 (unless (fboundp 'bbdb-record-xfield-string)
537 (user-error "This function only works with the git version of BBDB"))
538 (let* ((record (bbdb-current-record))
540 (if (not (memq gnorb-bbdb-messages-field
541 (mapcar 'car (bbdb-record-xfields record))))
543 (format "Start collecting message links for %s?"
544 (bbdb-record-name record)))
545 (bbdb-record-set-xfield record gnorb-bbdb-messages-field "no links yet")
546 (message "Opening messages from %s will add links to the %s field"
547 (bbdb-record-name record)
548 gnorb-bbdb-messages-field)
549 (bbdb-change-record record))
551 (bbdb-record-xfield record gnorb-bbdb-messages-field))
554 (nth (1- arg) msg-list))
557 (org-gnus-follow-link (gnorb-bbdb-link-group target-msg)
558 (gnorb-bbdb-link-id target-msg))))))
560 (defun gnorb-bbdb-mouse-open-link (event)
562 (mouse-set-point event)
563 (let ((rec (bbdb-current-record))
564 (num (get-text-property (point) 'gnorb-bbdb-link-count)))
566 (user-error "No link under point")
567 (gnorb-bbdb-open-link rec num))))
569 (defun gnorb-bbdb-RET-open-link ()
571 (let ((rec (bbdb-current-record))
572 (num (get-text-property (point) 'gnorb-bbdb-link-count)))
574 (user-error "No link under point")
575 (gnorb-bbdb-open-link rec num))))
577 (defun gnorb-bbdb-store-message-link (record)
578 "Used in the `bbdb-notice-record-hook' to possibly save a link
579 to a message into the record's `gnorb-bbdb-messages-field'."
581 (when (not (fboundp 'bbdb-record-xfield-string))
582 (user-error "This function only works with the git version of BBDB"))
583 (unless (or (not (and (memq gnorb-bbdb-messages-field
584 (mapcar 'car (bbdb-record-xfields record)))
585 (memq major-mode '(gnus-summary-mode gnus-article-mode))))
586 (with-current-buffer gnus-article-buffer
587 (not ; only store messages if the record is the sender
588 (member (nth 1 (car (bbdb-get-address-components 'sender)))
589 (bbdb-record-mail record)))))
590 (with-current-buffer gnus-summary-buffer
591 (let* ((val (bbdb-record-xfield record gnorb-bbdb-messages-field))
592 (art-no (gnus-summary-article-number))
593 (heads (gnus-summary-article-header art-no))
594 (date (apply 'encode-time
595 (parse-time-string (mail-header-date heads))))
596 (subject (mail-header-subject heads))
597 (id (mail-header-id heads))
598 (group (gnorb-get-real-group-name
602 (if (not (and date subject id group))
603 (message "Could not save a link to this message")
604 (setq link (make-gnorb-bbdb-link :subject subject :date date
605 :group group :id id))
608 (setq val (cons link (delete link val)))
609 (when (eq gnorb-bbdb-define-recent 'received)
613 (gnorb-bbdb-link-date b)
614 (gnorb-bbdb-link-date a))))))
615 (setq val (cl-subseq val 0 (min (length val) gnorb-bbdb-collect-N-messages)))
616 (bbdb-record-set-xfield record
617 gnorb-bbdb-messages-field
619 (bbdb-change-record record))))))
621 (add-hook 'bbdb-notice-record-hook 'gnorb-bbdb-store-message-link)
623 (provide 'gnorb-bbdb)
624 ;;; gnorb-bbdb.el ends here