44e91ea577ae001e2eeef58705e11b454aa69b93
[NeonServV5.git] / src / DBHelper.c
1 /* DBHelper.c - NeonServ v5.1
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
26 void _loadUserSettings(struct UserNode *user) {
27     MYSQL_RES *res;
28     MYSQL_ROW row;
29     printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
30     res = mysql_use();
31     if ((row = mysql_fetch_row(res)) != NULL) {
32         user->language = get_language_by_tag(row[0]);
33         if(user->language == NULL) user->language = get_default_language();
34         if(strcmp(row[1], "0"))
35             user->flags |= USERFLAG_REPLY_PRIVMSG;
36         if(strcmp(row[2], "0"))
37             user->flags |= USERFLAG_GOD_MODE;
38     } else
39         user->language = get_default_language();
40     user->flags |= USERFLAG_LOADED_SETTINGS;
41 }
42
43 int isGodMode(struct UserNode *user) {
44     loadUserSettings(user);
45     return (user->flags & USERFLAG_GOD_MODE);
46 }
47
48 int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override) {
49     if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
50     loadChannelSettings(chan);
51     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
52     MYSQL_RES *res;
53     MYSQL_ROW row;
54     int caccess = 0;
55     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
56     res = mysql_use();
57     if ((row = mysql_fetch_row(res)) != NULL) {
58         if(strcmp(row[2], "0") && override) 
59             caccess = atoi(row[1]);
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         //
62         res = mysql_use();
63         if ((row = mysql_fetch_row(res)) != NULL) {
64             int cflags = atoi(row[1]);
65             if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
66                 caccess = atoi(row[0]);
67         }
68         return caccess;
69     }
70     return 0;
71 }
72
73 char *getChanDefault(char *channel_setting) {
74     MYSQL_RES *res;
75     MYSQL_ROW row;
76     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
77     res = mysql_use();
78     if ((row = mysql_fetch_row(res)) == NULL) return "";
79     return row[0];
80 }
81
82 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_override, int allow_501) {
83     loadChannelSettings(chan);
84     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
85     if((user->flags & USERFLAG_ISIRCOP)) return 1;
86     MYSQL_RES *res;
87     MYSQL_ROW row;
88     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
89     res = mysql_use();
90     if ((row = mysql_fetch_row(res)) == NULL) return 0;
91     int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
92     if(require_access == 0) return 1;
93     if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
94     int caccess = 0;
95     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
96     res = mysql_use();
97     if ((row = mysql_fetch_row(res)) != NULL) {
98         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
99         res = mysql_use();
100         if ((row = mysql_fetch_row(res)) != NULL) {
101             int cflags = atoi(row[1]);
102             if(!(cflags & DB_CHANUSER_SUSPENDED))
103                 caccess = atoi(row[0]);
104         }
105     }
106     if(caccess >= require_access) return 1;
107     if(caccess == 500 && require_access == 501 && allow_501) return 1;
108     return 0;
109 }
110
111 void _loadChannelSettings(struct ChanNode *chan) {
112     MYSQL_RES *res;
113     MYSQL_ROW row;
114     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
115     res = mysql_use();
116     if ((row = mysql_fetch_row(res)) != NULL) {
117         chan->flags |= CHANFLAG_CHAN_REGISTERED;
118         chan->channel_id = atoi(row[0]);
119     }
120     chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
121 }
122
123 //TODO: fix performance: we should cache the user access
124 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
125     /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
126     if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
127     
128     /* Don't protect if no one is to be protected. */
129     MYSQL_RES *res;
130     MYSQL_ROW row;
131     char protection;
132     loadChannelSettings(chan);
133     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
134     printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
135     res = mysql_use();
136     if(!(row = mysql_fetch_row(res))) return 0;
137     if(row[0]) {
138         protection = (char) atoi(row[0]);
139     } else {
140          printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
141         res = mysql_use();
142         row = mysql_fetch_row(res);
143         protection = (char) atoi(row[0]);
144     }
145     if(protection == 3) return 0;
146     
147     /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
148     int victim_access = getChannelAccess(victim, chan, 0);
149     if (!victim_access && protection != 0) return 0;
150     
151     /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
152     int issuer_access = getChannelAccess(issuer, chan, 0);
153     if (!issuer_access) return 1;
154     
155     /* If the aggressor was a user, then the victim can't be helped. */
156     if(!victim_access) return 0;
157     
158     switch(protection) {
159         case 0:
160         case 1:
161             if(victim_access >= issuer_access) return 1;
162             break;
163         case 2:
164             if(victim_access > issuer_access) return 1;
165             break;
166     }
167     return 0;
168 }
169
170 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
171     loadChannelSettings(chan);
172     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
173     MYSQL_RES *res;
174     MYSQL_ROW row;
175     printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
176     res = mysql_use();
177     while ((row = mysql_fetch_row(res)) != NULL) {
178         if(!match(row[0], mask))
179             return row[0];
180     }
181     return NULL;
182 }
183
184 int renameAccount(char *oldauth, char *newauth) {
185     MYSQL_RES *res, *res2;
186     MYSQL_ROW row, row2;
187     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
188     res = mysql_use();
189     if ((row = mysql_fetch_row(res)) != NULL) {
190         int userid = atoi(row[0]);
191         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(newauth));
192         res = mysql_use();
193         if((row = mysql_fetch_row(res)) != NULL) {
194             //merge
195             int newuid = atoi(row[0]);
196             printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
197             res = mysql_use();
198             while((row = mysql_fetch_row(res)) != NULL) {
199                 printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
200                 res2 = mysql_use();
201                 if((row2 = mysql_fetch_row(res2)) != NULL) {
202                     if(atoi(row[0]) > atoi(row2[0])) {
203                         printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%s' WHERE `chanuser_id` = '%s'", row[0], row2[0]);
204                     }
205                     printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
206                 } else
207                     printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
208             }
209             printf_mysql_query("UPDATE `channels` SET `channel_registrator` = '%d' WHERE `channel_registrator` = '%d'", userid, newuid);
210             printf_mysql_query("UPDATE `bans` SET `ban_owner` = '%d' WHERE `ban_owner` = '%d'", userid, newuid);
211             printf_mysql_query("UPDATE `events` SET `auth` = '%s' WHERE `auth` = '%s'", escape_string(newauth), escape_string(oldauth));
212             printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = '%d' WHERE `godlog_uid` = '%d'", userid, newuid);
213             printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = '%d' WHERE `owner_history_to_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 `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
216             printf_mysql_query("UPDATE `noinvite` SET `uid` = '%d' WHERE `uid` = '%d'", userid, newuid);
217             printf_mysql_query("DELETE FROM `users` WHERE `chanuser_id` = '%d'", newuid);
218         } else {
219             //simply rename the account
220             printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
221         }
222         return 1;
223     }
224     return 0;
225 }