]> jfr.im git - irc/evilnet/mod.chanfix.git/blame - sqlcfUser.cc
Fixed missing include, corebug fix was already applied here.
[irc/evilnet/mod.chanfix.git] / sqlcfUser.cc
CommitLineData
43236e25 1/**
e7cde540 2 * sqlcfUser.cc
43236e25
C
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 *
23cb8771 19 * $Id: sqlcfUser.cc,v 1.4 2008/01/16 02:03:40 buzlip01 Exp $
43236e25
C
20 */
21
22#include <sstream>
23#include <string>
43236e25 24
23cb8771 25#include "dbHandle.h"
43236e25
C
26
27#include "ELog.h"
28#include "misc.h"
29
30#include "chanfix.h"
e7cde540 31#include "sqlcfUser.h"
43236e25 32
23cb8771 33using namespace std;
34
43236e25
C
35namespace gnuworld
36{
dee49a74 37
23cb8771 38using std::stringstream;
39using std::string;
40
1d54d2a1 41namespace cf
42{
43
e7cde540 44const sqlcfUser::flagType sqlcfUser::F_SERVERADMIN = 0x01; /* +a */
45const sqlcfUser::flagType sqlcfUser::F_BLOCK = 0x02; /* +b */
75b1ada6 46const sqlcfUser::flagType sqlcfUser::F_COMMENT = 0x04; /* +c */
47const sqlcfUser::flagType sqlcfUser::F_CHANFIX = 0x08; /* +f */
e7cde540 48const sqlcfUser::flagType sqlcfUser::F_OWNER = 0x10; /* +o */
23cb8771 49const sqlcfUser::flagType sqlcfUser::F_USERMANAGER = 0x20; /* +u */
75b1ada6 50const sqlcfUser::flagType sqlcfUser::F_PERMBLOCKER = 0x80; /* +p */
51const sqlcfUser::flagType sqlcfUser::F_LOGGEDIN = 0x40;
264890ed 52
e7cde540 53unsigned long int sqlcfUser::maxUserId = 0;
3a634680 54
e7cde540 55sqlcfUser::sqlcfUser(sqlManager* _myManager) :
dbbf1647 56 id(0),
dee49a74 57 user_name(),
43236e25
C
58 created(0),
59 last_seen(0),
60 last_updated(0),
dee49a74 61 last_updated_by(),
57caa9ac 62 language_id(0),
d939ef34 63 group(),
dee49a74 64 flags(0),
65 isSuspended(false),
c06812cf 66 useNotice(true),
67 needOper(true)
c67216a8 68{
3a634680 69 myManager = _myManager;
bbf51fa1 70}
c67216a8 71
23cb8771 72void sqlcfUser::setAllMembers(dbHandle* theDB, int row)
43236e25 73{
3a634680 74 id = atoi(theDB->GetValue(row, 0));
75 user_name = theDB->GetValue(row, 1);
76 created = atoi(theDB->GetValue(row, 2));
77 last_seen = atoi(theDB->GetValue(row, 3));
78 last_updated = atoi(theDB->GetValue(row, 4));
79 last_updated_by = theDB->GetValue(row, 5);
80 language_id = atoi(theDB->GetValue(row, 6));
81 group = theDB->GetValue(row, 7);
82 flags = atoi(theDB->GetValue(row, 8));
83 isSuspended = (!strcasecmp(theDB->GetValue(row,9),"t") ? true : false);
84 useNotice = (!strcasecmp(theDB->GetValue(row,10),"t") ? true : false);
c06812cf 85 needOper = (!strcasecmp(theDB->GetValue(row,11),"t") ? true : false);
3a634680 86
87 if (id > maxUserId) maxUserId = id;
4c59f7a3 88}
43236e25 89
23cb8771 90bool sqlcfUser::commit(dbHandle* cacheCon)
43236e25 91{
c2f14d27 92bool retval = false;
93
94/* Get a connection instance to our backend */
23cb8771 95//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 96
97/* Create the UPDATE statement */
3a634680 98std::stringstream userCommit;
99userCommit << "UPDATE users SET "
43236e25 100 << "last_seen = " << last_seen << ", "
43236e25 101 << "last_updated = " << last_updated << ", "
f813bdeb 102 << "last_updated_by = '" << escapeSQLChars(last_updated_by) << "', "
1ae996e4 103 << "language_id = " << language_id << ", "
f813bdeb 104 << "faction = '" << escapeSQLChars(group) << "', "
dee49a74 105 << "flags = " << flags << ", "
a6218cc3 106 << "issuspended = " << (isSuspended ? "'t'" : "'f'") << ", "
c06812cf 107 << "usenotice = " << (useNotice ? "'t'" : "'f'") << ", "
108 << "needoper = " << (needOper ? "'t'" : "'f'")
43236e25 109 << " WHERE "
c67216a8 110 << "id = " << id
43236e25 111 ;
c2f14d27 112
23cb8771 113if (!cacheCon->Exec(userCommit.str())) {
e7cde540 114 elog << "sqlcfUser::commit> Something went wrong: "
c2f14d27 115 << cacheCon->ErrorMessage()
116 << std::endl;
117 retval = false;
118} else
119 retval = true;
120
121/* Dispose of our connection instance */
23cb8771 122//myManager->removeConnection(cacheCon);
c2f14d27 123
124return retval;
43236e25
C
125}
126
3a634680 127/**
128 * This function inserts a brand new user into the DB.
129 * It is a slight fudge, in that it first creates a blank record then
130 * calls commit() to update the data fields for that record. This is done
131 * so that any new fields added will automatically be dealt with in commit()
132 * instead of in 50 different functions.
133 */
23cb8771 134bool sqlcfUser::Insert(dbHandle* cacheCon)
43236e25 135{
c2f14d27 136bool retval = false;
137
138/* Get a connection instance to our backend */
23cb8771 139//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 140
3a634680 141/* Grab the next available user id */
142id = ++maxUserId;
143
c2f14d27 144/* Create the INSERT statement */
43236e25
C
145std::stringstream insertString;
146insertString << "INSERT INTO users "
3a634680 147 << "(id, user_name) "
43236e25
C
148 << "VALUES "
149 << "("
3a634680 150 << id << ", "
f813bdeb 151 << "'" << escapeSQLChars(user_name) << "'"
43236e25
C
152 << ")"
153 ;
154
23cb8771 155if (!cacheCon->Exec(insertString.str())) {
e7cde540 156 elog << "sqlcfUser::Insert> Something went wrong: "
c2f14d27 157 << cacheCon->ErrorMessage()
158 << std::endl;
159 retval = false;
fd2029d8 160 maxUserId--;
c2f14d27 161} else
162 retval = true;
163
164/* Dispose of our connection instance */
23cb8771 165//myManager->removeConnection(cacheCon);
c2f14d27 166
fd2029d8 167if (retval)
23cb8771 168 commit(cacheCon);
c2f14d27 169
170return retval;
e7cde540 171} // sqlcfUser::Insert()
43236e25 172
23cb8771 173bool sqlcfUser::Delete(dbHandle* cacheCon)
43236e25 174{
c2f14d27 175bool retval = false;
176
177/* Get a connection instance to our backend */
23cb8771 178//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 179
180/* Create the DELETE statement */
86f2c0b8 181std::stringstream hostString;
182hostString << "DELETE FROM hosts "
183 << "WHERE user_id = " << id
43236e25 184 ;
43236e25 185
23cb8771 186if (!cacheCon->Exec(hostString.str())) {
e7cde540 187 elog << "sqlcfUser::Delete> Something went wrong: "
c2f14d27 188 << cacheCon->ErrorMessage()
189 << std::endl;
190 retval = false;
191} else
192 retval = true;
86f2c0b8 193
c2f14d27 194/* Create the DELETE statement */
86f2c0b8 195std::stringstream deleteString;
196deleteString << "DELETE FROM users "
197 << "WHERE id = '" << id << "'"
a6218cc3 198 ;
c2f14d27 199
23cb8771 200if (!cacheCon->Exec(deleteString.str())) {
e7cde540 201 elog << "sqlcfUser::Delete> Something went wrong: "
c2f14d27 202 << cacheCon->ErrorMessage()
203 << std::endl;
204 retval = false;
205} else
206 retval = true;
207
208/* Dispose of our connection instance */
23cb8771 209//myManager->removeConnection(cacheCon);
c2f14d27 210
211return retval;
43236e25
C
212}
213
23cb8771 214void sqlcfUser::loadHostList(dbHandle* cacheCon)
17febc02 215{
dbbf1647 216/* Get a connection instance to our backend */
23cb8771 217
218// dbHandle* cacheCon = myManager->getConnection();
219// if (!cacheCon)
220// elog << "[sqlcfUser::loadHostList() Could not get a connection to the database from the manager." << std::endl;
221
17febc02 222
dbbf1647 223/* Retrieve the hosts */
224std::stringstream theQuery;
23cb8771 225
dbbf1647 226theQuery << "SELECT host FROM hosts WHERE user_id = "
17febc02 227 << id
23cb8771 228 << ends ;
dbbf1647 229
23cb8771 230
231if (cacheCon->Exec(theQuery.str(),true)) {
dbbf1647 232 // SQL Query succeeded
23cb8771 233 for (unsigned int i = 0 ; i < cacheCon->Tuples(); i++) {
dbbf1647 234 hostList.push_back(cacheCon->GetValue(i, 0));
17febc02 235 }
dbbf1647 236}
237
238/* Dispose of our connection instance */
23cb8771 239// myManager->removeConnection(cacheCon);
dbbf1647 240return;
241}
242
23cb8771 243bool sqlcfUser::addHost(dbHandle* cacheCon, const std::string& _theHost)
dbbf1647 244{
c2f14d27 245bool retval = false;
246
247/* Get a connection instance to our backend */
23cb8771 248//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 249
250/* Create the INSERT statement */
dbbf1647 251std::stringstream insertString;
252insertString << "INSERT INTO hosts "
253 << "(user_id, host) VALUES "
254 << "("
255 << id
256 << ", '"
f813bdeb 257 << escapeSQLChars(_theHost).c_str()
dbbf1647 258 << "')"
259 ;
c2f14d27 260
23cb8771 261if (!cacheCon->Exec(insertString.str())) {
e7cde540 262 elog << "sqlcfUser::addHost> Something went wrong: "
c2f14d27 263 << cacheCon->ErrorMessage()
264 << std::endl;
265 retval = false;
266} else
267 retval = true;
268
269/* Dispose of our connection instance */
23cb8771 270//myManager->removeConnection(cacheCon);
dbbf1647 271
272hostList.push_back(_theHost);
273
c2f14d27 274return retval;
dbbf1647 275}
17febc02 276
23cb8771 277bool sqlcfUser::delHost(dbHandle* cacheCon, const std::string& _theHost)
17febc02 278{
c2f14d27 279bool retval = false;
280
281/* Get a connection instance to our backend */
23cb8771 282//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 283
284/* Create the DELETE statement */
dbbf1647 285std::stringstream deleteString;
286deleteString << "DELETE FROM hosts "
287 << "WHERE user_id = "
288 << id
289 << " AND lower(host) = '"
f813bdeb 290 << escapeSQLChars(string_lower(_theHost)).c_str()
dbbf1647 291 << "'"
292 ;
dbbf1647 293
23cb8771 294if (!cacheCon->Exec(deleteString.str())) {
e7cde540 295 elog << "sqlcfUser::delHost> Something went wrong: "
c2f14d27 296 << cacheCon->ErrorMessage()
297 << std::endl;
298 retval = false;
299} else
300 retval = true;
301
302/* Dispose of our connection instance */
23cb8771 303//myManager->removeConnection(cacheCon);
c2f14d27 304
305if (hostList.size() < 1) return false;
dbbf1647 306hostListType::iterator ptr = find( hostList.begin(), hostList.end(), string_lower(_theHost) );
c2f14d27 307if (ptr == hostList.end()) return false;
dbbf1647 308hostList.erase(ptr);
c2f14d27 309
310return retval;
17febc02 311}
312
e7cde540 313bool sqlcfUser::matchHost(const std::string& _host)
17febc02 314{
315 if (hostList.size() < 1) return false;
316 for(hostListType::iterator itr = hostList.begin() ;
317 itr != hostList.end() ; ++itr) {
318 if (match(*itr,_host) == 0) return true;
319 }
320 return false;
321}
322
e7cde540 323bool sqlcfUser::hasHost(const std::string& _host)
17febc02 324{
325 if (hostList.size() < 1) return false;
326 hostListType::iterator ptr = find( hostList.begin(), hostList.end(), string_lower(_host) );
327 if ( ptr == hostList.end() ) return false;
328 return true;
329}
330
e7cde540 331sqlcfUser::~sqlcfUser()
43236e25
C
332{
333// No heap space allocated
334}
335
1d54d2a1 336} // namespace cf
337
338} // namespace gnuworld