Subversion Repositories mdb

Rev

Rev 7 | Rev 9 | 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;
44
	char *zErrMsg = 0;
45
	int rc, id;
46
	sqlite3_stmt *res;
47
 
48
	strcpy(fname, configs.home);
49
	strcat(fname, "/music.db");
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
 
61
	sprintf(query, "select id, uname from \"main\".\"users\" where uname = \"%s\"", user);
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);
93
	return TRUE;
94
}
95
 
8 andreas 96
USERS *addUser()
7 andreas 97
{
8 andreas 98
	if (userchain == NULL)
7 andreas 99
	{
8 andreas 100
		if ((userchain = (USERS *)malloc(sizeof(USERS))) == NULL)
7 andreas 101
		{
8 andreas 102
			syslog(LOG_DAEMON, "Error allocating %ld bytes for a user: %s", sizeof(USERS), strerror(errno));
7 andreas 103
			return NULL;
104
		}
105
 
8 andreas 106
		memset(userchain, 0, sizeof(USERS));
107
		return userchain;
7 andreas 108
	}
109
	else
110
	{
8 andreas 111
		USERS *new, *act;
7 andreas 112
 
8 andreas 113
		if ((new = (USERS *)malloc(sizeof(USERS))) == NULL)
7 andreas 114
		{
8 andreas 115
			syslog(LOG_DAEMON, "Error allocating %ld bytes for a user: %s", sizeof(USERS), strerror(errno));
7 andreas 116
			return NULL;
117
		}
118
 
8 andreas 119
		memset(new, 0, sizeof(USERS));
7 andreas 120
		/* Find last structure in chain */
8 andreas 121
		act = userchain;
7 andreas 122
 
123
		while (act->next)
124
			act = act->next;
125
 
126
		act->next = new;
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
 
7 andreas 146
int selectUser (int s1, char *user)
147
{
148
	char query[1024];
149
	char fname[256];
150
	sqlite3 *db;
151
	char *uname, *playlist, *zErrMsg = 0;
8 andreas 152
	int rc, id, total;
7 andreas 153
	sqlite3_stmt *res;
154
 
155
	strcpy(fname, configs.home);
156
	strcat(fname, "/music.db");
157
 
158
	rc = sqlite3_open(fname, &db);
159
 
160
	if (rc)
161
	{
162
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
163
		strcpy(query, "ERROR:USER:Error opening database;");
164
		write (s1, query, strlen(query));
165
		return FALSE;
166
	}
8 andreas 167
 
168
	deleteUser();
7 andreas 169
	sprintf(query, "select id, uname, playlist from \"main\".\"users\" where uname = \"%s\"", user);
170
 
8 andreas 171
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
7 andreas 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));
176
		return FALSE;
177
	}
178
 
8 andreas 179
	total = 0;
180
 
7 andreas 181
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
182
	{
8 andreas 183
		USERS *act;
7 andreas 184
 
8 andreas 185
		if ((act = addUser()) == NULL)
7 andreas 186
		{
187
			sqlite3_finalize(res);
188
			sqlite3_close(db);
189
			return FALSE;
190
		}
191
 
8 andreas 192
		total++;
7 andreas 193
		act->id = sqlite3_column_int(res, 0);
8 andreas 194
		strncpy(act->uname, (const char *)sqlite3_column_text(res, 1), sizeof(act->uname));
195
		strncpy(act->playlist, (const char *)sqlite3_column_text(res, 2), sizeof(act->playlist));
7 andreas 196
	}
197
 
198
	sqlite3_finalize(res);
199
	sqlite3_close(db);
8 andreas 200
 
201
	sprintf(query, "PTOTAL:%s:%d;", user, total);
202
	write(s1, query, strlen(query));
7 andreas 203
	return TRUE;
204
}
8 andreas 205
 
206
USERS *findPlaylist(char *playlist)
207
{
208
	USERS *act;
209
 
210
	act = userchain;
211
 
212
	while (act)
213
	{
214
		if (strcmp(act->playlist, playlist) == 0)
215
			return act;
216
 
217
		act = act->next;
218
	}
219
 
220
	return NULL;
221
}
222