Mercurial > emacs
annotate lisp/net/eudcb-ph.el @ 97599:9806866dbf50
(x_update_menu_appearance): Check validity of menu font before using
it.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Thu, 21 Aug 2008 13:53:30 +0000 |
parents | 91e5880a36c1 |
children | a9dc0e7c3f2b |
rev | line source |
---|---|
27313 | 1 ;;; eudcb-ph.el --- Emacs Unified Directory Client - CCSO PH/QI Backend |
2 | |
74509 | 3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
79714 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
27313 | 5 |
42607
033986c328d5
New maintainer. New e-mail address of the author.
Pavel Janík <Pavel@Janik.cz>
parents:
42574
diff
changeset
|
6 ;; Author: Oscar Figueiredo <oscar@cpe.fr> |
033986c328d5
New maintainer. New e-mail address of the author.
Pavel Janík <Pavel@Janik.cz>
parents:
42574
diff
changeset
|
7 ;; Maintainer: Pavel Janík <Pavel@Janik.cz> |
42574 | 8 ;; Keywords: comm |
27313 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
27313 | 13 ;; it under the terms of the GNU General Public License as published by |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; (at your option) any later version. |
27313 | 16 |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
27313 | 24 |
25 ;;; Commentary: | |
42780
b136d94976d9
(eudc-ph-open-session): Remove XEmacs case.
Pavel Janík <Pavel@Janik.cz>
parents:
42607
diff
changeset
|
26 |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
27 ;; This library provides specific CCSO PH/QI protocol support for the |
42780
b136d94976d9
(eudc-ph-open-session): Remove XEmacs case.
Pavel Janík <Pavel@Janik.cz>
parents:
42607
diff
changeset
|
28 ;; Emacs Unified Directory Client package. |
27313 | 29 |
30 ;;; Code: | |
31 | |
32 (require 'eudc) | |
33 | |
34 ;;{{{ Internal cooking | |
35 | |
36 (eudc-protocol-set 'eudc-bbdb-conversion-alist 'eudc-ph-bbdb-conversion-alist 'ph) | |
37 (eudc-protocol-set 'eudc-query-function 'eudc-ph-query-internal 'ph) | |
38 (eudc-protocol-set 'eudc-list-attributes-function 'eudc-ph-get-field-list 'ph) | |
39 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes t 'ph) | |
40 | |
41 (defvar eudc-ph-process-buffer nil) | |
42 (defvar eudc-ph-read-point) | |
43 | |
44 (defconst eudc-ph-default-server-port 105 | |
45 "Default TCP port for CCSO PH/QI directory services.") | |
46 | |
47 (defun eudc-ph-query-internal (query &optional return-fields) | |
48 "Query the PH/QI server with QUERY. | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
49 QUERY can be a string NAME or a list made of strings NAME |
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
50 and/or cons cells (KEY . VALUE) where KEYs should be valid |
27313 | 51 CCSO database keys. NAME is equivalent to (DEFAULT . NAME), |
52 where DEFAULT is the default key of the database. | |
53 RETURN-FIELDS is a list of database fields to return, | |
54 defaulting to `eudc-default-return-attributes'." | |
55 (let (request) | |
56 (if (null return-fields) | |
57 (setq return-fields eudc-default-return-attributes)) | |
58 (if (eq 'all return-fields) | |
59 (setq return-fields '(all))) | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
60 (setq request |
27313 | 61 (concat "query " |
62 (if (stringp query) | |
63 query | |
64 (mapconcat (function (lambda (elt) | |
65 (if (stringp elt) elt) | |
66 (format "%s=%s" (car elt) (cdr elt)))) | |
67 query | |
68 " ")) | |
69 (if return-fields | |
70 (concat " return " (mapconcat 'symbol-name return-fields " "))))) | |
71 (and (> (length request) 6) | |
72 (eudc-ph-do-request request) | |
73 (eudc-ph-parse-query-result return-fields)))) | |
74 | |
75 (defun eudc-ph-get-field-list (full-records) | |
76 "Return a list of valid field names for the current server. | |
77 If FULL-RECORDS is non-nil, full records including field description | |
78 are returned" | |
79 (interactive) | |
80 (eudc-ph-do-request "fields") | |
81 (if full-records | |
82 (eudc-ph-parse-query-result) | |
83 (mapcar 'eudc-caar (eudc-ph-parse-query-result)))) | |
84 | |
85 (defun eudc-ph-parse-query-result (&optional fields) | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
86 "Return a list of alists of key/values from in `eudc-ph-process-buffer'. |
27313 | 87 Fields not in FIELDS are discarded." |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
88 (let (record |
27313 | 89 records |
90 line-regexp | |
91 current-key | |
92 key | |
93 value | |
94 ignore) | |
95 (save-excursion | |
96 (message "Parsing results...") | |
97 (set-buffer eudc-ph-process-buffer) | |
98 (goto-char (point-min)) | |
99 (while (re-search-forward "^\\(-[0-9]+\\):\\([0-9]+\\):" nil t) | |
100 (catch 'ignore | |
101 (setq line-regexp (concat "^\\(-[0-9]+\\):" (match-string 2) ":[ \t]*\\([-a-zA-Z_]*\\)?:[ \t]*\\(.*\\)$")) | |
102 (beginning-of-line) | |
103 (setq record nil | |
104 ignore nil | |
105 current-key nil) | |
106 (while (re-search-forward line-regexp nil t) | |
107 (catch 'skip-line | |
108 (if (string= "-508" (match-string 1)) | |
109 ;; A field is missing in this entry. Skip it or skip the | |
110 ;; whole record (see `eudc-strict-return-matches') | |
111 (if (not eudc-strict-return-matches) | |
112 (throw 'skip-line t) | |
113 (while (re-search-forward line-regexp nil t)) | |
114 (setq ignore t) | |
115 (throw 'ignore t))) | |
116 (setq key (and (not (string= (match-string 2) "")) | |
117 (intern (match-string 2))) | |
118 value (match-string 3)) | |
119 (if (and current-key | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
120 (eq key current-key)) |
27313 | 121 (setq key nil) |
122 (setq current-key key)) | |
123 (if (or (null fields) | |
124 (eq 'all fields) | |
125 (memq current-key fields)) | |
126 (if key | |
127 (setq record (cons (cons key value) record)) ; New key | |
128 (setcdr (car record) (if (listp (eudc-cdar record)) | |
129 (append (eudc-cdar record) (list value)) | |
130 (list (eudc-cdar record) value)))))))) | |
131 (and (not ignore) | |
132 (or (null fields) | |
133 (eq 'all fields) | |
134 (setq record (nreverse record))) | |
135 (setq record (if (not (eq 'list eudc-duplicate-attribute-handling-method)) | |
136 (eudc-filter-duplicate-attributes record) | |
137 (list record))) | |
42780
b136d94976d9
(eudc-ph-open-session): Remove XEmacs case.
Pavel Janík <Pavel@Janik.cz>
parents:
42607
diff
changeset
|
138 (setq records (append record records))))) |
27313 | 139 (message "Done") |
42780
b136d94976d9
(eudc-ph-open-session): Remove XEmacs case.
Pavel Janík <Pavel@Janik.cz>
parents:
42607
diff
changeset
|
140 records)) |
27313 | 141 |
142 (defun eudc-ph-do-request (request) | |
143 "Send REQUEST to the server. | |
144 Wait for response and return the buffer containing it." | |
145 (let (process | |
146 buffer) | |
147 (unwind-protect | |
148 (progn | |
149 (message "Contacting server...") | |
150 (setq process (eudc-ph-open-session)) | |
151 (if process | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
152 (save-excursion |
27313 | 153 (set-buffer (setq buffer (process-buffer process))) |
154 (eudc-ph-send-command process request) | |
155 (message "Request sent, waiting for reply...") | |
156 (eudc-ph-read-response process)))) | |
157 (if process | |
158 (eudc-ph-close-session process))) | |
159 buffer)) | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
160 |
27313 | 161 (defun eudc-ph-open-session (&optional server) |
162 "Open a connection to the given CCSO/QI SERVER. | |
163 SERVER is either a string naming the server or a list (NAME PORT)." | |
164 (let (process | |
165 host | |
166 port) | |
167 (catch 'done | |
168 (if (null server) | |
169 (setq server (or eudc-server | |
170 (call-interactively 'eudc-ph-set-server)))) | |
171 (string-match "\\(.*\\)\\(:\\(.*\\)\\)?" server) | |
172 (setq host (match-string 1 server)) | |
173 (setq port (or (match-string 3 server) | |
174 eudc-ph-default-server-port)) | |
175 (setq eudc-ph-process-buffer (get-buffer-create (format " *PH-%s*" host))) | |
176 (save-excursion | |
177 (set-buffer eudc-ph-process-buffer) | |
178 (erase-buffer) | |
179 (setq eudc-ph-read-point (point)) | |
85511
f873840f9fea
* emulation/edt-mapper.el (function-key-map):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
78230
diff
changeset
|
180 (and (featurep 'xemacs) (featurep 'mule) |
27313 | 181 (set-buffer-file-coding-system 'binary t))) |
182 (setq process (open-network-stream "ph" eudc-ph-process-buffer host port)) | |
183 (if (null process) | |
184 (throw 'done nil)) | |
66948
043fa9c73c77
(eudc-ph-open-session): Use set-process-query-on-exit-flag.
Richard M. Stallman <rms@gnu.org>
parents:
64701
diff
changeset
|
185 (set-process-query-on-exit-flag process t) |
27313 | 186 process))) |
187 | |
188 (defun eudc-ph-close-session (process) | |
189 (save-excursion | |
190 (set-buffer (process-buffer process)) | |
191 (eudc-ph-send-command process "quit") | |
192 (eudc-ph-read-response process) | |
42780
b136d94976d9
(eudc-ph-open-session): Remove XEmacs case.
Pavel Janík <Pavel@Janik.cz>
parents:
42607
diff
changeset
|
193 (run-at-time 2 nil 'delete-process process))) |
27313 | 194 |
195 (defun eudc-ph-send-command (process command) | |
196 (goto-char (point-max)) | |
197 (process-send-string process command) | |
198 (process-send-string process "\r\n") | |
199 ) | |
200 | |
201 (defun eudc-ph-read-response (process &optional return-response) | |
202 "Read a response from the PH/QI query process PROCESS. | |
203 Returns nil if response starts with an error code. If the | |
204 response is successful the return code or the response itself is returned | |
205 depending on RETURN-RESPONSE." | |
206 (let ((case-fold-search nil) | |
207 return-code | |
208 match-end) | |
209 (goto-char eudc-ph-read-point) | |
210 ;; CCSO protocol : response complete if status >= 200 | |
211 (while (not (re-search-forward "^\\(^[2-5].*\\):.*\n" nil t)) | |
212 (accept-process-output process) | |
213 (goto-char eudc-ph-read-point)) | |
214 (setq match-end (point)) | |
215 (goto-char eudc-ph-read-point) | |
216 (if (and (setq return-code (match-string 1)) | |
217 (setq return-code (string-to-number return-code)) | |
218 (>= (abs return-code) 300)) | |
219 (progn (setq eudc-ph-read-point match-end) nil) | |
220 (setq eudc-ph-read-point match-end) | |
221 (if return-response | |
222 (buffer-substring (point) match-end) | |
223 return-code)))) | |
224 | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
225 ;;}}} |
27313 | 226 |
227 ;;{{{ High-level interfaces (interactive functions) | |
228 | |
229 (defun eudc-ph-customize () | |
230 "Customize the EUDC PH support." | |
231 (interactive) | |
232 (customize-group 'eudc-ph)) | |
233 | |
234 (defun eudc-ph-set-server (server) | |
235 "Set the PH server to SERVER." | |
236 (interactive "sNew PH/QI Server: ") | |
237 (message "Selected PH/QI server is now %s" server) | |
238 (eudc-set-server server 'ph)) | |
239 | |
240 ;;}}} | |
241 | |
242 (eudc-register-protocol 'ph) | |
243 | |
244 (provide 'eudcb-ph) | |
245 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
246 ;; arch-tag: 4365bbf5-af20-453e-b5b6-2e7118ebfcdb |
27313 | 247 ;;; eudcb-ph.el ends here |