1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* whowas.c: WHOWAS user cache. |
4 |
* |
5 |
* Copyright (C) 2005 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
#include "whowas.h" |
27 |
#include "client.h" |
28 |
#include "hash.h" |
29 |
#include "irc_string.h" |
30 |
#include "ircd.h" |
31 |
#include "ircd_defs.h" |
32 |
#include "numeric.h" |
33 |
#include "s_serv.h" |
34 |
#include "s_user.h" |
35 |
#include "send.h" |
36 |
#include "s_conf.h" |
37 |
#include "memory.h" |
38 |
|
39 |
/* internally defined function */ |
40 |
static void add_whowas_to_clist(struct Whowas **, struct Whowas *); |
41 |
static void del_whowas_from_clist(struct Whowas **, struct Whowas *); |
42 |
static void add_whowas_to_list(struct Whowas **, struct Whowas *); |
43 |
static void del_whowas_from_list(struct Whowas **, struct Whowas *); |
44 |
|
45 |
struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH]; |
46 |
struct Whowas *WHOWASHASH[HASHSIZE]; |
47 |
|
48 |
static unsigned int whowas_next = 0; |
49 |
|
50 |
void |
51 |
add_history(struct Client *client_p, int online) |
52 |
{ |
53 |
struct Whowas *who = &WHOWAS[whowas_next]; |
54 |
|
55 |
assert(client_p && client_p->servptr); |
56 |
|
57 |
if (who->hashv != -1) |
58 |
{ |
59 |
if (who->online) |
60 |
del_whowas_from_clist(&(who->online->whowas),who); |
61 |
del_whowas_from_list(&WHOWASHASH[who->hashv], who); |
62 |
} |
63 |
|
64 |
who->hashv = strhash(client_p->name); |
65 |
who->logoff = CurrentTime; |
66 |
|
67 |
/* NOTE: strcpy ok here, the sizes in the client struct MUST |
68 |
* match the sizes in the whowas struct |
69 |
*/ |
70 |
strlcpy(who->name, client_p->name, sizeof(who->name)); |
71 |
strcpy(who->username, client_p->username); |
72 |
strcpy(who->hostname, client_p->host); |
73 |
strcpy(who->realname, client_p->info); |
74 |
|
75 |
strlcpy(who->servername, client_p->servptr->name, sizeof(who->servername)); |
76 |
|
77 |
if (online) |
78 |
{ |
79 |
who->online = client_p; |
80 |
add_whowas_to_clist(&(client_p->whowas), who); |
81 |
} |
82 |
else |
83 |
who->online = NULL; |
84 |
|
85 |
add_whowas_to_list(&WHOWASHASH[who->hashv], who); |
86 |
whowas_next++; |
87 |
|
88 |
if (whowas_next == NICKNAMEHISTORYLENGTH) |
89 |
whowas_next = 0; |
90 |
} |
91 |
|
92 |
void |
93 |
off_history(struct Client *client_p) |
94 |
{ |
95 |
struct Whowas *temp, *next; |
96 |
|
97 |
for (temp = client_p->whowas; temp; temp=next) |
98 |
{ |
99 |
next = temp->cnext; |
100 |
temp->online = NULL; |
101 |
del_whowas_from_clist(&(client_p->whowas), temp); |
102 |
} |
103 |
} |
104 |
|
105 |
struct Client * |
106 |
get_history(const char *nick, time_t timelimit) |
107 |
{ |
108 |
struct Whowas *temp; |
109 |
|
110 |
timelimit = CurrentTime - timelimit; |
111 |
temp = WHOWASHASH[strhash(nick)]; |
112 |
|
113 |
for (; temp; temp = temp->next) |
114 |
{ |
115 |
if (irccmp(nick, temp->name)) |
116 |
continue; |
117 |
if (temp->logoff < timelimit) |
118 |
continue; |
119 |
return temp->online; |
120 |
} |
121 |
|
122 |
return NULL; |
123 |
} |
124 |
|
125 |
void |
126 |
count_whowas_memory(unsigned int *wwu, uint64_t *wwum) |
127 |
{ |
128 |
const struct Whowas *tmp; |
129 |
int i; |
130 |
unsigned int u = 0; |
131 |
uint64_t um = 0; |
132 |
|
133 |
/* count the number of used whowas structs in 'u' */ |
134 |
/* count up the memory used of whowas structs in um */ |
135 |
for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; ++i, ++tmp) |
136 |
{ |
137 |
if (tmp->hashv != -1) |
138 |
{ |
139 |
++u; |
140 |
um += sizeof(struct Whowas); |
141 |
} |
142 |
} |
143 |
|
144 |
*wwu = u; |
145 |
*wwum = um; |
146 |
} |
147 |
|
148 |
void |
149 |
init_whowas(void) |
150 |
{ |
151 |
int i; |
152 |
|
153 |
for (i = 0; i < NICKNAMEHISTORYLENGTH; i++) |
154 |
{ |
155 |
memset(&WHOWAS[i], 0, sizeof(struct Whowas)); |
156 |
WHOWAS[i].hashv = -1; |
157 |
} |
158 |
|
159 |
for (i = 0; i < HASHSIZE; ++i) |
160 |
WHOWASHASH[i] = NULL; |
161 |
} |
162 |
|
163 |
static void |
164 |
add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas) |
165 |
{ |
166 |
whowas->cprev = NULL; |
167 |
|
168 |
if ((whowas->cnext = *bucket) != NULL) |
169 |
whowas->cnext->cprev = whowas; |
170 |
*bucket = whowas; |
171 |
} |
172 |
|
173 |
static void |
174 |
del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas) |
175 |
{ |
176 |
if (whowas->cprev) |
177 |
whowas->cprev->cnext = whowas->cnext; |
178 |
else |
179 |
*bucket = whowas->cnext; |
180 |
if (whowas->cnext) |
181 |
whowas->cnext->cprev = whowas->cprev; |
182 |
} |
183 |
|
184 |
static void |
185 |
add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas) |
186 |
{ |
187 |
whowas->prev = NULL; |
188 |
|
189 |
if ((whowas->next = *bucket) != NULL) |
190 |
whowas->next->prev = whowas; |
191 |
*bucket = whowas; |
192 |
} |
193 |
|
194 |
static void |
195 |
del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas) |
196 |
{ |
197 |
if (whowas->prev) |
198 |
whowas->prev->next = whowas->next; |
199 |
else |
200 |
*bucket = whowas->next; |
201 |
if (whowas->next) |
202 |
whowas->next->prev = whowas->prev; |
203 |
} |