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 "common.h" |
29 |
#include "hash.h" |
30 |
#include "irc_string.h" |
31 |
#include "ircd.h" |
32 |
#include "ircd_defs.h" |
33 |
#include "numeric.h" |
34 |
#include "s_serv.h" |
35 |
#include "s_user.h" |
36 |
#include "send.h" |
37 |
#include "s_conf.h" |
38 |
#include "memory.h" |
39 |
|
40 |
/* internally defined function */ |
41 |
static void add_whowas_to_clist(struct Whowas **, struct Whowas *); |
42 |
static void del_whowas_from_clist(struct Whowas **, struct Whowas *); |
43 |
static void add_whowas_to_list(struct Whowas **, struct Whowas *); |
44 |
static void del_whowas_from_list(struct Whowas **, struct Whowas *); |
45 |
|
46 |
struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH]; |
47 |
struct Whowas *WHOWASHASH[HASHSIZE]; |
48 |
|
49 |
static unsigned int whowas_next = 0; |
50 |
|
51 |
void |
52 |
add_history(struct Client *client_p, int online) |
53 |
{ |
54 |
struct Whowas *who = &WHOWAS[whowas_next]; |
55 |
|
56 |
assert(client_p && client_p->servptr); |
57 |
|
58 |
if (who->hashv != -1) |
59 |
{ |
60 |
if (who->online) |
61 |
del_whowas_from_clist(&(who->online->whowas),who); |
62 |
del_whowas_from_list(&WHOWASHASH[who->hashv], who); |
63 |
} |
64 |
|
65 |
who->hashv = strhash(client_p->name); |
66 |
who->logoff = CurrentTime; |
67 |
|
68 |
/* NOTE: strcpy ok here, the sizes in the client struct MUST |
69 |
* match the sizes in the whowas struct |
70 |
*/ |
71 |
strlcpy(who->name, client_p->name, sizeof(who->name)); |
72 |
strcpy(who->username, client_p->username); |
73 |
strcpy(who->hostname, client_p->host); |
74 |
strcpy(who->realname, client_p->info); |
75 |
|
76 |
strlcpy(who->servername, client_p->servptr->name, sizeof(who->servername)); |
77 |
|
78 |
if (online) |
79 |
{ |
80 |
who->online = client_p; |
81 |
add_whowas_to_clist(&(client_p->whowas), who); |
82 |
} |
83 |
else |
84 |
who->online = NULL; |
85 |
|
86 |
add_whowas_to_list(&WHOWASHASH[who->hashv], who); |
87 |
whowas_next++; |
88 |
|
89 |
if (whowas_next == NICKNAMEHISTORYLENGTH) |
90 |
whowas_next = 0; |
91 |
} |
92 |
|
93 |
void |
94 |
off_history(struct Client *client_p) |
95 |
{ |
96 |
struct Whowas *temp, *next; |
97 |
|
98 |
for (temp = client_p->whowas; temp; temp=next) |
99 |
{ |
100 |
next = temp->cnext; |
101 |
temp->online = NULL; |
102 |
del_whowas_from_clist(&(client_p->whowas), temp); |
103 |
} |
104 |
} |
105 |
|
106 |
struct Client * |
107 |
get_history(const char *nick, time_t timelimit) |
108 |
{ |
109 |
struct Whowas *temp; |
110 |
|
111 |
timelimit = CurrentTime - timelimit; |
112 |
temp = WHOWASHASH[strhash(nick)]; |
113 |
|
114 |
for (; temp; temp = temp->next) |
115 |
{ |
116 |
if (irccmp(nick, temp->name)) |
117 |
continue; |
118 |
if (temp->logoff < timelimit) |
119 |
continue; |
120 |
return(temp->online); |
121 |
} |
122 |
|
123 |
return(NULL); |
124 |
} |
125 |
|
126 |
void |
127 |
count_whowas_memory(unsigned int *wwu, uint64_t *wwum) |
128 |
{ |
129 |
const struct Whowas *tmp; |
130 |
int i; |
131 |
unsigned int u = 0; |
132 |
uint64_t um = 0; |
133 |
|
134 |
/* count the number of used whowas structs in 'u' */ |
135 |
/* count up the memory used of whowas structs in um */ |
136 |
for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; ++i, ++tmp) |
137 |
{ |
138 |
if (tmp->hashv != -1) |
139 |
{ |
140 |
++u; |
141 |
um += sizeof(struct Whowas); |
142 |
} |
143 |
} |
144 |
|
145 |
*wwu = u; |
146 |
*wwum = um; |
147 |
} |
148 |
|
149 |
void |
150 |
init_whowas(void) |
151 |
{ |
152 |
int i; |
153 |
|
154 |
for (i = 0; i < NICKNAMEHISTORYLENGTH; i++) |
155 |
{ |
156 |
memset(&WHOWAS[i], 0, sizeof(struct Whowas)); |
157 |
WHOWAS[i].hashv = -1; |
158 |
} |
159 |
|
160 |
for (i = 0; i < HASHSIZE; ++i) |
161 |
WHOWASHASH[i] = NULL; |
162 |
} |
163 |
|
164 |
static void |
165 |
add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas) |
166 |
{ |
167 |
whowas->cprev = NULL; |
168 |
|
169 |
if ((whowas->cnext = *bucket) != NULL) |
170 |
whowas->cnext->cprev = whowas; |
171 |
*bucket = whowas; |
172 |
} |
173 |
|
174 |
static void |
175 |
del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas) |
176 |
{ |
177 |
if (whowas->cprev) |
178 |
whowas->cprev->cnext = whowas->cnext; |
179 |
else |
180 |
*bucket = whowas->cnext; |
181 |
if (whowas->cnext) |
182 |
whowas->cnext->cprev = whowas->cprev; |
183 |
} |
184 |
|
185 |
static void |
186 |
add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas) |
187 |
{ |
188 |
whowas->prev = NULL; |
189 |
|
190 |
if ((whowas->next = *bucket) != NULL) |
191 |
whowas->next->prev = whowas; |
192 |
*bucket = whowas; |
193 |
} |
194 |
|
195 |
static void |
196 |
del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas) |
197 |
{ |
198 |
if (whowas->prev) |
199 |
whowas->prev->next = whowas->next; |
200 |
else |
201 |
*bucket = whowas->next; |
202 |
if (whowas->next) |
203 |
whowas->next->prev = whowas->prev; |
204 |
} |