added some code & compiler information to cmd_netinfo
[NeonServV5.git] / DBHelper.c
1
2 #include "DBHelper.h"
3 #include "UserNode.h"
4 #include "ChanNode.h"
5 #include "ChanUser.h"
6 #include "mysqlConn.h"
7 #include "lang.h"
8 #include "tools.h"
9
10 void _loadUserSettings(struct UserNode *user) {
11     check_mysql();
12     MYSQL_RES *res;
13     MYSQL_ROW row;
14     printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
15     res = mysql_use();
16     if ((row = mysql_fetch_row(res)) != NULL) {
17         user->language = get_language_by_tag(row[0]);
18         if(user->language == NULL) user->language = get_default_language();
19         if(strcmp(row[1], "0"))
20             user->flags |= USERFLAG_REPLY_PRIVMSG;
21         if(strcmp(row[2], "0"))
22             user->flags |= USERFLAG_GOD_MODE;
23     } else
24         user->language = get_default_language();
25     user->flags |= USERFLAG_LOADED_SETTINGS;
26 }
27
28 int isGodMode(struct UserNode *user) {
29     loadUserSettings(user);
30     return (user->flags & USERFLAG_GOD_MODE);
31 }
32
33 int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override) {
34     if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
35     loadChannelSettings(chan);
36     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
37     MYSQL_RES *res;
38     MYSQL_ROW row;
39     check_mysql();
40     int caccess = 0;
41     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
42     res = mysql_use();
43     if ((row = mysql_fetch_row(res)) != NULL) {
44         if(strcmp(row[2], "0") && override) 
45             caccess = atoi(row[1]);
46         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
47         //
48         res = mysql_use();
49         if ((row = mysql_fetch_row(res)) != NULL) {
50             int cflags = atoi(row[1]);
51             if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
52                 caccess = atoi(row[0]);
53         }
54         return caccess;
55     }
56     return 0;
57 }
58
59 char *getChanDefault(char *channel_setting) {
60     MYSQL_RES *res;
61     MYSQL_ROW row;
62     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
63     res = mysql_use();
64     if ((row = mysql_fetch_row(res)) == NULL) return "";
65     return row[0];
66 }
67
68 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_override, int allow_501) {
69     loadChannelSettings(chan);
70     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
71     if((user->flags & USERFLAG_ISIRCOP)) return 1;
72     MYSQL_RES *res;
73     MYSQL_ROW row;
74     printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
75     res = mysql_use();
76     if ((row = mysql_fetch_row(res)) == NULL) return 0;
77     int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
78     if(require_access == 0) return 1;
79     if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
80     int caccess = 0;
81     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
82     res = mysql_use();
83     if ((row = mysql_fetch_row(res)) != NULL) {
84         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
85         res = mysql_use();
86         if ((row = mysql_fetch_row(res)) != NULL) {
87             int cflags = atoi(row[1]);
88             if(!(cflags & DB_CHANUSER_SUSPENDED))
89                 caccess = atoi(row[0]);
90         }
91     }
92     if(caccess >= require_access) return 1;
93     if(caccess == 500 && require_access == 501 && allow_501) return 1;
94     return 0;
95 }
96
97 void _loadChannelSettings(struct ChanNode *chan) {
98     check_mysql();
99     MYSQL_RES *res;
100     MYSQL_ROW row;
101     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
102     res = mysql_use();
103     if ((row = mysql_fetch_row(res)) != NULL) {
104         chan->flags |= CHANFLAG_CHAN_REGISTERED;
105         chan->channel_id = atoi(row[0]);
106     }
107     chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
108 }
109
110 //TODO: fix performance: we should cache the user access
111 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
112     /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
113     if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
114     
115     /* Don't protect if no one is to be protected. */
116     MYSQL_RES *res;
117     MYSQL_ROW row;
118     char protection;
119     loadChannelSettings(chan);
120     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
121     printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
122     res = mysql_use();
123     if(!(row = mysql_fetch_row(res))) return 0;
124     if(row[0]) {
125         protection = (char) atoi(row[0]);
126     } else {
127          printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
128         res = mysql_use();
129         row = mysql_fetch_row(res);
130         protection = (char) atoi(row[0]);
131     }
132     if(protection == 3) return 0;
133     
134     /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
135     int victim_access = getChannelAccess(victim, chan, 0);
136     if (!victim_access && protection != 0) return 0;
137     
138     /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
139     int issuer_access = getChannelAccess(issuer, chan, 0);
140     if (!issuer_access) return 1;
141     
142     /* If the aggressor was a user, then the victim can't be helped. */
143     if(!victim_access) return 0;
144     
145     switch(protection) {
146         case 0:
147         case 1:
148             if(victim_access >= issuer_access) return 1;
149             break;
150         case 2:
151             if(victim_access > issuer_access) return 1;
152             break;
153     }
154     return 0;
155 }
156
157 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
158     loadChannelSettings(chan);
159     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
160     MYSQL_RES *res;
161     MYSQL_ROW row;
162     printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
163     res = mysql_use();
164     while ((row = mysql_fetch_row(res)) != NULL) {
165         if(!match(row[0], mask))
166             return row[0];
167     }
168     return NULL;
169 }