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