summaryrefslogtreecommitdiff
path: root/nameblocker.sp
diff options
context:
space:
mode:
authornwrl <n/a>2025-08-07 15:44:04 -0500
committernwrl <n/a>2025-08-07 15:44:04 -0500
commit223a5c54b2453685eb380ed691281c724c688c09 (patch)
tree5d67cf979e0747c7d86cf7e1a6eef309032317ad /nameblocker.sp
parent9d792ef2a7b24172d04a85797c97f9f241cde17f (diff)
Work on modpattern implementations
Diffstat (limited to 'nameblocker.sp')
-rw-r--r--nameblocker.sp127
1 files changed, 83 insertions, 44 deletions
diff --git a/nameblocker.sp b/nameblocker.sp
index c4c945c..8e6ab33 100644
--- a/nameblocker.sp
+++ b/nameblocker.sp
@@ -53,7 +53,6 @@ public Plugin myinfo = {
53// any other methodmap or descendant of Handle (like Regex) 53// any other methodmap or descendant of Handle (like Regex)
54 54
55#define PATTERN_MAX_LEN (512 + 1) /* 512 chars + null terminator */ 55#define PATTERN_MAX_LEN (512 + 1) /* 512 chars + null terminator */
56
57#define DATABASE_FAIL_MSG "Could not populate regex & pattern lists from database" 56#define DATABASE_FAIL_MSG "Could not populate regex & pattern lists from database"
58 57
59enum OperatingMode { 58enum OperatingMode {
@@ -126,22 +125,23 @@ void xRegisterIntConVar(ConVar& cv, int defaultVal, const char[] name, const cha
126 125
127int initPrepStatements() { 126int initPrepStatements() {
128 char sqlerr[256 + 1]; 127 char sqlerr[256 + 1];
128
129 SQL_LockDatabase(db); 129 SQL_LockDatabase(db);
130 if((dbInsert = SQL_PrepareQuery(db, DBINSERTSTATEMENT, sqlerr, sizeof(sqlerr))) == null) { 130 if((dbInsert = SQL_PrepareQuery(db, DBINSERTSTATEMENT, sqlerr, sizeof(sqlerr))) == null) {
131 logAndFail("Could not prepare insert statement: %s", sqlerr);
132 SQL_UnlockDatabase(db); 131 SQL_UnlockDatabase(db);
132 logAndFail("Could not prepare insert statement: %s", sqlerr);
133 } 133 }
134 if((dbDelete = SQL_PrepareQuery(db, DBDELETESTATEMENT, sqlerr, sizeof(sqlerr))) == null) { 134 if((dbDelete = SQL_PrepareQuery(db, DBDELETESTATEMENT, sqlerr, sizeof(sqlerr))) == null) {
135 logAndFail("Could not prepare delete statement: %s", sqlerr);
136 SQL_UnlockDatabase(db); 135 SQL_UnlockDatabase(db);
136 logAndFail("Could not prepare delete statement: %s", sqlerr);
137 } 137 }
138 if((dbReplace = SQL_PrepareQuery(db, DBREPLACESTATEMENT, sqlerr, sizeof(sqlerr))) == null) { 138 if((dbReplace = SQL_PrepareQuery(db, DBREPLACESTATEMENT, sqlerr, sizeof(sqlerr))) == null) {
139 logAndFail("Could not prepare replace statement: %s", sqlerr);
140 SQL_UnlockDatabase(db); 139 SQL_UnlockDatabase(db);
140 logAndFail("Could not prepare replace statement: %s", sqlerr);
141 } 141 }
142 if((dbPopulate = SQL_PrepareQuery(db, DBPOPULATESTATEMENT, sqlerr, sizeof(sqlerr))) == null) { 142 if((dbPopulate = SQL_PrepareQuery(db, DBPOPULATESTATEMENT, sqlerr, sizeof(sqlerr))) == null) {
143 logAndFail("Could not prepare populate statement: %s", sqlerr);
144 SQL_UnlockDatabase(db); 143 SQL_UnlockDatabase(db);
144 logAndFail("Could not prepare populate statement: %s", sqlerr);
145 } 145 }
146 146
147 // This might not work / I might have to use Format() instead of binding. We will see 147 // This might not work / I might have to use Format() instead of binding. We will see
@@ -215,7 +215,7 @@ int loadFromDatabase() {
215 continue; 215 continue;
216 } 216 }
217 217
218 if(aInsertPattern(pattern, cur, patternlist.Length)) { 218 if(aModPattern(MM_INSERT, regexlist.Length, pattern)) {
219 LogError("Couldn't add regex \"%s\" to arraylists, continuing", pattern); 219 LogError("Couldn't add regex \"%s\" to arraylists, continuing", pattern);
220 CloseHandle(cur); 220 CloseHandle(cur);
221 } 221 }
@@ -269,7 +269,7 @@ public Action cmdRegisterPattern(int client, int args) {
269 return Plugin_Handled; 269 return Plugin_Handled;
270 } 270 }
271 271
272 if(modPattern(DBM_INSERT, regexlist.Length, pattern)) { 272 if(modPattern(MM_INSERT, regexlist.Length, pattern)) {
273 ReplyToCommand(client, "Error: could not register pattern"); 273 ReplyToCommand(client, "Error: could not register pattern");
274 return Plugin_Handled; 274 return Plugin_Handled;
275 } 275 }
@@ -289,7 +289,7 @@ public Action cmdDeletePattern(int client, int args) {
289 return Plugin_Handled; 289 return Plugin_Handled;
290 } 290 }
291 291
292 if(modPattern(DBM_DELETE, index)) { 292 if(modPattern(MM_DELETE, index)) {
293 ReplyToCommand(client, "Error: could not remove pattern"); 293 ReplyToCommand(client, "Error: could not remove pattern");
294 return Plugin_Handled; 294 return Plugin_Handled;
295 } 295 }
@@ -315,7 +315,7 @@ public Action cmdReplacePattern(int client, int args) {
315 return Plugin_Handled; 315 return Plugin_Handled;
316 } 316 }
317 317
318 if(modPattern(DBM_REPLACE, index, pattern)) { 318 if(modPattern(MM_REPLACE, index, pattern)) {
319 ReplyToCommand(client, "Error: could not replace pattern in list"); 319 ReplyToCommand(client, "Error: could not replace pattern in list");
320 return Plugin_Handled; 320 return Plugin_Handled;
321 } 321 }
@@ -333,63 +333,102 @@ public Action cmdListPatterns(int client, int args) {
333 333
334 334
335 335
336enum DBMOD_MODE { 336enum MOD_MODE {
337 DBM_UNDEF, 337 MM_UNDEF,
338 DBM_INSERT, 338 MM_DELETE,
339 DBM_DELETE, 339 MM_INSERT,
340 DBM_REPLACE, 340 MM_REPLACE,
341 DBM_TOOBIG 341 MM_TOOBIG
342}; 342};
343 343
344int modPattern(DBMOD_MODE mode, int index, char[] pattern="") { 344int modPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) {
345 if(index < 0 || index > regexlist.Length) return -1; 345 if(index < 0 || index > regexlist.Length) return -1;
346 if(mode <= DBM_UNDEF || mode >= DBM_TOOBIG) return -1;
347 346
348 Transaction dbmod = SQL_CreateTransaction(); 347 switch(mode) {
349 if(dbmod == null) return -1; 348 case MM_INSERT: {
349 if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN) {return -1;}
350 350
351 // Update transaction 351 // Add pattern to database
352 SQL_LockDatabase(db);
353 SQL_UnlockDatabase(db);
352 354
353 // char errstr[512]; RegexError reerr; 355 aModPattern(mode, index, pattern);
354 // Regex res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr); 356 }
355 // if(res == null) {
356 // LogError("Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, errstr, reerr);
357 // return -1;
358 // }
359 357
360 // Update lists 358 case MM_DELETE: {
361 // Try to delete before inserting 359 // Detele from database
360 SQL_LockDatabase(db);
361 SQL_UnlockDatabase(db);
362 362
363 return 0; 363 aModPattern(mode, index);
364} 364 }
365 365
366int aInsertPattern(char pattern[PATTERN_MAX_LEN], Regex res, int index) { 366 case MM_REPLACE: {
367 if(IsNullString(pattern) || res == null || index < 0 || index > regexlist.Length) return -1; 367 LogError("Replace mode not implemented yet");
368 return -1;
368 369
369 if(index == regexlist.Length) { 370 // Replace in database
370 regexlist.Push(res); 371 // SQL_LockDatabase(db);
371 patternlist.Push(pattern); 372 // SQL_UnlockDatabase(db);
372 373
373 } else { 374 // aModPattern(mode, index, pattern);
374 regexlist.ShiftUp(index); 375 }
375 patternlist.ShiftUp(index);
376 regexlist.Set(index, res);
377 patternlist.Set(index, pattern);
378 376
377 default: {
378 LogError("Given invalid DBMOD_MODE");
379 return -1;
380 }
379 } 381 }
380 382
381 return 0; 383 return 0;
382} 384}
383 385
384int aRemovePattern(int index) { 386int aModPattern(MOD_MODE mode, int index, char[] pattern="", int patternlen=0) {
385 if(index < 0 || index >= regexlist.Length) return -1; 387 if(mode <= MM_UNDEF || mode >= MM_TOOBIG || index < 0 || index > patternlist.Length) return -1;
388
389 if(mode > MM_DELETE) {
390 if(IsNullString(pattern) || patternlen < 0 || patternlen > PATTERN_MAX_LEN) return -1;
391
392 char errstr[512]; RegexError reerr;
393 Regex res = CompileRegex(pattern, gcvarRegexCompFlags.IntValue, errstr, sizeof(errstr), reerr);
394 if(res == null) {
395 LogError("Error: Could not compile regex pattern \"%s\": %s (%d)", pattern, errstr, reerr);
396 return -1;
397 }
398 }
399
386 400
387 regexlist.Erase(index);
388 patternlist.Erase(index);
389 401
390 return 0; 402 return 0;
391} 403}
392 404
405// int aInsertPattern(char pattern[PATTERN_MAX_LEN], Regex res, int index) {
406// if(IsNullString(pattern) || res == null || index < 0 || index > regexlist.Length) return -1;
407
408// if(index == regexlist.Length) {
409// regexlist.Push(res);
410// patternlist.Push(pattern);
411
412// } else {
413// regexlist.ShiftUp(index);
414// patternlist.ShiftUp(index);
415// regexlist.Set(index, res);
416// patternlist.Set(index, pattern);
417
418// }
419
420// return 0;
421// }
422
423// int aRemovePattern(int index) {
424// if(index < 0 || index >= regexlist.Length) return -1;
425
426// regexlist.Erase(index);
427// patternlist.Erase(index);
428
429// return 0;
430// }
431
393 432
394 433
395// Check's a user's name against the regex list. Returns -1 on error, 0 if skipped, 1 on hit 434// Check's a user's name against the regex list. Returns -1 on error, 0 if skipped, 1 on hit