1 /* DBHelper.c - NeonServ v5.4
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 #include "ConfigParser.h"
31 void _loadUserSettings(struct UserNode *user) {
32 SYNCHRONIZE(cache_sync);
35 printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god`, `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
37 if ((row = mysql_fetch_row(res)) != NULL) {
38 user->language = get_language_by_tag(row[0]);
39 if(user->language == NULL) user->language = get_default_language();
40 if(strcmp(row[1], "0"))
41 user->flags |= USERFLAG_REPLY_PRIVMSG;
42 if(strcmp(row[2], "0"))
43 user->flags |= USERFLAG_GOD_MODE;
44 user->user_id = atoi(row[3]);
45 user->flags |= USERFLAG_HAS_USERID;
47 user->language = get_default_language();
48 user->flags |= USERFLAG_LOADED_SETTINGS;
49 DESYNCHRONIZE(cache_sync);
52 int isGodMode(struct UserNode *user) {
53 loadUserSettings(user);
54 return (user->flags & USERFLAG_GOD_MODE);
57 int getChannelAccess(struct UserNode *user, struct ChanNode *chan) {
58 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
59 loadChannelSettings(chan);
60 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
65 if(user->flags & USERFLAG_HAS_USERID)
66 userid = user->user_id;
68 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
70 if ((row = mysql_fetch_row(res)) != NULL) {
71 userid = atoi(row[0]);
72 user->user_id = userid;
73 user->flags |= USERFLAG_HAS_USERID;
77 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
79 if ((row = mysql_fetch_row(res)) != NULL) {
80 int cflags = atoi(row[1]);
81 if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
82 caccess = atoi(row[0]);
87 char *getChanDefault(char *channel_setting) {
90 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
92 if ((row = mysql_fetch_row(res)) == NULL) return "";
96 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_501) {
97 loadChannelSettings(chan);
98 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
99 if((user->flags & USERFLAG_ISIRCOP)) return 1;
102 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
104 if ((row = mysql_fetch_row(res)) == NULL) return 0;
105 int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
106 if(require_access == 0) return 1;
107 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
110 if(user->flags & USERFLAG_HAS_USERID)
111 userid = user->user_id;
113 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
115 if ((row = mysql_fetch_row(res)) != NULL) {
116 userid = atoi(row[0]);
117 user->user_id = userid;
118 user->flags |= USERFLAG_HAS_USERID;
123 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
125 if ((row = mysql_fetch_row(res)) != NULL) {
126 int cflags = atoi(row[1]);
127 if(!(cflags & DB_CHANUSER_SUSPENDED))
128 caccess = atoi(row[0]);
131 if(caccess >= require_access) return 1;
132 if(caccess == 500 && require_access == 501 && allow_501) return 1;
136 void _loadChannelSettings(struct ChanNode *chan) {
137 SYNCHRONIZE(cache_sync);
140 printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
142 if ((row = mysql_fetch_row(res)) != NULL) {
143 chan->flags |= CHANFLAG_CHAN_REGISTERED;
144 chan->channel_id = atoi(row[0]);
146 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
147 DESYNCHRONIZE(cache_sync);
150 //TODO: fix performance: we should cache the user access
151 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
152 /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
153 if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
155 /* Don't protect if no one is to be protected. */
159 loadChannelSettings(chan);
160 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
161 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
163 if(!(row = mysql_fetch_row(res))) return 0;
165 protection = (char) atoi(row[0]);
167 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
169 row = mysql_fetch_row(res);
170 protection = (char) atoi(row[0]);
172 if(protection == 3) return 0;
174 /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
175 int victim_access = getChannelAccess(victim, chan);
176 if (!victim_access && protection != 0) return 0;
178 /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
179 int issuer_access = getChannelAccess(issuer, chan);
180 if (!issuer_access) return 1;
182 /* If the aggressor was a user, then the victim can't be helped. */
183 if(!victim_access) return 0;
188 if(victim_access >= issuer_access) return 1;
191 if(victim_access > issuer_access) return 1;
197 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
198 loadChannelSettings(chan);
199 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
202 printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
204 while ((row = mysql_fetch_row(res)) != NULL) {
205 if(!match(row[0], mask))
211 int renameAccount(char *oldauth, char *newauth) {
212 MYSQL_RES *res, *res2;
214 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
216 if ((row = mysql_fetch_row(res)) != NULL) {
217 int userid = atoi(row[0]);
218 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(newauth));
220 if((row = mysql_fetch_row(res)) != NULL) {
222 int newuid = atoi(row[0]);
223 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
225 while((row = mysql_fetch_row(res)) != NULL) {
226 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
228 if((row2 = mysql_fetch_row(res2)) != NULL) {
229 if(atoi(row[0]) > atoi(row2[0])) {
230 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%s' WHERE `chanuser_id` = '%s'", row[0], row2[0]);
232 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
234 printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
236 printf_mysql_query("UPDATE `channels` SET `channel_registrator` = '%d' WHERE `channel_registrator` = '%d'", userid, newuid);
237 printf_mysql_query("UPDATE `bans` SET `ban_owner` = '%d' WHERE `ban_owner` = '%d'", userid, newuid);
238 printf_mysql_query("UPDATE `events` SET `auth` = '%s' WHERE `auth` = '%s'", escape_string(newauth), escape_string(oldauth));
239 printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = '%d' WHERE `godlog_uid` = '%d'", userid, newuid);
240 printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = '%d' WHERE `owner_history_to_uid` = '%d'", userid, newuid);
241 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
242 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
243 printf_mysql_query("UPDATE `noinvite` SET `uid` = '%d' WHERE `uid` = '%d'", userid, newuid);
244 printf_mysql_query("DELETE FROM `users` WHERE `chanuser_id` = '%d'", newuid);
246 //simply rename the account
247 printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
249 char *alertchan = get_string_field("General.CheckAuths.alertchan");
251 struct ChanNode *alertchan_chan = getChanByName(alertchan);
252 struct ClientSocket *alertclient;
253 if(alertchan_chan && (alertclient = getChannelBot(alertchan_chan, 0)) != NULL) {
254 putsock(alertclient, "PRIVMSG %s :Renamed User %s to %s", alertchan_chan->name, oldauth, newauth);
262 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
264 struct event_user_registered_cache {
265 struct UserNode *new_user;
269 static int event_user_registered(struct UserNode *old_user, struct UserNode *new_user) {
270 //check if there is a fakehost on both sides...
271 if(!isFakeHost(old_user->host) || !isFakeHost(new_user->host)) return 0;
273 char oldauth[AUTHLEN], newauth[AUTHLEN];
275 if((p = strstr(old_user->host, "."))) {
277 strcpy(oldauth, old_user->host);
280 if((p = strstr(new_user->host, "."))) {
282 strcpy(newauth, new_user->host);
285 if(!stricmp(oldauth, newauth))
287 //check if we know this user; then check the new auth
290 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
292 if ((row = mysql_fetch_row(res)) != NULL) {
293 struct event_user_registered_cache *cache = malloc(sizeof(*cache));
295 perror("malloc() failed");
298 cache->new_user = new_user;
299 cache->oldauth = strdup(oldauth);
300 lookup_authname(newauth, 0, event_user_registered_auth_lookup, cache);
305 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
306 struct event_user_registered_cache *cache = data;
308 renameAccount(cache->oldauth, auth);
309 strcpy(cache->new_user->auth, auth);
310 cache->new_user->flags |= USERFLAG_ISAUTHED;
312 free(cache->oldauth);
316 void deleteUser(int userid) {
317 //simply delete the user
318 MYSQL_RES *res, *res2;
320 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);
322 while((row = mysql_fetch_row(res))) {
323 if(!strcmp(row[0], "500") && !strcmp(row[2], "1")) {
325 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]);
327 while((row2 = mysql_fetch_row(res2))) {
328 struct ClientSocket *bot;
329 int clientid = atoi(row2[0]);
330 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
331 if(bot->clientid == clientid)
332 putsock(bot, "PART %s :Channel unregistered.", row2[1]);
335 printf_mysql_query("DELETE FROM `bot_channels` WHERE `chanid` = '%s'", row[1]);
338 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
339 printf_mysql_query("UPDATE `bans` SET `ban_owner` = 0 WHERE `ban_owner` = '%d'", userid);
340 printf_mysql_query("UPDATE `donotregister` SET `dnr_user` = 0 WHERE `dnr_user` = '%d'", userid);
341 printf_mysql_query("UPDATE `bans` SET `ban_owner` = 0 WHERE `ban_owner` = '%d'", userid);
342 printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = 0 WHERE `godlog_uid` = '%d'", userid);
343 printf_mysql_query("DELETE FROM `noinvite` WHERE `uid` = '%d'", userid);
344 printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = 0 WHERE `owner_history_to_uid` = '%d'", userid);
345 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = 0 WHERE `owner_history_from_uid` = '%d'", userid);
346 printf_mysql_query("UPDATE `channels` SET `channel_registrator` = 0 WHERE `channel_registrator` = '%d'", userid);
347 printf_mysql_query("DELETE FROM `users` WHERE `user_id` = '%d'", userid);
348 struct UserNode *user;
349 for(user = getAllUsers(NULL); user; user = getAllUsers(user)) {
350 if(user->flags & USERFLAG_HAS_USERID)
351 user->flags &= ~USERFLAG_HAS_USERID;
355 void init_DBHelper() {
356 bind_registered(event_user_registered, 0);