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 if (!clean_nick(name
, 0))
120 monptr
= find_monitor(name
, 1);
122 /* already monitoring this nick */
123 if(rb_dlinkFind(client_p
, &monptr
->users
))
126 rb_dlinkAddAlloc(client_p
, &monptr
->users
);
127 rb_dlinkAddAlloc(monptr
, &client_p
->localClient
->monitor_list
);
129 if((target_p
= find_named_person(name
)) != NULL
)
131 if(cur_onlen
+ strlen(target_p
->name
) +
132 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
134 sendto_one(client_p
, "%s", onbuf
);
136 onptr
= onbuf
+ mlen
;
139 if(cur_onlen
!= mlen
)
145 arglen
= sprintf(onptr
, "%s!%s@%s",
146 target_p
->name
, target_p
->username
,
153 if(cur_offlen
+ strlen(name
) + 1 >= BUFSIZE
-3)
155 sendto_one(client_p
, "%s", offbuf
);
157 offptr
= offbuf
+ mlen
;
160 if(cur_offlen
!= mlen
)
166 arglen
= sprintf(offptr
, "%s", name
);
168 cur_offlen
+= arglen
;
172 if(cur_onlen
!= mlen
)
173 sendto_one(client_p
, "%s", onbuf
);
174 if(cur_offlen
!= mlen
)
175 sendto_one(client_p
, "%s", offbuf
);
179 del_monitor(struct Client
*client_p
, const char *nicks
)
181 struct monitor
*monptr
;
186 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
189 tmp
= LOCAL_COPY(nicks
);
191 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
193 if(EmptyString(name
))
197 if((monptr
= find_monitor(name
, 0)) == NULL
)
200 rb_dlinkFindDestroy(client_p
, &monptr
->users
);
201 rb_dlinkFindDestroy(monptr
, &client_p
->localClient
->monitor_list
);
203 free_monitor(monptr
);
208 list_monitor(struct Client
*client_p
)
211 struct monitor
*monptr
;
214 int mlen
, arglen
, cur_len
;
216 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
218 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
219 me
.name
, client_p
->name
);
223 cur_len
= mlen
= sprintf(buf
, form_str(RPL_MONLIST
),
224 me
.name
, client_p
->name
, "");
227 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
231 if(cur_len
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
233 sendto_one(client_p
, "%s", buf
);
244 arglen
= sprintf(nbuf
, "%s", monptr
->name
);
249 sendto_one(client_p
, "%s", buf
);
250 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
251 me
.name
, client_p
->name
);
255 show_monitor_status(struct Client
*client_p
)
257 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
258 struct Client
*target_p
;
259 struct monitor
*monptr
;
260 char *onptr
, *offptr
;
261 int cur_onlen
, cur_offlen
;
265 mlen
= cur_onlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
266 me
.name
, client_p
->name
, "");
267 cur_offlen
= sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
268 me
.name
, client_p
->name
, "");
270 onptr
= onbuf
+ mlen
;
271 offptr
= offbuf
+ mlen
;
273 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
277 if((target_p
= find_named_person(monptr
->name
)) != NULL
)
279 if(cur_onlen
+ strlen(target_p
->name
) +
280 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
282 sendto_one(client_p
, "%s", onbuf
);
284 onptr
= onbuf
+ mlen
;
287 if(cur_onlen
!= mlen
)
293 arglen
= sprintf(onptr
, "%s!%s@%s",
294 target_p
->name
, target_p
->username
,
301 if(cur_offlen
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
303 sendto_one(client_p
, "%s", offbuf
);
305 offptr
= offbuf
+ mlen
;
308 if(cur_offlen
!= mlen
)
314 arglen
= sprintf(offptr
, "%s", monptr
->name
);
316 cur_offlen
+= arglen
;
320 if(cur_onlen
!= mlen
)
321 sendto_one(client_p
, "%s", onbuf
);
322 if(cur_offlen
!= mlen
)
323 sendto_one(client_p
, "%s", offbuf
);
327 m_monitor(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
332 if(parc
< 3 || EmptyString(parv
[2]))
334 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
335 me
.name
, source_p
->name
, "MONITOR");
339 add_monitor(source_p
, parv
[2]);
342 if(parc
< 3 || EmptyString(parv
[2]))
344 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
345 me
.name
, source_p
->name
, "MONITOR");
349 del_monitor(source_p
, parv
[2]);
354 clear_monitor(source_p
);
359 list_monitor(source_p
);
364 show_monitor_status(source_p
);