1 /* cmd_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 #include "cmd_neonserv.h"
22 * argv[1-*] - parameters
24 static USERLIST_CALLBACK(neonserv_cmd_mode_userlist_lookup);
25 static void neonserv_cmd_mode_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *mode);
27 struct neonserv_cmd_mode_cache {
28 struct ClientSocket *client, *textclient;
29 struct UserNode *user;
34 CMD_BIND(neonserv_cmd_mode) {
35 struct neonserv_cmd_mode_cache *cache = malloc(sizeof(*cache));
37 printf_log("neonserv", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
40 cache->client = client;
41 cache->textclient = textclient;
44 cache->mode = strdup(merge_argv(argv, 0, argc));
45 get_userlist_with_invisible(chan, module_id, neonserv_cmd_mode_userlist_lookup, cache);
48 static USERLIST_CALLBACK(neonserv_cmd_mode_userlist_lookup) {
49 struct neonserv_cmd_mode_cache *cache = data;
50 neonserv_cmd_mode_async1(cache->client, cache->textclient, cache->user, chan, cache->event, cache->mode);
55 static void neonserv_cmd_mode_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *mode) {
56 MYSQL_ROW row, defaults = NULL;
57 int i, arg, add = 1, skip = 0;
58 unsigned int modetype;
59 int db_canop, db_canvoice, db_canban, db_enfmodes;
60 struct ModeNode *modelock = createModeNode(NULL), *changemodes = createModeNode(NULL);
61 struct ModeBuffer *modeBuf;
62 struct UserNode *cuser;
63 struct ChanUser *chanuser;
64 modeBuf = initModeBuffer(client, chan);
65 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
66 row = mysql_fetch_row(mysql_use());
67 if(row[0] == NULL || row[1] == NULL || row[2] == NULL || row[3] == NULL || row[4] == NULL) {
68 printf_mysql_query("SELECT `channel_canop`, `channel_canvoice`, `channel_canban`, `channel_enfmodes`, `channel_modes` FROM `channels` WHERE `channel_name` = 'defaults'");
69 defaults = mysql_fetch_row(mysql_use());
71 db_canop = atoi((row[0] ? row[0] : defaults[0]));
72 db_canvoice = atoi((row[1] ? row[1] : defaults[1]));
73 db_canban = atoi((row[2] ? row[2] : defaults[2]));
74 db_enfmodes = atoi((row[3] ? row[3] : defaults[3]));
76 parseModeString(modelock, row[4]);
78 parseModeString(modelock, defaults[4]);
79 int uaccess = getChannelAccess(user, chan);
81 char *argv[MAXNUMPARAMS];
89 if(argc == MAXNUMPARAMS) break;
94 char *modeStr = argv[arg++];
96 for(i = 0; i < strlen(modeStr); i++) {
107 sprintf(modeStrBuf, "%c%c", (add ? '+' : '-'), modeStr[i]);
108 reply(textclient, user, "NS_MODE_INVALID", modeStrBuf);
112 if(modeStr[i] == 'o') {
113 if(uaccess < db_canop) {
114 reply(textclient, user, "NS_MODE_ENFOPS", chan->name);
118 if(db_canop == -1) break;
120 if(uaccess < db_canvoice) {
121 reply(textclient, user, "NS_MODE_ENFVOICE", chan->name);
125 if(db_canvoice == -1) break;
127 cuser = searchUserByNick(carg);
129 //check for an invisible user
130 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
131 if(!stricmp(chanuser->user->nick, carg)) {
132 cuser = chanuser->user;
138 chanuser = getChanUser(cuser, chan);
141 if(!(add ^ (chanuser->flags & (modeStr[i] == 'o' ? CHANUSERFLAG_OPPED : CHANUSERFLAG_VOICED)))) break;
144 if(modeStr[i] == 'o' && isNetworkService(cuser)) {
145 reply(textclient, user, "NS_SERVICE_IMMUNE", cuser->nick);
148 if(isUserProtected(chan, cuser, user)) {
149 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
153 modeBufferSet(modeBuf, add, modeStr[i], carg);
157 sprintf(modeStrBuf, "%c%c", (add ? '+' : '-'), modeStr[i]);
158 reply(textclient, user, "NS_MODE_INVALID", modeStrBuf);
162 if(uaccess < db_canban) {
163 reply(textclient, user, "NS_MODE_CANBAN", chan->name);
167 if(db_canban == -1) break;
168 char hostmask_buffer[NICKLEN+USERLEN+HOSTLEN+3];
169 char usermask[NICKLEN+USERLEN+HOSTLEN+3];
172 carg = make_banmask(carg, hostmask_buffer);
174 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
175 cuser = chanuser->user;
176 sprintf(usermask, "%s!%s@%s", cuser->nick, cuser->ident, cuser->host);
177 if(!match(carg, usermask)) {
178 if(isNetworkService(chanuser->user)) {
179 reply(textclient, user, "NS_SERVICE_IMMUNE", chanuser->user->nick);
183 if(isUserProtected(chan, cuser, user)) {
184 reply(textclient, user, "NS_USER_PROTECTED", cuser->nick);
189 if(match_count > 4 && (match_count * 3) > chan->usercount && !isGodMode(user)) {
191 reply(textclient, user, "NS_LAME_MASK", carg);
198 for(ban = chan->bans; ban; ban = ban->next) {
199 if(!match(carg, ban->mask)) {
206 modeBufferSet(modeBuf, add, 'b', carg);
210 modetype = getModeType(modelock, modeStr[i]);
212 sprintf(modeStrBuf, "%c%c", (add ? '+' : '-'), modeStr[i]);
213 reply(textclient, user, "NS_MODE_INVALID", modeStrBuf);
216 if(isModeAffected(modelock, modeStr[i]) && add == !isModeSet(modelock, modeStr[i]) && uaccess < db_enfmodes) {
218 event->flags |= CMDFLAG_OPLOG;
220 getFullModeString(modelock, tmp);
221 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
225 if(add && (modetype & CHANNEL_MODE_TYPE) != CHANNEL_MODE_TYPE_D) {
227 reply(textclient, user, "NS_MODE_INVALID", modeStr);
231 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modeStr[i])) {
232 char *modelock_val = getModeValue(modelock, modeStr[i]);
233 if(stricmp(carg, modelock_val)) {
235 event->flags |= CMDFLAG_OPLOG;
237 getFullModeString(modelock, tmp);
238 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
243 if((modetype & CHANNEL_MODE_VALUE) == CHANNEL_MODE_VALUE_STRING && isModeSet(modelock, modeStr[i])) {
244 int *modelock_val = getModeValue(modelock, modeStr[i]);
245 if(atoi(carg) != *modelock_val) {
247 event->flags |= CMDFLAG_OPLOG;
249 getFullModeString(modelock, tmp);
250 reply(textclient, user, "NS_MODE_LOCKED", tmp, chan->name);
255 } else if(!add && (modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_B) {
256 if(arg == argc && !(modetype & CHANNEL_MODE_KEY)) {
257 reply(textclient, user, "NS_MODE_INVALID", modeStr);
260 carg = (arg == argc ? NULL : argv[arg++]);
263 if((modetype & CHANNEL_MODE_TYPE) == CHANNEL_MODE_TYPE_D && isModeSet(chan->modes, modeStr[i]) == add)
265 if(!isModeAffected(changemodes, modeStr[i])) {
266 if(!add && (modetype & CHANNEL_MODE_KEY)) {
267 if(isModeSet(chan->modes, modeStr[i])) {
268 char *current_val = getModeValue(chan->modes, modeStr[i]);
272 if(parseMode(changemodes, add, modeStr[i], carg)) {
274 if(add && (modetype & CHANNEL_MODE_KEY) && isModeSet(chan->modes, modeStr[i])) {
275 char *current_val = getModeValue(chan->modes, modeStr[i]);
276 modeBufferSet(modeBuf, 0, modeStr[i], current_val);
277 flushModeBuffer(modeBuf);
279 if(!add && !isModeSet(chan->modes, modeStr[i])) break;
280 modeBufferSet(modeBuf, add, modeStr[i], carg);
282 modeBufferSimpleMode(modeBuf, add, modeStr[i]);
285 reply(textclient, user, "NS_MODE_INVALID", modeStr);
293 getFullModeString(changemodes, tmp);
294 freeModeBuffer(modeBuf);
296 reply(textclient, user, "NS_MODE_DONE", tmp);
299 freeModeNode(modelock);
300 freeModeNode(changemodes);