1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997 Jukka Santala (Donwulff) |
5 |
* Copyright (c) 2000-2019 ircd-hybrid development team |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
20 |
* USA |
21 |
*/ |
22 |
|
23 |
/*! \file m_watch.c |
24 |
* \brief Includes required functions for processing the WATCH command. |
25 |
* \version $Id$ |
26 |
*/ |
27 |
|
28 |
#include "stdinc.h" |
29 |
#include "client.h" |
30 |
#include "irc_string.h" |
31 |
#include "ircd.h" |
32 |
#include "numeric.h" |
33 |
#include "conf.h" |
34 |
#include "send.h" |
35 |
#include "parse.h" |
36 |
#include "modules.h" |
37 |
#include "user.h" |
38 |
#include "watch.h" |
39 |
#include "isupport.h" |
40 |
|
41 |
|
42 |
/* |
43 |
* RPL_NOWON - Online at the moment (Succesfully added to WATCH-list) |
44 |
* RPL_NOWOFF - Offline at the moment (Succesfully added to WATCH-list) |
45 |
* RPL_WATCHOFF - Succesfully removed from WATCH-list. |
46 |
* ERR_TOOMANYWATCH - Take a guess :> Too many WATCH entries. |
47 |
*/ |
48 |
static void |
49 |
show_watch(struct Client *source_p, const char *name, |
50 |
enum irc_numerics rpl1, enum irc_numerics rpl2) |
51 |
{ |
52 |
const struct Client *target_p; |
53 |
|
54 |
if ((target_p = find_person(source_p, name))) |
55 |
sendto_one_numeric(source_p, &me, rpl1, |
56 |
target_p->name, target_p->username, |
57 |
target_p->host, target_p->tsinfo); |
58 |
else |
59 |
sendto_one_numeric(source_p, &me, rpl2, name, "*", "*", 0); |
60 |
} |
61 |
|
62 |
/*! \brief WATCH command handler |
63 |
* |
64 |
* \param source_p Pointer to allocated Client struct from which the message |
65 |
* originally comes from. This can be a local or remote client. |
66 |
* \param parc Integer holding the number of supplied arguments. |
67 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
68 |
* pointers. |
69 |
* \note Valid arguments for this command are: |
70 |
* - parv[0] = command |
71 |
* - parv[1] = watch options |
72 |
*/ |
73 |
static int |
74 |
m_watch(struct Client *source_p, int parc, char *parv[]) |
75 |
{ |
76 |
dlink_node *node; |
77 |
char *p = NULL; |
78 |
char def[2] = "l"; |
79 |
unsigned int list_requested = 0; |
80 |
|
81 |
/* |
82 |
* Default to 'l' - list who's currently online |
83 |
*/ |
84 |
if (parc < 2) |
85 |
parv[1] = def; |
86 |
|
87 |
for (const char *s = strtok_r(parv[1], ", ", &p); s; |
88 |
s = strtok_r(NULL, ", ", &p)) |
89 |
{ |
90 |
char *user; |
91 |
if ((user = strchr(s, '!'))) |
92 |
*user = '\0'; /* Not used */ |
93 |
|
94 |
/* |
95 |
* Prefix of "+", they want to add a name to their WATCH list. |
96 |
*/ |
97 |
if (*s == '+') |
98 |
{ |
99 |
if (*(s + 1)) |
100 |
{ |
101 |
if (dlink_list_length(&source_p->connection->watches) >= |
102 |
ConfigGeneral.max_watch) |
103 |
{ |
104 |
sendto_one_numeric(source_p, &me, ERR_TOOMANYWATCH, s + 1, ConfigGeneral.max_watch); |
105 |
continue; |
106 |
} |
107 |
|
108 |
if (valid_nickname(s + 1, true) == true) |
109 |
watch_add_to_hash_table(s + 1, source_p); |
110 |
} |
111 |
|
112 |
show_watch(source_p, s + 1, RPL_NOWON, RPL_NOWOFF); |
113 |
continue; |
114 |
} |
115 |
|
116 |
/* |
117 |
* Prefix of "-", coward wants to remove somebody from their |
118 |
* WATCH list. So do it. :-) |
119 |
*/ |
120 |
if (*s == '-') |
121 |
{ |
122 |
watch_del_from_hash_table(s + 1, source_p); |
123 |
show_watch(source_p, s + 1, RPL_WATCHOFF, RPL_WATCHOFF); |
124 |
continue; |
125 |
} |
126 |
|
127 |
/* |
128 |
* Fancy "C" or "c", they want to nuke their WATCH list and start |
129 |
* over, so be it. |
130 |
*/ |
131 |
if (*s == 'C' || *s == 'c') |
132 |
{ |
133 |
watch_del_watch_list(source_p); |
134 |
continue; |
135 |
} |
136 |
|
137 |
/* |
138 |
* Now comes the fun stuff, "S" or "s" returns a status report of |
139 |
* their WATCH list. I imagine this could be CPU intensive if |
140 |
* it's done alot, perhaps an auto-lag on this? |
141 |
*/ |
142 |
if (*s == 'S' || *s == 's') |
143 |
{ |
144 |
char buf[IRCD_BUFSIZE] = ""; |
145 |
unsigned int count = 0; |
146 |
|
147 |
if (list_requested & 0x1) |
148 |
continue; |
149 |
|
150 |
list_requested |= 0x1; |
151 |
|
152 |
/* |
153 |
* Send a list of how many users they have on their WATCH list |
154 |
* and how many WATCH lists they are on. |
155 |
*/ |
156 |
const struct Watch *watch; |
157 |
if ((watch = watch_find_hash(source_p->name))) |
158 |
count = dlink_list_length(&watch->watched_by); |
159 |
|
160 |
sendto_one_numeric(source_p, &me, RPL_WATCHSTAT, |
161 |
dlink_list_length(&source_p->connection->watches), count); |
162 |
|
163 |
/* |
164 |
* Send a list of everybody in their WATCH list. Be careful |
165 |
* not to buffer overflow. |
166 |
*/ |
167 |
if ((node = source_p->connection->watches.head) == NULL) |
168 |
{ |
169 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWATCHLIST, *s); |
170 |
continue; |
171 |
} |
172 |
|
173 |
watch = node->data; |
174 |
strlcpy(buf, watch->name, sizeof(buf)); |
175 |
|
176 |
count = strlen(source_p->name) + strlen(me.name) + 10 + strlen(buf); |
177 |
|
178 |
while ((node = node->next)) |
179 |
{ |
180 |
watch = node->data; |
181 |
|
182 |
if (count + strlen(watch->name) + 1 > IRCD_BUFSIZE - 2) |
183 |
{ |
184 |
sendto_one_numeric(source_p, &me, RPL_WATCHLIST, buf); |
185 |
buf[0] = '\0'; |
186 |
count = strlen(source_p->name) + strlen(me.name) + 10; |
187 |
} |
188 |
|
189 |
strlcat(buf, " ", sizeof(buf)); |
190 |
strlcat(buf, watch->name, sizeof(buf)); |
191 |
count += strlen(watch->name) + 1; |
192 |
} |
193 |
|
194 |
sendto_one_numeric(source_p, &me, RPL_WATCHLIST, buf); |
195 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWATCHLIST, *s); |
196 |
continue; |
197 |
} |
198 |
|
199 |
/* |
200 |
* Well that was fun, NOT. Now they want a list of everybody in |
201 |
* their WATCH list AND if they are online or offline? Sheesh, |
202 |
* greedy aren't we? |
203 |
*/ |
204 |
if (*s == 'L' || *s == 'l') |
205 |
{ |
206 |
if (list_requested & 0x2) |
207 |
continue; |
208 |
|
209 |
list_requested |= 0x2; |
210 |
|
211 |
DLINK_FOREACH(node, source_p->connection->watches.head) |
212 |
{ |
213 |
const struct Watch *watch = node->data; |
214 |
const struct Client *target_p; |
215 |
|
216 |
if ((target_p = find_person(source_p, watch->name))) |
217 |
sendto_one_numeric(source_p, &me, RPL_NOWON, |
218 |
target_p->name, target_p->username, |
219 |
target_p->host, target_p->tsinfo); |
220 |
|
221 |
/* |
222 |
* But actually, only show them offline if it's a capital |
223 |
* 'L' (full list wanted). |
224 |
*/ |
225 |
else if (*s == 'L') |
226 |
sendto_one_numeric(source_p, &me, RPL_NOWOFF, watch->name, |
227 |
"*", "*", watch->lasttime); |
228 |
} |
229 |
|
230 |
sendto_one_numeric(source_p, &me, RPL_ENDOFWATCHLIST, *s); |
231 |
continue; |
232 |
} |
233 |
|
234 |
/* Hmm.. unknown prefix character.. Ignore it. :-) */ |
235 |
} |
236 |
|
237 |
return 0; |
238 |
} |
239 |
|
240 |
static struct Message watch_msgtab = |
241 |
{ |
242 |
.cmd = "WATCH", |
243 |
.args_max = 1, |
244 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
245 |
.handlers[CLIENT_HANDLER] = m_watch, |
246 |
.handlers[SERVER_HANDLER] = m_ignore, |
247 |
.handlers[ENCAP_HANDLER] = m_ignore, |
248 |
.handlers[OPER_HANDLER] = m_watch |
249 |
}; |
250 |
|
251 |
static void |
252 |
module_init(void) |
253 |
{ |
254 |
mod_add_cmd(&watch_msgtab); |
255 |
isupport_add("WATCH", NULL, ConfigGeneral.max_watch); |
256 |
} |
257 |
|
258 |
static void |
259 |
module_exit(void) |
260 |
{ |
261 |
mod_del_cmd(&watch_msgtab); |
262 |
isupport_delete("WATCH"); |
263 |
} |
264 |
|
265 |
struct module module_entry = |
266 |
{ |
267 |
.version = "$Revision$", |
268 |
.modinit = module_init, |
269 |
.modexit = module_exit, |
270 |
}; |