Fix git-version file creation.
[paraslash.git] / command.c
index fc5b5ca..029e289 100644 (file)
--- a/command.c
+++ b/command.c
@@ -1,16 +1,19 @@
 /*
- * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file command.c Client authentication and server commands. */
 
+#include <regex.h>
 #include <signal.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <openssl/rc4.h>
+#include <osl.h>
+#include <stdbool.h>
 
 #include "para.h"
 #include "error.h"
@@ -21,9 +24,9 @@
 #include "afh.h"
 #include "afs.h"
 #include "server.h"
-#include "vss.h"
 #include "list.h"
 #include "send.h"
+#include "vss.h"
 #include "rc4.h"
 #include "net.h"
 #include "daemon.h"
@@ -34,6 +37,7 @@
 #include "afs_command_list.h"
 #include "sched.h"
 #include "signal.h"
+#include "version.h"
 
 /** Commands including options must be shorter than this. */
 #define MAX_COMMAND_LEN 32768
@@ -41,6 +45,9 @@
 extern int mmd_mutex;
 extern struct misc_meta_data *mmd;
 extern struct sender senders[];
+int send_afs_status(struct rc4_context *rc4c, int parser_friendly);
+
+const char *status_item_list[] = {STATUS_ITEM_ARRAY};
 
 static void dummy(__a_unused int s)
 {
@@ -100,14 +107,15 @@ static char *vss_get_status_flags(unsigned int flags)
        return msg;
 }
 
-static char *get_status(struct misc_meta_data *nmmd)
+static char *get_status(struct misc_meta_data *nmmd, int parser_friendly)
 {
-       char *ret, mtime[30] = "";
+       char mtime[30] = "";
        char *status, *flags; /* vss status info */
        char *ut = uptime_str();
        long offset = (nmmd->offset + 500) / 1000;
        struct timeval current_time;
        struct tm mtime_tm;
+       struct para_buffer b = {.flags = parser_friendly? PBF_SIZE_PREFIX : 0};
 
        /* report real status */
        status = vss_status_tohuman(nmmd->vss_status_flags);
@@ -117,38 +125,22 @@ static char *get_status(struct misc_meta_data *nmmd)
                strftime(mtime, 29, "%b %d %Y", &mtime_tm);
        }
        gettimeofday(&current_time, NULL);
-       ret = make_message(
-               "%s: %zu\n" /* file size */
-               "%s: %s\n" /* mtime */
-               "%s: %s\n" /* status */
-               "%s: %s\n" /* status flags */
-               "%s: %li\n" /* offset */
-               "%s: %s\n" /* afs mode */
-               "%s: %lu.%lu\n" /* stream start */
-               "%s: %lu.%lu\n" /* current server time */
-               "%s", /* afs status info */
-               status_item_list[SI_FILE_SIZE], nmmd->size / 1024,
-               status_item_list[SI_MTIME], mtime,
-               status_item_list[SI_STATUS], status,
-               status_item_list[SI_STATUS_FLAGS], flags,
-
-               status_item_list[SI_OFFSET], offset,
-               status_item_list[SI_AFS_MODE], mmd->afs_mode_string,
-
-               status_item_list[SI_STREAM_START],
-                       (long unsigned)nmmd->stream_start.tv_sec,
-                       (long unsigned)nmmd->stream_start.tv_usec,
-               status_item_list[SI_CURRENT_TIME],
-                       (long unsigned)current_time.tv_sec,
-                       (long unsigned)current_time.tv_usec,
-
-               nmmd->afd.verbose_ls_output
-
-       );
+       WRITE_STATUS_ITEM(&b, SI_FILE_SIZE, "%zu\n", nmmd->size / 1024);
+       WRITE_STATUS_ITEM(&b, SI_MTIME, "%s\n", mtime);
+       WRITE_STATUS_ITEM(&b, SI_STATUS, "%s\n", status);
+       WRITE_STATUS_ITEM(&b, SI_STATUS_FLAGS, "%s\n", flags);
+       WRITE_STATUS_ITEM(&b, SI_OFFSET, "%li\n", offset);
+       WRITE_STATUS_ITEM(&b, SI_AFS_MODE, "%s\n", mmd->afs_mode_string);
+       WRITE_STATUS_ITEM(&b, SI_STREAM_START, "%lu.%lu\n",
+               (long unsigned)nmmd->stream_start.tv_sec,
+               (long unsigned)nmmd->stream_start.tv_usec);
+       WRITE_STATUS_ITEM(&b, SI_CURRENT_TIME, "%lu.%lu\n",
+               (long unsigned)current_time.tv_sec,
+               (long unsigned)current_time.tv_usec);
        free(flags);
        free(status);
        free(ut);
-       return ret;
+       return b.buf;
 }
 
 static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
@@ -201,10 +193,10 @@ static int check_sender_args(int argc, char * const * argv, struct sender_comman
 int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
 {
        int i, ret;
+       char *msg = NULL;
        struct sender_command_data scd;
 
        if (argc < 2) {
-               char *msg = NULL;
                for (i = 0; senders[i].name; i++) {
                        char *tmp = make_message("%s%s\n",
                                msg? msg : "", senders[i].name);
@@ -217,7 +209,6 @@ int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
        }
        ret = check_sender_args(argc, argv, &scd);
        if (ret < 0) {
-               char *msg;
                if (scd.sender_num < 0)
                        return ret;
                msg = senders[scd.sender_num].help();
@@ -225,6 +216,16 @@ int com_sender(struct rc4_context *rc4c, int argc, char * const * argv)
                free(msg);
                return ret;
        }
+
+       switch (scd.cmd_num) {
+       case SENDER_ADD:
+       case SENDER_DELETE:
+               assert(senders[scd.sender_num].resolve_target);
+               ret = senders[scd.sender_num].resolve_target(argv[3], &scd);
+               if (ret < 0)
+                       return ret;
+       }
+
        for (i = 0; i < 10; i++) {
                mutex_lock(mmd_mutex);
                if (mmd->sender_cmd_data.cmd_num >= 0) {
@@ -244,7 +245,7 @@ int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
 {
        int i, ret;
        char *ut;
-       char *sender_info = NULL, *sender_list = NULL;
+       char *sender_info = NULL;
 
        if (argc != 1)
                return -E_COMMAND_SYNTAX;
@@ -253,8 +254,6 @@ int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
                char *info = senders[i].info();
                sender_info = para_strcat(sender_info, info);
                free(info);
-               sender_list = para_strcat(sender_list, senders[i].name);
-               sender_list = para_strcat(sender_list, " ");
        }
        ut = uptime_str();
        ret = rc4_send_va_buffer(rc4c, "up: %s\nplayed: %u\n"
@@ -263,7 +262,6 @@ int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
                "connections (active/accepted/total): %u/%u/%u\n"
                "current loglevel: %s\n"
                "supported audio formats: %s\n"
-               "supported senders: %s\n"
                "%s",
                ut, mmd->num_played,
                (int)getppid(),
@@ -272,13 +270,11 @@ int com_si(struct rc4_context *rc4c, int argc, __a_unused char * const * argv)
                mmd->num_commands,
                mmd->num_connects,
                conf.loglevel_arg,
-               supported_audio_formats(),
-               sender_list,
+               SERVER_AUDIO_FORMATS,
                sender_info
        );
        mutex_unlock(mmd_mutex);
        free(ut);
-       free(sender_list);
        free(sender_info);
        return ret;
 }
@@ -294,27 +290,110 @@ int com_version(struct rc4_context *rc4c, int argc, __a_unused char * const * ar
        );
 }
 
+#define EMPTY_STATUS_ITEMS \
+       ITEM(PATH) \
+       ITEM(DIRECTORY) \
+       ITEM(BASENAME) \
+       ITEM(SCORE) \
+       ITEM(ATTRIBUTES_BITMAP) \
+       ITEM(ATTRIBUTES_TXT) \
+       ITEM(HASH) \
+       ITEM(IMAGE_ID) \
+       ITEM(IMAGE_NAME) \
+       ITEM(LYRICS_ID) \
+       ITEM(LYRICS_NAME) \
+       ITEM(BITRATE) \
+       ITEM(FORMAT) \
+       ITEM(FREQUENCY) \
+       ITEM(CHANNELS) \
+       ITEM(DURATION) \
+       ITEM(SECONDS_TOTAL) \
+       ITEM(NUM_PLAYED) \
+       ITEM(LAST_PLAYED) \
+       ITEM(TECHINFO) \
+       ITEM(ARTIST) \
+       ITEM(TITLE) \
+       ITEM(YEAR) \
+       ITEM(ALBUM) \
+       ITEM(COMMENT) \
+       ITEM(AMPLIFICATION)
+
+/**
+ * Write a list of audio-file related status items with empty values.
+ *
+ * This is used by vss when currently no audio file is open.
+ */
+static char *empty_status_items(int parser_friendly)
+{
+       if (parser_friendly)
+               return make_message(
+                       #define ITEM(x) "0004 %02x:\n"
+                       EMPTY_STATUS_ITEMS
+                       #undef ITEM
+                       #define ITEM(x) , SI_ ## x
+                       EMPTY_STATUS_ITEMS
+                       #undef ITEM
+               );
+       return make_message(
+               #define ITEM(x) "%s:\n"
+               EMPTY_STATUS_ITEMS
+               #undef ITEM
+               #define ITEM(x) ,status_item_list[SI_ ## x]
+               EMPTY_STATUS_ITEMS
+               #undef ITEM
+       );
+}
+#undef EMPTY_STATUS_ITEMS
+
 /* stat */
 int com_stat(struct rc4_context *rc4c, int argc, char * const * argv)
 {
-       int ret, num = 0;/* status will be printed that many
-                         * times. num <= 0 means: print forever
-                         */
+       int i, ret;
        struct misc_meta_data tmp, *nmmd = &tmp;
        char *s;
+       int32_t num = 0;
+       int parser_friendly = 0;
 
        para_sigaction(SIGUSR1, dummy);
 
-       if (argc > 1)
-               num = atoi(argv[1]);
+       for (i = 1; i < argc; i++) {
+               const char *arg = argv[i];
+               if (arg[0] != '-')
+                       break;
+               if (!strcmp(arg, "--")) {
+                       i++;
+                       break;
+               }
+               if (!strncmp(arg, "-n=", 3)) {
+                       ret = para_atoi32(arg + 3, &num);
+                       if (ret < 0)
+                               return ret;
+                       continue;
+               }
+               if (!strcmp(arg, "-p")) {
+                       parser_friendly = 1;
+                       continue;
+               }
+               return -E_COMMAND_SYNTAX;
+       }
+       if (i != argc)
+               return -E_COMMAND_SYNTAX;
        for (;;) {
-
                mmd_dup(nmmd);
-               s = get_status(nmmd);
+               s = get_status(nmmd, parser_friendly);
                ret = rc4_send_buffer(rc4c, s);
                free(s);
                if (ret < 0)
                        goto out;
+               if (nmmd->vss_status_flags & VSS_NEXT) {
+                       static char *esi;
+                       if (!esi)
+                               esi = empty_status_items(parser_friendly);
+                       ret = rc4_send_buffer(rc4c, esi);
+                       if (ret < 0)
+                               goto out;
+               } else
+                       send_afs_status(rc4c, parser_friendly);
                ret = 1;
                if (num > 0 && !--num)
                        goto out;
@@ -380,10 +459,8 @@ int com_help(struct rc4_context *rc4c, int argc, char * const * argv)
        }
        /* argument given for help */
        cmd = get_cmd_ptr(argv[1], &handler);
-       if (!cmd) {
-               free(handler);
+       if (!cmd)
                return -E_BAD_CMD;
-       }
        perms = cmd_perms_itohuman(cmd->perms);
        ret = rc4_send_va_buffer(rc4c,
                "%s - %s\n\n"
@@ -667,24 +744,24 @@ __noreturn void handle_connect(int fd, const char *peername)
        /* we need a blocking fd here as recv() might return EAGAIN otherwise. */
        ret = mark_fd_blocking(fd);
        if (ret < 0)
-               goto err_out;
+               goto net_err;
        /* send Welcome message */
        ret = send_va_buffer(fd, "This is para_server, version "
                PACKAGE_VERSION  ".\n" );
        if (ret < 0)
-               goto err_out;
+               goto net_err;
        /* recv auth request line */
        ret = recv_buffer(fd, buf, sizeof(buf));
        if (ret < 0)
-               goto err_out;
+               goto net_err;
        if (ret < 10) {
                ret = -E_AUTH_REQUEST;
-               goto err_out;
+               goto net_err;
        }
        numbytes = ret;
        ret = -E_AUTH_REQUEST;
        if (strncmp(buf, AUTH_REQUEST_MSG, strlen(AUTH_REQUEST_MSG)))
-               goto err_out;
+               goto net_err;
        p = buf + strlen(AUTH_REQUEST_MSG);
        PARA_DEBUG_LOG("received auth request for user %s\n", p);
        ret = -E_BAD_USER;
@@ -694,7 +771,7 @@ __noreturn void handle_connect(int fd, const char *peername)
                ret = para_encrypt_buffer(u->rsa, rand_buf, sizeof(rand_buf),
                        (unsigned char *)buf);
                if (ret < 0)
-                       goto err_out;
+                       goto net_err;
                numbytes = ret;
        } else {
                /*
@@ -705,7 +782,7 @@ __noreturn void handle_connect(int fd, const char *peername)
                numbytes = 256;
                get_random_bytes_or_die((unsigned char *)buf, numbytes);
        }
-       PARA_DEBUG_LOG("sending %zu byte challenge + rc4 keys (%u bytes)\n",
+       PARA_DEBUG_LOG("sending %u byte challenge + rc4 keys (%zu bytes)\n",
                CHALLENGE_SIZE, numbytes);
        ret = send_bin_buffer(fd, buf, numbytes);
        if (ret < 0)
@@ -715,7 +792,7 @@ __noreturn void handle_connect(int fd, const char *peername)
        if (ret < 0)
                goto net_err;
        numbytes = ret;
-       PARA_DEBUG_LOG("received %zu bytes challenge response\n", ret);
+       PARA_DEBUG_LOG("received %d bytes challenge response\n", ret);
        ret = -E_BAD_USER;
        if (!u)
                goto net_err;
@@ -753,10 +830,14 @@ __noreturn void handle_connect(int fd, const char *peername)
        if (ret < 0)
                goto err_out;
        /* valid command and sufficient perms */
-       argc = split_args(command, &argv, "\n");
+       ret = create_argv(command, "\n", &argv);
+       if (ret < 0)
+               goto err_out;
+       argc = ret;
        PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
                        peername);
        ret = cmd->handler(&rc4c, argc, argv);
+       free_argv(argv);
        mutex_lock(mmd_mutex);
        mmd->num_commands++;
        mutex_unlock(mmd_mutex);
@@ -768,7 +849,6 @@ net_err:
        PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
 out:
        free(command);
-       free(argv);
        mutex_lock(mmd_mutex);
        if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
                mmd->events++;