+++ /dev/null
-/* event_neonserv_ctcp.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/>.
- */
-
-struct neonserv_event_ctcp_cache {
- struct ClientSocket *client;
- struct UserNode *user;
- struct ChanNode *chan;
- char *command;
- char *text;
-};
-
-static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup);
-static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text);
-
-static void neonserv_event_chanctcp(struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
- if(!stricmp(command, "ACTION")) return; //always allow CTCP ACTION (/me)
- 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;
- if(!(user->flags & USERFLAG_ISAUTHED)) {
- struct neonserv_event_ctcp_cache *cache = malloc(sizeof(*cache));
- if (!cache) {
- perror("malloc() failed");
- return;
- }
- cache->client = client;
- cache->user = user;
- cache->chan = chan;
- cache->command = strdup(command);
- cache->text = (text ? strdup(text) : NULL);
- get_userauth(user, neonserv_event_ctcp_nick_lookup, cache);
- } else
- neonserv_event_ctcp_async1(client, user, chan, command, text);
-}
-
-static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup) {
- struct neonserv_event_ctcp_cache *cache = data;
- if(user) {
- neonserv_event_ctcp_async1(cache->client, cache->user, cache->chan, cache->command, cache->text);
- }
- free(cache->command);
- if(cache->text)
- free(cache->text);
- free(cache);
-}
-
-static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
- MYSQL_RES *res;
- MYSQL_ROW row, defaultrow = NULL, chanuser;
- int uaccess = 0;
- printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
- res = mysql_use();
- if ((row = mysql_fetch_row(res)) == NULL) return;
- if(!row[0] || !row[1]) {
- printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
- res = mysql_use();
- defaultrow = mysql_fetch_row(res);
- }
- int ctcpaccess = atoi((row[0] ? row[0] : defaultrow[0]));
- if((user->flags & USERFLAG_ISAUTHED)) {
- printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' AND `user_user` = '%s'", chan->channel_id, escape_string(user->auth));
- res = mysql_use();
- chanuser = mysql_fetch_row(res);
- if(chanuser)
- uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
- }
- int duration = 0;
- char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
- char *banmask = NULL;
- char *reason = "disallowed channel CTCP";
- if(uaccess < ctcpaccess) {
- switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
- case 2: //TIMEBAN: 3min
- duration = 180;
- case 3: //TIMEBAN: 1h
- if(!duration)
- duration = 3600;
- banmask = generate_banmask(user, banmaskBuf);
- printf_mysql_query("INSERT INTO `bans` (`ban_channel`, `ban_mask`, `ban_triggered`, `ban_timeout`, `ban_owner`, `ban_reason`) VALUES ('%d', '%s', UNIX_TIMESTAMP(), '%lu', '%d', '%s')", chan->channel_id, escape_string(banmask), (unsigned long) (time(0) + duration), 0, escape_string(reason));
- int banid = (int) mysql_insert_id(get_mysql_conn());
- char nameBuf[MAXLEN];
- char banidBuf[20];
- sprintf(nameBuf, "ban_%d", banid);
- sprintf(banidBuf, "%d", banid);
- timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
- case 1: //KICKBAN
- if(!banmask)
- banmask = generate_banmask(user, banmaskBuf);
- putsock(client, "MODE %s +b %s", chan->name, banmask);
- case 0: //KICK
- putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
- break;
- }
- }
-}
-