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