1 ;;; lsdb.el --- the Lovely Sister Database
3 ;; Copyright (C) 2002 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: adress book
8 ;; This file is part of the Lovely Sister Database.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
28 ;;; (autoload 'lsdb-gnus-insinuate "lsdb")
29 ;;; (autoload 'lsdb-gnus-insinuate-message "lsdb")
30 ;;; (add-hook 'gnus-startup-hook 'lsdb-gnus-insinuate)
31 ;;; (add-hook 'message-setup-hook
33 ;;; (define-key message-mode-map "\M-\t" 'lsdb-complete-name)))
35 ;;; For Wanderlust, put the following lines into your ~/.wl:
37 ;;; (lsdb-wl-insinuate)
38 ;;; (add-hook 'wl-draft-mode-hook
40 ;;; (define-key wl-draft-mode-map "\M-\t" 'lsdb-complete-name)))
47 ;;;_* USER CUSTOMIZATION VARIABLES:
49 "The Lovely Sister Database."
53 (defcustom lsdb-file (expand-file-name "~/.lsdb")
54 "The name of the Lovely Sister Database file."
58 (defcustom lsdb-file-coding-system 'iso-2022-jp
59 "Coding system for `lsdb-file'."
63 (defcustom lsdb-sender-headers
65 "List of headers to search for senders."
69 (defcustom lsdb-recipients-headers
70 "Resent-To\\|Resent-Cc\\|Reply-To\\|To\\|Cc\\|Bcc"
71 "List of headers to search for recipients."
75 (defcustom lsdb-interesting-header-alist
76 '(("Organization" nil organization)
77 ("\\(X-\\)?User-Agent\\|X-Mailer" nil user-agent)
78 ("\\(X-\\)?ML-Name" nil mailing-list)
79 ("X-Attribution\\|X-cite-me" nil attribution))
80 "Alist of headers we are interested in.
81 The format of elements of this list should be
82 (FIELD-NAME REGEXP ENTRY STRING)
83 where the last three elements are optional."
87 (defcustom lsdb-entry-type-alist
95 "Alist of entries to display.
96 The format of elements of this list should be
98 where the last element is optional."
102 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
103 "Field body decoder."
107 (defcustom lsdb-canonicalize-full-name-function
108 #'lsdb-canonicalize-spaces-and-dots
109 "Way to canonicalize full name."
113 (defcustom lsdb-print-record-function
115 "Function to print LSDB record."
119 (defcustom lsdb-window-max-height 7
120 "Maximum number of lines used to display LSDB record."
125 (defface lsdb-header-face
126 '((t (:underline t)))
127 "Face for the file header line in `lsdb-mode'."
129 (defvar lsdb-header-face 'lsdb-header-face)
131 (defface lsdb-field-name-face
132 '((((class color) (background dark))
133 (:foreground "PaleTurquoise" :bold t))
135 "Face for the message header line in `lsdb-mode'."
137 (defvar lsdb-field-name-face 'lsdb-field-name-face)
139 (defface lsdb-field-body-face
140 '((((class color) (background dark))
141 (:foreground "turquoise" :italic t))
143 "Face for the message header line in `lsdb-mode'."
145 (defvar lsdb-field-body-face 'lsdb-field-body-face)
147 (defconst lsdb-font-lock-keywords
149 (0 lsdb-header-face))
151 (0 lsdb-field-body-face))
152 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
153 (1 lsdb-field-name-face)
154 (2 lsdb-field-body-face))))
156 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
158 ;;;_* CODE - no user customizations below
159 (defvar lsdb-hash-table nil
160 "Internal hash table to hold LSDB records.")
162 (defvar lsdb-buffer-name "*LSDB*"
163 "Buffer name to display LSDB record.")
165 (defvar lsdb-hash-table-is-dirty nil
166 "Flag to indicate whether the hash table needs to be saved.")
168 ;;;_. Hash Table Emulation
169 (if (fboundp 'make-hash-table)
171 (defalias 'lsdb-puthash 'puthash)
172 (defalias 'lsdb-gethash 'gethash)
173 (defalias 'lsdb-remhash 'remhash)
174 (defalias 'lsdb-maphash 'maphash)
175 (defalias 'lsdb-hash-table-size 'hash-table-size)
176 (defalias 'lsdb-hash-table-count 'hash-table-count)
177 (defalias 'lsdb-make-hash-table 'make-hash-table))
178 (defun lsdb-puthash (key value hash-table)
179 "Hash KEY to VALUE in HASH-TABLE."
180 ;; Obarray is regarded as an open hash table, as a matter of
181 ;; fact, rehashing doesn't make sense.
183 (when (> (car hash-table)
184 (* (length (nth 1 hash-table)) 0.7))
185 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
188 (set (intern (symbol-name symbol) new-obarray)
189 (symbol-value symbol)))
191 (setcdr hash-table (list new-obarray)))
192 (set (intern key (nth 1 hash-table)) value)
193 (setcar hash-table (1+ (car hash-table)))))
194 (defun lsdb-gethash (key hash-table &optional default)
195 "Find hash value for KEY in HASH-TABLE.
196 If there is no corresponding value, return DEFAULT (which defaults to nil)."
197 (let ((symbol (intern-soft key (nth 1 hash-table))))
199 (symbol-value symbol)
201 (defun lsdb-remhash (key hash-table)
202 "Remove the entry for KEY from HASH-TABLE.
203 Do nothing if there is no entry for KEY in HASH-TABLE."
204 (unintern key (nth 1 hash-table))
205 (setcar hash-table (1- (car hash-table))))
206 (defun lsdb-maphash (function hash-table)
207 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
208 each key and value in HASH-TABLE.
210 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
211 may remhash or puthash the entry currently being processed by FUNCTION."
214 (funcall function (symbol-name symbol) (symbol-value symbol)))
216 (defun lsdb-hash-table-size (hash-table)
217 "Return the size of HASH-TABLE.
218 This is the current number of slots in HASH-TABLE, whether occupied or not."
219 (length (nth 1 hash-table)))
220 (defalias 'lsdb-hash-table-count 'car)
221 (defun lsdb-make-hash-table (&rest args)
222 "Return a new empty hash table object."
223 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
225 ;;;_. Hash Table Reader/Writer
229 ;; In XEmacs, hash tables can also be created by the lisp reader
230 ;; using structure syntax.
231 (read-from-string "#s(hash-table)")
232 (defun lsdb-load-file (file)
233 "Read the contents of FILE into a hash table."
235 (set-buffer (find-file-noselect file))
236 (re-search-forward "^#s")
238 (read (point-min-marker)))))
240 (defun lsdb-load-file (file)
241 "Read the contents of FILE into a hash table."
244 (insert-file-contents file)
246 (re-search-forward "^#s")
249 (cdr (read (point-marker))))))
250 (size (plist-get plist 'size))
251 (data (plist-get plist 'data))
252 (hash-table (lsdb-make-hash-table :size size :test 'equal)))
254 (lsdb-puthash (pop data) (pop data) hash-table))
257 (defun lsdb-save-file (file hash-table)
258 "Write the entries within HASH-TABLE into FILE."
259 (let ((coding-system-for-write lsdb-file-coding-system))
261 (if (and (or (featurep 'mule)
262 (featurep 'file-coding))
263 lsdb-file-coding-system)
264 (insert ";;; -*- coding: "
265 (if (symbolp lsdb-file-coding-system)
266 (symbol-name lsdb-file-coding-system)
268 (coding-system-name lsdb-file-coding-system))
270 (insert "#s(hash-table size "
271 (number-to-string (lsdb-hash-table-size hash-table))
272 " test equal data (")
275 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
279 (defun lsdb-offer-save ()
280 (if (and lsdb-hash-table-is-dirty
281 (y-or-n-p "Save the LSDB now?"))
282 (lsdb-save-file lsdb-file lsdb-hash-table)))
284 ;;;_. Mail Header Extraction
285 (defun lsdb-fetch-field-bodies (regexp)
287 (goto-char (point-min))
288 (let ((case-fold-search t)
290 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*")
292 (push (funcall lsdb-decode-field-body-function
293 (buffer-substring (point) (std11-field-end))
296 (nreverse field-bodies))))
298 (defun lsdb-canonicalize-spaces-and-dots (string)
299 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
300 (setq string (replace-match " " nil t string)))
303 (defun lsdb-extract-address-components (string)
304 (let ((components (std11-extract-address-components string)))
305 (if (nth 1 components)
307 (list (nth 1 components)
308 (funcall lsdb-canonicalize-full-name-function
310 (list (nth 1 components) (nth 1 components))))))
312 ;; stolen (and renamed) from nnheader.el
313 (defun lsdb-decode-field-body (field-body field-name
314 &optional mode max-column)
315 (let ((multibyte enable-multibyte-characters))
318 (set-buffer-multibyte t)
319 (mime-decode-field-body field-body
320 (if (stringp field-name)
321 (intern (capitalize field-name))
324 (set-buffer-multibyte multibyte))))
326 ;;;_. Record Management
327 (defun lsdb-maybe-load-file ()
328 (unless lsdb-hash-table
329 (if (file-exists-p lsdb-file)
330 (setq lsdb-hash-table (lsdb-load-file lsdb-file))
331 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))))
333 (defun lsdb-update-record (sender &optional interesting)
334 (let ((old (lsdb-gethash (nth 1 sender) lsdb-hash-table))
335 (new (cons (cons 'net (list (car sender)))
340 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
342 (setq merged (lsdb-merge-record-entries old new)
343 record (cons (nth 1 sender) merged))
344 (unless (equal merged old)
345 (let ((entry (assq 'last-modified (cdr record)))
346 (last-modified (format-time-string "%Y-%m-%d")))
348 (setcdr entry last-modified)
349 (setcdr record (cons (cons 'last-modified last-modified)
351 (lsdb-puthash (car record) (copy-sequence (cdr record))
353 (setq lsdb-hash-table-is-dirty t))
356 (defun lsdb-update-records ()
357 (lsdb-maybe-load-file)
358 (let (senders recipients interesting alist records bodies entry)
360 (std11-narrow-to-header)
362 (delq nil (mapcar #'lsdb-extract-address-components
363 (lsdb-fetch-field-bodies
364 lsdb-sender-headers)))
366 (delq nil (mapcar #'lsdb-extract-address-components
367 (lsdb-fetch-field-bodies
368 lsdb-recipients-headers))))
369 (setq alist lsdb-interesting-header-alist)
374 (if (and (nth 1 (car alist))
375 (string-match (nth 1 (car alist)) field-body))
376 (replace-match (nth 3 (car alist)) nil nil field-body)
378 (lsdb-fetch-field-bodies (car (car alist)))))
380 (setq entry (or (nth 2 (car alist))
383 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
387 (setq alist (cdr alist))))
389 (setq records (list (lsdb-update-record (pop senders) interesting))))
390 (setq alist (nconc senders recipients))
392 (setq records (cons (lsdb-update-record (pop alist)) records)))
395 (defun lsdb-merge-record-entries (old new)
396 (setq old (copy-sequence old))
398 (let ((entry (assq (car (car new)) old))
401 (setq old (nconc old (list (car new))))
402 (if (listp (cdr entry))
404 (setq list (cdr (car new)) pointer list)
406 (if (member (car pointer) (cdr entry))
407 (setq list (delq (car pointer) list)))
408 (setq pointer (cdr pointer)))
409 (setcdr entry (nconc (cdr entry) list)))
410 (setcdr entry (cdr (car new))))))
411 (setq new (cdr new)))
414 ;;;_. Display Management
415 (defun lsdb-temp-buffer-show-function (buffer)
416 (save-selected-window
417 (let ((window (or (get-buffer-window lsdb-buffer-name)
419 (select-window (get-largest-window))
420 (split-window-vertically))))
422 (set-window-buffer window buffer)
423 (select-window window)
424 (unless (pos-visible-in-window-p (point-max))
425 (enlarge-window (- lsdb-window-max-height (window-height))))
426 (shrink-window-if-larger-than-buffer)
427 (if (> (setq height (window-height))
428 lsdb-window-max-height)
429 (shrink-window (- height lsdb-window-max-height))
430 (shrink-window-if-larger-than-buffer)))))
432 (defun lsdb-display-record (record)
433 (let ((temp-buffer-show-function
434 (function lsdb-temp-buffer-show-function)))
435 (with-output-to-temp-buffer lsdb-buffer-name
436 (set-buffer standard-output)
437 (funcall lsdb-print-record-function record)
440 (defun lsdb-print-record (record)
441 (insert (car record) "\n")
444 (lambda (entry1 entry2)
445 (> (or (nth 1 (assq (car entry1) lsdb-entry-type-alist))
447 (or (nth 1 (assq (car entry2) lsdb-entry-type-alist))
450 (insert "\t" (capitalize (symbol-name (car (car entries)))) ": "
451 (if (listp (cdr (car entries)))
452 (mapconcat #'identity (cdr (car entries))
453 (if (eq ?, (nth 2 (assq (car (car entries))
454 lsdb-entry-type-alist)))
459 (setq entries (cdr entries)))))
462 (defvar lsdb-last-completion nil)
463 (defvar lsdb-last-candidates nil)
464 (defvar lsdb-last-candidates-pointer nil)
466 (defun lsdb-complete-name ()
467 "Complete the user full-name or net-address before point"
471 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
472 (goto-char (match-end 0))
476 (completion-ignore-case t))
477 (unless (eq last-command this-command)
478 (setq lsdb-last-candidates nil
479 lsdb-last-candidates-pointer nil
480 lsdb-last-completion (buffer-substring start (point))
481 pattern (concat "\\<" lsdb-last-completion))
484 (let ((net (cdr (assq 'net value))))
485 (if (string-match pattern key)
486 (setq lsdb-last-candidates
487 (nconc lsdb-last-candidates
488 (mapcar (lambda (address)
489 (if (equal key address)
491 (concat key " <" address ">")))
494 (if (string-match pattern (car net))
495 (push (car net) lsdb-last-candidates))
496 (setq net (cdr net))))))
498 (unless lsdb-last-candidates-pointer
499 (setq lsdb-last-candidates-pointer lsdb-last-candidates))
500 (when lsdb-last-candidates-pointer
501 (delete-region start (point))
502 (insert (pop lsdb-last-candidates-pointer)))))
504 ;;;_. Major Mode (`lsdb-mode') Implementation
505 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
506 "Major mode for browsing LSDB records."
507 (setq buffer-read-only t)
508 (if (featurep 'xemacs)
509 ;; In XEmacs, setting `font-lock-defaults' only affects on
510 ;; `find-file-hooks'.
511 (font-lock-set-defaults)
512 (set (make-local-variable 'font-lock-defaults)
513 '(lsdb-font-lock-keywords t))))
515 ;;;_. Interface to Semi-gnus
517 (defun lsdb-gnus-insinuate ()
518 "Call this function to hook LSDB into Semi-gnus."
519 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
520 (add-hook 'gnus-save-newsrc-hook 'lsdb-offer-save))
522 (defvar gnus-current-headers)
523 (defun lsdb-gnus-update-record ()
524 (let ((entity gnus-current-headers)
527 (set-buffer-multibyte nil)
528 (buffer-disable-undo)
529 (mime-insert-entity entity)
530 (setq records (lsdb-update-records))
532 (lsdb-display-record (car records))))))
534 ;;;_. Interface to Wanderlust
535 (defun lsdb-wl-insinuate ()
536 "Call this function to hook LSDB into Wanderlust."
537 (add-hook 'wl-message-redisplay-hook 'lsdb-wl-update-record)
538 (add-hook 'wl-exit-hook 'lsdb-offer-save))
540 (defun lsdb-wl-update-record ()
542 (set-buffer (wl-message-get-original-buffer))
543 (let ((records (lsdb-update-records)))
545 (lsdb-display-record (car records))))))
549 ;;;_* Local emacs vars.
550 ;;; The following `outline-layout' local variable setting:
551 ;;; - closes all topics from the first topic to just before the third-to-last,
552 ;;; - shows the children of the third to last (config vars)
553 ;;; - and the second to last (code section),
554 ;;; - and closes the last topic (this local-variables section).
556 ;;;outline-layout: (0 : -1 -1 0)
559 ;;; lsdb.el ends here