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