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