ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_watch.c
Revision: 8752
Committed: Tue Jan 1 11:07:01 2019 UTC (7 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 7586 byte(s)
Log Message:
- Update copyright years

File Contents

# User Rev Content
1 michael 876 /*
2 michael 2820 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 michael 876 *
4 michael 2820 * Copyright (c) 1997 Jukka Santala (Donwulff)
5 michael 8752 * Copyright (c) 2000-2019 ircd-hybrid development team
6 michael 876 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 michael 876 * USA
21     */
22    
23 michael 2820 /*! \file m_watch.c
24     * \brief Includes required functions for processing the WATCH command.
25     * \version $Id$
26     */
27    
28 michael 876 #include "stdinc.h"
29     #include "client.h"
30     #include "irc_string.h"
31     #include "ircd.h"
32     #include "numeric.h"
33 michael 1309 #include "conf.h"
34 michael 876 #include "send.h"
35     #include "parse.h"
36     #include "modules.h"
37 michael 3347 #include "user.h"
38 michael 876 #include "watch.h"
39 michael 6185 #include "isupport.h"
40 michael 876
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 michael 3156 show_watch(struct Client *source_p, const char *name,
50 michael 3109 enum irc_numerics rpl1, enum irc_numerics rpl2)
51 michael 876 {
52 michael 8437 const struct Client *target_p;
53 michael 876
54 michael 3156 if ((target_p = find_person(source_p, name)))
55     sendto_one_numeric(source_p, &me, rpl1,
56 michael 3109 target_p->name, target_p->username,
57     target_p->host, target_p->tsinfo);
58 michael 876 else
59 michael 3156 sendto_one_numeric(source_p, &me, rpl2, name, "*", "*", 0);
60 michael 876 }
61    
62 michael 3294 /*! \brief WATCH command handler
63 michael 876 *
64 michael 3294 * \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 michael 876 */
73 michael 2820 static int
74 michael 3156 m_watch(struct Client *source_p, int parc, char *parv[])
75 michael 876 {
76 michael 8437 dlink_node *node;
77 michael 876 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 michael 8733 for (const char *s = strtok_r(parv[1], ", ", &p); s;
88     s = strtok_r(NULL, ", ", &p))
89 michael 876 {
90 michael 8437 char *user;
91 michael 876 if ((user = strchr(s, '!')))
92 michael 7085 *user = '\0'; /* Not used */
93 michael 876
94     /*
95 michael 7084 * Prefix of "+", they want to add a name to their WATCH list.
96 michael 876 */
97     if (*s == '+')
98     {
99 michael 3368 if (*(s + 1))
100 michael 876 {
101 michael 4588 if (dlink_list_length(&source_p->connection->watches) >=
102 michael 4340 ConfigGeneral.max_watch)
103 michael 876 {
104 michael 4340 sendto_one_numeric(source_p, &me, ERR_TOOMANYWATCH, s + 1, ConfigGeneral.max_watch);
105 michael 876 continue;
106     }
107    
108 michael 8660 if (valid_nickname(s + 1, true) == true)
109 michael 3424 watch_add_to_hash_table(s + 1, source_p);
110 michael 876 }
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 michael 3294 * their WATCH list. I imagine this could be CPU intensive if
140 michael 876 * it's done alot, perhaps an auto-lag on this?
141     */
142     if (*s == 'S' || *s == 's')
143     {
144 michael 3213 char buf[IRCD_BUFSIZE] = "";
145 michael 876 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 michael 8437 const struct Watch *watch;
157 michael 4815 if ((watch = watch_find_hash(source_p->name)))
158     count = dlink_list_length(&watch->watched_by);
159 michael 876
160 michael 3109 sendto_one_numeric(source_p, &me, RPL_WATCHSTAT,
161 michael 4588 dlink_list_length(&source_p->connection->watches), count);
162 michael 876
163     /*
164 michael 3294 * Send a list of everybody in their WATCH list. Be careful
165 michael 876 * not to buffer overflow.
166     */
167 michael 4815 if ((node = source_p->connection->watches.head) == NULL)
168 michael 876 {
169 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFWATCHLIST, *s);
170 michael 876 continue;
171     }
172    
173 michael 4815 watch = node->data;
174 michael 7084 strlcpy(buf, watch->name, sizeof(buf));
175 michael 876
176 michael 8437 count = strlen(source_p->name) + strlen(me.name) + 10 + strlen(buf);
177 michael 876
178 michael 4815 while ((node = node->next))
179 michael 876 {
180 michael 4815 watch = node->data;
181 michael 876
182 michael 7084 if (count + strlen(watch->name) + 1 > IRCD_BUFSIZE - 2)
183 michael 876 {
184 michael 3109 sendto_one_numeric(source_p, &me, RPL_WATCHLIST, buf);
185 michael 876 buf[0] = '\0';
186     count = strlen(source_p->name) + strlen(me.name) + 10;
187     }
188    
189 michael 3213 strlcat(buf, " ", sizeof(buf));
190 michael 7084 strlcat(buf, watch->name, sizeof(buf));
191 michael 8437 count += strlen(watch->name) + 1;
192 michael 876 }
193    
194 michael 3109 sendto_one_numeric(source_p, &me, RPL_WATCHLIST, buf);
195     sendto_one_numeric(source_p, &me, RPL_ENDOFWATCHLIST, *s);
196 michael 876 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 michael 4815 DLINK_FOREACH(node, source_p->connection->watches.head)
212 michael 876 {
213 michael 4815 const struct Watch *watch = node->data;
214 michael 8437 const struct Client *target_p;
215 michael 876
216 michael 7084 if ((target_p = find_person(source_p, watch->name)))
217 michael 3109 sendto_one_numeric(source_p, &me, RPL_NOWON,
218     target_p->name, target_p->username,
219     target_p->host, target_p->tsinfo);
220    
221 michael 876 /*
222     * But actually, only show them offline if it's a capital
223     * 'L' (full list wanted).
224     */
225     else if (*s == 'L')
226 michael 7084 sendto_one_numeric(source_p, &me, RPL_NOWOFF, watch->name,
227 michael 4815 "*", "*", watch->lasttime);
228 michael 876 }
229    
230 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFWATCHLIST, *s);
231 michael 876 continue;
232     }
233    
234     /* Hmm.. unknown prefix character.. Ignore it. :-) */
235     }
236 michael 2820
237     return 0;
238 michael 876 }
239 michael 1230
240 michael 2820 static struct Message watch_msgtab =
241     {
242 michael 5881 .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 michael 1230 };
250    
251     static void
252     module_init(void)
253     {
254     mod_add_cmd(&watch_msgtab);
255 michael 6185 isupport_add("WATCH", NULL, ConfigGeneral.max_watch);
256 michael 1230 }
257    
258     static void
259     module_exit(void)
260     {
261     mod_del_cmd(&watch_msgtab);
262 michael 6185 isupport_delete("WATCH");
263 michael 1230 }
264    
265 michael 2820 struct module module_entry =
266     {
267 michael 1230 .version = "$Revision$",
268     .modinit = module_init,
269     .modexit = module_exit,
270     };

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision