1 /* event_neonserv_ctcp.c - NeonServ v5.3
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 struct neonserv_event_ctcp_cache {
19 struct ClientSocket *client;
20 struct UserNode *user;
21 struct ChanNode *chan;
26 static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup);
27 static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text);
29 static void neonserv_event_chanctcp(struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
30 if(!stricmp(command, "ACTION")) return; //always allow CTCP ACTION (/me)
31 struct ClientSocket *client = getBotForChannel(chan);
32 if(!client) return; //we can't "see" this event
33 if(isNetworkService(user)) return;
34 loadChannelSettings(chan);
35 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
36 if(!(user->flags & USERFLAG_ISAUTHED)) {
37 struct neonserv_event_ctcp_cache *cache = malloc(sizeof(*cache));
39 perror("malloc() failed");
42 cache->client = client;
45 cache->command = strdup(command);
46 cache->text = (text ? strdup(text) : NULL);
47 get_userauth(user, neonserv_event_ctcp_nick_lookup, cache);
49 neonserv_event_ctcp_async1(client, user, chan, command, text);
52 static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup) {
53 struct neonserv_event_ctcp_cache *cache = data;
55 neonserv_event_ctcp_async1(cache->client, cache->user, cache->chan, cache->command, cache->text);
63 static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
65 MYSQL_ROW row, defaultrow = NULL, chanuser;
67 printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
69 if ((row = mysql_fetch_row(res)) == NULL) return;
70 if(!row[0] || !row[1]) {
71 printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
73 defaultrow = mysql_fetch_row(res);
75 int ctcpaccess = atoi((row[0] ? row[0] : defaultrow[0]));
76 if((user->flags & USERFLAG_ISAUTHED)) {
77 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));
79 chanuser = mysql_fetch_row(res);
81 uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
84 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
86 char *reason = "disallowed channel CTCP";
87 if(uaccess < ctcpaccess) {
88 switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
89 case 2: //TIMEBAN: 3min
94 banmask = generate_banmask(user, banmaskBuf);
95 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));
96 int banid = (int) mysql_insert_id(mysql_conn);
99 sprintf(nameBuf, "ban_%d", banid);
100 sprintf(banidBuf, "%d", banid);
101 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
104 banmask = generate_banmask(user, banmaskBuf);
105 putsock(client, "MODE %s +b %s", chan->name, banmask);
107 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);