]> jfr.im git - solanum.git/blob - ircd/whowas.c
doc/reference.conf: document the auth::umodes configuration option
[solanum.git] / ircd / 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-2012 ircd-ratbox development team
8 * Copyright (C) 2016 Ariadne Conill <ariadne@dereferenced.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 * USA
24 */
25
26 #include "stdinc.h"
27 #include "hash.h"
28 #include "whowas.h"
29 #include "match.h"
30 #include "ircd.h"
31 #include "numeric.h"
32 #include "s_assert.h"
33 #include "s_serv.h"
34 #include "s_user.h"
35 #include "send.h"
36 #include "s_conf.h"
37 #include "client.h"
38 #include "send.h"
39 #include "logger.h"
40 #include "scache.h"
41 #include "rb_radixtree.h"
42
43 struct whowas_top
44 {
45 char *name;
46 rb_dlink_list wwlist;
47 };
48
49 static rb_radixtree *whowas_tree = NULL;
50 static rb_dlink_list whowas_list = {NULL, NULL, 0};
51 static unsigned int whowas_list_length = NICKNAMEHISTORYLENGTH;
52 static void whowas_trim(void *unused);
53
54 static void
55 whowas_free_wtop(struct whowas_top *wtop)
56 {
57 if(rb_dlink_list_length(&wtop->wwlist) == 0)
58 {
59 rb_radixtree_delete(whowas_tree, wtop->name);
60 rb_free(wtop->name);
61 rb_free(wtop);
62 }
63 }
64
65 static struct whowas_top *
66 whowas_get_top(const char *name)
67 {
68 struct whowas_top *wtop;
69
70 wtop = rb_radixtree_retrieve(whowas_tree, name);
71 if (wtop != NULL)
72 return wtop;
73
74 wtop = rb_malloc(sizeof(struct whowas_top));
75 wtop->name = rb_strdup(name);
76 rb_radixtree_add(whowas_tree, wtop->name, wtop);
77
78 return wtop;
79 }
80
81 rb_dlink_list *
82 whowas_get_list(const char *name)
83 {
84 struct whowas_top *wtop;
85 wtop = rb_radixtree_retrieve(whowas_tree, name);
86 if(wtop == NULL)
87 return NULL;
88 return &wtop->wwlist;
89 }
90
91 void
92 whowas_add_history(struct Client *client_p, int online)
93 {
94 struct whowas_top *wtop;
95 struct Whowas *who;
96 s_assert(NULL != client_p);
97
98 if(client_p == NULL)
99 return;
100
101 /* trim some of the entries if we're getting well over our history length */
102 if(rb_dlink_list_length(&whowas_list) > whowas_list_length + 100)
103 whowas_trim(NULL);
104
105 wtop = whowas_get_top(client_p->name);
106 who = rb_malloc(sizeof(struct Whowas));
107 who->wtop = wtop;
108 who->logoff = rb_current_time();
109
110 rb_strlcpy(who->name, client_p->name, sizeof(who->name));
111 rb_strlcpy(who->username, client_p->username, sizeof(who->username));
112 rb_strlcpy(who->hostname, client_p->host, sizeof(who->hostname));
113 rb_strlcpy(who->realname, client_p->info, sizeof(who->realname));
114 rb_strlcpy(who->sockhost, client_p->sockhost, sizeof(who->sockhost));
115 rb_strlcpy(who->suser, client_p->user->suser, sizeof(who->suser));
116
117 who->flags = (IsIPSpoof(client_p) ? WHOWAS_IP_SPOOFING : 0) |
118 (IsDynSpoof(client_p) ? WHOWAS_DYNSPOOF : 0);
119
120 /* this is safe do to with the servername cache */
121 who->servername = scache_get_name(client_p->servptr->serv->nameinfo);
122
123 if(online)
124 {
125 who->online = client_p;
126 rb_dlinkAdd(who, &who->cnode, &client_p->whowas_clist);
127 }
128 else
129 who->online = NULL;
130
131 rb_dlinkAdd(who, &who->wnode, &wtop->wwlist);
132 rb_dlinkAdd(who, &who->whowas_node, &whowas_list);
133 }
134
135
136 void
137 whowas_off_history(struct Client *client_p)
138 {
139 rb_dlink_node *ptr, *next;
140
141 RB_DLINK_FOREACH_SAFE(ptr, next, client_p->whowas_clist.head)
142 {
143 struct Whowas *who = ptr->data;
144 who->online = NULL;
145 rb_dlinkDelete(&who->cnode, &client_p->whowas_clist);
146 }
147 }
148
149 struct Client *
150 whowas_get_history(const char *nick, time_t timelimit)
151 {
152 struct whowas_top *wtop;
153 rb_dlink_node *ptr;
154
155 wtop = rb_radixtree_retrieve(whowas_tree, nick);
156 if(wtop == NULL)
157 return NULL;
158
159 timelimit = rb_current_time() - timelimit;
160
161 RB_DLINK_FOREACH_PREV(ptr, wtop->wwlist.tail)
162 {
163 struct Whowas *who = ptr->data;
164 if(who->logoff >= timelimit)
165 {
166 return who->online;
167 }
168 }
169
170 return NULL;
171 }
172
173 static void
174 whowas_trim(void *unused)
175 {
176 long over;
177
178 if(rb_dlink_list_length(&whowas_list) < whowas_list_length)
179 return;
180 over = rb_dlink_list_length(&whowas_list) - whowas_list_length;
181
182 /* remove whowas entries over the configured length */
183 for(long i = 0; i < over; i++)
184 {
185 if(whowas_list.tail != NULL && whowas_list.tail->data != NULL)
186 {
187 struct Whowas *twho = whowas_list.tail->data;
188 if(twho->online != NULL)
189 rb_dlinkDelete(&twho->cnode, &twho->online->whowas_clist);
190 rb_dlinkDelete(&twho->wnode, &twho->wtop->wwlist);
191 rb_dlinkDelete(&twho->whowas_node, &whowas_list);
192 whowas_free_wtop(twho->wtop);
193 rb_free(twho);
194 }
195 }
196 }
197
198 void
199 whowas_init(void)
200 {
201 whowas_tree = rb_radixtree_create("whowas", irccasecanon);
202 if(whowas_list_length == 0)
203 {
204 whowas_list_length = NICKNAMEHISTORYLENGTH;
205 }
206 rb_event_add("whowas_trim", whowas_trim, NULL, 10);
207 }
208
209 void
210 whowas_set_size(int len)
211 {
212 whowas_list_length = len;
213 whowas_trim(NULL);
214 }
215
216 void
217 whowas_memory_usage(size_t * count, size_t * memused)
218 {
219 *count = rb_dlink_list_length(&whowas_list);
220 *memused += *count * sizeof(struct Whowas);
221 *memused += sizeof(struct whowas_top) * rb_radixtree_size(whowas_tree);
222 }