* slp.el (toplevel): Fixed typo.
[elisp/wanderlust.git] / elmo / slp.el
1 ;;; slp.el --- A SLP interface.
2
3 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Keywords: SLP
5
6 ;; Copyright (C) 2001 Yuuichi Teranishi <teranisi@gohome.org>
7
8 ;; This file is not part of GNU Emacs
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
26 ;;; Commentary:
27 ;;
28 ;; slp.el is an elisp library providing an interface for SLP (RFC2614)
29 ;; using OpenSLP(http://www.openslp.org/) slptool .
30 ;; 
31 ;;; History:
32 ;; 28 Aug 2001 Created.
33
34 ;;; Code:
35
36 (defgroup slp nil
37   "Interface for `Service Location Protocol'."
38   :group 'comm)
39
40 (defcustom slp-program "slptool"
41   "SLP client program (OpenSLP's slptool)."
42   :type 'string
43   :group 'slp)
44
45 (defcustom slp-program-arguments nil
46   "Option argument for SLP client program."
47   :type '(repeat string)
48   :group 'slp)
49
50 (defun slp-exec-wait (type &rest args)
51   "Synchronous execution of slp-program.
52 TYPE is a symbol (one of `srvs', `attrs', `srvtypes', `as-is', `ignore')."
53   (with-temp-buffer 
54     (let ((result (apply 'call-process slp-program nil t nil (delq nil args))))
55       (unless (zerop result)
56         (error "SLP error: " (buffer-string)))
57       (goto-char (point-min))
58       (case type
59         (srvs (slp-parse-srvs))
60         (attrs (slp-parse-attrs))
61         (srvtypes (slp-parse-srvtypes))
62         (as-is (buffer-string))))))
63
64 ;; Response parser.
65 (defun slp-parse-srvs ()
66   (let (srvtype hostport host port lifetime srvs)
67     (while (and
68             (not (eobp))
69             (looking-at "service:\\([^:]+\\):/[^/]*/\\([^,]+\\),\\([0-9]+\\)"))
70       (setq srvtype (match-string 1)
71             hostport (match-string 2)
72             lifetime (string-to-number (match-string 3)))
73       (if (string-match ":\\([0-9]+\\)" hostport)
74           (setq host (substring hostport 0 (match-beginning 0))
75                 port (string-to-number (match-string 1 hostport)))
76         (setq host hostport
77               port nil))
78       (push (cons (list srvtype host port) lifetime) srvs)
79       (forward-line 1))
80     (list 'srvs (nreverse srvs))))
81
82 (defsubst slp-forward ()
83   (or (eobp) (forward-char)))
84
85 (defun slp-parse-attr ()
86   (when (looking-at "(\\([^=]+\\)=\\([^)]+\\))")
87     (prog1 (cons (match-string 1) (match-string 2))
88       (goto-char (match-end 0)))))
89
90 (defun slp-parse-attrs ()
91   (let (attrs)
92     (push (slp-parse-attr) attrs)
93     (while (eq (char-after (point)) ?,)
94       (slp-forward)
95       (push (slp-parse-attr) attrs))
96     (list 'attrs (nreverse attrs))))
97
98 (defun slp-parse-srvtypes ()
99   (let (types)
100     (while (not (eobp))
101       (when (looking-at "^service:\\([^/\n]+\\)$")
102         (push (buffer-substring (match-beginning 1) (match-end 1)) types))
103       (forward-line 1))
104     (list 'srvtypes (nreverse types))))
105
106 ;; Response accessor.
107 (defsubst slp-response-type (response)
108   (nth 0 response))
109
110 (defsubst slp-response-body (response)
111   (nth 1 response))
112
113 (defsubst slp-response-srv-url-service-type (srv)
114   (nth 0 (car srv)))
115
116 (defsubst slp-response-srv-url-host (srv)
117   (nth 1 (car srv)))
118
119 (defsubst slp-response-srv-url-port (srv)
120   (nth 2 (car srv)))
121
122 (defsubst slp-response-srv-lifetime (srv)
123   (cdr srv))
124
125 ;; Commands
126 (defun slp-findsrvs (service-type &optional filter)
127   (slp-exec-wait 'srvs "findsrvs" service-type filter))
128
129 (defun slp-findattrs (url &rest attrids)
130   (apply 'slp-exec-wait 'attrs "findattrs" url attrids))
131
132 (defun slp-findsrvtypes (&optional authority)
133   (slp-exec-wait 'srvtypes "findsrvtypes" authority))
134
135 (defun slp-findscopes ()
136   (slp-exec-wait 'as-is "findscopes"))
137
138 (defun slp-register (url &optional attrs)
139   (slp-exec-wait 'ignore "register" url (mapconcat
140                                          (lambda (pair)
141                                            (format "(%s=%s)"
142                                                    (car pair)
143                                                    (cdr pair)))
144                                          attrs
145                                          ",")))
146
147 (defun slp-deregister (url)
148   (slp-exec-wait 'ignore "deregister" url))
149
150 (defun slp-getproperty (propertyname)
151   (slp-exec-wait 'as-is "getproperty" propertyname))
152
153 (provide 'slp)
154
155 ;;; slp.el ends here