changed Makefile; build all commands as an own file
[NeonServV5.git] / UserNode.c
index 465fd2e04bf6d2c43a221cebe6ab25c9d58ffbed..2ced2eb5b88d3f3129173b9b867a0fbf3823d048 100644 (file)
@@ -1,5 +1,6 @@
 #include "UserNode.h"
 #include "ChanUser.h"
+#include "tools.h"
 
 static struct UserNode **userList;
 
@@ -7,6 +8,20 @@ void init_UserNode() {
     userList = calloc(VALID_NICK_CHARS_FIRST_LEN+1, sizeof(*userList));
 }
 
+void free_UserNode() {
+    //kamikaze free all users
+    //chanusers will be destroyed in free_ChanNode()
+    int i;
+    struct UserNode *user, *next;
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+        for(user = userList[i]; user; user = next) {
+            next = user->next;
+            free(user);
+        }
+    }
+    free(userList);
+}
+
 int is_valid_nick(const char *nick) {
     unsigned int i;
     //first char must be one of: a-zA-Z{|}~[\]^_`
@@ -87,6 +102,58 @@ struct UserNode* searchUserByNick(const char *nick) { //case insensitive
     return NULL;
 }
 
+int countUsersWithHost(char *host) {
+    int i, count = 0;
+    struct UserNode *user;
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+        for(user = userList[i]; user; user = user->next) {
+            if(!strcmp(user->host, host)) {
+                count++;
+            }
+        }
+    }
+    return count;
+}
+
+char *getAuthFakehost(char *auth) {
+    int i;
+    struct UserNode *user;
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+        for(user = userList[i]; user; user = user->next) {
+            if((user->flags & USERFLAG_ISAUTHED) && !strcmp(user->auth, auth) && isFakeHost(user->host)) {
+                return user->host;
+            }
+        }
+    }
+    return NULL;
+}
+
+struct UserNode* getAllUsers(struct UserNode *last) {
+    if(last == NULL || last->next == NULL) {
+        int cindex;
+        if(last == NULL)
+            cindex = 0;
+        else
+            cindex = get_nicklist_entry(last->nick[0]) + 1;
+        while(userList[cindex] == NULL && cindex <= VALID_NICK_CHARS_FIRST_LEN)
+            cindex++;
+        if(cindex > VALID_NICK_CHARS_FIRST_LEN) return NULL;
+        return userList[cindex];
+    } else
+        return last->next;
+}
+
+int getUserCount() {
+    int i, count = 0;
+    struct UserNode *user;
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+        for(user = userList[i]; user; user = user->next) {
+            count++;
+        }
+    }
+    return count;
+}
+
 struct UserNode* addUser(const char *nick) {
     int userListIndex = get_nicklist_entry(*nick);
     if(userListIndex == -1 || !is_valid_nick(nick))
@@ -181,7 +248,23 @@ struct UserNode* createTempUser(const char *mask) {
             strcpy(user->ident, &cmask[ii]);
             ii = i+1;
         } else if(cmask[i] == '\0') {
-            if(user == NULL) return NULL;
+            if(user == NULL) {
+                //nick only
+                user = malloc(sizeof(*user));
+                if (!user)
+                {
+                    perror("malloc() failed");
+                    return NULL;
+                }
+                strcpy(user->nick, cmask);
+                user->created = time(0);
+                user->ident[0] = 0;
+                user->host[0] = 0;
+                user->realname[0] = 0;
+                user->flags = 0;
+                user->channel = NULL;
+                return user;
+            }
             strcpy(user->host, &cmask[ii]);
         }
     }