1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-06-16 18:19:56 +00:00

Fix: cleanup source files

Feat: error handling for stickers
This commit is contained in:
jcorporation 2018-08-22 01:13:30 +01:00
parent fbb32ae17a
commit 6d1cf65f8f
3 changed files with 99 additions and 76 deletions

View File

@ -63,7 +63,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
fprintf(stdout,"Got request: %s\n",msg.p); fprintf(stdout,"Got request: %s\n",msg.p);
#endif #endif
je = json_scanf(msg.p, msg.len, "{cmd:%Q}", &cmd); je = json_scanf(msg.p, msg.len, "{cmd: %Q}", &cmd);
if (je == 1) if (je == 1)
cmd_id = get_cmd_id(cmd); cmd_id = get_cmd_id(cmd);
else else
@ -96,7 +96,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version, &mpd.queue_length); n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version, &mpd.queue_length);
break; break;
case MPD_API_SET_SETTINGS: case MPD_API_SET_SETTINGS:
je = json_scanf(msg.p, msg.len, "{data: { notificationWeb: %d, notificationPage: %d}}", &state.a, &state.b); je = json_scanf(msg.p, msg.len, "{data: {notificationWeb: %d, notificationPage: %d}}", &state.a, &state.b);
if (je == 2) { if (je == 2) {
char tmpfile[200]; char tmpfile[200];
snprintf(tmpfile,200,"%s.tmp", config.statefile); snprintf(tmpfile,200,"%s.tmp", config.statefile);
@ -464,7 +464,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
} }
break; break;
case MPD_API_RM_PLAYLIST: case MPD_API_RM_PLAYLIST:
je = json_scanf(msg.p, msg.len, "{data: { uri:%Q }}", &p_charbuf1); je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
if (je == 1) { if (je == 1) {
mpd_run_rm(mpd.conn, p_charbuf1); mpd_run_rm(mpd.conn, p_charbuf1);
free(p_charbuf1); free(p_charbuf1);
@ -498,13 +498,13 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
if (is_websocket(nc)) { if (is_websocket(nc)) {
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout, "Send websocket response:\n %s\n",mpd.buf); fprintf(stdout, "Send websocket response:\n %s\n", mpd.buf);
#endif #endif
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n); mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
} }
else { else {
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout, "Send http response:\n %s\n",mpd.buf); fprintf(stdout, "Send http response:\n %s\n", mpd.buf);
#endif #endif
mg_send_http_chunk(nc, mpd.buf, n); mg_send_http_chunk(nc, mpd.buf, n);
} }
@ -522,7 +522,7 @@ void mympd_notify(struct mg_mgr *s) {
#endif #endif
} }
void mympd_poll(struct mg_mgr *s, int timeout) { void mympd_idle(struct mg_mgr *s, int timeout) {
struct pollfd fds[1]; struct pollfd fds[1];
int pollrc; int pollrc;
@ -606,6 +606,8 @@ void mympd_get_sticker(const char *uri, t_sticker *sticker) {
mpd_return_sticker(mpd.conn, pair); 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) { 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) { void mympd_count_song_uri(const char *uri, char *name, int value) {
struct mpd_pair *pair; struct mpd_pair *pair;
int old_value = 0; int old_value = 0;
char v[3]; char v[4];
if (mpd_send_sticker_list(mpd.conn, "song", uri)) { if (mpd_send_sticker_list(mpd.conn, "song", uri)) {
while ((pair = mpd_recv_sticker(mpd.conn)) != NULL) { while ((pair = mpd_recv_sticker(mpd.conn)) != NULL) {
if (strcmp(pair->name, name) == 0) 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; old_value += value;
if (old_value > 99) if (old_value > 999)
old_value = 99; old_value = 999;
else if (old_value < 0) else if (old_value < 0)
old_value = 0; old_value = 0;
snprintf(v, 2, "%d", old_value); snprintf(v, 3, "%d", old_value);
mpd_run_sticker_set(mpd.conn, "song", uri, name, v); 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) { 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) else if (value < 0)
value = 0; value = 0;
snprintf(v, 2, "%d", value); 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) { 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) { void mympd_last_played_song_uri(const char *uri) {
char v[20]; char v[20];
snprintf(v, 19, "%lu", time(NULL)); 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) { void mympd_parse_idle(struct mg_mgr *s) {
@ -676,7 +681,7 @@ void mympd_parse_idle(struct mg_mgr *s) {
break; break;
if (idle_bitmask & idle_event) { if (idle_bitmask & idle_event) {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr,"IDLE: %s\n",idle_name); fprintf(stderr, "IDLE: %s\n", idle_name);
#endif #endif
switch(idle_event) { switch(idle_event) {
case MPD_IDLE_DATABASE: case MPD_IDLE_DATABASE:
@ -789,7 +794,7 @@ int mympd_put_welcome(char *buffer) {
int len; int len;
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE); struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
len = json_printf(&out, "{type: welcome, data: { version: %Q}}", MYMPD_VERSION); len = json_printf(&out, "{type: welcome, data: {version: %Q}}", MYMPD_VERSION);
if (len > MAX_SIZE) if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n"); fprintf(stderr,"Buffer truncated\n");
@ -870,12 +875,14 @@ int mympd_put_outputs(char *buffer) {
int nr; int nr;
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE); struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
if (!mpd_send_outputs(mpd.conn))
RETURN_ERROR_AND_RECOVER("outputs");
len = json_printf(&out,"{type: outputs, data: {outputs: ["); len = json_printf(&out,"{type: outputs, data: {outputs: [");
nr = 0;
mpd_send_outputs(mpd.conn);
nr=0;
while ((output = mpd_recv_output(mpd.conn)) != NULL) { 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}", len += json_printf(&out,"{id: %d, name: %Q, state: %d}",
mpd_output_get_id(output), mpd_output_get_id(output),
mpd_output_get_name(output), mpd_output_get_name(output),
@ -883,10 +890,8 @@ int mympd_put_outputs(char *buffer) {
); );
mpd_output_free(output); mpd_output_free(output);
} }
if (!mpd_response_finish(mpd.conn)) { if (!mpd_response_finish(mpd.conn))
fprintf(stderr, "MPD outputs: %s\n", mpd_connection_get_error_message(mpd.conn)); LOG_ERROR_AND_RECOVER("outputs");
mpd_connection_clear_error(mpd.conn);
}
len += json_printf(&out,"]}}"); 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) { int mympd_get_cover(const char *uri, char *cover, int cover_len) {
char *path = strdup(uri); char *path = strdup(uri);
int len; 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"); len = snprintf(cover, cover_len, "/assets/coverimage-httpstream.png");
} }
else { else {
@ -1021,7 +1026,7 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset + MAX_ELEMENTS_PER_PAGE)) if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset + MAX_ELEMENTS_PER_PAGE))
RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta"); RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta");
len = json_printf(&out, "{type:queue, data :[ "); len = json_printf(&out, "{type: queue, data: [");
while ((entity = mpd_recv_entity(mpd.conn)) != NULL) { while ((entity = mpd_recv_entity(mpd.conn)) != NULL) {
const struct mpd_song *song; const struct mpd_song *song;
@ -1031,7 +1036,8 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
drtn = mpd_song_get_duration(song); drtn = mpd_song_get_duration(song);
totalTime += drtn; totalTime += drtn;
entity_count ++; 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}", 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_id(song),
mpd_song_get_pos(song), mpd_song_get_pos(song),
@ -1045,7 +1051,7 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
mpd_entity_free(entity); mpd_entity_free(entity);
} }
len += json_printf(&out, "], totalTime: %d, totalEntities: %d, offset: %d, returnedEntities: %d, queue_version: %d }", len += json_printf(&out, "], totalTime: %d, totalEntities: %d, offset: %d, returnedEntities: %d, queue_version: %d}",
totalTime, totalTime,
mpd.queue_length, mpd.queue_length,
offset, offset,
@ -1070,7 +1076,7 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
if (!mpd_send_list_meta(mpd.conn, path)) if (!mpd_send_list_meta(mpd.conn, path))
RETURN_ERROR_AND_RECOVER("mpd_send_list_meta"); RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
len = json_printf(&out, "{type: browse, data: [ "); len = json_printf(&out, "{type: browse, data: [");
while((entity = mpd_recv_entity(mpd.conn)) != NULL) { while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
const struct mpd_song *song; const struct mpd_song *song;
@ -1079,16 +1085,17 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) {
switch (mpd_entity_get_type(entity)) { switch (mpd_entity_get_type(entity)) {
case MPD_ENTITY_TYPE_UNKNOWN: case MPD_ENTITY_TYPE_UNKNOWN:
entity_count --; entity_count --;
break; break;
case MPD_ENTITY_TYPE_SONG: case MPD_ENTITY_TYPE_SONG:
song = mpd_entity_get_song(entity); song = mpd_entity_get_song(entity);
entityName = mympd_get_tag(song, MPD_TAG_TITLE); entityName = mympd_get_tag(song, MPD_TAG_TITLE);
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,entityName,1) == 0 || if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 ||
( strncmp(filter,"0",1) == 0 && isalpha(*entityName) == 0 ) (strncmp(filter, "0", 1) == 0 && isalpha(*entityName) == 0 )
) { ) {
if (entities_returned ++) len += json_printf(&out,","); if (entities_returned ++)
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q }", 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), mpd_song_get_uri(song),
mympd_get_tag(song, MPD_TAG_ALBUM), mympd_get_tag(song, MPD_TAG_ALBUM),
mympd_get_tag(song, MPD_TAG_ARTIST), mympd_get_tag(song, MPD_TAG_ARTIST),
@ -1105,16 +1112,18 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
dir = mpd_entity_get_directory(entity); dir = mpd_entity_get_directory(entity);
entityName = mpd_directory_get_path(dir); entityName = mpd_directory_get_path(dir);
char *dirName = strrchr(entityName, '/'); char *dirName = strrchr(entityName, '/');
if (dirName != NULL) {
if (dirName != NULL)
dirName ++; dirName ++;
} else { else
dirName = strdup(entityName); dirName = strdup(entityName);
}
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,dirName,1) == 0 || if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, dirName, 1) == 0 ||
( strncmp(filter,"0",1) == 0 && isalpha(*dirName) == 0 ) (strncmp(filter, "0", 1) == 0 && isalpha(*dirName) == 0 )
) { ) {
if (entities_returned ++) len += json_printf(&out,","); if (entities_returned ++)
len += json_printf(&out, "{type: dir, uri: %Q, name: %Q }", len += json_printf(&out, ",");
len += json_printf(&out, "{type: dir, uri: %Q, name: %Q}",
entityName, entityName,
dirName dirName
); );
@ -1132,11 +1141,12 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
} else { } else {
plName = strdup(entityName); plName = strdup(entityName);
} }
if (strncmp(filter,"-",1) == 0 || strncasecmp(filter,plName,1) == 0 || if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, plName, 1) == 0 ||
( strncmp(filter,"0",1) == 0 && isalpha(*plName) == 0 ) (strncmp(filter, "0", 1) == 0 && isalpha(*plName) == 0 )
) { ) {
if (entities_returned ++) len += json_printf(&out,","); if (entities_returned ++)
len += json_printf(&out, "{type: plist, uri: %Q, name: %Q }", len += json_printf(&out, ",");
len += json_printf(&out, "{type: plist, uri: %Q, name: %Q}",
entityName, entityName,
plName plName
); );
@ -1155,7 +1165,7 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
return 0; return 0;
} }
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q }", len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q}",
entity_count, entity_count,
offset, offset,
entities_returned, entities_returned,
@ -1185,15 +1195,16 @@ int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char *
if (mpd_search_commit(mpd.conn) == false) if (mpd_search_commit(mpd.conn) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_commit"); RETURN_ERROR_AND_RECOVER("mpd_search_commit");
len = json_printf(&out, "{type: listDBtags, data: [ "); len = json_printf(&out, "{type: listDBtags, data: [");
while((pair = mpd_recv_pair_tag(mpd.conn, mpd_tag_name_parse(mpdtagtype))) != NULL) { while((pair = mpd_recv_pair_tag(mpd.conn, mpd_tag_name_parse(mpdtagtype))) != NULL) {
entity_count ++; entity_count ++;
if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) {
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, pair->value, 1) == 0 || if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, pair->value, 1) == 0 ||
(strncmp(filter,"0",1) == 0 && isalpha(*pair->value) == 0 ) (strncmp(filter, "0", 1) == 0 && isalpha(*pair->value) == 0 )
) { ) {
if (entities_returned ++) len += json_printf(&out, ", "); if (entities_returned ++)
len += json_printf(&out, "{type: %Q, value: %Q }", len += json_printf(&out, ", ");
len += json_printf(&out, "{type: %Q, value: %Q}",
mpdtagtype, mpdtagtype,
pair->value pair->value
); );
@ -1205,7 +1216,7 @@ int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char *
} }
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, " len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, "
"tagtype: %Q, searchtagtype: %Q, searchstr: %Q, filter: %Q }", "tagtype: %Q, searchtagtype: %Q, searchstr: %Q, filter: %Q}",
entity_count, entity_count,
offset, offset,
entities_returned, entities_returned,
@ -1240,14 +1251,17 @@ int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
if (mpd_search_commit(mpd.conn) == false) if (mpd_search_commit(mpd.conn) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_commit"); RETURN_ERROR_AND_RECOVER("mpd_search_commit");
else { else {
len = json_printf(&out, "{ type: listTitles, data: [ "); len = json_printf(&out, "{type: listTitles, data: [");
while((song = mpd_recv_song(mpd.conn)) != NULL) { while((song = mpd_recv_song(mpd.conn)) != NULL) {
entity_count ++; entity_count ++;
if (entity_count <= MAX_ELEMENTS_PER_PAGE) { if (entity_count <= MAX_ELEMENTS_PER_PAGE) {
if (entities_returned ++) len += json_printf(&out, ", "); if (entities_returned ++)
else mympd_get_cover(mpd_song_get_uri(song), cover, 500); len += json_printf(&out, ", ");
len += json_printf(&out, "{ type: song, uri: %Q, duration: %d, title: %Q, track: %Q}", 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_uri(song),
mpd_song_get_duration(song), mpd_song_get_duration(song),
mympd_get_tag(song, MPD_TAG_TITLE), mympd_get_tag(song, MPD_TAG_TITLE),
@ -1282,7 +1296,7 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) {
if (!mpd_send_list_playlists(mpd.conn)) if (!mpd_send_list_playlists(mpd.conn))
RETURN_ERROR_AND_RECOVER("mpd_send_lists_playlists"); RETURN_ERROR_AND_RECOVER("mpd_send_lists_playlists");
len = json_printf(&out, "{type: playlists, data: [ "); len = json_printf(&out, "{type: playlists, data: [");
while((pl = mpd_recv_playlist(mpd.conn)) != NULL) { while((pl = mpd_recv_playlist(mpd.conn)) != NULL) {
entity_count ++; entity_count ++;
@ -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 || if (strncmp(filter,"-",1) == 0 || strncasecmp(filter, plpath, 1) == 0 ||
(strncmp(filter, "0", 1) == 0 && isalpha(*plpath) == 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}", len += json_printf(&out, "{type: plist, uri: %Q, name: %Q, last_modified: %d}",
plpath, plpath,
plpath, plpath,
@ -1329,7 +1344,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
if (!mpd_send_list_playlist_meta(mpd.conn, uri)) if (!mpd_send_list_playlist_meta(mpd.conn, uri))
RETURN_ERROR_AND_RECOVER("mpd_send_list_meta"); RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
len = json_printf(&out, "{type: playlist_detail, data: [ "); len = json_printf(&out, "{type: playlist_detail, data: [");
while((entity = mpd_recv_entity(mpd.conn)) != NULL) { while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
const struct mpd_song *song; const struct mpd_song *song;
@ -1338,9 +1353,10 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
song = mpd_entity_get_song(entity); song = mpd_entity_get_song(entity);
entityName = mympd_get_tag(song, MPD_TAG_TITLE); entityName = mympd_get_tag(song, MPD_TAG_TITLE);
if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 || if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 ||
( strncmp(filter, "0", 1) == 0 && isalpha(*entityName) == 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}", len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
mpd_song_get_uri(song), mpd_song_get_uri(song),
mympd_get_tag(song, MPD_TAG_ALBUM), mympd_get_tag(song, MPD_TAG_ALBUM),
@ -1391,12 +1407,13 @@ int mympd_search(char *buffer, char *mpdtagtype, unsigned int offset, char *sear
if (mpd_search_commit(mpd.conn) == false) if (mpd_search_commit(mpd.conn) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_commit"); RETURN_ERROR_AND_RECOVER("mpd_search_commit");
else { else {
len = json_printf(&out, "{type: search, data: [ "); len = json_printf(&out, "{type: search, data: [");
while((song = mpd_recv_song(mpd.conn)) != NULL) { while((song = mpd_recv_song(mpd.conn)) != NULL) {
entity_count ++; entity_count ++;
if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { 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}", len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
mpd_song_get_uri(song), mpd_song_get_uri(song),
mympd_get_tag(song, MPD_TAG_ALBUM), mympd_get_tag(song, MPD_TAG_ALBUM),
@ -1475,7 +1492,7 @@ int mympd_queue_crop(char *buffer) {
int playing_song_pos = mpd_status_get_song_pos(status); int playing_song_pos = mpd_status_get_song_pos(status);
if (length < 0) { if (length < 0) {
len = json_printf(&out, "{type: error, data: %Q }", "A playlist longer than 1 song in length is required to crop."); len = json_printf(&out, "{type: error, data: %Q}", "A playlist longer than 1 song in length is required to crop.");
} }
else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) {
playing_song_pos ++; playing_song_pos ++;
@ -1519,12 +1536,13 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char
if (mpd_search_commit(mpd.conn) == false) if (mpd_search_commit(mpd.conn) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_commit"); RETURN_ERROR_AND_RECOVER("mpd_search_commit");
else { else {
len = json_printf(&out, "{ type: queuesearch, data: [ "); len = json_printf(&out, "{type: queuesearch, data: [");
while((song = mpd_recv_song(mpd.conn)) != NULL) { while((song = mpd_recv_song(mpd.conn)) != NULL) {
entity_count ++; entity_count ++;
if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { 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}", 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_id(song),
mpd_song_get_pos(song), mpd_song_get_pos(song),
@ -1560,7 +1578,7 @@ int mympd_put_stats(char *buffer) {
if (stats == NULL) if (stats == NULL)
RETURN_ERROR_AND_RECOVER("mympd_put_stats"); RETURN_ERROR_AND_RECOVER("mympd_put_stats");
len = json_printf(&out, "{type: mpdstats, data: { artists: %d, albums: %d, songs: %d, " len = json_printf(&out, "{type: mpdstats, data: {artists: %d, albums: %d, songs: %d, "
"playtime: %d, uptime: %d, dbUpdated: %d, dbPlaytime: %d, mympdVersion: %Q, mpdVersion: %Q}}", "playtime: %d, uptime: %d, dbUpdated: %d, dbPlaytime: %d, mympdVersion: %Q, mpdVersion: %Q}}",
mpd_stats_get_number_of_artists(stats), mpd_stats_get_number_of_artists(stats),
mpd_stats_get_number_of_albums(stats), mpd_stats_get_number_of_albums(stats),
@ -1581,5 +1599,5 @@ int mympd_put_stats(char *buffer) {
void mympd_disconnect() { void mympd_disconnect() {
mpd.conn_state = MPD_DISCONNECT; mpd.conn_state = MPD_DISCONNECT;
mympd_poll(NULL, 0); mympd_idle(NULL, 0);
} }

View File

@ -37,6 +37,11 @@
return len; \ return len; \
} while (0) } 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_SIZE 1024 * 100
#define MAX_ELEMENTS_PER_PAGE 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; 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 mympd_parse_idle(struct mg_mgr *s);
void callback_mympd(struct mg_connection *nc, const struct mg_str msg); void callback_mympd(struct mg_connection *nc, const struct mg_str msg);
void mympd_notify(struct mg_mgr *s); void mympd_notify(struct mg_mgr *s);

View File

@ -186,7 +186,7 @@ int main(int argc, char **argv) {
if (argc == 2) { if (argc == 2) {
if (ini_parse(argv[1], inihandler, &config) < 0) { 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; return EXIT_FAILURE;
} }
} }
@ -210,10 +210,10 @@ int main(int argc, char **argv) {
mg_mgr_init(&mgr, NULL); mg_mgr_init(&mgr, NULL);
if (config.ssl == true) { 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); nc_http = mg_bind(&mgr, config.webport, ev_handler_http);
if (nc_http == NULL) { if (nc_http == NULL) {
fprintf(stderr, "Error starting server on port %s\n", config.webport ); fprintf(stderr, "Error starting server on port %s\n", config.webport);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
memset(&bind_opts, 0, sizeof(bind_opts)); memset(&bind_opts, 0, sizeof(bind_opts));
@ -229,7 +229,7 @@ int main(int argc, char **argv) {
else { else {
nc = mg_bind(&mgr, config.webport, ev_handler); nc = mg_bind(&mgr, config.webport, ev_handler);
if (nc == NULL) { if (nc == NULL) {
fprintf(stderr, "Error starting server on port %s\n", config.webport ); fprintf(stderr, "Error starting server on port %s\n", config.webport);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
@ -238,22 +238,22 @@ int main(int argc, char **argv) {
printf("Droping privileges\n"); printf("Droping privileges\n");
struct passwd *pw; struct passwd *pw;
if ((pw = getpwnam(config.user)) == NULL) { if ((pw = getpwnam(config.user)) == NULL) {
printf("Unknown user\n"); fprintf(stderr, "Unknown user\n");
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
return EXIT_FAILURE; return EXIT_FAILURE;
} else if (setgid(pw->pw_gid) != 0) { } else if (setgid(pw->pw_gid) != 0) {
printf("setgid() failed\n"); fprintf(stderr, "setgid() failed\n");
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
return EXIT_FAILURE; return EXIT_FAILURE;
} else if (setuid(pw->pw_uid) != 0) { } else if (setuid(pw->pw_uid) != 0) {
printf("setuid() failed\n"); fprintf(stderr, "setuid() failed\n");
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
if (getuid() == 0) { 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); mg_mgr_free(&mgr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -271,7 +271,7 @@ int main(int argc, char **argv) {
while (s_signal_received == 0) { while (s_signal_received == 0) {
mg_mgr_poll(&mgr, 100); mg_mgr_poll(&mgr, 100);
mympd_poll(&mgr, 0); mympd_idle(&mgr, 0);
} }
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
mympd_disconnect(); mympd_disconnect();