]> jfr.im git - irc/rqf/shadowircd.git/blob - src/whowas.c
Get rid of User.server.
[irc/rqf/shadowircd.git] / src / whowas.c
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 #include "scache.h"
43
44 /* internally defined function */
45 static void add_whowas_to_clist(struct Whowas **, struct Whowas *);
46 static void del_whowas_from_clist(struct Whowas **, struct Whowas *);
47 static void add_whowas_to_list(struct Whowas **, struct Whowas *);
48 static void del_whowas_from_list(struct Whowas **, struct Whowas *);
49
50 struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH];
51 struct Whowas *WHOWASHASH[WW_MAX];
52
53 static int whowas_next = 0;
54
55 unsigned int hash_whowas_name(const char *name)
56 {
57 return fnv_hash_upper((const unsigned char *) name, WW_MAX_BITS);
58 }
59
60 void add_history(struct Client *client_p, int online)
61 {
62 struct Whowas *who = &WHOWAS[whowas_next];
63
64 s_assert(NULL != client_p);
65
66 if(client_p == NULL)
67 return;
68
69 if(who->hashv != -1)
70 {
71 if(who->online)
72 del_whowas_from_clist(&(who->online->whowas), who);
73 del_whowas_from_list(&WHOWASHASH[who->hashv], who);
74 }
75 who->hashv = hash_whowas_name(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 if (!EmptyString(client_p->sockhost) && strcmp(client_p->sockhost, "0") && show_ip(NULL, client_p))
86 strcpy(who->sockhost, client_p->sockhost);
87 else
88 who->sockhost[0] = '\0';
89
90 who->servername = find_or_add(client_p->servptr->name);
91
92 if(online)
93 {
94 who->online = client_p;
95 add_whowas_to_clist(&(client_p->whowas), who);
96 }
97 else
98 who->online = NULL;
99 add_whowas_to_list(&WHOWASHASH[who->hashv], who);
100 whowas_next++;
101 if(whowas_next == NICKNAMEHISTORYLENGTH)
102 whowas_next = 0;
103 }
104
105 void off_history(struct Client *client_p)
106 {
107 struct Whowas *temp, *next;
108
109 for (temp = client_p->whowas; temp; temp = next)
110 {
111 next = temp->cnext;
112 temp->online = NULL;
113 del_whowas_from_clist(&(client_p->whowas), temp);
114 }
115 }
116
117 struct Client *get_history(const char *nick, time_t timelimit)
118 {
119 struct Whowas *temp;
120 int blah;
121
122 timelimit = CurrentTime - timelimit;
123 blah = hash_whowas_name(nick);
124 temp = WHOWASHASH[blah];
125 for (; temp; temp = temp->next)
126 {
127 if(irccmp(nick, temp->name))
128 continue;
129 if(temp->logoff < timelimit)
130 continue;
131 return temp->online;
132 }
133 return NULL;
134 }
135
136 void count_whowas_memory(size_t * wwu, size_t * wwum)
137 {
138 struct Whowas *tmp;
139 int i;
140 size_t u = 0;
141 size_t um = 0;
142
143 /* count the number of used whowas structs in 'u' */
144 /* count up the memory used of whowas structs in um */
145
146 for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; i++, tmp++)
147 if(tmp->hashv != -1)
148 {
149 u++;
150 um += sizeof(struct Whowas);
151 }
152 *wwu = u;
153 *wwum = um;
154 return;
155 }
156
157 void
158 initwhowas()
159 {
160 int i;
161
162 for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
163 {
164 memset((void *) &WHOWAS[i], 0, sizeof(struct Whowas));
165 WHOWAS[i].hashv = -1;
166 }
167 for (i = 0; i < WW_MAX; i++)
168 WHOWASHASH[i] = NULL;
169 }
170
171
172 static void
173 add_whowas_to_clist(struct Whowas **bucket, struct Whowas *whowas)
174 {
175 whowas->cprev = NULL;
176 if((whowas->cnext = *bucket) != NULL)
177 whowas->cnext->cprev = whowas;
178 *bucket = whowas;
179 }
180
181 static void
182 del_whowas_from_clist(struct Whowas **bucket, struct Whowas *whowas)
183 {
184 if(whowas->cprev)
185 whowas->cprev->cnext = whowas->cnext;
186 else
187 *bucket = whowas->cnext;
188 if(whowas->cnext)
189 whowas->cnext->cprev = whowas->cprev;
190 }
191
192 static void
193 add_whowas_to_list(struct Whowas **bucket, struct Whowas *whowas)
194 {
195 whowas->prev = NULL;
196 if((whowas->next = *bucket) != NULL)
197 whowas->next->prev = whowas;
198 *bucket = whowas;
199 }
200
201 static void
202 del_whowas_from_list(struct Whowas **bucket, struct Whowas *whowas)
203 {
204 if(whowas->prev)
205 whowas->prev->next = whowas->next;
206 else
207 *bucket = whowas->next;
208 if(whowas->next)
209 whowas->next->prev = whowas->prev;
210 }