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
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.
40 #include "supported.h"
42 static int monitor_init(void);
43 static void monitor_deinit(void);
44 static int m_monitor(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
46 struct Message monitor_msgtab
= {
47 "MONITOR", 0, 0, 0, 0,
48 {mg_unreg
, {m_monitor
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_monitor
, 2}}
51 mapi_clist_av1 monitor_clist
[] = { &monitor_msgtab
, NULL
};
52 DECLARE_MODULE_AV2(monitor
, monitor_init
, monitor_deinit
, monitor_clist
, NULL
, NULL
, NULL
, NULL
, NULL
);
54 static int monitor_init(void)
56 add_isupport("MONITOR", isupport_intptr
, &ConfigFileEntry
.max_monitor
);
60 static void monitor_deinit(void)
62 delete_isupport("MONITOR");
66 add_monitor(struct Client
*client_p
, const char *nicks
)
68 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
69 struct Client
*target_p
;
70 struct monitor
*monptr
;
76 int cur_onlen
, cur_offlen
;
78 /* these two are same length, just diff numeric */
79 cur_offlen
= cur_onlen
= mlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
80 me
.name
, client_p
->name
, "");
81 sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
82 me
.name
, client_p
->name
, "");
85 offptr
= offbuf
+ mlen
;
87 tmp
= LOCAL_COPY(nicks
);
89 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
91 if(EmptyString(name
) || strlen(name
) > NICKLEN
-1)
94 if(rb_dlink_list_length(&client_p
->localClient
->monitor_list
) >=
95 (unsigned long)ConfigFileEntry
.max_monitor
)
100 sendto_one(client_p
, "%s", onbuf
);
101 if(cur_offlen
!= mlen
)
102 sendto_one(client_p
, "%s", offbuf
);
105 snprintf(buf
, sizeof(buf
), "%s,%s", name
, p
);
107 snprintf(buf
, sizeof(buf
), "%s", name
);
109 sendto_one(client_p
, form_str(ERR_MONLISTFULL
),
110 me
.name
, client_p
->name
,
111 ConfigFileEntry
.max_monitor
, buf
);
115 if (!clean_nick(name
, 0))
118 monptr
= find_monitor(name
, 1);
120 /* already monitoring this nick */
121 if(rb_dlinkFind(client_p
, &monptr
->users
))
124 rb_dlinkAddAlloc(client_p
, &monptr
->users
);
125 rb_dlinkAddAlloc(monptr
, &client_p
->localClient
->monitor_list
);
127 if((target_p
= find_named_person(name
)) != NULL
)
129 if(cur_onlen
+ strlen(target_p
->name
) +
130 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
132 sendto_one(client_p
, "%s", onbuf
);
134 onptr
= onbuf
+ mlen
;
137 if(cur_onlen
!= mlen
)
143 arglen
= sprintf(onptr
, "%s!%s@%s",
144 target_p
->name
, target_p
->username
,
151 if(cur_offlen
+ strlen(name
) + 1 >= BUFSIZE
-3)
153 sendto_one(client_p
, "%s", offbuf
);
155 offptr
= offbuf
+ mlen
;
158 if(cur_offlen
!= mlen
)
164 arglen
= sprintf(offptr
, "%s", name
);
166 cur_offlen
+= arglen
;
170 if(cur_onlen
!= mlen
)
171 sendto_one(client_p
, "%s", onbuf
);
172 if(cur_offlen
!= mlen
)
173 sendto_one(client_p
, "%s", offbuf
);
177 del_monitor(struct Client
*client_p
, const char *nicks
)
179 struct monitor
*monptr
;
184 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
187 tmp
= LOCAL_COPY(nicks
);
189 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
191 if(EmptyString(name
))
195 if((monptr
= find_monitor(name
, 0)) == NULL
)
198 rb_dlinkFindDestroy(client_p
, &monptr
->users
);
199 rb_dlinkFindDestroy(monptr
, &client_p
->localClient
->monitor_list
);
201 free_monitor(monptr
);
206 list_monitor(struct Client
*client_p
)
209 struct monitor
*monptr
;
212 int mlen
, arglen
, cur_len
;
214 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
216 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
217 me
.name
, client_p
->name
);
221 cur_len
= mlen
= sprintf(buf
, form_str(RPL_MONLIST
),
222 me
.name
, client_p
->name
, "");
225 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
229 if(cur_len
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
231 sendto_one(client_p
, "%s", buf
);
242 arglen
= sprintf(nbuf
, "%s", monptr
->name
);
247 sendto_one(client_p
, "%s", buf
);
248 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
249 me
.name
, client_p
->name
);
253 show_monitor_status(struct Client
*client_p
)
255 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
256 struct Client
*target_p
;
257 struct monitor
*monptr
;
258 char *onptr
, *offptr
;
259 int cur_onlen
, cur_offlen
;
263 mlen
= cur_onlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
264 me
.name
, client_p
->name
, "");
265 cur_offlen
= sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
266 me
.name
, client_p
->name
, "");
268 onptr
= onbuf
+ mlen
;
269 offptr
= offbuf
+ mlen
;
271 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
275 if((target_p
= find_named_person(monptr
->name
)) != NULL
)
277 if(cur_onlen
+ strlen(target_p
->name
) +
278 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
280 sendto_one(client_p
, "%s", onbuf
);
282 onptr
= onbuf
+ mlen
;
285 if(cur_onlen
!= mlen
)
291 arglen
= sprintf(onptr
, "%s!%s@%s",
292 target_p
->name
, target_p
->username
,
299 if(cur_offlen
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
301 sendto_one(client_p
, "%s", offbuf
);
303 offptr
= offbuf
+ mlen
;
306 if(cur_offlen
!= mlen
)
312 arglen
= sprintf(offptr
, "%s", monptr
->name
);
314 cur_offlen
+= arglen
;
318 if(cur_onlen
!= mlen
)
319 sendto_one(client_p
, "%s", onbuf
);
320 if(cur_offlen
!= mlen
)
321 sendto_one(client_p
, "%s", offbuf
);
325 m_monitor(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
330 if(parc
< 3 || EmptyString(parv
[2]))
332 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
333 me
.name
, source_p
->name
, "MONITOR");
337 add_monitor(source_p
, parv
[2]);
340 if(parc
< 3 || EmptyString(parv
[2]))
342 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
343 me
.name
, source_p
->name
, "MONITOR");
347 del_monitor(source_p
, parv
[2]);
352 clear_monitor(source_p
);
357 list_monitor(source_p
);
362 show_monitor_status(source_p
);