X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FUserNode.c;h=cd8735e8f5ba7716b4cb5c3996d04b1353d53c7f;hp=21b618d1118b51a16fac594e638f44976378ba6b;hb=f6bdc9d0fc8db22c265918f3325e11177fd001b9;hpb=727b8a503f88131d92025bc43e3803d4a7c2aa00 diff --git a/src/UserNode.c b/src/UserNode.c index 21b618d..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 @@ -15,15 +15,45 @@ * along with this program. If not, see . */ #include "UserNode.h" +#include "ChanNode.h" #include "ChanUser.h" #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() { @@ -64,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; @@ -85,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; } @@ -98,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; @@ -105,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++) { @@ -130,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) @@ -155,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++) { @@ -183,6 +278,7 @@ int getUserCount() { count++; } } + DESYNCHRONIZE(cache_sync); return count; } @@ -193,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); @@ -205,8 +301,11 @@ 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; + DESYNCHRONIZE(cache_sync); return user; } @@ -252,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; @@ -261,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; @@ -269,8 +369,10 @@ struct UserNode* createTempUser(const char *nick) { 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; } @@ -297,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; @@ -306,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; @@ -319,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; @@ -330,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; @@ -343,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); @@ -354,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; } @@ -361,8 +468,10 @@ struct UserNode* createTempUserMask(const char *mask) { } } if(!already_on_list) { + SYNCHRONIZE(cache_sync); user->next = userList[TEMPUSER_LIST_INDEX]; userList[TEMPUSER_LIST_INDEX] = user; + DESYNCHRONIZE(cache_sync); } return user; } @@ -376,6 +485,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; @@ -394,12 +504,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 = ((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) { @@ -429,9 +541,11 @@ void delUser(struct UserNode* user, int freeUser) { free(user); } else user->next = NULL; + DESYNCHRONIZE(cache_sync); } void clearTempUsers() { + SYNCHRONIZE(cache_sync); int userListIndex = TEMPUSER_LIST_INDEX; struct UserNode *cuser, *next; time_t now = time(0); @@ -441,4 +555,5 @@ void clearTempUsers() { delUser(cuser, 1); } } + DESYNCHRONIZE(cache_sync); }