Subversion Repositories mdb

Rev

Rev 8 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 */
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <syslog.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sqlite3.h>
#include "config.h"
#include "helplib.h"
#include "user.h"

struct ST_USERS *first = NULL;

int createUser(int s1, char *user, char *playlist)
{
        char query[1024];
        char fname[256];
        sqlite3 *db;
        char *zErrMsg = 0;
        int rc, id;
        sqlite3_stmt *res;

        strcpy(fname, configs.home);
        strcat(fname, "/music.db");

        rc = sqlite3_open(fname, &db);

        if (rc)
        {
                syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
                strcpy(query, "ERROR:USER:Error opening database;");
                write (s1, query, strlen(query));
                return FALSE;
        }

        sprintf(query, "select id, uname from \"main\".\"users\" where uname = \"%s\"", user);

        if (sqlite3_prepare(db, query, -1, res, NULL) != SQLITE_OK)
        {
                syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
                strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
                write(s1, query, strlen(query));
                return FALSE;
        }

        rc = sqlite3_step(res);
        
        if (rc == SQLITE_ROW)                   /* If we've a row, the user exists and we'll not add it again */
        {
                sqlite3_finalize(res);
                sqlite3_close(db);
                return TRUE;
        }

        sprintf(query, "insert into \"users\" (uname, playlist) values (\"%s\", \"%s\")", user, playlist);

        if (sqlite3_prepare(db, query, -1, res, NULL) != SQLITE_OK)
        {
                syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
                strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
                write(s1, query, strlen(query));
                return FALSE;
        }

        rc = sqlite3_step(res);
        sqlite3_finalize(res);
        sqlite3_close(db);
        return TRUE;
}

ST_USERS *addUser()
{
        if (first == NULL)
        {
                if ((first = (ST_USERS *)malloc(sizeof(ST_USERS))) == NULL)
                {
                        syslog(LOG_DAEMON, "Error allocating %d bytes for a user: %s", sizeof(ST_USERS), strerror(errno));
                        return NULL;
                }

                memset(first, 0, sizeof(ST_USERS));
                return first;
        }
        else
        {
                struct ST_USERS *new, *act;

                if ((new = (struct ST_USERS *)malloc(sizeof(ST_USERS))) == NULL)
                {
                        syslog(LOG_DAEMON, "Error allocating %d bytes for a user: %s", sizeof(ST_USERS), strerror(errno));
                        return NULL;
                }

                memset(new, 0, sizeof(ST_USERS));
                /* Find last structure in chain */
                act = first;

                while (act->next)
                        act = act->next;

                act->next = new;
        }
}

int selectUser (int s1, char *user)
{
        char query[1024];
        char fname[256];
        sqlite3 *db;
        char *uname, *playlist, *zErrMsg = 0;
        int rc, id;
        sqlite3_stmt *res;
        
        strcpy(fname, configs.home);
        strcat(fname, "/music.db");
        
        rc = sqlite3_open(fname, &db);
        
        if (rc)
        {
                syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
                strcpy(query, "ERROR:USER:Error opening database;");
                write (s1, query, strlen(query));
                return FALSE;
        }
        
        sprintf(query, "select id, uname, playlist from \"main\".\"users\" where uname = \"%s\"", user);

        if (sqlite3_prepare(db, query, -1, res, NULL) != SQLITE_OK)
        {
                syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
                strcpy(query, "ERROR:USER:Error preparing a SQL statement;");
                write(s1, query, strlen(query));
                return FALSE;
        }

        while ((rc = sqlite3_step(res)) == SQLITE_ROW)
        {
                struct ST_USERS *act;

                if ((act = addUser(user)) == NULL)
                {
                        sqlite3_finalize(res);
                        sqlite3_close(db);
                        return FALSE;
                }

                act->id = sqlite3_column_int(res, 0);
                strncpy(act->uname, sqlite3_column_text(res, 1), sizeof(struct ST_USERS.uname));
                strncpy(act->playlist, sqlite3_column_text(res, 1), sizeof(struct ST_USERS.playlist));
        }

        sqlite3_finalize(res);
        sqlite3_close(db);
        return TRUE;
}