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.
30 * $Id: m_monitor.c 312 2005-11-07 10:47:33Z jilles $
42 #include "supported.h"
44 static int monitor_init(void);
45 static void monitor_deinit(void);
46 static int m_monitor(struct Client
*, struct Client
*, int, const char **);
48 struct Message monitor_msgtab
= {
49 "MONITOR", 0, 0, 0, MFLG_SLOW
,
50 {mg_unreg
, {m_monitor
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_monitor
, 2}}
53 mapi_clist_av1 monitor_clist
[] = { &monitor_msgtab
, NULL
};
54 DECLARE_MODULE_AV1(monitor
, monitor_init
, monitor_deinit
, monitor_clist
, NULL
, NULL
, "$Revision: 312 $");
56 static int monitor_init(void)
58 add_isupport("MONITOR", isupport_intptr
, &ConfigFileEntry
.max_monitor
);
62 static void monitor_deinit(void)
64 delete_isupport("MONITOR");
68 add_monitor(struct Client
*client_p
, const char *nicks
)
70 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
71 struct Client
*target_p
;
72 struct monitor
*monptr
;
78 int cur_onlen
, cur_offlen
;
80 /* these two are same length, just diff numeric */
81 cur_offlen
= cur_onlen
= mlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
82 me
.name
, client_p
->name
, "");
83 sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
84 me
.name
, client_p
->name
, "");
87 offptr
= offbuf
+ mlen
;
89 tmp
= LOCAL_COPY(nicks
);
91 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
93 if(EmptyString(name
) || strlen(name
) > NICKLEN
-1)
96 if(rb_dlink_list_length(&client_p
->localClient
->monitor_list
) >=
97 (unsigned long)ConfigFileEntry
.max_monitor
)
101 if(cur_onlen
!= mlen
)
102 sendto_one(client_p
, "%s", onbuf
);
103 if(cur_offlen
!= mlen
)
104 sendto_one(client_p
, "%s", offbuf
);
107 rb_snprintf(buf
, sizeof(buf
), "%s,%s", name
, p
);
109 rb_snprintf(buf
, sizeof(buf
), "%s", name
);
111 sendto_one(client_p
, form_str(ERR_MONLISTFULL
),
112 me
.name
, client_p
->name
,
113 ConfigFileEntry
.max_monitor
, buf
);
117 monptr
= find_monitor(name
, 1);
119 /* already monitoring this nick */
120 if(rb_dlinkFind(client_p
, &monptr
->users
))
123 rb_dlinkAddAlloc(client_p
, &monptr
->users
);
124 rb_dlinkAddAlloc(monptr
, &client_p
->localClient
->monitor_list
);
126 if((target_p
= find_named_person(name
)) != NULL
)
128 if(cur_onlen
+ strlen(target_p
->name
) +
129 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
131 sendto_one(client_p
, "%s", onbuf
);
133 onptr
= onbuf
+ mlen
;
136 if(cur_onlen
!= mlen
)
142 arglen
= sprintf(onptr
, "%s!%s@%s",
143 target_p
->name
, target_p
->username
,
150 if(cur_offlen
+ strlen(name
) + 1 >= BUFSIZE
-3)
152 sendto_one(client_p
, "%s", offbuf
);
154 offptr
= offbuf
+ mlen
;
157 if(cur_offlen
!= mlen
)
163 arglen
= sprintf(offptr
, "%s", name
);
165 cur_offlen
+= arglen
;
169 if(cur_onlen
!= mlen
)
170 sendto_one(client_p
, "%s", onbuf
);
171 if(cur_offlen
!= mlen
)
172 sendto_one(client_p
, "%s", offbuf
);
176 del_monitor(struct Client
*client_p
, const char *nicks
)
178 struct monitor
*monptr
;
183 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
186 tmp
= LOCAL_COPY(nicks
);
188 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
190 if(EmptyString(name
))
194 if((monptr
= find_monitor(name
, 0)) == NULL
)
197 rb_dlinkFindDestroy(client_p
, &monptr
->users
);
198 rb_dlinkFindDestroy(monptr
, &client_p
->localClient
->monitor_list
);
200 free_monitor(monptr
);
205 list_monitor(struct Client
*client_p
)
208 struct monitor
*monptr
;
211 int mlen
, arglen
, cur_len
;
213 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
215 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
216 me
.name
, client_p
->name
);
220 cur_len
= mlen
= sprintf(buf
, form_str(RPL_MONLIST
),
221 me
.name
, client_p
->name
, "");
224 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
228 if(cur_len
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
230 sendto_one(client_p
, "%s", buf
);
241 arglen
= sprintf(nbuf
, "%s", monptr
->name
);
246 sendto_one(client_p
, "%s", buf
);
247 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
248 me
.name
, client_p
->name
);
252 show_monitor_status(struct Client
*client_p
)
254 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
255 struct Client
*target_p
;
256 struct monitor
*monptr
;
257 char *onptr
, *offptr
;
258 int cur_onlen
, cur_offlen
;
262 mlen
= cur_onlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
263 me
.name
, client_p
->name
, "");
264 cur_offlen
= sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
265 me
.name
, client_p
->name
, "");
267 onptr
= onbuf
+ mlen
;
268 offptr
= offbuf
+ mlen
;
270 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
274 if((target_p
= find_named_person(monptr
->name
)) != NULL
)
276 if(cur_onlen
+ strlen(target_p
->name
) +
277 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
279 sendto_one(client_p
, "%s", onbuf
);
281 onptr
= onbuf
+ mlen
;
284 if(cur_onlen
!= mlen
)
290 arglen
= sprintf(onptr
, "%s!%s@%s",
291 target_p
->name
, target_p
->username
,
298 if(cur_offlen
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
300 sendto_one(client_p
, "%s", offbuf
);
302 offptr
= offbuf
+ mlen
;
305 if(cur_offlen
!= mlen
)
311 arglen
= sprintf(offptr
, "%s", monptr
->name
);
313 cur_offlen
+= arglen
;
317 if(cur_onlen
!= mlen
)
318 sendto_one(client_p
, "%s", onbuf
);
319 if(cur_offlen
!= mlen
)
320 sendto_one(client_p
, "%s", offbuf
);
324 m_monitor(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
329 if(parc
< 3 || EmptyString(parv
[2]))
331 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
332 me
.name
, source_p
->name
, "MONITOR");
336 add_monitor(source_p
, parv
[2]);
339 if(parc
< 3 || EmptyString(parv
[2]))
341 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
342 me
.name
, source_p
->name
, "MONITOR");
346 del_monitor(source_p
, parv
[2]);
351 clear_monitor(source_p
);
356 list_monitor(source_p
);
361 show_monitor_status(source_p
);