1 /* event_neonserv_mode.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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, see <http://www.gnu.org/licenses/>.
18 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup);
19 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc);
20 static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, int *botwar_detect_executed);
22 struct neonserv_event_mode_cache {
23 struct ClientSocket *client;
24 struct UserNode *user;
30 static void neonserv_event_mode(struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
31 struct ClientSocket *client = getBotForChannel(chan);
32 if(!client) return; //we can't "see" this event
33 if(isNetworkService(user)) return;
34 loadChannelSettings(chan);
35 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
36 struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
37 char **temp_argv = NULL;
39 temp_argv = malloc(argc*sizeof(*temp_argv));
40 if (!cache || (argc && !temp_argv)) {
41 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
46 for(i = 0; i < argc; i++) {
47 temp_argv[i] = strdup(argv[i]);
50 cache->client = client;
52 cache->modes = strdup(modes);
53 cache->argv = temp_argv;
55 get_userlist_with_invisible(chan, module_id, neonserv_event_mode_userlist_lookup, cache);
58 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
59 struct neonserv_event_mode_cache *cache = data;
60 neonserv_event_mode_async1(cache->client, cache->user, chan, cache->modes, cache->argv, cache->argc);
63 for(i = 0; i < cache->argc; i++) {
72 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
73 struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
74 int botwar_detect_executed = 0;
75 MYSQL_ROW row, defaults = NULL;
76 int i, arg, add = 1, skip = 0;
77 unsigned int modetype;
78 int db_canop, db_canhalfop, db_canvoice, db_canban, db_enfmodes, db_getop, db_gethalfop, db_getvoice;
79 struct ModeNode *modelock = createModeNode(NULL);
80 struct ModeBuffer *modeBuf;
81 struct UserNode *cuser;
82 struct ChanUser *chanuser;
83 int with_halfops = get_int_field("General.have_halfop");
84 modeBuf = initModeBuffer(client, chan);
85 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice`, `channel_gethalfop`, `channel_canhalfop` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
86 row = mysql_fetch_row(mysql_use());
87 if(row[0] == NULL || row[1] == NULL || row[2] == NULL || row[3] == NULL || row[4] == NULL || row[5] == NULL || row[6] == NULL || (row[7] == NULL && with_halfops) || (row[8] == NULL && with_halfops)) {
88 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes`, `channel_getop`, `channel_getvoice`, `channel_gethalfop`, `channel_canhalfop` FROM `channels` WHERE `channel_name` = 'defaults'");
89 defaults = mysql_fetch_row(mysql_use());
91 db_canop = atoi((row[0] ? row[0] : defaults[0]));
92 db_canvoice = atoi((row[1] ? row[1] : defaults[1]));
93 db_canban = atoi((row[2] ? row[2] : defaults[2]));
94 db_enfmodes = atoi((row[3] ? row[3] : defaults[3]));
95 db_getop = atoi((row[5] ? row[5] : defaults[5]));
96 db_getvoice = atoi((row[6] ? row[6] : defaults[6]));
97 db_gethalfop = (with_halfops ? atoi((row[7] ? row[7] : defaults[7])) : 0);
98 db_canhalfop = (with_halfops ? atoi((row[8] ? row[8] : defaults[8])) : 0);
100 parseModeString(modelock, row[4]);
102 parseModeString(modelock, defaults[4]);
103 int uaccess = getChannelAccess(user, chan);
106 int sent_modes_locked = 0;
110 for(i = 0; i < strlen(modes); i++) {
125 cuser = searchUserByNick(carg);
127 break; //internal Bot error - this should never happen
129 caccess = getChannelAccess(cuser, chan);
130 if(modes[i] == 'o' && !add && cuser == client->user) {
131 //someone deopped the bot???
132 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
133 requestOp(client->user, chan);
134 } else if(modes[i] == 'o' && add && isBot(cuser)) {
135 //someone opped a bot
136 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
137 module_neonbackup_recover_chan(chan);
139 if((modes[i] == 'o' || (modes[i] == 'h' && !with_halfops)) && !(add && isBot(cuser))) {
140 if(uaccess < db_canop) {
141 reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
143 if(uaccess < db_getop)
146 if(db_canop == -1 && caccess < db_getop) {
147 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
148 modeBufferSet(modeBuf, !add, modes[i], carg);
151 } else if(modes[i] == 'h' && with_halfops && !(add && isBot(cuser))) {
152 if(uaccess < db_canhalfop) {
153 reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
155 if(uaccess < db_gethalfop)
158 if(db_canhalfop == -1 && caccess < db_gethalfop) {
159 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
160 modeBufferSet(modeBuf, !add, modes[i], carg);
163 } else if(modes[i] == 'v' && !(add && isBot(cuser))) {
164 if(uaccess < db_canvoice) {
165 reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
167 if(uaccess < db_getop)
170 if(db_canvoice == -1 && caccess < db_getvoice) {
171 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
172 modeBufferSet(modeBuf, !add, modes[i], carg);
179 //don't send this - just try to reop
180 //reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
181 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
182 modeBufferSet(modeBuf, !add, modes[i], carg);
185 if(isUserProtected(chan, cuser, user)) {
186 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
187 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
188 modeBufferSet(modeBuf, !add, modes[i], carg);
189 if(uaccess < db_getop)
200 if(uaccess < db_canban) {
201 reply(textclient, user, "NS_MODE_CANBAN", chan->name);
204 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
205 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
207 if(db_canban == -1) {
208 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed)) {
210 //check if a user just removed a bot ban
211 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
212 cuser = chanuser->user;
213 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
214 if(!match(carg, usermask) && isBot(cuser)) {
222 modeBufferSet(modeBuf, !add, modes[i], carg);
224 if(uaccess < db_getop)
228 carg = make_banmask(carg, hostmask_buffer);
230 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
231 cuser = chanuser->user;
232 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
233 if(!match(carg, usermask)) {
234 if(isUserProtected(chan, cuser, user)) {
235 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
242 if(match_count > 4 && (match_count * 3) > chan->usercount) {
243 reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
247 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
248 modeBufferSet(modeBuf, !add, modes[i], carg);
249 if(uaccess < db_getop)
254 modetype = getModeType(modelock, modes[i]);
256 break; //unknown mode
259 if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
264 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
265 char *modelock_val = getModeValue(modelock, modes[i]);
266 if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
267 if(!sent_modes_locked) {
268 getFullModeString(modelock, tmp);
269 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
270 sent_modes_locked = 1;
271 if(uaccess < db_getop)
274 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
275 modeBufferSet(modeBuf, add, modes[i], modelock_val);
279 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
280 int *modelock_val = getModeValue(modelock, modes[i]);
281 if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
282 if(!sent_modes_locked) {
283 getFullModeString(modelock, tmp);
284 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
285 sent_modes_locked = 1;
286 if(uaccess < db_getop)
289 sprintf(tmp, "%d", *modelock_val);
290 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
291 modeBufferSet(modeBuf, add, modes[i], tmp);
295 } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
302 if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
303 if(!sent_modes_locked) {
304 getFullModeString(modelock, tmp);
305 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
306 sent_modes_locked = 1;
307 if(uaccess < db_getop)
310 if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
311 modeBufferSet(modeBuf, !add, modes[i], carg);
317 if(deop_user && !neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed)) {
318 modeBufferDeop(modeBuf, user->nick);
320 freeModeBuffer(modeBuf);
321 freeModeNode(modelock);
324 static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, int *botwar_detect_executed) {
325 struct ChanUser *chanuser = getChanUser(user, chan);
326 if(!chanuser) return 0; //flying super cow?
327 if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
328 chanuser->changeTime = time(0);
329 chanuser->chageEvents = 1;
331 if(!*botwar_detect_executed)
332 chanuser->chageEvents++;
333 *botwar_detect_executed = 1;
334 if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
336 chanuser->changeTime = time(0);
337 if(chanuser->chageEvents > 0) {
338 char *alertchan = get_string_field("General.alertchan");
339 putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (alertchan ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
341 struct ChanNode *alertchan_chan = getChanByName(alertchan);
342 struct ClientSocket *alertclient;
343 if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
345 putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
349 chanuser->chageEvents = -2;