ec88b8494e0fb1ddd15bcc3f13295a3f0b3838bd
[NeonServV5.git] / src / event_neonserv_ctcp.c
1 /* event_neonserv_ctcp.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 struct neonserv_event_ctcp_cache {
19     struct ClientSocket *client;
20     struct UserNode *user;
21     struct ChanNode *chan;
22     char *command;
23     char *text;
24 };
25
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);
28
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));
38         if (!cache) {
39             perror("malloc() failed");
40             return;
41         }
42         cache->client = client;
43         cache->user = user;
44         cache->chan = chan;
45         cache->command = strdup(command);
46         cache->text = (text ? strdup(text) : NULL);
47         get_userauth(user, neonserv_event_ctcp_nick_lookup, cache);
48     } else
49         neonserv_event_ctcp_async1(client, user, chan, command, text);
50 }
51
52 static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup) {
53     struct neonserv_event_ctcp_cache *cache = data;
54     if(user) {
55         neonserv_event_ctcp_async1(cache->client, cache->user, cache->chan, cache->command, cache->text);
56     }
57     free(cache->command);
58     if(cache->text)
59         free(cache->text);
60     free(cache);
61 }
62
63 static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
64     MYSQL_RES *res;
65     MYSQL_ROW row, defaultrow = NULL, chanuser;
66     int uaccess = 0;
67     printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
68     res = mysql_use();
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'");
72         res = mysql_use();
73         defaultrow = mysql_fetch_row(res);
74     }
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));
78         res = mysql_use();
79         chanuser = mysql_fetch_row(res);
80         if(chanuser)
81             uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
82     }
83     int duration = 0;
84     char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
85     char *banmask = NULL;
86     char *reason = "disallowed channel CTCP";
87     if(uaccess < ctcpaccess) {
88         switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
89             case 2: //TIMEBAN: 3min
90                 duration = 180;
91             case 3: //TIMEBAN: 1h
92                 if(!duration)
93                     duration = 3600;
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);
97                 char nameBuf[MAXLEN];
98                 char banidBuf[20];
99                 sprintf(nameBuf, "ban_%d", banid);
100                 sprintf(banidBuf, "%d", banid);
101                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
102             case 1: //KICKBAN
103                 if(!banmask)
104                     banmask = generate_banmask(user, banmaskBuf);
105                 putsock(client, "MODE %s +b %s", chan->name, banmask);
106             case 0: //KICK
107                 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
108                 break;
109         }
110     }
111 }
112