* riece-lsdb.el (riece-lsdb-insinuate): Don't require 'lsdb.
[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 (autoload 'lsdb-maybe-load-hash-tables "lsdb")
33
34 (eval-when-compile
35   (autoload 'lsdb-lookup-records "lsdb")
36   (autoload 'lsdb-puthash "lsdb")
37   (autoload 'lsdb-remhash "lsdb")
38   (autoload 'lsdb-gethash "lsdb")
39   (autoload 'lsdb-display-records "lsdb"))
40
41 (defvar riece-lsdb-cache nil)
42
43 (defun riece-lsdb-update-cache (record)
44   (let ((irc (cdr (assq 'irc record))))
45     (while irc
46       (lsdb-puthash (car irc) (car record) riece-lsdb-cache)
47       (setq irc (cdr irc)))))
48
49 (defun riece-lsdb-delete-cache (record)
50   (let ((irc (cdr (assq 'irc record))))
51     (while irc
52       (lsdb-remhash (car irc) riece-lsdb-cache)
53       (setq irc (cdr irc)))))
54
55 (defun riece-lsdb-lookup-records (user)
56   (lsdb-maybe-load-hash-tables)
57   (let ((name (lsdb-gethash (riece-format-identity user t)
58                             riece-lsdb-cache)))
59     (if name
60         (lsdb-lookup-records name))))
61
62 (defun riece-lsdb-display-records (user)
63   (interactive
64    (let ((completion-ignore-case t))
65      (list (riece-completing-read-identity
66             "User: "
67             (riece-get-users-on-server (riece-current-server-name))))))
68   (let ((records (riece-lsdb-lookup-records user)))
69     (if records
70         (lsdb-display-records records)
71       (message "No entry for `%s'" (riece-format-identity user t)))))
72
73 (defun riece-lsdb-insinuate ()
74   (add-to-list 'riece-startup-hook
75                'lsdb-maybe-load-hash-tables)
76   (add-to-list 'lsdb-secondary-hash-tables
77                'riece-lsdb-cache)
78   (add-to-list 'lsdb-after-update-record-functions
79                'riece-lsdb-update-cache)
80   (add-to-list 'lsdb-after-delete-record-functions
81                'riece-lsdb-delete-cache)
82   (define-key riece-command-mode-map
83     "\C-cL" 'riece-lsdb-display-records))
84
85 (provide 'riece-lsdb)
86
87 ;;; riece-lsdb.el ends here