1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997 Jukka Santala (Donwulff) |
5 |
* Copyright (c) 2005-2019 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 "memory.h" |
31 |
#include "client.h" |
32 |
#include "hash.h" |
33 |
#include "irc_string.h" |
34 |
#include "ircd.h" |
35 |
#include "numeric.h" |
36 |
#include "send.h" |
37 |
#include "watch.h" |
38 |
|
39 |
|
40 |
static dlink_list watchTable[HASHSIZE]; |
41 |
|
42 |
|
43 |
/* |
44 |
* Rough figure of the datastructures for watch: |
45 |
* |
46 |
* NOTIFY HASH client_p1 |
47 |
* | |- nick1 |
48 |
* nick1-|- client_p1 |- nick2 |
49 |
* | |- client_p2 client_p3 |
50 |
* | |- client_p3 client_p2 |- nick1 |
51 |
* | |- nick1 |
52 |
* nick2-|- client_p2 |- nick2 |
53 |
* |- client_p1 |
54 |
*/ |
55 |
|
56 |
/*! \brief Counts up memory used by watch list headers |
57 |
*/ |
58 |
void |
59 |
watch_count_memory(unsigned int *const count, size_t *const bytes) |
60 |
{ |
61 |
for (unsigned int i = 0; i < HASHSIZE; ++i) |
62 |
(*count) += dlink_list_length(&watchTable[i]); |
63 |
|
64 |
(*bytes) = *count * sizeof(struct Watch); |
65 |
} |
66 |
|
67 |
/*! \brief Notifies all clients that have client_p's name on |
68 |
* their watch list. |
69 |
* \param client_p Pointer to Client struct |
70 |
* \param reply Numeric to send. Either RPL_LOGON or RPL_LOGOFF |
71 |
*/ |
72 |
void |
73 |
watch_check_hash(const struct Client *client_p, const enum irc_numerics reply) |
74 |
{ |
75 |
struct Watch *watch = NULL; |
76 |
dlink_node *node = NULL; |
77 |
|
78 |
assert(IsClient(client_p)); |
79 |
|
80 |
if ((watch = watch_find_hash(client_p->name)) == NULL) |
81 |
return; /* This name isn't on watch */ |
82 |
|
83 |
/* Update the time of last change to item */ |
84 |
watch->lasttime = event_base->time.sec_real; |
85 |
|
86 |
/* Send notifies out to everybody on the list in header */ |
87 |
DLINK_FOREACH(node, watch->watched_by.head) |
88 |
sendto_one_numeric(node->data, &me, reply, client_p->name, |
89 |
client_p->username, client_p->host, |
90 |
watch->lasttime, client_p->info); |
91 |
} |
92 |
|
93 |
/*! \brief Looks up the watch table for a given name |
94 |
* \param name Nick name to look up |
95 |
*/ |
96 |
struct Watch * |
97 |
watch_find_hash(const char *name) |
98 |
{ |
99 |
dlink_node *node = NULL; |
100 |
|
101 |
DLINK_FOREACH(node, watchTable[strhash(name)].head) |
102 |
{ |
103 |
struct Watch *watch = node->data; |
104 |
|
105 |
if (irccmp(watch->name, name) == 0) |
106 |
return watch; |
107 |
} |
108 |
|
109 |
return NULL; |
110 |
} |
111 |
|
112 |
/*! \brief Adds a watch entry to client_p's watch list |
113 |
* \param name Nick name to add |
114 |
* \param client_p Pointer to Client struct |
115 |
*/ |
116 |
void |
117 |
watch_add_to_hash_table(const char *name, struct Client *client_p) |
118 |
{ |
119 |
struct Watch *watch = NULL; |
120 |
dlink_node *node = NULL; |
121 |
|
122 |
/* If found NULL (no header for this name), make one... */ |
123 |
if ((watch = watch_find_hash(name)) == NULL) |
124 |
{ |
125 |
watch = xcalloc(sizeof(*watch)); |
126 |
|
127 |
strlcpy(watch->name, name, sizeof(watch->name)); |
128 |
watch->hash_value = strhash(watch->name); |
129 |
watch->lasttime = event_base->time.sec_real; |
130 |
|
131 |
dlinkAdd(watch, &watch->node, &watchTable[watch->hash_value]); |
132 |
} |
133 |
else |
134 |
{ |
135 |
/* Is this client already on the watch-list? */ |
136 |
node = dlinkFind(&watch->watched_by, client_p); |
137 |
} |
138 |
|
139 |
if (node == NULL) |
140 |
{ |
141 |
/* No it isn't, so add it in the bucket and client adding it */ |
142 |
dlinkAdd(client_p, make_dlink_node(), &watch->watched_by); |
143 |
dlinkAdd(watch, make_dlink_node(), &client_p->connection->watches); |
144 |
} |
145 |
} |
146 |
|
147 |
/*! \brief Removes a single entry from client_p's watch list |
148 |
* \param name Name to remove |
149 |
* \param client_p Pointer to Client struct |
150 |
*/ |
151 |
void |
152 |
watch_del_from_hash_table(const char *name, struct Client *client_p) |
153 |
{ |
154 |
struct Watch *watch = NULL; |
155 |
dlink_node *node = NULL; |
156 |
|
157 |
if ((watch = watch_find_hash(name)) == NULL) |
158 |
return; /* No header found for that name. i.e. it's not being watched */ |
159 |
|
160 |
if ((node = dlinkFind(&watch->watched_by, client_p)) == NULL) |
161 |
return; /* This name isn't being watched by client_p */ |
162 |
|
163 |
dlinkDelete(node, &watch->watched_by); |
164 |
free_dlink_node(node); |
165 |
|
166 |
if ((node = dlinkFindDelete(&client_p->connection->watches, watch))) |
167 |
free_dlink_node(node); |
168 |
|
169 |
/* In case this header is now empty of notices, remove it */ |
170 |
if (watch->watched_by.head == NULL) |
171 |
{ |
172 |
assert(dlinkFind(&watchTable[watch->hash_value], watch)); |
173 |
dlinkDelete(&watch->node, &watchTable[watch->hash_value]); |
174 |
xfree(watch); |
175 |
} |
176 |
} |
177 |
|
178 |
/*! \brief Removes all entries from client_p's watch list |
179 |
* and deletes headers that are no longer being watched. |
180 |
* \param client_p Pointer to Client struct |
181 |
*/ |
182 |
void |
183 |
watch_del_watch_list(struct Client *client_p) |
184 |
{ |
185 |
dlink_node *node = NULL, *node_next = NULL; |
186 |
dlink_node *temp = NULL; |
187 |
|
188 |
DLINK_FOREACH_SAFE(node, node_next, client_p->connection->watches.head) |
189 |
{ |
190 |
struct Watch *watch = node->data; |
191 |
|
192 |
assert(dlinkFind(&watch->watched_by, client_p)); |
193 |
|
194 |
if ((temp = dlinkFindDelete(&watch->watched_by, client_p))) |
195 |
free_dlink_node(temp); |
196 |
|
197 |
/* If this leaves a header without notifies, remove it. */ |
198 |
if (watch->watched_by.head == NULL) |
199 |
{ |
200 |
assert(dlinkFind(&watchTable[watch->hash_value], watch)); |
201 |
dlinkDelete(&watch->node, &watchTable[watch->hash_value]); |
202 |
|
203 |
xfree(watch); |
204 |
} |
205 |
|
206 |
dlinkDelete(node, &client_p->connection->watches); |
207 |
free_dlink_node(node); |
208 |
} |
209 |
|
210 |
assert(client_p->connection->watches.head == NULL); |
211 |
assert(client_p->connection->watches.tail == NULL); |
212 |
} |