]> jfr.im git - irc/evilnet/mod.chanfix.git/blame - sqlChannel.cc
Fixed missing include, corebug fix was already applied here.
[irc/evilnet/mod.chanfix.git] / sqlChannel.cc
CommitLineData
07d004e4 1/**
dbbf1647 2 * sqlChannel.cc
3 *
07d004e4 4 * Author: Matthias Crauwels <ultimate_@wol.be>
5 *
dbbf1647 6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 *
21 * $Id$
07d004e4 22 */
23
d508315a 24#include <sstream>
25#include <string>
07d004e4 26
23cb8771 27#include "dbHandle.h"
79d591cc 28
d508315a 29#include "ELog.h"
30#include "misc.h"
79d591cc 31
d508315a 32#include "sqlChannel.h"
07d004e4 33#include "chanfix.h"
34
35namespace gnuworld
36{
37
1d54d2a1 38namespace cf
39{
40
79d591cc 41const sqlChannel::flagType sqlChannel::F_BLOCKED = 0x00000001 ;
8e9ffde1 42const sqlChannel::flagType sqlChannel::F_ALERT = 0x00000002 ;
07d004e4 43
f5b9e7e0 44const int sqlChannel::EV_MISC = 1 ; /* Uncategorized event */
45const int sqlChannel::EV_NOTE = 2 ; /* Miscellaneous notes */
46const int sqlChannel::EV_CHANFIX = 3 ; /* Manual chanfixes */
47const int sqlChannel::EV_BLOCK = 4 ; /* Channel block */
48const int sqlChannel::EV_UNBLOCK = 5 ; /* Channel unblock */
49const int sqlChannel::EV_ALERT = 6 ; /* Channel alert */
50const int sqlChannel::EV_UNALERT = 7 ; /* Channel unalert */
51const int sqlChannel::EV_REQUESTOP = 8 ; /* Requestops */
52const int sqlChannel::EV_TEMPBLOCK = 9 ; /* Temp channel block */
53const int sqlChannel::EV_UNTEMPBLOCK = 10 ; /* Temp channel unblock */
608302fe 54const int sqlChannel::EV_SIMULATE = 11 ; /* Fix simulation */
dee49a74 55
dbbf1647 56unsigned long int sqlChannel::maxUserId = 0;
57
58sqlChannel::sqlChannel(sqlManager* _myManager) :
59 id(0),
dee49a74 60 channel(),
e0282ee9 61 user_name(),
c57bccaa 62 last(0),
63 start(0),
07d004e4 64 maxScore(0),
f5462d9e 65 modesRemoved(false),
fd2029d8 66 flags(0),
67 inSQL(false)
c67216a8 68{
dbbf1647 69 myManager = _myManager;
bbf51fa1 70}
c67216a8 71
23cb8771 72void sqlChannel::setAllMembers(dbHandle* theDB, int row)
c67216a8 73{
dbbf1647 74 id = atoi(theDB->GetValue(row, 0));
75 channel = theDB->GetValue(row, 1);
76 flags = atoi(theDB->GetValue(row, 2));
fd2029d8 77 inSQL = true;
c67216a8 78
dbbf1647 79 if (id > maxUserId) maxUserId = id;
4c59f7a3 80}
07d004e4 81
dbbf1647 82/**
83 * This function inserts a brand new channel into the DB.
dbbf1647 84 */
23cb8771 85bool sqlChannel::Insert(dbHandle* cacheCon)
07d004e4 86{
c2f14d27 87/* Get a connection instance to our backend */
23cb8771 88//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 89
dbbf1647 90/* Grab the next available user id */
91id = ++maxUserId;
92
c2f14d27 93/* Create the INSERT statement */
dbbf1647 94std::stringstream insertString;
95insertString << "INSERT INTO channels "
f813bdeb 96 << "(id, channel, flags) "
dbbf1647 97 << "VALUES "
98 << "("
99 << id << ", "
f813bdeb 100 << "'" << escapeSQLChars(channel) << "', "
101 << flags
dbbf1647 102 << ")"
a6218cc3 103 ;
07d004e4 104
23cb8771 105if (!cacheCon->Exec(insertString.str())) {
c2f14d27 106 elog << "sqlChannel::Insert> Something went wrong: "
107 << cacheCon->ErrorMessage()
108 << std::endl;
fd2029d8 109 inSQL = false;
110 maxUserId--;
111} else {
112 inSQL = true;
113}
c2f14d27 114
115/* Dispose of our connection instance */
23cb8771 116//myManager->removeConnection(cacheCon);
c2f14d27 117
fd2029d8 118return inSQL;
dbbf1647 119} // sqlChannel::Insert()
07d004e4 120
23cb8771 121bool sqlChannel::Delete(dbHandle* cacheCon)
3a1cc317 122{
c2f14d27 123bool retval = false;
124
125/* Get a connection instance to our backend */
23cb8771 126//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 127
128/* Create the DELETE statement */
dbbf1647 129std::stringstream deleteString;
130deleteString << "DELETE FROM channels "
131 << "WHERE id = '" << id << "'"
a6218cc3 132 ;
3a1cc317 133
23cb8771 134if (!cacheCon->Exec(deleteString.str())) {
c2f14d27 135 elog << "sqlChannel::Delete> Something went wrong: "
136 << cacheCon->ErrorMessage()
137 << std::endl;
138 retval = false;
139} else
140 retval = true;
141
142/* Dispose of our connection instance */
23cb8771 143//myManager->removeConnection(cacheCon);
c2f14d27 144
145return retval;
dbbf1647 146}
07d004e4 147
23cb8771 148bool sqlChannel::commit(dbHandle* cacheCon)
07d004e4 149{
c2f14d27 150bool retval = false;
151
152/* Get a connection instance to our backend */
23cb8771 153//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 154
155/* Create the UPDATE statement */
dbbf1647 156std::stringstream chanCommit;
157chanCommit << "UPDATE channels SET "
158 << "flags = " << flags
159 << " WHERE "
160 << "id = " << id
a6218cc3 161 ;
c2f14d27 162
23cb8771 163if (!cacheCon->Exec(chanCommit.str())) {
c2f14d27 164 elog << "sqlChannel::commit> Something went wrong: "
165 << cacheCon->ErrorMessage()
166 << std::endl;
167 retval = false;
168} else
169 retval = true;
170
171/* Dispose of our connection instance */
23cb8771 172//myManager->removeConnection(cacheCon);
c2f14d27 173
174return retval;
dbbf1647 175}
07d004e4 176
92b0a9b9 177/**
178 * This method writes a 'notes' record, recording an event that has
179 * occured in this channel.
180 */
181
23cb8771 182void sqlChannel::addNote(dbHandle* cacheCon, unsigned short eventType, iClient* theUser,
92b0a9b9 183 const std::string& theMessage)
184{
23cb8771 185unsigned int num_notes = countNotes(cacheCon, 0);
92b0a9b9 186while (num_notes >= MAXNOTECOUNT) {
23cb8771 187 if (!deleteOldestNote(cacheCon))
92b0a9b9 188 return;
189 num_notes--;
190}
191
c2f14d27 192/* Get a connection instance to our backend */
23cb8771 193//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 194
195/* Create the INSERT statement */
92b0a9b9 196std::stringstream theLog;
e0282ee9 197theLog << "INSERT INTO notes (ts, channelID, user_name, event, message) "
92b0a9b9 198 << "VALUES ("
199 << "now()::abstime::int4"
200 << ", "
201 << id
e0282ee9 202 << ", '"
203 << theUser->getAccount()
204 << "', "
92b0a9b9 205 << eventType
206 << ", "
207 << "'"
208 << escapeSQLChars(theMessage)
209 << "')"
a6218cc3 210 ;
92b0a9b9 211
23cb8771 212if (!cacheCon->Exec(theLog.str())) {
c2f14d27 213 elog << "sqlChannel::addNote> Something went wrong: "
214 << cacheCon->ErrorMessage()
215 << std::endl;
216}
217
218/* Dispose of our connection instance */
23cb8771 219//myManager->removeConnection(cacheCon);
c2f14d27 220
221return;
92b0a9b9 222}
223
23cb8771 224const std::string sqlChannel::getLastNote(dbHandle* cacheCon, unsigned short eventType, time_t& eventTime)
92b0a9b9 225{
dbbf1647 226std::string retval;
92b0a9b9 227
dbbf1647 228/* Get a connection instance to our backend */
23cb8771 229//dbHandle* cacheCon = myManager->getConnection();
92b0a9b9 230
dbbf1647 231/* Retrieve the last note */
232std::stringstream queryString;
233queryString << "SELECT message,ts"
234 << " FROM notes WHERE channelID = "
235 << id
236 << " AND event = "
237 << eventType
238 << " ORDER BY ts DESC LIMIT 1"
239 ;
92b0a9b9 240
23cb8771 241if (cacheCon->Exec(queryString.str(),true)) {
dbbf1647 242 if (cacheCon->Tuples() > 0) {
243 std::string note = cacheCon->GetValue(0, 0);
244 eventTime = atoi(cacheCon->GetValue(0, 1));
245 retval = note;
246 }
247}
92b0a9b9 248
dbbf1647 249/* Dispose of our connection instance */
23cb8771 250//myManager->removeConnection(cacheCon);
92b0a9b9 251
dbbf1647 252return retval;
92b0a9b9 253}
254
23cb8771 255bool sqlChannel::deleteNote(dbHandle* cacheCon, unsigned int messageId)
92b0a9b9 256{
c2f14d27 257bool retval = false;
258
259/* Get a connection instance to our backend */
23cb8771 260//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 261
262/* Create the DELETE statement */
dbbf1647 263std::stringstream deleteString;
264deleteString << "DELETE FROM notes WHERE channelID = "
92b0a9b9 265 << id
92b0a9b9 266 << " AND id = "
267 << messageId
a6218cc3 268 ;
c2f14d27 269
23cb8771 270if (!cacheCon->Exec(deleteString.str())) {
c2f14d27 271 elog << "sqlChannel::deleteNote> Something went wrong: "
272 << cacheCon->ErrorMessage()
273 << std::endl;
274 retval = false;
275} else
276 retval = true;
277
278/* Dispose of our connection instance */
23cb8771 279//myManager->removeConnection(cacheCon);
c2f14d27 280
281return retval;
92b0a9b9 282}
283
23cb8771 284bool sqlChannel::deleteOldestNote(dbHandle* cacheCon)
92b0a9b9 285{
dbbf1647 286bool retval = false;
287
288/* Get a connection instance to our backend */
23cb8771 289//dbHandle* cacheCon = myManager->getConnection();
dbbf1647 290
291/* Retrieve the id of the oldest note */
292std::stringstream selectString;
293selectString << "SELECT id FROM notes WHERE channelID = "
92b0a9b9 294 << id
295 << " ORDER BY ts ASC LIMIT 1"
a6218cc3 296 ;
92b0a9b9 297
23cb8771 298if (cacheCon->Exec(selectString.str(),true)) {
dbbf1647 299 if (cacheCon->Tuples() > 0) {
300 unsigned int note_id = atoi(cacheCon->GetValue(0, 0));
c67216a8 301
dbbf1647 302 std::stringstream deleteString;
303 deleteString << "DELETE FROM notes WHERE id = "
304 << note_id
305 ;
c67216a8 306
23cb8771 307 if (cacheCon->Exec(deleteString.str(),true))
dbbf1647 308 retval = true;
309 }
310}
c67216a8 311
dbbf1647 312/* Dispose of our connection instance */
23cb8771 313//myManager->removeConnection(cacheCon);
92b0a9b9 314
dbbf1647 315return retval;
92b0a9b9 316}
317
23cb8771 318bool sqlChannel::deleteAllNotes(dbHandle* cacheCon)
92b0a9b9 319{
c2f14d27 320bool retval = false;
321
322/* Get a connection instance to our backend */
23cb8771 323//dbHandle* cacheCon = myManager->getConnection();
c2f14d27 324
325/* Create the DELETE statement */
dbbf1647 326std::stringstream deleteString;
327deleteString << "DELETE FROM notes WHERE channelID = "
92b0a9b9 328 << id
a6218cc3 329 ;
92b0a9b9 330
23cb8771 331if (!cacheCon->Exec(deleteString.str())) {
c2f14d27 332 elog << "sqlChannel::deleteAllNotes> Something went wrong: "
333 << cacheCon->ErrorMessage()
334 << std::endl;
335 retval = false;
336} else
337 retval = true;
338
339/* Dispose of our connection instance */
23cb8771 340//myManager->removeConnection(cacheCon);
c2f14d27 341
342return retval;
92b0a9b9 343}
344
23cb8771 345size_t sqlChannel::countNotes(dbHandle* cacheCon, unsigned short eventType)
92b0a9b9 346{
dbbf1647 347/* Get a connection instance to our backend */
23cb8771 348//dbHandle* cacheCon = myManager->getConnection();
dbbf1647 349
350/* Count the notes */
92b0a9b9 351std::stringstream queryString;
92b0a9b9 352queryString << "SELECT count(id) FROM notes WHERE channelID = "
353 << id
a6218cc3 354 ;
f813bdeb 355if (eventType) {
356queryString << " AND event = "
357 << eventType
a6218cc3 358 ;
92b0a9b9 359}
360
dbbf1647 361size_t num_notes = 0;
92b0a9b9 362
23cb8771 363if (cacheCon->Exec(queryString.str(),true))
f813bdeb 364 num_notes = atoi(cacheCon->GetValue(0, 0));
92b0a9b9 365
dbbf1647 366/* Dispose of our connection instance */
23cb8771 367//myManager->removeConnection(cacheCon);
92b0a9b9 368
dbbf1647 369return num_notes;
92b0a9b9 370}
371
07d004e4 372sqlChannel::~sqlChannel()
373{
374// No heap space allocated
375}
376
1d54d2a1 377} // namespace cf
378
07d004e4 379} // namespace gnuworld