blob: b64ca7cb5a4202d951e147286f69aac7dc1897a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <stdlib.h>
#include <string.h>
#include "user.h"
#include "sdbm.h"
#include "nick.h"
struct user_t *users;
void user_init() {
users = malloc(sizeof(struct user_t) * USERS_MAX);
memset(users, 0, sizeof(struct user_t) * USERS_MAX);
}
struct user_t *user_get(char *nick) {
unsigned long hash = sdbm(nick);
int index = hash % USERS_MAX;
struct user_t *user = &users[index];
/* If hash doesn't match and there exists another user, fetch it. */
while(user->hash != hash && user->next) user = user->next;
/* If hash still doesn't match and the user exists, add a new user. */
if(user->hash != hash && user->nick) {
struct user_t *temp_user = malloc(sizeof(struct user_t));
user->next = temp_user;
user = temp_user;
/* Initialize nick to NULL so the user can be correctly added. */
user->nick = NULL;
}
/* Add the new user data to the current pointer if none was found. */
if(!user->nick) {
user->hash = hash;
user->nick = strdup(nick);
memset(user->lines, 0, 24*4 * sizeof(unsigned long));
user->words = user->characters = user->kicks = user->kicked = user->monolog_lines = user->monologs = 0;
user->next = NULL;
char *_nick = nick_get(nick);
user->real_user = strcmp(nick, _nick) ? user_get(_nick) : NULL;
}
return (user->real_user ? user->real_user : user);
}
void user_free() {
struct user_t *user;
for(int i = 0; i < USERS_MAX; i++) {
if(users[i].nick)
free(users[i].nick);
user = users[i].next;
while(user) {
struct user_t *temp = user->next;
free(user->nick);
free(user);
user = temp;
}
}
free(users);
}
|