added experimental multi thread support
[NeonServV5.git] / src / DBHelper.c
1 /* DBHelper.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  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 #include "DBHelper.h"
19 #include "UserNode.h"
20 #include "ChanNode.h"
21 #include "ChanUser.h"
22 #include "mysqlConn.h"
23 #include "lang.h"
24 #include "tools.h"
25 #include "IRCEvents.h"
26 #include "HandleInfoHandler.h"
27
28 void _loadUserSettings(struct UserNode *user) {
29     SYNCHRONIZE(cache_sync);
30     MYSQL_RES *res;
31     MYSQL_ROW row;
32     printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god`, `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
33     res = mysql_use();
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;
43     } else
44         user->language = get_default_language();
45     user->flags |= USERFLAG_LOADED_SETTINGS;
46     DESYNCHRONIZE(cache_sync);
47 }
48
49 int isGodMode(struct UserNode *user) {
50     loadUserSettings(user);
51     return (user->flags & USERFLAG_GOD_MODE);
52 }
53
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;
58     MYSQL_RES *res;
59     MYSQL_ROW row;
60     int caccess = 0;
61     int userid;
62     if(user->flags & USERFLAG_HAS_USERID)
63         userid = user->user_id;
64     else {
65         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
66         res = mysql_use();
67         if ((row = mysql_fetch_row(res)) != NULL) {
68             userid = atoi(row[0]);
69             user->user_id = userid;
70             user->flags |= USERFLAG_HAS_USERID;
71         } else
72             return 0;
73     }
74     printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
75     res = mysql_use();
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]);
80     }
81     return caccess;
82 }
83
84 char *getChanDefault(char *channel_setting) {
85     MYSQL_RES *res;
86     MYSQL_ROW row;
87     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
88     res = mysql_use();
89     if ((row = mysql_fetch_row(res)) == NULL) return "";
90     return row[0];
91 }
92
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;
97     MYSQL_RES *res;
98     MYSQL_ROW row;
99     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
100     res = mysql_use();
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;
105     int caccess = 0;
106     int userid;
107     if(user->flags & USERFLAG_HAS_USERID)
108         userid = user->user_id;
109     else {
110         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
111         res = mysql_use();
112         if ((row = mysql_fetch_row(res)) != NULL) {
113             userid = atoi(row[0]);
114             user->user_id = userid;
115             user->flags |= USERFLAG_HAS_USERID;
116         } else
117             userid = -1;
118     }
119     if(userid > -1) {
120         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d' AND `chanuser_cid` = '%d'", userid, chan->channel_id);
121         res = mysql_use();
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]);
126         }
127     }
128     if(caccess >= require_access) return 1;
129     if(caccess == 500 && require_access == 501 && allow_501) return 1;
130     return 0;
131 }
132
133 void _loadChannelSettings(struct ChanNode *chan) {
134     SYNCHRONIZE(cache_sync);
135     MYSQL_RES *res;
136     MYSQL_ROW row;
137     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
138     res = mysql_use();
139     if ((row = mysql_fetch_row(res)) != NULL) {
140         chan->flags |= CHANFLAG_CHAN_REGISTERED;
141         chan->channel_id = atoi(row[0]);
142     }
143     chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
144     DESYNCHRONIZE(cache_sync);
145 }
146
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;
151     
152     /* Don't protect if no one is to be protected. */
153     MYSQL_RES *res;
154     MYSQL_ROW row;
155     char protection;
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);
159     res = mysql_use();
160     if(!(row = mysql_fetch_row(res))) return 0;
161     if(row[0]) {
162         protection = (char) atoi(row[0]);
163     } else {
164          printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
165         res = mysql_use();
166         row = mysql_fetch_row(res);
167         protection = (char) atoi(row[0]);
168     }
169     if(protection == 3) return 0;
170     
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;
174     
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;
178     
179     /* If the aggressor was a user, then the victim can't be helped. */
180     if(!victim_access) return 0;
181     
182     switch(protection) {
183         case 0:
184         case 1:
185             if(victim_access >= issuer_access) return 1;
186             break;
187         case 2:
188             if(victim_access > issuer_access) return 1;
189             break;
190     }
191     return 0;
192 }
193
194 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
195     loadChannelSettings(chan);
196     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
197     MYSQL_RES *res;
198     MYSQL_ROW row;
199     printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
200     res = mysql_use();
201     while ((row = mysql_fetch_row(res)) != NULL) {
202         if(!match(row[0], mask))
203             return row[0];
204     }
205     return NULL;
206 }
207
208 int renameAccount(char *oldauth, char *newauth) {
209     MYSQL_RES *res, *res2;
210     MYSQL_ROW row, row2;
211     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
212     res = mysql_use();
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));
216         res = mysql_use();
217         if((row = mysql_fetch_row(res)) != NULL) {
218             //merge
219             int newuid = atoi(row[0]);
220             printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
221             res = mysql_use();
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);
224                 res2 = mysql_use();
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]);
228                     }
229                     printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
230                 } else
231                     printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
232             }
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);
242         } else {
243             //simply rename the account
244             printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
245         }
246         return 1;
247     }
248     return 0;
249 }
250
251 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
252
253 struct event_user_registered_cache {
254     struct UserNode *new_user;
255     char *oldauth;
256 };
257
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;
261     //extract user names
262     char oldauth[AUTHLEN], newauth[AUTHLEN];
263     char *p;
264     if((p = strstr(old_user->host, "."))) {
265         *p = '\0';
266         strcpy(oldauth, old_user->host);
267         *p = '.';
268     }
269     if((p = strstr(new_user->host, "."))) {
270         *p = '\0';
271         strcpy(newauth, new_user->host);
272         *p = '.';
273     }
274     if(!stricmp(oldauth, newauth))
275         return 0;
276     //check if we know this user; then check the new auth
277     MYSQL_RES *res;
278     MYSQL_ROW row;
279     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
280     res = mysql_use();
281     if ((row = mysql_fetch_row(res)) != NULL) {
282         struct event_user_registered_cache *cache = malloc(sizeof(*cache));
283         if (!cache) {
284             perror("malloc() failed");
285             return 1;
286         }
287         cache->new_user = new_user;
288         cache->oldauth = strdup(oldauth);
289         lookup_authname(newauth, event_user_registered_auth_lookup, cache);
290     }
291     return 1;
292 }
293
294 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
295     struct event_user_registered_cache *cache = data;
296     if(exists) {
297         renameAccount(cache->oldauth, auth);
298         strcpy(cache->new_user->auth, auth);
299         cache->new_user->flags |= USERFLAG_ISAUTHED;
300     }
301     free(cache->oldauth);
302 }
303
304 void init_DBHelper() {
305     bind_registered(event_user_registered);
306 }
307