#include "UserNode.h"
+#include "ChanUser.h"
+#include "tools.h"
static struct UserNode **userList;
void init_UserNode() {
- userList = calloc(VALID_NICK_CHARS_FIRST_LEN, sizeof(*userList));
-
+ 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) {
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))
return NULL;
}
strcpy(user->nick, nick);
+ user->created = time(0);
user->ident[0] = 0;
user->host[0] = 0;
user->realname[0] = 0;
return user;
}
-
struct UserNode* addUserMask(const char *mask) {
char cmask[strlen(mask)+1];
strcpy(cmask, mask);
for(i = 0; i < strlen(mask)+1; i++) {
if(cmask[i] == '!') {
cmask[i] = 0;
- user = addUser(&cmask[0]);
+ user = addUser(cmask);
if(user == NULL) return NULL;
ii = i+1;
} else if(cmask[i] == '.' && !user) {
strcpy(user->host, &cmask[ii]);
}
}
- return NULL;
+ return user;
+}
+
+struct UserNode* createTempUser(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;
+ 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");
+ 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;
+ ii = i+1;
+ } else if(cmask[i] == '.' && !user) {
+ //it's a server
+ user = malloc(sizeof(*user));
+ if (!user)
+ {
+ perror("malloc() failed");
+ return NULL;
+ }
+ strcpy(user->host, cmask);
+ user->created = time(0);
+ user->ident[0] = 0;
+ user->host[0] = 0;
+ user->realname[0] = 0;
+ user->flags = USERFLAG_ISSERVER;
+ user->channel = NULL;
+ return user;
+ } else if(cmask[i] == '@') {
+ if(user == NULL) return NULL;
+ cmask[i] = 0;
+ strcpy(user->ident, &cmask[ii]);
+ ii = i+1;
+ } else if(cmask[i] == '\0') {
+ 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]);
+ }
+ }
+ return user;
}
int renameUser(struct UserNode* user, const char *new_nick) {
} else
last_user = cuser;
}
+ if(user->channel) {
+ struct ChanUser *chanUser, *next;
+ for(chanUser = user->channel; chanUser; chanUser = next) {
+ next = chanUser->next_chan;
+ removeChanUserFromLists(chanUser, 1, 0, freeUser);
+ }
+ }
if(freeUser)
free(user);
else
user->next = NULL;
}
+
+void clearTempUsers() {
+ int userListIndex = TEMPUSER_LIST_INDEX;
+ struct UserNode *cuser, *last_user = NULL, *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;
+ }
+}