chiark / gitweb /
Attach return type to return name/descr rather than choice of sub.
[disorder] / scripts / protocol
CommitLineData
200adb00
RK
1#! /usr/bin/perl -w
2#
3# This file is part of DisOrder.
4# Copyright (C) 2010 Richard Kettlewell
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18#
19use strict;
20
21# Variables and utilities -----------------------------------------------------
22
23our @h = ();
24our @c = ();
25
26sub Write {
27 my $path = shift;
28 my $lines = shift;
29
30 (open(F, ">$path")
31 and print F @$lines
32 and close F)
7788b7c7 33 or die "$0: $path: $!\n";
200adb00
RK
34}
35
36# Command classes -------------------------------------------------------------
37
50d905eb
RK
38sub c_in_decl {
39 my $arg = shift;
40
41 my $type = $arg->[0];
42 my $name = $arg->[1];
43 if($type eq 'string') {
44 return "const char *$name";
45 } elsif($type eq 'integer') {
46 return "long $name";
47 } else {
48 die "$0: unknown type '$type'\n";
49 }
50}
51
52sub c_out_decl {
53 my $arg = shift;
54
830d5c43 55 return () unless defined $arg;
50d905eb
RK
56 my $type = $arg->[0];
57 my $name = $arg->[1];
58 if($type eq 'string') {
830d5c43 59 return ("char **${name}p");
50d905eb 60 } elsif($type eq 'integer') {
830d5c43
RK
61 return ("long *${name}p");
62 } elsif($type eq 'boolean') {
63 return ("int *${name}p");
64 } elsif($type eq 'list') {
65 return ("char ***${name}p",
66 "int *n${name}p");
50d905eb
RK
67 } else {
68 die "$0: unknown type '$type'\n";
69 }
70}
71
72sub c_param_docs {
73 my $args = shift;
74 return map(" * \@param $_->[1] $_->[2]\n", @$args);
75}
76
830d5c43
RK
77sub c_return_docs {
78 my $return = shift;
79 return () unless defined $return;
80 my $type = $return->[0];
81 my $name = $return->[1];
82 my $descr = $return->[2];
83 if($type eq 'string'
84 or $type eq 'integer'
85 or $type eq 'boolean') {
86 return (" * \@param ${name}p $descr\n");
87 } elsif($type eq 'list') {
88 return (" * \@param ${name}p $descr\n",
89 " * \@param n${name}p Number of elements in ${name}p\n");
90 } else {
91 die "$0: unknown return type '$type'\n";
92 }
7788b7c7
RK
93}
94
830d5c43
RK
95# simple(CMD, SUMMARY, DETAIL,
96# [[TYPE,NAME,DESCR], [TYPE,NAME,DESCR], ...],
97# [RETURN-TYPE, RETURN-NAME, RETURN_DESCR)
98sub simple {
7788b7c7
RK
99 my $cmd = shift;
100 my $summary = shift;
101 my $detail = shift;
102 my $args = shift;
103 my $return = shift;
104
105 my $cmdc = $cmd;
106 $cmdc =~ s/-/_/g;
107 # Synchronous C API
108 push(@h, "/** \@brief $summary\n",
109 " *\n",
110 " * $detail\n",
111 " *\n",
830d5c43
RK
112 c_param_docs($args),
113 c_return_docs($return),
7788b7c7
RK
114 " * \@return 0 on success, non-0 on error\n",
115 " */\n",
50d905eb
RK
116 "int disorder_$cmdc(",
117 join(", ", "disorder_client *c",
118 map(c_in_decl($_), @$args),
830d5c43
RK
119 c_out_decl($return)),
120 ");\n\n");
50d905eb
RK
121 push(@c, "int disorder_$cmdc(",
122 join(", ", "disorder_client *c",
123 map(c_in_decl($_), @$args),
830d5c43
RK
124 c_out_decl($return)),
125 ") {\n");
126 if(!defined $return) {
127 push(@c, " return disorder_simple(c, 0, \"$cmd\"",
128 map(", $_->[1]", @$args),
129 ", (char *)0);\n",
130 );
131 } elsif($return->[0] eq 'string') {
132 push(@c, " return dequote(disorder_simple(c, $return->[1]p, \"$cmd\"",
133 map(", $_->[1]", @$args),
134 ", (char *)0), $return->[1]p);\n");
135 } elsif($return->[0] eq 'boolean') {
136 push(@c, " char *v;\n",
137 " int rc;\n",
138 " if((rc = disorder_simple(c, &v, \"$cmd\"",
139 map(", $_->[1]", @$args),
140 ", (char *)0)))\n",
141 " return rc;\n",
142 " return boolean(\"$cmd\", v, $return->[1]p);\n");
143 } elsif($return->[0] eq 'integer') {
144 push(@c, " char *v;\n",
145 " int rc;\n",
146 "\n",
147 " if((rc = disorder_simple(c, &v, \"$cmd\"",
148 map(", $_->[1]", @$args),
149 ", (char *)0)))\n",
150 " return rc;\n",
151 " *$return->[1]p = atol(v);\n",
152 " xfree(v);\n",
153 " return 0;\n");
154 } elsif($return->[0] eq 'list') {
155 push(@c, " return disorder_simple_list(c, $return->[1]p, n$return->[1]p, \"$cmd\"",
156 map(", $_->[1]", @$args),
157 ", (char *)0);\n");
158 } else {
159 die "$0: unknown return type '$return->[0]' for '$cmd'\n";
160 }
161 push(@c, "}\n\n");
7788b7c7
RK
162
163 # Asynchronous C API
164 # TODO
165
166 # Python API
167 # TODO
168
169 # Java API
170 # TODO
171}
172
50d905eb 173# string_login(CMD, SUMMARY, DETAIL, [[TYPE,NAME,DESCR], [TYPE,NAME,DESCR], ...])
7788b7c7 174#
3680ef53
RK
175# Like string(), but the server returns a username, which we squirrel
176# away rather than returning to the caller.
7788b7c7
RK
177sub string_login {
178 my $cmd = shift;
179 my $summary = shift;
180 my $detail = shift;
181 my $args = shift;
182 my $return = shift;
183
184 my $cmdc = $cmd;
185 $cmdc =~ s/-/_/g;
186 # Synchronous C API
187 push(@h, "/** \@brief $summary\n",
188 " *\n",
189 " * $detail\n",
190 " *\n",
50d905eb 191 c_param_docs($args),
7788b7c7
RK
192 " * \@return 0 on success, non-0 on error\n",
193 " */\n",
50d905eb
RK
194 "int disorder_$cmdc(",
195 join(", ", "disorder_client *c",
196 map(c_in_decl($_), @$args)),
7788b7c7 197 ");\n");
50d905eb
RK
198 push(@c, "int disorder_$cmdc(",
199 join(", ", "disorder_client *c",
200 map(c_in_decl($_), @$args)),
7788b7c7
RK
201 ") {\n",
202 " char *u;\n",
203 " int rc;\n",
204 " if((rc = disorder_simple(c, &u, \"$cmd\"",
50d905eb 205 map(", $_->[1]", @$args),
7788b7c7
RK
206 " )))\n",
207 " return rc;\n",
208 " c->user = u;\n",
209 " return 0;\n",
210 "}\n\n");
200adb00
RK
211
212 # Asynchronous C API
213 # TODO
214
215 # Python API
216 # TODO
217
218 # Java API
219 # TODO
220}
221
222# TODO other command classes
223
224# Front matter ----------------------------------------------------------------
225
226our @gpl = ("/*\n",
7788b7c7
RK
227 " * This file is part of DisOrder.\n",
228 " * Copyright (C) 2010 Richard Kettlewell\n",
229 " *\n",
230 " * This program is free software: you can redistribute it and/or modify\n",
231 " * it under the terms of the GNU General Public License as published by\n",
232 " * the Free Software Foundation, either version 3 of the License, or\n",
233 " * (at your option) any later version.\n",
234 " *\n",
235 " * This program is distributed in the hope that it will be useful,\n",
236 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n",
237 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n",
238 " * GNU General Public License for more details.\n",
239 " *\n",
240 " * You should have received a copy of the GNU General Public License\n",
241 " * along with this program. If not, see <http://www.gnu.org/licenses/>.\n",
242 " */\n");
200adb00
RK
243
244
245push(@h, @gpl,
246 "#ifndef CLIENT_STUBS_H\n",
247 "#define CLIENT_STUBS_H\n",
248 "\n");
249
250push(@c, @gpl,
251 "\n");
252
253# The protocol ----------------------------------------------------------------
254
96b1cf08
RK
255simple("adopt",
256 "Adopt a track",
257 "Makes the calling user owner of a randomly picked track.",
50d905eb 258 [["string", "id", "Track ID"]]);
200adb00 259
96b1cf08
RK
260simple("adduser",
261 "Create a user",
262 "Create a new user. Requires the 'admin' right. Email addresses etc must be filled in in separate commands.",
50d905eb
RK
263 [["string", "user", "New username"],
264 ["string", "password", "Initial password"],
265 ["string", "rights", "Initial rights (optional)"]]);
200adb00 266
830d5c43
RK
267simple("allfiles",
268 "List files and directories in a directory",
269 "See 'files' and 'dirs' for more specific lists.",
270 [["string", "dir", "Directory to list (optional)"],
271 ["string", "re", "Regexp that results must match (optional)"]],
272 ["list", "files", "List of matching files and directories"]);
200adb00 273
7788b7c7
RK
274string_login("confirm",
275 "Confirm registration",
276 "The confirmation string must have been created with 'register'. The username is returned so the caller knows who they are.",
50d905eb 277 [["string", "confirmation", "Confirmation string"]]);
200adb00 278
7788b7c7
RK
279string_login("cookie",
280 "Log in with a cookie",
281 "The cookie must have been created with 'make-cookie'. The username is returned so the caller knows who they are.",
50d905eb 282 [["string", "cookie", "Cookie string"]]);
200adb00 283
96b1cf08
RK
284simple("deluser",
285 "Delete user",
286 "Requires the 'admin' right.",
50d905eb 287 [["string", "user", "User to delete"]]);
200adb00 288
830d5c43
RK
289simple("dirs",
290 "List directories in a directory",
291 "",
292 [["string", "dir", "Directory to list (optional)"],
293 ["string", "re", "Regexp that results must match (optional)"]],
294 ["list", "files", "List of matching directories"]);
200adb00 295
96b1cf08
RK
296simple("disable",
297 "Disable play",
298 "Play will stop at the end of the current track, if one is playing. Requires the 'global prefs' right.",
299 []);
300
301simple("edituser",
302 "Set a user property",
303 "With the 'admin' right you can do anything. Otherwise you need the 'userinfo' right and can only set 'email' and 'password'.",
50d905eb
RK
304 [["string", "username", "User to modify"],
305 ["string", "property", "Property name"],
306 ["string", "value", "New property value"]]);
96b1cf08
RK
307
308simple("enable",
309 "Enable play",
310 "Requires the 'global prefs' right.",
311 []);
312
830d5c43
RK
313simple("enabled",
314 "Detect whether play is enabled",
315 "",
316 [],
317 ["boolean", "enabled", "1 if play is enabled and 0 otherwise"]);
318
319simple("exists",
320 "Test whether a track exists",
321 "",
322 [["string", "track", "Track name"]],
323 ["boolean", "exists", "1 if the track exists and 0 otherwise"]);
324
325simple("files",
326 "List files in a directory",
327 "",
328 [["string", "dir", "Directory to list (optional)"],
329 ["string", "re", "Regexp that results must match (optional)"]],
330 ["list", "files", "List of matching files"]);
331
332simple("get",
7788b7c7
RK
333 "Get a track preference",
334 "If the track does not exist that is an error. If the track exists but the preference does not then a null value is returned.",
50d905eb
RK
335 [["string", "track", "Track name"],
336 ["string", "pref", "Preference name"]],
830d5c43 337 ["string", "value", "Preference value"]);
200adb00 338
830d5c43 339simple("get-global",
7788b7c7
RK
340 "Get a global preference",
341 "If the preference does exist not then a null value is returned.",
50d905eb 342 [["string", "pref", "Global preference name"]],
830d5c43 343 ["string", "value", "Preference value"]);
200adb00 344
830d5c43
RK
345simple("length",
346 "Get a track's length",
347 "If the track does not exist an error is returned.",
348 [["string", "track", "Track name"]],
349 ["integer", "length", "Track length in seconds"]);
200adb00
RK
350
351# TODO log
352
830d5c43 353simple("make-cookie",
7788b7c7
RK
354 "Create a login cookie for this user",
355 "The cookie may be redeemed via the 'cookie' command",
356 [],
830d5c43 357 ["string", "cookie", "Newly created cookie"]);
200adb00
RK
358
359# TODO move
360
361# TODO moveafter
362
363# TODO new
364
96b1cf08
RK
365simple("nop",
366 "Do nothing",
367 "Used as a keepalive. No authentication required.",
368 []);
200adb00 369
830d5c43 370simple("part",
7788b7c7
RK
371 "Get a track name part",
372 "If the name part cannot be constructed an empty string is returned.",
50d905eb
RK
373 [["string", "track", "Track name"],
374 ["string", "context", "Context (\"sort\" or \"display\")"],
375 ["string", "part", "Name part (\"artist\", \"album\" or \"title\")"]],
830d5c43 376 ["string", "part", "Value of name part"]);
200adb00 377
96b1cf08
RK
378simple("pause",
379 "Pause the currently playing track",
380 "Requires the 'pause' right.",
381 []);
200adb00 382
830d5c43 383simple("play",
00861dcb
RK
384 "Play a track",
385 "Requires the 'play' right.",
50d905eb 386 [["string", "track", "Track to play"]],
830d5c43 387 ["string", "id", "Queue ID of new track"]);
00861dcb 388
200adb00
RK
389# TODO playafter
390
391# TODO playing
392
96b1cf08
RK
393simple("playlist-delete",
394 "Delete a playlist",
395 "Requires the 'play' right and permission to modify the playlist.",
50d905eb 396 [["string", "playlist", "Playlist to delete"]]);
200adb00 397
830d5c43
RK
398simple("playlist-get",
399 "List the contents of a playlist",
400 "Requires the 'read' right and oermission to read the playlist.",
401 [["string", "playlist", "Playlist name"]],
402 ["list", "tracks", "List of tracks in playlist"]);
200adb00 403
830d5c43 404simple("playlist-get-share",
7788b7c7
RK
405 "Get a playlist's sharing status",
406 "Requires the 'read' right and permission to read the playlist.",
50d905eb 407 [["string", "playlist", "Playlist to read"]],
830d5c43 408 ["string", "share", "Sharing status (\"public\", \"private\" or \"shared\")"]);
200adb00 409
3680ef53
RK
410simple("playlist-lock",
411 "Lock a playlist",
412 "Requires the 'play' right and permission to modify the playlist. A given connection may lock at most one playlist.",
50d905eb 413 [["string", "playlist", "Playlist to delete"]]);
3680ef53 414
96b1cf08
RK
415simple("playlist-set-share",
416 "Set a playlist's sharing status",
7788b7c7 417 "Requires the 'play' right and permission to modify the playlist.",
50d905eb
RK
418 [["string", "playlist", "Playlist to modify"],
419 ["string", "share", "New sharing status (\"public\", \"private\" or \"shared\")"]]);
200adb00 420
96b1cf08
RK
421simple("playlist-unlock",
422 "Unlock the locked playlist playlist",
423 "The playlist to unlock is implicit in the connection.",
424 []);
200adb00 425
830d5c43
RK
426simple("playlists",
427 "List playlists",
428 "Requires the 'read' right. Only playlists that you have permission to read are returned.",
429 [],
430 ["list", "playlists", "Playlist names"]);
200adb00
RK
431
432# TODO prefs
433
434# TODO queue
435
96b1cf08
RK
436simple("random-disable",
437 "Disable random play",
438 "Requires the 'global prefs' right.",
439 []);
440
441simple("random-enable",
442 "Enable random play",
443 "Requires the 'global prefs' right.",
444 []);
200adb00 445
830d5c43
RK
446simple("random-enabled",
447 "Detect whether random play is enabled",
448 "Random play counts as enabled even if play is disabled.",
449 [],
450 ["boolean", "enabled", "1 if random play is enabled and 0 otherwise"]);
200adb00
RK
451
452# TODO recent
453
96b1cf08
RK
454simple("reconfigure",
455 "Re-read configuraiton file.",
456 "Requires the 'admin' right.",
457 []);
200adb00 458
830d5c43 459simple("register",
7788b7c7
RK
460 "Register a new user",
461 "Requires the 'register' right which is usually only available to the 'guest' user. Redeem the confirmation string via 'confirm' to complete registration.",
50d905eb
RK
462 [["string", "username", "Requested new username"],
463 ["string", "password", "Requested initial password"],
464 ["string", "email", "New user's email address"]],
830d5c43 465 ["string", "confirmation", "Confirmation string"]);
200adb00 466
96b1cf08
RK
467simple("reminder",
468 "Send a password reminder.",
469 "If the user has no valid email address, or no password, or a reminder has been sent too recently, then no reminder will be sent.",
50d905eb 470 [["string", "username", "User to remind"]]);
200adb00 471
96b1cf08
RK
472simple("remove",
473 "Remove a track form the queue.",
474 "Requires one of the 'remove mine', 'remove random' or 'remove any' rights depending on how the track came to be added to the queue.",
50d905eb 475 [["string", "id", "Track ID"]]);
200adb00 476
96b1cf08
RK
477simple("rescan",
478 "Rescan all collections for new or obsolete tracks.",
479 "Requires the 'rescan' right.",
7788b7c7 480 []); # TODO wait/fresh flags
200adb00 481
830d5c43 482simple("resolve",
7788b7c7
RK
483 "Resolve a track name",
484 "Converts aliases to non-alias track names",
50d905eb 485 [["string", "track", "Track name (might be an alias)"]],
830d5c43 486 ["string", "resolved", "Resolve track name (definitely not an alias)"]);
200adb00 487
96b1cf08
RK
488simple("resume",
489 "Resume the currently playing track",
490 "Requires the 'pause' right.",
491 []);
200adb00 492
96b1cf08
RK
493simple("revoke",
494 "Revoke a cookie.",
495 "It will not subsequently be possible to log in with the cookie.",
7788b7c7 496 []); # TODO fix docs!
200adb00
RK
497
498# TODO rtp-address
499
96b1cf08
RK
500simple("scratch",
501 "Terminate the playing track.",
502 "Requires one of the 'scratch mine', 'scratch random' or 'scratch any' rights depending on how the track came to be added to the queue.",
50d905eb 503 [["string", "id", "Track ID (optional)"]]);
200adb00
RK
504
505# TODO schedule-add
506
96b1cf08
RK
507simple("schedule-del",
508 "Delete a scheduled event.",
509 "Users can always delete their own scheduled events; with the admin right you can delete any event.",
50d905eb 510 [["string", "event", "ID of event to delete"]]);
200adb00
RK
511
512# TODO schedule-get
513
830d5c43
RK
514simple("schedule-list",
515 "List scheduled events",
516 "This just lists IDs. Use 'schedule-get' to retrieve more detail",
517 [],
518 ["list", "ids", "List of event IDs"]);
200adb00 519
830d5c43
RK
520simple("search",
521 "Search for tracks",
522 "Terms are either keywords or tags formatted as 'tag:TAG-NAME'.",
523 [["string", "terms", "List of search terms"]],
524 ["list", "tracks", "List of matching tracks"]);
200adb00 525
96b1cf08
RK
526simple("set",
527 "Set a track preference",
528 "Requires the 'prefs' right.",
50d905eb
RK
529 [["string", "track", "Track name"],
530 ["string", "pref", "Preference name"],
531 ["string", "value", "New value"]]);
200adb00 532
96b1cf08
RK
533simple("set-global",
534 "Set a global preference",
535 "Requires the 'global prefs' right.",
50d905eb
RK
536 [["string", "pref", "Preference name"],
537 ["string", "value", "New value"]]);
200adb00 538
eea34c08
RK
539simple("shutdown",
540 "Request server shutdown",
541 "Requires the 'admin' right.",
542 []);
7788b7c7 543
830d5c43
RK
544simple("stats",
545 "Get server statistics",
546 "The details of what the server reports are not really defined. The returned strings are intended to be printed out one to a line..",
547 [],
548 ["list", "stats", "List of server information strings."]);
200adb00 549
830d5c43
RK
550simple("tags",
551 "Get a list of known tags",
552 "Only tags which apply to at least one track are returned.",
553 [],
554 ["list", "tags", "List of tags"]);
200adb00 555
96b1cf08
RK
556simple("unset",
557 "Unset a track preference",
558 "Requires the 'prefs' right.",
50d905eb
RK
559 [["string", "track", "Track name"],
560 ["string", "pref", "Preference name"]]);
200adb00 561
96b1cf08
RK
562simple("unset-global",
563 "Set a global preference",
564 "Requires the 'global prefs' right.",
50d905eb 565 [["string", "pref", "Preference name"]]);
200adb00 566
50d905eb 567# 'user' only used for authentication
200adb00 568
830d5c43 569simple("userinfo",
7788b7c7
RK
570 "Get a user property.",
571 "If the user does not exist an error is returned, if the user exists but the property does not then a null value is returned.",
50d905eb
RK
572 [["string", "username", "User to read"],
573 ["string", "property", "Property to read"]],
830d5c43 574 ["string", "value", "Value of property"]);
200adb00 575
830d5c43
RK
576simple("users",
577 "Get a list of users",
578 "",
579 [],
580 ["list", "users", "List of users"]);
200adb00 581
830d5c43 582simple("version",
7788b7c7
RK
583 "Get the server version",
584 "",
585 [],
830d5c43 586 ["string", "version", "Server version string"]);
200adb00
RK
587
588# TODO volume
589
590# End matter ------------------------------------------------------------------
591
592push(@h, "#endif\n");
593
594# Write it all out ------------------------------------------------------------
595
7788b7c7
RK
596Write("lib/client-stubs.h", \@h);
597Write("lib/client-stubs.c", \@c);