Rev 8 | Rev 11 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*
* NOTICE: All information contained herein is, and remains
* the property of Andreas Theofilu and his suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to Andreas Theofilu and its suppliers and
* may be covered by European and Foreign Patents, patents in process,
* and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Andreas Theofilu.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <ctype.h>
#include <math.h>
#include <syslog.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sqlite3.h>
#include <id3.h>
#include "config.h"
#include "helplib.h"
#include "list.h"
#include "user.h"
#include "play.h"
struct callPars
{
int s1;
int start;
int length;
char *type;
};
int line;
int pos;
static int listCallback(void *hint, int argc, char **argv, char **azColName);
static int folderCallback(void *hint, int argc, char **argv, char **azColName);
int listSongs(int s1, char *p_type, int start, int length)
{
char query[1024];
char fname[256];
sqlite3 *db;
char *zErrMsg = 0;
int rc;
struct callPars cp;
strcpy(fname, configs.home);
strcat(fname, MUSICDB);
rc = sqlite3_open(fname, &db);
if (rc)
{
syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
strcpy(query, "ERROR:LIST:Error opening database;");
write (s1, query, strlen(query));
return FALSE;
}
strcpy (query, "select id, title, interpret, album, genre from \"main\".\"musicdb\" ");
strcat (query, "order by ");
if (strcmp(p_type, "TITLE") == 0)
strcat (query, "title");
else if (strcmp(p_type, "ARTIST") == 0)
strcat (query, "interpret");
else if (strcmp(p_type, "ALBUM") == 0)
strcat (query, "album");
else if (strcmp(p_type, "GENRE") == 0)
strcat (query, "genre");
else if (strcmp(p_type, "QUEUE") == 0)
{
sqlite3_close(db);
return listQueue(s1, start, length);
}
else if (strcmp(p_type, "PLAYLIST") == 0)
{
sqlite3_close(db);
return listPlaylists(s1, start, length);
}
else /* No or unknown type */
{
strcpy (query, "ERROR:LIST:Missing type;");
write(s1, query, strlen(query));
sqlite3_close(db);
return FALSE;
}
cp.s1 = s1;
cp.type = p_type;
cp.start = start;
cp.length = length;
line = 0;
pos = -1;
if ((rc = sqlite3_exec(db, query, listCallback, (void *)&cp, &zErrMsg)) != SQLITE_OK)
{
syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
strcpy(query, "ERROR:LIST:SQL error;");
write (s1, query, strlen(query));
return FALSE;
}
if (pos >= 0)
{
sprintf(query, "TOTAL:%d;", pos + 1);
write (s1, query, strlen(query));
}
sqlite3_close(db);
return TRUE;
}
int listQueue(int s1, int start, int length)
{
int fd, num;
char fname[256], hv0[256], line[8192], buffer[8192];
char path[512], id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
char *title, *artist, *album, *t;
int x, id, pos, len;
strcpy (fname, configs.home);
strcat (fname, NOWPLAY);
if (access(fname, R_OK))
{
strcpy (hv0, "ERROR:LIST:No or empty queue;");
write (s1, hv0, strlen(hv0));
return FALSE;
}
if ((fd = open(fname, O_RDONLY)) <= 0)
{
syslog(LOG_WARNING, "Error opening file %s: %s", fname, strerror(errno));
strcpy(hv0, "ERROR:LIST:Error opening queue;");
write(s1, hv0, strlen(hv0));
return FALSE;
}
/* Count the lines for a total value first */
num = 0;
while (readLine(fd, line, sizeof(line)) != NULL)
num++;
sprintf(hv0, "TOTAL:%d;", num);
/* Reset the position in file to the start */
lseek (fd, 0L, SEEK_SET);
/* Read the file again and write the result to the connected client */
pos = len = 0;
while (readLine(fd, line, sizeof(line)) != NULL)
{
pos++;
if (pos < start)
continue;
len++;
if (len > length)
break;
memset(path, 0, sizeof(path));
memset(id3_title, 0, sizeof(id3_title));
memset(id3_artist, 0, sizeof(id3_artist));
memset(id3_album, 0, sizeof(id3_album));
memset(id3_genre, 0, sizeof(id3_genre));
x = 0;
t = strtok(line, "\t");
while (t)
{
switch (x)
{
case 0: strncpy(path, t, sizeof(path)); break;
case 1: id = atoi(t); break;
case 2: strncpy(id3_title, t, sizeof(id3_title)); break;
case 3: strncpy(id3_artist, t, sizeof(id3_artist)); break;
case 4: strncpy(id3_album, t, sizeof(id3_album)); break;
case 5: strncpy(id3_genre, t, sizeof(id3_genre)); break;
}
t = strtok(NULL, "\t");
x++;
}
title = urlencode(id3_title);
artist = urlencode(id3_artist);
album = urlencode(id3_album);
sprintf(buffer, "LINE:QUEUE:%d:%d:", id, len);
if (title != NULL)
{
strcat(buffer, title);
free(title);
}
else
strcat(buffer, id3_title);
strcat(buffer, ":");
if (artist != NULL)
{
strcat(buffer, artist);
free(artist);
}
else
strcat(buffer, id3_artist);
strcat(buffer, ":");
if (album != NULL)
{
strcat(buffer, album);
free(album);
}
else
strcat(buffer, id3_album);
strcat(buffer, ":");
strcat(buffer, id3_genre);
strcat(buffer, ";");
write(s1, buffer, strlen(buffer));
}
close(fd);
return TRUE;
}
int listFolders(int s1, char *p_type, int start, int length)
{
char query[1024], field[16];
char fname[256];
sqlite3 *db;
char *zErrMsg = 0;
int rc;
struct callPars cp;
strcpy(fname, configs.home);
strcat(fname, MUSICDB);
rc = sqlite3_open(fname, &db);
if (rc)
{
syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
strcpy(query, "ERROR:FOLDER:Error opening database;");
write (s1, query, strlen(query));
return FALSE;
}
strcpy (query, "select distinct ");
if (strcmp(p_type, "TITLE") == 0)
strcpy (field, "title");
else if (strcmp(p_type, "ARTIST") == 0)
strcpy (field, "interpret");
else if (strcmp(p_type, "ALBUM") == 0)
strcpy (field, "album");
else if (strcmp(p_type, "GENRE") == 0)
strcpy (field, "genre");
else /* No or unknown type */
{
strcpy (query, "ERROR:FOLDER:Missing type;");
write(s1, query, strlen(query));
sqlite3_close(db);
return FALSE;
}
strcat (query, field);
strcat (query, " from \"main\".\"musicdb\" order by ");
strcat (query, field);
cp.s1 = s1;
cp.type = p_type;
cp.start = start;
cp.length = length;
line = 0;
pos = -1;
if ((rc = sqlite3_exec(db, query, folderCallback, (void *)&cp, &zErrMsg)) != SQLITE_OK)
{
syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
strcpy(query, "ERROR:FOLDER:SQL error;");
write (s1, query, strlen(query));
return FALSE;
}
if (pos >= 0)
{
sprintf(query, "TOTAL:%d;", pos + 1);
write (s1, query, strlen(query));
}
sqlite3_close(db);
return TRUE;
}
static int listCallback(void *hint, int argc, char **argv, char **azColName)
{
int i, id;
char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
char buffer[8192];
char *title, *artist, *album;
struct callPars *cp;
pos++;
cp = (struct callPars *)hint;
memset(id3_title, 0, sizeof(id3_title));
memset(id3_artist, 0, sizeof(id3_artist));
memset(id3_album, 0, sizeof(id3_album));
memset(id3_genre, 0, sizeof(id3_genre));
id = -1;
if (pos < cp->start || pos >= (cp->start + cp->length))
return 0;
for(i = 0; i < argc; i++)
{
if (strcasecmp(azColName[i], "id") == 0)
if (argv[i])
id = atoi(argv[i]);
if (strcasecmp(azColName[i], "title") == 0)
if (argv[i])
strncpy(id3_title, argv[i], sizeof(id3_title));
if (strcasecmp(azColName[i], "interpret") == 0)
if (argv[i])
strncpy(id3_artist, argv[i], sizeof(id3_artist));
if (strcasecmp(azColName[i], "album") == 0)
if (argv[i])
strncpy(id3_album, argv[i], sizeof(id3_album));
if (strcasecmp(azColName[i], "genre") == 0)
if (argv[i])
strncpy(id3_genre, argv[i], sizeof(id3_genre));
}
title = urlencode(id3_title);
artist = urlencode(id3_artist);
album = urldecode(id3_album);
/* title = artist = album = NULL; */
line++;
sprintf(buffer, "LINE:%s:%d:%d:", cp->type, id, line);
if (title != NULL)
{
strcat(buffer, title);
free(title);
}
else
strcat(buffer, id3_title);
strcat(buffer, ":");
if (artist != NULL)
{
strcat(buffer, artist);
free(artist);
}
else
strcat(buffer, id3_artist);
strcat(buffer, ":");
if (album != NULL)
{
strcat(buffer, album);
free(album);
}
else
strcat(buffer, id3_album);
strcat(buffer, ":");
strcat(buffer, id3_genre);
strcat(buffer, ";");
write(cp->s1, buffer, strlen(buffer));
return 0;
}
static int folderCallback(void *hint, int argc, char **argv, char **azColName)
{
int i;
char id3_buffer[256];
char buffer[8192];
char *buf;
struct callPars *cp;
pos++;
cp = (struct callPars *)hint;
memset(id3_buffer, 0, sizeof(id3_buffer));
if (pos < cp->start || pos >= (cp->start + cp->length))
return 0;
for(i = 0; i < argc; i++)
{
if (strcasecmp(azColName[i], "title") == 0 || strcasecmp(azColName[i], "interpret") == 0 ||
strcasecmp(azColName[i], "album") == 0 || strcasecmp(azColName[i], "genre") == 0)
if (argv[i])
strncpy(id3_buffer, argv[i], sizeof(id3_buffer));
}
buf = urlencode(id3_buffer);
line++;
sprintf(buffer, "FOLDER:%s:%d:", cp->type, line);
if (buf != NULL)
{
strcat(buffer, buf);
free(buf);
}
else
strcat(buffer, id3_buffer);
strcat(buffer, ";");
write(cp->s1, buffer, strlen(buffer));
return 0;
}
/*
* List the playlists of the current user.
*/
int listPlaylists(int s1, int start, int length)
{
USERS *act;
char hv0[512], *user, *playlist;
int pos;
if (userchain == NULL)
{
strcpy(hv0, "ERROR:PLAYLIST:No user selected;");
write(s1, hv0, strlen(hv0));
return FALSE;
}
act = userchain;
pos = 1;
while (act)
{
if (pos < start)
{
act = act->next;
continue;
}
if (pos >= (start + length))
break;
user = urlencode(act->uname);
playlist = urlencode(act->playlist);
sprintf(hv0, "PLAYLIST:%d:%s:%s;", act->id, user, playlist);
write (s1, hv0, strlen(hv0));
if (user)
free (user);
if (playlist)
free (playlist);
act = act->next;
}
return TRUE;
}
int listUserPlaylist(int s1, const char *user, const char *playlist, int start, int length)
{
char query[1024], hv0[128], buffer[8192];
char fname[256];
sqlite3 *db;
char *zErrMsg = 0;
int rc, id, total, pos, line;
sqlite3_stmt *res;
char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
char *title, *artist, *album, *t;
USERS *act;
if (playlist == NULL)
{
strcpy(hv0, "ERROR:LIST:Missing name of playlist;");
write (s1, hv0, strlen(hv0));
return FALSE;
}
/* Check current user */
if (user != NULL)
{
if (userchain == NULL || strcmp(userchain->uname, user))
{
if (!selectUser(s1, user))
return FALSE;
}
}
/* Find the playlist */
act = userchain;
while (act)
{
if (!strcmp(act->playlist, playlist))
break;
act = act->next;
}
if (act == NULL)
{
strcpy(hv0, "ERROR:LIST:Playlist not found;");
write (s1, hv0, strlen(hv0));
return FALSE;
}
strcpy(fname, configs.home);
strcat(fname, MUSICDB);
rc = sqlite3_open(fname, &db);
if (rc)
{
syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
strcpy(query, "ERROR:USER:Error opening database;");
write (s1, query, strlen(query));
return FALSE;
}
strcpy (query, "select id, title, interpret, album, genre from musicdb as a where ");
strcat (query, "(select musicid from playlists as b where a.id = b.musicid and b.userid = ");
sprintf(hv0, "%d", act->id);
strcat (query, hv0);
strcat (query, ")");
if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
{
syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
strcpy(hv0, "ERROR:USER:Error preparing a SQL statement;");
write(s1, hv0, strlen(hv0));
return FALSE;
}
total = sqlite3_column_count(res);
sprintf(hv0, "TOTAL:%d;", total);
write (s1, hv0, strlen(hv0));
pos = 1;
line = 0;
while ((rc = sqlite3_step(res)) == SQLITE_ROW)
{
if (pos < start)
{
pos++;
continue;
}
if (pos >= (start + length))
break;
line++;
memset(id3_title, 0, sizeof(id3_title));
memset(id3_artist, 0, sizeof(id3_artist));
memset(id3_album, 0, sizeof(id3_album));
memset(id3_genre, 0, sizeof(id3_genre));
id = sqlite3_column_int(res, 0);
strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
title = urlencode(id3_title);
artist = urlencode(id3_artist);
album = urlencode(id3_album);
if (title != NULL)
{
strncpy(id3_title, title, sizeof(id3_title));
free(title);
}
if (artist != NULL)
{
strncpy(id3_artist, artist, sizeof(id3_artist));
free(artist);
}
if (album != NULL)
{
strncpy(id3_album, album, sizeof(id3_album));
free(album);
}
sprintf (buffer, "LINE:PLAYLIST:%d:%d:%s:%s:%s:%s;", id, line, id3_title, id3_artist, id3_album, id3_genre);
write (s1, buffer, strlen(buffer));
pos++;
}
sqlite3_finalize(res);
sqlite3_close(db);
return TRUE;
}