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);
21 struct neonserv_event_mode_cache {
22 struct ClientSocket *client;
23 struct UserNode *user;
29 static void neonserv_event_mode(struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
30 struct ClientSocket *client = getBotForChannel(chan);
31 if(!client) return; //we can't "see" this event
32 if(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) return;
33 loadChannelSettings(chan);
34 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
35 struct neonserv_event_mode_cache *cache = malloc(sizeof(*cache));
36 char **temp_argv = NULL;
38 temp_argv = malloc(argc*sizeof(*temp_argv));
39 if (!cache || (argc && !temp_argv)) {
40 perror("malloc() failed");
45 for(i = 0; i < argc; i++) {
46 temp_argv[i] = strdup(argv[i]);
49 cache->client = client;
51 cache->modes = strdup(modes);
52 cache->argv = temp_argv;
54 get_userlist_with_invisible(chan, neonserv_event_mode_userlist_lookup, cache);
57 static USERLIST_CALLBACK(neonserv_event_mode_userlist_lookup) {
58 struct neonserv_event_mode_cache *cache = data;
59 neonserv_event_mode_async1(cache->client, cache->user, chan, cache->modes, cache->argv, cache->argc);
62 for(i = 0; i < cache->argc; i++) {
70 static void neonserv_event_mode_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *modes, char **argv, int argc) {
71 struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
72 MYSQL_ROW row, defaults = NULL;
73 int i, arg, add = 1, skip = 0;
74 unsigned int modetype;
75 int db_canop, db_canvoice, db_canban, db_enfmodes;
76 struct ModeNode *modelock = createModeNode(NULL);
77 struct ModeBuffer *modeBuf;
78 struct UserNode *cuser;
79 struct ChanUser *chanuser;
80 modeBuf = initModeBuffer(client, chan);
81 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
82 row = mysql_fetch_row(mysql_use());
83 if(row[0] == NULL || row[1] == NULL || row[2] == NULL || row[3] == NULL) {
84 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` FROM `channels` WHERE `channel_name` = 'defaults'");
85 defaults = mysql_fetch_row(mysql_use());
87 db_canop = atoi((row[0] ? row[0] : defaults[0]));
88 db_canvoice = atoi((row[1] ? row[1] : defaults[1]));
89 db_canban = atoi((row[2] ? row[2] : defaults[2]));
90 db_enfmodes = atoi((row[3] ? row[3] : defaults[3]));
92 parseModeString(modelock, row[4]);
94 parseModeString(modelock, defaults[4]);
95 int uaccess = getChannelAccess(user, chan, 0);
97 int sent_modes_locked = 0;
100 for(i = 0; i < strlen(modes); i++) {
114 if(modes[i] == 'o') {
115 if(uaccess < db_canop) {
116 reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
120 modeBufferSet(modeBuf, !add, modes[i], carg);
124 if(uaccess < db_canvoice) {
125 reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
128 if(db_canvoice == -1) {
129 modeBufferSet(modeBuf, !add, modes[i], carg);
133 cuser = searchUserByNick(carg);
135 break; //internal Bot error - this should never happen
140 reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
141 modeBufferSet(modeBuf, !add, modes[i], carg);
144 if(isUserProtected(chan, cuser, user)) {
145 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
146 modeBufferSet(modeBuf, !add, modes[i], carg);
156 if(uaccess < db_canban) {
157 reply(textclient, user, "NS_MODE_CANBAN", chan->name);
160 if(db_canban == -1) {
161 modeBufferSet(modeBuf, !add, modes[i], carg);
164 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
165 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
167 carg = make_banmask(carg, hostmask_buffer);
169 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
170 cuser = chanuser->user;
171 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
172 if(!match(carg, usermask)) {
173 if(isUserProtected(chan, cuser, user)) {
174 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
181 if(match_count > 4 && (match_count * 3) > chan->usercount) {
182 reply(textclient, user, "NS_LAME_MASK_WARNING", carg, match_count);
186 modeBufferSet(modeBuf, !add, modes[i], carg);
190 modetype = getModeType(modelock, modes[i]);
192 break; //unknown mode
195 if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
200 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
201 char *modelock_val = getModeValue(modelock, modes[i]);
202 if(stricmp(carg, modelock_val) && uaccess < db_enfmodes && !isGodMode(user)) {
203 if(!sent_modes_locked) {
204 getFullModeString(modelock, tmp);
205 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
206 sent_modes_locked = 1;
208 modeBufferSet(modeBuf, add, modes[i], modelock_val);
212 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modes[i])) {
213 int *modelock_val = getModeValue(modelock, modes[i]);
214 if(atoi(carg) != *modelock_val && uaccess < db_enfmodes && !isGodMode(user)) {
215 if(!sent_modes_locked) {
216 getFullModeString(modelock, tmp);
217 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
218 sent_modes_locked = 1;
220 sprintf(tmp, "%d", *modelock_val);
221 modeBufferSet(modeBuf, add, modes[i], tmp);
225 } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
232 if(isModeAffected(modelock, modes[i]) && add == !isModeSet(modelock, modes[i]) && uaccess < db_enfmodes && !isGodMode(user)) {
233 if(!sent_modes_locked) {
234 getFullModeString(modelock, tmp);
235 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
236 sent_modes_locked = 1;
238 modeBufferSet(modeBuf, !add, modes[i], carg);
244 freeModeBuffer(modeBuf);
245 freeModeNode(modelock);