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 |
|
|
|
33 |
struct ST_USERS *first = NULL;
|
|
|
34 |
|
|
|
35 |
int createUser(int s1, char *user, char *playlist)
|
|
|
36 |
{
|
|
|
37 |
char query[1024];
|
|
|
38 |
char fname[256];
|
|
|
39 |
sqlite3 *db;
|
|
|
40 |
char *zErrMsg = 0;
|
|
|
41 |
int rc, id;
|
|
|
42 |
sqlite3_stmt *res;
|
|
|
43 |
|
|
|
44 |
strcpy(fname, configs.home);
|
|
|
45 |
strcat(fname, "/music.db");
|
|
|
46 |
|
|
|
47 |
rc = sqlite3_open(fname, &db);
|
|
|
48 |
|
|
|
49 |
if (rc)
|
|
|
50 |
{
|
|
|
51 |
syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
|
|
|
52 |
strcpy(query, "ERROR:USER:Error opening database;");
|
|
|
53 |
write (s1, query, strlen(query));
|
|
|
54 |
return FALSE;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
sprintf(query, "select id, uname from \"main\".\"users\" where uname = \"%s\"", user);
|
|
|
58 |
|
|
|
59 |
if (sqlite3_prepare(db, query, -1, res, NULL) != SQLITE_OK)
|
|
|
60 |
{
|
|
|
61 |
syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
|
|
|
62 |
strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
|
|
|
63 |
write(s1, query, strlen(query));
|
|
|
64 |
return FALSE;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
rc = sqlite3_step(res);
|
|
|
68 |
|
|
|
69 |
if (rc == SQLITE_ROW) /* If we've a row, the user exists and we'll not add it again */
|
|
|
70 |
{
|
|
|
71 |
sqlite3_finalize(res);
|
|
|
72 |
sqlite3_close(db);
|
|
|
73 |
return TRUE;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
sprintf(query, "insert into \"users\" (uname, playlist) values (\"%s\", \"%s\")", user, playlist);
|
|
|
77 |
|
|
|
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 |
rc = sqlite3_step(res);
|
|
|
87 |
sqlite3_finalize(res);
|
|
|
88 |
sqlite3_close(db);
|
|
|
89 |
return TRUE;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
ST_USERS *addUser()
|
|
|
93 |
{
|
|
|
94 |
if (first == NULL)
|
|
|
95 |
{
|
|
|
96 |
if ((first = (ST_USERS *)malloc(sizeof(ST_USERS))) == NULL)
|
|
|
97 |
{
|
|
|
98 |
syslog(LOG_DAEMON, "Error allocating %d bytes for a user: %s", sizeof(ST_USERS), strerror(errno));
|
|
|
99 |
return NULL;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
memset(first, 0, sizeof(ST_USERS));
|
|
|
103 |
return first;
|
|
|
104 |
}
|
|
|
105 |
else
|
|
|
106 |
{
|
|
|
107 |
struct ST_USERS *new, *act;
|
|
|
108 |
|
|
|
109 |
if ((new = (struct ST_USERS *)malloc(sizeof(ST_USERS))) == NULL)
|
|
|
110 |
{
|
|
|
111 |
syslog(LOG_DAEMON, "Error allocating %d bytes for a user: %s", sizeof(ST_USERS), strerror(errno));
|
|
|
112 |
return NULL;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
memset(new, 0, sizeof(ST_USERS));
|
|
|
116 |
/* Find last structure in chain */
|
|
|
117 |
act = first;
|
|
|
118 |
|
|
|
119 |
while (act->next)
|
|
|
120 |
act = act->next;
|
|
|
121 |
|
|
|
122 |
act->next = new;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
int selectUser (int s1, char *user)
|
|
|
127 |
{
|
|
|
128 |
char query[1024];
|
|
|
129 |
char fname[256];
|
|
|
130 |
sqlite3 *db;
|
|
|
131 |
char *uname, *playlist, *zErrMsg = 0;
|
|
|
132 |
int rc, id;
|
|
|
133 |
sqlite3_stmt *res;
|
|
|
134 |
|
|
|
135 |
strcpy(fname, configs.home);
|
|
|
136 |
strcat(fname, "/music.db");
|
|
|
137 |
|
|
|
138 |
rc = sqlite3_open(fname, &db);
|
|
|
139 |
|
|
|
140 |
if (rc)
|
|
|
141 |
{
|
|
|
142 |
syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
|
|
|
143 |
strcpy(query, "ERROR:USER:Error opening database;");
|
|
|
144 |
write (s1, query, strlen(query));
|
|
|
145 |
return FALSE;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
sprintf(query, "select id, uname, playlist from \"main\".\"users\" where uname = \"%s\"", user);
|
|
|
149 |
|
|
|
150 |
if (sqlite3_prepare(db, query, -1, res, NULL) != SQLITE_OK)
|
|
|
151 |
{
|
|
|
152 |
syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
|
|
|
153 |
strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
|
|
|
154 |
write(s1, query, strlen(query));
|
|
|
155 |
return FALSE;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
while ((rc = sqlite3_step(res)) == SQLITE_ROW)
|
|
|
159 |
{
|
|
|
160 |
struct ST_USERS *act;
|
|
|
161 |
|
|
|
162 |
if ((act = addUser(user)) == NULL)
|
|
|
163 |
{
|
|
|
164 |
sqlite3_finalize(res);
|
|
|
165 |
sqlite3_close(db);
|
|
|
166 |
return FALSE;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
act->id = sqlite3_column_int(res, 0);
|
|
|
170 |
strncpy(act->uname, sqlite3_column_text(res, 1), sizeof(struct ST_USERS.uname));
|
|
|
171 |
strncpy(act->playlist, sqlite3_column_text(res, 1), sizeof(struct ST_USERS.playlist));
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
sqlite3_finalize(res);
|
|
|
175 |
sqlite3_close(db);
|
|
|
176 |
return TRUE;
|
|
|
177 |
}
|