96741f73c93df2efa7f14e959db831c5497079f6
[NeonServV5.git] / src / modules / NeonSpam.mod / cmd_neonspam_badwords.c
1 /* cmd_neonspam_badwords.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonspam.h"
19
20 CMD_BIND(neonspam_cmd_badwords) {
21     MYSQL_RES *res;
22     MYSQL_ROW row, defaults;
23     loadChannelSettings(chan);
24     loadNeonSpamSettings(chan);
25     struct NeonSpamSettings *settings = chan->spam_settings;
26     if(!settings) return;
27     if(argc > 0) {
28         int badword_id = atoi(argv[0]);
29         printf_mysql_query("SELECT `badword_match` FROM `spamserv_badwords` WHERE `badword_id` = '%d' AND `badword_cid` = '%d'", badword_id, chan->channel_id);
30         res = mysql_use();
31         if(!(row = mysql_fetch_row(res))) {
32             reply(textclient, user, "SS_BADWORD_ID_UNKNOWN", badword_id, chan->name);
33             return;
34         }
35         //to be continued...
36         
37     } else {
38         struct Table *table;
39         printf_mysql_query("SELECT `channel_badword_reaction`, `channel_badword_reaction_duration` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
40         res = mysql_use();
41         row = mysql_fetch_row(res);
42         if(!row[0] || !row[1]) {
43             printf_mysql_query("SELECT `channel_badword_reaction`, `channel_badword_reaction_duration` FROM `channels` WHERE `channel_name` = 'defaults'");
44             res = mysql_use();
45             defaults = mysql_fetch_row(res);
46         }
47         int default_reaction = atoi((row[0] ? row[0] : defaults[0]));
48         int default_reaction_time = atoi((row[1] ? row[1] : defaults[1]));
49         printf_mysql_query("SELECT `badword_id`, `badword_match`, `badword_use_default`, `badword_exceptlevel`, `badword_scan_ops`, `badword_scan_voice`, `badword_use_default_reaction`, `badword_reaction`, `badword_reaction_time` FROM `spamserv_badwords` WHERE `badword_cid` = '%d' ORDER BY `badword_id` ASC", chan->channel_id);
50         res = mysql_use();
51         table = table_init(4, mysql_num_rows(res) + 1, 0);
52         char *content[4];
53         content[0] = get_language_string(user, "SS_BADWORDS_ID");
54         content[1] = get_language_string(user, "SS_BADWORDS_PATTERN");
55         content[2] = get_language_string(user, "SS_BADWORDS_SCAN");
56         content[3] = get_language_string(user, "SS_BADWORDS_REACTION");
57         table_add(table, content);
58         char default_exception[100];
59         char exception_buf[100], reaction_buf[100], reaction_time_buf[100];
60         int exception_pos = 0, reaction_pos;
61         int reaction, reaction_time;
62         if(settings->flags & SPAMSETTINGS_BADWORDSCAN_OPS)
63             exception_pos += sprintf(default_exception+exception_pos, (exception_pos ? " & @" : "@"));
64         if(settings->flags & SPAMSETTINGS_BADWORDSCAN_VOICE)
65             exception_pos += sprintf(default_exception+exception_pos, (exception_pos ? " & +" : "+"));
66         if(settings->exceptlevel[SPAMSETTINGS_BADWORDEXCINDEX])
67             exception_pos += sprintf(default_exception+exception_pos, (exception_pos ? " & <%d" : "<%d"), settings->exceptlevel[SPAMSETTINGS_BADWORDEXCINDEX]);
68         while ((row = mysql_fetch_row(res)) != NULL) {
69             content[0] = row[0];
70             content[1] = row[1];
71             exception_pos = 0;
72             if(strcmp(row[2], "0")) {
73                 exception_pos += sprintf(exception_buf, "\00314%s\003", default_exception);
74             } else {
75                 if(!strcmp(row[4], "1"))
76                     exception_pos += sprintf(exception_buf+exception_pos, (exception_pos ? " & @" : "@"));
77                 if(!strcmp(row[5], "1"))
78                     exception_pos += sprintf(exception_buf+exception_pos, (exception_pos ? " & +" : "+"));
79                 if(atoi(row[3]))
80                     exception_pos += sprintf(exception_buf+exception_pos, (exception_pos ? " & <%d" : "<%d"), atoi(row[3]));
81             }
82             content[2] = exception_buf;
83             reaction_pos = 0;
84             if(strcmp(row[6], "0")) {
85                 reaction = default_reaction;
86                 reaction_time = default_reaction_time;
87             } else {
88                 reaction = atoi(row[7]);
89                 reaction_time = atoi(row[8]);
90             }
91             sprintf(reaction_buf, "SS_BADWORDS_REACTION_%d", reaction);
92             reaction_pos += sprintf(reaction_buf, "%s", get_language_string(user, reaction_buf));
93             if(reaction == 2) {
94                 reaction_pos += sprintf(reaction_buf+reaction_pos, " (%s)", timeToStr(user, reaction_time, 2, reaction_time_buf));
95             }
96             content[3] = reaction_buf;
97             table_add(table, content);
98         }
99         char **table_lines = table_end(table);
100         int i;
101         for(i = 0; i < table->entrys; i++) {
102             reply(textclient, user, table_lines[i]);
103         }
104         table_free(table);
105     }
106 }