Commit | Line | Data |
---|---|---|
c38e0c97 | 1 | ;;; eudcb-ph.el --- Emacs Unified Directory Client - CCSO PH/QI Backend -*- coding: utf-8 -*- |
7970b229 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1998-2013 Free Software Foundation, Inc. |
7970b229 | 4 | |
ca151ad6 | 5 | ;; Author: Oscar Figueiredo <oscar@cpe.fr> |
c38e0c97 | 6 | ;; Maintainer: Pavel Janík <Pavel@Janik.cz> |
ab651127 | 7 | ;; Keywords: comm |
bd78fa1d | 8 | ;; Package: eudc |
7970b229 GM |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
874a927a | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7970b229 | 13 | ;; it under the terms of the GNU General Public License as published by |
874a927a GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
7970b229 GM |
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 | |
874a927a | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
7970b229 GM |
24 | |
25 | ;;; Commentary: | |
886a2a61 | 26 | |
aed3fbc3 | 27 | ;; This library provides specific CCSO PH/QI protocol support for the |
886a2a61 | 28 | ;; Emacs Unified Directory Client package. |
7970b229 GM |
29 | |
30 | ;;; Code: | |
31 | ||
32 | (require 'eudc) | |
33 | ||
7970b229 GM |
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 | ||
7970b229 GM |
47 | (defun eudc-ph-query-internal (query &optional return-fields) |
48 | "Query the PH/QI server with QUERY. | |
aed3fbc3 PJ |
49 | QUERY can be a string NAME or a list made of strings NAME |
50 | and/or cons cells (KEY . VALUE) where KEYs should be valid | |
7970b229 GM |
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))) | |
aed3fbc3 | 60 | (setq request |
7970b229 GM |
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 | ||
7970b229 | 85 | (defun eudc-ph-parse-query-result (&optional fields) |
aed3fbc3 | 86 | "Return a list of alists of key/values from in `eudc-ph-process-buffer'. |
7970b229 | 87 | Fields not in FIELDS are discarded." |
aed3fbc3 | 88 | (let (record |
7970b229 GM |
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 | |
aed3fbc3 | 120 | (eq key current-key)) |
7970b229 GM |
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))) | |
886a2a61 | 138 | (setq records (append record records))))) |
7970b229 | 139 | (message "Done") |
886a2a61 | 140 | records)) |
7970b229 GM |
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 | |
9a529312 | 152 | (with-current-buffer (setq buffer (process-buffer process)) |
7970b229 GM |
153 | (eudc-ph-send-command process request) |
154 | (message "Request sent, waiting for reply...") | |
155 | (eudc-ph-read-response process)))) | |
156 | (if process | |
157 | (eudc-ph-close-session process))) | |
158 | buffer)) | |
aed3fbc3 | 159 | |
7970b229 GM |
160 | (defun eudc-ph-open-session (&optional server) |
161 | "Open a connection to the given CCSO/QI SERVER. | |
162 | SERVER is either a string naming the server or a list (NAME PORT)." | |
163 | (let (process | |
164 | host | |
165 | port) | |
166 | (catch 'done | |
167 | (if (null server) | |
168 | (setq server (or eudc-server | |
169 | (call-interactively 'eudc-ph-set-server)))) | |
170 | (string-match "\\(.*\\)\\(:\\(.*\\)\\)?" server) | |
171 | (setq host (match-string 1 server)) | |
172 | (setq port (or (match-string 3 server) | |
173 | eudc-ph-default-server-port)) | |
174 | (setq eudc-ph-process-buffer (get-buffer-create (format " *PH-%s*" host))) | |
9a529312 | 175 | (with-current-buffer eudc-ph-process-buffer |
7970b229 GM |
176 | (erase-buffer) |
177 | (setq eudc-ph-read-point (point)) | |
f8246027 | 178 | (and (featurep 'xemacs) (featurep 'mule) |
7970b229 GM |
179 | (set-buffer-file-coding-system 'binary t))) |
180 | (setq process (open-network-stream "ph" eudc-ph-process-buffer host port)) | |
181 | (if (null process) | |
182 | (throw 'done nil)) | |
4e46799e | 183 | (set-process-query-on-exit-flag process t) |
7970b229 GM |
184 | process))) |
185 | ||
7970b229 | 186 | (defun eudc-ph-close-session (process) |
9a529312 | 187 | (with-current-buffer (process-buffer process) |
7970b229 GM |
188 | (eudc-ph-send-command process "quit") |
189 | (eudc-ph-read-response process) | |
886a2a61 | 190 | (run-at-time 2 nil 'delete-process process))) |
7970b229 GM |
191 | |
192 | (defun eudc-ph-send-command (process command) | |
193 | (goto-char (point-max)) | |
194 | (process-send-string process command) | |
195 | (process-send-string process "\r\n") | |
196 | ) | |
197 | ||
198 | (defun eudc-ph-read-response (process &optional return-response) | |
199 | "Read a response from the PH/QI query process PROCESS. | |
200 | Returns nil if response starts with an error code. If the | |
201 | response is successful the return code or the response itself is returned | |
202 | depending on RETURN-RESPONSE." | |
203 | (let ((case-fold-search nil) | |
204 | return-code | |
205 | match-end) | |
206 | (goto-char eudc-ph-read-point) | |
207 | ;; CCSO protocol : response complete if status >= 200 | |
208 | (while (not (re-search-forward "^\\(^[2-5].*\\):.*\n" nil t)) | |
209 | (accept-process-output process) | |
210 | (goto-char eudc-ph-read-point)) | |
211 | (setq match-end (point)) | |
212 | (goto-char eudc-ph-read-point) | |
213 | (if (and (setq return-code (match-string 1)) | |
214 | (setq return-code (string-to-number return-code)) | |
215 | (>= (abs return-code) 300)) | |
216 | (progn (setq eudc-ph-read-point match-end) nil) | |
217 | (setq eudc-ph-read-point match-end) | |
218 | (if return-response | |
219 | (buffer-substring (point) match-end) | |
220 | return-code)))) | |
221 | ||
aed3fbc3 | 222 | ;;}}} |
7970b229 GM |
223 | |
224 | ;;{{{ High-level interfaces (interactive functions) | |
225 | ||
226 | (defun eudc-ph-customize () | |
227 | "Customize the EUDC PH support." | |
228 | (interactive) | |
229 | (customize-group 'eudc-ph)) | |
230 | ||
231 | (defun eudc-ph-set-server (server) | |
232 | "Set the PH server to SERVER." | |
233 | (interactive "sNew PH/QI Server: ") | |
234 | (message "Selected PH/QI server is now %s" server) | |
235 | (eudc-set-server server 'ph)) | |
236 | ||
237 | ;;}}} | |
238 | ||
7970b229 GM |
239 | (eudc-register-protocol 'ph) |
240 | ||
241 | (provide 'eudcb-ph) | |
242 | ||
243 | ;;; eudcb-ph.el ends here |