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
81 "Alist of entries to display.
82 The format of elements of this list should be
84 where the last element is optional."
88 (defcustom lsdb-decode-field-body-function #'lsdb-decode-field-body
93 (defcustom lsdb-canonicalize-full-name-function
94 #'lsdb-canonicalize-spaces-and-dots
95 "Way to canonicalize full name."
99 (defcustom lsdb-print-record-function
101 "Function to print LSDB record."
105 (defcustom lsdb-window-max-height 7
106 "Maximum number of lines used to display LSDB record."
111 (defface lsdb-header-face
112 '((t (:underline t)))
113 "Face for the file header line in `lsdb-mode'."
115 (defvar lsdb-header-face 'lsdb-header-face)
117 (defface lsdb-field-name-face
118 '((((class color) (background dark))
119 (:foreground "PaleTurquoise" :bold t))
121 "Face for the message header line in `lsdb-mode'."
123 (defvar lsdb-field-name-face 'lsdb-field-name-face)
125 (defface lsdb-field-body-face
126 '((((class color) (background dark))
127 (:foreground "turquoise" :italic t))
129 "Face for the message header line in `lsdb-mode'."
131 (defvar lsdb-field-body-face 'lsdb-field-body-face)
133 (defconst lsdb-font-lock-keywords
135 (0 lsdb-header-face))
137 (0 lsdb-field-body-face))
138 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
139 (1 lsdb-field-name-face)
140 (2 lsdb-field-body-face))))
142 (put 'lsdb-mode 'font-lock-defaults '(lsdb-font-lock-keywords t))
144 ;;;_* CODE - no user customizations below
145 (defvar lsdb-hash-table nil
146 "Internal hash table to hold LSDB records.")
148 (defvar lsdb-buffer-name "*LSDB*"
149 "Buffer name to display LSDB record.")
151 (defvar lsdb-hash-table-is-dirty nil
152 "Flag to indicate whether the hash table needs to be saved.")
154 ;;;_. Hash Table Emulation
155 (if (fboundp 'make-hash-table)
157 (defalias 'lsdb-puthash 'puthash)
158 (defalias 'lsdb-gethash 'gethash)
159 (defalias 'lsdb-remhash 'remhash)
160 (defalias 'lsdb-maphash 'maphash)
161 (defalias 'lsdb-hash-table-size 'hash-table-size)
162 (defalias 'lsdb-hash-table-count 'hash-table-count)
163 (defalias 'lsdb-make-hash-table 'make-hash-table))
164 (defun lsdb-puthash (key value hash-table)
165 "Hash KEY to VALUE in HASH-TABLE."
166 ;; Obarray is regarded as an open hash table, as a matter of
167 ;; fact, rehashing doesn't make sense.
169 (when (> (car hash-table)
170 (* (length (nth 1 hash-table)) 0.7))
171 (setq new-obarray (make-vector (* (length (nth 1 hash-table)) 2) 0))
174 (set (intern (symbol-name symbol) new-obarray)
175 (symbol-value symbol)))
177 (setcdr hash-table (list new-obarray)))
178 (set (intern key (nth 1 hash-table)) value)
179 (setcar hash-table (1+ (car hash-table)))))
180 (defun lsdb-gethash (key hash-table &optional default)
181 "Find hash value for KEY in HASH-TABLE.
182 If there is no corresponding value, return DEFAULT (which defaults to nil)."
183 (or (intern-soft key (nth 1 hash-table))
185 (defun lsdb-remhash (key hash-table)
186 "Remove the entry for KEY from HASH-TABLE.
187 Do nothing if there is no entry for KEY in HASH-TABLE."
188 (unintern key (nth 1 hash-table))
189 (setcar hash-table (1- (car hash-table))))
190 (defun lsdb-maphash (function hash-table)
191 "Map FUNCTION over entries in HASH-TABLE, calling it with two args,
192 each key and value in HASH-TABLE.
194 FUNCTION may not modify HASH-TABLE, with the one exception that FUNCTION
195 may remhash or puthash the entry currently being processed by FUNCTION."
198 (funcall function (symbol-name symbol) (symbol-value symbol)))
200 (defun lsdb-hash-table-size (hash-table)
201 "Return the size of HASH-TABLE.
202 This is the current number of slots in HASH-TABLE, whether occupied or not."
203 (length (nth 1 hash-table)))
204 (defalias 'lsdb-hash-table-count 'car)
205 (defun lsdb-make-hash-table (&rest args)
206 "Return a new empty hash table object."
207 (list 0 (make-vector (or (plist-get args :size) 29) 0))))
209 ;;;_. Hash Table Reader/Writer
213 ;; In XEmacs, hash tables can also be created by the lisp reader
214 ;; using structure syntax.
215 (read-from-string "#s(hash-table)")
216 (defun lsdb-load-file (file)
217 "Read the contents of FILE into a hash table."
219 (set-buffer (find-file-noselect file))
220 (re-search-forward "^#s")
222 (read (point-min-marker)))))
224 (defun lsdb-load-file (file)
225 "Read the contents of FILE into a hash table."
228 (insert-file-contents file)
230 (re-search-forward "^#s")
233 (cdr (read (point-marker))))))
234 (size (plist-get plist 'size))
235 (data (plist-get plist 'data))
236 (hash-table (lsdb-make-hash-table :size size :test 'equal)))
238 (lsdb-puthash (pop data) (pop data) hash-table))
241 (defun lsdb-save-file (file hash-table)
242 "Write the entries within HASH-TABLE into FILE."
243 (let ((coding-system-for-write lsdb-file-coding-system))
245 (if (and (or (featurep 'mule)
246 (featurep 'file-coding))
247 lsdb-file-coding-system)
248 (insert ";;; -*- coding: "
249 (if (symbolp lsdb-file-coding-system)
250 (symbol-name lsdb-file-coding-system)
252 (coding-system-name lsdb-file-coding-system))
254 (insert "#s(hash-table size "
255 (number-to-string (lsdb-hash-table-size hash-table))
256 " test equal data (")
259 (insert (prin1-to-string key) " " (prin1-to-string value) " "))
263 ;;;_. Mail Header Extraction
264 (defun lsdb-fetch-field-bodies (entity regexp)
266 (goto-char (point-min))
267 (let ((case-fold-search t)
269 (while (re-search-forward (concat "^\\(" regexp "\\):[ \t]*") nil t)
270 (push (funcall lsdb-decode-field-body-function
271 (buffer-substring (point) (std11-field-end))
274 (nreverse field-bodies))))
276 (defun lsdb-canonicalize-spaces-and-dots (string)
277 (while (string-match " +\\|[\f\t\n\r\v]+\\|\\." string)
278 (setq string (replace-match " " nil t string)))
281 (defun lsdb-extract-address-components (string)
282 (let ((components (std11-extract-address-components string)))
283 (if (nth 1 components)
285 (list (nth 1 components)
286 (funcall lsdb-canonicalize-full-name-function
288 (list (nth 1 components) (nth 1 components))))))
290 ;; stolen (and renamed) from nnheader.el
291 (defun lsdb-decode-field-body (field-body field-name
292 &optional mode max-column)
293 (mime-decode-field-body field-body
294 (if (stringp field-name)
295 (intern (capitalize field-name))
299 ;;;_. Record Management
300 (defun lsdb-maybe-load-file ()
301 (unless lsdb-hash-table
302 (if (file-exists-p lsdb-file)
303 (setq lsdb-hash-table (lsdb-load-file lsdb-file))
304 (setq lsdb-hash-table (lsdb-make-hash-table :test 'equal)))))
306 (defun lsdb-update-record (sender &optional interesting)
307 (let ((old (lsdb-gethash (nth 1 sender) lsdb-hash-table))
308 (new (cons (cons 'net (list (car sender)))
313 (setq new (cons (cons 'creation-date (format-time-string "%Y-%m-%d"))
315 (setq merged (lsdb-merge-record-entries old new)
316 record (cons (nth 1 sender) merged))
317 (unless (equal merged old)
318 (lsdb-puthash (car record) (cdr record) lsdb-hash-table)
319 (setq lsdb-hash-table-is-dirty t))
322 (defun lsdb-update-records (entity)
323 (lsdb-maybe-load-file)
324 (let (senders recipients interesting alist records bodies entry)
326 (set-buffer-multibyte nil)
327 (buffer-disable-undo)
328 (mime-insert-entity entity)
329 (std11-narrow-to-header)
331 (delq nil (mapcar #'lsdb-extract-address-components
332 (lsdb-fetch-field-bodies
333 entity lsdb-sender-headers)))
335 (delq nil (mapcar #'lsdb-extract-address-components
336 (lsdb-fetch-field-bodies
337 entity lsdb-recipients-headers))))
338 (setq alist lsdb-interesting-header-alist)
343 (if (and (nth 1 (car alist))
344 (string-match (nth 1 (car alist)) field-body))
345 (replace-match (nth 3 (car alist)) nil nil field-body)
347 (lsdb-fetch-field-bodies entity (car (car alist)))))
349 (setq entry (or (nth 2 (car alist))
352 (if (eq ?. (nth 2 (assq entry lsdb-entry-type-alist)))
356 (setq alist (cdr alist))))
358 (setq records (list (lsdb-update-record (pop senders) interesting))))
359 (setq alist (nconc senders recipients))
361 (setq records (cons (lsdb-update-record (pop alist)) records)))
364 (defun lsdb-merge-record-entries (old new)
366 (let ((entry (assq (car (car new)) old))
369 (setq old (nconc old (list (car new))))
370 (if (listp (cdr entry))
372 (setq list (cdr (car new)) pointer list)
374 (if (member (car pointer) (cdr entry))
375 (setq list (delq (car pointer) list)))
376 (setq pointer (cdr pointer)))
377 (setcdr entry (nconc (cdr entry) list)))
378 (setcdr entry (cdr (car new))))))
379 (setq new (cdr new)))
382 ;;;_. Display Management
383 (defun lsdb-temp-buffer-show-function (buffer)
384 (save-selected-window
385 (let ((window (or (get-buffer-window lsdb-buffer-name)
387 (select-window (get-largest-window))
388 (split-window-vertically))))
390 (set-window-buffer window buffer)
391 (select-window window)
392 (unless (pos-visible-in-window-p (point-max))
393 (enlarge-window (- lsdb-window-max-height (window-height))))
394 (shrink-window-if-larger-than-buffer)
395 (if (> (setq height (window-height))
396 lsdb-window-max-height)
397 (shrink-window (- height lsdb-window-max-height))
398 (shrink-window-if-larger-than-buffer)))))
400 (defun lsdb-display-record (record)
401 (let ((temp-buffer-show-function
402 (function lsdb-temp-buffer-show-function)))
403 (with-output-to-temp-buffer lsdb-buffer-name
404 (set-buffer standard-output)
405 (funcall lsdb-print-record-function record)
408 (defun lsdb-print-record (record)
409 (insert (car record) "\n")
412 (lambda (entry1 entry2)
413 (> (or (nth 1 (assq (car entry1) lsdb-entry-type-alist))
415 (or (nth 1 (assq (car entry2) lsdb-entry-type-alist))
418 (insert "\t" (capitalize (symbol-name (car (car entries)))) ": "
419 (if (listp (cdr (car entries)))
420 (mapconcat #'identity (cdr (car entries))
421 (if (eq ?, (nth 2 (assq (car (car entries))
422 lsdb-entry-type-alist)))
427 (setq entries (cdr entries)))))
430 (defvar lsdb-last-completion nil)
432 (defun lsdb-complete-name ()
433 "Complete the user full-name or net-address before point"
437 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
438 (goto-char (match-end 0))
441 (if (and (eq last-command this-command)
442 (stringp lsdb-last-completion))
444 (buffer-substring start (point))))
446 (concat "\\`" string))
448 (completion-ignore-case t)
452 (let ((net (cdr (assq 'net value))))
453 (if (string-match pattern key)
456 (mapcar (lambda (address)
457 (list (concat key " <" address ">")))
460 (if (string-match pattern (car net))
461 (push (list (car net)) candidates))
462 (setq net (cdr net))))))
464 (setq lsdb-last-completion (try-completion string candidates))
465 (if (null lsdb-last-completion)
467 (when (stringp lsdb-last-completion)
468 (delete-region start (point))
469 (insert lsdb-last-completion)))))
471 ;;;_. Major Mode (`lsdb-mode') Implementation
472 (define-derived-mode lsdb-mode fundamental-mode "LSDB"
473 "Major mode for browsing LSDB records."
474 (setq buffer-read-only t)
475 (if (featurep 'xemacs)
476 ;; In XEmacs, setting `font-lock-defaults' only affects on
477 ;; `find-file-hooks'.
478 (font-lock-set-defaults)
479 (set (make-local-variable 'font-lock-defaults)
480 '(lsdb-font-lock-keywords t))))
482 ;;;_. Interface to Semi-gnus
484 (defun lsdb-gnus-insinuate ()
485 "Call this function to hook LSDB into Semi-gnus."
486 (add-hook 'gnus-article-prepare-hook 'lsdb-gnus-update-record)
487 (add-hook 'gnus-save-newsrc-hook 'lsdb-gnus-offer-save))
489 (defvar message-mode-map)
490 (defun lsdb-gnus-insinuate-message ()
491 "Call this function to hook LSDB into Message mode."
492 (define-key message-mode-map "\M-\t" 'lsdb-complete-name))
494 (defvar gnus-current-headers)
495 (defun lsdb-gnus-update-record ()
496 (let ((records (lsdb-update-records gnus-current-headers)))
498 (lsdb-display-record (car records)))))
500 (defun lsdb-gnus-offer-save ()
501 (if (and lsdb-hash-table-is-dirty
502 (y-or-n-p "Save the LSDB now?"))
503 (lsdb-save-file lsdb-file lsdb-hash-table)))
507 ;;;_* Local emacs vars.
508 ;;; The following `outline-layout' local variable setting:
509 ;;; - closes all topics from the first topic to just before the third-to-last,
510 ;;; - shows the children of the third to last (config vars)
511 ;;; - and the second to last (code section),
512 ;;; - and closes the last topic (this local-variables section).
514 ;;;outline-layout: (0 : -1 -1 0)
517 ;;; lsdb.el ends here