10 void _loadUserSettings(struct UserNode *user) {
13 printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
15 if ((row = mysql_fetch_row(res)) != NULL) {
16 user->language = get_language_by_tag(row[0]);
17 if(user->language == NULL) user->language = get_default_language();
18 if(strcmp(row[1], "0"))
19 user->flags |= USERFLAG_REPLY_PRIVMSG;
20 if(strcmp(row[2], "0"))
21 user->flags |= USERFLAG_GOD_MODE;
23 user->language = get_default_language();
24 user->flags |= USERFLAG_LOADED_SETTINGS;
27 int isGodMode(struct UserNode *user) {
28 loadUserSettings(user);
29 return (user->flags & USERFLAG_GOD_MODE);
32 int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override) {
33 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
34 loadChannelSettings(chan);
35 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
39 printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
41 if ((row = mysql_fetch_row(res)) != NULL) {
42 if(strcmp(row[2], "0") && override)
43 caccess = atoi(row[1]);
44 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
47 if ((row = mysql_fetch_row(res)) != NULL) {
48 int cflags = atoi(row[1]);
49 if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
50 caccess = atoi(row[0]);
57 char *getChanDefault(char *channel_setting) {
60 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
62 if ((row = mysql_fetch_row(res)) == NULL) return "";
66 int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_override, int allow_501) {
67 loadChannelSettings(chan);
68 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
69 if((user->flags & USERFLAG_ISIRCOP)) return 1;
72 printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
74 if ((row = mysql_fetch_row(res)) == NULL) return 0;
75 int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
76 if(require_access == 0) return 1;
77 if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
79 printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
81 if ((row = mysql_fetch_row(res)) != NULL) {
82 printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
84 if ((row = mysql_fetch_row(res)) != NULL) {
85 int cflags = atoi(row[1]);
86 if(!(cflags & DB_CHANUSER_SUSPENDED))
87 caccess = atoi(row[0]);
90 if(caccess >= require_access) return 1;
91 if(caccess == 500 && require_access == 501 && allow_501) return 1;
95 void _loadChannelSettings(struct ChanNode *chan) {
98 printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
100 if ((row = mysql_fetch_row(res)) != NULL) {
101 chan->flags |= CHANFLAG_CHAN_REGISTERED;
102 chan->channel_id = atoi(row[0]);
104 chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
107 //TODO: fix performance: we should cache the user access
108 int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
109 /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
110 if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
112 /* Don't protect if no one is to be protected. */
116 loadChannelSettings(chan);
117 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
118 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
120 if(!(row = mysql_fetch_row(res))) return 0;
122 protection = (char) atoi(row[0]);
124 printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
126 row = mysql_fetch_row(res);
127 protection = (char) atoi(row[0]);
129 if(protection == 3) return 0;
131 /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
132 int victim_access = getChannelAccess(victim, chan, 0);
133 if (!victim_access && protection != 0) return 0;
135 /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
136 int issuer_access = getChannelAccess(issuer, chan, 0);
137 if (!issuer_access) return 1;
139 /* If the aggressor was a user, then the victim can't be helped. */
140 if(!victim_access) return 0;
145 if(victim_access >= issuer_access) return 1;
148 if(victim_access > issuer_access) return 1;
154 char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
155 loadChannelSettings(chan);
156 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
159 printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
161 while ((row = mysql_fetch_row(res)) != NULL) {
162 if(!match(row[0], mask))