10 void _loadUserSettings(struct UserNode *user) {
14 printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
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;
24 user->language = get_default_language();
25 user->flags |= USERFLAG_LOADED_SETTINGS;
28 int isGodMode(struct UserNode *user) {
29 loadUserSettings(user);
30 return (user->flags & USERFLAG_GOD_MODE);
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;
41 printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
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);
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]);
59 char *getChanDefault(char *channel_setting) {
62 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
64 if ((row = mysql_fetch_row(res)) == NULL) return "";
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;
74 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
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;
81 printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
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);
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]);
92 if(caccess >= require_access) return 1;
93 if(caccess == 500 && require_access == 501 && allow_501) return 1;
97 void _loadChannelSettings(struct ChanNode *chan) {
101 printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
103 if ((row = mysql_fetch_row(res)) != NULL) {
104 chan->flags |= CHANFLAG_CHAN_REGISTERED;
105 chan->channel_id = atoi(row[0]);
107 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
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;
115 /* Don't protect if no one is to be protected. */
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);
123 if(!(row = mysql_fetch_row(res))) return 0;
125 protection = (char) atoi(row[0]);
127 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
129 row = mysql_fetch_row(res);
130 protection = (char) atoi(row[0]);
132 if(protection == 3) return 0;
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;
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;
142 /* If the aggressor was a user, then the victim can't be helped. */
143 if(!victim_access) return 0;
148 if(victim_access >= issuer_access) return 1;
151 if(victim_access > issuer_access) return 1;