ympd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ympd
  2. (c) 2013-2014 Andrew Karpow <andy@ndyk.de>
  3. This project's homepage is: http://www.ympd.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <getopt.h>
  20. #include <sys/time.h>
  21. #include <pthread.h>
  22. #include "mongoose.h"
  23. #include "http_server.h"
  24. #include "mpd_client.h"
  25. #include "config.h"
  26. extern char *optarg;
  27. int force_exit = 0;
  28. void bye()
  29. {
  30. force_exit = 1;
  31. }
  32. static int server_callback(struct mg_connection *c, enum mg_event ev) {
  33. switch(ev) {
  34. case MG_CLOSE:
  35. mpd_close_handler(c);
  36. return MG_TRUE;
  37. case MG_REQUEST:
  38. if (c->is_websocket) {
  39. c->content[c->content_len] = '\0';
  40. if(c->content_len)
  41. return callback_mpd(c);
  42. else
  43. return MG_TRUE;
  44. } else
  45. #ifdef WITH_DYNAMIC_ASSETS
  46. return MG_FALSE;
  47. #else
  48. return callback_http(c);
  49. #endif
  50. case MG_AUTH:
  51. return MG_TRUE;
  52. default:
  53. return MG_FALSE;
  54. }
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. int n, option_index = 0;
  59. struct mg_server *server = mg_create_server(NULL, server_callback);
  60. unsigned int current_timer = 0, last_timer = 0;
  61. char *run_as_user = NULL;
  62. char const *error_msg = NULL;
  63. char *webport = "8080";
  64. atexit(bye);
  65. #ifdef WITH_DYNAMIC_ASSETS
  66. mg_set_option(server, "document_root", SRC_PATH);
  67. #endif
  68. mpd.port = 6600;
  69. strcpy(mpd.host, "127.0.0.1");
  70. static struct option long_options[] = {
  71. {"host", required_argument, 0, 'h'},
  72. {"port", required_argument, 0, 'p'},
  73. {"webport", required_argument, 0, 'w'},
  74. {"dirbletoken", required_argument, 0, 'd'},
  75. {"user", required_argument, 0, 'u'},
  76. {"version", no_argument, 0, 'v'},
  77. {"help", no_argument, 0, 0 },
  78. {"mpdpass", required_argument, 0, 'm'},
  79. {0, 0, 0, 0 }
  80. };
  81. while((n = getopt_long(argc, argv, "h:p:w:u:vm:",
  82. long_options, &option_index)) != -1) {
  83. switch (n) {
  84. case 'h':
  85. strncpy(mpd.host, optarg, sizeof(mpd.host));
  86. break;
  87. case 'p':
  88. mpd.port = atoi(optarg);
  89. break;
  90. case 'w':
  91. webport = strdup(optarg);
  92. break;
  93. case 'd':
  94. strncpy(dirble_api_token, optarg, sizeof(dirble_api_token));
  95. break;
  96. case 'u':
  97. run_as_user = strdup(optarg);
  98. break;
  99. case 'm':
  100. if (strlen(optarg) > 0)
  101. mpd.password = strdup(optarg);
  102. break;
  103. case 'v':
  104. fprintf(stdout, "ympd %d.%d.%d\n"
  105. "Copyright (C) 2014 Andrew Karpow <andy@ndyk.de>\n"
  106. "built " __DATE__ " "__TIME__ " ("__VERSION__")\n",
  107. YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH);
  108. return EXIT_SUCCESS;
  109. break;
  110. default:
  111. fprintf(stderr, "Usage: %s [OPTION]...\n\n"
  112. " -h, --host <host>\t\tconnect to mpd at host [localhost]\n"
  113. " -p, --port <port>\t\tconnect to mpd at port [6600]\n"
  114. " -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n"
  115. " -u, --user <username>\t\tdrop priviliges to user after socket bind\n"
  116. " -d, --dirbletoken <apitoken>\tDirble API token\n"
  117. " -V, --version\t\t\tget version\n"
  118. " -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n"
  119. " --help\t\t\t\tthis help\n"
  120. , argv[0]);
  121. return EXIT_FAILURE;
  122. }
  123. if(error_msg)
  124. {
  125. fprintf(stderr, "Mongoose error: %s\n", error_msg);
  126. return EXIT_FAILURE;
  127. }
  128. }
  129. error_msg = mg_set_option(server, "listening_port", webport);
  130. if(error_msg) {
  131. fprintf(stderr, "Mongoose error: %s\n", error_msg);
  132. return EXIT_FAILURE;
  133. }
  134. /* drop privilges at last to ensure proper port binding */
  135. if(run_as_user != NULL) {
  136. error_msg = mg_set_option(server, "run_as_user", run_as_user);
  137. free(run_as_user);
  138. if(error_msg)
  139. {
  140. fprintf(stderr, "Mongoose error: %s\n", error_msg);
  141. return EXIT_FAILURE;
  142. }
  143. }
  144. while (!force_exit) {
  145. mg_poll_server(server, 200);
  146. current_timer = time(NULL);
  147. if(current_timer - last_timer)
  148. {
  149. last_timer = current_timer;
  150. mpd_poll(server);
  151. }
  152. }
  153. mpd_disconnect();
  154. mg_destroy_server(&server);
  155. return EXIT_SUCCESS;
  156. }