Subversion Repositories mdb

Rev

Rev 18 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 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 <ctype.h>
22
#include <math.h>
23
#include <syslog.h>
24
#include <errno.h>
25
#include <sys/stat.h>
26
#include <sys/types.h>
27
#include <fcntl.h>
28
#include <sqlite3.h>
29
#include "config.h"
30
#include "helplib.h"
31
#include "user.h"
32
 
8 andreas 33
/* Global variables */
34
USERS *userchain = NULL;
7 andreas 35
 
8 andreas 36
/* Prototypes */
37
USERS *addUser();
38
 
7 andreas 39
int createUser(int s1, char *user, char *playlist)
40
{
41
	char query[1024];
42
	char fname[256];
43
	sqlite3 *db;
14 andreas 44
	int rc;
7 andreas 45
	sqlite3_stmt *res;
29 andreas 46
	USERS *neu;
7 andreas 47
 
48
	strcpy(fname, configs.home);
9 andreas 49
	strcat(fname, MUSICDB);
7 andreas 50
 
51
	rc = sqlite3_open(fname, &db);
52
 
53
	if (rc)
54
	{
55
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
56
		strcpy(query, "ERROR:USER:Error opening database;");
57
		write (s1, query, strlen(query));
58
		return FALSE;
59
	}
60
 
29 andreas 61
	sprintf(query, "select id, uname from \"main\".\"users\" where uname = \"%s\" and playlist = \"%s\"", user, playlist);
7 andreas 62
 
8 andreas 63
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 64
	{
65
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
66
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
67
		write(s1, query, strlen(query));
68
		return FALSE;
69
	}
70
 
71
	rc = sqlite3_step(res);
72
 
73
	if (rc == SQLITE_ROW)			/* If we've a row, the user exists and we'll not add it again */
74
	{
75
		sqlite3_finalize(res);
76
		sqlite3_close(db);
77
		return TRUE;
78
	}
79
 
80
	sprintf(query, "insert into \"users\" (uname, playlist) values (\"%s\", \"%s\")", user, playlist);
81
 
8 andreas 82
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 83
	{
84
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
85
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
86
		write(s1, query, strlen(query));
87
		return FALSE;
88
	}
89
 
90
	rc = sqlite3_step(res);
91
	sqlite3_finalize(res);
92
	sqlite3_close(db);
29 andreas 93
	scanUser(s1, user);
7 andreas 94
	return TRUE;
95
}
96
 
8 andreas 97
USERS *addUser()
7 andreas 98
{
8 andreas 99
	if (userchain == NULL)
7 andreas 100
	{
8 andreas 101
		if ((userchain = (USERS *)malloc(sizeof(USERS))) == NULL)
7 andreas 102
		{
8 andreas 103
			syslog(LOG_DAEMON, "Error allocating %ld bytes for a user: %s", sizeof(USERS), strerror(errno));
7 andreas 104
			return NULL;
105
		}
106
 
8 andreas 107
		memset(userchain, 0, sizeof(USERS));
108
		return userchain;
7 andreas 109
	}
110
	else
111
	{
14 andreas 112
		USERS *neu, *act;
7 andreas 113
 
14 andreas 114
		if ((neu = (USERS *)malloc(sizeof(USERS))) == NULL)
7 andreas 115
		{
8 andreas 116
			syslog(LOG_DAEMON, "Error allocating %ld bytes for a user: %s", sizeof(USERS), strerror(errno));
7 andreas 117
			return NULL;
118
		}
119
 
14 andreas 120
		memset(neu, 0, sizeof(USERS));
7 andreas 121
		/* Find last structure in chain */
8 andreas 122
		act = userchain;
7 andreas 123
 
124
		while (act->next)
125
			act = act->next;
126
 
14 andreas 127
		act->next = neu;
128
		return neu;
7 andreas 129
	}
130
}
131
 
8 andreas 132
void deleteUser()
133
{
134
	USERS *act;
135
 
136
	act = userchain;
137
 
138
	while (act)
139
	{
140
		userchain = act->next;
141
		free(act);
142
		act = userchain;
143
	}
144
 
145
	userchain = NULL;
146
}
147
 
9 andreas 148
int selectUser (int s1, const char *user)
7 andreas 149
{
12 andreas 150
	char query[1024], hv0[128];
7 andreas 151
	char fname[256];
152
	sqlite3 *db;
14 andreas 153
	int rc, total;
7 andreas 154
	sqlite3_stmt *res;
155
 
156
	strcpy(fname, configs.home);
9 andreas 157
	strcat(fname, MUSICDB);
7 andreas 158
 
159
	rc = sqlite3_open(fname, &db);
160
 
161
	if (rc)
162
	{
163
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
164
		strcpy(query, "ERROR:USER:Error opening database;");
165
		write (s1, query, strlen(query));
166
		return FALSE;
167
	}
8 andreas 168
 
169
	deleteUser();
12 andreas 170
	/* First count the future result set */
18 andreas 171
	sprintf (query, "select count(*) as cnt from users where uname = \"%s\"", user);
12 andreas 172
 
173
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
174
	{
175
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
176
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
177
		write(s1, query, strlen(query));
18 andreas 178
		total = 0;
12 andreas 179
	}
18 andreas 180
	else if (sqlite3_step(res) == SQLITE_ROW)
12 andreas 181
		total = sqlite3_column_int(res, 0);
182
	else
183
		total = 0;
184
 
185
	sprintf(hv0, "USER:%s;TOTAL:%d;", user, total);
186
	write(s1, hv0, strlen(hv0));
187
	sqlite3_finalize(res);
7 andreas 188
	sprintf(query, "select id, uname, playlist from \"main\".\"users\" where uname = \"%s\"", user);
189
 
8 andreas 190
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 191
	{
192
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
193
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
194
		write(s1, query, strlen(query));
195
		return FALSE;
196
	}
197
 
198
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
199
	{
8 andreas 200
		USERS *act;
7 andreas 201
 
8 andreas 202
		if ((act = addUser()) == NULL)
7 andreas 203
		{
204
			sqlite3_finalize(res);
205
			sqlite3_close(db);
206
			return FALSE;
207
		}
208
 
209
		act->id = sqlite3_column_int(res, 0);
8 andreas 210
		strncpy(act->uname, (const char *)sqlite3_column_text(res, 1), sizeof(act->uname));
211
		strncpy(act->playlist, (const char *)sqlite3_column_text(res, 2), sizeof(act->playlist));
7 andreas 212
	}
213
 
214
	sqlite3_finalize(res);
215
	sqlite3_close(db);
8 andreas 216
 
7 andreas 217
	return TRUE;
218
}
8 andreas 219
 
29 andreas 220
void scanUser(int s1, char *user)
221
{
222
	char query[1024], hv0[128];
223
	char fname[256];
224
	sqlite3 *db;
225
	int rc;
226
	sqlite3_stmt *res;
227
	USERS *act;
228
 
229
	if (user == NULL)
230
		return;
231
 
232
	deleteUser();
233
 
234
	strcpy(fname, configs.home);
235
	strcat(fname, MUSICDB);
236
 
237
	if ((rc = sqlite3_open(fname, &db)) != SQLITE_OK)
238
	{
239
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
240
		strcpy(hv0, "ERROR:USER:Error opening database;");
241
		write (s1, hv0, strlen(hv0));
242
		return;
243
	}
244
 
245
	sprintf(query, "select id, uname, playlist from \"main\".\"users\" where uname = \"%s\"", user);
246
 
247
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
248
	{
249
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
250
		strcpy(hv0, "ERROR:USER:Error preparing a SQL statement;");
251
		write(s1, hv0, strlen(hv0));
252
	}
253
 
254
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
255
	{
256
		if ((act = addUser()) == NULL)
257
		{
258
			sqlite3_finalize(res);
259
			sqlite3_close(db);
260
			return;
261
		}
262
 
263
		act->id = sqlite3_column_int(res, 0);
264
		strncpy(act->uname, (const char *)sqlite3_column_text(res, 1), sizeof(act->uname));
265
		strncpy(act->playlist, (const char *)sqlite3_column_text(res, 2), sizeof(act->playlist));
266
	}
267
 
268
	sqlite3_finalize(res);
269
	sqlite3_close(db);
270
}
271
 
14 andreas 272
USERS *findPlaylist(char *user, char *playlist)
8 andreas 273
{
274
	USERS *act;
275
 
276
	act = userchain;
277
 
278
	while (act)
279
	{
14 andreas 280
		if (!strcmp(act->uname, user) && !strcmp(act->playlist, playlist))
8 andreas 281
			return act;
282
 
283
		act = act->next;
284
	}
285
 
286
	return NULL;
287
}
288