1 /* UserNode.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "IRCEvents.h"
25 static struct UserNode **userList;
27 unsigned int valid_user_modes[] = {
50 void init_UserNode() {
51 unsigned int *mode, flag = 1;
52 userList = calloc(VALID_NICK_CHARS_FIRST_LEN+1, sizeof(*userList));
53 for (mode = valid_user_modes; mode[1]; mode += 2) {
59 void free_UserNode() {
60 //kamikaze free all users
61 //chanusers will be destroyed in free_ChanNode()
63 struct UserNode *user, *next;
64 for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
65 for(user = userList[i]; user; user = next) {
73 int is_valid_nick(const char *nick) {
75 //first char must be one of: a-zA-Z{|}~[\]^_`
76 if (!strchr(VALID_NICK_CHARS_FIRST, *nick))
78 //all other chars must be one of: a-zA-Z0-9{|}~[\]^_`
79 for (i = 0; nick[i]; ++i)
80 if (!strchr(VALID_NICK_CHARS, nick[i]))
82 if (strlen(nick) > NICKLEN)
87 static int get_nicklist_entry(int nick) {
89 char *valid_chars = VALID_NICK_CHARS_FIRST;
90 for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
91 if(valid_chars[i] == nick)
97 static unsigned int* getUserModeOptions(char mode) {
99 for (cmode = valid_user_modes; cmode[1]; cmode += 2) {
106 int isUserModeSet(struct UserNode *user, char modeChar) {
107 unsigned int *modeOpt = getUserModeOptions(modeChar);
108 return (user->usermode & modeOpt[0]);
111 void parseUserModes(struct UserNode* user, char *modeStr) {
113 unsigned int *modeOpt;
114 for(i = 0; i < strlen(modeStr); i++) {
115 if(modeStr[i] == '+') {
119 if(modeStr[i] == '-') {
123 modeOpt = getUserModeOptions(modeStr[i]);
124 if(!modeOpt) continue; // unknown mode?
126 user->usermode |= modeOpt[0];
128 user->usermode &= ~modeOpt[0];
134 struct UserNode* getUserByNick(const char *nick) { //case sensitive
135 int userListIndex = get_nicklist_entry(*nick);
136 if(userListIndex == -1 || userList[userListIndex] == NULL)
138 SYNCHRONIZE(cache_sync);
139 struct UserNode *user;
140 for(user = userList[userListIndex]; user; user = user->next) {
141 if(!stricmp(nick, user->nick)) {
142 DESYNCHRONIZE(cache_sync);
146 DESYNCHRONIZE(cache_sync);
150 struct UserNode* getUserByMask(const char *mask) { //case sensitive
151 SYNCHRONIZE(cache_sync);
152 char cmask[strlen(mask)+1];
155 struct UserNode *user = NULL;
156 for(i = 0; i < strlen(mask); i++) {
157 if(cmask[i] == '!') {
159 user = getUserByNick(&cmask[0]);
160 DESYNCHRONIZE(cache_sync);
162 } else if(cmask[i] == '.') {
164 DESYNCHRONIZE(cache_sync);
168 DESYNCHRONIZE(cache_sync);
172 struct UserNode* searchUserByNick(const char *nick) { //case insensitive
174 return getUserByNick(nick);
176 SYNCHRONIZE(cache_sync);
178 struct UserNode *user;
180 //search in the lower case "section"
181 userListIndex = get_nicklist_entry(tolower(*nick));
182 if(userListIndex != -1 && userList[userListIndex] != NULL) {
183 for(user = userList[userListIndex]; user; user = user->next) {
184 if(!stricmp(nick, user->nick)) {
185 DESYNCHRONIZE(cache_sync);
190 //search in the upper case "section"
191 userListIndex = get_nicklist_entry(toupper(*nick));
192 if(userListIndex != -1 && userList[userListIndex] != NULL) {
193 for(user = userList[userListIndex]; user; user = user->next) {
194 if(!stricmp(nick, user->nick)) {
195 DESYNCHRONIZE(cache_sync);
200 DESYNCHRONIZE(cache_sync);
204 int countUsersWithHost(char *host) {
205 SYNCHRONIZE(cache_sync);
207 struct UserNode *user;
208 for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
209 for(user = userList[i]; user; user = user->next) {
210 if(!strcmp(user->host, host)) {
215 DESYNCHRONIZE(cache_sync);
219 char *getAuthFakehost(char *auth) {
220 SYNCHRONIZE(cache_sync);
222 struct UserNode *user;
223 for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
224 for(user = userList[i]; user; user = user->next) {
225 if((user->flags & USERFLAG_ISAUTHED) && !strcmp(user->auth, auth) && isFakeHost(user->host)) {
226 DESYNCHRONIZE(cache_sync);
231 DESYNCHRONIZE(cache_sync);
235 struct UserNode* getAllUsers(struct UserNode *last) {
236 SYNCHRONIZE(cache_sync);
237 if(last == NULL || last->next == NULL) {
242 cindex = get_nicklist_entry(last->nick[0]) + 1;
243 while(userList[cindex] == NULL && cindex < VALID_NICK_CHARS_FIRST_LEN)
245 DESYNCHRONIZE(cache_sync);
246 if(cindex >= VALID_NICK_CHARS_FIRST_LEN) return NULL;
247 return userList[cindex];
249 DESYNCHRONIZE(cache_sync);
254 struct UserNode* getUsersWithAuth(const char *auth, struct UserNode *last) {
255 SYNCHRONIZE(cache_sync);
256 int cindex = (last ? get_nicklist_entry(last->nick[0]) : 0);
257 struct UserNode *cuser = last;
258 while(cindex <= VALID_NICK_CHARS_FIRST_LEN) {
259 for(cuser = (cuser ? cuser->next : userList[cindex]); cuser; cuser = cuser->next) {
260 if((cuser->flags & USERFLAG_ISAUTHED) && !strcmp(cuser->auth, auth)) {
261 DESYNCHRONIZE(cache_sync);
268 DESYNCHRONIZE(cache_sync);
273 SYNCHRONIZE(cache_sync);
275 struct UserNode *user;
276 for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
277 for(user = userList[i]; user; user = user->next) {
281 DESYNCHRONIZE(cache_sync);
285 struct UserNode* addUser(const char *nick) {
286 int userListIndex = get_nicklist_entry(*nick);
287 if(userListIndex == -1 || !is_valid_nick(nick))
289 struct UserNode *user = malloc(sizeof(*user));
292 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
295 strcpy(user->nick, nick);
296 user->created = time(0);
300 user->realname[0] = 0;
302 user->channel = NULL;
305 SYNCHRONIZE(cache_sync);
306 user->next = userList[userListIndex];
307 userList[userListIndex] = user;
308 DESYNCHRONIZE(cache_sync);
312 struct UserNode* addUserMask(const char *mask) {
313 char cmask[strlen(mask)+1];
316 struct UserNode *user = NULL;
317 for(i = 0; i < strlen(mask)+1; i++) {
318 if(cmask[i] == '!') {
320 user = addUser(cmask);
321 if(user == NULL) return NULL;
323 } else if(cmask[i] == '.' && !user) {
326 } else if(cmask[i] == '@') {
327 if(user == NULL) return NULL;
329 strcpy(user->ident, &cmask[ii]);
331 } else if(cmask[i] == '\0') {
332 if(user == NULL) return NULL;
333 strcpy(user->host, &cmask[ii]);
339 struct UserNode* createTempUser(const char *nick) {
340 int already_on_list = 0;
341 struct UserNode *user = NULL;
342 if(!is_valid_nick(nick)) {
345 for(user = userList[TEMPUSER_LIST_INDEX]; user; user = user->next) {
346 if(!stricmp(user->nick, nick)) {
352 user = malloc(sizeof(*user));
354 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
360 user->realname[0] = 0;
362 user->channel = NULL;
366 user->flags &= ~USERFLAG_FREETMPUSER;
367 user->created = time(0);
368 if(user->created - user->last_who > REWHO_TIMEOUT)
369 user->flags &= ~USERFLAG_ISAUTHED; //remove authed flag (security reasons)
370 strcpy(user->nick, nick);
371 if(!already_on_list) {
372 SYNCHRONIZE(cache_sync);
373 user->next = userList[TEMPUSER_LIST_INDEX];
374 userList[TEMPUSER_LIST_INDEX] = user;
375 DESYNCHRONIZE(cache_sync);
380 struct UserNode* createTempUserMask(const char *mask) {
381 //note: it could also be a server we have to create a temponary user for...
382 char cmask[strlen(mask)+1];
385 int already_on_list = 0;
386 struct UserNode *user = NULL;
387 for(i = 0; i < strlen(mask)+1; i++) {
388 if(cmask[i] == '!') {
390 if(!is_valid_nick(cmask)) {
393 for(user = userList[TEMPUSER_LIST_INDEX]; user; user = user->next) {
394 if(!stricmp(user->nick, cmask)) {
400 user = malloc(sizeof(*user));
402 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
408 user->realname[0] = 0;
410 user->channel = NULL;
414 user->flags &= ~USERFLAG_FREETMPUSER;
415 user->created = time(0);
416 if(user->created - user->last_who > REWHO_TIMEOUT)
417 user->flags &= ~USERFLAG_ISAUTHED; //remove authed flag (security reasons)
418 strcpy(user->nick, cmask);
420 } else if(cmask[i] == '.' && !user) {
422 user = malloc(sizeof(*user));
425 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
428 strcpy(user->host, cmask);
429 strncpy(user->nick, cmask, NICKLEN);
430 user->nick[NICKLEN] = 0;
431 user->created = time(0);
435 user->realname[0] = 0;
436 user->flags = USERFLAG_ISSERVER;
437 user->channel = NULL;
441 } else if(cmask[i] == '@') {
442 if(user == NULL) return NULL;
444 strcpy(user->ident, &cmask[ii]);
446 } else if(cmask[i] == '\0') {
449 user = malloc(sizeof(*user));
452 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
455 strcpy(user->nick, cmask);
456 user->created = time(0);
460 user->realname[0] = 0;
462 user->channel = NULL;
467 strcpy(user->host, &cmask[ii]);
470 if(!already_on_list) {
471 SYNCHRONIZE(cache_sync);
472 user->next = userList[TEMPUSER_LIST_INDEX];
473 userList[TEMPUSER_LIST_INDEX] = user;
474 DESYNCHRONIZE(cache_sync);
479 int renameUser(struct UserNode* user, const char *new_nick) {
480 if(!is_valid_nick(new_nick))
482 if(user->nick[0] == *new_nick) {
483 strcpy(user->nick, new_nick);
486 //delUser(user, 0); //EPIC FAIL! This deletes the user from the channel Userlist -.-
487 //manually remove the user from the old userList
488 SYNCHRONIZE(cache_sync);
489 int userListIndex = get_nicklist_entry(user->nick[0]);
490 if(userListIndex != -1) {
491 struct UserNode *cuser, *last_user = NULL;
492 for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
495 last_user->next = user->next;
497 userList[userListIndex] = user->next;
503 userListIndex = get_nicklist_entry(*new_nick);
504 strcpy(user->nick, new_nick);
505 user->next = userList[userListIndex];
506 userList[userListIndex] = user;
507 DESYNCHRONIZE(cache_sync);
511 void delUser(struct UserNode* user, int freeUser) {
512 int userListIndex = ((user->flags & USERFLAG_ISTMPUSER) ? TEMPUSER_LIST_INDEX : get_nicklist_entry(user->nick[0]));
513 if(userListIndex == -1) return;
514 SYNCHRONIZE(cache_sync);
515 event_freeuser(user);
516 struct UserNode *cuser, *last_user = NULL;
517 for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
520 last_user->next = user->next;
522 userList[userListIndex] = user->next;
527 if(freeUser && (user->flags & USERFLAG_IS_ON_WHO_QUEUE)) {
528 user->flags |= USERFLAG_FREE_AFTER_WHO;
532 struct ChanUser *chanUser, *next;
533 for(chanUser = user->channel; chanUser; chanUser = next) {
534 next = chanUser->next_chan;
535 removeChanUserFromLists(chanUser, 1, 0, freeUser);
540 freeIPNode(user->ip);
544 DESYNCHRONIZE(cache_sync);
547 void clearTempUsers() {
548 SYNCHRONIZE(cache_sync);
549 int userListIndex = TEMPUSER_LIST_INDEX;
550 struct UserNode *cuser, *next;
551 time_t now = time(0);
552 for(cuser = userList[userListIndex]; cuser; cuser = next) {
554 if(cuser->flags & USERFLAG_FREETMPUSER || now - cuser->created >= 300) {
558 DESYNCHRONIZE(cache_sync);