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