*** VERSION 5.1.0 ***
[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 static int neonserv_ctcp(char *buffer, char *command, char *text);
29
30 static void neonserv_event_chanctcp(struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
31     if(!stricmp(command, "ACTION")) return; //always allow CTCP ACTION (/me)
32     struct ClientSocket *client = getBotForChannel(chan);
33     if(!client) return; //we can't "see" this event
34     if(isNetworkService(user)) return; 
35     loadChannelSettings(chan);
36     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
37     if(!(user->flags & USERFLAG_ISAUTHED)) {
38         struct neonserv_event_ctcp_cache *cache = malloc(sizeof(*cache));
39         if (!cache) {
40             perror("malloc() failed");
41             return;
42         }
43         cache->client = client;
44         cache->user = user;
45         cache->chan = chan;
46         cache->command = strdup(command);
47         cache->text = (text ? strdup(text) : NULL);
48         get_userauth(user, neonserv_event_ctcp_nick_lookup, cache);
49     } else
50         neonserv_event_ctcp_async1(client, user, chan, command, text);
51 }
52
53 static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup) {
54     struct neonserv_event_ctcp_cache *cache = data;
55     if(user) {
56         neonserv_event_ctcp_async1(cache->client, cache->user, cache->chan, cache->command, cache->text);
57     }
58     free(cache->command);
59     if(cache->text)
60         free(cache->text);
61     free(cache);
62 }
63
64 static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
65     MYSQL_RES *res;
66     MYSQL_ROW row, defaultrow = NULL, chanuser;
67     int uaccess = 0;
68     printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
69     res = mysql_use();
70     if ((row = mysql_fetch_row(res)) == NULL) return;
71     if(!row[0] || !row[1]) {
72         printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
73         res = mysql_use();
74         defaultrow = mysql_fetch_row(res);
75     }
76     int ctcpaccess = atoi((row[0] ? row[0] : defaultrow[0]));
77     if((user->flags & USERFLAG_ISAUTHED)) {
78         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         res = mysql_use();
80         chanuser = mysql_fetch_row(res);
81         if(chanuser)
82             uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
83     }
84     int duration = 0;
85     char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
86     char *banmask = NULL;
87     char *reason = "disallowed channel CTCP";
88     if(uaccess < ctcpaccess) {
89         switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
90             case 2: //TIMEBAN: 3min
91                 duration = 180;
92             case 3: //TIMEBAN: 1h
93                 if(!duration)
94                     duration = 3600;
95                 banmask = generate_banmask(user, banmaskBuf);
96                 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));
97                 int banid = (int) mysql_insert_id(mysql_conn);
98                 char nameBuf[MAXLEN];
99                 char banidBuf[20];
100                 sprintf(nameBuf, "ban_%d", banid);
101                 sprintf(banidBuf, "%d", banid);
102                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
103             case 1: //KICKBAN
104                 if(!banmask)
105                     banmask = generate_banmask(user, banmaskBuf);
106                 putsock(client, "MODE %s +b %s", chan->name, banmask);
107             case 0: //KICK
108                 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
109                 break;
110         }
111     }
112 }
113
114 static void neonserv_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
115     char ctcpBuf[MAXLEN];
116     if(neonserv_ctcp(ctcpBuf, command, text)) {
117         struct ClientSocket *bot;
118         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
119             if(bot->user == target) break;
120         }
121         if(bot)
122             putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
123     }
124 }
125
126 static int neonserv_ctcp(char *buffer, char *command, char *text) {
127     if(!stricmp(command, "VERSION")) {
128         sprintf(buffer, "VERSION NeonServ v" NEONSERV_VERSION " by pk910 (%s)", (strcmp(revision, "") ? revision : "-"));
129         return 1;
130     }
131     if(!stricmp(command, "FINGER")) {
132         sprintf(buffer, "FINGER NeonServ v" NEONSERV_VERSION " (%s) build %s lines C code using " COMPILER " (see +netinfo)", (strcmp(revision, "") ? revision : "-"), codelines);
133         return 1;
134     }
135     if(!stricmp(command, "PING")) {
136         sprintf(buffer, "PING %s", (text ? text : "0"));
137         return 1;
138     }
139     if(!stricmp(command, "TIME")) {
140         time_t rawtime;
141         struct tm *timeinfo;
142         char timeBuf[80];
143         time(&rawtime);
144         timeinfo = localtime(&rawtime);
145         strftime(timeBuf, 80, "%c", timeinfo);
146         sprintf(buffer, "TIME %s", timeBuf);
147         return 1;
148     }
149     return 0;
150 }