]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd). | |
3 | * monitor.c - Code for server-side notify lists | |
4 | * | |
5 | * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk> | |
6 | * Copyright (C) 2005 ircd-ratbox development team | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions are | |
10 | * met: | |
11 | * | |
12 | * 1.Redistributions of source code must retain the above copyright notice, | |
13 | * this list of conditions and the following disclaimer. | |
14 | * 2.Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * 3.The name of the author may not be used to endorse or promote products | |
18 | * derived from this software without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
23 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
24 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
28 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 | * POSSIBILITY OF SUCH DAMAGE. | |
31 | * | |
32 | * $Id: monitor.c 312 2005-11-07 10:47:33Z jilles $ | |
33 | */ | |
34 | #include "stdinc.h" | |
35 | #include "tools.h" | |
36 | #include "client.h" | |
37 | #include "memory.h" | |
38 | #include "balloc.h" | |
39 | #include "monitor.h" | |
40 | #include "hash.h" | |
41 | #include "event.h" | |
42 | #include "numeric.h" | |
43 | ||
44 | static struct monitor *monitorTable[MONITOR_HASH_SIZE]; | |
45 | BlockHeap *monitor_heap; | |
46 | ||
47 | static void cleanup_monitor(void *unused); | |
48 | ||
49 | void | |
50 | init_monitor(void) | |
51 | { | |
52 | monitor_heap = BlockHeapCreate(sizeof(struct monitor), MONITOR_HEAP_SIZE); | |
53 | eventAddIsh("cleanup_monitor", cleanup_monitor, NULL, 3600); | |
54 | } | |
55 | ||
56 | static inline unsigned int | |
57 | hash_monitor_nick(const char *name) | |
58 | { | |
59 | return fnv_hash_upper((const unsigned char *) name, MONITOR_HASH_BITS); | |
60 | } | |
61 | ||
62 | struct monitor * | |
63 | find_monitor(const char *name, int add) | |
64 | { | |
65 | struct monitor *monptr; | |
66 | ||
67 | unsigned int hashv = hash_monitor_nick(name); | |
68 | ||
69 | for(monptr = monitorTable[hashv]; monptr; monptr = monptr->hnext) | |
70 | { | |
71 | if(!irccmp(monptr->name, name)) | |
72 | return monptr; | |
73 | } | |
74 | ||
75 | if(add) | |
76 | { | |
77 | monptr = BlockHeapAlloc(monitor_heap); | |
78 | strlcpy(monptr->name, name, sizeof(monptr->name)); | |
79 | ||
80 | monptr->hnext = monitorTable[hashv]; | |
81 | monitorTable[hashv] = monptr; | |
82 | ||
83 | return monptr; | |
84 | } | |
85 | ||
86 | return NULL; | |
87 | } | |
88 | ||
89 | /* monitor_signon() | |
90 | * | |
91 | * inputs - client who has just connected | |
92 | * outputs - | |
93 | * side effects - notifies any clients monitoring this nickname that it has | |
94 | * connected to the network | |
95 | */ | |
96 | void | |
97 | monitor_signon(struct Client *client_p) | |
98 | { | |
99 | char buf[USERHOST_REPLYLEN]; | |
100 | struct monitor *monptr = find_monitor(client_p->name, 0); | |
101 | struct Client *target_p; | |
102 | dlink_node *ptr; | |
103 | ||
104 | /* noones watching this nick */ | |
105 | if(monptr == NULL) | |
106 | return; | |
107 | ||
108 | ircsnprintf(buf, sizeof(buf), "%s!%s@%s", | |
109 | client_p->name, client_p->username, client_p->host); | |
110 | ||
111 | DLINK_FOREACH(ptr, monptr->users.head) | |
112 | { | |
113 | target_p = ptr->data; | |
114 | ||
115 | sendto_one(target_p, form_str(RPL_MONONLINE), | |
116 | me.name, target_p->name, buf); | |
117 | } | |
118 | } | |
119 | ||
120 | /* monitor_signoff() | |
121 | * | |
122 | * inputs - client who is exiting | |
123 | * outputs - | |
124 | * side effects - notifies any clients monitoring this nickname that it has | |
125 | * left the network | |
126 | */ | |
127 | void | |
128 | monitor_signoff(struct Client *client_p) | |
129 | { | |
130 | struct monitor *monptr = find_monitor(client_p->name, 0); | |
131 | dlink_node *ptr; | |
132 | ||
133 | /* noones watching this nick */ | |
134 | if(monptr == NULL) | |
135 | return; | |
136 | ||
137 | DLINK_FOREACH(ptr, monptr->users.head) | |
138 | { | |
139 | sendto_one(ptr->data, form_str(RPL_MONOFFLINE), | |
140 | me.name, ((struct Client *) ptr->data)->name, client_p->name); | |
141 | } | |
142 | } | |
143 | ||
144 | void | |
145 | clear_monitor(struct Client *client_p) | |
146 | { | |
147 | struct monitor *monptr; | |
148 | dlink_node *ptr, *next_ptr; | |
149 | ||
150 | DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->monitor_list.head) | |
151 | { | |
152 | monptr = ptr->data; | |
153 | ||
154 | /* we leave the actual entry around with no users, itll be | |
155 | * cleaned up periodically by cleanup_monitor() --anfl | |
156 | */ | |
157 | dlinkFindDestroy(client_p, &monptr->users); | |
158 | free_dlink_node(ptr); | |
159 | } | |
160 | ||
161 | client_p->localClient->monitor_list.head = client_p->localClient->monitor_list.tail = NULL; | |
162 | client_p->localClient->monitor_list.length = 0; | |
163 | } | |
164 | ||
165 | static void | |
166 | cleanup_monitor(void *unused) | |
167 | { | |
168 | struct monitor *last_ptr = NULL; | |
169 | struct monitor *next_ptr, *ptr; | |
170 | int i; | |
171 | ||
172 | for(i = 0; i < MONITOR_HASH_SIZE; i++) | |
173 | { | |
174 | last_ptr = NULL; | |
175 | for(ptr = monitorTable[i]; ptr; ptr = next_ptr) | |
176 | { | |
177 | next_ptr = ptr->hnext; | |
178 | ||
179 | if(!dlink_list_length(&ptr->users)) | |
180 | { | |
181 | if(last_ptr) | |
182 | last_ptr->hnext = next_ptr; | |
183 | else | |
184 | monitorTable[i] = next_ptr; | |
185 | ||
186 | BlockHeapFree(monitor_heap, ptr); | |
187 | } | |
188 | else | |
189 | last_ptr = ptr; | |
190 | } | |
191 | } | |
192 | } |