File: Storage.cpp

package info (click to toggle)
cyphesis-cpp 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: cpp: 94,194; xml: 40,196; python: 8,717; sh: 4,164; makefile: 1,968; ansic: 753
file content (180 lines) | stat: -rw-r--r-- 5,137 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2001-2005 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


#include "Storage.h"

#include "system.h"

#include <Atlas/Message/Element.h>

#include <iostream>

/// \brief Initialise a connection to the accounts database
int Storage::init()
{
    if (m_connection.initConnection() != 0) {
        return -1;
    }
    if (m_connection.initRule(true) != 0) {
        return -1;
    }
    return 0;
}

/// \brief Store a new Account in the database
///
/// @param account Atlas description of Account to be stored
int Storage::putAccount(const Atlas::Message::MapType & account)
{
    Atlas::Message::MapType::const_iterator I = account.find("username");
    if (I == account.end() || !I->second.isString()) {
        return -1;
    }
    const std::string & username = I->second.String();
    
    I = account.find("password");
    if (I == account.end() || !I->second.isString()) {
        return -1;
    }
    const std::string & password = I->second.String();
    std::string hash;
    encrypt_password(password, hash);
    
    std::string type = "player";
    I = account.find("type");
    if (I != account.end() && I->second.isString()) {
        type = I->second.String();
    }
    
    std::string columns = "username, type, password";
    std::string values = "'";
    values += username;
    values += "', '";
    values += type;
    values += "', '";
    values += hash;
    values += "'";

    std::string id;
    if (m_connection.newId(id) < 0) {
        return -1;
    }
    return m_connection.createSimpleRow("accounts", id, columns, values);
}

/// \brief Modify the attributes of an Account in the database
///
/// @param account Atlas description of the Account to be modified
/// @param accountId String identifier of the Account to be modified
int Storage::modAccount(const Atlas::Message::MapType & account,
                        const std::string & accountId)
{
    std::string columns;
    bool empty = true;

    Atlas::Message::MapType::const_iterator I = account.find("type");
    if (I != account.end() && I->second.isString()) {
        empty = false;
        columns += "type = '";
        columns += I->second.String();
        columns += "'";
    }

    I = account.find("password");
    if (I != account.end() && I->second.isString()) {
        if (!empty) { columns += ", "; }
        std::string hash;
        encrypt_password(I->second.String(), hash);
        columns += "password = '";
        columns += hash;
        columns += "'";
    }
    return m_connection.updateSimpleRow("accounts", "username",
                                        accountId, columns);
}

/// \brief Remove an Account from the accounts database
///
/// @param account String identifier of the Account to be removed.
int Storage::delAccount(const std::string & account)
{
    return -1;
}

/// \brief Retrieve an Account from the accounts database
///
/// @param username Username of the Account to be found
/// @param account Account description returned here
int Storage::getAccount(const std::string & username,
                        Atlas::Message::MapType & account)
{
    std::string namestr = "'" + username + "'";
    DatabaseResult dr = m_connection.selectSimpleRowBy("accounts", "username", namestr);
    if (dr.error()) {
        return -1;
    }
    if (dr.empty()) {
        return -1;
    }
    if (dr.size() > 1) {
        return -1;
    }
    const char * c = dr.field("id");
    if (c == 0) {
        return -1;
    }
    std::string id = c;

    c = dr.field("password");
    if (c == 0) {
        return -1;
    }
    std::string password = c;

    c = dr.field("type");
    if (c == 0) {
        return -1;
    }
    std::string type = c;

    account["id"] = id;
    account["username"] = username;
    account["password"] = password;
    account["type"] = type;

    return 0;
}

void Storage::storeInRules(const Atlas::Message::MapType & rule,
                           const std::string & key)
{
    if (m_connection.hasKey(m_connection.rule(), key)) {
        return;
    }
    m_connection.putObject(m_connection.rule(), key, rule, StringVector(1, m_rulesetName));
    if (m_connection.clearPendingQuery() != 0) {
        // FIXME NO cerr
        std::cerr << "Failed" << std::endl << std::flush;
    }
}

int Storage::clearRules()
{
    return m_connection.clearTable(m_connection.rule()) ||
           m_connection.clearPendingQuery();
}