X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FUserNode.c;h=cd8735e8f5ba7716b4cb5c3996d04b1353d53c7f;hp=f90e72f2c1876fc95dcf315bb0b376d34bd6d276;hb=HEAD;hpb=55831bf424312a6908ca07a904f288fba0919a9a diff --git a/src/UserNode.c b/src/UserNode.c index f90e72f..cd8735e 100644 --- a/src/UserNode.c +++ b/src/UserNode.c @@ -1,4 +1,4 @@ -/* UserNode.c - NeonServ v5.3 +/* UserNode.c - NeonServ v5.6 * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -20,11 +20,40 @@ #include "tools.h" #include "IRCEvents.h" #include "IPNode.h" +#include "log.h" static struct UserNode **userList; +unsigned int valid_user_modes[] = { + 1, 'o', + 2, 'O', + 3, 'i', + 4, 'w', + 5, 's', + 6, 'd', + 7, 'k', + 8, 'g', + 9, 'n', + 10, 'I', + 11, 'X', + 12, 'S', + 13, 'H', + 14, 'c', + 15, 'W', + 16, 't', + 17, 'D', + 18, 'x', +// ^ maximum is 32!!! + 0x00, 0x00 +}; + void init_UserNode() { + unsigned int *mode, flag = 1; userList = calloc(VALID_NICK_CHARS_FIRST_LEN+1, sizeof(*userList)); + for (mode = valid_user_modes; mode[1]; mode += 2) { + mode[0] = flag; + flag = flag << 1; + } } void free_UserNode() { @@ -65,19 +94,61 @@ static int get_nicklist_entry(int nick) { return -1; //ERROR! } +static unsigned int* getUserModeOptions(char mode) { + unsigned int *cmode; + for (cmode = valid_user_modes; cmode[1]; cmode += 2) { + if(cmode[1] == mode) + return cmode; + } + return NULL; +} + +int isUserModeSet(struct UserNode *user, char modeChar) { + unsigned int *modeOpt = getUserModeOptions(modeChar); + return (user->usermode & modeOpt[0]); +} + +void parseUserModes(struct UserNode* user, char *modeStr) { + int i, add = 1; + unsigned int *modeOpt; + for(i = 0; i < strlen(modeStr); i++) { + if(modeStr[i] == '+') { + add = 1; + continue; + } + if(modeStr[i] == '-') { + add = 0; + continue; + } + modeOpt = getUserModeOptions(modeStr[i]); + if(!modeOpt) continue; // unknown mode? + if(add) { + user->usermode |= modeOpt[0]; + } else { + user->usermode &= ~modeOpt[0]; + } + } +} + + struct UserNode* getUserByNick(const char *nick) { //case sensitive int userListIndex = get_nicklist_entry(*nick); if(userListIndex == -1 || userList[userListIndex] == NULL) return NULL; + SYNCHRONIZE(cache_sync); struct UserNode *user; for(user = userList[userListIndex]; user; user = user->next) { - if(!stricmp(nick, user->nick)) + if(!stricmp(nick, user->nick)) { + DESYNCHRONIZE(cache_sync); return user; + } } + DESYNCHRONIZE(cache_sync); return NULL; } struct UserNode* getUserByMask(const char *mask) { //case sensitive + SYNCHRONIZE(cache_sync); char cmask[strlen(mask)+1]; strcpy(cmask, mask); int i; @@ -86,12 +157,15 @@ struct UserNode* getUserByMask(const char *mask) { //case sensitive if(cmask[i] == '!') { cmask[i] = 0; user = getUserByNick(&cmask[0]); + DESYNCHRONIZE(cache_sync); return user; } else if(cmask[i] == '.') { //it's a server + DESYNCHRONIZE(cache_sync); return NULL; } } + DESYNCHRONIZE(cache_sync); return NULL; } @@ -99,6 +173,7 @@ struct UserNode* searchUserByNick(const char *nick) { //case insensitive if(!isalpha(*nick)) return getUserByNick(nick); + SYNCHRONIZE(cache_sync); int userListIndex; struct UserNode *user; @@ -106,22 +181,28 @@ struct UserNode* searchUserByNick(const char *nick) { //case insensitive userListIndex = get_nicklist_entry(tolower(*nick)); if(userListIndex != -1 && userList[userListIndex] != NULL) { for(user = userList[userListIndex]; user; user = user->next) { - if(!stricmp(nick, user->nick)) + if(!stricmp(nick, user->nick)) { + DESYNCHRONIZE(cache_sync); return user; + } } } //search in the upper case "section" userListIndex = get_nicklist_entry(toupper(*nick)); if(userListIndex != -1 && userList[userListIndex] != NULL) { for(user = userList[userListIndex]; user; user = user->next) { - if(!stricmp(nick, user->nick)) + if(!stricmp(nick, user->nick)) { + DESYNCHRONIZE(cache_sync); return user; + } } } + DESYNCHRONIZE(cache_sync); return NULL; } int countUsersWithHost(char *host) { + SYNCHRONIZE(cache_sync); int i, count = 0; struct UserNode *user; for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) { @@ -131,23 +212,28 @@ int countUsersWithHost(char *host) { } } } + DESYNCHRONIZE(cache_sync); return count; } char *getAuthFakehost(char *auth) { + SYNCHRONIZE(cache_sync); int i; struct UserNode *user; 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)) { + DESYNCHRONIZE(cache_sync); return user->host; } } } + DESYNCHRONIZE(cache_sync); return NULL; } struct UserNode* getAllUsers(struct UserNode *last) { + SYNCHRONIZE(cache_sync); if(last == NULL || last->next == NULL) { int cindex; if(last == NULL) @@ -156,27 +242,35 @@ struct UserNode* getAllUsers(struct UserNode *last) { 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; + DESYNCHRONIZE(cache_sync); + if(cindex >= VALID_NICK_CHARS_FIRST_LEN) return NULL; return userList[cindex]; - } else + } else { + DESYNCHRONIZE(cache_sync); return last->next; + } } struct UserNode* getUsersWithAuth(const char *auth, struct UserNode *last) { + SYNCHRONIZE(cache_sync); int cindex = (last ? get_nicklist_entry(last->nick[0]) : 0); struct UserNode *cuser = last; while(cindex <= VALID_NICK_CHARS_FIRST_LEN) { for(cuser = (cuser ? cuser->next : userList[cindex]); cuser; cuser = cuser->next) { - if((cuser->flags & USERFLAG_ISAUTHED) && !strcmp(cuser->auth, auth)) + if((cuser->flags & USERFLAG_ISAUTHED) && !strcmp(cuser->auth, auth)) { + DESYNCHRONIZE(cache_sync); return cuser; + } } cindex++; cuser = NULL; } + DESYNCHRONIZE(cache_sync); return NULL; } int getUserCount() { + SYNCHRONIZE(cache_sync); int i, count = 0; struct UserNode *user; for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) { @@ -184,6 +278,7 @@ int getUserCount() { count++; } } + DESYNCHRONIZE(cache_sync); return count; } @@ -194,7 +289,7 @@ struct UserNode* addUser(const char *nick) { struct UserNode *user = malloc(sizeof(*user)); if (!user) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return NULL; } strcpy(user->nick, nick); @@ -206,6 +301,7 @@ struct UserNode* addUser(const char *nick) { user->flags = 0; user->channel = NULL; user->last_who = 0; + user->usermode = 0; SYNCHRONIZE(cache_sync); user->next = userList[userListIndex]; userList[userListIndex] = user; @@ -255,7 +351,7 @@ struct UserNode* createTempUser(const char *nick) { if(!user) { user = malloc(sizeof(*user)); if (!user) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return NULL; } user->ident[0] = 0; @@ -264,6 +360,7 @@ struct UserNode* createTempUser(const char *nick) { user->realname[0] = 0; user->flags = 0; user->channel = NULL; + user->usermode = 0; user->last_who = 0; } else user->flags &= ~USERFLAG_FREETMPUSER; @@ -302,7 +399,7 @@ struct UserNode* createTempUserMask(const char *mask) { if(!user) { user = malloc(sizeof(*user)); if (!user) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return NULL; } user->ident[0] = 0; @@ -311,6 +408,7 @@ struct UserNode* createTempUserMask(const char *mask) { user->realname[0] = 0; user->flags = 0; user->channel = NULL; + user->usermode = 0; user->last_who = 0; } else user->flags &= ~USERFLAG_FREETMPUSER; @@ -324,10 +422,12 @@ struct UserNode* createTempUserMask(const char *mask) { user = malloc(sizeof(*user)); if (!user) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return NULL; } strcpy(user->host, cmask); + strncpy(user->nick, cmask, NICKLEN); + user->nick[NICKLEN] = 0; user->created = time(0); user->ident[0] = 0; user->host[0] = 0; @@ -335,8 +435,9 @@ struct UserNode* createTempUserMask(const char *mask) { user->realname[0] = 0; user->flags = USERFLAG_ISSERVER; user->channel = NULL; + user->usermode = 0; user->last_who = 0; - return user; + break; } else if(cmask[i] == '@') { if(user == NULL) return NULL; cmask[i] = 0; @@ -348,7 +449,7 @@ struct UserNode* createTempUserMask(const char *mask) { user = malloc(sizeof(*user)); if (!user) { - perror("malloc() failed"); + printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__); return NULL; } strcpy(user->nick, cmask); @@ -359,6 +460,7 @@ struct UserNode* createTempUserMask(const char *mask) { user->realname[0] = 0; user->flags = 0; user->channel = NULL; + user->usermode = 0; user->last_who = 0; break; }