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 static int m_monitor(struct Client
*, struct Client
*, int, const char **);
44 struct Message monitor_msgtab
= {
45 "MONITOR", 0, 0, 0, MFLG_SLOW
,
46 {mg_unreg
, {m_monitor
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_monitor
, 2}}
49 mapi_clist_av1 monitor_clist
[] = { &monitor_msgtab
, NULL
};
50 DECLARE_MODULE_AV1(monitor
, NULL
, NULL
, monitor_clist
, NULL
, NULL
, "$Revision: 312 $");
53 add_monitor(struct Client
*client_p
, const char *nicks
)
55 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
56 struct Client
*target_p
;
57 struct monitor
*monptr
;
63 int cur_onlen
, cur_offlen
;
65 /* these two are same length, just diff numeric */
66 cur_offlen
= cur_onlen
= mlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
67 me
.name
, client_p
->name
, "");
68 sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
69 me
.name
, client_p
->name
, "");
72 offptr
= offbuf
+ mlen
;
74 tmp
= LOCAL_COPY(nicks
);
76 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
78 if(EmptyString(name
) || strlen(name
) > NICKLEN
-1)
81 if(rb_dlink_list_length(&client_p
->localClient
->monitor_list
) >=
82 ConfigFileEntry
.max_monitor
)
87 sendto_one(client_p
, "%s", onbuf
);
88 if(cur_offlen
!= mlen
)
89 sendto_one(client_p
, "%s", offbuf
);
92 snprintf(buf
, sizeof(buf
), "%s,%s", name
, p
);
94 snprintf(buf
, sizeof(buf
), "%s", name
);
96 sendto_one(client_p
, form_str(ERR_MONLISTFULL
),
97 me
.name
, client_p
->name
,
98 ConfigFileEntry
.max_monitor
, buf
);
102 monptr
= find_monitor(name
, 1);
104 /* already monitoring this nick */
105 if(rb_dlinkFind(client_p
, &monptr
->users
))
108 rb_dlinkAddAlloc(client_p
, &monptr
->users
);
109 rb_dlinkAddAlloc(monptr
, &client_p
->localClient
->monitor_list
);
111 if((target_p
= find_named_person(name
)) != NULL
)
113 if(cur_onlen
+ strlen(target_p
->name
) +
114 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
116 sendto_one(client_p
, "%s", onbuf
);
118 onptr
= onbuf
+ mlen
;
121 if(cur_onlen
!= mlen
)
127 arglen
= sprintf(onptr
, "%s!%s@%s",
128 target_p
->name
, target_p
->username
,
135 if(cur_offlen
+ strlen(name
) + 1 >= BUFSIZE
-3)
137 sendto_one(client_p
, "%s", offbuf
);
139 offptr
= offbuf
+ mlen
;
142 if(cur_offlen
!= mlen
)
148 arglen
= sprintf(offptr
, "%s", name
);
150 cur_offlen
+= arglen
;
154 if(cur_onlen
!= mlen
)
155 sendto_one(client_p
, "%s", onbuf
);
156 if(cur_offlen
!= mlen
)
157 sendto_one(client_p
, "%s", offbuf
);
161 del_monitor(struct Client
*client_p
, const char *nicks
)
163 struct monitor
*monptr
;
168 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
171 tmp
= LOCAL_COPY(nicks
);
173 for(name
= rb_strtok_r(tmp
, ",", &p
); name
; name
= rb_strtok_r(NULL
, ",", &p
))
175 if(EmptyString(name
))
179 if((monptr
= find_monitor(name
, 0)) == NULL
)
182 rb_dlinkFindDestroy(client_p
, &monptr
->users
);
183 rb_dlinkFindDestroy(monptr
, &client_p
->localClient
->monitor_list
);
188 list_monitor(struct Client
*client_p
)
191 struct monitor
*monptr
;
194 int mlen
, arglen
, cur_len
;
196 if(!rb_dlink_list_length(&client_p
->localClient
->monitor_list
))
198 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
199 me
.name
, client_p
->name
);
203 cur_len
= mlen
= sprintf(buf
, form_str(RPL_MONLIST
),
204 me
.name
, client_p
->name
, "");
207 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
211 if(cur_len
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
213 sendto_one(client_p
, "%s", buf
);
224 arglen
= sprintf(nbuf
, "%s", monptr
->name
);
229 sendto_one(client_p
, "%s", buf
);
230 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
231 me
.name
, client_p
->name
);
235 show_monitor_status(struct Client
*client_p
)
237 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
238 struct Client
*target_p
;
239 struct monitor
*monptr
;
240 char *onptr
, *offptr
;
241 int cur_onlen
, cur_offlen
;
245 mlen
= cur_onlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
246 me
.name
, client_p
->name
, "");
247 cur_offlen
= sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
248 me
.name
, client_p
->name
, "");
250 onptr
= onbuf
+ mlen
;
251 offptr
= offbuf
+ mlen
;
253 RB_DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
257 if((target_p
= find_named_person(monptr
->name
)) != NULL
)
259 if(cur_onlen
+ strlen(target_p
->name
) +
260 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
262 sendto_one(client_p
, "%s", onbuf
);
264 onptr
= onbuf
+ mlen
;
267 if(cur_onlen
!= mlen
)
273 arglen
= sprintf(onptr
, "%s!%s@%s",
274 target_p
->name
, target_p
->username
,
281 if(cur_offlen
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
283 sendto_one(client_p
, "%s", offbuf
);
285 offptr
= offbuf
+ mlen
;
288 if(cur_offlen
!= mlen
)
294 arglen
= sprintf(offptr
, "%s", monptr
->name
);
296 cur_offlen
+= arglen
;
300 if(cur_onlen
!= mlen
)
301 sendto_one(client_p
, "%s", onbuf
);
302 if(cur_offlen
!= mlen
)
303 sendto_one(client_p
, "%s", offbuf
);
307 m_monitor(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
312 if(parc
< 3 || EmptyString(parv
[2]))
314 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
315 me
.name
, source_p
->name
, "MONITOR");
319 add_monitor(source_p
, parv
[2]);
322 if(parc
< 3 || EmptyString(parv
[2]))
324 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
325 me
.name
, source_p
->name
, "MONITOR");
329 del_monitor(source_p
, parv
[2]);
334 clear_monitor(source_p
);
339 list_monitor(source_p
);
344 show_monitor_status(source_p
);