changed Makefile; build all commands as an own file
[NeonServV5.git] / DBHelper.c
index eb8953691d5225904fbe898a3bb73ed087fe12ff..181fff607b542c6262721a4fe973414594ec5d8c 100644 (file)
@@ -5,10 +5,9 @@
 #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));
@@ -36,7 +35,6 @@ int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override)
     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();
@@ -47,7 +45,8 @@ int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override)
         //
         res = mysql_use();
         if ((row = mysql_fetch_row(res)) != NULL) {
-            if(!(atoi(row[1]) & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
+            int cflags = atoi(row[1]);
+            if(!(cflags & DB_CHANUSER_SUSPENDED) && atoi(row[0]) > caccess)
                 caccess = atoi(row[0]);
         }
         return caccess;
@@ -55,15 +54,25 @@ int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override)
     return 0;
 }
 
+char *getChanDefault(char *channel_setting) {
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    printf_mysql_query("SELECT `%s` FROM `channels` WHERE `channel_name` = 'defaults'", channel_setting);
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) == NULL) return "";
+    return row[0];
+}
+
 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 = atoi(row[0]);
+    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;
@@ -73,7 +82,8 @@ int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *chann
         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%s' AND `chanuser_cid` = '%d'", row[0], chan->channel_id);
         res = mysql_use();
         if ((row = mysql_fetch_row(res)) != NULL) {
-            if(!(atoi(row[1]) & DB_CHANUSER_SUSPENDED))
+            int cflags = atoi(row[1]);
+            if(!(cflags & DB_CHANUSER_SUSPENDED))
                 caccess = atoi(row[0]);
         }
     }
@@ -83,7 +93,6 @@ int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *chann
 }
 
 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));
@@ -95,4 +104,63 @@ void _loadChannelSettings(struct ChanNode *chan) {
     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;
+}