]> jfr.im git - solanum.git/blame - modules/m_services.c
msg: remove last vestiges of the fakelag system. charybdis has never supported fakelag.
[solanum.git] / modules / m_services.c
CommitLineData
212380e3
AC
1/* modules/m_services.c
2 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
3 * Copyright (C) 2005 ircd-ratbox development team
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1.Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2.Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3.The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * $Id: m_services.c 1907 2006-08-29 19:18:15Z jilles $
30 */
31
32#include "stdinc.h"
33
212380e3
AC
34#include "send.h"
35#include "channel.h"
36#include "client.h"
37#include "common.h"
38#include "config.h"
39#include "ircd.h"
40#include "numeric.h"
212380e3
AC
41#include "s_conf.h"
42#include "s_newconf.h"
43#include "s_serv.h"
44#include "hash.h"
45#include "msg.h"
46#include "parse.h"
47#include "modules.h"
212380e3
AC
48#include "whowas.h"
49#include "monitor.h"
0b904d91 50#include "supported.h"
212380e3 51
c46a4ecd
KB
52static int _modinit(void);
53static void _moddeinit(void);
54
55static void mark_services(void);
56static void unmark_services(void);
57
428ca87b
AC
58static int me_su(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
59static int me_login(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
60static int me_rsfnc(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
61static int me_nickdelay(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
62
63static void h_svc_server_introduced(hook_data_client *);
64static void h_svc_whois(hook_data_client *);
65static void h_svc_stats(hook_data_int *);
c46a4ecd
KB
66static void h_svc_conf_read_start(void *);
67static void h_svc_conf_read_end(void *);
212380e3
AC
68
69struct Message su_msgtab = {
7baa37a9 70 "SU", 0, 0, 0, 0,
212380e3
AC
71 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore}
72};
73struct Message login_msgtab = {
7baa37a9 74 "LOGIN", 0, 0, 0, 0,
212380e3
AC
75 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore}
76};
77struct Message rsfnc_msgtab = {
7baa37a9 78 "RSFNC", 0, 0, 0, 0,
212380e3
AC
79 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore}
80};
81struct Message nickdelay_msgtab = {
7baa37a9 82 "NICKDELAY", 0, 0, 0, 0,
212380e3
AC
83 {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore}
84};
85
55abcbb2 86mapi_clist_av1 services_clist[] = {
212380e3
AC
87 &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL
88};
89mapi_hfn_list_av1 services_hfnlist[] = {
90 { "doing_stats", (hookfn) h_svc_stats },
91 { "doing_whois", (hookfn) h_svc_whois },
92 { "doing_whois_global", (hookfn) h_svc_whois },
93 { "server_introduced", (hookfn) h_svc_server_introduced },
c46a4ecd
KB
94 { "conf_read_start", (hookfn) h_svc_conf_read_start },
95 { "conf_read_end", (hookfn) h_svc_conf_read_end },
212380e3
AC
96 { NULL, NULL }
97};
98
c46a4ecd
KB
99DECLARE_MODULE_AV1(services, _modinit, _moddeinit, services_clist, NULL, services_hfnlist, "$Revision: 1907 $");
100
101static int
102_modinit(void)
103{
104 mark_services();
0b904d91 105 add_isupport("FNC", isupport_string, "");
8a4b8377 106 return 0;
c46a4ecd
KB
107}
108
109static void
110_moddeinit(void)
111{
0b904d91 112 delete_isupport("FNC");
c46a4ecd
KB
113 unmark_services();
114}
212380e3
AC
115
116static int
428ca87b 117me_su(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
118 int parc, const char *parv[])
119{
120 struct Client *target_p;
121
122 if(!(source_p->flags & FLAGS_SERVICE))
ec57fe67
KB
123 {
124 sendto_realops_snomask(SNO_GENERAL, L_ALL,
125 "Non-service server %s attempting to execute services-only command SU", source_p->name);
212380e3 126 return 0;
ec57fe67 127 }
212380e3
AC
128
129 if((target_p = find_client(parv[1])) == NULL)
130 return 0;
131
132 if(!target_p->user)
133 return 0;
134
135 if(EmptyString(parv[2]))
136 target_p->user->suser[0] = '\0';
137 else
f427c8b0 138 rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser));
212380e3 139
583f064f 140 sendto_common_channels_local_butone(target_p, CLICAP_ACCOUNT_NOTIFY, NOCAPS, ":%s!%s@%s ACCOUNT %s",
7a7f86d3
AC
141 target_p->name, target_p->username, target_p->host,
142 EmptyString(target_p->user->suser) ? "*" : target_p->user->suser);
143
212380e3
AC
144 invalidate_bancache_user(target_p);
145
146 return 0;
147}
148
149static int
428ca87b 150me_login(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
151 int parc, const char *parv[])
152{
153 if(!IsPerson(source_p))
154 return 0;
155
f427c8b0 156 rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser));
212380e3
AC
157 return 0;
158}
159
212380e3 160static int
428ca87b 161me_rsfnc(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
162 int parc, const char *parv[])
163{
164 struct Client *target_p;
165 struct Client *exist_p;
166 time_t newts, curts;
75d60088 167 char note[NICKLEN + 10];
212380e3
AC
168
169 if(!(source_p->flags & FLAGS_SERVICE))
ec57fe67
KB
170 {
171 sendto_realops_snomask(SNO_GENERAL, L_ALL,
172 "Non-service server %s attempting to execute services-only command RSFNC", source_p->name);
212380e3 173 return 0;
ec57fe67 174 }
212380e3
AC
175
176 if((target_p = find_person(parv[1])) == NULL)
177 return 0;
178
179 if(!MyClient(target_p))
180 return 0;
181
2d28539c 182 if(!clean_nick(parv[2], 0) || IsDigit(parv[2][0]))
212380e3
AC
183 return 0;
184
185 curts = atol(parv[4]);
186
187 /* if tsinfo is different from what it was when services issued the
188 * RSFNC, then we ignore it. This can happen when a client changes
189 * nicknames before the RSFNC arrives.. --anfl
190 */
191 if(target_p->tsinfo != curts)
192 return 0;
193
194 if((exist_p = find_named_client(parv[2])))
195 {
196 char buf[BUFSIZE];
197
198 /* this would be one hell of a race condition to trigger
55abcbb2 199 * this one given the tsinfo check above, but its here for
212380e3
AC
200 * safety --anfl
201 */
202 if(target_p == exist_p)
03510227 203 goto doit;
212380e3
AC
204
205 if(MyClient(exist_p))
206 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
207 me.name, exist_p->name);
208
209 exist_p->flags |= FLAGS_KILLED;
210 /* Do not send kills to servers for unknowns -- jilles */
211 if(IsClient(exist_p))
45ed8835 212 {
212380e3
AC
213 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
214 me.name);
45ed8835
JT
215 sendto_realops_snomask(SNO_SKILL, L_ALL,
216 "Nick collision due to services forced nick change on %s",
217 parv[2]);
218 }
212380e3 219
5203cba5 220 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
212380e3
AC
221 me.name);
222 exit_client(NULL, exist_p, &me, buf);
223 }
224
03510227 225doit:
212380e3
AC
226 newts = atol(parv[3]);
227
228 /* timestamp is older than 15mins, ignore it */
e3354945
VY
229 if(newts < (rb_current_time() - 900))
230 newts = rb_current_time() - 900;
212380e3
AC
231
232 target_p->tsinfo = newts;
233
234 monitor_signoff(target_p);
235
236 invalidate_bancache_user(target_p);
237
238 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
239 "Nick change: From %s to %s [%s@%s]",
240 target_p->name, parv[2], target_p->username,
241 target_p->host);
242
583f064f 243 sendto_common_channels_local(target_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s",
212380e3
AC
244 target_p->name, target_p->username,
245 target_p->host, parv[2]);
246
b47f8a4f 247 whowas_add_history(target_p, 1);
212380e3
AC
248 sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
249 use_id(target_p), parv[2], (long) target_p->tsinfo);
212380e3
AC
250
251 del_from_client_hash(target_p->name, target_p);
e2606551 252 rb_strlcpy(target_p->name, parv[2], NICKLEN);
212380e3
AC
253 add_to_client_hash(target_p->name, target_p);
254
255 monitor_signon(target_p);
256
257 del_all_accepts(target_p);
258
5203cba5 259 snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name);
75d60088 260 rb_note(target_p->localClient->F, note);
212380e3
AC
261 return 0;
262}
263
264/*
265** me_nickdelay
212380e3
AC
266** parv[1] = duration in seconds (0 to remove)
267** parv[2] = nick
268*/
269static int
428ca87b 270me_nickdelay(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
271{
272 int duration;
273 struct nd_entry *nd;
274
275 if(!(source_p->flags & FLAGS_SERVICE))
ec57fe67
KB
276 {
277 sendto_realops_snomask(SNO_GENERAL, L_ALL,
278 "Non-service server %s attempting to execute services-only command NICKDELAY", source_p->name);
212380e3 279 return 0;
ec57fe67 280 }
212380e3
AC
281
282 duration = atoi(parv[1]);
283 if (duration <= 0)
284 {
b37021a4 285 nd = irc_dictionary_retrieve(nd_dict, parv[2]);
212380e3
AC
286 if (nd != NULL)
287 free_nd_entry(nd);
288 }
289 else
290 {
291 if (duration > 86400)
292 duration = 86400;
293 add_nd_entry(parv[2]);
b37021a4 294 nd = irc_dictionary_retrieve(nd_dict, parv[2]);
212380e3 295 if (nd != NULL)
e3354945 296 nd->expire = rb_current_time() + duration;
212380e3
AC
297 }
298
299 return 0;
300}
301
302static void
303h_svc_server_introduced(hook_data_client *hdata)
304{
5b96d9a6 305 rb_dlink_node *ptr;
212380e3 306
5b96d9a6 307 RB_DLINK_FOREACH(ptr, service_list.head)
212380e3
AC
308 {
309 if(!irccmp((const char *) ptr->data, hdata->target->name))
310 {
311 hdata->target->flags |= FLAGS_SERVICE;
312 return;
313 }
314 }
315}
316
317static void
318h_svc_whois(hook_data_client *data)
319{
320 char *p = data->target->user->suser;
321 if(!EmptyString(p))
322 {
323 /* Try to strip off any leading digits as this may be used to
324 * store both an ID number and an account name in one field.
325 * If only digits are present, leave as is.
326 */
327 while(IsDigit(*p))
328 p++;
329 if(*p == '\0')
330 p = data->target->user->suser;
331
5b383ce0
JT
332 sendto_one_numeric(data->client, RPL_WHOISLOGGEDIN,
333 form_str(RPL_WHOISLOGGEDIN),
212380e3
AC
334 data->target->name, p);
335 }
336}
337
338static void
339h_svc_stats(hook_data_int *data)
340{
341 char statchar = (char) data->arg2;
5b96d9a6 342 rb_dlink_node *ptr;
212380e3
AC
343
344 if (statchar == 'U' && IsOper(data->client))
345 {
5b96d9a6 346 RB_DLINK_FOREACH(ptr, service_list.head)
212380e3
AC
347 {
348 sendto_one_numeric(data->client, RPL_STATSULINE,
349 form_str(RPL_STATSULINE),
0cce01d3 350 (const char *)ptr->data, "*", "*", "s");
212380e3
AC
351 }
352 }
353}
c46a4ecd
KB
354
355static void
356h_svc_conf_read_start(void *dummy)
357{
358 unmark_services();
359}
360
361static void
362unmark_services(void)
363{
364 struct Client *target_p;
365 rb_dlink_node *ptr;
366
367 RB_DLINK_FOREACH(ptr, global_serv_list.head)
368 {
369 target_p = ptr->data;
370
371 target_p->flags &= ~FLAGS_SERVICE;
372 }
373}
374
375static void
376h_svc_conf_read_end(void *dummy)
377{
378 mark_services();
379}
380
381static void
382mark_services(void)
383{
384 struct Client *target_p;
385 rb_dlink_node *ptr;
386
387 RB_DLINK_FOREACH(ptr, service_list.head)
388 {
389 target_p = find_server(NULL, (const char *)ptr->data);
390
391 if (target_p)
392 target_p->flags |= FLAGS_SERVICE;
393 }
394}