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 != NULL); |
57 |
|
58 |
if (client_p == NULL) |
59 |
return; |
60 |
|
61 |
/* XXX when is this possible? Looks like it could happen |
62 |
* (with a half registered client.) |
63 |
* and what is the correct action here? - Dianora |
64 |
*/ |
65 |
if (client_p->servptr == NULL) |
66 |
return; |
67 |
|
68 |
if (who->hashv != -1) |
69 |
{ |
70 |
if (who->online) |
71 |
del_whowas_from_clist(&(who->online->whowas),who); |
72 |
del_whowas_from_list(&WHOWASHASH[who->hashv], who); |
73 |
} |
74 |
|
75 |
who->hashv = strhash(client_p->name); |
76 |
who->logoff = CurrentTime; |
77 |
|
78 |
/* NOTE: strcpy ok here, the sizes in the client struct MUST |
79 |
* match the sizes in the whowas struct |
80 |
*/ |
81 |
strlcpy(who->name, client_p->name, sizeof(who->name)); |
82 |
strcpy(who->username, client_p->username); |
83 |
strcpy(who->hostname, client_p->host); |
84 |
strcpy(who->realname, client_p->info); |
85 |
|
86 |
strlcpy(who->servername, client_p->servptr->name, sizeof(who->servername)); |
87 |
|
88 |
if (online) |
89 |
{ |
90 |
who->online = client_p; |
91 |
add_whowas_to_clist(&(client_p->whowas), who); |
92 |
} |
93 |
else |
94 |
who->online = NULL; |
95 |
|
96 |
add_whowas_to_list(&WHOWASHASH[who->hashv], who); |
97 |
whowas_next++; |
98 |
|
99 |
if (whowas_next == NICKNAMEHISTORYLENGTH) |
100 |
whowas_next = 0; |
101 |
} |
102 |
|
103 |
void |
104 |
off_history(struct Client *client_p) |
105 |
{ |
106 |
struct Whowas *temp, *next; |
107 |
|
108 |
for (temp = client_p->whowas; temp; temp=next) |
109 |
{ |
110 |
next = temp->cnext; |
111 |
temp->online = NULL; |
112 |
del_whowas_from_clist(&(client_p->whowas), temp); |
113 |
} |
114 |
} |
115 |
|
116 |
struct Client * |
117 |
get_history(const char *nick, time_t timelimit) |
118 |
{ |
119 |
struct Whowas *temp; |
120 |
|
121 |
timelimit = CurrentTime - timelimit; |
122 |
temp = WHOWASHASH[strhash(nick)]; |
123 |
|
124 |
for (; temp; temp = temp->next) |
125 |
{ |
126 |
if (irccmp(nick, temp->name)) |
127 |
continue; |
128 |
if (temp->logoff < timelimit) |
129 |
continue; |
130 |
return(temp->online); |
131 |
} |
132 |
|
133 |
return(NULL); |
134 |
} |
135 |
|
136 |
void |
137 |
count_whowas_memory(int *wwu, unsigned long *wwum) |
138 |
{ |
139 |
struct Whowas *tmp; |
140 |
int i; |
141 |
int u = 0; |
142 |
unsigned long um = 0; |
143 |
|
144 |
/* count the number of used whowas structs in 'u' */ |
145 |
/* count up the memory used of whowas structs in um */ |
146 |
for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; i++, tmp++) |
147 |
{ |
148 |
if (tmp->hashv != -1) |
149 |
{ |
150 |
u++; |
151 |
um += sizeof(struct Whowas); |
152 |
} |
153 |
} |
154 |
|
155 |
*wwu = u; |
156 |
*wwum = um; |
157 |
} |
158 |
|
159 |
void |
160 |
init_whowas(void) |
161 |
{ |
162 |
int i; |
163 |
|
164 |
for (i = 0; i < NICKNAMEHISTORYLENGTH; i++) |
165 |
{ |
166 |
memset(&WHOWAS[i], 0, sizeof(struct Whowas)); |
167 |
WHOWAS[i].hashv = -1; |
168 |
} |
169 |
|
170 |
for (i = 0; i < HASHSIZE; ++i) |
171 |
WHOWASHASH[i] = NULL; |
172 |
} |
173 |
|
174 |
static void |
175 |
add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas) |
176 |
{ |
177 |
whowas->cprev = NULL; |
178 |
|
179 |
if ((whowas->cnext = *bucket) != NULL) |
180 |
whowas->cnext->cprev = whowas; |
181 |
*bucket = whowas; |
182 |
} |
183 |
|
184 |
static void |
185 |
del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas) |
186 |
{ |
187 |
if (whowas->cprev) |
188 |
whowas->cprev->cnext = whowas->cnext; |
189 |
else |
190 |
*bucket = whowas->cnext; |
191 |
if (whowas->cnext) |
192 |
whowas->cnext->cprev = whowas->cprev; |
193 |
} |
194 |
|
195 |
static void |
196 |
add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas) |
197 |
{ |
198 |
whowas->prev = NULL; |
199 |
|
200 |
if ((whowas->next = *bucket) != NULL) |
201 |
whowas->next->prev = whowas; |
202 |
*bucket = whowas; |
203 |
} |
204 |
|
205 |
static void |
206 |
del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas) |
207 |
{ |
208 |
if (whowas->prev) |
209 |
whowas->prev->next = whowas->next; |
210 |
else |
211 |
*bucket = whowas->next; |
212 |
if (whowas->next) |
213 |
whowas->next->prev = whowas->prev; |
214 |
} |