]> jfr.im git - irc/evilnet/mod.chanfix.git/blob - sqlcfUser.cc
Fixed missing include, corebug fix was already applied here.
[irc/evilnet/mod.chanfix.git] / sqlcfUser.cc
1 /**
2 * sqlcfUser.cc
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 *
19 * $Id: sqlcfUser.cc,v 1.4 2008/01/16 02:03:40 buzlip01 Exp $
20 */
21
22 #include <sstream>
23 #include <string>
24
25 #include "dbHandle.h"
26
27 #include "ELog.h"
28 #include "misc.h"
29
30 #include "chanfix.h"
31 #include "sqlcfUser.h"
32
33 using namespace std;
34
35 namespace gnuworld
36 {
37
38 using std::stringstream;
39 using std::string;
40
41 namespace cf
42 {
43
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;
52
53 unsigned long int sqlcfUser::maxUserId = 0;
54
55 sqlcfUser::sqlcfUser(sqlManager* _myManager) :
56 id(0),
57 user_name(),
58 created(0),
59 last_seen(0),
60 last_updated(0),
61 last_updated_by(),
62 language_id(0),
63 group(),
64 flags(0),
65 isSuspended(false),
66 useNotice(true),
67 needOper(true)
68 {
69 myManager = _myManager;
70 }
71
72 void sqlcfUser::setAllMembers(dbHandle* theDB, int row)
73 {
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);
86
87 if (id > maxUserId) maxUserId = id;
88 }
89
90 bool sqlcfUser::commit(dbHandle* cacheCon)
91 {
92 bool retval = false;
93
94 /* Get a connection instance to our backend */
95 //dbHandle* cacheCon = myManager->getConnection();
96
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'")
109 << " WHERE "
110 << "id = " << id
111 ;
112
113 if (!cacheCon->Exec(userCommit.str())) {
114 elog << "sqlcfUser::commit> Something went wrong: "
115 << cacheCon->ErrorMessage()
116 << std::endl;
117 retval = false;
118 } else
119 retval = true;
120
121 /* Dispose of our connection instance */
122 //myManager->removeConnection(cacheCon);
123
124 return retval;
125 }
126
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 */
134 bool sqlcfUser::Insert(dbHandle* cacheCon)
135 {
136 bool retval = false;
137
138 /* Get a connection instance to our backend */
139 //dbHandle* cacheCon = myManager->getConnection();
140
141 /* Grab the next available user id */
142 id = ++maxUserId;
143
144 /* Create the INSERT statement */
145 std::stringstream insertString;
146 insertString << "INSERT INTO users "
147 << "(id, user_name) "
148 << "VALUES "
149 << "("
150 << id << ", "
151 << "'" << escapeSQLChars(user_name) << "'"
152 << ")"
153 ;
154
155 if (!cacheCon->Exec(insertString.str())) {
156 elog << "sqlcfUser::Insert> Something went wrong: "
157 << cacheCon->ErrorMessage()
158 << std::endl;
159 retval = false;
160 maxUserId--;
161 } else
162 retval = true;
163
164 /* Dispose of our connection instance */
165 //myManager->removeConnection(cacheCon);
166
167 if (retval)
168 commit(cacheCon);
169
170 return retval;
171 } // sqlcfUser::Insert()
172
173 bool sqlcfUser::Delete(dbHandle* cacheCon)
174 {
175 bool retval = false;
176
177 /* Get a connection instance to our backend */
178 //dbHandle* cacheCon = myManager->getConnection();
179
180 /* Create the DELETE statement */
181 std::stringstream hostString;
182 hostString << "DELETE FROM hosts "
183 << "WHERE user_id = " << id
184 ;
185
186 if (!cacheCon->Exec(hostString.str())) {
187 elog << "sqlcfUser::Delete> Something went wrong: "
188 << cacheCon->ErrorMessage()
189 << std::endl;
190 retval = false;
191 } else
192 retval = true;
193
194 /* Create the DELETE statement */
195 std::stringstream deleteString;
196 deleteString << "DELETE FROM users "
197 << "WHERE id = '" << id << "'"
198 ;
199
200 if (!cacheCon->Exec(deleteString.str())) {
201 elog << "sqlcfUser::Delete> Something went wrong: "
202 << cacheCon->ErrorMessage()
203 << std::endl;
204 retval = false;
205 } else
206 retval = true;
207
208 /* Dispose of our connection instance */
209 //myManager->removeConnection(cacheCon);
210
211 return retval;
212 }
213
214 void sqlcfUser::loadHostList(dbHandle* cacheCon)
215 {
216 /* Get a connection instance to our backend */
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
222
223 /* Retrieve the hosts */
224 std::stringstream theQuery;
225
226 theQuery << "SELECT host FROM hosts WHERE user_id = "
227 << id
228 << ends ;
229
230
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));
235 }
236 }
237
238 /* Dispose of our connection instance */
239 // myManager->removeConnection(cacheCon);
240 return;
241 }
242
243 bool sqlcfUser::addHost(dbHandle* cacheCon, const std::string& _theHost)
244 {
245 bool retval = false;
246
247 /* Get a connection instance to our backend */
248 //dbHandle* cacheCon = myManager->getConnection();
249
250 /* Create the INSERT statement */
251 std::stringstream insertString;
252 insertString << "INSERT INTO hosts "
253 << "(user_id, host) VALUES "
254 << "("
255 << id
256 << ", '"
257 << escapeSQLChars(_theHost).c_str()
258 << "')"
259 ;
260
261 if (!cacheCon->Exec(insertString.str())) {
262 elog << "sqlcfUser::addHost> Something went wrong: "
263 << cacheCon->ErrorMessage()
264 << std::endl;
265 retval = false;
266 } else
267 retval = true;
268
269 /* Dispose of our connection instance */
270 //myManager->removeConnection(cacheCon);
271
272 hostList.push_back(_theHost);
273
274 return retval;
275 }
276
277 bool sqlcfUser::delHost(dbHandle* cacheCon, const std::string& _theHost)
278 {
279 bool retval = false;
280
281 /* Get a connection instance to our backend */
282 //dbHandle* cacheCon = myManager->getConnection();
283
284 /* Create the DELETE statement */
285 std::stringstream deleteString;
286 deleteString << "DELETE FROM hosts "
287 << "WHERE user_id = "
288 << id
289 << " AND lower(host) = '"
290 << escapeSQLChars(string_lower(_theHost)).c_str()
291 << "'"
292 ;
293
294 if (!cacheCon->Exec(deleteString.str())) {
295 elog << "sqlcfUser::delHost> Something went wrong: "
296 << cacheCon->ErrorMessage()
297 << std::endl;
298 retval = false;
299 } else
300 retval = true;
301
302 /* Dispose of our connection instance */
303 //myManager->removeConnection(cacheCon);
304
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;
308 hostList.erase(ptr);
309
310 return retval;
311 }
312
313 bool sqlcfUser::matchHost(const std::string& _host)
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
323 bool sqlcfUser::hasHost(const std::string& _host)
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
331 sqlcfUser::~sqlcfUser()
332 {
333 // No heap space allocated
334 }
335
336 } // namespace cf
337
338 } // namespace gnuworld