mirror of
https://github.com/SuperBFG7/ympd
synced 2025-05-02 15:34:06 +00:00
Fix: cleanup source files
Feat: error handling for stickers
This commit is contained in:
parent
fbb32ae17a
commit
6d1cf65f8f
@ -522,7 +522,7 @@ void mympd_notify(struct mg_mgr *s) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void mympd_poll(struct mg_mgr *s, int timeout) {
|
||||
void mympd_idle(struct mg_mgr *s, int timeout) {
|
||||
struct pollfd fds[1];
|
||||
int pollrc;
|
||||
|
||||
@ -606,6 +606,8 @@ void mympd_get_sticker(const char *uri, t_sticker *sticker) {
|
||||
mpd_return_sticker(mpd.conn, pair);
|
||||
}
|
||||
}
|
||||
else
|
||||
LOG_ERROR_AND_RECOVER("mpd_send_sticker_list");
|
||||
}
|
||||
|
||||
char* mympd_get_song_uri_from_song_id(int song_id) {
|
||||
@ -628,7 +630,7 @@ void mympd_count_song_id(int song_id, char *name, int value) {
|
||||
void mympd_count_song_uri(const char *uri, char *name, int value) {
|
||||
struct mpd_pair *pair;
|
||||
int old_value = 0;
|
||||
char v[3];
|
||||
char v[4];
|
||||
if (mpd_send_sticker_list(mpd.conn, "song", uri)) {
|
||||
while ((pair = mpd_recv_sticker(mpd.conn)) != NULL) {
|
||||
if (strcmp(pair->name, name) == 0)
|
||||
@ -637,12 +639,13 @@ void mympd_count_song_uri(const char *uri, char *name, int value) {
|
||||
}
|
||||
}
|
||||
old_value += value;
|
||||
if (old_value > 99)
|
||||
old_value = 99;
|
||||
if (old_value > 999)
|
||||
old_value = 999;
|
||||
else if (old_value < 0)
|
||||
old_value = 0;
|
||||
snprintf(v, 2, "%d", old_value);
|
||||
mpd_run_sticker_set(mpd.conn, "song", uri, name, v);
|
||||
snprintf(v, 3, "%d", old_value);
|
||||
if (!mpd_run_sticker_set(mpd.conn, "song", uri, name, v))
|
||||
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
||||
}
|
||||
|
||||
void mympd_like_song_uri(const char *uri, int value) {
|
||||
@ -652,7 +655,8 @@ void mympd_like_song_uri(const char *uri, int value) {
|
||||
else if (value < 0)
|
||||
value = 0;
|
||||
snprintf(v, 2, "%d", value);
|
||||
mpd_run_sticker_set(mpd.conn, "song", uri, "like", v);
|
||||
if (!mpd_run_sticker_set(mpd.conn, "song", uri, "like", v))
|
||||
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
||||
}
|
||||
|
||||
void mympd_last_played_song_id(int song_id) {
|
||||
@ -662,7 +666,8 @@ void mympd_last_played_song_id(int song_id) {
|
||||
void mympd_last_played_song_uri(const char *uri) {
|
||||
char v[20];
|
||||
snprintf(v, 19, "%lu", time(NULL));
|
||||
mpd_run_sticker_set(mpd.conn, "song", uri, "lastPlayed", v);
|
||||
if (!mpd_run_sticker_set(mpd.conn, "song", uri, "lastPlayed", v))
|
||||
LOG_ERROR_AND_RECOVER("mpd_send_sticker_set");
|
||||
}
|
||||
|
||||
void mympd_parse_idle(struct mg_mgr *s) {
|
||||
@ -870,12 +875,14 @@ int mympd_put_outputs(char *buffer) {
|
||||
int nr;
|
||||
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
|
||||
|
||||
len = json_printf(&out,"{type: outputs, data: {outputs: [");
|
||||
if (!mpd_send_outputs(mpd.conn))
|
||||
RETURN_ERROR_AND_RECOVER("outputs");
|
||||
|
||||
mpd_send_outputs(mpd.conn);
|
||||
len = json_printf(&out,"{type: outputs, data: {outputs: [");
|
||||
nr = 0;
|
||||
while ((output = mpd_recv_output(mpd.conn)) != NULL) {
|
||||
if (nr ++) len += json_printf(&out, ",");
|
||||
if (nr ++)
|
||||
len += json_printf(&out, ",");
|
||||
len += json_printf(&out,"{id: %d, name: %Q, state: %d}",
|
||||
mpd_output_get_id(output),
|
||||
mpd_output_get_name(output),
|
||||
@ -883,10 +890,8 @@ int mympd_put_outputs(char *buffer) {
|
||||
);
|
||||
mpd_output_free(output);
|
||||
}
|
||||
if (!mpd_response_finish(mpd.conn)) {
|
||||
fprintf(stderr, "MPD outputs: %s\n", mpd_connection_get_error_message(mpd.conn));
|
||||
mpd_connection_clear_error(mpd.conn);
|
||||
}
|
||||
if (!mpd_response_finish(mpd.conn))
|
||||
LOG_ERROR_AND_RECOVER("outputs");
|
||||
|
||||
len += json_printf(&out,"]}}");
|
||||
|
||||
@ -898,7 +903,7 @@ int mympd_put_outputs(char *buffer) {
|
||||
int mympd_get_cover(const char *uri, char *cover, int cover_len) {
|
||||
char *path = strdup(uri);
|
||||
int len;
|
||||
if (strncasecmp("http:", path, 5) == 0 ) {
|
||||
if (strncasecmp("http:", path, 5) == 0 || strncasecmp("https:", path, 6) == 0) {
|
||||
len = snprintf(cover, cover_len, "/assets/coverimage-httpstream.png");
|
||||
}
|
||||
else {
|
||||
@ -1031,7 +1036,8 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
|
||||
drtn = mpd_song_get_duration(song);
|
||||
totalTime += drtn;
|
||||
entity_count ++;
|
||||
if (entities_returned ++) len += json_printf(&out,",");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ",");
|
||||
len += json_printf(&out, "{id: %d, pos: %d, duration: %d, artist: %Q, album: %Q, title: %Q, uri: %Q}",
|
||||
mpd_song_get_id(song),
|
||||
mpd_song_get_pos(song),
|
||||
@ -1087,7 +1093,8 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
|
||||
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 ||
|
||||
(strncmp(filter, "0", 1) == 0 && isalpha(*entityName) == 0 )
|
||||
) {
|
||||
if (entities_returned ++) len += json_printf(&out,",");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ",");
|
||||
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
|
||||
mpd_song_get_uri(song),
|
||||
mympd_get_tag(song, MPD_TAG_ALBUM),
|
||||
@ -1105,15 +1112,17 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
|
||||
dir = mpd_entity_get_directory(entity);
|
||||
entityName = mpd_directory_get_path(dir);
|
||||
char *dirName = strrchr(entityName, '/');
|
||||
if (dirName != NULL) {
|
||||
|
||||
if (dirName != NULL)
|
||||
dirName ++;
|
||||
} else {
|
||||
else
|
||||
dirName = strdup(entityName);
|
||||
}
|
||||
|
||||
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, dirName, 1) == 0 ||
|
||||
(strncmp(filter, "0", 1) == 0 && isalpha(*dirName) == 0 )
|
||||
) {
|
||||
if (entities_returned ++) len += json_printf(&out,",");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ",");
|
||||
len += json_printf(&out, "{type: dir, uri: %Q, name: %Q}",
|
||||
entityName,
|
||||
dirName
|
||||
@ -1135,7 +1144,8 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
|
||||
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, plName, 1) == 0 ||
|
||||
(strncmp(filter, "0", 1) == 0 && isalpha(*plName) == 0 )
|
||||
) {
|
||||
if (entities_returned ++) len += json_printf(&out,",");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ",");
|
||||
len += json_printf(&out, "{type: plist, uri: %Q, name: %Q}",
|
||||
entityName,
|
||||
plName
|
||||
@ -1192,7 +1202,8 @@ int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char *
|
||||
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, pair->value, 1) == 0 ||
|
||||
(strncmp(filter, "0", 1) == 0 && isalpha(*pair->value) == 0 )
|
||||
) {
|
||||
if (entities_returned ++) len += json_printf(&out, ", ");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ", ");
|
||||
len += json_printf(&out, "{type: %Q, value: %Q}",
|
||||
mpdtagtype,
|
||||
pair->value
|
||||
@ -1245,8 +1256,11 @@ int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
|
||||
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
||||
entity_count ++;
|
||||
if (entity_count <= MAX_ELEMENTS_PER_PAGE) {
|
||||
if (entities_returned ++) len += json_printf(&out, ", ");
|
||||
else mympd_get_cover(mpd_song_get_uri(song), cover, 500);
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ", ");
|
||||
else
|
||||
mympd_get_cover(mpd_song_get_uri(song), cover, 500);
|
||||
|
||||
len += json_printf(&out, "{type: song, uri: %Q, duration: %d, title: %Q, track: %Q}",
|
||||
mpd_song_get_uri(song),
|
||||
mpd_song_get_duration(song),
|
||||
@ -1291,7 +1305,8 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) {
|
||||
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter, plpath, 1) == 0 ||
|
||||
(strncmp(filter, "0", 1) == 0 && isalpha(*plpath) == 0 )
|
||||
) {
|
||||
if (entities_returned ++) len += json_printf(&out, ", ");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ", ");
|
||||
len += json_printf(&out, "{type: plist, uri: %Q, name: %Q, last_modified: %d}",
|
||||
plpath,
|
||||
plpath,
|
||||
@ -1340,7 +1355,8 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
|
||||
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 ||
|
||||
(strncmp(filter, "0", 1) == 0 && isalpha(*entityName) == 0 )
|
||||
) {
|
||||
if (entities_returned ++) len += json_printf(&out, ",");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ",");
|
||||
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
|
||||
mpd_song_get_uri(song),
|
||||
mympd_get_tag(song, MPD_TAG_ALBUM),
|
||||
@ -1396,7 +1412,8 @@ int mympd_search(char *buffer, char *mpdtagtype, unsigned int offset, char *sear
|
||||
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
||||
entity_count ++;
|
||||
if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) {
|
||||
if (entities_returned ++) len += json_printf(&out, ", ");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ", ");
|
||||
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
|
||||
mpd_song_get_uri(song),
|
||||
mympd_get_tag(song, MPD_TAG_ALBUM),
|
||||
@ -1524,7 +1541,8 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char
|
||||
while((song = mpd_recv_song(mpd.conn)) != NULL) {
|
||||
entity_count ++;
|
||||
if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) {
|
||||
if (entities_returned ++) len += json_printf(&out, ", ");
|
||||
if (entities_returned ++)
|
||||
len += json_printf(&out, ", ");
|
||||
len += json_printf(&out, "{type: song, id: %d, pos: %d, album: %Q, artist: %Q, duration: %d, title: %Q}",
|
||||
mpd_song_get_id(song),
|
||||
mpd_song_get_pos(song),
|
||||
@ -1581,5 +1599,5 @@ int mympd_put_stats(char *buffer) {
|
||||
|
||||
void mympd_disconnect() {
|
||||
mpd.conn_state = MPD_DISCONNECT;
|
||||
mympd_poll(NULL, 0);
|
||||
mympd_idle(NULL, 0);
|
||||
}
|
||||
|
@ -37,6 +37,11 @@
|
||||
return len; \
|
||||
} while (0)
|
||||
|
||||
#define LOG_ERROR_AND_RECOVER(X) do { \
|
||||
fprintf(stderr, "MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \
|
||||
if (!mpd_connection_clear_error(mpd.conn)) \
|
||||
mpd.conn_state = MPD_FAILURE; \
|
||||
} while (0)
|
||||
|
||||
#define MAX_SIZE 1024 * 100
|
||||
#define MAX_ELEMENTS_PER_PAGE 100
|
||||
@ -153,7 +158,7 @@ static int is_websocket(const struct mg_connection *nc) {
|
||||
return nc->flags & MG_F_IS_WEBSOCKET;
|
||||
}
|
||||
|
||||
void mympd_poll(struct mg_mgr *sm, int timeout);
|
||||
void mympd_idle(struct mg_mgr *sm, int timeout);
|
||||
void mympd_parse_idle(struct mg_mgr *s);
|
||||
void callback_mympd(struct mg_connection *nc, const struct mg_str msg);
|
||||
void mympd_notify(struct mg_mgr *s);
|
||||
|
14
src/mympd.c
14
src/mympd.c
@ -186,7 +186,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
if (argc == 2) {
|
||||
if (ini_parse(argv[1], inihandler, &config) < 0) {
|
||||
printf("Can't load '%s'\n", argv[1]);
|
||||
printf("Can't load config file \"%s\"\n", argv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
@ -210,7 +210,7 @@ int main(int argc, char **argv) {
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
|
||||
if (config.ssl == true) {
|
||||
snprintf(s_redirect, 200, "https://%s:%s/", hostname, config.sslport);
|
||||
snprintf(s_redirect, 249, "https://%s:%s/", hostname, config.sslport);
|
||||
nc_http = mg_bind(&mgr, config.webport, ev_handler_http);
|
||||
if (nc_http == NULL) {
|
||||
fprintf(stderr, "Error starting server on port %s\n", config.webport);
|
||||
@ -238,22 +238,22 @@ int main(int argc, char **argv) {
|
||||
printf("Droping privileges\n");
|
||||
struct passwd *pw;
|
||||
if ((pw = getpwnam(config.user)) == NULL) {
|
||||
printf("Unknown user\n");
|
||||
fprintf(stderr, "Unknown user\n");
|
||||
mg_mgr_free(&mgr);
|
||||
return EXIT_FAILURE;
|
||||
} else if (setgid(pw->pw_gid) != 0) {
|
||||
printf("setgid() failed\n");
|
||||
fprintf(stderr, "setgid() failed\n");
|
||||
mg_mgr_free(&mgr);
|
||||
return EXIT_FAILURE;
|
||||
} else if (setuid(pw->pw_uid) != 0) {
|
||||
printf("setuid() failed\n");
|
||||
fprintf(stderr, "setuid() failed\n");
|
||||
mg_mgr_free(&mgr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (getuid() == 0) {
|
||||
printf("myMPD should not be run with root privileges\n");
|
||||
fprintf(stderr, "myMPD should not be run with root privileges\n");
|
||||
mg_mgr_free(&mgr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@ -271,7 +271,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
while (s_signal_received == 0) {
|
||||
mg_mgr_poll(&mgr, 100);
|
||||
mympd_poll(&mgr, 0);
|
||||
mympd_idle(&mgr, 0);
|
||||
}
|
||||
mg_mgr_free(&mgr);
|
||||
mympd_disconnect();
|
||||
|
Loading…
x
Reference in New Issue
Block a user