]> jfr.im git - irc/rqf/shadowircd.git/blame - src/whowas.c
[svn] Merge old trunk r2294:
[irc/rqf/shadowircd.git] / src / whowas.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * whowas.c: WHOWAS user cache.
4 *
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
8 *
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.
13 *
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.
18 *
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
23 *
24 * $Id: whowas.c 1717 2006-07-04 14:41:11Z jilles $
25 */
26
27#include "stdinc.h"
28
29#include "whowas.h"
30#include "client.h"
31#include "common.h"
32#include "hash.h"
33#include "irc_string.h"
34#include "ircd.h"
35#include "ircd_defs.h"
36#include "numeric.h"
37#include "s_serv.h"
38#include "s_user.h"
39#include "send.h"
40#include "s_conf.h"
41#include "memory.h"
42
43/* internally defined function */
44static void add_whowas_to_clist(struct Whowas **, struct Whowas *);
45static void del_whowas_from_clist(struct Whowas **, struct Whowas *);
46static void add_whowas_to_list(struct Whowas **, struct Whowas *);
47static void del_whowas_from_list(struct Whowas **, struct Whowas *);
48
49struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH];
50struct Whowas *WHOWASHASH[WW_MAX];
51
52static int whowas_next = 0;
53
54unsigned int hash_whowas_name(const char *name)
55{
56 return fnv_hash_upper((const unsigned char *) name, WW_MAX_BITS);
57}
58
59void add_history(struct Client *client_p, int online)
60{
61 struct Whowas *who = &WHOWAS[whowas_next];
62
63 s_assert(NULL != client_p);
64
65 if(client_p == 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 who->hashv = hash_whowas_name(client_p->name);
75 who->logoff = CurrentTime;
76 /*
77 * NOTE: strcpy ok here, the sizes in the client struct MUST
78 * match the sizes in the whowas struct
79 */
80 strlcpy(who->name, client_p->name, sizeof(who->name));
81 strcpy(who->username, client_p->username);
82 strcpy(who->hostname, client_p->host);
83 strcpy(who->realname, client_p->info);
84 if (!EmptyString(client_p->sockhost) && strcmp(client_p->sockhost, "0") && show_ip(NULL, client_p))
85 strcpy(who->sockhost, client_p->sockhost);
86 else
87 who->sockhost[0] = '\0';
88
89 who->servername = client_p->user->server;
90
91 if(online)
92 {
93 who->online = client_p;
94 add_whowas_to_clist(&(client_p->whowas), who);
95 }
96 else
97 who->online = NULL;
98 add_whowas_to_list(&WHOWASHASH[who->hashv], who);
99 whowas_next++;
100 if(whowas_next == NICKNAMEHISTORYLENGTH)
101 whowas_next = 0;
102}
103
104void 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
116struct Client *get_history(const char *nick, time_t timelimit)
117{
118 struct Whowas *temp;
119 int blah;
120
121 timelimit = CurrentTime - timelimit;
122 blah = hash_whowas_name(nick);
123 temp = WHOWASHASH[blah];
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 return NULL;
133}
134
135void count_whowas_memory(size_t * wwu, size_t * wwum)
136{
137 struct Whowas *tmp;
138 int i;
139 size_t u = 0;
140 size_t um = 0;
141
142 /* count the number of used whowas structs in 'u' */
143 /* count up the memory used of whowas structs in um */
144
145 for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; i++, tmp++)
146 if(tmp->hashv != -1)
147 {
148 u++;
149 um += sizeof(struct Whowas);
150 }
151 *wwu = u;
152 *wwum = um;
153 return;
154}
155
156void
157initwhowas()
158{
159 int i;
160
161 for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
162 {
163 memset((void *) &WHOWAS[i], 0, sizeof(struct Whowas));
164 WHOWAS[i].hashv = -1;
165 }
166 for (i = 0; i < WW_MAX; i++)
167 WHOWASHASH[i] = NULL;
168}
169
170
171static void
172add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas)
173{
174 whowas->cprev = NULL;
175 if((whowas->cnext = *bucket) != NULL)
176 whowas->cnext->cprev = whowas;
177 *bucket = whowas;
178}
179
180static void
181del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas)
182{
183 if(whowas->cprev)
184 whowas->cprev->cnext = whowas->cnext;
185 else
186 *bucket = whowas->cnext;
187 if(whowas->cnext)
188 whowas->cnext->cprev = whowas->cprev;
189}
190
191static void
192add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas)
193{
194 whowas->prev = NULL;
195 if((whowas->next = *bucket) != NULL)
196 whowas->next->prev = whowas;
197 *bucket = whowas;
198}
199
200static void
201del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas)
202{
203 if(whowas->prev)
204 whowas->prev->next = whowas->next;
205 else
206 *bucket = whowas->next;
207 if(whowas->next)
208 whowas->next->prev = whowas->prev;
209}