ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/watch.c
Revision: 231
Committed: Fri Nov 4 17:33:00 2005 UTC (20 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 5767 byte(s)
Log Message:
- Cleaned up watchcode

File Contents

# User Rev Content
1 michael 214 /*
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 michael 215 #include "stdinc.h"
29     #include "client.h"
30     #include "common.h"
31     #include "hash.h"
32     #include "ircd.h"
33     #include "numeric.h"
34     #include "s_conf.h"
35     #include "s_user.h"
36     #include "send.h"
37     #include "supported.h"
38     #include "watch.h"
39     #include "msg.h"
40    
41 michael 214 #define WATCH_HEAP_SIZE 32
42    
43 michael 231 static dlink_list watchTable[HASHSIZE];
44 michael 214
45     static BlockHeap *watch_heap = NULL;
46    
47     void
48     init_watch(void)
49     {
50 michael 231 memset(watchTable, 0, sizeof(watchTable));
51 michael 214
52     watch_heap = BlockHeapCreate("watch", sizeof(struct Watch), WATCH_HEAP_SIZE);
53     }
54 michael 215
55     /*
56     * Rough figure of the datastructures for watch:
57     *
58     * NOTIFY HASH client_p1
59     * | |- nick1
60     * nick1-|- client_p1 |- nick2
61     * | |- client_p2 client_p3
62     * | |- client_p3 client_p2 |- nick1
63     * | |- nick1
64     * nick2-|- client_p2 |- nick2
65     * |- client_p1
66     */
67    
68     /*
69     * count_watch_memory()
70     */
71     void
72     count_watch_memory(unsigned int *count, unsigned int *memory)
73     {
74     unsigned int idx;
75    
76     for (idx = 0; idx < HASHSIZE; ++idx)
77 michael 231 *count += dlink_list_length(&watchTable[idx]);
78 michael 215
79 michael 231 *memory = *count * sizeof(struct Watch);
80 michael 215 }
81    
82     /*
83     * hash_check_watch()
84     */
85     void
86     hash_check_watch(struct Client *client_p, int reply)
87     {
88     struct Watch *anptr = NULL;
89     dlink_node *ptr = NULL;
90     assert(IsClient(client_p));
91     if ((anptr = hash_get_watch(client_p->name)) == NULL)
92     return; /* This nick isn't on watch */
93    
94     /* Update the time of last change to item */
95     anptr->lasttime = CurrentTime;
96    
97     /* Send notifies out to everybody on the list in header */
98     DLINK_FOREACH(ptr, anptr->watched_by.head)
99     {
100     struct Client *target_p = ptr->data;
101    
102     sendto_one(target_p, form_str(reply),
103     me.name, target_p->name, client_p->name,
104 michael 216 client_p->username, client_p->host,
105 michael 215 anptr->lasttime, client_p->info);
106     }
107     }
108    
109     /*
110     * hash_get_watch()
111     */
112     struct Watch *
113     hash_get_watch(const char *name)
114     {
115 michael 231 dlink_node *ptr = NULL;
116 michael 215
117 michael 231 DLINK_FOREACH(ptr, watchTable[strhash(name)].head) {
118     struct Watch *anptr = ptr->data;
119     if (!irccmp(anptr->nick, name))
120     return anptr;
121     }
122 michael 215
123 michael 231 return NULL;
124 michael 215 }
125    
126     /*
127     * add_to_watch_hash_table()
128     */
129     void
130     add_to_watch_hash_table(const char *nick, struct Client *client_p)
131     {
132     struct Watch *anptr = NULL;
133     dlink_node *ptr = NULL;
134    
135     /* If found NULL (no header for this nick), make one... */
136 michael 231 if ((anptr = hash_get_watch(nick)) == NULL)
137 michael 215 {
138     anptr = BlockHeapAlloc(watch_heap);
139     anptr->lasttime = CurrentTime;
140     strlcpy(anptr->nick, nick, sizeof(anptr->nick));
141    
142 michael 231 dlinkAdd(anptr, &anptr->node, &watchTable[strhash(nick)]);
143 michael 215 }
144     else
145     {
146     /* Is this client already on the watch-list? */
147     ptr = dlinkFind(&anptr->watched_by, client_p);
148     }
149    
150     if (ptr == NULL)
151     {
152     /* No it isn't, so add it in the bucket and client addint it */
153     dlinkAdd(client_p, make_dlink_node(), &anptr->watched_by);
154     dlinkAdd(anptr, make_dlink_node(), &client_p->localClient->watches);
155     }
156     }
157    
158     /*
159     * del_from_watch_hash_table()
160     */
161     void
162     del_from_watch_hash_table(const char *nick, struct Client *client_p)
163     {
164     struct Watch *anptr = NULL;
165     dlink_node *ptr = NULL;
166    
167 michael 231 if (!(anptr = hash_get_watch(nick)))
168     return; /* No header found for that nick. i.e. it's not being watched */
169 michael 215
170     if ((ptr = dlinkFind(&anptr->watched_by, client_p)) == NULL)
171     return;
172    
173     dlinkDelete(ptr, &anptr->watched_by);
174     free_dlink_node(ptr);
175    
176     if ((ptr = dlinkFindDelete(&client_p->localClient->watches, anptr)))
177     free_dlink_node(ptr);
178    
179     /* In case this header is now empty of notices, remove it */
180     if (anptr->watched_by.head == NULL)
181     {
182 michael 231 assert(dlinkFind(&watchTable[strhash(nick)], anptr) != NULL);
183     dlinkDelete(&anptr->node, &watchTable[strhash(nick)]);
184 michael 215 BlockHeapFree(watch_heap, anptr);
185     }
186     }
187    
188     /*
189     * hash_del_watch_list()
190     */
191     void
192     hash_del_watch_list(struct Client *client_p)
193     {
194     dlink_node *ptr = NULL, *ptr_next = NULL;
195     dlink_node *tmp = NULL;
196    
197     if (client_p->localClient->watches.head == NULL)
198     return; /* Nothing to do */
199    
200     DLINK_FOREACH_SAFE(ptr, ptr_next, client_p->localClient->watches.head)
201     {
202     struct Watch *anptr = ptr->data;
203    
204     assert(anptr);
205    
206 michael 231 assert(dlinkFind(&anptr->watched_by, client_p) != NULL);
207 michael 215 if ((tmp = dlinkFindDelete(&anptr->watched_by, client_p)))
208     free_dlink_node(tmp);
209    
210     /*
211     * If this leaves a header without notifies, remove it.
212     */
213     if (anptr->watched_by.head == NULL)
214     {
215 michael 231 assert(dlinkFind(&watchTable[strhash(anptr->nick)], anptr) != NULL);
216     dlinkDelete(&anptr->node, &watchTable[strhash(anptr->nick)]);
217 michael 215
218     BlockHeapFree(watch_heap, 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     }

Properties

Name Value
svn:eol-style native
svn:keywords Id