1 ;;; riece-identity.el --- an identity object
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
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)
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.
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.
27 (require 'riece-globals)
28 (require 'riece-coding)
29 (require 'riece-server)
31 (defun riece-identity-prefix (identity)
32 "Return the component sans its server from IDENTITY."
35 (defun riece-identity-server (identity)
36 "Return the server component in IDENTITY."
39 (defun riece-make-identity (prefix server)
40 "Make an identity object from PREFIX and SERVER."
41 (vector prefix server))
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))
49 (riece-identity-server ident1)
50 (riece-identity-server ident2))))
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.
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))
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)))
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)))
82 (defun riece-identity-member (elt list)
83 "Return non-nil if an identity ELT is an element of LIST."
86 (if (and (vectorp (car list)) ;needed because
87 ;riece-current-channels
89 (riece-identity-equal (car list) elt))
91 (setq list (cdr list))))))
93 (defun riece-identity-assoc (elt alist)
94 "Return non-nil if an identity ELT matches the car of an element of ALIST."
97 (if (riece-identity-equal (car (car alist)) elt)
98 (throw 'found (car alist))
99 (setq alist (cdr alist))))))
101 (defun riece-identity-assign-binding (item list binding)
102 (let ((slot (riece-identity-member item binding))
104 (unless list ;we need at least one room
105 (setq list (list nil)))
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)
121 (defmacro riece-with-identity-buffer (identity &rest body)
122 `(let ((process (riece-server-process (riece-identity-server ,identity))))
124 (with-current-buffer (process-buffer process)
126 (error "Server closed"))))
128 (put 'riece-with-identity-buffer 'lisp-indent-function 1)
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 ""))
137 (concat prefix " " server)))))
139 (defun riece-encode-identity (string)
140 (let ((prefix (if (string-match " " string)
141 (substring string 0 (match-beginning 0))
143 (server (if (string-match " " string)
144 (substring string (match-end 0))
146 (riece-with-server-buffer server
147 (riece-make-identity (riece-encode-coding-string prefix) server))))
149 (defun riece-completing-read-identity (prompt channels
150 &optional predicate must-match)
154 (mapcar (lambda (channel)
155 (list (riece-decode-identity channel)))
156 (delq nil (copy-sequence (or channels
157 riece-current-channels))))
158 predicate must-match))
160 (riece-encode-identity decoded)))
161 (if (and (not (string-match "[ ,]" decoded))
162 (string-match "[ ,]" (riece-identity-prefix encoded))
163 (not (y-or-n-p (format "The encoded channel name contains illegal character \"%s\". continue? "
164 (match-string 0 (riece-identity-prefix encoded))))))
165 (error "Invalid channel name!"))
168 (provide 'riece-identity)
170 ;;; riece-identity.el ends here