2014-01-17 18:41:41 +00:00
|
|
|
/* ympd
|
2014-02-22 00:57:26 +00:00
|
|
|
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
|
2014-01-17 18:41:41 +00:00
|
|
|
This project's homepage is: http://www.ympd.org
|
2014-02-22 00:57:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
2014-01-17 18:41:41 +00:00
|
|
|
|
2014-02-22 00:57:26 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-01-17 18:41:41 +00:00
|
|
|
|
2014-02-22 00:57:26 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2014-01-17 18:41:41 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-04 17:18:38 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2013-11-09 01:07:03 +00:00
|
|
|
#include <unistd.h>
|
2013-11-11 14:53:54 +00:00
|
|
|
#include <getopt.h>
|
2013-11-05 13:59:12 +00:00
|
|
|
#include <sys/time.h>
|
2014-02-16 18:46:53 +00:00
|
|
|
#include <pthread.h>
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
#include "mongoose.h"
|
2013-11-04 17:18:38 +00:00
|
|
|
#include "http_server.h"
|
|
|
|
#include "mpd_client.h"
|
2014-01-08 01:23:02 +00:00
|
|
|
#include "config.h"
|
2013-11-04 17:18:38 +00:00
|
|
|
|
2013-11-09 01:07:03 +00:00
|
|
|
extern char *optarg;
|
2013-11-04 17:18:38 +00:00
|
|
|
|
|
|
|
int force_exit = 0;
|
|
|
|
|
|
|
|
void bye()
|
|
|
|
{
|
2013-11-09 01:07:03 +00:00
|
|
|
force_exit = 1;
|
2013-11-04 17:18:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-19 17:52:23 +00:00
|
|
|
static int server_callback(struct mg_connection *c, enum mg_event ev) {
|
|
|
|
switch(ev) {
|
|
|
|
case MG_CLOSE:
|
|
|
|
mpd_close_handler(c);
|
|
|
|
return MG_TRUE;
|
|
|
|
case MG_REQUEST:
|
|
|
|
if (c->is_websocket) {
|
|
|
|
c->content[c->content_len] = '\0';
|
|
|
|
if(c->content_len)
|
|
|
|
return callback_mpd(c);
|
|
|
|
else
|
|
|
|
return MG_TRUE;
|
|
|
|
} else
|
2014-11-11 20:13:11 +00:00
|
|
|
#ifdef WITH_DYNAMIC_ASSETS
|
2014-10-27 21:12:10 +00:00
|
|
|
return MG_FALSE;
|
|
|
|
#else
|
2014-10-19 17:52:23 +00:00
|
|
|
return callback_http(c);
|
2014-10-27 21:12:10 +00:00
|
|
|
#endif
|
2014-10-19 17:52:23 +00:00
|
|
|
case MG_AUTH:
|
|
|
|
return MG_TRUE;
|
|
|
|
default:
|
|
|
|
return MG_FALSE;
|
2014-02-16 18:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 17:18:38 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2014-02-16 18:46:53 +00:00
|
|
|
int n, option_index = 0;
|
2014-10-19 17:52:23 +00:00
|
|
|
struct mg_server *server = mg_create_server(NULL, server_callback);
|
2014-02-16 18:46:53 +00:00
|
|
|
unsigned int current_timer = 0, last_timer = 0;
|
2014-04-23 10:17:55 +00:00
|
|
|
char *run_as_user = NULL;
|
2014-10-19 17:52:23 +00:00
|
|
|
char const *error_msg = NULL;
|
2015-05-01 19:05:15 +00:00
|
|
|
char *webport = "8080";
|
2013-11-09 01:07:03 +00:00
|
|
|
|
|
|
|
atexit(bye);
|
2014-11-11 20:13:11 +00:00
|
|
|
#ifdef WITH_DYNAMIC_ASSETS
|
2014-10-27 21:12:10 +00:00
|
|
|
mg_set_option(server, "document_root", SRC_PATH);
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.port = 6600;
|
|
|
|
strcpy(mpd.host, "127.0.0.1");
|
2013-11-11 14:53:54 +00:00
|
|
|
|
2016-09-11 15:54:25 +00:00
|
|
|
strcpy(dirble_api_token, "2e223c9909593b94fc6577361a");
|
|
|
|
|
2013-11-11 14:53:54 +00:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"host", required_argument, 0, 'h'},
|
|
|
|
{"port", required_argument, 0, 'p'},
|
|
|
|
{"webport", required_argument, 0, 'w'},
|
2016-09-11 15:54:25 +00:00
|
|
|
{"dirbletoken", required_argument, 0, 'd'},
|
2014-02-16 18:46:53 +00:00
|
|
|
{"user", required_argument, 0, 'u'},
|
2014-02-22 01:11:45 +00:00
|
|
|
{"version", no_argument, 0, 'v'},
|
2013-11-11 14:53:54 +00:00
|
|
|
{"help", no_argument, 0, 0 },
|
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
2013-11-09 01:07:03 +00:00
|
|
|
|
2014-02-22 01:11:45 +00:00
|
|
|
while((n = getopt_long(argc, argv, "h:p:w:u:v",
|
2013-11-11 14:53:54 +00:00
|
|
|
long_options, &option_index)) != -1) {
|
2013-11-09 01:07:03 +00:00
|
|
|
switch (n) {
|
2013-11-11 14:53:54 +00:00
|
|
|
case 'h':
|
2014-02-16 18:46:53 +00:00
|
|
|
strncpy(mpd.host, optarg, sizeof(mpd.host));
|
2013-11-11 14:53:54 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd.port = atoi(optarg);
|
2014-10-29 02:48:08 +00:00
|
|
|
break;
|
2013-11-11 14:53:54 +00:00
|
|
|
case 'w':
|
2015-05-01 19:05:15 +00:00
|
|
|
webport = strdup(optarg);
|
2013-11-09 01:07:03 +00:00
|
|
|
break;
|
2016-09-11 15:54:25 +00:00
|
|
|
case 'd':
|
|
|
|
strncpy(dirble_api_token, optarg, sizeof(dirble_api_token));
|
|
|
|
break;
|
2013-11-09 01:07:03 +00:00
|
|
|
case 'u':
|
2014-04-23 10:17:55 +00:00
|
|
|
run_as_user = strdup(optarg);
|
2013-11-11 14:53:54 +00:00
|
|
|
break;
|
2014-02-22 01:11:45 +00:00
|
|
|
case 'v':
|
2014-01-08 01:23:02 +00:00
|
|
|
fprintf(stdout, "ympd %d.%d.%d\n"
|
2014-02-16 18:46:53 +00:00
|
|
|
"Copyright (C) 2014 Andrew Karpow <andy@ndyk.de>\n"
|
2014-01-08 01:23:02 +00:00
|
|
|
"built " __DATE__ " "__TIME__ " ("__VERSION__")\n",
|
|
|
|
YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
break;
|
2013-11-11 14:53:54 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Usage: %s [OPTION]...\n\n"
|
2014-02-22 01:11:45 +00:00
|
|
|
" -h, --host <host>\t\tconnect to mpd at host [localhost]\n"
|
|
|
|
" -p, --port <port>\t\tconnect to mpd at port [6600]\n"
|
|
|
|
" -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n"
|
|
|
|
" -u, --user <username>\t\tdrop priviliges to user after socket bind\n"
|
2016-09-11 15:54:25 +00:00
|
|
|
" -d, --dirbletoken <apitoken>\tDirble API token\n"
|
2014-02-22 01:11:45 +00:00
|
|
|
" -V, --version\t\t\tget version\n"
|
|
|
|
" --help\t\t\t\tthis help\n"
|
2013-11-09 01:07:03 +00:00
|
|
|
, argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-10-19 17:52:23 +00:00
|
|
|
|
|
|
|
if(error_msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mongoose error: %s\n", error_msg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-11-09 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 19:05:15 +00:00
|
|
|
error_msg = mg_set_option(server, "listening_port", webport);
|
|
|
|
if(error_msg) {
|
|
|
|
fprintf(stderr, "Mongoose error: %s\n", error_msg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-23 10:17:55 +00:00
|
|
|
/* drop privilges at last to ensure proper port binding */
|
2015-05-01 19:05:15 +00:00
|
|
|
if(run_as_user != NULL) {
|
2014-10-19 17:52:23 +00:00
|
|
|
error_msg = mg_set_option(server, "run_as_user", run_as_user);
|
2014-04-23 10:17:55 +00:00
|
|
|
free(run_as_user);
|
2014-10-19 17:52:23 +00:00
|
|
|
if(error_msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Mongoose error: %s\n", error_msg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-04-23 10:17:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
while (!force_exit) {
|
2014-10-19 17:52:23 +00:00
|
|
|
mg_poll_server(server, 200);
|
|
|
|
current_timer = time(NULL);
|
2014-02-16 18:46:53 +00:00
|
|
|
if(current_timer - last_timer)
|
|
|
|
{
|
|
|
|
last_timer = current_timer;
|
|
|
|
mpd_poll(server);
|
2013-11-09 01:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 18:46:53 +00:00
|
|
|
mpd_disconnect();
|
|
|
|
mg_destroy_server(&server);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2013-11-05 13:59:12 +00:00
|
|
|
}
|