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