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"
28 void _loadUserSettings(struct UserNode *user) {
29 SYNCHRONIZE(cache_sync);
32 printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god`, `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
34 if ((row = mysql_fetch_row(res)) != NULL) {
35 user->language = get_language_by_tag(row[0]);
36 if(user->language == NULL) user->language = get_default_language();
37 if(strcmp(row[1], "0"))
38 user->flags |= USERFLAG_REPLY_PRIVMSG;
39 if(strcmp(row[2], "0"))
40 user->flags |= USERFLAG_GOD_MODE;
41 user->user_id = atoi(row[3]);
42 user->flags |= USERFLAG_HAS_USERID;
44 user->language = get_default_language();
45 user->flags |= USERFLAG_LOADED_SETTINGS;
46 DESYNCHRONIZE(cache_sync);
49 int isGodMode(struct UserNode *user) {
50 loadUserSettings(user);
51 return (user->flags & USERFLAG_GOD_MODE);
54 int getChannelAccess(struct UserNode *user, struct ChanNode *chan) {
55 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
56 loadChannelSettings(chan);
57 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
62 if(user->flags & USERFLAG_HAS_USERID)
63 userid = user->user_id;
65 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
67 if ((row = mysql_fetch_row(res)) != NULL) {
68 userid = atoi(row[0]);
69 user->user_id = userid;
70 user->flags |= USERFLAG_HAS_USERID;
74 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
76 if ((row = mysql_fetch_row(res)) != NULL) {
77 int cflags = atoi(row[1]);
78 if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
79 caccess = atoi(row[0]);
84 char *getChanDefault(char *channel_setting) {
87 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
89 if ((row = mysql_fetch_row(res)) == NULL) return "";
93 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_501) {
94 loadChannelSettings(chan);
95 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
96 if((user->flags & USERFLAG_ISIRCOP)) return 1;
99 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
101 if ((row = mysql_fetch_row(res)) == NULL) return 0;
102 int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
103 if(require_access == 0) return 1;
104 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
107 if(user->flags & USERFLAG_HAS_USERID)
108 userid = user->user_id;
110 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
112 if ((row = mysql_fetch_row(res)) != NULL) {
113 userid = atoi(row[0]);
114 user->user_id = userid;
115 user->flags |= USERFLAG_HAS_USERID;
120 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
122 if ((row = mysql_fetch_row(res)) != NULL) {
123 int cflags = atoi(row[1]);
124 if(!(cflags & DB_CHANUSER_SUSPENDED))
125 caccess = atoi(row[0]);
128 if(caccess >= require_access) return 1;
129 if(caccess == 500 && require_access == 501 && allow_501) return 1;
133 void _loadChannelSettings(struct ChanNode *chan) {
134 SYNCHRONIZE(cache_sync);
137 printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
139 if ((row = mysql_fetch_row(res)) != NULL) {
140 chan->flags |= CHANFLAG_CHAN_REGISTERED;
141 chan->channel_id = atoi(row[0]);
143 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
144 DESYNCHRONIZE(cache_sync);
147 //TODO: fix performance: we should cache the user access
148 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
149 /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
150 if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
152 /* Don't protect if no one is to be protected. */
156 loadChannelSettings(chan);
157 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
158 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
160 if(!(row = mysql_fetch_row(res))) return 0;
162 protection = (char) atoi(row[0]);
164 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
166 row = mysql_fetch_row(res);
167 protection = (char) atoi(row[0]);
169 if(protection == 3) return 0;
171 /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
172 int victim_access = getChannelAccess(victim, chan);
173 if (!victim_access && protection != 0) return 0;
175 /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
176 int issuer_access = getChannelAccess(issuer, chan);
177 if (!issuer_access) return 1;
179 /* If the aggressor was a user, then the victim can't be helped. */
180 if(!victim_access) return 0;
185 if(victim_access >= issuer_access) return 1;
188 if(victim_access > issuer_access) return 1;
194 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
195 loadChannelSettings(chan);
196 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
199 printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
201 while ((row = mysql_fetch_row(res)) != NULL) {
202 if(!match(row[0], mask))
208 int renameAccount(char *oldauth, char *newauth) {
209 MYSQL_RES *res, *res2;
211 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
213 if ((row = mysql_fetch_row(res)) != NULL) {
214 int userid = atoi(row[0]);
215 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(newauth));
217 if((row = mysql_fetch_row(res)) != NULL) {
219 int newuid = atoi(row[0]);
220 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
222 while((row = mysql_fetch_row(res)) != NULL) {
223 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
225 if((row2 = mysql_fetch_row(res2)) != NULL) {
226 if(atoi(row[0]) > atoi(row2[0])) {
227 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%s' WHERE `chanuser_id` = '%s'", row[0], row2[0]);
229 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
231 printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
233 printf_mysql_query("UPDATE `channels` SET `channel_registrator` = '%d' WHERE `channel_registrator` = '%d'", userid, newuid);
234 printf_mysql_query("UPDATE `bans` SET `ban_owner` = '%d' WHERE `ban_owner` = '%d'", userid, newuid);
235 printf_mysql_query("UPDATE `events` SET `auth` = '%s' WHERE `auth` = '%s'", escape_string(newauth), escape_string(oldauth));
236 printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = '%d' WHERE `godlog_uid` = '%d'", userid, newuid);
237 printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = '%d' WHERE `owner_history_to_uid` = '%d'", userid, newuid);
238 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_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 `noinvite` SET `uid` = '%d' WHERE `uid` = '%d'", userid, newuid);
241 printf_mysql_query("DELETE FROM `users` WHERE `chanuser_id` = '%d'", newuid);
243 //simply rename the account
244 printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
251 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
253 struct event_user_registered_cache {
254 struct UserNode *new_user;
258 static int event_user_registered(struct UserNode *old_user, struct UserNode *new_user) {
259 //check if there is a fakehost on both sides...
260 if(!isFakeHost(old_user->host) || !isFakeHost(new_user->host)) return 0;
262 char oldauth[AUTHLEN], newauth[AUTHLEN];
264 if((p = strstr(old_user->host, "."))) {
266 strcpy(oldauth, old_user->host);
269 if((p = strstr(new_user->host, "."))) {
271 strcpy(newauth, new_user->host);
274 if(!stricmp(oldauth, newauth))
276 //check if we know this user; then check the new auth
279 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
281 if ((row = mysql_fetch_row(res)) != NULL) {
282 struct event_user_registered_cache *cache = malloc(sizeof(*cache));
284 perror("malloc() failed");
287 cache->new_user = new_user;
288 cache->oldauth = strdup(oldauth);
289 lookup_authname(newauth, event_user_registered_auth_lookup, cache);
294 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
295 struct event_user_registered_cache *cache = data;
297 renameAccount(cache->oldauth, auth);
298 strcpy(cache->new_user->auth, auth);
299 cache->new_user->flags |= USERFLAG_ISAUTHED;
301 free(cache->oldauth);
304 void init_DBHelper() {
305 bind_registered(event_user_registered);