(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / net / eudcb-ph.el
blobefd89beaaa98827511340cc2619a270dd21b6a6e
1 ;;; eudcb-ph.el --- Emacs Unified Directory Client - CCSO PH/QI Backend
3 ;; Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <oscar@cpe.fr>
6 ;; Maintainer: Pavel Janík <Pavel@Janik.cz>
7 ;; Keywords: comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; This library provides specific CCSO PH/QI protocol support for the
29 ;; Emacs Unified Directory Client package.
31 ;;; Code:
33 (require 'eudc)
35 ;;{{{ Internal cooking
37 (eudc-protocol-set 'eudc-bbdb-conversion-alist 'eudc-ph-bbdb-conversion-alist 'ph)
38 (eudc-protocol-set 'eudc-query-function 'eudc-ph-query-internal 'ph)
39 (eudc-protocol-set 'eudc-list-attributes-function 'eudc-ph-get-field-list 'ph)
40 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes t 'ph)
42 (defvar eudc-ph-process-buffer nil)
43 (defvar eudc-ph-read-point)
45 (defconst eudc-ph-default-server-port 105
46 "Default TCP port for CCSO PH/QI directory services.")
48 (defun eudc-ph-query-internal (query &optional return-fields)
49 "Query the PH/QI server with QUERY.
50 QUERY can be a string NAME or a list made of strings NAME
51 and/or cons cells (KEY . VALUE) where KEYs should be valid
52 CCSO database keys. NAME is equivalent to (DEFAULT . NAME),
53 where DEFAULT is the default key of the database.
54 RETURN-FIELDS is a list of database fields to return,
55 defaulting to `eudc-default-return-attributes'."
56 (let (request)
57 (if (null return-fields)
58 (setq return-fields eudc-default-return-attributes))
59 (if (eq 'all return-fields)
60 (setq return-fields '(all)))
61 (setq request
62 (concat "query "
63 (if (stringp query)
64 query
65 (mapconcat (function (lambda (elt)
66 (if (stringp elt) elt)
67 (format "%s=%s" (car elt) (cdr elt))))
68 query
69 " "))
70 (if return-fields
71 (concat " return " (mapconcat 'symbol-name return-fields " ")))))
72 (and (> (length request) 6)
73 (eudc-ph-do-request request)
74 (eudc-ph-parse-query-result return-fields))))
76 (defun eudc-ph-get-field-list (full-records)
77 "Return a list of valid field names for the current server.
78 If FULL-RECORDS is non-nil, full records including field description
79 are returned"
80 (interactive)
81 (eudc-ph-do-request "fields")
82 (if full-records
83 (eudc-ph-parse-query-result)
84 (mapcar 'eudc-caar (eudc-ph-parse-query-result))))
86 (defun eudc-ph-parse-query-result (&optional fields)
87 "Return a list of alists of key/values from in `eudc-ph-process-buffer'.
88 Fields not in FIELDS are discarded."
89 (let (record
90 records
91 line-regexp
92 current-key
93 key
94 value
95 ignore)
96 (save-excursion
97 (message "Parsing results...")
98 (set-buffer eudc-ph-process-buffer)
99 (goto-char (point-min))
100 (while (re-search-forward "^\\(-[0-9]+\\):\\([0-9]+\\):" nil t)
101 (catch 'ignore
102 (setq line-regexp (concat "^\\(-[0-9]+\\):" (match-string 2) ":[ \t]*\\([-a-zA-Z_]*\\)?:[ \t]*\\(.*\\)$"))
103 (beginning-of-line)
104 (setq record nil
105 ignore nil
106 current-key nil)
107 (while (re-search-forward line-regexp nil t)
108 (catch 'skip-line
109 (if (string= "-508" (match-string 1))
110 ;; A field is missing in this entry. Skip it or skip the
111 ;; whole record (see `eudc-strict-return-matches')
112 (if (not eudc-strict-return-matches)
113 (throw 'skip-line t)
114 (while (re-search-forward line-regexp nil t))
115 (setq ignore t)
116 (throw 'ignore t)))
117 (setq key (and (not (string= (match-string 2) ""))
118 (intern (match-string 2)))
119 value (match-string 3))
120 (if (and current-key
121 (eq key current-key))
122 (setq key nil)
123 (setq current-key key))
124 (if (or (null fields)
125 (eq 'all fields)
126 (memq current-key fields))
127 (if key
128 (setq record (cons (cons key value) record)) ; New key
129 (setcdr (car record) (if (listp (eudc-cdar record))
130 (append (eudc-cdar record) (list value))
131 (list (eudc-cdar record) value))))))))
132 (and (not ignore)
133 (or (null fields)
134 (eq 'all fields)
135 (setq record (nreverse record)))
136 (setq record (if (not (eq 'list eudc-duplicate-attribute-handling-method))
137 (eudc-filter-duplicate-attributes record)
138 (list record)))
139 (setq records (append record records)))))
140 (message "Done")
141 records))
143 (defun eudc-ph-do-request (request)
144 "Send REQUEST to the server.
145 Wait for response and return the buffer containing it."
146 (let (process
147 buffer)
148 (unwind-protect
149 (progn
150 (message "Contacting server...")
151 (setq process (eudc-ph-open-session))
152 (if process
153 (save-excursion
154 (set-buffer (setq buffer (process-buffer process)))
155 (eudc-ph-send-command process request)
156 (message "Request sent, waiting for reply...")
157 (eudc-ph-read-response process))))
158 (if process
159 (eudc-ph-close-session process)))
160 buffer))
162 (defun eudc-ph-open-session (&optional server)
163 "Open a connection to the given CCSO/QI SERVER.
164 SERVER is either a string naming the server or a list (NAME PORT)."
165 (let (process
166 host
167 port)
168 (catch 'done
169 (if (null server)
170 (setq server (or eudc-server
171 (call-interactively 'eudc-ph-set-server))))
172 (string-match "\\(.*\\)\\(:\\(.*\\)\\)?" server)
173 (setq host (match-string 1 server))
174 (setq port (or (match-string 3 server)
175 eudc-ph-default-server-port))
176 (setq eudc-ph-process-buffer (get-buffer-create (format " *PH-%s*" host)))
177 (save-excursion
178 (set-buffer eudc-ph-process-buffer)
179 (erase-buffer)
180 (setq eudc-ph-read-point (point))
181 (and eudc-xemacs-mule-p
182 (set-buffer-file-coding-system 'binary t)))
183 (setq process (open-network-stream "ph" eudc-ph-process-buffer host port))
184 (if (null process)
185 (throw 'done nil))
186 (process-kill-without-query process)
187 process)))
189 (defun eudc-ph-close-session (process)
190 (save-excursion
191 (set-buffer (process-buffer process))
192 (eudc-ph-send-command process "quit")
193 (eudc-ph-read-response process)
194 (run-at-time 2 nil 'delete-process process)))
196 (defun eudc-ph-send-command (process command)
197 (goto-char (point-max))
198 (process-send-string process command)
199 (process-send-string process "\r\n")
202 (defun eudc-ph-read-response (process &optional return-response)
203 "Read a response from the PH/QI query process PROCESS.
204 Returns nil if response starts with an error code. If the
205 response is successful the return code or the response itself is returned
206 depending on RETURN-RESPONSE."
207 (let ((case-fold-search nil)
208 return-code
209 match-end)
210 (goto-char eudc-ph-read-point)
211 ;; CCSO protocol : response complete if status >= 200
212 (while (not (re-search-forward "^\\(^[2-5].*\\):.*\n" nil t))
213 (accept-process-output process)
214 (goto-char eudc-ph-read-point))
215 (setq match-end (point))
216 (goto-char eudc-ph-read-point)
217 (if (and (setq return-code (match-string 1))
218 (setq return-code (string-to-number return-code))
219 (>= (abs return-code) 300))
220 (progn (setq eudc-ph-read-point match-end) nil)
221 (setq eudc-ph-read-point match-end)
222 (if return-response
223 (buffer-substring (point) match-end)
224 return-code))))
226 ;;}}}
228 ;;{{{ High-level interfaces (interactive functions)
230 (defun eudc-ph-customize ()
231 "Customize the EUDC PH support."
232 (interactive)
233 (customize-group 'eudc-ph))
235 (defun eudc-ph-set-server (server)
236 "Set the PH server to SERVER."
237 (interactive "sNew PH/QI Server: ")
238 (message "Selected PH/QI server is now %s" server)
239 (eudc-set-server server 'ph))
241 ;;}}}
243 (eudc-register-protocol 'ph)
245 (provide 'eudcb-ph)
247 ;;; arch-tag: 4365bbf5-af20-453e-b5b6-2e7118ebfcdb
248 ;;; eudcb-ph.el ends here