Remove cruft left behind by removed stats_p_hook.
[seven-1.x.git] / modules / m_monitor.c
blobd62eb93ee5f3c5e5c6a705403b85a18cec8989a9
1 /* modules/m_monitor.c
2 *
3 * Copyright (C) 2005 Lee Hardy <lee@leeh.co.uk>
4 * Copyright (C) 2005 ircd-ratbox development team
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "stdinc.h"
32 #include "tools.h"
33 #include "client.h"
34 #include "msg.h"
35 #include "parse.h"
36 #include "modules.h"
37 #include "monitor.h"
38 #include "numeric.h"
39 #include "s_conf.h"
41 static int m_monitor(struct Client *, struct Client *, int, const char **);
43 struct Message monitor_msgtab = {
44 "MONITOR", 0, 0, 0, MFLG_SLOW,
45 {mg_unreg, {m_monitor, 2}, mg_ignore, mg_ignore, mg_ignore, {m_monitor, 2}}
48 mapi_clist_av1 monitor_clist[] = { &monitor_msgtab, NULL };
49 DECLARE_MODULE_AV1(monitor, NULL, NULL, monitor_clist, NULL, NULL, "$Revision: 26 $");
51 static void
52 add_monitor(struct Client *client_p, const char *nicks)
54 char onbuf[BUFSIZE], offbuf[BUFSIZE];
55 struct Client *target_p;
56 struct monitor *monptr;
57 const char *name;
58 char *tmp;
59 char *p;
60 char *onptr, *offptr;
61 int mlen, arglen;
62 int cur_onlen, cur_offlen;
64 /* these two are same length, just diff numeric */
65 cur_offlen = cur_onlen = mlen = sprintf(onbuf, form_str(RPL_MONONLINE),
66 me.name, client_p->name, "");
67 sprintf(offbuf, form_str(RPL_MONOFFLINE),
68 me.name, client_p->name, "");
70 onptr = onbuf + mlen;
71 offptr = offbuf + mlen;
73 tmp = LOCAL_COPY(nicks);
75 for(name = strtoken(&p, tmp, ","); name; name = strtoken(&p, NULL, ","))
77 if(EmptyString(name) || strlen(name) > NICKLEN-1)
78 continue;
80 if(dlink_list_length(&client_p->localClient->monitor_list) >=
81 ConfigFileEntry.max_monitor)
83 char buf[100];
85 if(cur_onlen != mlen)
86 sendto_one(client_p, "%s", onbuf);
87 if(cur_offlen != mlen)
88 sendto_one(client_p, "%s", offbuf);
90 if(p)
91 snprintf(buf, sizeof(buf), "%s,%s", name, p);
92 else
93 snprintf(buf, sizeof(buf), "%s", name);
95 sendto_one(client_p, form_str(ERR_MONLISTFULL),
96 me.name, client_p->name,
97 ConfigFileEntry.max_monitor, buf);
98 return;
101 monptr = find_monitor(name, 1);
103 /* already monitoring this nick */
104 if(dlinkFind(client_p, &monptr->users))
105 continue;
107 dlinkAddAlloc(client_p, &monptr->users);
108 dlinkAddAlloc(monptr, &client_p->localClient->monitor_list);
110 if((target_p = find_named_person(name)) != NULL)
112 if(cur_onlen + strlen(target_p->name) +
113 strlen(target_p->username) + strlen(target_p->host) + 3 >= BUFSIZE-3)
115 sendto_one(client_p, "%s", onbuf);
116 cur_onlen = mlen;
117 onptr = onbuf + mlen;
120 if(cur_onlen != mlen)
122 *onptr++ = ',';
123 cur_onlen++;
126 arglen = sprintf(onptr, "%s!%s@%s",
127 target_p->name, target_p->username,
128 target_p->host);
129 onptr += arglen;
130 cur_onlen += arglen;
132 else
134 if(cur_offlen + strlen(name) + 1 >= BUFSIZE-3)
136 sendto_one(client_p, "%s", offbuf);
137 cur_offlen = mlen;
138 offptr = offbuf + mlen;
141 if(cur_offlen != mlen)
143 *offptr++ = ',';
144 cur_offlen++;
147 arglen = sprintf(offptr, "%s", name);
148 offptr += arglen;
149 cur_offlen += arglen;
153 if(cur_onlen != mlen)
154 sendto_one(client_p, "%s", onbuf);
155 if(cur_offlen != mlen)
156 sendto_one(client_p, "%s", offbuf);
159 static void
160 del_monitor(struct Client *client_p, const char *nicks)
162 struct monitor *monptr;
163 const char *name;
164 char *tmp;
165 char *p;
167 if(!dlink_list_length(&client_p->localClient->monitor_list))
168 return;
170 tmp = LOCAL_COPY(nicks);
172 for(name = strtoken(&p, tmp, ","); name; name = strtoken(&p, NULL, ","))
174 if(EmptyString(name))
175 continue;
177 /* not monitored */
178 if((monptr = find_monitor(name, 0)) == NULL)
179 continue;
181 dlinkFindDestroy(client_p, &monptr->users);
182 dlinkFindDestroy(monptr, &client_p->localClient->monitor_list);
186 static void
187 list_monitor(struct Client *client_p)
189 char buf[BUFSIZE];
190 struct monitor *monptr;
191 char *nbuf;
192 dlink_node *ptr;
193 int mlen, arglen, cur_len;
195 if(!dlink_list_length(&client_p->localClient->monitor_list))
197 sendto_one(client_p, form_str(RPL_ENDOFMONLIST),
198 me.name, client_p->name);
199 return;
202 cur_len = mlen = sprintf(buf, form_str(RPL_MONLIST),
203 me.name, client_p->name, "");
204 nbuf = buf + mlen;
206 DLINK_FOREACH(ptr, client_p->localClient->monitor_list.head)
208 monptr = ptr->data;
210 if(cur_len + strlen(monptr->name) + 1 >= BUFSIZE-3)
212 sendto_one(client_p, "%s", buf);
213 nbuf = buf + mlen;
214 cur_len = mlen;
217 if(cur_len != mlen)
219 *nbuf++ = ',';
220 cur_len++;
223 arglen = sprintf(nbuf, "%s", monptr->name);
224 cur_len += arglen;
225 nbuf += arglen;
228 sendto_one(client_p, "%s", buf);
229 sendto_one(client_p, form_str(RPL_ENDOFMONLIST),
230 me.name, client_p->name);
233 static void
234 show_monitor_status(struct Client *client_p)
236 char onbuf[BUFSIZE], offbuf[BUFSIZE];
237 struct Client *target_p;
238 struct monitor *monptr;
239 char *onptr, *offptr;
240 int cur_onlen, cur_offlen;
241 int mlen, arglen;
242 dlink_node *ptr;
244 mlen = cur_onlen = sprintf(onbuf, form_str(RPL_MONONLINE),
245 me.name, client_p->name, "");
246 cur_offlen = sprintf(offbuf, form_str(RPL_MONOFFLINE),
247 me.name, client_p->name, "");
249 onptr = onbuf + mlen;
250 offptr = offbuf + mlen;
252 DLINK_FOREACH(ptr, client_p->localClient->monitor_list.head)
254 monptr = ptr->data;
256 if((target_p = find_named_person(monptr->name)) != NULL)
258 if(cur_onlen + strlen(target_p->name) +
259 strlen(target_p->username) + strlen(target_p->host) + 3 >= BUFSIZE-3)
261 sendto_one(client_p, "%s", onbuf);
262 cur_onlen = mlen;
263 onptr = onbuf + mlen;
266 if(cur_onlen != mlen)
268 *onptr++ = ',';
269 cur_onlen++;
272 arglen = sprintf(onptr, "%s!%s@%s",
273 target_p->name, target_p->username,
274 target_p->host);
275 onptr += arglen;
276 cur_onlen += arglen;
278 else
280 if(cur_offlen + strlen(monptr->name) + 1 >= BUFSIZE-3)
282 sendto_one(client_p, "%s", offbuf);
283 cur_offlen = mlen;
284 offptr = offbuf + mlen;
287 if(cur_offlen != mlen)
289 *offptr++ = ',';
290 cur_offlen++;
293 arglen = sprintf(offptr, "%s", monptr->name);
294 offptr += arglen;
295 cur_offlen += arglen;
299 if(cur_onlen != mlen)
300 sendto_one(client_p, "%s", onbuf);
301 if(cur_offlen != mlen)
302 sendto_one(client_p, "%s", offbuf);
305 static int
306 m_monitor(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
308 switch(parv[1][0])
310 case '+':
311 if(parc < 3 || EmptyString(parv[2]))
313 sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS),
314 me.name, source_p->name, "MONITOR");
315 return 0;
318 add_monitor(source_p, parv[2]);
319 break;
320 case '-':
321 if(parc < 3 || EmptyString(parv[2]))
323 sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS),
324 me.name, source_p->name, "MONITOR");
325 return 0;
328 del_monitor(source_p, parv[2]);
329 break;
331 case 'C':
332 case 'c':
333 clear_monitor(source_p);
334 break;
336 case 'L':
337 case 'l':
338 list_monitor(source_p);
339 break;
341 case 'S':
342 case 's':
343 show_monitor_status(source_p);
344 break;
346 default:
347 break;
350 return 0;