1 /* event_neonserv_mode.c - NeonServ v5.0
2 * Copyright (C) 2011 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);
22 static int botwar_detect_executed;
24 struct neonserv_event_mode_cache {
25 struct ClientSocket *client;
26 struct UserNode *user;
32 static void neonserv_event_mode(struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
33 struct ClientSocket *client = getBotForChannel(chan);
34 if(!client) return; //we can't "see" this event
35 if(isNetworkService(user)) return;
36 loadChannelSettings(chan);
37 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
38 struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
39 char **temp_argv = NULL;
41 temp_argv = malloc(argc*sizeof(*temp_argv));
42 if (!cache || (argc && !temp_argv)) {
43 perror("malloc() failed");
48 for(i = 0; i < argc; i++) {
49 temp_argv[i] = strdup(argv[i]);
52 cache->client = client;
54 cache->modes = strdup(modes);
55 cache->argv = temp_argv;
57 get_userlist_with_invisible(chan, neonserv_event_mode_userlist_lookup, cache);
60 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
61 struct neonserv_event_mode_cache *cache = data;
62 neonserv_event_mode_async1(cache->client, cache->user, chan, cache->modes, cache->argv, cache->argc);
65 for(i = 0; i < cache->argc; i++) {
73 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
74 struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
75 botwar_detect_executed = 0;
76 MYSQL_ROW row, defaults = NULL;
77 int i, arg, add = 1, skip = 0;
78 unsigned int modetype;
79 int db_canop, db_canvoice, db_canban, db_enfmodes;
80 struct ModeNode *modelock = createModeNode(NULL);
81 struct ModeBuffer *modeBuf;
82 struct UserNode *cuser;
83 struct ChanUser *chanuser;
84 modeBuf = initModeBuffer(client, chan);
85 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` 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) {
88 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` 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]));
96 parseModeString(modelock, row[4]);
98 parseModeString(modelock, defaults[4]);
99 int uaccess = getChannelAccess(user, chan, 0);
101 int sent_modes_locked = 0;
104 for(i = 0; i < strlen(modes); i++) {
118 cuser = searchUserByNick(carg);
119 if(modes[i] == 'o' && !(add && isBot(cuser))) {
120 if(uaccess < db_canop) {
121 reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
125 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
126 modeBufferSet(modeBuf, !add, modes[i], carg);
129 } else if(modes[i] == 'v') {
130 if(uaccess < db_canvoice) {
131 reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
134 if(db_canvoice == -1) {
135 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
136 modeBufferSet(modeBuf, !add, modes[i], carg);
141 break; //internal Bot error - this should never happen
146 reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
147 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
148 modeBufferSet(modeBuf, !add, modes[i], carg);
151 if(isUserProtected(chan, cuser, user)) {
152 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
153 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
154 modeBufferSet(modeBuf, !add, modes[i], carg);
164 if(uaccess < db_canban) {
165 reply(textclient, user, "NS_MODE_CANBAN", chan->name);
168 if(db_canban == -1) {
169 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
170 modeBufferSet(modeBuf, !add, modes[i], carg);
173 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
174 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
176 carg = make_banmask(carg, hostmask_buffer);
178 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
179 cuser = chanuser->user;
180 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
181 if(!match(carg, usermask)) {
182 if(isUserProtected(chan, cuser, user)) {
183 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
190 if(match_count > 4 && (match_count * 3) > chan->usercount) {
191 reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
194 if(skip && !neonserv_cmd_mode_botwar_detect(client, user, chan)) {
195 modeBufferSet(modeBuf, !add, modes[i], carg);
199 modetype = getModeType(modelock, modes[i]);
201 break; //unknown mode
204 if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
209 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
210 char *modelock_val = getModeValue(modelock, modes[i]);
211 if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
212 if(!sent_modes_locked) {
213 getFullModeString(modelock, tmp);
214 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
215 sent_modes_locked = 1;
217 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
218 modeBufferSet(modeBuf, add, modes[i], modelock_val);
222 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
223 int *modelock_val = getModeValue(modelock, modes[i]);
224 if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
225 if(!sent_modes_locked) {
226 getFullModeString(modelock, tmp);
227 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
228 sent_modes_locked = 1;
230 sprintf(tmp, "%d", *modelock_val);
231 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
232 modeBufferSet(modeBuf, add, modes[i], tmp);
236 } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
243 if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
244 if(!sent_modes_locked) {
245 getFullModeString(modelock, tmp);
246 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
247 sent_modes_locked = 1;
249 if(!neonserv_cmd_mode_botwar_detect(client, user, chan))
250 modeBufferSet(modeBuf, !add, modes[i], carg);
256 freeModeBuffer(modeBuf);
257 freeModeNode(modelock);
260 static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan) {
261 struct ChanUser *chanuser = getChanUser(user, chan);
262 if(!chanuser) return 0; //flying super cow?
263 if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
264 chanuser->changeTime = time(0);
265 chanuser->chageEvents = 1;
267 if(!botwar_detect_executed)
268 chanuser->chageEvents++;
269 botwar_detect_executed = 1;
270 if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
272 chanuser->changeTime = time(0);
273 if(chanuser->chageEvents > 0) {
274 putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (BOTWAR_ALERT_CHAN ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
275 if(BOTWAR_ALERT_CHAN) {
276 struct ChanNode *alertchan = getChanByName(BOTWAR_ALERT_CHAN);
277 struct ClientSocket *alertclient;
278 if(alertchan && (alertclient = getBotForChannel(chan)) != NULL) {
280 putsock(alertclient, "PRIVMSG %s :%s", alertchan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
284 chanuser->chageEvents = -2;