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.
41 static int m_monitor(struct Client
*, struct Client
*, int, const char **);
43 struct Message monitor_msgtab
= {
44 "MONITOR", 0, 0, 0, MFLG_SLOW
,
45 {mg_unreg
, {m_monitor
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_monitor
, 2}}
48 mapi_clist_av1 monitor_clist
[] = { &monitor_msgtab
, NULL
};
49 DECLARE_MODULE_AV1(monitor
, NULL
, NULL
, monitor_clist
, NULL
, NULL
, "$Revision: 26 $");
52 add_monitor(struct Client
*client_p
, const char *nicks
)
54 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
55 struct Client
*target_p
;
56 struct monitor
*monptr
;
62 int cur_onlen
, cur_offlen
;
64 /* these two are same length, just diff numeric */
65 cur_offlen
= cur_onlen
= mlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
66 me
.name
, client_p
->name
, "");
67 sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
68 me
.name
, client_p
->name
, "");
71 offptr
= offbuf
+ mlen
;
73 tmp
= LOCAL_COPY(nicks
);
75 for(name
= strtoken(&p
, tmp
, ","); name
; name
= strtoken(&p
, NULL
, ","))
77 if(EmptyString(name
) || strlen(name
) > NICKLEN
-1)
80 if(dlink_list_length(&client_p
->localClient
->monitor_list
) >=
81 ConfigFileEntry
.max_monitor
)
86 sendto_one(client_p
, "%s", onbuf
);
87 if(cur_offlen
!= mlen
)
88 sendto_one(client_p
, "%s", offbuf
);
91 snprintf(buf
, sizeof(buf
), "%s,%s", name
, p
);
93 snprintf(buf
, sizeof(buf
), "%s", name
);
95 sendto_one(client_p
, form_str(ERR_MONLISTFULL
),
96 me
.name
, client_p
->name
,
97 ConfigFileEntry
.max_monitor
, buf
);
101 monptr
= find_monitor(name
, 1);
103 /* already monitoring this nick */
104 if(dlinkFind(client_p
, &monptr
->users
))
107 dlinkAddAlloc(client_p
, &monptr
->users
);
108 dlinkAddAlloc(monptr
, &client_p
->localClient
->monitor_list
);
110 if((target_p
= find_named_person(name
)) != NULL
)
112 if(cur_onlen
+ strlen(target_p
->name
) +
113 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
115 sendto_one(client_p
, "%s", onbuf
);
117 onptr
= onbuf
+ mlen
;
120 if(cur_onlen
!= mlen
)
126 arglen
= sprintf(onptr
, "%s!%s@%s",
127 target_p
->name
, target_p
->username
,
134 if(cur_offlen
+ strlen(name
) + 1 >= BUFSIZE
-3)
136 sendto_one(client_p
, "%s", offbuf
);
138 offptr
= offbuf
+ mlen
;
141 if(cur_offlen
!= mlen
)
147 arglen
= sprintf(offptr
, "%s", name
);
149 cur_offlen
+= arglen
;
153 if(cur_onlen
!= mlen
)
154 sendto_one(client_p
, "%s", onbuf
);
155 if(cur_offlen
!= mlen
)
156 sendto_one(client_p
, "%s", offbuf
);
160 del_monitor(struct Client
*client_p
, const char *nicks
)
162 struct monitor
*monptr
;
167 if(!dlink_list_length(&client_p
->localClient
->monitor_list
))
170 tmp
= LOCAL_COPY(nicks
);
172 for(name
= strtoken(&p
, tmp
, ","); name
; name
= strtoken(&p
, NULL
, ","))
174 if(EmptyString(name
))
178 if((monptr
= find_monitor(name
, 0)) == NULL
)
181 dlinkFindDestroy(client_p
, &monptr
->users
);
182 dlinkFindDestroy(monptr
, &client_p
->localClient
->monitor_list
);
187 list_monitor(struct Client
*client_p
)
190 struct monitor
*monptr
;
193 int mlen
, arglen
, cur_len
;
195 if(!dlink_list_length(&client_p
->localClient
->monitor_list
))
197 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
198 me
.name
, client_p
->name
);
202 cur_len
= mlen
= sprintf(buf
, form_str(RPL_MONLIST
),
203 me
.name
, client_p
->name
, "");
206 DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
210 if(cur_len
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
212 sendto_one(client_p
, "%s", buf
);
223 arglen
= sprintf(nbuf
, "%s", monptr
->name
);
228 sendto_one(client_p
, "%s", buf
);
229 sendto_one(client_p
, form_str(RPL_ENDOFMONLIST
),
230 me
.name
, client_p
->name
);
234 show_monitor_status(struct Client
*client_p
)
236 char onbuf
[BUFSIZE
], offbuf
[BUFSIZE
];
237 struct Client
*target_p
;
238 struct monitor
*monptr
;
239 char *onptr
, *offptr
;
240 int cur_onlen
, cur_offlen
;
244 mlen
= cur_onlen
= sprintf(onbuf
, form_str(RPL_MONONLINE
),
245 me
.name
, client_p
->name
, "");
246 cur_offlen
= sprintf(offbuf
, form_str(RPL_MONOFFLINE
),
247 me
.name
, client_p
->name
, "");
249 onptr
= onbuf
+ mlen
;
250 offptr
= offbuf
+ mlen
;
252 DLINK_FOREACH(ptr
, client_p
->localClient
->monitor_list
.head
)
256 if((target_p
= find_named_person(monptr
->name
)) != NULL
)
258 if(cur_onlen
+ strlen(target_p
->name
) +
259 strlen(target_p
->username
) + strlen(target_p
->host
) + 3 >= BUFSIZE
-3)
261 sendto_one(client_p
, "%s", onbuf
);
263 onptr
= onbuf
+ mlen
;
266 if(cur_onlen
!= mlen
)
272 arglen
= sprintf(onptr
, "%s!%s@%s",
273 target_p
->name
, target_p
->username
,
280 if(cur_offlen
+ strlen(monptr
->name
) + 1 >= BUFSIZE
-3)
282 sendto_one(client_p
, "%s", offbuf
);
284 offptr
= offbuf
+ mlen
;
287 if(cur_offlen
!= mlen
)
293 arglen
= sprintf(offptr
, "%s", monptr
->name
);
295 cur_offlen
+= arglen
;
299 if(cur_onlen
!= mlen
)
300 sendto_one(client_p
, "%s", onbuf
);
301 if(cur_offlen
!= mlen
)
302 sendto_one(client_p
, "%s", offbuf
);
306 m_monitor(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
311 if(parc
< 3 || EmptyString(parv
[2]))
313 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
314 me
.name
, source_p
->name
, "MONITOR");
318 add_monitor(source_p
, parv
[2]);
321 if(parc
< 3 || EmptyString(parv
[2]))
323 sendto_one(client_p
, form_str(ERR_NEEDMOREPARAMS
),
324 me
.name
, source_p
->name
, "MONITOR");
328 del_monitor(source_p
, parv
[2]);
333 clear_monitor(source_p
);
338 list_monitor(source_p
);
343 show_monitor_status(source_p
);