ympd.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. {"user", required_argument, 0, 'u'},
  75. {"version", no_argument, 0, 'v'},
  76. {"help", no_argument, 0, 0 },
  77. {"mpdpass", required_argument, 0, 'm'},
  78. {0, 0, 0, 0 }
  79. };
  80. while((n = getopt_long(argc, argv, "h:p:w:u:v:m",
  81. long_options, &option_index)) != -1) {
  82. switch (n) {
  83. case 'h':
  84. strncpy(mpd.host, optarg, sizeof(mpd.host));
  85. break;
  86. case 'p':
  87. mpd.port = atoi(optarg);
  88. break;
  89. case 'w':
  90. webport = strdup(optarg);
  91. break;
  92. case 'u':
  93. run_as_user = strdup(optarg);
  94. break;
  95. case 'm':
  96. mpd.password = strdup(optarg);
  97. break;
  98. case 'v':
  99. fprintf(stdout, "ympd %d.%d.%d\n"
  100. "Copyright (C) 2014 Andrew Karpow <andy@ndyk.de>\n"
  101. "built " __DATE__ " "__TIME__ " ("__VERSION__")\n",
  102. YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH);
  103. return EXIT_SUCCESS;
  104. break;
  105. default:
  106. fprintf(stderr, "Usage: %s [OPTION]...\n\n"
  107. " -h, --host <host>\t\tconnect to mpd at host [localhost]\n"
  108. " -p, --port <port>\t\tconnect to mpd at port [6600]\n"
  109. " -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n"
  110. " -u, --user <username>\t\tdrop priviliges to user after socket bind\n"
  111. " -V, --version\t\t\tget version\n"
  112. " -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n"
  113. " --help\t\t\t\tthis help\n"
  114. , argv[0]);
  115. return EXIT_FAILURE;
  116. }
  117. if(error_msg)
  118. {
  119. fprintf(stderr, "Mongoose error: %s\n", error_msg);
  120. return EXIT_FAILURE;
  121. }
  122. }
  123. error_msg = mg_set_option(server, "listening_port", webport);
  124. if(error_msg) {
  125. fprintf(stderr, "Mongoose error: %s\n", error_msg);
  126. return EXIT_FAILURE;
  127. }
  128. /* drop privilges at last to ensure proper port binding */
  129. if(run_as_user != NULL) {
  130. error_msg = mg_set_option(server, "run_as_user", run_as_user);
  131. free(run_as_user);
  132. if(error_msg)
  133. {
  134. fprintf(stderr, "Mongoose error: %s\n", error_msg);
  135. return EXIT_FAILURE;
  136. }
  137. }
  138. while (!force_exit) {
  139. mg_poll_server(server, 200);
  140. current_timer = time(NULL);
  141. if(current_timer - last_timer)
  142. {
  143. last_timer = current_timer;
  144. mpd_poll(server);
  145. }
  146. }
  147. mpd_disconnect();
  148. mg_destroy_server(&server);
  149. return EXIT_SUCCESS;
  150. }