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.
27 ;;; (autoload 'lsdb-gnus-insinuate "lsdb")
28 ;;; (autoload 'lsdb-gnus-insinuate-message "lsdb")
29 ;;; (add-hook 'gnus-startup-hook 'lsdb-gnus-insinuate)
30 ;;; (add-hook 'message-setup-hook 'lsdb-gnus-insinuate-message)
36 ;;;_* USER CUSTOMIZATION VARIABLES:
38 "The Lovely Sister Database."
42 (defcustom lsdb-file (expand-file-name "~/.lsdb")
43 "The name of the Lovely Sister Database file."
47 (defcustom lsdb-file-coding-system 'iso-2022-jp
48 "Coding system for `lsdb-file'."
52 (defcustom lsdb-sender-headers
54 "List of headers to search for senders."
58 (defcustom lsdb-recipients-headers
59 "Resent-To\\|Resent-Cc\\|Reply-To\\|To\\|Cc\\|Bcc"
60 "List of headers to search for recipients."
64 (defcustom lsdb-interesting-header-alist
65 '(("Organization" nil organization)
66 ("\\(X-\\)?User-Agent\\|X-Mailer" nil user-agent)
67 ("\\(X-\\)?ML-Name" nil mailing-list)
68 ("X-Attribution\\|X-cite-me" nil attribution))
69 "Alist of headers we are interested in.
70 The format of elements of this list should be
71 (FIELD-NAME REGEXP ENTRY STRING)
72 where the last three elements are optional."
76 (defcustom lsdb-entry-type-alist
82 "Alist of entries to display.
83 The format of elements of this list should be
85 where the last element is optional."
89 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
94 (defcustom lsdb-canonicalize-full-name-function
95 #'lsdb-canonicalize-spaces-and-dots
96 "Way to canonicalize full name."
100 (defcustom lsdb-print-record-function
102 "Function to print LSDB record."
106 (defcustom lsdb-window-max-height 7
107 "Maximum number of lines used to display LSDB record."
112 (defface lsdb-header-face
113 '((t (:underline t)))
114 "Face for the file header line in `lsdb-mode'."
116 (defvar lsdb-header-face 'lsdb-header-face)
118 (defface lsdb-field-name-face
119 '((((class color) (background dark))
120 (:foreground "PaleTurquoise" :bold t))
122 "Face for the message header line in `lsdb-mode'."
124 (defvar lsdb-field-name-face 'lsdb-field-name-face)
126 (defface lsdb-field-body-face
127 '((((class color) (background dark))
128 (:foreground "turquoise" :italic t))
130 "Face for the message header line in `lsdb-mode'."
132 (defvar lsdb-field-body-face 'lsdb-field-body-face)
134 (defconst lsdb-font-lock-keywords
136 (0 lsdb-header-face))
138 (0 lsdb-field-body-face))
139 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
140 (1 lsdb-field-name-face)
141 (2 lsdb-field-body-face))))
143 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
145 ;;;_* CODE - no user customizations below
146 (defvar lsdb-hash-table nil
147 "Internal hash table to hold LSDB records.")
149 (defvar lsdb-buffer-name "*LSDB*"
150 "Buffer name to display LSDB record.")
152 (defvar lsdb-hash-table-is-dirty nil
153 "Flag to indicate whether the hash table needs to be saved.")
155 ;;;_. Hash Table Emulation
156 (if (fboundp 'make-hash-table)
158 (defalias 'lsdb-puthash 'puthash)
159 (defalias 'lsdb-gethash 'gethash)
160 (defalias 'lsdb-remhash 'remhash)
161 (defalias 'lsdb-maphash 'maphash)
162 (defalias 'lsdb-hash-table-size 'hash-table-size)
163 (defalias 'lsdb-hash-table-count 'hash-table-count)
164 (defalias 'lsdb-make-hash-table 'make-hash-table))
165 (defun lsdb-puthash (key value hash-table)
166 "Hash KEY to VALUE in HASH-TABLE."
167 ;; Obarray is regarded as an open hash table, as a matter of
168 ;; fact, rehashing doesn't make sense.
170 (when (> (car hash-table)
171 (* (length (nth 1 hash-table)) 0.7))
172 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
175 (set (intern (symbol-name symbol) new-obarray)
176 (symbol-value symbol)))
178 (setcdr hash-table (list new-obarray)))
179 (set (intern key (nth 1 hash-table)) value)
180 (setcar hash-table (1+ (car hash-table)))))
181 (defun lsdb-gethash (key hash-table &optional default)
182 "Find hash value for KEY in HASH-TABLE.
183 If there is no corresponding value, return DEFAULT (which defaults to nil)."
184 (or (intern-soft key (nth 1 hash-table))
186 (defun lsdb-remhash (key hash-table)
187 "Remove the entry for KEY from HASH-TABLE.
188 Do nothing if there is no entry for KEY in HASH-TABLE."
189 (unintern key (nth 1 hash-table))
190 (setcar hash-table (1- (car hash-table))))
191 (defun lsdb-maphash (function hash-table)
192 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
193 each key and value in HASH-TABLE.
195 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
196 may remhash or puthash the entry currently being processed by FUNCTION."
199 (funcall function (symbol-name symbol) (symbol-value symbol)))
201 (defun lsdb-hash-table-size (hash-table)
202 "Return the size of HASH-TABLE.
203 This is the current number of slots in HASH-TABLE, whether occupied or not."
204 (length (nth 1 hash-table)))
205 (defalias 'lsdb-hash-table-count 'car)
206 (defun lsdb-make-hash-table (&rest args)
207 "Return a new empty hash table object."
208 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
210 ;;;_. Hash Table Reader/Writer
214 ;; In XEmacs, hash tables can also be created by the lisp reader
215 ;; using structure syntax.
216 (read-from-string "#s(hash-table)")
217 (defun lsdb-load-file (file)
218 "Read the contents of FILE into a hash table."
220 (set-buffer (find-file-noselect file))
221 (re-search-forward "^#s")
223 (read (point-min-marker)))))
225 (defun lsdb-load-file (file)
226 "Read the contents of FILE into a hash table."
229 (insert-file-contents file)
231 (re-search-forward "^#s")
234 (cdr (read (point-marker))))))
235 (size (plist-get plist 'size))
236 (data (plist-get plist 'data))
237 (hash-table (lsdb-make-hash-table :size size :test 'equal)))
239 (lsdb-puthash (pop data) (pop data) hash-table))
242 (defun lsdb-save-file (file hash-table)
243 "Write the entries within HASH-TABLE into FILE."
244 (let ((coding-system-for-write lsdb-file-coding-system))
246 (if (and (or (featurep 'mule)
247 (featurep 'file-coding))
248 lsdb-file-coding-system)
249 (insert ";;; -*- coding: "
250 (if (symbolp lsdb-file-coding-system)
251 (symbol-name lsdb-file-coding-system)
253 (coding-system-name lsdb-file-coding-system))
255 (insert "#s(hash-table size "
256 (number-to-string (lsdb-hash-table-size hash-table))
257 " test equal data (")
260 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
264 (defun lsdb-offer-save ()
265 (if (and lsdb-hash-table-is-dirty
266 (y-or-n-p "Save the LSDB now?"))
267 (lsdb-save-file lsdb-file lsdb-hash-table)))
269 ;;;_. Mail Header Extraction
270 (defun lsdb-fetch-field-bodies (entity regexp)
272 (goto-char (point-min))
273 (let ((case-fold-search t)
275 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*") nil t)
276 (push (funcall lsdb-decode-field-body-function
277 (buffer-substring (point) (std11-field-end))
280 (nreverse field-bodies))))
282 (defun lsdb-canonicalize-spaces-and-dots (string)
283 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
284 (setq string (replace-match " " nil t string)))
287 (defun lsdb-extract-address-components (string)
288 (let ((components (std11-extract-address-components string)))
289 (if (nth 1 components)
291 (list (nth 1 components)
292 (funcall lsdb-canonicalize-full-name-function
294 (list (nth 1 components) (nth 1 components))))))
296 ;; stolen (and renamed) from nnheader.el
297 (defun lsdb-decode-field-body (field-body field-name
298 &optional mode max-column)
299 (mime-decode-field-body field-body
300 (if (stringp field-name)
301 (intern (capitalize field-name))
305 ;;;_. Record Management
306 (defun lsdb-maybe-load-file ()
307 (unless lsdb-hash-table
308 (if (file-exists-p lsdb-file)
309 (setq lsdb-hash-table (lsdb-load-file lsdb-file))
310 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))))
312 (defun lsdb-update-record (sender &optional interesting)
313 (let ((old (lsdb-gethash (nth 1 sender) lsdb-hash-table))
314 (new (cons (cons 'net (list (car sender)))
319 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
321 (setq merged (lsdb-merge-record-entries old new)
322 record (cons (nth 1 sender) merged))
323 (unless (equal merged old)
324 (lsdb-puthash (car record) (cdr record) lsdb-hash-table)
325 (setq lsdb-hash-table-is-dirty t))
328 (defun lsdb-update-records (entity)
329 (lsdb-maybe-load-file)
330 (let (senders recipients interesting alist records bodies entry)
332 (set-buffer-multibyte nil)
333 (buffer-disable-undo)
334 (mime-insert-entity entity)
335 (std11-narrow-to-header)
337 (delq nil (mapcar #'lsdb-extract-address-components
338 (lsdb-fetch-field-bodies
339 entity lsdb-sender-headers)))
341 (delq nil (mapcar #'lsdb-extract-address-components
342 (lsdb-fetch-field-bodies
343 entity lsdb-recipients-headers))))
344 (setq alist lsdb-interesting-header-alist)
349 (if (and (nth 1 (car alist))
350 (string-match (nth 1 (car alist)) field-body))
351 (replace-match (nth 3 (car alist)) nil nil field-body)
353 (lsdb-fetch-field-bodies entity (car (car alist)))))
355 (setq entry (or (nth 2 (car alist))
358 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
362 (setq alist (cdr alist))))
364 (setq records (list (lsdb-update-record (pop senders) interesting))))
365 (setq alist (nconc senders recipients))
367 (setq records (cons (lsdb-update-record (pop alist)) records)))
370 (defun lsdb-merge-record-entries (old new)
372 (let ((entry (assq (car (car new)) old))
375 (setq old (nconc old (list (car new))))
376 (if (listp (cdr entry))
378 (setq list (cdr (car new)) pointer list)
380 (if (member (car pointer) (cdr entry))
381 (setq list (delq (car pointer) list)))
382 (setq pointer (cdr pointer)))
383 (setcdr entry (nconc (cdr entry) list)))
384 (setcdr entry (cdr (car new))))))
385 (setq new (cdr new)))
388 ;;;_. Display Management
389 (defun lsdb-temp-buffer-show-function (buffer)
390 (save-selected-window
391 (let ((window (or (get-buffer-window lsdb-buffer-name)
393 (select-window (get-largest-window))
394 (split-window-vertically))))
396 (set-window-buffer window buffer)
397 (select-window window)
398 (unless (pos-visible-in-window-p (point-max))
399 (enlarge-window (- lsdb-window-max-height (window-height))))
400 (shrink-window-if-larger-than-buffer)
401 (if (> (setq height (window-height))
402 lsdb-window-max-height)
403 (shrink-window (- height lsdb-window-max-height))
404 (shrink-window-if-larger-than-buffer)))))
406 (defun lsdb-display-record (record)
407 (let ((temp-buffer-show-function
408 (function lsdb-temp-buffer-show-function)))
409 (with-output-to-temp-buffer lsdb-buffer-name
410 (set-buffer standard-output)
411 (funcall lsdb-print-record-function record)
414 (defun lsdb-print-record (record)
415 (insert (car record) "\n")
418 (lambda (entry1 entry2)
419 (> (or (nth 1 (assq (car entry1) lsdb-entry-type-alist))
421 (or (nth 1 (assq (car entry2) lsdb-entry-type-alist))
424 (insert "\t" (capitalize (symbol-name (car (car entries)))) ": "
425 (if (listp (cdr (car entries)))
426 (mapconcat #'identity (cdr (car entries))
427 (if (eq ?, (nth 2 (assq (car (car entries))
428 lsdb-entry-type-alist)))
433 (setq entries (cdr entries)))))
436 (defvar lsdb-last-completion nil)
438 (defun lsdb-complete-name ()
439 "Complete the user full-name or net-address before point"
443 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
444 (goto-char (match-end 0))
447 (if (and (eq last-command this-command)
448 (stringp lsdb-last-completion))
450 (buffer-substring start (point))))
452 (concat "\\`" string))
454 (completion-ignore-case t)
458 (let ((net (cdr (assq 'net value))))
459 (if (string-match pattern key)
462 (mapcar (lambda (address)
463 (list (concat key " <" address ">")))
466 (if (string-match pattern (car net))
467 (push (list (car net)) candidates))
468 (setq net (cdr net))))))
470 (setq lsdb-last-completion (try-completion string candidates))
471 (if (null lsdb-last-completion)
473 (when (stringp lsdb-last-completion)
474 (delete-region start (point))
475 (insert lsdb-last-completion)))))
477 ;;;_. Major Mode (`lsdb-mode') Implementation
478 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
479 "Major mode for browsing LSDB records."
480 (setq buffer-read-only t)
481 (if (featurep 'xemacs)
482 ;; In XEmacs, setting `font-lock-defaults' only affects on
483 ;; `find-file-hooks'.
484 (font-lock-set-defaults)
485 (set (make-local-variable 'font-lock-defaults)
486 '(lsdb-font-lock-keywords t))))
488 ;;;_. Interface to Semi-gnus
490 (defun lsdb-gnus-insinuate ()
491 "Call this function to hook LSDB into Semi-gnus."
492 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
493 (add-hook 'gnus-save-newsrc-hook 'lsdb-offer-save))
495 (defvar message-mode-map)
496 (defun lsdb-gnus-insinuate-message ()
497 "Call this function to hook LSDB into Message mode."
498 (define-key message-mode-map "\M-\t" 'lsdb-complete-name))
500 (defvar gnus-current-headers)
501 (defun lsdb-gnus-update-record ()
502 (let ((records (lsdb-update-records gnus-current-headers)))
504 (lsdb-display-record (car records)))))
508 ;;;_* Local emacs vars.
509 ;;; The following `outline-layout' local variable setting:
510 ;;; - closes all topics from the first topic to just before the third-to-last,
511 ;;; - shows the children of the third to last (config vars)
512 ;;; - and the second to last (code section),
513 ;;; - and closes the last topic (this local-variables section).
515 ;;;outline-layout: (0 : -1 -1 0)
518 ;;; lsdb.el ends here