added user registered event, tidied up helper functions and added automatic auth...
[NeonServV5.git] / src / DBHelper.c
index 643eb2f0082b643e1105840e0fe8e48d1e1aa609..a55b8c677de27c7ec0f7204df4c9c624b2703172 100644 (file)
@@ -1,4 +1,4 @@
-/* DBHelper.c - NeonServ v5.0
+/* DBHelper.c - NeonServ v5.2
  * Copyright (C) 2011  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -22,6 +22,8 @@
 #include "mysqlConn.h"
 #include "lang.h"
 #include "tools.h"
+#include "IRCEvents.h"
+#include "HandleInfoHandler.h"
 
 void _loadUserSettings(struct UserNode *user) {
     MYSQL_RES *res;
@@ -45,7 +47,7 @@ int isGodMode(struct UserNode *user) {
     return (user->flags & USERFLAG_GOD_MODE);
 }
 
-int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override) {
+int getChannelAccess(struct UserNode *user, struct ChanNode *chan) {
     if(!(user->flags & USERFLAG_ISAUTHED)) return 0;
     loadChannelSettings(chan);
     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
@@ -55,10 +57,7 @@ int getChannelAccess(struct UserNode *user, struct ChanNode *chan, int override)
     printf_mysql_query("SELECT `user_id`, `user_access`, `user_god` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
     res = mysql_use();
     if ((row = mysql_fetch_row(res)) != NULL) {
-        if(strcmp(row[2], "0") && override) 
-            caccess = atoi(row[1]);
         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) {
             int cflags = atoi(row[1]);
@@ -79,7 +78,7 @@ char *getChanDefault(char *channel_setting) {
     return row[0];
 }
 
-int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_override, int allow_501) {
+int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *channel_setting, int allow_501) {
     loadChannelSettings(chan);
     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return 0;
     if((user->flags & USERFLAG_ISIRCOP)) return 1;
@@ -145,11 +144,11 @@ int isUserProtected(struct ChanNode *chan, struct UserNode *victim, struct UserN
     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);
+    int victim_access = getChannelAccess(victim, chan);
     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);
+    int issuer_access = getChannelAccess(issuer, chan);
     if (!issuer_access) return 1;
     
     /* If the aggressor was a user, then the victim can't be helped. */
@@ -180,3 +179,102 @@ char *getBanAffectingMask(struct ChanNode *chan, char *mask) {
     }
     return NULL;
 }
+
+int renameAccount(char *oldauth, char *newauth) {
+    MYSQL_RES *res, *res2;
+    MYSQL_ROW row, row2;
+    printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) != NULL) {
+        int userid = atoi(row[0]);
+        printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(newauth));
+        res = mysql_use();
+        if((row = mysql_fetch_row(res)) != NULL) {
+            //merge
+            int newuid = atoi(row[0]);
+            printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", newuid);
+            res = mysql_use();
+            while((row = mysql_fetch_row(res)) != NULL) {
+                printf_mysql_query("SELECT `chanuser_id`, `chanuser_access`, `chanuser_flags` FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
+                res2 = mysql_use();
+                if((row2 = mysql_fetch_row(res2)) != NULL) {
+                    if(atoi(row[0]) > atoi(row2[0])) {
+                        printf_mysql_query("UPDATE `chanusers` SET `chanuser_access` = '%s' WHERE `chanuser_id` = '%s'", row[0], row2[0]);
+                    }
+                    printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_id` = '%s'", row[0]);
+                } else
+                    printf_mysql_query("UPDATE `chanusers` SET `chanuser_uid` = '%d' WHERE `chanuser_id` = '%s'", userid, row[0]);
+            }
+            printf_mysql_query("UPDATE `channels` SET `channel_registrator` = '%d' WHERE `channel_registrator` = '%d'", userid, newuid);
+            printf_mysql_query("UPDATE `bans` SET `ban_owner` = '%d' WHERE `ban_owner` = '%d'", userid, newuid);
+            printf_mysql_query("UPDATE `events` SET `auth` = '%s' WHERE `auth` = '%s'", escape_string(newauth), escape_string(oldauth));
+            printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = '%d' WHERE `godlog_uid` = '%d'", userid, newuid);
+            printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = '%d' WHERE `owner_history_to_uid` = '%d'", userid, newuid);
+            printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
+            printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = '%d' WHERE `owner_history_from_uid` = '%d'", userid, newuid);
+            printf_mysql_query("UPDATE `noinvite` SET `uid` = '%d' WHERE `uid` = '%d'", userid, newuid);
+            printf_mysql_query("DELETE FROM `users` WHERE `chanuser_id` = '%d'", newuid);
+        } else {
+            //simply rename the account
+            printf_mysql_query("UPDATE `users` SET `user_user` = '%s' WHERE `user_id` = '%d'", escape_string(newauth), userid);
+        }
+        return 1;
+    }
+    return 0;
+}
+
+static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup);
+
+struct event_user_registered_cache {
+    struct UserNode *new_user;
+    char *oldauth;
+};
+
+static int event_user_registered(struct UserNode *old_user, struct UserNode *new_user) {
+    //check if there is a fakehost on both sides...
+    if(!isFakeHost(old_user->host) || !isFakeHost(new_user->host)) return 0;
+    //extract user names
+    char oldauth[AUTHLEN], newauth[AUTHLEN];
+    char *p;
+    if((p = strstr(old_user->host, "."))) {
+        *p = '\0';
+        strcpy(oldauth, old_user->host);
+        *p = '.';
+    }
+    if((p = strstr(new_user->host, "."))) {
+        *p = '\0';
+        strcpy(newauth, new_user->host);
+        *p = '.';
+    }
+    //check if we know this user; then check the new auth
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(oldauth));
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) != NULL) {
+        struct event_user_registered_cache *cache = malloc(sizeof(*cache));
+        if (!cache) {
+            perror("malloc() failed");
+            return 1;
+        }
+        cache->new_user = new_user;
+        cache->oldauth = strdup(oldauth);
+        lookup_authname(newauth, event_user_registered_auth_lookup, cache);
+    }
+    return 1;
+}
+
+static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
+    struct event_user_registered_cache *cache = data;
+    if(exists) {
+        renameAccount(cache->oldauth, auth);
+        strcpy(cache->new_user->auth, auth);
+        cache->new_user->flags |= USERFLAG_ISAUTHED;
+    }
+    free(cache->oldauth);
+}
+
+void init_DBHelper() {
+    bind_registered(event_user_registered);
+}
+