7a08a7a27ebc72aa9a1e381922a2d5f3b3319e73
[elisp/riece.git] / lisp / riece-identity.el
1 ;;; riece-identity.el --- an identity object
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 ;;; Code:
26
27 (require 'riece-globals)
28 (require 'riece-coding)
29 (require 'riece-server)
30
31 (defun riece-identity-prefix (identity)
32   "Return the component sans its server from IDENTITY."
33   (aref identity 0))
34
35 (defun riece-identity-server (identity)
36   "Return the server component in IDENTITY."
37   (aref identity 1))
38
39 (defun riece-make-identity (prefix server)
40   "Make an identity object from PREFIX and SERVER."
41   (vector prefix server))
42
43 (defun riece-identity-equal (ident1 ident2)
44   "Return t, if IDENT1 and IDENT2 is equal."
45   (and (riece-identity-equal-no-server
46         (riece-identity-prefix ident1)
47         (riece-identity-prefix ident2))
48        (equal
49         (riece-identity-server ident1)
50         (riece-identity-server ident2))))
51
52 (defun riece-identity-canonicalize-prefix (prefix)
53   "Canonicalize identity PREFIX.
54 This function downcases PREFIX first, then does special treatment for
55 Scandinavian alphabets.
56
57 RFC2812, 2.2 \"Character codes\" says:
58    Because of IRC's Scandinavian origin, the characters {}|^ are
59    considered to be the lower case equivalents of the characters []\~,
60    respectively. This is a critical issue when determining the
61    equivalence of two nicknames or channel names."
62   (let* ((result (downcase prefix))
63          (length (length result))
64          (index 0))
65     (while (< index length)
66       (if (eq (aref result index) ?\[)
67           (aset result index ?{)
68         (if (eq (aref result index) ?\])
69             (aset result index ?})
70           (if (eq (aref result index) ?\\)
71               (aset result index ?|)
72             (if (eq (aref result index) ?~)
73                 (aset result index ?^)))))
74       (setq index (1+ index)))
75     result))
76
77 (defun riece-identity-equal-no-server (prefix1 prefix2)
78   "Return t, if IDENT1 and IDENT2 is equal without server."
79   (equal (riece-identity-canonicalize-prefix prefix1)
80          (riece-identity-canonicalize-prefix prefix2)))
81
82 (defun riece-identity-member (elt list)
83   "Return non-nil if an identity ELT is an element of LIST."
84   (catch 'found
85     (while list
86       (if (and (vectorp (car list))     ;needed because
87                                         ;riece-current-channels
88                                         ;contains nil.
89                (riece-identity-equal (car list) elt))
90           (throw 'found list)
91         (setq list (cdr list))))))
92
93 (defun riece-identity-assoc (elt alist)
94   "Return non-nil if an identity ELT matches the car of an element of ALIST."
95   (catch 'found
96     (while alist
97       (if (riece-identity-equal (car (car alist)) elt)
98           (throw 'found (car alist))
99         (setq alist (cdr alist))))))
100
101 (defun riece-identity-assign-binding (item list binding)
102   (let ((slot (riece-identity-member item binding))
103         pointer)
104     (unless list                        ;we need at least one room
105       (setq list (list nil)))
106     (setq pointer list)
107     (if slot
108         (while (not (eq binding slot))
109           (unless (cdr pointer)
110             (setcdr pointer (list nil)))
111           (setq pointer (cdr pointer)
112                 binding (cdr binding)))
113       (while (or (car pointer) (car binding))
114         (unless (cdr pointer)
115           (setcdr pointer (list nil)))
116         (setq pointer (cdr pointer)
117               binding (cdr binding))))
118     (setcar pointer item)
119     list))
120
121 (defmacro riece-with-identity-buffer (identity &rest body)
122   `(let ((process (riece-server-process (riece-identity-server ,identity))))
123      (if process
124          (with-current-buffer (process-buffer process)
125            ,@body)
126        (error "Server closed"))))
127
128 (put 'riece-with-identity-buffer 'lisp-indent-function 1)
129
130 (defun riece-decode-identity (identity &optional prefix-only)
131   (riece-with-identity-buffer identity
132     (let ((prefix (riece-decode-coding-string
133                    (riece-identity-prefix identity)))
134           (server (riece-identity-server identity)))
135       (if (or prefix-only (equal server ""))
136           prefix
137         (concat prefix " " server)))))
138
139 (defun riece-encode-identity (string)
140   (let ((prefix (if (string-match " " string)
141                     (substring string 0 (match-beginning 0))
142                   string))
143         (server (if (string-match " " string)
144                     (substring string (match-end 0))
145                   "")))
146     (riece-with-server-buffer server
147       (riece-make-identity (riece-encode-coding-string prefix) server))))
148
149 (defun riece-completing-read-identity (prompt channels
150                                               &optional predicate must-match)
151   (let* ((decoded
152           (completing-read
153            prompt
154            (delq nil (mapcar (lambda (channel)
155                                (list (riece-decode-identity channel)))
156                              (or channels riece-current-channels)))
157            predicate must-match))
158          (encoded
159           (riece-encode-identity decoded)))
160     (if (and (not (string-match "[ ,]" decoded))
161              (string-match "[ ,]" (riece-identity-prefix encoded))
162              (not (y-or-n-p (format "The encoded channel name contains illegal character \"%s\".  continue? "
163                                     (match-string 0 (riece-identity-prefix encoded))))))
164         (error "Invalid channel name!"))
165     encoded))
166
167 (provide 'riece-identity)
168
169 ;;; riece-identity.el ends here