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