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.
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.
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,
19 * $Id: sqlcfUser.cc,v 1.4 2008/01/16 02:03:40 buzlip01 Exp $
31 #include "sqlcfUser.h"
38 using std::stringstream
;
44 const sqlcfUser::flagType
sqlcfUser::F_SERVERADMIN
= 0x01; /* +a */
45 const sqlcfUser::flagType
sqlcfUser::F_BLOCK
= 0x02; /* +b */
46 const sqlcfUser::flagType
sqlcfUser::F_COMMENT
= 0x04; /* +c */
47 const sqlcfUser::flagType
sqlcfUser::F_CHANFIX
= 0x08; /* +f */
48 const sqlcfUser::flagType
sqlcfUser::F_OWNER
= 0x10; /* +o */
49 const sqlcfUser::flagType
sqlcfUser::F_USERMANAGER
= 0x20; /* +u */
50 const sqlcfUser::flagType
sqlcfUser::F_PERMBLOCKER
= 0x80; /* +p */
51 const sqlcfUser::flagType
sqlcfUser::F_LOGGEDIN
= 0x40;
53 unsigned long int sqlcfUser::maxUserId
= 0;
55 sqlcfUser::sqlcfUser(sqlManager
* _myManager
) :
69 myManager
= _myManager
;
72 void sqlcfUser::setAllMembers(dbHandle
* theDB
, int row
)
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);
85 needOper
= (!strcasecmp(theDB
->GetValue(row
,11),"t") ? true : false);
87 if (id
> maxUserId
) maxUserId
= id
;
90 bool sqlcfUser::commit(dbHandle
* cacheCon
)
94 /* Get a connection instance to our backend */
95 //dbHandle* cacheCon = myManager->getConnection();
97 /* Create the UPDATE statement */
98 std::stringstream userCommit
;
99 userCommit
<< "UPDATE users SET "
100 << "last_seen = " << last_seen
<< ", "
101 << "last_updated = " << last_updated
<< ", "
102 << "last_updated_by = '" << escapeSQLChars(last_updated_by
) << "', "
103 << "language_id = " << language_id
<< ", "
104 << "faction = '" << escapeSQLChars(group
) << "', "
105 << "flags = " << flags
<< ", "
106 << "issuspended = " << (isSuspended
? "'t'" : "'f'") << ", "
107 << "usenotice = " << (useNotice
? "'t'" : "'f'") << ", "
108 << "needoper = " << (needOper
? "'t'" : "'f'")
113 if (!cacheCon
->Exec(userCommit
.str())) {
114 elog
<< "sqlcfUser::commit> Something went wrong: "
115 << cacheCon
->ErrorMessage()
121 /* Dispose of our connection instance */
122 //myManager->removeConnection(cacheCon);
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.
134 bool sqlcfUser::Insert(dbHandle
* cacheCon
)
138 /* Get a connection instance to our backend */
139 //dbHandle* cacheCon = myManager->getConnection();
141 /* Grab the next available user id */
144 /* Create the INSERT statement */
145 std::stringstream insertString
;
146 insertString
<< "INSERT INTO users "
147 << "(id, user_name) "
151 << "'" << escapeSQLChars(user_name
) << "'"
155 if (!cacheCon
->Exec(insertString
.str())) {
156 elog
<< "sqlcfUser::Insert> Something went wrong: "
157 << cacheCon
->ErrorMessage()
164 /* Dispose of our connection instance */
165 //myManager->removeConnection(cacheCon);
171 } // sqlcfUser::Insert()
173 bool sqlcfUser::Delete(dbHandle
* cacheCon
)
177 /* Get a connection instance to our backend */
178 //dbHandle* cacheCon = myManager->getConnection();
180 /* Create the DELETE statement */
181 std::stringstream hostString
;
182 hostString
<< "DELETE FROM hosts "
183 << "WHERE user_id = " << id
186 if (!cacheCon
->Exec(hostString
.str())) {
187 elog
<< "sqlcfUser::Delete> Something went wrong: "
188 << cacheCon
->ErrorMessage()
194 /* Create the DELETE statement */
195 std::stringstream deleteString
;
196 deleteString
<< "DELETE FROM users "
197 << "WHERE id = '" << id
<< "'"
200 if (!cacheCon
->Exec(deleteString
.str())) {
201 elog
<< "sqlcfUser::Delete> Something went wrong: "
202 << cacheCon
->ErrorMessage()
208 /* Dispose of our connection instance */
209 //myManager->removeConnection(cacheCon);
214 void sqlcfUser::loadHostList(dbHandle
* cacheCon
)
216 /* Get a connection instance to our backend */
218 // dbHandle* cacheCon = myManager->getConnection();
220 // elog << "[sqlcfUser::loadHostList() Could not get a connection to the database from the manager." << std::endl;
223 /* Retrieve the hosts */
224 std::stringstream theQuery
;
226 theQuery
<< "SELECT host FROM hosts WHERE user_id = "
231 if (cacheCon
->Exec(theQuery
.str(),true)) {
232 // SQL Query succeeded
233 for (unsigned int i
= 0 ; i
< cacheCon
->Tuples(); i
++) {
234 hostList
.push_back(cacheCon
->GetValue(i
, 0));
238 /* Dispose of our connection instance */
239 // myManager->removeConnection(cacheCon);
243 bool sqlcfUser::addHost(dbHandle
* cacheCon
, const std::string
& _theHost
)
247 /* Get a connection instance to our backend */
248 //dbHandle* cacheCon = myManager->getConnection();
250 /* Create the INSERT statement */
251 std::stringstream insertString
;
252 insertString
<< "INSERT INTO hosts "
253 << "(user_id, host) VALUES "
257 << escapeSQLChars(_theHost
).c_str()
261 if (!cacheCon
->Exec(insertString
.str())) {
262 elog
<< "sqlcfUser::addHost> Something went wrong: "
263 << cacheCon
->ErrorMessage()
269 /* Dispose of our connection instance */
270 //myManager->removeConnection(cacheCon);
272 hostList
.push_back(_theHost
);
277 bool sqlcfUser::delHost(dbHandle
* cacheCon
, const std::string
& _theHost
)
281 /* Get a connection instance to our backend */
282 //dbHandle* cacheCon = myManager->getConnection();
284 /* Create the DELETE statement */
285 std::stringstream deleteString
;
286 deleteString
<< "DELETE FROM hosts "
287 << "WHERE user_id = "
289 << " AND lower(host) = '"
290 << escapeSQLChars(string_lower(_theHost
)).c_str()
294 if (!cacheCon
->Exec(deleteString
.str())) {
295 elog
<< "sqlcfUser::delHost> Something went wrong: "
296 << cacheCon
->ErrorMessage()
302 /* Dispose of our connection instance */
303 //myManager->removeConnection(cacheCon);
305 if (hostList
.size() < 1) return false;
306 hostListType::iterator ptr
= find( hostList
.begin(), hostList
.end(), string_lower(_theHost
) );
307 if (ptr
== hostList
.end()) return false;
313 bool sqlcfUser::matchHost(const std::string
& _host
)
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;
323 bool sqlcfUser::hasHost(const std::string
& _host
)
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;
331 sqlcfUser::~sqlcfUser()
333 // No heap space allocated
338 } // namespace gnuworld