c2caf4a9d4b53b8a0c6691c37836f3d60bcd3e05
[ircu2.10.12-pk.git] / ircd / whowas.c
1 /*
2  * IRC - Internet Relay Chat, ircd/whowas.c
3  * Copyright (C) 1990 Markku Savela
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * --- avalon --- 6th April 1992
20  * rewritten to scrap linked lists and use a table of structures which
21  * is referenced like a circular loop. Should be faster and more efficient.
22  *
23  * --- comstud --- 25th March 1997
24  * Everything rewritten from scratch.  Avalon's code was bad.  My version
25  * is faster and more efficient.  No more hangs on /squits and you can
26  * safely raise NICKNAMEHISTORYLENGTH to a higher value without hurting
27  * performance.
28  *
29  * --- comstud --- 5th August 1997
30  * Fixed for Undernet..
31  *
32  * --- Run --- 27th August 1997
33  * Speeded up the code, added comments.
34  *
35  * $Id$
36  */
37 #include "whowas.h"
38 #include "client.h"
39 #include "ircd.h"
40 #include "ircd_alloc.h"
41 #include "ircd_chattr.h"
42 #include "ircd_string.h"
43 #include "list.h"
44 #include "numeric.h"
45 #include "s_misc.h"
46 #include "s_user.h"
47 #include "send.h"
48 #include "struct.h"
49 #include "support.h"
50 #include "sys.h"
51 #include "msg.h"
52
53 #include <assert.h>
54 #include <stdlib.h>
55
56
57 static struct Whowas  whowas[NICKNAMEHISTORYLENGTH];
58 static struct Whowas* whowas_next = whowas;
59 struct Whowas* whowashash[WW_MAX];
60
61 /*
62  * Since the introduction of numeric nicks (at least for upstream messages,
63  * like MODE +o <nick>, KICK #chan <nick>, KILL <nick> etc), there is no
64  * real important reason for a nick history anymore.
65  * Nevertheless, there are two reason why we might want to keep it:
66  * 1) The /WHOWAS command, which is often usefull to catch harrashing
67  *    users or abusers in general.
68  * 2) Clients still use the normal nicks in the client-server protocol,
69  *    and it might be considered a nice feature that here we still have
70  *    nick chasing.
71  * Note however that BOTH reasons make it redundant to keep a whowas history
72  * for users that split off.
73  *
74  * The rewrite of comstud was many to safe cpu during net.breaks and therefore
75  * a bit redundant imho (Run).
76  *
77  * But - it was written anyway.  So lets look at the structure of the
78  * whowas history now:
79  *
80  * We still have a static table of 'struct Whowas' structures in which we add
81  * new nicks (plus info) as in a rotating buffer.  We keep a global pointer
82  * `whowas_next' that points to the next entry to be overwritten - or to
83  * the oldest entry in the table (which is the same).
84  *
85  * Each entry keeps pointers for two doubly linked lists (thus four pointers):
86  * A list of the entries that have the same hash value ('hashv list'), and
87  * a list of the entries that have the same online pointer (`online list').
88  * Note that the last list (pointers) is only updated as long as online points
89  * to the corresponding client: As soon as the client signs off, this list
90  * is not anymore maintained (and hopefully not used anymore either ;).
91  *
92  * So now we have two ways of accessing this database:
93  * 1) Given a <nick> we can calculate a hashv and then whowashash[hashv] will
94  *    point to the start of the 'hash list': all entries with the same hashv.
95  *    We'll have to search this list to find the entry with the correct <nick>.
96  *    Once we found the correct whowas entry, we have a pointer to the
97  *    corresponding client - if still online - for nich chasing purposes.
98  *    Note that the same nick can occur multiple times in the whowas history,
99  *    each of these having the same hash value of course.  While a /WHOWAS on
100  *    just a nick will return all entries, nick chasing will only find the
101  *    first in the list.  Because new entries are added at the start of the
102  *    'hash list' we will always find the youngest entry, which is what we want.
103  * 2) Given an online client we have a pointer to the first whowas entry
104  *    of the linked list of whowas entries that all belong to this client.
105  *    We ONLY need this to reset all `online' pointers when this client
106  *    signs off.
107  *
108  * 27/8/79:
109  *
110  * Note that following:
111  *
112  * a) We *only* (need to) change the 'hash list' and the 'online' list
113  *    in add_history().
114  * b) There we always ADD an entry to the BEGINNING of the 'hash list'
115  *    and the 'online list': *new* entries are at the start of the lists.
116  *    The oldest entries are at the end of the lists.
117  * c) We always REMOVE the oldest entry we have (whowas_next), this means
118  *    that this is always an entry that is at the *end* of the 'hash list'
119  *    and 'online list' that it is a part of: the next pointer will
120  *    always be NULL.
121  * d) The previous pointer is *only* used to update the next pointer of the
122  *    previous entry, therefore we could better use a pointer to this
123  *    next pointer: That is faster - saves us a 'if' test (it will never be
124  *    NULL because the last added entry will point to the pointer that
125  *    points to the start of the list) and we won't need special code to
126  *    update the list start pointers.
127  *
128  * I incorporated these considerations into the code below.
129  *
130  * --Run
131  */
132
133 typedef union {
134   struct Whowas *newww;
135   struct Whowas *oldww;
136 } Current;
137
138 #define WHOWAS_UNUSED ((unsigned int)-1)
139
140 /*
141  * add_history
142  *
143  * Add a client (cptr) that just changed nick (still_on == true), or
144  * just signed off (still_on == false) to the `whowas' table.
145  *
146  * If the entry used was already in use, then this entry is
147  * freed (lost).
148  */
149 void add_history(struct Client *cptr, int still_on)
150 {
151   Current ww;
152   ww.newww = whowas_next;
153
154   /* If this entry has already been used, remove it from the lists */
155   if (ww.newww->hashv != WHOWAS_UNUSED)
156   {
157     if (ww.oldww->online)       /* No need to update cnext/cprev when offline! */
158     {
159       /* Remove ww.oldww from the linked list with the same `online' pointers */
160       *ww.oldww->cprevnextp = ww.oldww->cnext;
161
162       assert(0 == ww.oldww->cnext);
163
164     }
165     /* Remove ww.oldww from the linked list with the same `hashv' */
166     *ww.oldww->hprevnextp = ww.oldww->hnext;
167
168     assert(0 == ww.oldww->hnext);
169
170     if (ww.oldww->name)
171       MyFree(ww.oldww->name);
172     if (ww.oldww->username)
173       MyFree(ww.oldww->username);
174     if (ww.oldww->hostname)
175       MyFree(ww.oldww->hostname);
176     if (ww.oldww->servername)
177       MyFree(ww.oldww->servername);
178     if (ww.oldww->realname)
179       MyFree(ww.oldww->realname);
180     if (ww.oldww->away)
181       MyFree(ww.oldww->away);
182   }
183
184   /* Initialize aWhoWas struct `newww' */
185   ww.newww->hashv = hash_whowas_name(cptr->name);
186   ww.newww->logoff = CurrentTime;
187   DupString(ww.newww->name, cptr->name);
188   DupString(ww.newww->username, cptr->user->username);
189   DupString(ww.newww->hostname, cptr->user->host);
190   /* Should be changed to server numeric */
191   DupString(ww.newww->servername, cptr->user->server->name);
192   DupString(ww.newww->realname, cptr->info);
193   if (cptr->user->away)
194     DupString(ww.newww->away, cptr->user->away);
195   else
196     ww.newww->away = NULL;
197
198   /* Update/initialize online/cnext/cprev: */
199   if (still_on)                 /* User just changed nicknames */
200   {
201     ww.newww->online = cptr;
202     /* Add struct Whowas struct `newww' to start of 'online list': */
203     if ((ww.newww->cnext = cptr->whowas))
204       ww.newww->cnext->cprevnextp = &ww.newww->cnext;
205     ww.newww->cprevnextp = &cptr->whowas;
206     cptr->whowas = ww.newww;
207   }
208   else                          /* User quitting */
209     ww.newww->online = NULL;
210
211   /* Add struct Whowas struct `newww' to start of 'hashv list': */
212   if ((ww.newww->hnext = whowashash[ww.newww->hashv]))
213     ww.newww->hnext->hprevnextp = &ww.newww->hnext;
214   ww.newww->hprevnextp = &whowashash[ww.newww->hashv];
215   whowashash[ww.newww->hashv] = ww.newww;
216
217   /* Advance `whowas_next' to next entry in the `whowas' table: */
218   if (++whowas_next == &whowas[NICKNAMEHISTORYLENGTH])
219     whowas_next = whowas;
220 }
221
222 /*
223  * off_history
224  *
225  * Client `cptr' signed off: Set all `online' pointers
226  * corresponding to this client to NULL.
227  */
228 void off_history(const struct Client *cptr)
229 {
230   struct Whowas *temp;
231
232   for (temp = cptr->whowas; temp; temp = temp->cnext)
233     temp->online = NULL;
234 }
235
236 /*
237  * get_history
238  *
239  * Return a pointer to a client that had nick `nick' not more then
240  * `timelimit' seconds ago, if still on line.  Otherwise return NULL.
241  *
242  * This function is used for "nick chasing"; since the use of numeric
243  * nicks for "upstream" messages in ircu2.10, this is only used for
244  * looking up non-existing nicks in client->server messages.
245  */
246 struct Client *get_history(const char *nick, time_t timelimit)
247 {
248   struct Whowas *temp = whowashash[hash_whowas_name(nick)];
249   timelimit = CurrentTime - timelimit;
250
251   for (; temp; temp = temp->hnext)
252     if (0 == ircd_strcmp(nick, temp->name) && temp->logoff > timelimit)
253       return temp->online;
254
255   return NULL;
256 }
257
258 void count_whowas_memory(int *wwu, size_t *wwum, int *wwa, size_t *wwam)
259 {
260   struct Whowas *tmp;
261   int i;
262   int u = 0;
263   int a = 0;
264   size_t um = 0;
265   size_t am = 0;
266   assert(0 != wwu);
267   assert(0 != wwum);
268   assert(0 != wwa);
269   assert(0 != wwam);
270
271   for (i = 0, tmp = whowas; i < NICKNAMEHISTORYLENGTH; i++, tmp++) {
272     if (tmp->hashv != WHOWAS_UNUSED) {
273       u++;
274       um += (strlen(tmp->name) + 1);
275       um += (strlen(tmp->username) + 1);
276       um += (strlen(tmp->hostname) + 1);
277       um += (strlen(tmp->servername) + 1);
278       if (tmp->away) {
279         a++;
280         am += (strlen(tmp->away) + 1);
281       }
282     }
283   }
284   *wwu = u;
285   *wwum = um;
286   *wwa = a;
287   *wwam = am;
288 }
289
290
291 void initwhowas(void)
292 {
293   int i;
294
295   for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
296     whowas[i].hashv = WHOWAS_UNUSED;
297 }
298
299 unsigned int hash_whowas_name(const char *name)
300 {
301   unsigned int hash = 0;
302   unsigned int hash2 = 0;
303   unsigned char lower;
304
305   do
306   {
307     lower = ToLower(*name);
308     hash = (hash << 1) + lower;
309     hash2 = (hash2 >> 1) + lower;
310   }
311   while (*++name);
312
313   return ((hash & WW_MAX_INITIAL_MASK) << BITS_PER_COL) +
314       (hash2 & BITS_PER_COL_MASK);
315 }
316