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 const char monitor_desc
[] = "Provides the MONITOR facility for tracking user signon and signoff";
44 static int monitor_init(void);
45 static void monitor_deinit(void);
46 static void m_monitor(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
48 struct Message monitor_msgtab
= {
49 "MONITOR", 0, 0, 0, 0,
50 {mg_unreg
, {m_monitor
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_monitor
, 2}}
53 mapi_clist_av1 monitor_clist
[] = { &monitor_msgtab
, NULL
};
55 DECLARE_MODULE_AV2(monitor
, monitor_init
, monitor_deinit
, monitor_clist
, NULL
, NULL
, NULL
, NULL
, monitor_desc
);
57 static int monitor_init(void)
59 add_isupport("MONITOR", isupport_intptr
, &ConfigFileEntry
.max_monitor
);
63 static void monitor_deinit(void)
65 delete_isupport("MONITOR");
69 add_monitor(struct Client
*client_p
, const char *nicks
)
71 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
72 struct Client
*target_p
;
73 struct monitor
*monptr
;
79 int cur_onlen
, cur_offlen
;
81 /* these two are same length, just diff numeric */
82 cur_offlen
= cur_onlen
= mlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
83 me
.name
, client_p
->name
, "");
84 sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
85 me
.name
, client_p
->name
, "");
88 offptr
= offbuf
+ mlen
;
90 tmp
= LOCAL_COPY(nicks
);
92 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
94 if(EmptyString(name
) || strlen(name
) > NICKLEN
-1)
97 if(rb_dlink_list_length(&client_p
->localClient
->monitor_list
) >=
98 (unsigned long)ConfigFileEntry
.max_monitor
)
102 if(cur_onlen
!= mlen
)
103 sendto_one(client_p
, "%s", onbuf
);
104 if(cur_offlen
!= mlen
)
105 sendto_one(client_p
, "%s", offbuf
);
108 snprintf(buf
, sizeof(buf
), "%s,%s", name
, p
);
110 snprintf(buf
, sizeof(buf
), "%s", name
);
112 sendto_one(client_p
, form_str(ERR_MONLISTFULL
),
113 me
.name
, client_p
->name
,
114 ConfigFileEntry
.max_monitor
, buf
);
118 if (!clean_nick(name
, 0))
121 monptr
= find_monitor(name
, 1);
123 /* already monitoring this nick */
124 if(rb_dlinkFind(client_p
, &monptr
->users
))
127 rb_dlinkAddAlloc(client_p
, &monptr
->users
);
128 rb_dlinkAddAlloc(monptr
, &client_p
->localClient
->monitor_list
);
130 if((target_p
= find_named_person(name
)) != NULL
)
132 if(cur_onlen
+ strlen(target_p
->name
) +
133 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
135 sendto_one(client_p
, "%s", onbuf
);
137 onptr
= onbuf
+ mlen
;
140 if(cur_onlen
!= mlen
)
146 arglen
= sprintf(onptr
, "%s!%s@%s",
147 target_p
->name
, target_p
->username
,
154 if(cur_offlen
+ strlen(name
) + 1 >= BUFSIZE
-3)
156 sendto_one(client_p
, "%s", offbuf
);
158 offptr
= offbuf
+ mlen
;
161 if(cur_offlen
!= mlen
)
167 arglen
= sprintf(offptr
, "%s", name
);
169 cur_offlen
+= arglen
;
173 if(cur_onlen
!= mlen
)
174 sendto_one(client_p
, "%s", onbuf
);
175 if(cur_offlen
!= mlen
)
176 sendto_one(client_p
, "%s", offbuf
);
180 del_monitor(struct Client
*client_p
, const char *nicks
)
182 struct monitor
*monptr
;
187 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
190 tmp
= LOCAL_COPY(nicks
);
192 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
194 if(EmptyString(name
))
198 if((monptr
= find_monitor(name
, 0)) == NULL
)
201 rb_dlinkFindDestroy(client_p
, &monptr
->users
);
202 rb_dlinkFindDestroy(monptr
, &client_p
->localClient
->monitor_list
);
204 free_monitor(monptr
);
209 list_monitor(struct Client
*client_p
)
212 struct monitor
*monptr
;
215 int mlen
, arglen
, cur_len
;
217 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
219 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
220 me
.name
, client_p
->name
);
224 cur_len
= mlen
= sprintf(buf
, form_str(RPL_MONLIST
),
225 me
.name
, client_p
->name
, "");
228 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
232 if(cur_len
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
234 sendto_one(client_p
, "%s", buf
);
245 arglen
= sprintf(nbuf
, "%s", monptr
->name
);
250 sendto_one(client_p
, "%s", buf
);
251 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
252 me
.name
, client_p
->name
);
256 show_monitor_status(struct Client
*client_p
)
258 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
259 struct Client
*target_p
;
260 struct monitor
*monptr
;
261 char *onptr
, *offptr
;
262 int cur_onlen
, cur_offlen
;
266 mlen
= cur_onlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
267 me
.name
, client_p
->name
, "");
268 cur_offlen
= sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
269 me
.name
, client_p
->name
, "");
271 onptr
= onbuf
+ mlen
;
272 offptr
= offbuf
+ mlen
;
274 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
278 if((target_p
= find_named_person(monptr
->name
)) != NULL
)
280 if(cur_onlen
+ strlen(target_p
->name
) +
281 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
283 sendto_one(client_p
, "%s", onbuf
);
285 onptr
= onbuf
+ mlen
;
288 if(cur_onlen
!= mlen
)
294 arglen
= sprintf(onptr
, "%s!%s@%s",
295 target_p
->name
, target_p
->username
,
302 if(cur_offlen
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
304 sendto_one(client_p
, "%s", offbuf
);
306 offptr
= offbuf
+ mlen
;
309 if(cur_offlen
!= mlen
)
315 arglen
= sprintf(offptr
, "%s", monptr
->name
);
317 cur_offlen
+= arglen
;
321 if(cur_onlen
!= mlen
)
322 sendto_one(client_p
, "%s", onbuf
);
323 if(cur_offlen
!= mlen
)
324 sendto_one(client_p
, "%s", offbuf
);
328 m_monitor(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
333 if(parc
< 3 || EmptyString(parv
[2]))
335 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
336 me
.name
, source_p
->name
, "MONITOR");
340 add_monitor(source_p
, parv
[2]);
343 if(parc
< 3 || EmptyString(parv
[2]))
345 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
346 me
.name
, source_p
->name
, "MONITOR");
350 del_monitor(source_p
, parv
[2]);
355 clear_monitor(source_p
);
360 list_monitor(source_p
);
365 show_monitor_status(source_p
);