Subversion Repositories mdb

Rev

Rev 14 | Go to most recent revision | 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;
46
 
47
	strcpy(fname, configs.home);
9 andreas 48
	strcat(fname, MUSICDB);
7 andreas 49
 
50
	rc = sqlite3_open(fname, &db);
51
 
52
	if (rc)
53
	{
54
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
55
		strcpy(query, "ERROR:USER:Error opening database;");
56
		write (s1, query, strlen(query));
57
		return FALSE;
58
	}
59
 
60
	sprintf(query, "select id, uname from \"main\".\"users\" where uname = \"%s\"", user);
61
 
8 andreas 62
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 63
	{
64
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
65
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
66
		write(s1, query, strlen(query));
67
		return FALSE;
68
	}
69
 
70
	rc = sqlite3_step(res);
71
 
72
	if (rc == SQLITE_ROW)			/* If we've a row, the user exists and we'll not add it again */
73
	{
74
		sqlite3_finalize(res);
75
		sqlite3_close(db);
76
		return TRUE;
77
	}
78
 
79
	sprintf(query, "insert into \"users\" (uname, playlist) values (\"%s\", \"%s\")", user, playlist);
80
 
8 andreas 81
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 82
	{
83
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
84
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
85
		write(s1, query, strlen(query));
86
		return FALSE;
87
	}
88
 
89
	rc = sqlite3_step(res);
90
	sqlite3_finalize(res);
91
	sqlite3_close(db);
92
	return TRUE;
93
}
94
 
8 andreas 95
USERS *addUser()
7 andreas 96
{
8 andreas 97
	if (userchain == NULL)
7 andreas 98
	{
8 andreas 99
		if ((userchain = (USERS *)malloc(sizeof(USERS))) == NULL)
7 andreas 100
		{
8 andreas 101
			syslog(LOG_DAEMON, "Error allocating %ld bytes for a user: %s", sizeof(USERS), strerror(errno));
7 andreas 102
			return NULL;
103
		}
104
 
8 andreas 105
		memset(userchain, 0, sizeof(USERS));
106
		return userchain;
7 andreas 107
	}
108
	else
109
	{
14 andreas 110
		USERS *neu, *act;
7 andreas 111
 
14 andreas 112
		if ((neu = (USERS *)malloc(sizeof(USERS))) == NULL)
7 andreas 113
		{
8 andreas 114
			syslog(LOG_DAEMON, "Error allocating %ld bytes for a user: %s", sizeof(USERS), strerror(errno));
7 andreas 115
			return NULL;
116
		}
117
 
14 andreas 118
		memset(neu, 0, sizeof(USERS));
7 andreas 119
		/* Find last structure in chain */
8 andreas 120
		act = userchain;
7 andreas 121
 
122
		while (act->next)
123
			act = act->next;
124
 
14 andreas 125
		act->next = neu;
126
		return neu;
7 andreas 127
	}
128
}
129
 
8 andreas 130
void deleteUser()
131
{
132
	USERS *act;
133
 
134
	act = userchain;
135
 
136
	while (act)
137
	{
138
		userchain = act->next;
139
		free(act);
140
		act = userchain;
141
	}
142
 
143
	userchain = NULL;
144
}
145
 
9 andreas 146
int selectUser (int s1, const char *user)
7 andreas 147
{
12 andreas 148
	char query[1024], hv0[128];
7 andreas 149
	char fname[256];
150
	sqlite3 *db;
14 andreas 151
	int rc, total;
7 andreas 152
	sqlite3_stmt *res;
153
 
154
	strcpy(fname, configs.home);
9 andreas 155
	strcat(fname, MUSICDB);
7 andreas 156
 
157
	rc = sqlite3_open(fname, &db);
158
 
159
	if (rc)
160
	{
161
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
162
		strcpy(query, "ERROR:USER:Error opening database;");
163
		write (s1, query, strlen(query));
164
		return FALSE;
165
	}
8 andreas 166
 
167
	deleteUser();
12 andreas 168
	/* First count the future result set */
18 andreas 169
	sprintf (query, "select count(*) as cnt from users where uname = \"%s\"", user);
12 andreas 170
 
171
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
172
	{
173
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
174
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
175
		write(s1, query, strlen(query));
18 andreas 176
		total = 0;
12 andreas 177
	}
18 andreas 178
	else if (sqlite3_step(res) == SQLITE_ROW)
12 andreas 179
		total = sqlite3_column_int(res, 0);
180
	else
181
		total = 0;
182
 
183
	sprintf(hv0, "USER:%s;TOTAL:%d;", user, total);
184
	write(s1, hv0, strlen(hv0));
185
	sqlite3_finalize(res);
7 andreas 186
	sprintf(query, "select id, uname, playlist from \"main\".\"users\" where uname = \"%s\"", user);
187
 
8 andreas 188
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 189
	{
190
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
191
		strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
192
		write(s1, query, strlen(query));
193
		return FALSE;
194
	}
195
 
196
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
197
	{
8 andreas 198
		USERS *act;
7 andreas 199
 
8 andreas 200
		if ((act = addUser()) == NULL)
7 andreas 201
		{
202
			sqlite3_finalize(res);
203
			sqlite3_close(db);
204
			return FALSE;
205
		}
206
 
207
		act->id = sqlite3_column_int(res, 0);
8 andreas 208
		strncpy(act->uname, (const char *)sqlite3_column_text(res, 1), sizeof(act->uname));
209
		strncpy(act->playlist, (const char *)sqlite3_column_text(res, 2), sizeof(act->playlist));
7 andreas 210
	}
211
 
212
	sqlite3_finalize(res);
213
	sqlite3_close(db);
8 andreas 214
 
7 andreas 215
	return TRUE;
216
}
8 andreas 217
 
14 andreas 218
USERS *findPlaylist(char *user, char *playlist)
8 andreas 219
{
220
	USERS *act;
221
 
222
	act = userchain;
223
 
224
	while (act)
225
	{
14 andreas 226
		if (!strcmp(act->uname, user) && !strcmp(act->playlist, playlist))
8 andreas 227
			return act;
228
 
229
		act = act->next;
230
	}
231
 
232
	return NULL;
233
}
234