2 * ircd-ratbox: A slightly useful ircd.
3 * whowas.c: WHOWAS user cache.
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 William Pitcock <nenolod@dereferenced.org>
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.
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.
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
41 #include "rb_radixtree.h"
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
);
55 whowas_free_wtop(struct whowas_top
*wtop
)
57 if(rb_dlink_list_length(&wtop
->wwlist
) == 0)
59 rb_radixtree_delete(whowas_tree
, wtop
->name
);
65 static struct whowas_top
*
66 whowas_get_top(const char *name
)
68 struct whowas_top
*wtop
;
70 wtop
= rb_radixtree_retrieve(whowas_tree
, name
);
74 wtop
= rb_malloc(sizeof(struct whowas_top
));
75 wtop
->name
= rb_strdup(name
);
76 rb_radixtree_add(whowas_tree
, wtop
->name
, wtop
);
82 whowas_get_list(const char *name
)
84 struct whowas_top
*wtop
;
85 wtop
= rb_radixtree_retrieve(whowas_tree
, name
);
92 whowas_add_history(struct Client
*client_p
, int online
)
94 struct whowas_top
*wtop
;
96 s_assert(NULL
!= client_p
);
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)
105 wtop
= whowas_get_top(client_p
->name
);
106 who
= rb_malloc(sizeof(struct Whowas
));
108 who
->logoff
= rb_current_time();
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
));
116 who
->flags
= (IsIPSpoof(client_p
) ? WHOWAS_IP_SPOOFING
: 0) |
117 (IsDynSpoof(client_p
) ? WHOWAS_DYNSPOOF
: 0);
119 /* this is safe do to with the servername cache */
120 who
->servername
= scache_get_name(client_p
->servptr
->serv
->nameinfo
);
124 who
->online
= client_p
;
125 rb_dlinkAdd(who
, &who
->cnode
, &client_p
->whowas_clist
);
130 rb_dlinkAdd(who
, &who
->wnode
, &wtop
->wwlist
);
131 rb_dlinkAdd(who
, &who
->whowas_node
, &whowas_list
);
136 whowas_off_history(struct Client
*client_p
)
138 rb_dlink_node
*ptr
, *next
;
140 RB_DLINK_FOREACH_SAFE(ptr
, next
, client_p
->whowas_clist
.head
)
142 struct Whowas
*who
= ptr
->data
;
144 rb_dlinkDelete(&who
->cnode
, &client_p
->whowas_clist
);
149 whowas_get_history(const char *nick
, time_t timelimit
)
151 struct whowas_top
*wtop
;
154 wtop
= rb_radixtree_retrieve(whowas_tree
, nick
);
158 timelimit
= rb_current_time() - timelimit
;
160 RB_DLINK_FOREACH_PREV(ptr
, wtop
->wwlist
.tail
)
162 struct Whowas
*who
= ptr
->data
;
163 if(who
->logoff
>= timelimit
)
173 whowas_trim(void *unused
)
177 if(rb_dlink_list_length(&whowas_list
) < whowas_list_length
)
179 over
= rb_dlink_list_length(&whowas_list
) - whowas_list_length
;
181 /* remove whowas entries over the configured length */
182 for(long i
= 0; i
< over
; i
++)
184 if(whowas_list
.tail
!= NULL
&& whowas_list
.tail
->data
!= NULL
)
186 struct Whowas
*twho
= whowas_list
.tail
->data
;
187 if(twho
->online
!= NULL
)
188 rb_dlinkDelete(&twho
->cnode
, &twho
->online
->whowas_clist
);
189 rb_dlinkDelete(&twho
->wnode
, &twho
->wtop
->wwlist
);
190 rb_dlinkDelete(&twho
->whowas_node
, &whowas_list
);
191 whowas_free_wtop(twho
->wtop
);
200 whowas_tree
= rb_radixtree_create("whowas", irccasecanon
);
201 if(whowas_list_length
== 0)
203 whowas_list_length
= NICKNAMEHISTORYLENGTH
;
205 rb_event_add("whowas_trim", whowas_trim
, NULL
, 10);
209 whowas_set_size(int len
)
211 whowas_list_length
= len
;
216 whowas_memory_usage(size_t * count
, size_t * memused
)
218 *count
= rb_dlink_list_length(&whowas_list
);
219 *memused
+= *count
* sizeof(struct Whowas
);
220 *memused
+= sizeof(struct whowas_top
) * rb_radixtree_size(whowas_tree
);