* Riece: Version 0.2.0 released.
[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 (require 'riece-identity)
33
34 (eval-when-compile
35   (autoload 'lsdb-maybe-load-hash-tables "lsdb")
36   (autoload 'lsdb-rebuild-secondary-hash-tables "lsdb")
37   (autoload 'lsdb-lookup-records "lsdb")
38   (autoload 'lsdb-puthash "lsdb")
39   (autoload 'lsdb-maphash "lsdb")
40   (autoload 'lsdb-gethash "lsdb")
41   (autoload 'lsdb-display-records "lsdb")
42   (autoload 'lsdb-update-record "lsdb"))
43
44 (defvar riece-lsdb-cache nil)
45
46 (defun riece-lsdb-update-cache (record)
47   (let ((irc (cdr (assq 'irc record))))
48     (while irc
49       (lsdb-puthash (car irc)
50                     (cons (car record)
51                           (lsdb-gethash (car irc) riece-lsdb-cache))
52                     riece-lsdb-cache)
53       (setq irc (cdr irc)))))
54
55 (defun riece-lsdb-delete-cache (record)
56   (let ((irc (cdr (assq 'irc record))))
57     (while irc
58       (lsdb-puthash (car irc)
59                     (delete (car record)
60                             (lsdb-gethash (car irc) riece-lsdb-cache))
61                     riece-lsdb-cache)
62       (setq irc (cdr irc)))))
63
64 (defun riece-lsdb-lookup-records (user)
65   (lsdb-maybe-load-hash-tables)
66   (unless riece-lsdb-cache
67     (lsdb-rebuild-secondary-hash-tables))
68   (let ((names (lsdb-gethash (riece-format-identity user t)
69                              riece-lsdb-cache))
70         records)
71     (while names
72       (setq records (append records (lsdb-lookup-records (car names))))
73       (setq names (cdr names)))
74     records))
75
76 (defun riece-lsdb-display-records (user)
77   (interactive
78    (let ((completion-ignore-case t))
79      (list (riece-completing-read-identity
80             "User: "
81             (riece-get-users-on-server (riece-current-server-name))))))
82   (let ((records (riece-lsdb-lookup-records user)))
83     (if records
84         (lsdb-display-records records)
85       (message "No entry for `%s'" (riece-format-identity user t)))))
86
87 (defvar lsdb-hash-table)
88 (defun riece-lsdb-add-user (user full-name)
89   (interactive
90    (let ((completion-ignore-case t)
91          (table lsdb-hash-table))
92      (unless (vectorp table)
93        (setq table (make-vector 29 0))
94        (lsdb-maphash (lambda (key value)
95                        (intern key table))
96                      lsdb-hash-table))
97      (list (riece-completing-read-identity
98             "User: "
99             (riece-get-users-on-server (riece-current-server-name)))
100            (completing-read "Full name: " table))))
101   (let* ((record (lsdb-gethash full-name lsdb-hash-table))
102          (irc (riece-format-identity user t))
103          (old (cdr (assq 'irc record))))
104     ;; Remove all properties before adding entry.
105     (set-text-properties 0 (length irc) nil irc)
106     (unless (member irc old)
107       (lsdb-update-record (list full-name
108                                 ;; LSDB does not allow empty 'net entry.
109                                 (or (nth 1 (assq 'net (lsdb-lookup-records
110                                                        full-name)))
111                                     ""))
112                           (list (cons 'irc (cons irc old)))))))
113
114 (defvar riece-command-mode-map)
115 (defun riece-lsdb-insinuate ()
116   (require 'lsdb)
117   (add-to-list 'lsdb-secondary-hash-tables
118                'riece-lsdb-cache)
119   (add-to-list 'lsdb-after-update-record-functions
120                'riece-lsdb-update-cache)
121   (add-to-list 'lsdb-after-delete-record-functions
122                'riece-lsdb-delete-cache)
123   (define-key riece-command-mode-map
124     "\C-c\C-ll" 'riece-lsdb-display-records)
125   (define-key riece-command-mode-map
126     "\C-c\C-la" 'riece-lsdb-add-user))
127
128 (provide 'riece-lsdb)
129
130 ;;; riece-lsdb.el ends here