1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2016 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file whowas.c |
23 |
* \brief WHOWAS user cache. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "whowas.h" |
30 |
#include "client.h" |
31 |
#include "hash.h" |
32 |
#include "irc_string.h" |
33 |
#include "ircd.h" |
34 |
|
35 |
|
36 |
static struct Whowas WHOWAS[NICKNAMEHISTORYLENGTH]; |
37 |
dlink_list WHOWASHASH[HASHSIZE]; |
38 |
|
39 |
|
40 |
/*! \brief Initializes whowas table |
41 |
*/ |
42 |
void |
43 |
whowas_init(void) |
44 |
{ |
45 |
for (unsigned int i = 0; i < NICKNAMEHISTORYLENGTH; ++i) |
46 |
WHOWAS[i].hashv = -1; |
47 |
} |
48 |
|
49 |
/*! \brief Adds the currently defined name of the client to history. |
50 |
* Usually called before changing to a new name (nick). |
51 |
* Client must be a fully registered user. |
52 |
* \param client_p pointer to Client struct to add |
53 |
* \param online either 1 if it's a nick change or 0 on client exit |
54 |
*/ |
55 |
void |
56 |
whowas_add_history(struct Client *client_p, const int online) |
57 |
{ |
58 |
static unsigned int whowas_next = 0; |
59 |
struct Whowas *const whowas = &WHOWAS[whowas_next]; |
60 |
|
61 |
assert(IsClient(client_p)); |
62 |
|
63 |
if (++whowas_next == NICKNAMEHISTORYLENGTH) |
64 |
whowas_next = 0; |
65 |
|
66 |
if (whowas->hashv != -1) |
67 |
{ |
68 |
if (whowas->online) |
69 |
dlinkDelete(&whowas->cnode, &whowas->online->whowas); |
70 |
|
71 |
dlinkDelete(&whowas->tnode, &WHOWASHASH[whowas->hashv]); |
72 |
} |
73 |
|
74 |
whowas->hashv = strhash(client_p->name); |
75 |
whowas->shide = IsHidden(client_p->servptr) != 0; |
76 |
whowas->logoff = CurrentTime; |
77 |
|
78 |
strlcpy(whowas->account, client_p->account, sizeof(whowas->account)); |
79 |
strlcpy(whowas->name, client_p->name, sizeof(whowas->name)); |
80 |
strlcpy(whowas->username, client_p->username, sizeof(whowas->username)); |
81 |
strlcpy(whowas->hostname, client_p->host, sizeof(whowas->hostname)); |
82 |
strlcpy(whowas->sockhost, client_p->sockhost, sizeof(whowas->sockhost)); |
83 |
strlcpy(whowas->realname, client_p->info, sizeof(whowas->realname)); |
84 |
strlcpy(whowas->servername, client_p->servptr->name, sizeof(whowas->servername)); |
85 |
|
86 |
if (online) |
87 |
{ |
88 |
whowas->online = client_p; |
89 |
dlinkAdd(whowas, &whowas->cnode, &client_p->whowas); |
90 |
} |
91 |
else |
92 |
whowas->online = NULL; |
93 |
|
94 |
dlinkAdd(whowas, &whowas->tnode, &WHOWASHASH[whowas->hashv]); |
95 |
} |
96 |
|
97 |
/*! \brief This must be called when the client structure is about to |
98 |
* be released. History mechanism keeps pointers to client |
99 |
* structures and it must know when they cease to exist. |
100 |
* \param client_p pointer to Client struct |
101 |
*/ |
102 |
void |
103 |
whowas_off_history(struct Client *client_p) |
104 |
{ |
105 |
while (client_p->whowas.head) |
106 |
{ |
107 |
struct Whowas *whowas = client_p->whowas.head->data; |
108 |
|
109 |
whowas->online = NULL; |
110 |
dlinkDelete(&whowas->cnode, &client_p->whowas); |
111 |
} |
112 |
} |
113 |
|
114 |
/*! \brief Returns the current client that was using the given |
115 |
* nickname within the timelimit. Returns NULL, if no |
116 |
* one found... |
117 |
* \param nick name of the nick |
118 |
* \param timelimit maximum age for a client since log-off |
119 |
*/ |
120 |
struct Client * |
121 |
whowas_get_history(const char *nick, uintmax_t timelimit) |
122 |
{ |
123 |
dlink_node *node = NULL; |
124 |
|
125 |
timelimit = CurrentTime - timelimit; |
126 |
|
127 |
DLINK_FOREACH(node, WHOWASHASH[strhash(nick)].head) |
128 |
{ |
129 |
struct Whowas *whowas = node->data; |
130 |
|
131 |
if (whowas->logoff < timelimit) |
132 |
continue; |
133 |
if (irccmp(nick, whowas->name)) |
134 |
continue; |
135 |
return whowas->online; |
136 |
} |
137 |
|
138 |
return NULL; |
139 |
} |
140 |
|
141 |
/*! \brief For debugging. Counts related structures stored in whowas array |
142 |
*/ |
143 |
void |
144 |
whowas_count_memory(unsigned int *const count, size_t *const bytes) |
145 |
{ |
146 |
const struct Whowas *whowas = &WHOWAS[0]; |
147 |
|
148 |
for (unsigned int i = 0; i < NICKNAMEHISTORYLENGTH; ++i, ++whowas) |
149 |
{ |
150 |
if (whowas->hashv != -1) |
151 |
{ |
152 |
(*count)++; |
153 |
(*bytes) += sizeof(struct Whowas); |
154 |
} |
155 |
} |
156 |
} |