added user registered event, tidied up helper functions and added automatic auth...
[NeonServV5.git] / src / DBHelper.c
index fe48bc4a54487bd5075fcfcbc483fc206b099b1b..a55b8c677de27c7ec0f7204df4c9c624b2703172 100644 (file)
@@ -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. */
@@ -223,3 +222,59 @@ int renameAccount(char *oldauth, char *newauth) {
     }
     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);
+}
+