1 /* DBHelper.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/>.
22 #include "mysqlConn.h"
25 #include "IRCEvents.h"
26 #include "HandleInfoHandler.h"
27 #include "ClientSocket.h"
29 void _loadUserSettings(struct UserNode *user) {
30 SYNCHRONIZE(cache_sync);
33 printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god`, `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
35 if ((row = mysql_fetch_row(res)) != NULL) {
36 user->language = get_language_by_tag(row[0]);
37 if(user->language == NULL) user->language = get_default_language();
38 if(strcmp(row[1], "0"))
39 user->flags |= USERFLAG_REPLY_PRIVMSG;
40 if(strcmp(row[2], "0"))
41 user->flags |= USERFLAG_GOD_MODE;
42 user->user_id = atoi(row[3]);
43 user->flags |= USERFLAG_HAS_USERID;
45 user->language = get_default_language();
46 user->flags |= USERFLAG_LOADED_SETTINGS;
47 DESYNCHRONIZE(cache_sync);
50 int isGodMode(struct UserNode *user) {
51 loadUserSettings(user);
52 return (user->flags & USERFLAG_GOD_MODE);
55 int getChannelAccess(struct UserNode *user, struct ChanNode *chan) {
56 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
57 loadChannelSettings(chan);
58 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
63 if(user->flags & USERFLAG_HAS_USERID)
64 userid = user->user_id;
66 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
68 if ((row = mysql_fetch_row(res)) != NULL) {
69 userid = atoi(row[0]);
70 user->user_id = userid;
71 user->flags |= USERFLAG_HAS_USERID;
75 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
77 if ((row = mysql_fetch_row(res)) != NULL) {
78 int cflags = atoi(row[1]);
79 if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
80 caccess = atoi(row[0]);
85 char *getChanDefault(char *channel_setting) {
88 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
90 if ((row = mysql_fetch_row(res)) == NULL) return "";
94 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_501) {
95 loadChannelSettings(chan);
96 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
97 if((user->flags & USERFLAG_ISIRCOP)) return 1;
100 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
102 if ((row = mysql_fetch_row(res)) == NULL) return 0;
103 int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
104 if(require_access == 0) return 1;
105 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
108 if(user->flags & USERFLAG_HAS_USERID)
109 userid = user->user_id;
111 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
113 if ((row = mysql_fetch_row(res)) != NULL) {
114 userid = atoi(row[0]);
115 user->user_id = userid;
116 user->flags |= USERFLAG_HAS_USERID;
121 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
123 if ((row = mysql_fetch_row(res)) != NULL) {
124 int cflags = atoi(row[1]);
125 if(!(cflags & DB_CHANUSER_SUSPENDED))
126 caccess = atoi(row[0]);
129 if(caccess >= require_access) return 1;
130 if(caccess == 500 && require_access == 501 && allow_501) return 1;
134 void _loadChannelSettings(struct ChanNode *chan) {
135 SYNCHRONIZE(cache_sync);
138 printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
140 if ((row = mysql_fetch_row(res)) != NULL) {
141 chan->flags |= CHANFLAG_CHAN_REGISTERED;
142 chan->channel_id = atoi(row[0]);
144 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
145 DESYNCHRONIZE(cache_sync);
148 //TODO: fix performance: we should cache the user access
149 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
150 /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
151 if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
153 /* Don't protect if no one is to be protected. */
157 loadChannelSettings(chan);
158 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
159 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
161 if(!(row = mysql_fetch_row(res))) return 0;
163 protection = (char) atoi(row[0]);
165 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
167 row = mysql_fetch_row(res);
168 protection = (char) atoi(row[0]);
170 if(protection == 3) return 0;
172 /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
173 int victim_access = getChannelAccess(victim, chan);
174 if (!victim_access && protection != 0) return 0;
176 /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
177 int issuer_access = getChannelAccess(issuer, chan);
178 if (!issuer_access) return 1;
180 /* If the aggressor was a user, then the victim can't be helped. */
181 if(!victim_access) return 0;
186 if(victim_access >= issuer_access) return 1;
189 if(victim_access > issuer_access) return 1;
195 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
196 loadChannelSettings(chan);
197 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
200 printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
202 while ((row = mysql_fetch_row(res)) != NULL) {
203 if(!match(row[0], mask))
209 int renameAccount(char *oldauth, char *newauth) {
210 MYSQL_RES *res, *res2;
212 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
214 if ((row = mysql_fetch_row(res)) != NULL) {
215 int userid = atoi(row[0]);
216 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(newauth));
218 if((row = mysql_fetch_row(res)) != NULL) {
220 int newuid = atoi(row[0]);
221 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
223 while((row = mysql_fetch_row(res)) != NULL) {
224 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
226 if((row2 = mysql_fetch_row(res2)) != NULL) {
227 if(atoi(row[0]) > atoi(row2[0])) {
228 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%s' WHERE `chanuser_id` = '%s'", row[0], row2[0]);
230 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
232 printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
234 printf_mysql_query("UPDATE `channels` SET `channel_registrator` = '%d' WHERE `channel_registrator` = '%d'", userid, newuid);
235 printf_mysql_query("UPDATE `bans` SET `ban_owner` = '%d' WHERE `ban_owner` = '%d'", userid, newuid);
236 printf_mysql_query("UPDATE `events` SET `auth` = '%s' WHERE `auth` = '%s'", escape_string(newauth), escape_string(oldauth));
237 printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = '%d' WHERE `godlog_uid` = '%d'", userid, newuid);
238 printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = '%d' WHERE `owner_history_to_uid` = '%d'", userid, newuid);
239 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
240 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
241 printf_mysql_query("UPDATE `noinvite` SET `uid` = '%d' WHERE `uid` = '%d'", userid, newuid);
242 printf_mysql_query("DELETE FROM `users` WHERE `chanuser_id` = '%d'", newuid);
244 //simply rename the account
245 printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
252 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
254 struct event_user_registered_cache {
255 struct UserNode *new_user;
259 static int event_user_registered(struct UserNode *old_user, struct UserNode *new_user) {
260 //check if there is a fakehost on both sides...
261 if(!isFakeHost(old_user->host) || !isFakeHost(new_user->host)) return 0;
263 char oldauth[AUTHLEN], newauth[AUTHLEN];
265 if((p = strstr(old_user->host, "."))) {
267 strcpy(oldauth, old_user->host);
270 if((p = strstr(new_user->host, "."))) {
272 strcpy(newauth, new_user->host);
275 if(!stricmp(oldauth, newauth))
277 //check if we know this user; then check the new auth
280 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
282 if ((row = mysql_fetch_row(res)) != NULL) {
283 struct event_user_registered_cache *cache = malloc(sizeof(*cache));
285 perror("malloc() failed");
288 cache->new_user = new_user;
289 cache->oldauth = strdup(oldauth);
290 lookup_authname(newauth, 0, event_user_registered_auth_lookup, cache);
295 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
296 struct event_user_registered_cache *cache = data;
298 renameAccount(cache->oldauth, auth);
299 strcpy(cache->new_user->auth, auth);
300 cache->new_user->flags |= USERFLAG_ISAUTHED;
302 free(cache->oldauth);
306 void deleteUser(int userid) {
307 //simply delete the user
308 MYSQL_RES *res, *res2;
310 printf_mysql_query("SELECT a.`chanuser_access`, a.`chanuser_cid`, (SELECT COUNT(*) FROM `chanusers` AS b WHERE b.`chanuser_cid` = a.`chanuser_cid` AND b.`chanuser_access` = 500) FROM `chanusers` AS a WHERE a.`chanuser_uid` = '%d'", userid);
312 while((row = mysql_fetch_row(res))) {
313 if(!strcmp(row[0], "500") && !strcmp(row[2], "1")) {
315 printf_mysql_query("SELECT `botid`, `channel_name` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `chanid` = '%s' AND `suspended` = '0'", row[1]);
317 while((row2 = mysql_fetch_row(res))) {
318 struct ClientSocket *bot;
319 int clientid = atoi(row2[0]);
320 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
321 if(bot->clientid == clientid)
322 putsock(bot, "PART %s :Channel unregistered.", row2[1]);
325 printf_mysql_query("DELETE FROM `bot_channels` WHERE `chanid` = '%s'", row[1]);
328 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
329 printf_mysql_query("UPDATE `bans` SET `ban_owner` = 0 WHERE `ban_owner` = '%d'", userid);
330 printf_mysql_query("UPDATE `donotregister` SET `dnr_user` = 0 WHERE `dnr_user` = '%d'", userid);
331 printf_mysql_query("UPDATE `bans` SET `ban_owner` = 0 WHERE `ban_owner` = '%d'", userid);
332 printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = 0 WHERE `godlog_uid` = '%d'", userid);
333 printf_mysql_query("DELETE FROM `noinvite` WHERE `uid` = '%d'", userid);
334 printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = 0 WHERE `owner_history_to_uid` = '%d'", userid);
335 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = 0 WHERE `owner_history_from_uid` = '%d'", userid);
336 printf_mysql_query("UPDATE `channels` SET `channel_registrator` = 0 WHERE `channel_registrator` = '%d'", userid);
337 printf_mysql_query("DELETE FROM `users` WHERE `user_id` = '%d'", userid);
338 struct UserNode *user;
339 for(user = getAllUsers(NULL); user; user = getAllUsers(user)) {
340 if(user->flags & USERFLAG_HAS_USERID)
341 user->flags &= ~USERFLAG_HAS_USERID;
345 void init_DBHelper() {
346 bind_registered(event_user_registered, 0);