Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / whowas.c
blobfdf6b02f735b54c4f2bfa65e6922fc8a71600291
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * whowas.c: WHOWAS user cache.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
27 #include "whowas.h"
28 #include "client.h"
29 #include "common.h"
30 #include "hash.h"
31 #include "irc_string.h"
32 #include "ircd.h"
33 #include "ircd_defs.h"
34 #include "numeric.h"
35 #include "s_serv.h"
36 #include "s_user.h"
37 #include "send.h"
38 #include "s_conf.h"
39 #include "memory.h"
41 /* internally defined function */
42 static void add_whowas_to_clist(struct Whowas **, struct Whowas *);
43 static void del_whowas_from_clist(struct Whowas **, struct Whowas *);
44 static void add_whowas_to_list(struct Whowas **, struct Whowas *);
45 static void del_whowas_from_list(struct Whowas **, struct Whowas *);
47 struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH];
48 struct Whowas *WHOWASHASH[WW_MAX];
50 static int whowas_next = 0;
52 unsigned int hash_whowas_name(const char *name)
54 return fnv_hash_upper((const unsigned char *) name, WW_MAX_BITS);
57 void add_history(struct Client *client_p, int online)
59 struct Whowas *who = &WHOWAS[whowas_next];
61 s_assert(NULL != client_p);
63 if(client_p == NULL)
64 return;
66 if(who->hashv != -1)
68 if(who->online)
69 del_whowas_from_clist(&(who->online->whowas), who);
70 del_whowas_from_list(&WHOWASHASH[who->hashv], who);
72 who->hashv = hash_whowas_name(client_p->name);
73 who->logoff = CurrentTime;
75 * NOTE: strcpy ok here, the sizes in the client struct MUST
76 * match the sizes in the whowas struct
78 strlcpy(who->name, client_p->name, sizeof(who->name));
79 strcpy(who->username, client_p->username);
80 strcpy(who->hostname, client_p->host);
81 strcpy(who->realname, client_p->info);
82 if (!EmptyString(client_p->sockhost) && strcmp(client_p->sockhost, "0") && show_ip(NULL, client_p))
83 strcpy(who->sockhost, client_p->sockhost);
84 else
85 who->sockhost[0] = '\0';
87 who->servername = client_p->user->server;
89 if(online)
91 who->online = client_p;
92 add_whowas_to_clist(&(client_p->whowas), who);
94 else
95 who->online = NULL;
96 add_whowas_to_list(&WHOWASHASH[who->hashv], who);
97 whowas_next++;
98 if(whowas_next == NICKNAMEHISTORYLENGTH)
99 whowas_next = 0;
102 void off_history(struct Client *client_p)
104 struct Whowas *temp, *next;
106 for (temp = client_p->whowas; temp; temp = next)
108 next = temp->cnext;
109 temp->online = NULL;
110 del_whowas_from_clist(&(client_p->whowas), temp);
114 struct Client *get_history(const char *nick, time_t timelimit)
116 struct Whowas *temp;
117 int blah;
119 timelimit = CurrentTime - timelimit;
120 blah = hash_whowas_name(nick);
121 temp = WHOWASHASH[blah];
122 for (; temp; temp = temp->next)
124 if(irccmp(nick, temp->name))
125 continue;
126 if(temp->logoff < timelimit)
127 continue;
128 return temp->online;
130 return NULL;
133 void count_whowas_memory(size_t * wwu, size_t * wwum)
135 struct Whowas *tmp;
136 int i;
137 size_t u = 0;
138 size_t um = 0;
140 /* count the number of used whowas structs in 'u' */
141 /* count up the memory used of whowas structs in um */
143 for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; i++, tmp++)
144 if(tmp->hashv != -1)
146 u++;
147 um += sizeof(struct Whowas);
149 *wwu = u;
150 *wwum = um;
151 return;
154 void
155 initwhowas()
157 int i;
159 for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
161 memset((void *) &WHOWAS[i], 0, sizeof(struct Whowas));
162 WHOWAS[i].hashv = -1;
164 for (i = 0; i < WW_MAX; i++)
165 WHOWASHASH[i] = NULL;
169 static void
170 add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas)
172 whowas->cprev = NULL;
173 if((whowas->cnext = *bucket) != NULL)
174 whowas->cnext->cprev = whowas;
175 *bucket = whowas;
178 static void
179 del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas)
181 if(whowas->cprev)
182 whowas->cprev->cnext = whowas->cnext;
183 else
184 *bucket = whowas->cnext;
185 if(whowas->cnext)
186 whowas->cnext->cprev = whowas->cprev;
189 static void
190 add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas)
192 whowas->prev = NULL;
193 if((whowas->next = *bucket) != NULL)
194 whowas->next->prev = whowas;
195 *bucket = whowas;
198 static void
199 del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas)
201 if(whowas->prev)
202 whowas->prev->next = whowas->next;
203 else
204 *bucket = whowas->next;
205 if(whowas->next)
206 whowas->next->prev = whowas->prev;