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 "Alist of headers we are interested in.
69 The format of elements of this list should be
70 (FIELD-NAME REGEXP ENTRY STRING)
71 where the last three elements are optional."
75 (defcustom lsdb-entry-type-alist
78 (mailing-list 1 ", "))
79 "Alist of entries to display.
80 The format of elements of this list should be
81 (ENTRY SCORE DELIMITER)
82 where the last element is optional."
86 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
91 (defcustom lsdb-canonicalize-full-name-function
92 #'lsdb-canonicalize-spaces-and-dots
93 "Way to canonicalize full name."
97 (defcustom lsdb-print-record-function
99 "Function to print LSDB record."
103 (defcustom lsdb-window-max-height 7
104 "Maximum number of lines used to display LSDB record."
109 (defface lsdb-header-face
110 '((t (:underline t)))
111 "Face for the file header line in `lsdb-mode'."
113 (defvar lsdb-header-face 'lsdb-header-face)
115 (defface lsdb-field-name-face
116 '((((class color) (background dark))
117 (:foreground "PaleTurquoise" :bold t))
119 "Face for the message header line in `lsdb-mode'."
121 (defvar lsdb-field-name-face 'lsdb-field-name-face)
123 (defface lsdb-field-body-face
124 '((((class color) (background dark))
125 (:foreground "turquoise" :italic t))
127 "Face for the message header line in `lsdb-mode'."
129 (defvar lsdb-field-body-face 'lsdb-field-body-face)
131 (defconst lsdb-font-lock-keywords
133 (0 lsdb-header-face))
135 (0 lsdb-field-body-face))
136 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
137 (1 lsdb-field-name-face)
138 (2 lsdb-field-body-face))))
140 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
142 ;;;_* CODE - no user customizations below
143 (defvar lsdb-hash-table nil
144 "Internal hash table to hold LSDB records.")
146 (defvar lsdb-buffer-name "*LSDB*"
147 "Buffer name to display LSDB record.")
149 ;;;_. Hash Table Emulation
150 (if (fboundp 'make-hash-table)
152 (defalias 'lsdb-puthash 'puthash)
153 (defalias 'lsdb-gethash 'gethash)
154 (defalias 'lsdb-remhash 'remhash)
155 (defalias 'lsdb-maphash 'maphash)
156 (defalias 'lsdb-hash-table-size 'hash-table-size)
157 (defalias 'lsdb-hash-table-count 'hash-table-count)
158 (defalias 'lsdb-make-hash-table 'make-hash-table))
159 (defun lsdb-puthash (key value hash-table)
160 "Hash KEY to VALUE in HASH-TABLE."
161 ;; Obarray is regarded as an open hash table, as a matter of
162 ;; fact, rehashing doesn't make sense.
164 (when (> (car hash-table)
165 (* (length (nth 1 hash-table)) 0.7))
166 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
169 (set (intern (symbol-name symbol) new-obarray)
170 (symbol-value symbol)))
172 (setcdr hash-table (list new-obarray)))
173 (set (intern key (nth 1 hash-table)) value)
174 (setcar hash-table (1+ (car hash-table)))))
175 (defun lsdb-gethash (key hash-table &optional default)
176 "Find hash value for KEY in HASH-TABLE.
177 If there is no corresponding value, return DEFAULT (which defaults to nil)."
178 (or (intern-soft key (nth 1 hash-table))
180 (defun lsdb-remhash (key hash-table)
181 "Remove the entry for KEY from HASH-TABLE.
182 Do nothing if there is no entry for KEY in HASH-TABLE."
183 (unintern key (nth 1 hash-table))
184 (setcar hash-table (1- (car hash-table))))
185 (defun lsdb-maphash (function hash-table)
186 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
187 each key and value in HASH-TABLE.
189 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
190 may remhash or puthash the entry currently being processed by FUNCTION."
193 (funcall function (symbol-name symbol) (symbol-value symbol)))
195 (defun lsdb-hash-table-size (hash-table)
196 "Return the size of HASH-TABLE.
197 This is the current number of slots in HASH-TABLE, whether occupied or not."
198 (length (nth 1 hash-table)))
199 (defalias 'lsdb-hash-table-count 'car)
200 (defun lsdb-make-hash-table (&rest args)
201 "Return a new empty hash table object."
202 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
204 ;;;_. Hash Table Reader/Writer
208 ;; In XEmacs, hash tables can also be created by the lisp reader
209 ;; using structure syntax.
210 (read-from-string "#s(hash-table)")
211 (defun lsdb-load-file (file)
212 "Read the contents of FILE into a hash table."
214 (set-buffer (find-file-noselect file))
215 (re-search-forward "^#s")
217 (read (point-min-marker)))))
219 (defun lsdb-load-file (file)
220 "Read the contents of FILE into a hash table."
223 (insert-file-contents file)
225 (re-search-forward "^#s")
228 (cdr (read (point-marker))))))
229 (size (plist-get plist 'size))
230 (data (plist-get plist 'data))
231 (hash-table (lsdb-make-hash-table :size size :test 'equal)))
233 (lsdb-puthash (pop data) (pop data) hash-table))
236 (defun lsdb-save-file (file hash-table)
237 "Write the entries within HASH-TABLE into FILE."
238 (let ((coding-system-for-write lsdb-file-coding-system))
240 (if (and (or (featurep 'mule)
241 (featurep 'file-coding))
242 lsdb-file-coding-system)
243 (insert ";;; -*- coding: "
244 (if (symbolp lsdb-file-coding-system)
245 (symbol-name lsdb-file-coding-system)
247 (coding-system-name lsdb-file-coding-system))
249 (insert "#s(hash-table size "
250 (number-to-string (lsdb-hash-table-size hash-table))
251 " test equal data (")
254 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
258 ;;;_. Mail Header Extraction
259 (defun lsdb-fetch-field-bodies (entity regexp)
261 (goto-char (point-min))
262 (let ((case-fold-search t)
264 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*") nil t)
265 (push (funcall lsdb-decode-field-body-function
266 (buffer-substring (point) (std11-field-end))
269 (nreverse field-bodies))))
271 (defun lsdb-canonicalize-spaces-and-dots (string)
272 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
273 (setq string (replace-match " " nil t string)))
276 (defun lsdb-extract-address-components (string)
277 (let ((components (std11-extract-address-components string)))
278 (if (nth 1 components)
280 (list (nth 1 components)
281 (funcall lsdb-canonicalize-full-name-function
283 (list (nth 1 components) (nth 1 components))))))
285 ;; stolen (and renamed) from nnheader.el
286 (defun lsdb-decode-field-body (field-body field-name
287 &optional mode max-column)
288 (mime-decode-field-body field-body
289 (if (stringp field-name)
290 (intern (capitalize field-name))
294 ;;;_. Record Management
295 (defun lsdb-maybe-load-file ()
296 (unless lsdb-hash-table
297 (if (file-exists-p lsdb-file)
298 (setq lsdb-hash-table (lsdb-load-file lsdb-file))
299 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))))
301 (defun lsdb-update-record (sender &optional interesting)
302 (let ((old (lsdb-gethash (nth 1 sender) lsdb-hash-table))
303 (new (cons (cons 'net (list (car sender)))
307 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
309 (setq record (cons (nth 1 sender)
310 (lsdb-merge-record-entries old new)))
311 (lsdb-puthash (car record) (cdr record) lsdb-hash-table)
314 (defun lsdb-update-records (entity)
315 (lsdb-maybe-load-file)
316 (let (senders recipients interesting alist records bodies)
318 (set-buffer-multibyte nil)
319 (buffer-disable-undo)
320 (mime-insert-entity entity)
321 (std11-narrow-to-header)
323 (delq nil (mapcar 'lsdb-extract-address-components
324 (lsdb-fetch-field-bodies
325 entity lsdb-sender-headers)))
327 (delq nil (mapcar 'lsdb-extract-address-components
328 (lsdb-fetch-field-bodies
329 entity lsdb-recipients-headers))))
330 (setq alist lsdb-interesting-header-alist)
335 (if (and (nth 1 (car alist))
336 (string-match (nth 1 (car alist)) field-body))
337 (replace-match (nth 3 (car alist)) nil nil field-body)
339 (lsdb-fetch-field-bodies entity (car (car alist)))))
341 (push (cons (or (nth 2 (car alist))
345 (setq alist (cdr alist))))
347 (setq records (list (lsdb-update-record (pop senders) interesting))))
348 (setq alist (nconc senders recipients))
350 (setq records (cons (lsdb-update-record (pop alist)) records)))
353 (defun lsdb-merge-record-entries (old new)
355 (let ((entry (assq (car (car new)) old))
358 (setq old (nconc old (list (car new))))
359 (if (listp (cdr entry))
361 (setq list (cdr (car new)) pointer list)
363 (if (member (car pointer) (cdr entry))
364 (setq list (delq (car pointer) list)))
365 (setq pointer (cdr pointer)))
366 (setcdr entry (nconc (cdr entry) list)))
367 (setcdr entry (cdr (car new))))))
368 (setq new (cdr new)))
371 ;;;_. Display Management
372 (defun lsdb-temp-buffer-show-function (buffer)
373 (save-selected-window
374 (let ((window (or (get-buffer-window lsdb-buffer-name)
376 (select-window (get-largest-window))
377 (split-window-vertically))))
379 (set-window-buffer window buffer)
380 (select-window window)
381 (unless (pos-visible-in-window-p (point-max))
382 (enlarge-window (- lsdb-window-max-height (window-height))))
383 (shrink-window-if-larger-than-buffer)
384 (if (> (setq height (window-height))
385 lsdb-window-max-height)
386 (shrink-window (- height lsdb-window-max-height))
387 (shrink-window-if-larger-than-buffer)))))
389 (defun lsdb-display-record (record)
390 (let ((temp-buffer-show-function
391 (function lsdb-temp-buffer-show-function)))
392 (with-output-to-temp-buffer lsdb-buffer-name
393 (set-buffer standard-output)
394 (funcall lsdb-print-record-function record)
397 (defun lsdb-print-record (record)
398 (insert (car record) "\n")
401 (lambda (entry1 entry2)
402 (> (or (nth 1 (assq (car entry1) lsdb-entry-type-alist))
404 (or (nth 1 (assq (car entry2) lsdb-entry-type-alist))
407 (insert "\t" (capitalize (symbol-name (car (car entries)))) ": "
408 (if (listp (cdr (car entries)))
409 (mapconcat #'identity (cdr (car entries))
410 (or (nth 2 (assq (car (car entries))
411 lsdb-entry-type-alist))
415 (setq entries (cdr entries)))))
418 (defvar lsdb-last-completion nil)
420 (defun lsdb-complete-name ()
421 "Complete the user full-name or net-address before point"
425 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
426 (goto-char (match-end 0))
429 (if (and (eq last-command this-command)
430 (stringp lsdb-last-completion))
432 (buffer-substring start (point))))
434 (concat "\\`" string))
436 (completion-ignore-case t)
440 (let ((net (cdr (assq 'net value))))
441 (if (string-match pattern key)
444 (mapcar (lambda (address)
445 (list (concat key " <" address ">")))
448 (if (string-match pattern (car net))
449 (push (list (car net)) candidates))
450 (setq net (cdr net))))))
452 (setq lsdb-last-completion (try-completion string candidates))
453 (if (null lsdb-last-completion)
455 (when (stringp lsdb-last-completion)
456 (delete-region start (point))
457 (insert lsdb-last-completion)))))
459 ;;;_. Major Mode (`lsdb-mode') Implementation
460 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
461 "Major mode for browsing LSDB records."
462 (setq buffer-read-only t)
463 (if (featurep 'xemacs)
464 ;; In XEmacs, setting `font-lock-defaults' only affects on
465 ;; `find-file-hooks'.
466 (font-lock-set-defaults)
467 (set (make-local-variable 'font-lock-defaults)
468 '(lsdb-font-lock-keywords t))))
470 ;;;_. Interface to Semi-gnus
472 (defun lsdb-gnus-insinuate ()
473 "Call this function to hook LSDB into Semi-gnus."
474 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
475 (add-hook 'gnus-save-newsrc-hook 'lsdb-gnus-offer-save))
477 (defvar message-mode-map)
478 (defun lsdb-gnus-insinuate-message ()
479 "Call this function to hook LSDB into Message mode."
480 (define-key message-mode-map "\M-\t" 'lsdb-complete-name))
482 (defvar gnus-current-headers)
483 (defun lsdb-gnus-update-record ()
484 (let ((records (lsdb-update-records gnus-current-headers)))
486 (lsdb-display-record (car records)))))
488 (defun lsdb-gnus-offer-save ()
489 (if (y-or-n-p "Save the LSDB now?")
490 (lsdb-save-file lsdb-file lsdb-hash-table)))
494 ;;;_* Local emacs vars.
495 ;;; The following `outline-layout' local variable setting:
496 ;;; - closes all topics from the first topic to just before the third-to-last,
497 ;;; - shows the children of the third to last (config vars)
498 ;;; - and the second to last (code section),
499 ;;; - and closes the last topic (this local-variables section).
501 ;;;outline-layout: (0 : -1 -1 0)
504 ;;; lsdb.el ends here