added user registered event, tidied up helper functions and added automatic auth...
[NeonServV5.git] / src / DBHelper.c
1 /* DBHelper.c - NeonServ v5.2
2  * Copyright (C) 2011  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     MYSQL_RES *res;
30     MYSQL_ROW row;
31     printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
32     res = mysql_use();
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;
40     } else
41         user->language = get_default_language();
42     user->flags |= USERFLAG_LOADED_SETTINGS;
43 }
44
45 int isGodMode(struct UserNode *user) {
46     loadUserSettings(user);
47     return (user->flags & USERFLAG_GOD_MODE);
48 }
49
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;
54     MYSQL_RES *res;
55     MYSQL_ROW row;
56     int caccess = 0;
57     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
58     res = mysql_use();
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);
61         res = mysql_use();
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]);
66         }
67         return caccess;
68     }
69     return 0;
70 }
71
72 char *getChanDefault(char *channel_setting) {
73     MYSQL_RES *res;
74     MYSQL_ROW row;
75     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
76     res = mysql_use();
77     if ((row = mysql_fetch_row(res)) == NULL) return "";
78     return row[0];
79 }
80
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;
85     MYSQL_RES *res;
86     MYSQL_ROW row;
87     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
88     res = mysql_use();
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;
93     int caccess = 0;
94     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
95     res = mysql_use();
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);
98         res = mysql_use();
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]);
103         }
104     }
105     if(caccess >= require_access) return 1;
106     if(caccess == 500 && require_access == 501 && allow_501) return 1;
107     return 0;
108 }
109
110 void _loadChannelSettings(struct ChanNode *chan) {
111     MYSQL_RES *res;
112     MYSQL_ROW row;
113     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
114     res = mysql_use();
115     if ((row = mysql_fetch_row(res)) != NULL) {
116         chan->flags |= CHANFLAG_CHAN_REGISTERED;
117         chan->channel_id = atoi(row[0]);
118     }
119     chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
120 }
121
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;
126     
127     /* Don't protect if no one is to be protected. */
128     MYSQL_RES *res;
129     MYSQL_ROW row;
130     char protection;
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);
134     res = mysql_use();
135     if(!(row = mysql_fetch_row(res))) return 0;
136     if(row[0]) {
137         protection = (char) atoi(row[0]);
138     } else {
139          printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
140         res = mysql_use();
141         row = mysql_fetch_row(res);
142         protection = (char) atoi(row[0]);
143     }
144     if(protection == 3) return 0;
145     
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;
149     
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;
153     
154     /* If the aggressor was a user, then the victim can't be helped. */
155     if(!victim_access) return 0;
156     
157     switch(protection) {
158         case 0:
159         case 1:
160             if(victim_access >= issuer_access) return 1;
161             break;
162         case 2:
163             if(victim_access > issuer_access) return 1;
164             break;
165     }
166     return 0;
167 }
168
169 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
170     loadChannelSettings(chan);
171     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
172     MYSQL_RES *res;
173     MYSQL_ROW row;
174     printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
175     res = mysql_use();
176     while ((row = mysql_fetch_row(res)) != NULL) {
177         if(!match(row[0], mask))
178             return row[0];
179     }
180     return NULL;
181 }
182
183 int renameAccount(char *oldauth, char *newauth) {
184     MYSQL_RES *res, *res2;
185     MYSQL_ROW row, row2;
186     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
187     res = mysql_use();
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));
191         res = mysql_use();
192         if((row = mysql_fetch_row(res)) != NULL) {
193             //merge
194             int newuid = atoi(row[0]);
195             printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
196             res = mysql_use();
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);
199                 res2 = mysql_use();
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]);
203                     }
204                     printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
205                 } else
206                     printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
207             }
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);
217         } else {
218             //simply rename the account
219             printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
220         }
221         return 1;
222     }
223     return 0;
224 }
225
226 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
227
228 struct event_user_registered_cache {
229     struct UserNode *new_user;
230     char *oldauth;
231 };
232
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;
236     //extract user names
237     char oldauth[AUTHLEN], newauth[AUTHLEN];
238     char *p;
239     if((p = strstr(old_user->host, "."))) {
240         *p = '\0';
241         strcpy(oldauth, old_user->host);
242         *p = '.';
243     }
244     if((p = strstr(new_user->host, "."))) {
245         *p = '\0';
246         strcpy(newauth, new_user->host);
247         *p = '.';
248     }
249     //check if we know this user; then check the new auth
250     MYSQL_RES *res;
251     MYSQL_ROW row;
252     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
253     res = mysql_use();
254     if ((row = mysql_fetch_row(res)) != NULL) {
255         struct event_user_registered_cache *cache = malloc(sizeof(*cache));
256         if (!cache) {
257             perror("malloc() failed");
258             return 1;
259         }
260         cache->new_user = new_user;
261         cache->oldauth = strdup(oldauth);
262         lookup_authname(newauth, event_user_registered_auth_lookup, cache);
263     }
264     return 1;
265 }
266
267 static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
268     struct event_user_registered_cache *cache = data;
269     if(exists) {
270         renameAccount(cache->oldauth, auth);
271         strcpy(cache->new_user->auth, auth);
272         cache->new_user->flags |= USERFLAG_ISAUTHED;
273     }
274     free(cache->oldauth);
275 }
276
277 void init_DBHelper() {
278     bind_registered(event_user_registered);
279 }
280