added experimental multi thread support
[NeonServV5.git] / src / UserNode.c
index df4edebfe73f54a55e5d731ef957f9085baf1695..f90e72f2c1876fc95dcf315bb0b376d34bd6d276 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
  */
 #include "UserNode.h"
+#include "ChanNode.h"
 #include "ChanUser.h"
 #include "tools.h"
 #include "IRCEvents.h"
+#include "IPNode.h"
 
 static struct UserNode **userList;
 
@@ -122,7 +124,7 @@ struct UserNode* searchUserByNick(const char *nick) { //case insensitive
 int countUsersWithHost(char *host) {
     int i, count = 0;
     struct UserNode *user;
-    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
         for(user = userList[i]; user; user = user->next) {
             if(!strcmp(user->host, host)) {
                 count++;
@@ -135,7 +137,7 @@ int countUsersWithHost(char *host) {
 char *getAuthFakehost(char *auth) {
     int i;
     struct UserNode *user;
-    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
         for(user = userList[i]; user; user = user->next) {
             if((user->flags & USERFLAG_ISAUTHED) && !strcmp(user->auth, auth) && isFakeHost(user->host)) {
                 return user->host;
@@ -152,7 +154,7 @@ struct UserNode* getAllUsers(struct UserNode *last) {
             cindex = 0;
         else
             cindex = get_nicklist_entry(last->nick[0]) + 1;
-        while(userList[cindex] == NULL && cindex <= VALID_NICK_CHARS_FIRST_LEN)
+        while(userList[cindex] == NULL && cindex < VALID_NICK_CHARS_FIRST_LEN)
             cindex++;
         if(cindex > VALID_NICK_CHARS_FIRST_LEN) return NULL;
         return userList[cindex];
@@ -177,7 +179,7 @@ struct UserNode* getUsersWithAuth(const char *auth, struct UserNode *last) {
 int getUserCount() {
     int i, count = 0;
     struct UserNode *user;
-    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
         for(user = userList[i]; user; user = user->next) {
             count++;
         }
@@ -199,12 +201,15 @@ struct UserNode* addUser(const char *nick) {
     user->created = time(0);
     user->ident[0] = 0;
     user->host[0] = 0;
+    user->ip = NULL;
     user->realname[0] = 0;
     user->flags = 0;
     user->channel = NULL;
     user->last_who = 0;
+    SYNCHRONIZE(cache_sync);
     user->next = userList[userListIndex];
     userList[userListIndex] = user;
+    DESYNCHRONIZE(cache_sync);
     return user;
 }
 
@@ -235,29 +240,84 @@ struct UserNode* addUserMask(const char *mask) {
     return user;
 }
 
-struct UserNode* createTempUser(const char *mask) {
+struct UserNode* createTempUser(const char *nick) {
+    int already_on_list = 0;
+    struct UserNode *user = NULL;
+    if(!is_valid_nick(nick)) {
+        return NULL;
+    }
+    for(user = userList[TEMPUSER_LIST_INDEX]; user; user = user->next) {
+        if(!stricmp(user->nick, nick)) {
+            already_on_list = 1;
+            break;
+        }
+    }
+    if(!user) {
+        user = malloc(sizeof(*user));
+        if (!user) {
+            perror("malloc() failed");
+            return NULL;
+        }
+        user->ident[0] = 0;
+        user->host[0] = 0;
+        user->ip = NULL;
+        user->realname[0] = 0;
+        user->flags = 0;
+        user->channel = NULL;
+        user->last_who = 0;
+    } else
+        user->flags &= ~USERFLAG_FREETMPUSER;
+    user->created = time(0);
+    if(user->created - user->last_who > REWHO_TIMEOUT)
+        user->flags &= ~USERFLAG_ISAUTHED; //remove authed flag (security reasons)
+    strcpy(user->nick, nick);
+    if(!already_on_list) {
+        SYNCHRONIZE(cache_sync);
+        user->next = userList[TEMPUSER_LIST_INDEX];
+        userList[TEMPUSER_LIST_INDEX] = user;
+        DESYNCHRONIZE(cache_sync);
+    }
+    return user;
+}
+
+struct UserNode* createTempUserMask(const char *mask) {
     //note: it could also be a server we have to create a temponary user for...
     char cmask[strlen(mask)+1];
     strcpy(cmask, mask);
     int i, ii = 0;
+    int already_on_list = 0;
     struct UserNode *user = NULL;
     for(i = 0; i < strlen(mask)+1; i++) {
         if(cmask[i] == '!') {
             cmask[i] = 0;
-            user = malloc(sizeof(*user));
-            if (!user)
-            {
-                perror("malloc() failed");
+            if(!is_valid_nick(cmask)) {
                 return NULL;
             }
-            strcpy(user->nick, cmask);
+            for(user = userList[TEMPUSER_LIST_INDEX]; user; user = user->next) {
+                if(!stricmp(user->nick, cmask)) {
+                    already_on_list = 1;
+                    break;
+                }
+            }
+            if(!user) {
+                user = malloc(sizeof(*user));
+                if (!user) {
+                    perror("malloc() failed");
+                    return NULL;
+                }
+                user->ident[0] = 0;
+                user->host[0] = 0;
+                user->ip = NULL;
+                user->realname[0] = 0;
+                user->flags = 0;
+                user->channel = NULL;
+                user->last_who = 0;
+            } else
+                user->flags &= ~USERFLAG_FREETMPUSER;
             user->created = time(0);
-            user->ident[0] = 0;
-            user->host[0] = 0;
-            user->realname[0] = 0;
-            user->flags = 0;
-            user->channel = NULL;
-            user->last_who = 0;
+            if(user->created - user->last_who > REWHO_TIMEOUT)
+                user->flags &= ~USERFLAG_ISAUTHED; //remove authed flag (security reasons)
+            strcpy(user->nick, cmask);
             ii = i+1;
         } else if(cmask[i] == '.' && !user) {
             //it's a server
@@ -271,6 +331,7 @@ struct UserNode* createTempUser(const char *mask) {
             user->created = time(0);
             user->ident[0] = 0;
             user->host[0] = 0;
+            user->ip = NULL;
             user->realname[0] = 0;
             user->flags = USERFLAG_ISSERVER;
             user->channel = NULL;
@@ -294,15 +355,22 @@ struct UserNode* createTempUser(const char *mask) {
                 user->created = time(0);
                 user->ident[0] = 0;
                 user->host[0] = 0;
+                user->ip = NULL;
                 user->realname[0] = 0;
                 user->flags = 0;
                 user->channel = NULL;
                 user->last_who = 0;
-                return user;
+                break;
             }
             strcpy(user->host, &cmask[ii]);
         }
     }
+    if(!already_on_list) {
+        SYNCHRONIZE(cache_sync);
+        user->next = userList[TEMPUSER_LIST_INDEX];
+        userList[TEMPUSER_LIST_INDEX] = user;
+        DESYNCHRONIZE(cache_sync);
+    }
     return user;
 }
 
@@ -315,6 +383,7 @@ int renameUser(struct UserNode* user, const char *new_nick) {
     }
     //delUser(user, 0); //EPIC FAIL! This deletes the user from the channel Userlist -.-
     //manually remove the user from the old userList
+    SYNCHRONIZE(cache_sync);
     int userListIndex = get_nicklist_entry(user->nick[0]);
     if(userListIndex != -1) {
         struct UserNode *cuser, *last_user = NULL;
@@ -333,12 +402,14 @@ int renameUser(struct UserNode* user, const char *new_nick) {
     strcpy(user->nick, new_nick);
     user->next = userList[userListIndex];
     userList[userListIndex] = user;
+    DESYNCHRONIZE(cache_sync);
     return 1;
 }
 
 void delUser(struct UserNode* user, int freeUser) {
-    int userListIndex = get_nicklist_entry(user->nick[0]);
+    int userListIndex = ((user->flags & USERFLAG_ISTMPUSER) ? TEMPUSER_LIST_INDEX : get_nicklist_entry(user->nick[0]));
     if(userListIndex == -1) return;
+    SYNCHRONIZE(cache_sync);
     event_freeuser(user);
     struct UserNode *cuser, *last_user = NULL;
     for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
@@ -351,10 +422,10 @@ void delUser(struct UserNode* user, int freeUser) {
         } else
             last_user = cuser;
     }
-       if(freeUser && (user->flags & USERFLAG_IS_ON_WHO_QUEUE)) {
-               user->flags |= USERFLAG_FREE_AFTER_WHO;
-               freeUser = 0;
-       }
+    if(freeUser && (user->flags & USERFLAG_IS_ON_WHO_QUEUE)) {
+        user->flags |= USERFLAG_FREE_AFTER_WHO;
+        freeUser = 0;
+    }
     if(user->channel) {
         struct ChanUser *chanUser, *next;
         for(chanUser = user->channel; chanUser; chanUser = next) {
@@ -362,25 +433,25 @@ void delUser(struct UserNode* user, int freeUser) {
             removeChanUserFromLists(chanUser, 1, 0, freeUser);
         }
     }
-    if(freeUser)
+    if(freeUser) {
+        if(user->ip)
+            freeIPNode(user->ip);
         free(user);
-    else
+    else
         user->next = NULL;
+    DESYNCHRONIZE(cache_sync);
 }
 
 void clearTempUsers() {
+    SYNCHRONIZE(cache_sync);
     int userListIndex = TEMPUSER_LIST_INDEX;
-    struct UserNode *cuser, *last_user = NULL, *next;
+    struct UserNode *cuser, *next;
     time_t now = time(0);
     for(cuser = userList[userListIndex]; cuser; cuser = next) {
         next = cuser->next;
         if(cuser->flags & USERFLAG_FREETMPUSER || now - cuser->created >= 300) {
-            if(last_user)
-                last_user->next = cuser->next;
-            else
-                userList[userListIndex] = cuser->next;
-            break;
-        } else
-            last_user = cuser;
+            delUser(cuser, 1);
+        }
     }
+    DESYNCHRONIZE(cache_sync);
 }