chiark / gitweb /
doxygen: add some missing docstrings.
[disorder] / clients / disorder.c
index b07ffa1f2b66edc655d1cf4413ce98b34a7bec71..6d0a29d971b7290f96ae1887662f10a0ff6b75b7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004-2008 Richard Kettlewell
+ * Copyright (C) 2004-2009 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -15,6 +15,9 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+/** @file clients/disorder.c
+ * @brief Command-line client
+ */
 
 #include "common.h"
 
@@ -29,6 +32,8 @@
 #include <unistd.h>
 #include <pcre.h>
 #include <ctype.h>
+#include <gcrypt.h>
+#include <langinfo.h>
 
 #include "configuration.h"
 #include "syscalls.h"
@@ -47,6 +52,8 @@
 #include "vector.h"
 #include "version.h"
 #include "dateparse.h"
+#include "trackdb.h"
+#include "inputline.h"
 
 static disorder_client *client;
 
@@ -60,6 +67,7 @@ static const struct option options[] = {
   { "help-commands", no_argument, 0, 'H' },
   { "user", required_argument, 0, 'u' },
   { "password", required_argument, 0, 'p' },
+  { "wait-for-root", no_argument, 0, 'W' },
   { 0, 0, 0, 0 }
 };
 
@@ -90,14 +98,25 @@ static void cf_version(char attribute((unused)) **argv) {
   char *v;
 
   if(disorder_version(getclient(), &v)) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(v)));
+  v = nullcheck(utf82mb_f(v));
+  xprintf("%s\n", v);
+  xfree(v);
 }
 
 static void print_queue_entry(const struct queue_entry *q) {
   if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
   if(q->id) xprintf("  id %s\n", nullcheck(utf82mb(q->id)));
-  if(q->submitter) xprintf("  submitted by %s at %s",
-                          nullcheck(utf82mb(q->submitter)), ctime(&q->when));
+  switch(q->origin) {
+  case origin_adopted:
+  case origin_picked:
+  case origin_scheduled:
+    xprintf("  %s by %s at %s",
+            track_origins[q->origin],
+            nullcheck(utf82mb(q->submitter)), ctime(&q->when));
+    break;
+  default:
+    break;
+  }
   if(q->played) xprintf("  played at %s", ctime(&q->played));
   if(q->state == playing_started
      || q->state == playing_paused) xprintf("  %lds so far",  q->sofar);
@@ -145,7 +164,8 @@ static void cf_shutdown(char attribute((unused)) **argv) {
 
 static void cf_reconfigure(char attribute((unused)) **argv) {
   /* Re-check configuration for server */
-  if(config_read(1)) fatal(0, "cannot read configuration");
+  if(config_read(1, NULL))
+    disorder_fatal(0, "cannot read configuration");
   if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE);
 }
 
@@ -155,13 +175,12 @@ static void cf_rescan(char attribute((unused)) **argv) {
 
 static void cf_somequeue(int (*fn)(disorder_client *c,
                                   struct queue_entry **qp)) {
-  struct queue_entry *q;
+  struct queue_entry *q, *qbase;
 
-  if(fn(getclient(), &q)) exit(EXIT_FAILURE);
-  while(q) {
+  if(fn(getclient(), &qbase)) exit(EXIT_FAILURE);
+  for(q = qbase; q; q = q->next)
     print_queue_entry(q);
-    q = q->next;
-  }
+  queue_free(qbase, 1);
 }
 
 static void cf_recent(char attribute((unused)) **argv) {
@@ -173,31 +192,51 @@ static void cf_queue(char attribute((unused)) **argv) {
 }
 
 static void cf_quack(char attribute((unused)) **argv) {
-  xprintf("\n"
-         " .------------------.\n"
-         " | Naath is a babe! |\n"
-         " `---------+--------'\n"
-         "            \\\n"
-         "              >0\n"
-         "               (<)'\n"
-         "~~~~~~~~~~~~~~~~~~~~~~\n"
-         "\n");
+  if(!strcasecmp(nl_langinfo(CODESET), "utf-8")) {
+#define TL "\xE2\x95\xAD"
+#define TR "\xE2\x95\xAE"
+#define BR "\xE2\x95\xAF"
+#define BL "\xE2\x95\xB0"
+#define H "\xE2\x94\x80"
+#define V "\xE2\x94\x82"
+#define T "\xE2\x94\xAC"
+    xprintf("\n"
+            " "TL H H H H H H H H H H H H H H H H H H TR"\n"
+            " "V" Naath is a babe! "V"\n"
+            " "BL H H H H H H H H H T H H H H H H H H BR"\n"
+            "            \\\n"
+            "              >0\n"
+            "               (<)'\n"
+            "~~~~~~~~~~~~~~~~~~~~~~\n"
+            "\n");
+  } else {
+    xprintf("\n"
+            " .------------------.\n"
+            " | Naath is a babe! |\n"
+            " `---------+--------'\n"
+            "            \\\n"
+            "              >0\n"
+            "               (<)'\n"
+            "~~~~~~~~~~~~~~~~~~~~~~\n"
+            "\n");
+  }
 }
 
 static void cf_somelist(char **argv,
                        int (*fn)(disorder_client *c,
                                  const char *arg, const char *re,
                                  char ***vecp, int *nvecp)) {
-  char **vec;
+  char **vec, **base;
   const char *re;
 
   if(argv[1])
     re = xstrdup(argv[1] + 1);
   else
     re = 0;
-  if(fn(getclient(), argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
-  while(*vec)
-    xprintf("%s\n", nullcheck(utf82mb(*vec++)));
+  if(fn(getclient(), argv[0], re, &base, 0)) exit(EXIT_FAILURE);
+  for(vec = base; *vec; ++vec)
+    xprintf("%s\n", nullcheck(utf82mb_f(*vec)));
+  xfree(base);
 }
 
 static int isarg_regexp(const char *s) {
@@ -220,7 +259,7 @@ static void cf_get(char **argv) {
   char *value;
 
   if(disorder_get(getclient(), argv[0], argv[1], &value)) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(value)));
+  xprintf("%s\n", nullcheck(utf82mb_f(value)));
 }
 
 static void cf_length(char **argv) {
@@ -239,12 +278,13 @@ static void cf_unset(char **argv) {
 }
 
 static void cf_prefs(char **argv) {
-  struct kvp *k;
+  struct kvp *k, *base;
 
-  if(disorder_prefs(getclient(), argv[0], &k)) exit(EXIT_FAILURE);
-  for(; k; k = k->next)
+  if(disorder_prefs(getclient(), argv[0], &base)) exit(EXIT_FAILURE);
+  for(k = base; k; k = k->next)
     xprintf("%s = %s\n",
            nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
+  kvp_free(base);
 }
 
 static void cf_search(char **argv) {
@@ -254,6 +294,7 @@ static void cf_search(char **argv) {
   if(disorder_search(getclient(), *argv, &results, &nresults)) exit(EXIT_FAILURE);
   for(n = 0; n < nresults; ++n)
     xprintf("%s\n", nullcheck(utf82mb(results[n])));
+  free_strings(nresults, results);
 }
 
 static void cf_random_disable(char attribute((unused)) **argv) {
@@ -266,10 +307,12 @@ static void cf_random_enable(char attribute((unused)) **argv) {
 
 static void cf_stats(char attribute((unused)) **argv) {
   char **vec;
+  int nvec;
 
-  if(disorder_stats(getclient(), &vec, 0)) exit(EXIT_FAILURE);
-  while(*vec)
-      xprintf("%s\n", nullcheck(utf82mb(*vec++)));
+  if(disorder_stats(getclient(), &vec, &nvec)) exit(EXIT_FAILURE);
+  for(int n = 0; n < nvec; ++n)
+    xprintf("%s\n", nullcheck(utf82mb(vec[n])));
+  free_strings(nvec, vec);
 }
 
 static void cf_get_volume(char attribute((unused)) **argv) {
@@ -292,9 +335,9 @@ static void cf_move(char **argv) {
   int e;
   
   if((e = xstrtol(&n, argv[1], 0, 10)))
-    fatal(e, "cannot convert '%s'", argv[1]);
+    disorder_fatal(e, "cannot convert '%s'", argv[1]);
   if(n > INT_MAX || n < INT_MIN)
-    fatal(e, "%ld out of range", n);
+    disorder_fatal(e, "%ld out of range", n);
   if(disorder_move(getclient(), argv[0], (int)n)) exit(EXIT_FAILURE);
 }
 
@@ -302,7 +345,7 @@ static void cf_part(char **argv) {
   char *s;
 
   if(disorder_part(getclient(), &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(s)));
+  xprintf("%s\n", nullcheck(utf82mb_f(s)));
 }
 
 static int isarg_filename(const char *s) {
@@ -317,7 +360,7 @@ static void cf_resolve(char **argv) {
   char *track;
 
   if(disorder_resolve(getclient(), &track, argv[0])) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(track)));
+  xprintf("%s\n", nullcheck(utf82mb_f(track)));
 }
 
 static void cf_pause(char attribute((unused)) **argv) {
@@ -348,7 +391,7 @@ static void cf_get_global(char **argv) {
   char *value;
 
   if(disorder_get_global(getclient(), argv[0], &value)) exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(value)));
+  xprintf("%s\n", nullcheck(utf82mb_f(value)));
 }
 
 static void cf_set_global(char **argv) {
@@ -409,7 +452,7 @@ static void cf_userinfo(char **argv) {
 
   if(disorder_userinfo(getclient(), argv[0], argv[1], &s))
     exit(EXIT_FAILURE);
-  xprintf("%s\n", nullcheck(utf82mb(s)));
+  xprintf("%s\n", nullcheck(utf82mb_f(s)));
 }
 
 static int isarg_option(const char *arg) {
@@ -451,11 +494,11 @@ static void cf_setup_guest(char **argv) {
     case 'h': help_setup_guest();
     case 'r': online_registration = 1; break;
     case 'R': online_registration = 0; break;
-    default: fatal(0, "invalid option");
+    default: disorder_fatal(0, "invalid option");
     }
   }
   if(online_registration && !config->mail_sender)
-    fatal(0, "you MUST set mail_sender if you want online registration");
+    disorder_fatal(0, "you MUST set mail_sender if you want online registration");
   if(disorder_adduser(getclient(), "guest", "",
                      online_registration ? "read,register" : "read"))
     exit(EXIT_FAILURE);
@@ -565,15 +608,97 @@ static void cf_schedule_unset_global(char **argv) {
     exit(EXIT_FAILURE);
 }
 
-static const struct command {
+static void cf_adopt(char **argv) {
+  if(disorder_adopt(getclient(), argv[0]))
+    exit(EXIT_FAILURE);
+}
+
+static void cf_playlists(char attribute((unused)) **argv) {
+  char **vec;
+  int nvec;
+
+  if(disorder_playlists(getclient(), &vec, &nvec))
+    exit(EXIT_FAILURE);
+  for(int n = 0; n < nvec; ++n)
+    xprintf("%s\n", nullcheck(utf82mb(vec[n])));
+  free_strings(nvec, vec);
+}
+
+static void cf_playlist_del(char **argv) {
+  if(disorder_playlist_delete(getclient(), argv[0]))
+    exit(EXIT_FAILURE);
+}
+
+static void cf_playlist_get(char **argv) {
+  char **vec;
+  int nvec;
+
+  if(disorder_playlist_get(getclient(), argv[0], &vec, &nvec))
+    exit(EXIT_FAILURE);
+  for(int n = 0; n < nvec; ++n)
+    xprintf("%s\n", nullcheck(utf82mb(vec[n])));
+  free_strings(nvec, vec);
+}
+
+static void cf_playlist_set(char **argv) {
+  struct vector v[1];
+  FILE *input;
+  const char *tag;
+  char *l;
+
+  if(argv[1]) {
+    // Read track list from file
+    if(!(input = fopen(argv[1], "r")))
+      disorder_fatal(errno, "opening %s", argv[1]);
+    tag = argv[1];
+  } else {
+    // Read track list from standard input
+    input = stdin;
+    tag = "stdin";
+  }
+  vector_init(v);
+  while(!inputline(tag, input, &l, '\n')) {
+    if(!strcmp(l, "."))
+      break;
+    vector_append(v, l);
+  }
+  if(ferror(input))
+    disorder_fatal(errno, "reading %s", tag);
+  if(input != stdin)
+    fclose(input);
+  if(disorder_playlist_lock(getclient(), argv[0])
+     || disorder_playlist_set(getclient(), argv[0], v->vec, v->nvec)
+     || disorder_playlist_unlock(getclient()))
+    exit(EXIT_FAILURE);
+}
+
+/** @brief Command-line client's definition of a command */
+static const struct client_command {
+  /** @brief Command name */
   const char *name;
-  int min, max;
+
+  /** @brief Minimum number of argument */
+  int min;
+
+  /** @brief Maximum number of argument */
+  int max;
+
+  /** @brief Pointer to function implementing command */
   void (*fn)(char **);
+
+  /** @brief Function to recognize a valid argument, or NULL */
   int (*isarg)(const char *);
-  const char *argstr, *desc;
+
+  /** @brief Summary of arguments */
+  const char *argstr;
+
+  /** @brief Description */
+  const char *desc;
 } commands[] = {
   { "adduser",        2, 3, cf_adduser, isarg_rights, "USERNAME PASSWORD [RIGHTS]",
                       "Create a new user" },
+  { "adopt",          1, 1, cf_adopt, 0, "ID",
+                      "Adopt a randomly picked track" },
   { "allfiles",       1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
                       "List all files and directories in DIR" },
   { "authorize",      1, 2, cf_authorize, isarg_rights, "USERNAME [RIGHTS]",
@@ -616,6 +741,14 @@ static const struct command {
                       "Add TRACKS to the end of the queue" },
   { "playing",        0, 0, cf_playing, 0, "",
                       "Report the playing track" },
+  { "playlist-del",   1, 1, cf_playlist_del, 0, "PLAYLIST",
+                      "Delete a playlist" },
+  { "playlist-get",   1, 1, cf_playlist_get, 0, "PLAYLIST",
+                      "Get the contents of a playlist" },
+  { "playlist-set",   1, 2, cf_playlist_set, isarg_filename, "PLAYLIST [PATH]",
+                      "Set the contents of a playlist" },
+  { "playlists",      0, 0, cf_playlists, 0, "",
+                      "List playlists" },
   { "prefs",          1, 1, cf_prefs, 0, "TRACK",
                       "Display all the preferences for TRACK" },
   { "quack",          0, 0, cf_quack, 0, 0, 0 },
@@ -708,8 +841,28 @@ static void help_commands(void) {
   exit(0);
 }
 
+static void wait_for_root(void) {
+  const char *password;
+
+  while(!trackdb_readable()) {
+    disorder_info("waiting for trackdb...");
+    sleep(1);
+  }
+  trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
+  for(;;) {
+    trackdb_open(TRACKDB_READ_ONLY);
+    password = trackdb_get_password("root");
+    trackdb_close();
+    if(password)
+      break;
+    disorder_info("waiting for root user to be created...");
+    sleep(1);
+  }
+  trackdb_deinit(NULL);
+}
+
 int main(int argc, char **argv) {
-  int n, i, j, local = 0;
+  int n, i, j, local = 0, wfr = 0;
   int status = 0;
   struct vector args;
   const char *user = 0, *password = 0;
@@ -718,9 +871,9 @@ int main(int argc, char **argv) {
   /* garbage-collect PCRE's memory */
   pcre_malloc = xmalloc;
   pcre_free = xfree;
-  if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
-  if(!setlocale(LC_TIME, "")) fatal(errno, "error calling setlocale");
-  while((n = getopt_long(argc, argv, "+hVc:dHlNu:p:", options, 0)) >= 0) {
+  if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale");
+  if(!setlocale(LC_TIME, "")) disorder_fatal(errno, "error calling setlocale");
+  while((n = getopt_long(argc, argv, "+hVc:dHlNu:p:W", options, 0)) >= 0) {
     switch(n) {
     case 'h': help();
     case 'H': help_commands();
@@ -731,30 +884,41 @@ int main(int argc, char **argv) {
     case 'N': config_per_user = 0; break;
     case 'u': user = optarg; break;
     case 'p': password = optarg; break;
-    default: fatal(0, "invalid option");
+    case 'W': wfr = 1; break;
+    default: disorder_fatal(0, "invalid option");
     }
   }
-  if(config_read(0)) fatal(0, "cannot read configuration");
+  if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
   if(user) {
-    config->username = user;
+    xfree(config->username);
+    config->username = xstrdup(user);
     config->password = 0;
   }
-  if(password)
-    config->password = password;
+  if(password) {
+    xfree(config->password);
+    config->password = xstrdup(password);
+  }
   if(local)
-    config->connect.n = 0;
+    config->connect.af = -1;
+  if(wfr)
+    wait_for_root();
   n = optind;
   optind = 1;                          /* for subsequent getopt calls */
+  /* gcrypt initialization */
+  if(!gcry_check_version(NULL))
+    disorder_fatal(0, "gcry_check_version failed");
+  gcry_control(GCRYCTL_INIT_SECMEM, 0);
+  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
   /* accumulate command args */
   while(n < argc) {
     if((i = TABLE_FIND(commands, name, argv[n])) < 0)
-      fatal(0, "unknown command '%s'", argv[n]);
+      disorder_fatal(0, "unknown command '%s'", argv[n]);
     if(n + commands[i].min >= argc)
-      fatal(0, "missing arguments to '%s'", argv[n]);
+      disorder_fatal(0, "missing arguments to '%s'", argv[n]);
     vector_init(&args);
     /* Include the command name in the args, but at element -1, for
      * the benefit of subcommand getopt calls */
-    vector_append(&args, argv[n]);
+    vector_append(&args, xstrdup(argv[n]));
     n++;
     for(j = 0; j < commands[i].min; ++j)
       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
@@ -764,10 +928,13 @@ int main(int argc, char **argv) {
       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
     vector_terminate(&args);
     commands[i].fn(args.vec + 1);
+    vector_clear(&args);
     n += j;
   }
   if(client && disorder_close(client)) exit(EXIT_FAILURE);
-  if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
+  if(fclose(stdout) < 0) disorder_fatal(errno, "error closing stdout");
+  config_free(config);
+  xfree(client);
   return status;
 }