File: bbdb-wl.el

package info (click to toggle)
wl-beta 2.15.9+0.20190205-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,260 kB
  • sloc: lisp: 51,446; sh: 191; makefile: 141
file content (491 lines) | stat: -rw-r--r-- 17,600 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
;;; bbdb-wl.el -- BBDB interface to Wanderlust

;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>

;; Author: Yuuichi Teranishi <teranisi@gohome.org>
;; Keywords: mail, news, database

;;; Commentary:
;;
;;  Insert the following lines in your ~/.wl
;;
;;  (require 'bbdb-wl)
;;  (bbdb-wl-setup)

;;; Code:
;;

;; bbdb setup.
(eval-when-compile
  (require 'static)
  (require 'mime-setup)
  (require 'elmo-vars)
  (require 'elmo-util)
  (require 'wl-summary)
  (require 'wl-message)
  (require 'wl-draft)
  (require 'wl-address)
  (require 'bbdb-com)
  (defvar bbdb-pop-up-elided-display nil))

(require 'bbdb)

(defvar bbdb-wl-get-update-record-hook nil)
(defvar bbdb-wl-folder-regexp nil)
(defvar bbdb-wl-ignore-folder-regexp nil)

(defvar bbdb-wl-canonicalize-full-name-function
  #'bbdb-wl-canonicalize-spaces-and-dots
  "Way to canonicalize full name.")

(defun bbdb-wl-canonicalize-spaces-and-dots (string)
  (while (and string (string-match "  +\\|[\f\t\n\r\v]+\\|\\." string))
    (setq string (replace-match " " nil t string)))
  (and string (string-match "^ " string)
       (setq string (replace-match "" nil t string)))
  string)

;;;###autoload
(defun bbdb-wl-setup ()
  (add-hook 'wl-message-redisplay-hook 'bbdb-wl-get-update-record)
  (add-hook 'wl-summary-exit-hook 'bbdb-wl-hide-bbdb-buffer)
  (add-hook 'wl-message-window-deleted-hook 'bbdb-wl-hide-bbdb-buffer)
  (add-hook 'wl-exit-hook 'bbdb-wl-exit)
  (add-hook 'wl-save-hook 'bbdb-offer-save)
  (add-hook 'wl-summary-toggle-disp-off-hook 'bbdb-wl-hide-bbdb-buffer)
  (add-hook 'wl-summary-toggle-disp-folder-on-hook 'bbdb-wl-hide-bbdb-buffer)
  (add-hook 'wl-summary-toggle-disp-folder-off-hook 'bbdb-wl-hide-bbdb-buffer)
  (add-hook 'wl-summary-toggle-disp-folder-message-resumed-hook
	    'bbdb-wl-show-bbdb-buffer)
  (add-hook 'wl-summary-mode-hook
	    (function
	     (lambda ()
	       (define-key (current-local-map) ":" 'bbdb-wl-show-sender)
	       (define-key (current-local-map) ";" 'bbdb-wl-edit-notes))))
  (add-hook 'wl-summary-exit-hook 'bbdb-flush-all-caches)
  (add-hook 'wl-summary-exec-hook 'bbdb-flush-all-caches)
  (add-hook 'wl-mail-setup-hook
	    (function
	     (lambda ()
;;;	       (local-set-key "\M-\t" 'bbdb-complete-name)
	       (define-key (current-local-map) "\M-\t" 'bbdb-complete-name))))
  (require 'bbdb)
  (bbdb-initialize)

  (if (not (boundp 'bbdb-get-addresses-from-headers))
      (defvar bbdb-get-addresses-from-headers
	'("From" "Resent-From" "Reply-To")))

  (if (not (boundp 'bbdb-get-addresses-to-headers))
      (defvar bbdb-get-addresses-to-headers
	'("Resent-To" "Resent-CC" "To" "CC" "BCC")))

  (if (not (boundp 'bbdb-get-addresses-headers))
      (defvar bbdb-get-addresses-headers
	(append bbdb-get-addresses-from-headers
		bbdb-get-addresses-to-headers))))

(defun bbdb-wl-exit ()
  (let (bbdb-buf)
    (if (setq bbdb-buf (get-buffer bbdb-buffer-name))
	(kill-buffer bbdb-buf)))
  (bbdb-offer-save))

(defun bbdb-wl-get-update-record ()
  (let ((folder-name (with-current-buffer
			 wl-message-buffer-cur-summary-buffer
		       (wl-summary-buffer-folder-name))))
    (if (and (or (null bbdb-wl-folder-regexp)
		 (string-match bbdb-wl-folder-regexp folder-name))
	     (not (and bbdb-wl-ignore-folder-regexp
		       (string-match bbdb-wl-ignore-folder-regexp
				     folder-name))))
	(with-current-buffer (wl-message-get-original-buffer)
	  (bbdb-wl-update-record)
	  (run-hooks 'bbdb-wl-get-update-record-hook)))))

(defun bbdb-wl-hide-bbdb-buffer ()
  (let (bbdb-buf bbdb-win)
    (if (setq bbdb-buf (get-buffer bbdb-buffer-name))
	(if (setq bbdb-win (get-buffer-window bbdb-buf))
	    (delete-window bbdb-win)))))

(defun bbdb-wl-show-bbdb-buffer ()
  (save-selected-window
    (if (get-buffer-window bbdb-buffer-name)
	nil
      (let ((mes-win (get-buffer-window
		      (save-excursion
			(if (buffer-live-p  wl-current-summary-buffer)
			    (set-buffer wl-current-summary-buffer))
			wl-message-buffer)))
	    (cur-win (selected-window))
	    (b (current-buffer)))
	(and mes-win (select-window mes-win))
	(let ((size (min
		     (- (window-height mes-win)
			window-min-height 1)
		     (- (window-height mes-win)
			(max window-min-height
			     (1+ bbdb-pop-up-target-lines))))))
	  (split-window mes-win (if (> size 0) size window-min-height)))
	;; goto the bottom of the two...
	(select-window (next-window))
	;; make it display *BBDB*...
	(let ((pop-up-windows nil))
	  (switch-to-buffer (get-buffer-create bbdb-buffer-name)))))))

(defun bbdb-wl-get-petname (from)
  "For `wl-summary-get-petname-function'."
  (let* ((address (wl-address-header-extract-address from))
	 (record (bbdb-search-simple nil address)))
    (and record
	 (or (bbdb-record-name record)
	     (car (bbdb-record-name record))))))

(defun bbdb-wl-from-func (string)
  "A candidate From field STRING.  For `wl-summary-from-function'."
  (let ((hit (bbdb-search-simple nil (wl-address-header-extract-address
				      string)))
	first-name last-name from-str)
    (if hit
	(progn
	  (setq first-name (aref hit 0))
	  (setq last-name (aref hit 1))
	  (cond ((and (null first-name)
		      (null last-name))
		 (setq from-str string))
		((and first-name last-name)
		 (setq from-str (concat first-name " " last-name)))
		((or first-name last-name)
		 (setq from-str (or first-name last-name))))
	  from-str)
      string)))

(defun bbdb-wl-get-addresses-1 (&optional only-first-address)
  "Return real name and email address of sender respectively recipients.
If an address matches `bbdb-user-mail-names' it will be ignored.
The headers to search can be configured by `bbdb-get-addresses-headers'.
For BBDB 2.33 or earlier."
  (save-excursion
    (save-restriction
      (std11-narrow-to-header)
      (let ((headers bbdb-get-addresses-headers)
	    (uninteresting-senders bbdb-user-mail-names)
	    addrlist header structures structure fn ad)
	(while headers
	  (setq header (std11-fetch-field (car headers)))
	  (when header
	    (setq structures (std11-parse-addresses-string
			      (std11-unfold-string header)))
	    (while (and (setq structure (car structures))
			(eq (car structure) 'mailbox))
	      (setq fn (std11-full-name-string structure)
		    fn (and fn
			    (with-temp-buffer ; to keep raw buffer unibyte.
			      (set-buffer-multibyte
			       default-enable-multibyte-characters)
			      (eword-decode-string
			       (decode-mime-charset-string
				fn wl-mime-charset))))
                    fn (funcall bbdb-wl-canonicalize-full-name-function fn)
		    ad (std11-address-string structure))
	      ;; ignore uninteresting addresses, this is kinda gross!
	      (when (or (not (stringp uninteresting-senders))
			(not
			 (or
			  (and fn (string-match uninteresting-senders fn))
			  (and ad (string-match uninteresting-senders ad)))))
		(add-to-list 'addrlist (list fn ad)))
	      (if (and only-first-address addrlist)
		  (setq structures nil headers nil)
		(setq structures (cdr structures)))))
	  (setq headers (cdr headers)))
	(nreverse addrlist)))))

(defun bbdb-wl-get-addresses-2 (&optional only-first-address)
  "Return real name and email address of sender respectively recipients.
If an address matches `bbdb-user-mail-names' it will be ignored.
The headers to search can be configured by `bbdb-get-addresses-headers'.
For BBDB 2.34 or later."
  (save-excursion
    (save-restriction
      (std11-narrow-to-header)
      (let ((headers bbdb-get-addresses-headers)
	    (uninteresting-senders bbdb-user-mail-names)
	    addrlist header structures structure fn ad
	    header-type header-fields header-content)
	(while headers
	  (setq header-type (caar headers)
		header-fields (cdar headers))
	  (while header-fields
	    (setq header-content (std11-fetch-field (car header-fields)))
	    (when header-content
	      (setq structures (std11-parse-addresses-string
				(std11-unfold-string header-content)))
	      (while (and (setq structure (car structures))
			  (eq (car structure) 'mailbox))
                (setq fn (std11-full-name-string structure)
		      fn (and fn
			      (with-temp-buffer ; to keep raw buffer unibyte.
				(set-buffer-multibyte
				 default-enable-multibyte-characters)
				(eword-decode-string
				 (decode-mime-charset-string
				  fn wl-mime-charset))))
		      fn (funcall bbdb-wl-canonicalize-full-name-function fn)
		      ad (std11-address-string structure))
		;; ignore uninteresting addresses, this is kinda gross!
		(when (or (not (stringp uninteresting-senders))
			  (not
			   (or
			    (and fn
				 (string-match uninteresting-senders fn))
			    (and ad
				 (string-match uninteresting-senders ad)))))
		  (add-to-list 'addrlist (list header-type
					       (car header-fields)
					       (list fn ad))))
		(if (and only-first-address addrlist)
		    (setq structures nil headers nil)
		  (setq structures (cdr structures)))))
	    (setq header-fields (cdr header-fields)))
	  (setq headers (cdr headers)))
	(nreverse addrlist)))))

(defun bbdb-wl-get-addresses (&optional only-first-address)
  "Return real name and email address of sender respectively recipients.
If an address matches `bbdb-user-mail-names' it will be ignored.
The headers to search can be configured by `bbdb-get-addresses-headers'."
  (if (string< bbdb-version "2.34")
      (bbdb-wl-get-addresses-1)
    (bbdb-wl-get-addresses-2)))

(defun bbdb-wl-update-record (&optional offer-to-create)
  "Returns the record corresponding to the current WL message,
creating or modifying it as necessary.  A record will be created if
bbdb/mail-auto-create-p is non-nil, or if OFFER-TO-CREATE is true and
the user confirms the creation."
  (let* ((bbdb-get-only-first-address-p t)
	 (records (bbdb-wl-update-records offer-to-create)))
    (if (and records (listp records))
	(car records)
      records)))

(defun bbdb-wl-update-records (&optional offer-to-create)
  "Returns the records corresponding to the current WL message,
creating or modifying it as necessary.  A record will be created if
bbdb/mail-auto-create-p is non-nil, or if OFFER-TO-CREATE is true and
the user confirms the creation."
  (save-excursion
    (if bbdb-use-pop-up
	(bbdb-wl-pop-up-bbdb-buffer offer-to-create)
      (let ((key
	     (save-excursion
	       (set-buffer
		(save-excursion
		  (if (buffer-live-p wl-current-summary-buffer)
		      (set-buffer wl-current-summary-buffer))
		  wl-message-buffer))
	       (intern (format
			"%s-%d"
			wl-current-summary-buffer
			wl-message-buffer-cur-number))))
	    record)
	(or (progn (setq record (bbdb-message-cache-lookup key))
		   (if (listp record) (nth 1 record) record))
	    (static-if (not (fboundp 'bbdb-update-records))
		(let* ((from (or (std11-field-body "From") ""))
		       (addr (and from
				  (nth 1 (std11-extract-address-components
					  from)))))
		  (if (or (null from)
			  (null addr)
			  (string-match (bbdb-user-mail-names) addr))
		      (setq from (or (std11-field-body "To") from)))
		  (with-temp-buffer ; to keep raw buffer unibyte.
		    (set-buffer-multibyte
		     default-enable-multibyte-characters)
		    (setq from (eword-decode-string
				(decode-mime-charset-string
				 from
				 wl-mime-charset))))
		  (if from
		      (bbdb-encache-message
		       key
		       (bbdb-annotate-message-sender
			from t
			(or (bbdb-invoke-hook-for-value
			     bbdb/mail-auto-create-p)
			    offer-to-create)
			offer-to-create))))
	      (bbdb-encache-message
	       key
	       (bbdb-update-records (bbdb-wl-get-addresses
				     bbdb-get-only-first-address-p)
				    (or (bbdb-invoke-hook-for-value
					 bbdb/mail-auto-create-p)
					offer-to-create)
				    offer-to-create))))))))

(defun bbdb-wl-annotate-sender (string)
  "Add a line to the end of the Notes field of the BBDB record
corresponding to the sender of this message."
  (interactive (list (if bbdb-readonly-p
			 (error "The Insidious Big Brother Database is read-only")
		       (read-string "Comments: "))))
  (set-buffer (wl-message-get-original-buffer))
  (bbdb-annotate-notes (bbdb-wl-update-record t) string))

(defun bbdb-wl-edit-notes (&optional arg)
  "Edit the notes field or (with a prefix arg) a user-defined field
of the BBDB record corresponding to the sender of this message."
  (interactive "P")
  (wl-summary-set-message-buffer-or-redisplay)
  (set-buffer (wl-message-get-original-buffer))
  (let ((record (or (bbdb-wl-update-record t) (error ""))))
    (bbdb-display-records (list record))
    (if arg
	(bbdb-record-edit-property record nil t)
      (bbdb-record-edit-notes record t))))

(defun bbdb-wl-show-records (&optional headers)
  "Display the contents of the BBDB for the sender of this message.
This buffer will be in `bbdb-mode', with associated keybindings."
  (interactive)
  (wl-summary-set-message-buffer-or-redisplay)
  (set-buffer (wl-message-get-original-buffer))
  (let ((bbdb-get-addresses-headers (or headers bbdb-get-addresses-headers))
	(bbdb-update-records-mode 'annotating)
	(bbdb-message-cache nil)
	(bbdb-user-mail-names nil)
	records bbdb-win)
    (setq records (bbdb-wl-update-records t))
    (if records
	(progn
	  (bbdb-wl-pop-up-bbdb-buffer)
	  (bbdb-display-records (if (listp records) records
				  (list records))))
      (bbdb-undisplay-records))
    (setq bbdb-win (get-buffer-window (get-buffer bbdb-buffer-name)))
    (and bbdb-win
	 (select-window bbdb-win))
    records))

(defun bbdb-wl-address-headers-spec (address-class)
  "Return address headers structure for ADDRESS-CLASS."
  (if (string< bbdb-version "2.34")
      (cond
       ((eq address-class 'recipients)
	bbdb-get-addresses-to-headers)
       ((eq address-class 'authors)
	bbdb-get-addresses-from-headers)
       (t
	(append bbdb-get-addresses-to-headers
		bbdb-get-addresses-from-headers)))
    (list (assoc address-class bbdb-get-addresses-headers))))

(defun bbdb-wl-show-all-recipients ()
  "Show all recipients of this message. Counterpart to `bbdb/vm-show-sender'."
  (interactive)
  (bbdb-wl-show-records (bbdb-wl-address-headers-spec 'recipients)))

(defun bbdb-wl-show-sender (&optional show-recipients)
  "Display the contents of the BBDB for the senders of this message.
With a prefix argument show the recipients instead,
with two prefix arguments show all records.
This buffer will be in `bbdb-mode', with associated keybindings."
  (interactive "p")
  (cond ((= 4 show-recipients)
	 (bbdb-wl-show-all-recipients))
	((= 16 show-recipients)
	 (bbdb-wl-show-records))
	(t
	 (if (null (bbdb-wl-show-records
		    (bbdb-wl-address-headers-spec 'authors)))
	     (bbdb-wl-show-all-recipients)))))

(defun bbdb-wl-pop-up-bbdb-buffer (&optional offer-to-create)
  "Make the *BBDB* buffer be displayed along with the WL window(s),
displaying the record corresponding to the sender of the current message."
  (if (get-buffer-window bbdb-buffer-name)
      nil
    (let ((mes-win (get-buffer-window
		    (save-excursion
		      (if (buffer-live-p  wl-current-summary-buffer)
			  (set-buffer wl-current-summary-buffer))
		      wl-message-buffer)))
	  (cur-win (selected-window))
	  (b (current-buffer)))
      (and mes-win
	   (select-window mes-win))
      (let ((size (min
		   (- (window-height mes-win)
		      window-min-height 1)
		   (- (window-height mes-win)
		      (max window-min-height
			   (1+ bbdb-pop-up-target-lines))))))
	(split-window mes-win (if (> size 0) size window-min-height)))
      ;; goto the bottom of the two...
      (select-window (next-window))
      ;; make it display *BBDB*...
      (let ((pop-up-windows nil))
	(switch-to-buffer (get-buffer-create bbdb-buffer-name)))
      ;; select the original window we were in...
      (select-window cur-win)
      ;; and make sure the current buffer is correct as well.
      (set-buffer b)))
  (let ((bbdb-gag-messages t)
	(bbdb-use-pop-up nil)
	(bbdb-electric-p nil))
    (let* ((records (static-if (fboundp 'bbdb-update-records)
			(bbdb-wl-update-records offer-to-create)
		      (bbdb-wl-update-record offer-to-create)))
	   ;; BBDB versions v2.33 and later.
	   (bbdb-display-layout
	    (cond ((boundp 'bbdb-pop-up-display-layout)
		   (symbol-value 'bbdb-pop-up-display-layout))
		  ((boundp 'bbdb-pop-up-elided-display)
		   (symbol-value 'bbdb-pop-up-elided-display))))
	   ;; BBDB versions prior to v2.33,
	   (bbdb-elided-display bbdb-display-layout)
	   (b (current-buffer)))
      (bbdb-display-records (if (listp records) records
			      (list records)))
      (set-buffer b)
      records)))

(defun bbdb-wl-send-mail-internal (&optional to subj records)
  (unwind-protect
      (wl-draft (wl-address-header-extract-address to) "" (or subj ""))
    (condition-case nil (delete-other-windows) (error))))

;;; @ bbdb-extract-field-value -- stolen from tm-bbdb.
;;;
(eval-and-compile
  (if (fboundp 'bbdb-wl-extract-field-value-internal)
;;(if (fboundp 'PLEASE_REPLACE_WITH_SEMI-BASED_MIME-BBDB)) ;; mime-bbdb
      nil
    (if (and (string< bbdb-version "1.58")
	     ;;(not (fboundp 'bbdb-extract-field-value) ;; defined as autoload
	     (not (fboundp 'bbdb-header-start)))
	(load "bbdb-hooks")
      (require 'bbdb-hooks))
    (fset 'bbdb-wl-extract-field-value-internal
	  (cond
	   ((fboundp 'tm:bbdb-extract-field-value)
	    (symbol-function 'tm:bbdb-extract-field-value))
	   (t (symbol-function 'bbdb-extract-field-value))))
    (defun bbdb-extract-field-value (field)
      (let ((value (bbdb-wl-extract-field-value-internal field)))
	(with-temp-buffer ; to keep raw buffer unibyte.
	  (set-buffer-multibyte
	   default-enable-multibyte-characters)
	  (and value
	       (eword-decode-string value)))))
    ))


(provide 'bbdb-wl)

;;; bbdb-wl.el ends here