1 /* DBHelper.c - NeonServ v5.2
2 * Copyright (C) 2011 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) {
31 printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
33 if ((row = mysql_fetch_row(res)) != NULL) {
34 user->language = get_language_by_tag(row[0]);
35 if(user->language == NULL) user->language = get_default_language();
36 if(strcmp(row[1], "0"))
37 user->flags |= USERFLAG_REPLY_PRIVMSG;
38 if(strcmp(row[2], "0"))
39 user->flags |= USERFLAG_GOD_MODE;
41 user->language = get_default_language();
42 user->flags |= USERFLAG_LOADED_SETTINGS;
45 int isGodMode(struct UserNode *user) {
46 loadUserSettings(user);
47 return (user->flags & USERFLAG_GOD_MODE);
50 int getChannelAccess(struct UserNode *user, struct ChanNode *chan) {
51 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
52 loadChannelSettings(chan);
53 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
57 printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
59 if ((row = mysql_fetch_row(res)) != NULL) {
60 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
62 if ((row = mysql_fetch_row(res)) != NULL) {
63 int cflags = atoi(row[1]);
64 if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
65 caccess = atoi(row[0]);
72 char *getChanDefault(char *channel_setting) {
75 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
77 if ((row = mysql_fetch_row(res)) == NULL) return "";
81 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_501) {
82 loadChannelSettings(chan);
83 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
84 if((user->flags & USERFLAG_ISIRCOP)) return 1;
87 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
89 if ((row = mysql_fetch_row(res)) == NULL) return 0;
90 int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
91 if(require_access == 0) return 1;
92 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
94 printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
96 if ((row = mysql_fetch_row(res)) != NULL) {
97 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
99 if ((row = mysql_fetch_row(res)) != NULL) {
100 int cflags = atoi(row[1]);
101 if(!(cflags & DB_CHANUSER_SUSPENDED))
102 caccess = atoi(row[0]);
105 if(caccess >= require_access) return 1;
106 if(caccess == 500 && require_access == 501 && allow_501) return 1;
110 void _loadChannelSettings(struct ChanNode *chan) {
113 printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
115 if ((row = mysql_fetch_row(res)) != NULL) {
116 chan->flags |= CHANFLAG_CHAN_REGISTERED;
117 chan->channel_id = atoi(row[0]);
119 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
122 //TODO: fix performance: we should cache the user access
123 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
124 /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
125 if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
127 /* Don't protect if no one is to be protected. */
131 loadChannelSettings(chan);
132 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
133 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
135 if(!(row = mysql_fetch_row(res))) return 0;
137 protection = (char) atoi(row[0]);
139 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
141 row = mysql_fetch_row(res);
142 protection = (char) atoi(row[0]);
144 if(protection == 3) return 0;
146 /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
147 int victim_access = getChannelAccess(victim, chan);
148 if (!victim_access && protection != 0) return 0;
150 /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
151 int issuer_access = getChannelAccess(issuer, chan);
152 if (!issuer_access) return 1;
154 /* If the aggressor was a user, then the victim can't be helped. */
155 if(!victim_access) return 0;
160 if(victim_access >= issuer_access) return 1;
163 if(victim_access > issuer_access) return 1;
169 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
170 loadChannelSettings(chan);
171 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
174 printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
176 while ((row = mysql_fetch_row(res)) != NULL) {
177 if(!match(row[0], mask))
183 int renameAccount(char *oldauth, char *newauth) {
184 MYSQL_RES *res, *res2;
186 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
188 if ((row = mysql_fetch_row(res)) != NULL) {
189 int userid = atoi(row[0]);
190 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(newauth));
192 if((row = mysql_fetch_row(res)) != NULL) {
194 int newuid = atoi(row[0]);
195 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
197 while((row = mysql_fetch_row(res)) != NULL) {
198 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
200 if((row2 = mysql_fetch_row(res2)) != NULL) {
201 if(atoi(row[0]) > atoi(row2[0])) {
202 printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%s' WHERE `chanuser_id` = '%s'", row[0], row2[0]);
204 printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
206 printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
208 printf_mysql_query("UPDATE `channels` SET `channel_registrator` = '%d' WHERE `channel_registrator` = '%d'", userid, newuid);
209 printf_mysql_query("UPDATE `bans` SET `ban_owner` = '%d' WHERE `ban_owner` = '%d'", userid, newuid);
210 printf_mysql_query("UPDATE `events` SET `auth` = '%s' WHERE `auth` = '%s'", escape_string(newauth), escape_string(oldauth));
211 printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = '%d' WHERE `godlog_uid` = '%d'", userid, newuid);
212 printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = '%d' WHERE `owner_history_to_uid` = '%d'", userid, newuid);
213 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
214 printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
215 printf_mysql_query("UPDATE `noinvite` SET `uid` = '%d' WHERE `uid` = '%d'", userid, newuid);
216 printf_mysql_query("DELETE FROM `users` WHERE `chanuser_id` = '%d'", newuid);
218 //simply rename the account
219 printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
226 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
228 struct event_user_registered_cache {
229 struct UserNode *new_user;
233 static int event_user_registered(struct UserNode *old_user, struct UserNode *new_user) {
234 //check if there is a fakehost on both sides...
235 if(!isFakeHost(old_user->host) || !isFakeHost(new_user->host)) return 0;
237 char oldauth[AUTHLEN], newauth[AUTHLEN];
239 if((p = strstr(old_user->host, "."))) {
241 strcpy(oldauth, old_user->host);
244 if((p = strstr(new_user->host, "."))) {
246 strcpy(newauth, new_user->host);
249 //check if we know this user; then check the new auth
252 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
254 if ((row = mysql_fetch_row(res)) != NULL) {
255 struct event_user_registered_cache *cache = malloc(sizeof(*cache));
257 perror("malloc() failed");
260 cache->new_user = new_user;
261 cache->oldauth = strdup(oldauth);
262 lookup_authname(newauth, event_user_registered_auth_lookup, cache);
267 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
268 struct event_user_registered_cache *cache = data;
270 renameAccount(cache->oldauth, auth);
271 strcpy(cache->new_user->auth, auth);
272 cache->new_user->flags |= USERFLAG_ISAUTHED;
274 free(cache->oldauth);
277 void init_DBHelper() {
278 bind_registered(event_user_registered);