Author: Kev <klmitch@mit.edu>
[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_features.h"
43 #include "ircd_string.h"
44 #include "list.h"
45 #include "numeric.h"
46 #include "s_debug.h"
47 #include "s_misc.h"
48 #include "s_user.h"
49 #include "send.h"
50 #include "struct.h"
51 #include "support.h"
52 #include "sys.h"
53 #include "msg.h"
54
55 #include <assert.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59
60 static struct {
61   struct Whowas *ww_list;       /* list of whowas structures */
62   struct Whowas *ww_tail;       /* tail of list for getting structures */
63   unsigned int   ww_alloc;      /* alloc count */
64 } wwList = { 0, 0, 0 };
65
66 struct Whowas* whowashash[WW_MAX];
67
68 /*
69  * Since the introduction of numeric nicks (at least for upstream messages,
70  * like MODE +o <nick>, KICK #chan <nick>, KILL <nick> etc), there is no
71  * real important reason for a nick history anymore.
72  * Nevertheless, there are two reason why we might want to keep it:
73  * 1) The /WHOWAS command, which is often usefull to catch harrashing
74  *    users or abusers in general.
75  * 2) Clients still use the normal nicks in the client-server protocol,
76  *    and it might be considered a nice feature that here we still have
77  *    nick chasing.
78  * Note however that BOTH reasons make it redundant to keep a whowas history
79  * for users that split off.
80  *
81  * The rewrite of comstud was many to safe cpu during net.breaks and therefore
82  * a bit redundant imho (Run).
83  *
84  * But - it was written anyway.  So lets look at the structure of the
85  * whowas history now:
86  *
87  * We still have a static table of 'struct Whowas' structures in which we add
88  * new nicks (plus info) as in a rotating buffer.  We keep a global pointer
89  * `whowas_next' that points to the next entry to be overwritten - or to
90  * the oldest entry in the table (which is the same).
91  *
92  * Each entry keeps pointers for two doubly linked lists (thus four pointers):
93  * A list of the entries that have the same hash value ('hashv list'), and
94  * a list of the entries that have the same online pointer (`online list').
95  * Note that the last list (pointers) is only updated as long as online points
96  * to the corresponding client: As soon as the client signs off, this list
97  * is not anymore maintained (and hopefully not used anymore either ;).
98  *
99  * So now we have two ways of accessing this database:
100  * 1) Given a <nick> we can calculate a hashv and then whowashash[hashv] will
101  *    point to the start of the 'hash list': all entries with the same hashv.
102  *    We'll have to search this list to find the entry with the correct <nick>.
103  *    Once we found the correct whowas entry, we have a pointer to the
104  *    corresponding client - if still online - for nich chasing purposes.
105  *    Note that the same nick can occur multiple times in the whowas history,
106  *    each of these having the same hash value of course.  While a /WHOWAS on
107  *    just a nick will return all entries, nick chasing will only find the
108  *    first in the list.  Because new entries are added at the start of the
109  *    'hash list' we will always find the youngest entry, which is what we want.
110  * 2) Given an online client we have a pointer to the first whowas entry
111  *    of the linked list of whowas entries that all belong to this client.
112  *    We ONLY need this to reset all `online' pointers when this client
113  *    signs off.
114  *
115  * 27/8/79:
116  *
117  * Note that following:
118  *
119  * a) We *only* (need to) change the 'hash list' and the 'online' list
120  *    in add_history().
121  * b) There we always ADD an entry to the BEGINNING of the 'hash list'
122  *    and the 'online list': *new* entries are at the start of the lists.
123  *    The oldest entries are at the end of the lists.
124  * c) We always REMOVE the oldest entry we have (whowas_next), this means
125  *    that this is always an entry that is at the *end* of the 'hash list'
126  *    and 'online list' that it is a part of: the next pointer will
127  *    always be NULL.
128  * d) The previous pointer is *only* used to update the next pointer of the
129  *    previous entry, therefore we could better use a pointer to this
130  *    next pointer: That is faster - saves us a 'if' test (it will never be
131  *    NULL because the last added entry will point to the pointer that
132  *    points to the start of the list) and we won't need special code to
133  *    update the list start pointers.
134  *
135  * I incorporated these considerations into the code below.
136  *
137  * --Run
138  */
139
140 /* whowas_clean()
141  *
142  * Clean up a whowas structure
143  */
144 static struct Whowas *
145 whowas_clean(struct Whowas *ww)
146 {
147   if (!ww)
148     return 0;
149
150   Debug((DEBUG_LIST, "Cleaning whowas structure for %s", ww->name));
151
152   if (ww->online) { /* unlink from client */
153     if (ww->cnext) /* shouldn't happen, but I'm not confident of that */
154       ww->cnext->cprevnextp = ww->cprevnextp;
155     *ww->cprevnextp = ww->cnext;
156   }
157
158   if (ww->hnext) /* now unlink from hash table */
159     ww->hnext->hprevnextp = ww->hprevnextp;
160   *ww->hprevnextp = ww->hnext;
161
162   if (ww->wnext) /* unlink from whowas linked list... */
163     ww->wnext->wprev = ww->wprev;
164   if (ww->wprev)
165     ww->wprev->wnext = ww->wnext;
166
167   if (wwList.ww_tail == ww) /* update tail pointer appropriately */
168     wwList.ww_tail = ww->wprev;
169
170   /* Free old info */
171   if (ww->name)
172     MyFree(ww->name);
173   if (ww->username)
174     MyFree(ww->username);
175   if (ww->hostname)
176     MyFree(ww->hostname);
177   if (ww->servername)
178     MyFree(ww->servername);
179   if (ww->realname)
180     MyFree(ww->realname);
181   if (ww->away)
182     MyFree(ww->away);
183
184   return ww;
185 }
186
187 /* whowas_free()
188  *
189  * Free a struct Whowas...
190  */
191 static void
192 whowas_free(struct Whowas *ww)
193 {
194   if (!ww)
195     return;
196
197   Debug((DEBUG_LIST, "Destroying whowas structure for %s", ww->name));
198
199   whowas_clean(ww);
200   MyFree(ww);
201
202   wwList.ww_alloc--;
203 }
204
205 /* whowas_init()
206  *
207  * Initializes a given whowas structure
208  */
209 static struct Whowas *
210 whowas_init(struct Whowas *ww)
211 {
212   if (!ww)
213     return 0;
214
215   ww->hashv = 0;
216   ww->name = 0;
217   ww->username = 0;
218   ww->hostname = 0;
219   ww->servername = 0;
220   ww->realname = 0;
221   ww->away = 0;
222   ww->logoff = 0;
223   ww->online = 0;
224   ww->hnext = 0;
225   ww->hprevnextp = 0;
226   ww->cnext = 0;
227   ww->cprevnextp = 0;
228   ww->wnext = 0;
229   ww->wprev = 0;
230
231   return ww;
232 }
233
234 /* whowas_alloc()
235  *
236  * Returns a whowas structure to use
237  */
238 static struct Whowas *
239 whowas_alloc(void)
240 {
241   if (wwList.ww_alloc >= feature_int(FEAT_NICKNAMEHISTORYLENGTH))
242     return whowas_init(whowas_clean(wwList.ww_tail));
243
244   wwList.ww_alloc++; /* going to allocate a new one... */
245   return whowas_init((struct Whowas *) MyMalloc(sizeof(struct Whowas)));
246 }
247
248 /* whowas_realloc()
249  *
250  * Prune whowas list
251  */
252 void
253 whowas_realloc(void)
254 {
255   Debug((DEBUG_LIST, "whowas_realloc() called with alloc count %d, "
256          "history length %d, tail pointer %p", wwList.ww_alloc,
257          feature_int(FEAT_NICKNAMEHISTORYLENGTH), wwList.ww_tail));
258
259   while (wwList.ww_alloc > feature_int(FEAT_NICKNAMEHISTORYLENGTH)) {
260     if (!wwList.ww_tail) { /* list is empty... */
261       Debug((DEBUG_LIST, "whowas list emptied with alloc count %d",
262              wwList.ww_alloc));
263       return;
264     }
265
266     whowas_free(wwList.ww_tail); /* free oldest element of whowas list */
267   }
268 }
269
270 /*
271  * add_history
272  *
273  * Add a client (cptr) that just changed nick (still_on == true), or
274  * just signed off (still_on == false) to the `whowas' table.
275  *
276  * If the entry used was already in use, then this entry is
277  * freed (lost).
278  */
279 void add_history(struct Client *cptr, int still_on)
280 {
281   struct Whowas *ww;
282
283   if (!(ww = whowas_alloc()))
284     return; /* couldn't get a structure */
285
286   ww->hashv = hash_whowas_name(cli_name(cptr)); /* initialize struct */
287   ww->logoff = CurrentTime;
288   DupString(ww->name, cli_name(cptr));
289   DupString(ww->username, cli_user(cptr)->username);
290   DupString(ww->hostname, cli_user(cptr)->host);
291   DupString(ww->servername, cli_name(cli_user(cptr)->server));
292   DupString(ww->realname, cli_info(cptr));
293   if (cli_user(cptr)->away)
294     DupString(ww->away, cli_user(cptr)->away);
295
296   if (still_on) { /* user changed nicknames... */
297     ww->online = cptr;
298     if ((ww->cnext = cli_whowas(cptr)))
299       ww->cnext->cprevnextp = &ww->cnext;
300     ww->cprevnextp = &(cli_whowas(cptr));
301     cli_whowas(cptr) = ww;
302   } else /* user quit */
303     ww->online = 0;
304
305   /* link new whowas structure to list */
306   ww->wnext = wwList.ww_list;
307   if (wwList.ww_list)
308     wwList.ww_list->wprev = ww;
309   wwList.ww_list = ww;
310
311   if (!wwList.ww_tail) /* update the tail pointer... */
312     wwList.ww_tail = ww;
313
314   /* Now link it into the hash table */
315   if ((ww->hnext = whowashash[ww->hashv]))
316     ww->hnext->hprevnextp = &ww->hnext;
317   ww->hprevnextp = &whowashash[ww->hashv];
318   whowashash[ww->hashv] = ww;
319 }
320
321 /*
322  * off_history
323  *
324  * Client `cptr' signed off: Set all `online' pointers
325  * corresponding to this client to NULL.
326  */
327 void off_history(const struct Client *cptr)
328 {
329   struct Whowas *temp;
330
331   for (temp = cli_whowas(cptr); temp; temp = temp->cnext)
332     temp->online = NULL;
333 }
334
335 /*
336  * get_history
337  *
338  * Return a pointer to a client that had nick `nick' not more then
339  * `timelimit' seconds ago, if still on line.  Otherwise return NULL.
340  *
341  * This function is used for "nick chasing"; since the use of numeric
342  * nicks for "upstream" messages in ircu2.10, this is only used for
343  * looking up non-existing nicks in client->server messages.
344  */
345 struct Client *get_history(const char *nick, time_t timelimit)
346 {
347   struct Whowas *temp = whowashash[hash_whowas_name(nick)];
348   timelimit = CurrentTime - timelimit;
349
350   for (; temp; temp = temp->hnext)
351     if (0 == ircd_strcmp(nick, temp->name) && temp->logoff > timelimit)
352       return temp->online;
353
354   return NULL;
355 }
356
357 void count_whowas_memory(int *wwu, size_t *wwum, int *wwa, size_t *wwam)
358 {
359   struct Whowas *tmp;
360   int u = 0;
361   int a = 0;
362   size_t um = 0;
363   size_t am = 0;
364   assert(0 != wwu);
365   assert(0 != wwum);
366   assert(0 != wwa);
367   assert(0 != wwam);
368
369   for (tmp = wwList.ww_list; tmp; tmp = tmp->wnext) {
370     u++;
371     um += (strlen(tmp->name) + 1);
372     um += (strlen(tmp->username) + 1);
373     um += (strlen(tmp->hostname) + 1);
374     um += (strlen(tmp->servername) + 1);
375     if (tmp->away) {
376       a++;
377       am += (strlen(tmp->away) + 1);
378     }
379   }
380   *wwu = u;
381   *wwum = um;
382   *wwa = a;
383   *wwam = am;
384 }
385
386
387 void initwhowas(void)
388 {
389   int i;
390
391   for (i = 0; i < WW_MAX; i++)
392     whowashash[i] = 0;
393 }
394
395 unsigned int hash_whowas_name(const char *name)
396 {
397   unsigned int hash = 0;
398   unsigned int hash2 = 0;
399   unsigned char lower;
400
401   do
402   {
403     lower = ToLower(*name);
404     hash = (hash << 1) + lower;
405     hash2 = (hash2 >> 1) + lower;
406   }
407   while (*++name);
408
409   return ((hash & WW_MAX_INITIAL_MASK) << BITS_PER_COL) +
410       (hash2 & BITS_PER_COL_MASK);
411 }
412