#include "ChanUser.h"
#include "mysqlConn.h"
#include "lang.h"
-
+#include "tools.h"
void _loadUserSettings(struct UserNode *user) {
- check_mysql();
MYSQL_RES *res;
MYSQL_ROW row;
printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
MYSQL_RES *res;
MYSQL_ROW row;
- check_mysql();
int caccess = 0;
printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
res = mysql_use();
int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_override, int allow_501) {
loadChannelSettings(chan);
if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
+ if((user->flags & USERFLAG_ISIRCOP)) return 1;
MYSQL_RES *res;
MYSQL_ROW row;
printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_id` = '%d'", channel_setting, chan->channel_id);
res = mysql_use();
if ((row = mysql_fetch_row(res)) == NULL) return 0;
- int require_access = (row[0] ? atoi(row[0]) : getChanDefault(channel_setting));
+ int require_access = atoi((row[0] ? row[0] : getChanDefault(channel_setting)));
if(require_access == 0) return 1;
if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
int caccess = 0;
}
void _loadChannelSettings(struct ChanNode *chan) {
- check_mysql();
MYSQL_RES *res;
MYSQL_ROW row;
printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
}
+//TODO: fix performance: we should cache the user access
+int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserNode *issuer) {
+ /* Don't protect if someone is attacking himself, or if the aggressor is an IRC Operator. */
+ if(victim == issuer || (issuer->flags & USERFLAG_ISIRCOP)) return 0;
+
+ /* Don't protect if no one is to be protected. */
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ char protection;
+ loadChannelSettings(chan);
+ if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
+ printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+ res = mysql_use();
+ if(!(row = mysql_fetch_row(res))) return 0;
+ if(row[0]) {
+ protection = (char) atoi(row[0]);
+ } else {
+ printf_mysql_query("SELECT `channel_protect` FROM `channels` WHERE `channel_name` = 'defaults'");
+ res = mysql_use();
+ row = mysql_fetch_row(res);
+ protection = (char) atoi(row[0]);
+ }
+ if(protection == 3) return 0;
+
+ /* Don't protect if the victim isn't added to the channel, unless we are to protect non-users also. */
+ int victim_access = getChannelAccess(victim, chan, 0);
+ if (!victim_access && protection != 0) return 0;
+
+ /* Protect if the aggressor isn't a user because at this point, the aggressor can only be less than or equal to the victim. */
+ int issuer_access = getChannelAccess(issuer, chan, 0);
+ if (!issuer_access) return 1;
+
+ /* If the aggressor was a user, then the victim can't be helped. */
+ if(!victim_access) return 0;
+
+ switch(protection) {
+ case 0:
+ case 1:
+ if(victim_access >= issuer_access) return 1;
+ break;
+ case 2:
+ if(victim_access > issuer_access) return 1;
+ break;
+ }
+ return 0;
+}
+char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
+ loadChannelSettings(chan);
+ if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `ban_mask` FROM `bans` WHERE `ban_channel` = '%d'", chan->channel_id);
+ res = mysql_use();
+ while ((row = mysql_fetch_row(res)) != NULL) {
+ if(!match(row[0], mask))
+ return row[0];
+ }
+ return NULL;
+}