*** VERSION 5.4.0 ***
[NeonServV5.git] / src / DBHelper.c
index 43b984bae8b557b6d9941c6676e65ccb831254df..cfcdd20eab128d79fde135e7f3ec133e1ba182cf 100644 (file)
@@ -1,5 +1,5 @@
-/* DBHelper.c - NeonServ v5.2
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* DBHelper.c - NeonServ v5.4
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "tools.h"
 #include "IRCEvents.h"
 #include "HandleInfoHandler.h"
+#include "ClientSocket.h"
 
 void _loadUserSettings(struct UserNode *user) {
+    SYNCHRONIZE(cache_sync);
     MYSQL_RES *res;
     MYSQL_ROW row;
     printf_mysql_query("SELECT `user_lang`, `user_reply_privmsg`, `user_god`, `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
@@ -42,6 +44,7 @@ void _loadUserSettings(struct UserNode *user) {
     } else
         user->language = get_default_language();
     user->flags |= USERFLAG_LOADED_SETTINGS;
+    DESYNCHRONIZE(cache_sync);
 }
 
 int isGodMode(struct UserNode *user) {
@@ -129,6 +132,7 @@ int checkChannelAccess(struct UserNode *user, struct ChanNode *chan, char *chann
 }
 
 void _loadChannelSettings(struct ChanNode *chan) {
+    SYNCHRONIZE(cache_sync);
     MYSQL_RES *res;
     MYSQL_ROW row;
     printf_mysql_query("SELECT `channel_id` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
@@ -138,6 +142,7 @@ void _loadChannelSettings(struct ChanNode *chan) {
         chan->channel_id = atoi(row[0]);
     }
     chan->flags |= CHANFLAG_REQUESTED_CHANINFO;
+    DESYNCHRONIZE(cache_sync);
 }
 
 //TODO: fix performance: we should cache the user access
@@ -267,6 +272,8 @@ static int event_user_registered(struct UserNode *old_user, struct UserNode *new
         strcpy(newauth, new_user->host);
         *p = '.';
     }
+    if(!stricmp(oldauth, newauth))
+        return 0;
     //check if we know this user; then check the new auth
     MYSQL_RES *res;
     MYSQL_ROW row;
@@ -280,7 +287,7 @@ static int event_user_registered(struct UserNode *old_user, struct UserNode *new
         }
         cache->new_user = new_user;
         cache->oldauth = strdup(oldauth);
-        lookup_authname(newauth, event_user_registered_auth_lookup, cache);
+        lookup_authname(newauth, 0, event_user_registered_auth_lookup, cache);
     }
     return 1;
 }
@@ -293,9 +300,49 @@ static AUTHLOOKUP_CALLBACK(event_user_registered_auth_lookup) {
         cache->new_user->flags |= USERFLAG_ISAUTHED;
     }
     free(cache->oldauth);
+    free(cache);
+}
+
+void deleteUser(int userid) {
+    //simply delete the user
+    MYSQL_RES *res, *res2;
+    MYSQL_ROW row, row2;
+    printf_mysql_query("SELECT a.`chanuser_access`, a.`chanuser_cid`, (SELECT COUNT(*) FROM `chanusers` AS b WHERE b.`chanuser_cid` = a.`chanuser_cid` AND b.`chanuser_access` = 500) FROM `chanusers` AS a WHERE a.`chanuser_uid` = '%d'", userid);
+    res = mysql_use();
+    while((row = mysql_fetch_row(res))) {
+        if(!strcmp(row[0], "500") && !strcmp(row[2], "1")) {
+            //unregister channel
+            printf_mysql_query("SELECT `botid`, `channel_name` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `chanid` = '%s' AND `suspended` = '0'", row[1]);
+            res2 = mysql_use();
+            while((row2 = mysql_fetch_row(res2))) {
+                struct ClientSocket *bot;
+                int clientid = atoi(row2[0]);
+                for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+                    if(bot->clientid == clientid)
+                        putsock(bot, "PART %s :Channel unregistered.", row2[1]);
+                }
+            }
+            printf_mysql_query("DELETE FROM `bot_channels` WHERE `chanid` = '%s'", row[1]);
+        }
+    }
+    printf_mysql_query("DELETE FROM `chanusers` WHERE `chanuser_uid` = '%d'", userid);
+    printf_mysql_query("UPDATE `bans` SET `ban_owner` = 0 WHERE `ban_owner` = '%d'", userid);
+    printf_mysql_query("UPDATE `donotregister` SET `dnr_user` = 0 WHERE `dnr_user` = '%d'", userid);
+    printf_mysql_query("UPDATE `bans` SET `ban_owner` = 0 WHERE `ban_owner` = '%d'", userid);
+    printf_mysql_query("UPDATE `godlog` SET `godlog_uid` = 0 WHERE `godlog_uid` = '%d'", userid);
+    printf_mysql_query("DELETE FROM `noinvite` WHERE `uid` = '%d'", userid);
+    printf_mysql_query("UPDATE `owner_history` SET `owner_history_to_uid` = 0 WHERE `owner_history_to_uid` = '%d'", userid);
+    printf_mysql_query("UPDATE `owner_history` SET `owner_history_from_uid` = 0 WHERE `owner_history_from_uid` = '%d'", userid);
+    printf_mysql_query("UPDATE `channels` SET `channel_registrator` = 0 WHERE `channel_registrator` = '%d'", userid);
+    printf_mysql_query("DELETE FROM `users` WHERE `user_id` = '%d'", userid);
+    struct UserNode *user;
+    for(user = getAllUsers(NULL); user; user = getAllUsers(user)) {
+        if(user->flags & USERFLAG_HAS_USERID)
+            user->flags &= ~USERFLAG_HAS_USERID;
+    }
 }
 
 void init_DBHelper() {
-    bind_registered(event_user_registered);
+    bind_registered(event_user_registered, 0);
 }