Subversion Repositories mdb

Rev

Rev 25 | Rev 31 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 */
16
#include <stdio.h>
17
#include <string.h>
18
#include <strings.h>
19
#include <unistd.h>
20
#include <stdlib.h>
21
#include <libgen.h>
22
#include <ctype.h>
23
#include <math.h>
24
#include <syslog.h>
25
#include <errno.h>
26
#include <sys/stat.h>
27
#include <sys/types.h>
28
#include <fcntl.h>
29
#include <sqlite3.h>
30
#include <id3.h>
31
#include "config.h"
32
#include "helplib.h"
33
#include "list.h"
8 andreas 34
#include "user.h"
35
#include "play.h"
5 andreas 36
 
37
struct callPars
38
{
39
	int s1;
40
	int start;
41
	int length;
42
	char *type;
43
};
44
 
12 andreas 45
int globalLine;
46
int globalPos;
5 andreas 47
 
12 andreas 48
static int listCallback(int s1, char *type, int line, sqlite3_stmt *res);
5 andreas 49
static int folderCallback(void *hint, int argc, char **argv, char **azColName);
14 andreas 50
int countPlaylists();
5 andreas 51
 
52
int listSongs(int s1, char *p_type, int start, int length)
53
{
11 andreas 54
	char query[1024], hv0[128];
5 andreas 55
	char fname[256];
56
	sqlite3 *db;
12 andreas 57
	sqlite3_stmt *res;
58
	int rc, pos, line;
5 andreas 59
 
60
	strcpy(fname, configs.home);
9 andreas 61
	strcat(fname, MUSICDB);
5 andreas 62
 
63
	rc = sqlite3_open(fname, &db);
64
 
65
	if (rc)
66
	{
67
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
68
		strcpy(query, "ERROR:LIST:Error opening database;");
69
		write (s1, query, strlen(query));
70
		return FALSE;
71
	}
72
 
12 andreas 73
	/* First count the future result set */
14 andreas 74
	if (strcmp(p_type, "QUEUE") && strcmp(p_type, "PLAYLIST"))
75
	{
76
		strcpy (query, "select count(*) as cnt from musicdb");
12 andreas 77
 
14 andreas 78
		if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
79
		{
80
			syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
81
			strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
82
			write(s1, query, strlen(query));
83
			return FALSE;
84
		}
85
 
86
		if (sqlite3_step(res) == SQLITE_ROW)
87
			pos = sqlite3_column_int(res, 0);
88
		else
89
			pos = 0;
90
 
91
		sqlite3_finalize(res);
12 andreas 92
	}
14 andreas 93
	else if (!strcmp(p_type, "QUEUE"))
94
		pos = queueTotal;
12 andreas 95
	else
14 andreas 96
		pos = countPlaylists();
12 andreas 97
 
98
	sprintf(hv0, "TOTAL:%d;", pos);
99
	write(s1, hv0, strlen(hv0));
100
 
11 andreas 101
	strcpy (query, "select id, title, interpret, album, genre, cover from \"main\".\"musicdb\" ");
5 andreas 102
	strcat (query, "order by ");
103
 
104
	if (strcmp(p_type, "TITLE") == 0)
105
		strcat (query, "title");
106
	else if (strcmp(p_type, "ARTIST") == 0)
107
		strcat (query, "interpret");
108
	else if (strcmp(p_type, "ALBUM") == 0)
109
		strcat (query, "album");
110
	else if (strcmp(p_type, "GENRE") == 0)
111
		strcat (query, "genre");
8 andreas 112
	else if (strcmp(p_type, "QUEUE") == 0)
113
	{
114
		sqlite3_close(db);
115
		return listQueue(s1, start, length);
116
	}
117
	else if (strcmp(p_type, "PLAYLIST") == 0)
118
	{
119
		sqlite3_close(db);
120
		return listPlaylists(s1, start, length);
121
	}
24 andreas 122
	else if (strcmp(p_type, "USERS") == 0)
123
	{
124
		sqlite3_close(db);
125
		return listUsers(s1, start, length);
126
	}
5 andreas 127
	else		/* No or unknown type */
128
	{
129
		strcpy (query, "ERROR:LIST:Missing type;");
130
		write(s1, query, strlen(query));
131
		sqlite3_close(db);
132
		return FALSE;
133
	}
134
 
11 andreas 135
	/* Tell client which page to show */
136
	sprintf(hv0, "PAGE:%s;", p_type);
137
	write (s1, hv0, strlen (hv0));
138
 
139
	/* Retrieve data from database */
25 andreas 140
	line = pos = 0;
5 andreas 141
 
12 andreas 142
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
5 andreas 143
	{
12 andreas 144
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
145
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
146
		write(s1, query, strlen(query));
5 andreas 147
		return FALSE;
148
	}
149
 
12 andreas 150
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
5 andreas 151
	{
25 andreas 152
		pos++;
12 andreas 153
 
25 andreas 154
		if (pos >= start && pos < (start + length))
155
		{
156
			line++;
12 andreas 157
			listCallback(s1, p_type, line, res);
25 andreas 158
		}
12 andreas 159
 
25 andreas 160
		if (pos >= (start + length))
12 andreas 161
			break;
5 andreas 162
	}
163
 
12 andreas 164
	sqlite3_finalize(res);
5 andreas 165
	sqlite3_close(db);
166
	return TRUE;
167
}
168
 
8 andreas 169
int listQueue(int s1, int start, int length)
170
{
11 andreas 171
	int fd, line;
172
	char fname[256], hv0[256], buffer[8192];
173
	char *title, *artist, *album, *cover;
174
	int pos;
175
	QUEUE *act;
8 andreas 176
 
11 andreas 177
	if (pqueue == NULL)
8 andreas 178
	{
11 andreas 179
		strcpy (fname, configs.home);
180
		strcat (fname, NOWPLAY);
8 andreas 181
 
11 andreas 182
		if (access(fname, R_OK))
183
		{
184
			strcpy (hv0, "ERROR:LIST:No or empty queue;");
185
			write (s1, hv0, strlen(hv0));
186
			return FALSE;
187
		}
8 andreas 188
 
11 andreas 189
		if ((fd = open(fname, O_RDONLY)) <= 0)
190
		{
191
			syslog(LOG_WARNING, "Error opening file %s: %s", fname, strerror(errno));
192
			strcpy(hv0, "ERROR:LIST:Error opening queue;");
193
			write(s1, hv0, strlen(hv0));
194
			return FALSE;
195
		}
8 andreas 196
 
11 andreas 197
		scanQueue(fd);
198
		close(fd);
199
	}
8 andreas 200
 
11 andreas 201
	sprintf(hv0, "PAGE:QUEUE;TOTAL:%d;", queueTotal);
202
	write (s1, hv0, strlen(hv0));
203
	act = pqueue;
204
	pos = line = 1;
8 andreas 205
 
11 andreas 206
	while (act)
8 andreas 207
	{
208
		if (pos < start)
25 andreas 209
		{
210
			act = act->next;
211
			pos++;
8 andreas 212
			continue;
25 andreas 213
		}
214
		else if (pos >= (start + length))
8 andreas 215
			break;
216
 
11 andreas 217
		title = urlencode(act->title);
218
		artist = urlencode(act->artist);
219
		album = urlencode(act->album);
220
		cover = urlencode(act->cover);
221
		sprintf(buffer, "LINE:QUEUE:%d:%d:", act->id, line);
8 andreas 222
 
223
		if (title != NULL)
224
		{
225
			strcat(buffer, title);
226
			free(title);
227
		}
228
		else
11 andreas 229
			strcat(buffer, act->title);
230
 
8 andreas 231
		strcat(buffer, ":");
11 andreas 232
 
8 andreas 233
		if (artist != NULL)
234
		{
235
			strcat(buffer, artist);
236
			free(artist);
237
		}
238
		else
11 andreas 239
			strcat(buffer, act->artist);
240
 
8 andreas 241
		strcat(buffer, ":");
11 andreas 242
 
8 andreas 243
		if (album != NULL)
244
		{
245
			strcat(buffer, album);
246
			free(album);
247
		}
248
		else
11 andreas 249
			strcat(buffer, act->album);
250
 
8 andreas 251
		strcat(buffer, ":");
11 andreas 252
		strcat(buffer, act->genre);
253
		strcat(buffer, ":");
254
 
255
		if (cover != NULL)
256
		{
257
			strcat(buffer, cover);
258
			free(cover);
259
		}
260
		else
261
			strcat(buffer, act->cover);
262
 
8 andreas 263
		strcat(buffer, ";");
264
		write(s1, buffer, strlen(buffer));
11 andreas 265
		act = act->next;
25 andreas 266
		pos++;
11 andreas 267
		line++;
8 andreas 268
	}
269
 
270
	return TRUE;
271
}
272
 
5 andreas 273
int listFolders(int s1, char *p_type, int start, int length)
274
{
275
	char query[1024], field[16];
276
	char fname[256];
277
	sqlite3 *db;
278
	char *zErrMsg = 0;
279
	int rc;
280
	struct callPars cp;
281
 
282
	strcpy(fname, configs.home);
9 andreas 283
	strcat(fname, MUSICDB);
5 andreas 284
 
285
	rc = sqlite3_open(fname, &db);
286
 
287
	if (rc)
288
	{
289
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
290
		strcpy(query, "ERROR:FOLDER:Error opening database;");
291
		write (s1, query, strlen(query));
292
		return FALSE;
293
	}
294
 
295
	strcpy (query, "select distinct ");
296
 
297
	if (strcmp(p_type, "TITLE") == 0)
298
		strcpy (field, "title");
299
	else if (strcmp(p_type, "ARTIST") == 0)
300
		strcpy (field, "interpret");
301
	else if (strcmp(p_type, "ALBUM") == 0)
302
		strcpy (field, "album");
303
	else if (strcmp(p_type, "GENRE") == 0)
304
		strcpy (field, "genre");
305
	else		/* No or unknown type */
306
	{
307
		strcpy (query, "ERROR:FOLDER:Missing type;");
308
		write(s1, query, strlen(query));
309
		sqlite3_close(db);
310
		return FALSE;
311
	}
312
 
313
	strcat (query, field);
25 andreas 314
	strcat (query, " from \"main\".\"musicdb\" order by ");
5 andreas 315
	strcat (query, field);
316
 
317
	cp.s1 = s1;
318
	cp.type = p_type;
319
	cp.start = start;
320
	cp.length = length;
12 andreas 321
	globalLine = 0;
25 andreas 322
	globalPos = 0;
5 andreas 323
 
324
	if ((rc = sqlite3_exec(db, query, folderCallback, (void *)&cp, &zErrMsg)) != SQLITE_OK)
325
	{
326
		syslog(LOG_WARNING, "SQL error [%s]: %s", query, zErrMsg);
327
		sqlite3_free(zErrMsg);
328
		sqlite3_close(db);
329
		strcpy(query, "ERROR:FOLDER:SQL error;");
330
		write (s1, query, strlen(query));
331
		return FALSE;
332
	}
333
 
12 andreas 334
	if (globalPos >= 0)
5 andreas 335
	{
12 andreas 336
		sprintf(query, "TOTAL:%d;", globalPos + 1);
5 andreas 337
		write (s1, query, strlen(query));
338
	}
339
 
340
	sqlite3_close(db);
341
	return TRUE;
342
}
343
 
12 andreas 344
static int listCallback(int s1, char *type, int line, sqlite3_stmt *res)
5 andreas 345
{
14 andreas 346
	int id;
12 andreas 347
	char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256], id3_cover[256];
5 andreas 348
	char buffer[8192];
12 andreas 349
	char *title, *artist, *album;
5 andreas 350
 
351
	memset(id3_title, 0, sizeof(id3_title));
352
	memset(id3_artist, 0, sizeof(id3_artist));
353
	memset(id3_album, 0, sizeof(id3_album));
354
	memset(id3_genre, 0, sizeof(id3_genre));
11 andreas 355
	memset(id3_cover, 0, sizeof(id3_cover));
5 andreas 356
	id = -1;
12 andreas 357
	id = sqlite3_column_int(res, 0);
358
	strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
359
	strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
360
	strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
361
	strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
362
	strncpy(id3_cover, (const char *)sqlite3_column_text(res, 5), sizeof(id3_cover));
5 andreas 363
 
364
	title = urlencode(id3_title);
365
	artist = urlencode(id3_artist);
11 andreas 366
	album = urlencode(id3_album);
5 andreas 367
 
12 andreas 368
	sprintf(buffer, "LINE:%s:%d:%d:", type, id, line);
5 andreas 369
 
370
	if (title != NULL)
371
	{
372
		strcat(buffer, title);
373
		free(title);
374
	}
375
	else
376
		strcat(buffer, id3_title);
12 andreas 377
 
5 andreas 378
	strcat(buffer, ":");
12 andreas 379
 
5 andreas 380
	if (artist != NULL)
381
	{
382
		strcat(buffer, artist);
383
		free(artist);
384
	}
385
	else
386
		strcat(buffer, id3_artist);
12 andreas 387
 
5 andreas 388
	strcat(buffer, ":");
12 andreas 389
 
5 andreas 390
	if (album != NULL)
391
	{
392
		strcat(buffer, album);
393
		free(album);
394
	}
395
	else
396
		strcat(buffer, id3_album);
12 andreas 397
 
5 andreas 398
	strcat(buffer, ":");
399
	strcat(buffer, id3_genre);
11 andreas 400
	strcat(buffer, ":");
12 andreas 401
	strcat(buffer, id3_cover);
5 andreas 402
	strcat(buffer, ";");
12 andreas 403
	write(s1, buffer, strlen(buffer));
5 andreas 404
	return 0;
405
}
406
 
407
static int folderCallback(void *hint, int argc, char **argv, char **azColName)
408
{
409
	int i;
25 andreas 410
	char id3_buffer[256];
5 andreas 411
	char buffer[8192];
412
	char *buf;
413
	struct callPars *cp;
414
 
12 andreas 415
	globalPos++;
5 andreas 416
	cp = (struct callPars *)hint;
417
	memset(id3_buffer, 0, sizeof(id3_buffer));
418
 
12 andreas 419
	if (globalPos < cp->start || globalPos >= (cp->start + cp->length))
5 andreas 420
		return 0;
421
 
422
	for(i = 0; i < argc; i++)
423
	{
424
		if (strcasecmp(azColName[i], "title") == 0 || strcasecmp(azColName[i], "interpret") == 0 ||
425
			strcasecmp(azColName[i], "album") == 0 || strcasecmp(azColName[i], "genre") == 0)
426
			if (argv[i])
427
				strncpy(id3_buffer, argv[i], sizeof(id3_buffer));
428
	}
429
 
430
	buf = urlencode(id3_buffer);
12 andreas 431
	globalLine++;
5 andreas 432
 
12 andreas 433
	sprintf(buffer, "FOLDER:%s:%d:", cp->type, globalLine);
5 andreas 434
 
435
	if (buf != NULL)
436
	{
437
		strcat(buffer, buf);
438
		free(buf);
439
	}
440
	else
441
		strcat(buffer, id3_buffer);
442
 
443
	strcat(buffer, ";");
444
	write(cp->s1, buffer, strlen(buffer));
445
	return 0;
446
}
8 andreas 447
 
13 andreas 448
int listFolderContent(int s1, char *p_type, char *name, int start, int length)
449
{
450
	char query[1024], hv0[128], field[32];
451
	char fname[256];
452
	sqlite3 *db;
453
	sqlite3_stmt *res;
454
	int rc, pos, line;
455
 
456
	strcpy(fname, configs.home);
457
	strcat(fname, MUSICDB);
458
 
459
	if (!strcasecmp(p_type, "TITLE"))
460
		strcpy (field, "title");
461
	else if (!strcasecmp(p_type, "ARTIST"))
462
		strcpy (field, "interpret");
463
	else if (!strcasecmp(p_type, "ALBUM"))
464
		strcpy (field, "album");
465
	else if (!strcasecmp(p_type, "GENRE"))
466
		strcpy (field, "genre");
467
	else
468
	{
469
		strcpy (hv0, "ERROR:LIST:Missing valid type;");
470
		write(s1, hv0, strlen(hv0));
471
		return FALSE;
472
	}
473
 
474
	rc = sqlite3_open(fname, &db);
475
 
476
	if (rc)
477
	{
478
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
479
		strcpy(query, "ERROR:LIST:Error opening database;");
480
		write (s1, query, strlen(query));
481
		return FALSE;
482
	}
483
 
484
	/* First count the future result set */
485
	sprintf (query, "select count(*) as cnt from musicdb where %s = \"%s\"", field, name);
486
 
487
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
488
	{
489
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
490
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
491
		write(s1, query, strlen(query));
492
		return FALSE;
493
	}
494
 
495
	if (sqlite3_step(res) == SQLITE_ROW)
496
		pos = sqlite3_column_int(res, 0);
497
	else
498
		pos = 0;
499
 
500
	sprintf(hv0, "TOTAL:%d;", pos);
501
	write(s1, hv0, strlen(hv0));
502
	sqlite3_finalize(res);
503
 
504
	sprintf (query, "select id, title, interpret, album, genre, cover from \"main\".\"musicdb\" where %s = \"%s\" order by title", field, name);
505
 
506
	/* Tell client which page to show */
25 andreas 507
	sprintf(hv0, "PAGE:%s;FOLDERNAME:%s;", p_type, name);
13 andreas 508
	write (s1, hv0, strlen (hv0));
509
 
510
	/* Retrieve data from database */
29 andreas 511
	line = pos = 1;
13 andreas 512
 
513
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
514
	{
515
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
516
		strcpy(query, "ERROR:LIST:Error preparing a SQL statement;");
517
		write(s1, query, strlen(query));
518
		return FALSE;
519
	}
520
 
521
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
522
	{
29 andreas 523
		if (pos >= start && pos < (start + length))
524
		{
13 andreas 525
			listCallback(s1, p_type, line, res);
29 andreas 526
			line++;
527
		}
13 andreas 528
 
29 andreas 529
		if (pos >= (start + length))
13 andreas 530
			break;
29 andreas 531
 
532
		pos++;
13 andreas 533
	}
534
 
535
	sqlite3_finalize(res);
536
	sqlite3_close(db);
537
	return TRUE;
538
}
539
 
8 andreas 540
/*
14 andreas 541
 * Count the playlists of the users and return the result.
542
 */
543
int countPlaylists()
544
{
545
	USERS *act;
546
	int count;
547
 
548
	count = 0;
549
	act = userchain;
550
 
551
	while (act)
552
	{
553
		count++;
554
		act = act->next;
555
	}
556
 
557
	return count;
558
}
559
 
560
/*
8 andreas 561
 * List the playlists of the current user.
562
 */
563
int listPlaylists(int s1, int start, int length)
564
{
565
	USERS *act;
566
	char hv0[512], *user, *playlist;
29 andreas 567
	int pos, line;
8 andreas 568
 
569
	if (userchain == NULL)
570
	{
571
		strcpy(hv0, "ERROR:PLAYLIST:No user selected;");
572
		write(s1, hv0, strlen(hv0));
573
		return FALSE;
574
	}
575
 
576
	act = userchain;
29 andreas 577
	pos = line = 1;
8 andreas 578
 
579
	while (act)
580
	{
581
		if (pos < start)
582
		{
583
			act = act->next;
584
			continue;
585
		}
586
 
9 andreas 587
		if (pos >= (start + length))
8 andreas 588
			break;
589
 
590
		user = urlencode(act->uname);
591
		playlist = urlencode(act->playlist);
29 andreas 592
		sprintf(hv0, "PLAYLIST:%d:%d:%s:%s;", line, act->id, user, playlist);
9 andreas 593
		write (s1, hv0, strlen(hv0));
8 andreas 594
 
595
		if (user)
596
			free (user);
597
 
598
		if (playlist)
599
			free (playlist);
600
 
601
		act = act->next;
29 andreas 602
		line++;
8 andreas 603
	}
604
 
605
	return TRUE;
606
}
9 andreas 607
 
608
int listUserPlaylist(int s1, const char *user, const char *playlist, int start, int length)
609
{
610
	char query[1024], hv0[128], buffer[8192];
611
	char fname[256];
612
	sqlite3 *db;
613
	int rc, id, total, pos, line;
614
	sqlite3_stmt *res;
11 andreas 615
	char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256], id3_cover[512];
14 andreas 616
	char *title, *artist, *album, *cover;
9 andreas 617
	USERS *act;
618
 
619
	if (playlist == NULL)
620
	{
621
		strcpy(hv0, "ERROR:LIST:Missing name of playlist;");
622
		write (s1, hv0, strlen(hv0));
623
		return FALSE;
624
	}
625
 
626
	/* Check current user */
627
	if (user != NULL)
628
	{
629
		if (userchain == NULL || strcmp(userchain->uname, user))
630
		{
631
			if (!selectUser(s1, user))
632
				return FALSE;
633
		}
634
	}
635
 
636
	/* Find the playlist */
637
	act = userchain;
638
 
639
	while (act)
640
	{
641
		if (!strcmp(act->playlist, playlist))
642
			break;
643
 
644
		act = act->next;
645
	}
646
 
647
	if (act == NULL)
648
	{
649
		strcpy(hv0, "ERROR:LIST:Playlist not found;");
650
		write (s1, hv0, strlen(hv0));
651
		return FALSE;
652
	}
653
 
654
	strcpy(fname, configs.home);
655
	strcat(fname, MUSICDB);
656
 
657
	rc = sqlite3_open(fname, &db);
658
 
659
	if (rc)
660
	{
661
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
662
		strcpy(query, "ERROR:USER:Error opening database;");
663
		write (s1, query, strlen(query));
664
		return FALSE;
665
	}
14 andreas 666
 
29 andreas 667
	sprintf(query, "select count(*) from \"main\".\"playlists\" where userid = %d", act->id);
668
 
14 andreas 669
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
670
	{
671
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
672
		strcpy(hv0, "ERROR:USER:Error preparing a SQL statement;");
673
		write(s1, hv0, strlen(hv0));
674
		return FALSE;
675
	}
676
 
677
	if ((rc = sqlite3_step(res)) == SQLITE_ROW)
678
	{
679
		total = sqlite3_column_int(res, 0);
680
		sprintf(hv0, "TOTAL:%d;", total);
681
		write (s1, hv0, strlen(hv0));
682
	}
683
 
684
	sqlite3_finalize(res);
11 andreas 685
	strcpy (query, "select id, title, interpret, album, genre, cover from musicdb as a where ");
9 andreas 686
	strcat (query, "(select musicid from playlists as b where a.id = b.musicid and b.userid = ");
687
	sprintf(hv0, "%d", act->id);
688
	strcat (query, hv0);
689
	strcat (query, ")");
690
 
691
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
692
	{
693
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
694
		strcpy(hv0, "ERROR:USER:Error preparing a SQL statement;");
695
		write(s1, hv0, strlen(hv0));
696
		return FALSE;
697
	}
698
 
699
	pos = 1;
700
	line = 0;
701
 
702
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
703
	{
704
		if (pos < start)
705
		{
706
			pos++;
707
			continue;
708
		}
709
 
710
		if (pos >= (start + length))
711
			break;
712
 
713
		line++;
714
		memset(id3_title, 0, sizeof(id3_title));
715
		memset(id3_artist, 0, sizeof(id3_artist));
716
		memset(id3_album, 0, sizeof(id3_album));
717
		memset(id3_genre, 0, sizeof(id3_genre));
11 andreas 718
		memset(id3_cover, 0, sizeof(id3_cover));
9 andreas 719
		id = sqlite3_column_int(res, 0);
720
		strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
721
		strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
722
		strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
723
		strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
11 andreas 724
		strncpy(id3_cover, (const char *)sqlite3_column_text(res, 5), sizeof(id3_cover));
9 andreas 725
		title = urlencode(id3_title);
726
		artist = urlencode(id3_artist);
727
		album = urlencode(id3_album);
11 andreas 728
		cover = urlencode(id3_cover);
729
 
9 andreas 730
		if (title != NULL)
731
		{
732
			strncpy(id3_title, title, sizeof(id3_title));
733
			free(title);
734
		}
11 andreas 735
 
9 andreas 736
		if (artist != NULL)
737
		{
738
			strncpy(id3_artist, artist, sizeof(id3_artist));
739
			free(artist);
740
		}
11 andreas 741
 
9 andreas 742
		if (album != NULL)
743
		{
744
			strncpy(id3_album, album, sizeof(id3_album));
745
			free(album);
746
		}
11 andreas 747
 
748
		if (cover != NULL)
749
		{
750
			strncpy(id3_cover, cover, sizeof(id3_cover));
751
			free(cover);
752
		}
753
 
754
		sprintf (buffer, "LINE:PLAYLIST:%d:%d:%s:%s:%s:%s:%s;", id, line, id3_title, id3_artist, id3_album, id3_genre, id3_cover);
9 andreas 755
		write (s1, buffer, strlen(buffer));
756
		pos++;
757
	}
758
 
759
	sqlite3_finalize(res);
760
	sqlite3_close(db);
761
	return TRUE;
762
}
24 andreas 763
 
764
int listUsers(int s1, int start, int length)
765
{
766
	char query[1024], hv0[256];
767
	char fname[256], uname[128], *un;
768
	sqlite3 *db;
769
	int rc, id, total, pos;
770
	sqlite3_stmt *res;
771
 
772
	rc = sqlite3_open(fname, &db);
773
 
774
	if (rc)
775
	{
776
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
777
		strcpy(query, "ERROR:LIST:Error opening database;");
778
		write (s1, query, strlen(query));
779
		return FALSE;
780
	}
781
 
782
	sprintf(query, "select distinct count(*) from users order by uname");
783
 
784
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
785
	{
786
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
787
		strcpy(hv0, "ERROR:USER:Error preparing a SQL statement;");
788
		write(s1, hv0, strlen(hv0));
789
		return FALSE;
790
	}
791
 
792
	if ((rc = sqlite3_step(res)) == SQLITE_ROW)
793
	{
794
		total = sqlite3_column_int(res, 0);
795
		sprintf(hv0, "TOTAL:%d;", total);
796
		write (s1, hv0, strlen(hv0));
797
	}
798
 
799
	sqlite3_finalize(res);
800
 
801
	strcpy (query, "select distinct uname, id from users order by users");
802
 
803
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
804
	{
805
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
806
		strcpy(hv0, "ERROR:USER:Error preparing a SQL statement;");
807
		write(s1, hv0, strlen(hv0));
808
		return FALSE;
809
	}
810
 
811
	pos = 0;
812
 
813
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
814
	{
815
		pos++;
816
 
817
		if (pos < start)
818
			continue;
819
 
820
		if (pos >= (start + length))
821
			break;
822
 
823
		strncpy(uname, (const char *)sqlite3_column_text(res, 0), sizeof(uname));
824
		id = sqlite3_column_int(res, 1);
825
		un = urlencode(uname);
826
 
827
		if (un)
828
		{
829
			strncpy(uname, un, sizeof(uname));
830
			free(un);
831
		}
832
 
833
		sprintf(hv0, "USERS:%d:%d:%s;", id, pos+1, uname);
834
		write(s1, hv0, strlen(hv0));
835
	}
836
 
837
	sqlite3_finalize(res);
838
	sqlite3_close(db);
839
	return TRUE;
840
}