+++ /dev/null
-/* event_neonserv_mode.c - NeonServ v5.3
- * Copyright (C) 2011-2012 Philipp Kreil (pk910)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup);
-static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc);
-static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, int *botwar_detect_executed);
-
-struct neonserv_event_mode_cache {
- struct ClientSocket *client;
- struct UserNode *user;
- char *modes;
- char **argv;
- int argc;
-};
-
-static void neonserv_event_mode(struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
- struct ClientSocket *client = getBotForChannel(chan);
- if(!client) return; //we can't "see" this event
- if(isNetworkService(user)) return;
- loadChannelSettings(chan);
- if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
- struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
- char **temp_argv = NULL;
- if(argc)
- temp_argv = malloc(argc*sizeof(*temp_argv));
- if (!cache || (argc && !temp_argv)) {
- perror("malloc() failed");
- return;
- }
- if(argc) {
- int i;
- for(i = 0; i < argc; i++) {
- temp_argv[i] = strdup(argv[i]);
- }
- }
- cache->client = client;
- cache->user = user;
- cache->modes = strdup(modes);
- cache->argv = temp_argv;
- cache->argc = argc;
- get_userlist_with_invisible(chan, neonserv_event_mode_userlist_lookup, cache);
-}
-
-static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
- struct neonserv_event_mode_cache *cache = data;
- neonserv_event_mode_async1(cache->client, cache->user, chan, cache->modes, cache->argv, cache->argc);
- if(cache->argc) {
- int i;
- for(i = 0; i < cache->argc; i++) {
- free(cache->argv[i]);
- }
- free(cache->argv);
- }
- free(cache->modes);
- free(cache);
-}
-
-static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
- struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
- int botwar_detect_executed = 0;
- MYSQL_ROW row, defaults = NULL;
- int i, arg, add = 1, skip = 0;
- unsigned int modetype;
- int db_canop, db_canhalfop, db_canvoice, db_canban, db_enfmodes, db_getop, db_gethalfop, db_getvoice;
- struct ModeNode *modelock = createModeNode(NULL);
- struct ModeBuffer *modeBuf;
- struct UserNode *cuser;
- struct ChanUser *chanuser;
- int with_halfops = get_int_field("General.have_halfop");
- modeBuf = initModeBuffer(client, chan);
- 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);
- row = mysql_fetch_row(mysql_use());
- 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)) {
- 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'");
- defaults = mysql_fetch_row(mysql_use());
- }
- db_canop = atoi((row[0] ? row[0] : defaults[0]));
- db_canvoice = atoi((row[1] ? row[1] : defaults[1]));
- db_canban = atoi((row[2] ? row[2] : defaults[2]));
- db_enfmodes = atoi((row[3] ? row[3] : defaults[3]));
- db_getop = atoi((row[5] ? row[5] : defaults[5]));
- db_getvoice = atoi((row[6] ? row[6] : defaults[6]));
- db_gethalfop = (with_halfops ? atoi((row[7] ? row[7] : defaults[7])) : 0);
- db_canhalfop = (with_halfops ? atoi((row[8] ? row[8] : defaults[8])) : 0);
- if(row[4])
- parseModeString(modelock, row[4]);
- else if(defaults[4])
- parseModeString(modelock, defaults[4]);
- int uaccess = getChannelAccess(user, chan);
- int caccess;
- char *carg;
- int sent_modes_locked = 0;
- char deop_user = 0;
- char tmp[MAXLEN];
- arg = 0;
- for(i = 0; i < strlen(modes); i++) {
- switch(modes[i]) {
- case '+':
- add = 1;
- break;
- case '-':
- add = 0;
- break;
- case 'o':
- case 'h':
- case 'v':
- if(arg == argc) {
- break;
- }
- carg = argv[arg++];
- cuser = searchUserByNick(carg);
- if(!cuser) {
- break; //internal Bot error - this should never happen
- }
- caccess = getChannelAccess(cuser, chan);
- if(modes[i] == 'o' && !add && cuser == client->user) {
- //someone deopped the bot???
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- requestOp(client->user, chan);
- }
- if((modes[i] == 'o' || (modes[i] == 'h' && !with_halfops)) && !(add && isBot(cuser))) {
- if(uaccess < db_canop) {
- reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
- db_canop = -1;
- if(uaccess < db_getop)
- deop_user = 1;
- }
- if(db_canop == -1 && caccess < db_getop) {
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- break;
- }
- } else if(modes[i] == 'h' && with_halfops && !(add && isBot(cuser))) {
- if(uaccess < db_canhalfop) {
- reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
- db_canhalfop = -1;
- if(uaccess < db_gethalfop)
- deop_user = 1;
- }
- if(db_canhalfop == -1 && caccess < db_gethalfop) {
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- break;
- }
- } else if(modes[i] == 'v' && !(add && isBot(cuser))) {
- if(uaccess < db_canvoice) {
- reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
- db_canvoice = -1;
- if(uaccess < db_getop)
- deop_user = 1;
- }
- if(db_canvoice == -1 && caccess < db_getvoice) {
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- break;
- }
- }
- if(!add) {
- //check protection
- if(isBot(cuser)) {
- //don't send this - just try to reop
- //reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- break;
- }
- if(isUserProtected(chan, cuser, user)) {
- reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- if(uaccess < db_getop)
- deop_user = 1;
- break;
- }
- }
- break;
- case 'b':
- if(arg == argc) {
- break;
- }
- carg = argv[arg++];
- if(uaccess < db_canban) {
- reply(textclient, user, "NS_MODE_CANBAN", chan->name);
- db_canban = -1;
- }
- if(db_canban == -1) {
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- if(uaccess < db_getop)
- deop_user = 1;
- break;
- }
- char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
- char usermask[NICKLEN+USERLEN+HOSTLEN+3];
- int match_count = 0;
- carg = make_banmask(carg, hostmask_buffer);
- if(add) {
- for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
- cuser = chanuser->user;
- sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
- if(!match(carg, usermask)) {
- if(isUserProtected(chan, cuser, user)) {
- reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
- skip = 1;
- break;
- }
- match_count++;
- }
- }
- if(match_count > 4 && (match_count * 3) > chan->usercount) {
- reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
- }
- }
- if(skip) {
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- if(uaccess < db_getop)
- deop_user = 1;
- }
- break;
- default:
- modetype = getModeType(modelock, modes[i]);
- if(modetype == 0) {
- break; //unknown mode
- }
-
- if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
- if(arg == argc) {
- break;
- }
- carg = argv[arg++];
- if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
- char *modelock_val = getModeValue(modelock, modes[i]);
- if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
- if(!sent_modes_locked) {
- getFullModeString(modelock, tmp);
- reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
- sent_modes_locked = 1;
- if(uaccess < db_getop)
- deop_user = 1;
- }
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, add, modes[i], modelock_val);
- break;
- }
- }
- if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
- int *modelock_val = getModeValue(modelock, modes[i]);
- if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
- if(!sent_modes_locked) {
- getFullModeString(modelock, tmp);
- reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
- sent_modes_locked = 1;
- if(uaccess < db_getop)
- deop_user = 1;
- }
- sprintf(tmp, "%d", *modelock_val);
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, add, modes[i], tmp);
- break;
- }
- }
- } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
- if(arg == argc) {
- break;
- }
- carg = argv[arg++];
- } else
- carg = NULL;
- if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
- if(!sent_modes_locked) {
- getFullModeString(modelock, tmp);
- reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
- sent_modes_locked = 1;
- if(uaccess < db_getop)
- deop_user = 1;
- }
- if(!neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed))
- modeBufferSet(modeBuf, !add, modes[i], carg);
- break;
- }
- break;
- }
- }
- if(deop_user && !neonserv_cmd_mode_botwar_detect(client, user, chan, &botwar_detect_executed)) {
- modeBufferDeop(modeBuf, user->nick);
- }
- freeModeBuffer(modeBuf);
- freeModeNode(modelock);
-}
-
-static int neonserv_cmd_mode_botwar_detect(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, int *botwar_detect_executed) {
- struct ChanUser *chanuser = getChanUser(user, chan);
- if(!chanuser) return 0; //flying super cow?
- if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
- chanuser->changeTime = time(0);
- chanuser->chageEvents = 1;
- } else {
- if(!*botwar_detect_executed)
- chanuser->chageEvents++;
- *botwar_detect_executed = 1;
- if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
- //BOTWAR!
- chanuser->changeTime = time(0);
- if(chanuser->chageEvents > 0) {
- char *alertchan = get_string_field("General.alertchan");
- putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (alertchan ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
- if(alertchan) {
- struct ChanNode *alertchan_chan = getChanByName(alertchan);
- struct ClientSocket *alertclient;
- if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
- char msgBuf[MAXLEN];
- putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
- }
- }
- }
- chanuser->chageEvents = -2;
- return 1;
- }
- }
- return 0;
-}
-