3 # This file is part of DisOrder.
4 # Copyright (C) 2010-11 Richard Kettlewell
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.
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.
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/>.
21 # This file contains the definition of the disorder protocol, plus
22 # code to generates stubs for it in the various supported languages.
24 # At the time of writing it is a work in progress!
29 # string A (Unicode) string.
30 # string-raw A string that is not subject to de-quoting (return only)
31 # integer An integer. Decimal on the wire.
32 # time A timestamp. Decimal on the wire.
33 # boolean True or false. "yes" or "no" on the wire.
34 # list In commands: a list of strings in the command.
35 # In returns: a list of lines in the response.
36 # pair-list In returns: a list of key-value pairs in a response body.
37 # body In commands: a list of strings as a command body.
38 # In returns: a list of strings as a response body.
39 # queue In returns: a list of queue entries in a response body.
40 # queue-one In returns: a queue entry in the response.
41 # literal Constant string sent in sequence
44 # Variables and utilities -----------------------------------------------------
51 # Write array ref LINES to file PATH.
59 or die "$0: $path: $!\n";
62 # Command classes -------------------------------------------------------------
64 # c_in_decl([TYPE, NAME])
66 # Return the C declaration for an input parameter of type TYPE with
73 if($type eq 'string') {
74 return "const char *$name";
75 } elsif($type eq 'integer') {
77 } elsif($type eq 'time') {
78 return "time_t $name";
79 } elsif($type eq 'list' or $type eq 'body') {
80 return ("char **$name",
82 } elsif($type eq 'literal') {
85 die "$0: c_in_decl: unknown type '$type'\n";
89 # c_out_decl([TYPE, NAME])
91 # Return the C declaration for an output (reference) parameter of type
92 # TYPE with name NAME.
96 return () unless defined $arg;
99 if($type eq 'string' or $type eq 'string-raw') {
100 return ("char **${name}p");
101 } elsif($type eq 'integer') {
102 return ("long *${name}p");
103 } elsif($type eq 'time') {
104 return ("time_t *${name}p");
105 } elsif($type eq 'boolean') {
106 return ("int *${name}p");
107 } elsif($type eq 'list' or $type eq 'body') {
108 return ("char ***${name}p",
110 } elsif($type eq 'pair-list') {
111 return ("struct kvp **${name}p");
112 } elsif($type eq 'queue' or $type eq 'queue-one') {
113 return ("struct queue_entry **${name}p");
114 } elsif($type eq 'user') {
117 die "$0: c_out_decl: unknown type '$type'\n";
121 # c_param_docs([TYPE, NAME})
123 # Return the doc string for a C input parameter.
127 for my $arg (@$args) {
128 my $type = $arg->[0];
129 my $name = $arg->[1];
130 my $description = $arg->[2];
131 if($type eq 'body' or $type eq 'list') {
133 " * \@param $name $description\n",
134 " * \@param n$name Length of $name\n");
135 } elsif($type ne 'literal') {
136 push(@d, " * \@param $name $description\n");
142 # c_param_docs([TYPE, NAME})
144 # Return the doc string for a C output parameter.
147 return () unless defined $returns;
148 for my $return (@$returns) {
149 my $type = $return->[0];
150 my $name = $return->[1];
151 my $descr = $return->[2];
153 or $type eq 'string-raw'
154 or $type eq 'integer'
156 or $type eq 'boolean') {
157 return (" * \@param ${name}p $descr\n");
158 } elsif($type eq 'list' or $type eq 'body') {
159 return (" * \@param ${name}p $descr\n",
160 " * \@param n${name}p Number of elements in ${name}p\n");
161 } elsif($type eq 'pair-list') {
162 return (" * \@param ${name}p $descr\n");
163 } elsif($type eq 'queue' or $type eq 'queue-one') {
164 return (" * \@param ${name}p $descr\n");
165 } elsif($type eq 'user') {
168 die "$0: c_return_docs: unknown type '$type'\n";
173 # simple(CMD, SUMMARY, DETAIL,
174 # [[TYPE,NAME,DESCR], [TYPE,NAME,DESCR], ...],
175 # [[RETURN-TYPE, RETURN-NAME, RETURN_DESCR]])
177 # CMD is normally just the name of the command, but can
178 # be [COMMAND,FUNCTION] if the function name should differ
179 # from the protocol command.
188 if(ref $cmd eq 'ARRAY') {
195 print STDERR "Processing $cmd... ";
196 # C argument types and conversions
198 my @conversions = ();
199 for my $arg (@$args) {
200 if($arg->[0] eq 'body' or $arg->[0] eq 'list') {
201 push(@cargs, "disorder_$arg->[0]", $arg->[1], "n$arg->[1]");
202 } elsif($arg->[0] eq 'string') {
203 push(@cargs, $arg->[1]);
204 } elsif($arg->[0] eq 'integer') {
205 push(@cargs, "buf_$arg->[1]");
207 " char buf_$arg->[1]\[16];\n",
208 " byte_snprintf(buf_$arg->[1], sizeof buf_$arg->[1], \"%ld\", $arg->[1]);\n");
209 } elsif($arg->[0] eq 'time') {
210 push(@cargs, "buf_$arg->[1]");
212 " char buf_$arg->[1]\[16];\n",
213 " byte_snprintf(buf_$arg->[1], sizeof buf_$arg->[1], \"%lld\", (long long)$arg->[1]);\n");
214 } elsif($arg->[0] eq 'literal') {
215 push(@cargs, "\"$arg->[1]\"");
217 die "$0: unsupported arg type '$arg->[0]' for '$cmd'\n";
222 push(@h, "/** \@brief $summary\n",
226 " * \@param c Client\n",
228 c_return_docs($returns),
229 " * \@return 0 on success, non-0 on error\n",
231 "int disorder_$cmdc(",
232 join(", ", "disorder_client *c",
233 map(c_in_decl($_), @$args),
234 map(c_out_decl($_), @$returns)),
237 push(@c, "int disorder_$cmdc(",
238 join(", ", "disorder_client *c",
239 map(c_in_decl($_), @$args),
240 map(c_out_decl($_), @$returns)),
243 if(!defined $returns or scalar @$returns == 0) {
245 push(@c, " return disorder_simple(",
246 join(", ", "c", "NULL", "\"$cmd\"", @cargs, "(char *)NULL"),
248 } elsif(scalar @$returns == 1
249 and $returns->[0]->[0] eq 'queue-one') {
251 my $return = $$returns[0];
252 push(@c, " return onequeue(c, \"$cmd\", $return->[1]p);\n");
253 } elsif(scalar @$returns == 1
254 and $returns->[0]->[0] eq 'string-raw') {
256 my $return = $$returns[0];
257 push(@c, " return disorder_simple(",
258 join(", ", "c", "$return->[1]p", "\"$cmd\"", @cargs, "(char *)NULL"),
260 } elsif(scalar @$returns == 1
261 and $returns->[0]->[0] eq 'pair-list') {
263 my $return = $$returns[0];
264 push(@c, " return pairlist(",
265 join(", ", "c", "$return->[1]p", "\"$cmd\"",
271 for(my $n = 0; $n < scalar @$returns; ++$n) {
272 my $return = $returns->[$n];
273 my $type = $return->[0];
274 my $name = $return->[1];
276 or $type eq 'boolean'
277 or $type eq 'integer'
279 or $type eq 'user') {
284 push(@c, " char **v;\n",
285 " int nv, rc = disorder_simple_split(",
299 " int rc = disorder_simple(",
310 for(my $n = 0; $n < scalar @$returns; ++$n) {
311 my $return = $returns->[$n];
312 my $type = $return->[0];
313 my $name = $return->[1];
314 if($type eq 'string') {
316 " *${name}p = v[$n];\n",
318 } elsif($type eq 'boolean') {
320 " if(boolean(\"$cmd\", v[$n], ${name}p))\n",
322 } elsif($type eq 'integer') {
324 " *${name}p = atol(v[$n]);\n");
325 } elsif($type eq 'time') {
327 " *${name}p = atoll(v[$n]);\n");
328 } elsif($type eq 'user') {
330 " c->user = v[$n];\n",
332 } elsif($type eq 'body') {
334 " if(readlist(c, ${name}p, n${name}p))\n",
336 } elsif($type eq 'queue') {
338 " if(readqueue(c, ${name}p))\n",
341 die "$0: C API: unknown return type '$type' for '$name'\n";
346 " free_strings(nv, v);\n");
348 push(@c, " return 0;\n");
363 # TODO other command classes
365 # Front matter ----------------------------------------------------------------
367 our @generated = ("/*\n",
368 " * Automatically generated file, see scripts/protocol\n",
374 " * This file is part of DisOrder.\n",
375 " * Copyright (C) 2010-11 Richard Kettlewell\n",
377 " * This program is free software: you can redistribute it and/or modify\n",
378 " * it under the terms of the GNU General Public License as published by\n",
379 " * the Free Software Foundation, either version 3 of the License, or\n",
380 " * (at your option) any later version.\n",
382 " * This program is distributed in the hope that it will be useful,\n",
383 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n",
384 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n",
385 " * GNU General Public License for more details.\n",
387 " * You should have received a copy of the GNU General Public License\n",
388 " * along with this program. If not, see <http://www.gnu.org/licenses/>.\n",
392 push(@h, @generated, @gpl,
393 "#ifndef CLIENT_STUBS_H\n",
394 "#define CLIENT_STUBS_H\n",
397 push(@c, @generated, @gpl,
400 # The protocol ----------------------------------------------------------------
404 "Makes the calling user owner of a randomly picked track.",
405 [["string", "id", "Track ID"]]);
409 "Create a new user. Requires the 'admin' right. Email addresses etc must be filled in in separate commands.",
410 [["string", "user", "New username"],
411 ["string", "password", "Initial password"],
412 ["string", "rights", "Initial rights (optional)"]]);
415 "List files and directories in a directory",
416 "See 'files' and 'dirs' for more specific lists.",
417 [["string", "dir", "Directory to list (optional)"],
418 ["string", "re", "Regexp that results must match (optional)"]],
419 [["body", "files", "List of matching files and directories"]]);
422 "Confirm registration",
423 "The confirmation string must have been created with 'register'. The username is returned so the caller knows who they are.",
424 [["string", "confirmation", "Confirmation string"]],
428 "Log in with a cookie",
429 "The cookie must have been created with 'make-cookie'. The username is returned so the caller knows who they are.",
430 [["string", "cookie", "Cookie string"]],
435 "Requires the 'admin' right.",
436 [["string", "user", "User to delete"]]);
439 "List directories in a directory",
441 [["string", "dir", "Directory to list (optional)"],
442 ["string", "re", "Regexp that results must match (optional)"]],
443 [["body", "files", "List of matching directories"]]);
447 "Play will stop at the end of the current track, if one is playing. Requires the 'global prefs' right.",
451 "Set a user property",
452 "With the 'admin' right you can do anything. Otherwise you need the 'userinfo' right and can only set 'email' and 'password'.",
453 [["string", "username", "User to modify"],
454 ["string", "property", "Property name"],
455 ["string", "value", "New property value"]]);
459 "Requires the 'global prefs' right.",
463 "Detect whether play is enabled",
466 [["boolean", "enabled", "1 if play is enabled and 0 otherwise"]]);
469 "Test whether a track exists",
471 [["string", "track", "Track name"]],
472 [["boolean", "exists", "1 if the track exists and 0 otherwise"]]);
475 "List files in a directory",
477 [["string", "dir", "Directory to list (optional)"],
478 ["string", "re", "Regexp that results must match (optional)"]],
479 [["body", "files", "List of matching files"]]);
482 "Get a track preference",
483 "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.",
484 [["string", "track", "Track name"],
485 ["string", "pref", "Preference name"]],
486 [["string", "value", "Preference value"]]);
489 "Get a global preference",
490 "If the preference does exist not then a null value is returned.",
491 [["string", "pref", "Global preference name"]],
492 [["string", "value", "Preference value"]]);
495 "Get a track's length",
496 "If the track does not exist an error is returned.",
497 [["string", "track", "Track name"]],
498 [["integer", "length", "Track length in seconds"]]);
502 simple("make-cookie",
503 "Create a login cookie for this user",
504 "The cookie may be redeemed via the 'cookie' command",
506 [["string", "cookie", "Newly created cookie"]]);
510 "Requires one of the 'move mine', 'move random' or 'move any' rights depending on how the track came to be added to the queue.",
511 [["string", "track", "Track ID or name"],
512 ["integer", "delta", "How far to move the track towards the head of the queue"]]);
515 "Move multiple tracks",
516 "Requires one of the 'move mine', 'move random' or 'move any' rights depending on how the track came to be added to the queue.",
517 [["string", "target", "Move after this track, or to head if \"\""],
518 ["list", "ids", "List of tracks to move by ID"]]);
520 simple(["new", "new_tracks"],
521 "List recently added tracks",
523 [["integer", "max", "Maximum tracks to fetch, or 0 for all available"]],
524 [["body", "tracks", "Recently added tracks"]]);
528 "Used as a keepalive. No authentication required.",
532 "Get a track name part",
533 "If the name part cannot be constructed an empty string is returned.",
534 [["string", "track", "Track name"],
535 ["string", "context", "Context (\"sort\" or \"display\")"],
536 ["string", "part", "Name part (\"artist\", \"album\" or \"title\")"]],
537 [["string", "part", "Value of name part"]]);
540 "Pause the currently playing track",
541 "Requires the 'pause' right.",
546 "Requires the 'play' right.",
547 [["string", "track", "Track to play"]],
548 [["string-raw", "id", "Queue ID of new track"]]);
551 "Play multiple tracks",
552 "Requires the 'play' right.",
553 [["string", "target", "Insert into queue after this track, or at head if \"\""],
554 ["list", "tracks", "List of track names to play"]]);
557 "Retrieve the playing track",
560 [["queue-one", "playing", "Details of the playing track"]]);
562 simple("playlist-delete",
564 "Requires the 'play' right and permission to modify the playlist.",
565 [["string", "playlist", "Playlist to delete"]]);
567 simple("playlist-get",
568 "List the contents of a playlist",
569 "Requires the 'read' right and oermission to read the playlist.",
570 [["string", "playlist", "Playlist name"]],
571 [["body", "tracks", "List of tracks in playlist"]]);
573 simple("playlist-get-share",
574 "Get a playlist's sharing status",
575 "Requires the 'read' right and permission to read the playlist.",
576 [["string", "playlist", "Playlist to read"]],
577 [["string-raw", "share", "Sharing status (\"public\", \"private\" or \"shared\")"]]);
579 simple("playlist-lock",
581 "Requires the 'play' right and permission to modify the playlist. A given connection may lock at most one playlist.",
582 [["string", "playlist", "Playlist to delete"]]);
584 simple("playlist-set",
585 "Set the contents of a playlist",
586 "Requires the 'play' right and permission to modify the playlist, which must be locked.",
587 [["string", "playlist", "Playlist to modify"],
588 ["body", "tracks", "New list of tracks for playlist"]]);
590 simple("playlist-set-share",
591 "Set a playlist's sharing status",
592 "Requires the 'play' right and permission to modify the playlist.",
593 [["string", "playlist", "Playlist to modify"],
594 ["string", "share", "New sharing status (\"public\", \"private\" or \"shared\")"]]);
596 simple("playlist-unlock",
597 "Unlock the locked playlist playlist",
598 "The playlist to unlock is implicit in the connection.",
603 "Requires the 'read' right. Only playlists that you have permission to read are returned.",
605 [["body", "playlists", "Playlist names"]]);
608 "Get all the preferences for a track",
610 [["string", "track", "Track name"]],
611 [["pair-list", "prefs", "Track preferences"]]);
617 [["queue", "queue", "Current queue contents"]]);
619 simple("random-disable",
620 "Disable random play",
621 "Requires the 'global prefs' right.",
624 simple("random-enable",
625 "Enable random play",
626 "Requires the 'global prefs' right.",
629 simple("random-enabled",
630 "Detect whether random play is enabled",
631 "Random play counts as enabled even if play is disabled.",
633 [["boolean", "enabled", "1 if random play is enabled and 0 otherwise"]]);
636 "List recently played tracks",
639 [["queue", "recent", "Recently played tracks"]]);
641 simple("reconfigure",
642 "Re-read configuraiton file.",
643 "Requires the 'admin' right.",
647 "Register a new user",
648 "Requires the 'register' right which is usually only available to the 'guest' user. Redeem the confirmation string via 'confirm' to complete registration.",
649 [["string", "username", "Requested new username"],
650 ["string", "password", "Requested initial password"],
651 ["string", "email", "New user's email address"]],
652 [["string", "confirmation", "Confirmation string"]]);
655 "Send a password reminder.",
656 "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.",
657 [["string", "username", "User to remind"]]);
660 "Remove a track form the queue.",
661 "Requires one of the 'remove mine', 'remove random' or 'remove any' rights depending on how the track came to be added to the queue.",
662 [["string", "id", "Track ID"]]);
665 "Rescan all collections for new or obsolete tracks.",
666 "Requires the 'rescan' right.",
667 []); # TODO wait/fresh flags
670 "Resolve a track name",
671 "Converts aliases to non-alias track names",
672 [["string", "track", "Track name (might be an alias)"]],
673 [["string", "resolved", "Resolve track name (definitely not an alias)"]]);
676 "Resume the currently playing track",
677 "Requires the 'pause' right.",
682 "It will not subsequently be possible to log in with the cookie.",
685 simple("rtp-address",
686 "Get the server's RTP address information",
689 [["string", "address", "Where to store hostname or address"],
690 ["string", "port", "Where to store service name or port number"]]);
693 "Terminate the playing track.",
694 "Requires one of the 'scratch mine', 'scratch random' or 'scratch any' rights depending on how the track came to be added to the queue.",
695 [["string", "id", "Track ID (optional)"]]);
697 simple(["schedule-add", "schedule_add_play"],
698 "Schedule a track to play in the future",
700 [["time", "when", "When to play the track"],
701 ["string", "priority", "Event priority (\"normal\" or \"junk\")"],
702 ["literal", "play", ""],
703 ["string", "track", "Track to play"]]);
705 simple(["schedule-add", "schedule_add_set_global"],
706 "Schedule a global setting to be changed in the future",
708 [["time", "when", "When to change the setting"],
709 ["string", "priority", "Event priority (\"normal\" or \"junk\")"],
710 ["literal", "set-global", ""],
711 ["string", "pref", "Global preference to set"],
712 ["string", "value", "New value of global preference"]]);
714 simple(["schedule-add", "schedule_add_unset_global"],
715 "Schedule a global setting to be unset in the future",
717 [["time", "when", "When to change the setting"],
718 ["string", "priority", "Event priority (\"normal\" or \"junk\")"],
719 ["literal", "set-global", ""],
720 ["string", "pref", "Global preference to set"]]);
722 simple("schedule-del",
723 "Delete a scheduled event.",
724 "Users can always delete their own scheduled events; with the admin right you can delete any event.",
725 [["string", "event", "ID of event to delete"]]);
727 simple("schedule-get",
728 "Get the details of scheduled event",
730 [["string", "id", "Event ID"]],
731 [["pair-list", "actiondata", "Details of event"]]);
733 simple("schedule-list",
734 "List scheduled events",
735 "This just lists IDs. Use 'schedule-get' to retrieve more detail",
737 [["body", "ids", "List of event IDs"]]);
741 "Terms are either keywords or tags formatted as 'tag:TAG-NAME'.",
742 [["string", "terms", "List of search terms"]],
743 [["body", "tracks", "List of matching tracks"]]);
746 "Set a track preference",
747 "Requires the 'prefs' right.",
748 [["string", "track", "Track name"],
749 ["string", "pref", "Preference name"],
750 ["string", "value", "New value"]]);
753 "Set a global preference",
754 "Requires the 'global prefs' right.",
755 [["string", "pref", "Preference name"],
756 ["string", "value", "New value"]]);
759 "Request server shutdown",
760 "Requires the 'admin' right.",
764 "Get server statistics",
765 "The details of what the server reports are not really defined. The returned strings are intended to be printed out one to a line.",
767 [["body", "stats", "List of server information strings."]]);
770 "Get a list of known tags",
771 "Only tags which apply to at least one track are returned.",
773 [["body", "tags", "List of tags"]]);
776 "Unset a track preference",
777 "Requires the 'prefs' right.",
778 [["string", "track", "Track name"],
779 ["string", "pref", "Preference name"]]);
781 simple("unset-global",
782 "Set a global preference",
783 "Requires the 'global prefs' right.",
784 [["string", "pref", "Preference name"]]);
786 # 'user' only used for authentication
789 "Get a user property.",
790 "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.",
791 [["string", "username", "User to read"],
792 ["string", "property", "Property to read"]],
793 [["string", "value", "Value of property"]]);
796 "Get a list of users",
799 [["body", "users", "List of users"]]);
802 "Get the server version",
805 [["string", "version", "Server version string"]]);
807 simple(["volume", "set_volume"],
810 [["integer", "left", "Left channel volume"],
811 ["integer", "right", "Right channel volume"]]);
813 simple(["volume", "get_volume"],
817 [["integer", "left", "Left channel volume"],
818 ["integer", "right", "Right channel volume"]]);
820 # End matter ------------------------------------------------------------------
822 push(@h, "#endif\n");
824 # Write it all out ------------------------------------------------------------
826 Write("lib/client-stubs.h", \@h);
827 Write("lib/client-stubs.c", \@c);