1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997 Jukka Santala (Donwulff) |
5 |
* Copyright (c) 2005-2018 ircd-hybrid development team |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
20 |
* USA |
21 |
*/ |
22 |
|
23 |
/*! \file watch.c |
24 |
* \brief File including functions for WATCH support |
25 |
* \version $Id$ |
26 |
*/ |
27 |
|
28 |
#include "stdinc.h" |
29 |
#include "list.h" |
30 |
#include "client.h" |
31 |
#include "hash.h" |
32 |
#include "irc_string.h" |
33 |
#include "ircd.h" |
34 |
#include "numeric.h" |
35 |
#include "send.h" |
36 |
#include "mempool.h" |
37 |
#include "watch.h" |
38 |
|
39 |
|
40 |
static mp_pool_t *watch_pool; |
41 |
static dlink_list watchTable[HASHSIZE]; |
42 |
|
43 |
/*! \brief Initializes the watch table |
44 |
*/ |
45 |
void |
46 |
watch_init(void) |
47 |
{ |
48 |
watch_pool = mp_pool_new(sizeof(struct Watch), MP_CHUNK_SIZE_WATCH); |
49 |
} |
50 |
|
51 |
/* |
52 |
* Rough figure of the datastructures for watch: |
53 |
* |
54 |
* NOTIFY HASH client_p1 |
55 |
* | |- nick1 |
56 |
* nick1-|- client_p1 |- nick2 |
57 |
* | |- client_p2 client_p3 |
58 |
* | |- client_p3 client_p2 |- nick1 |
59 |
* | |- nick1 |
60 |
* nick2-|- client_p2 |- nick2 |
61 |
* |- client_p1 |
62 |
*/ |
63 |
|
64 |
/*! \brief Counts up memory used by watch list headers |
65 |
*/ |
66 |
void |
67 |
watch_count_memory(unsigned int *const count, size_t *const bytes) |
68 |
{ |
69 |
for (unsigned int i = 0; i < HASHSIZE; ++i) |
70 |
(*count) += dlink_list_length(&watchTable[i]); |
71 |
|
72 |
(*bytes) = *count * sizeof(struct Watch); |
73 |
} |
74 |
|
75 |
/*! \brief Notifies all clients that have client_p's name on |
76 |
* their watch list. |
77 |
* \param client_p Pointer to Client struct |
78 |
* \param reply Numeric to send. Either RPL_LOGON or RPL_LOGOFF |
79 |
*/ |
80 |
void |
81 |
watch_check_hash(const struct Client *client_p, const enum irc_numerics reply) |
82 |
{ |
83 |
struct Watch *watch = NULL; |
84 |
dlink_node *node = NULL; |
85 |
|
86 |
assert(IsClient(client_p)); |
87 |
|
88 |
if ((watch = watch_find_hash(client_p->name)) == NULL) |
89 |
return; /* This name isn't on watch */ |
90 |
|
91 |
/* Update the time of last change to item */ |
92 |
watch->lasttime = CurrentTime; |
93 |
|
94 |
/* Send notifies out to everybody on the list in header */ |
95 |
DLINK_FOREACH(node, watch->watched_by.head) |
96 |
sendto_one_numeric(node->data, &me, reply, client_p->name, |
97 |
client_p->username, client_p->host, |
98 |
watch->lasttime, client_p->info); |
99 |
} |
100 |
|
101 |
/*! \brief Looks up the watch table for a given name |
102 |
* \param name Nick name to look up |
103 |
*/ |
104 |
struct Watch * |
105 |
watch_find_hash(const char *name) |
106 |
{ |
107 |
dlink_node *node = NULL; |
108 |
|
109 |
DLINK_FOREACH(node, watchTable[strhash(name)].head) |
110 |
{ |
111 |
struct Watch *watch = node->data; |
112 |
|
113 |
if (!irccmp(watch->name, name)) |
114 |
return watch; |
115 |
} |
116 |
|
117 |
return NULL; |
118 |
} |
119 |
|
120 |
/*! \brief Adds a watch entry to client_p's watch list |
121 |
* \param name Nick name to add |
122 |
* \param client_p Pointer to Client struct |
123 |
*/ |
124 |
void |
125 |
watch_add_to_hash_table(const char *name, struct Client *client_p) |
126 |
{ |
127 |
struct Watch *watch = NULL; |
128 |
dlink_node *node = NULL; |
129 |
|
130 |
/* If found NULL (no header for this name), make one... */ |
131 |
if ((watch = watch_find_hash(name)) == NULL) |
132 |
{ |
133 |
watch = mp_pool_get(watch_pool); |
134 |
|
135 |
watch->lasttime = CurrentTime; |
136 |
strlcpy(watch->name, name, sizeof(watch->name)); |
137 |
|
138 |
dlinkAdd(watch, &watch->node, &watchTable[strhash(watch->name)]); |
139 |
} |
140 |
else |
141 |
{ |
142 |
/* Is this client already on the watch-list? */ |
143 |
node = dlinkFind(&watch->watched_by, client_p); |
144 |
} |
145 |
|
146 |
if (node == NULL) |
147 |
{ |
148 |
/* No it isn't, so add it in the bucket and client adding it */ |
149 |
dlinkAdd(client_p, make_dlink_node(), &watch->watched_by); |
150 |
dlinkAdd(watch, make_dlink_node(), &client_p->connection->watches); |
151 |
} |
152 |
} |
153 |
|
154 |
/*! \brief Removes a single entry from client_p's watch list |
155 |
* \param name Name to remove |
156 |
* \param client_p Pointer to Client struct |
157 |
*/ |
158 |
void |
159 |
watch_del_from_hash_table(const char *name, struct Client *client_p) |
160 |
{ |
161 |
struct Watch *watch = NULL; |
162 |
dlink_node *node = NULL; |
163 |
|
164 |
if ((watch = watch_find_hash(name)) == NULL) |
165 |
return; /* No header found for that name. i.e. it's not being watched */ |
166 |
|
167 |
if ((node = dlinkFind(&watch->watched_by, client_p)) == NULL) |
168 |
return; /* This name isn't being watched by client_p */ |
169 |
|
170 |
dlinkDelete(node, &watch->watched_by); |
171 |
free_dlink_node(node); |
172 |
|
173 |
if ((node = dlinkFindDelete(&client_p->connection->watches, watch))) |
174 |
free_dlink_node(node); |
175 |
|
176 |
/* In case this header is now empty of notices, remove it */ |
177 |
if (watch->watched_by.head == NULL) |
178 |
{ |
179 |
assert(dlinkFind(&watchTable[strhash(watch->name)], watch)); |
180 |
dlinkDelete(&watch->node, &watchTable[strhash(watch->name)]); |
181 |
mp_pool_release(watch); |
182 |
} |
183 |
} |
184 |
|
185 |
/*! \brief Removes all entries from client_p's watch list |
186 |
* and deletes headers that are no longer being watched. |
187 |
* \param client_p Pointer to Client struct |
188 |
*/ |
189 |
void |
190 |
watch_del_watch_list(struct Client *client_p) |
191 |
{ |
192 |
dlink_node *node = NULL, *node_next = NULL; |
193 |
dlink_node *temp = NULL; |
194 |
|
195 |
DLINK_FOREACH_SAFE(node, node_next, client_p->connection->watches.head) |
196 |
{ |
197 |
struct Watch *watch = node->data; |
198 |
|
199 |
assert(dlinkFind(&watch->watched_by, client_p)); |
200 |
|
201 |
if ((temp = dlinkFindDelete(&watch->watched_by, client_p))) |
202 |
free_dlink_node(temp); |
203 |
|
204 |
/* If this leaves a header without notifies, remove it. */ |
205 |
if (watch->watched_by.head == NULL) |
206 |
{ |
207 |
assert(dlinkFind(&watchTable[strhash(watch->name)], watch)); |
208 |
dlinkDelete(&watch->node, &watchTable[strhash(watch->name)]); |
209 |
|
210 |
mp_pool_release(watch); |
211 |
} |
212 |
|
213 |
dlinkDelete(node, &client_p->connection->watches); |
214 |
free_dlink_node(node); |
215 |
} |
216 |
|
217 |
assert(client_p->connection->watches.head == NULL); |
218 |
assert(client_p->connection->watches.tail == NULL); |
219 |
} |