1566ec3663d7391fc5217873a4c020a2df108526
[riece] / lisp / riece-lsdb.el
1 ;;; riece-lsdb.el --- interface to LSDB
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; 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.
24
25 ;;; Commentary:
26
27 ;; To use, add the following line to your ~/.riece/init.el:
28 ;; (add-to-list 'riece-addons 'riece-lsdb)
29
30 ;;; Code:
31
32 (eval-when-compile
33   (autoload 'lsdb-maybe-load-hash-tables "lsdb")
34   (autoload 'lsdb-lookup-records "lsdb")
35   (autoload 'lsdb-puthash "lsdb")
36   (autoload 'lsdb-remhash "lsdb")
37   (autoload 'lsdb-gethash "lsdb")
38   (autoload 'lsdb-display-records "lsdb"))
39
40 (defvar riece-lsdb-cache nil)
41
42 (defun riece-lsdb-update-cache (record)
43   (let ((irc (cdr (assq 'irc record))))
44     (while irc
45       (lsdb-puthash (car irc) (car record) riece-lsdb-cache)
46       (setq irc (cdr irc)))))
47
48 (defun riece-lsdb-delete-cache (record)
49   (let ((irc (cdr (assq 'irc record))))
50     (while irc
51       (lsdb-remhash (car irc) riece-lsdb-cache)
52       (setq irc (cdr irc)))))
53
54 (defun riece-lsdb-lookup-records (user)
55   (lsdb-maybe-load-hash-tables)
56   (let ((name (lsdb-gethash (riece-format-identity user t)
57                             riece-lsdb-cache)))
58     (if name
59         (lsdb-lookup-records name))))
60
61 (defun riece-lsdb-display-records (user)
62   (interactive
63    (let ((completion-ignore-case t))
64      (list (riece-completing-read-identity
65             "User: "
66             (riece-get-users-on-server (riece-current-server-name))))))
67   (let ((records (riece-lsdb-lookup-records user)))
68     (if records
69         (lsdb-display-records records)
70       (message "No entry for `%s'" (riece-format-identity user t)))))
71
72 (defun riece-lsdb-insinuate ()
73   (require 'lsdb)
74   (add-to-list 'lsdb-secondary-hash-tables
75                'riece-lsdb-cache)
76   (add-to-list 'lsdb-after-update-record-functions
77                'riece-lsdb-update-cache)
78   (add-to-list 'lsdb-after-delete-record-functions
79                'riece-lsdb-delete-cache)
80   (define-key riece-command-mode-map
81     "\C-cL" 'riece-lsdb-display-records))
82
83 (provide 'riece-lsdb)
84
85 ;;; riece-lsdb.el ends here