]> jfr.im git - solanum.git/blame - modules/m_services.c
check bans and quiets for cmode -n/nonmember PRIVMSG
[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.
212380e3
AC
28 */
29
30#include "stdinc.h"
31
212380e3
AC
32#include "send.h"
33#include "channel.h"
34#include "client.h"
9b8e9eb3 35#include "defaults.h"
212380e3
AC
36#include "ircd.h"
37#include "numeric.h"
212380e3
AC
38#include "s_conf.h"
39#include "s_newconf.h"
40#include "s_serv.h"
41#include "hash.h"
42#include "msg.h"
43#include "parse.h"
44#include "modules.h"
212380e3
AC
45#include "whowas.h"
46#include "monitor.h"
0b904d91 47#include "supported.h"
212380e3 48
eeabf33a
EM
49static const char services_desc[] = "Provides support for running a services daemon";
50
c46a4ecd
KB
51static int _modinit(void);
52static void _moddeinit(void);
53
54static void mark_services(void);
55static void unmark_services(void);
56
3c7d6fcc
EM
57static void me_su(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
58static void me_login(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
59static void me_rsfnc(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
60static void me_nickdelay(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3 61
82436efb
EM
62static void h_svc_server_introduced(void *);
63static void h_svc_whois(void *);
64static void h_svc_stats(void *);
c46a4ecd
KB
65static void h_svc_conf_read_start(void *);
66static void h_svc_conf_read_end(void *);
212380e3
AC
67
68struct Message su_msgtab = {
7baa37a9 69 "SU", 0, 0, 0, 0,
212380e3
AC
70 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore}
71};
72struct Message login_msgtab = {
7baa37a9 73 "LOGIN", 0, 0, 0, 0,
212380e3
AC
74 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore}
75};
76struct Message rsfnc_msgtab = {
7baa37a9 77 "RSFNC", 0, 0, 0, 0,
212380e3
AC
78 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore}
79};
80struct Message nickdelay_msgtab = {
7baa37a9 81 "NICKDELAY", 0, 0, 0, 0,
212380e3
AC
82 {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore}
83};
84
55abcbb2 85mapi_clist_av1 services_clist[] = {
212380e3
AC
86 &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL
87};
88mapi_hfn_list_av1 services_hfnlist[] = {
82436efb
EM
89 { "doing_stats", h_svc_stats },
90 { "doing_whois", h_svc_whois },
91 { "doing_whois_global", h_svc_whois },
92 { "server_introduced", h_svc_server_introduced },
93 { "conf_read_start", h_svc_conf_read_start },
94 { "conf_read_end", h_svc_conf_read_end },
212380e3
AC
95 { NULL, NULL }
96};
97
3abc337f 98DECLARE_MODULE_AV2(services, _modinit, _moddeinit, services_clist, NULL, services_hfnlist, NULL, NULL, services_desc);
c46a4ecd
KB
99
100static int
101_modinit(void)
102{
103 mark_services();
0b904d91 104 add_isupport("FNC", isupport_string, "");
8a4b8377 105 return 0;
c46a4ecd
KB
106}
107
108static void
109_moddeinit(void)
110{
0b904d91 111 delete_isupport("FNC");
c46a4ecd
KB
112 unmark_services();
113}
212380e3 114
3c7d6fcc 115static void
428ca87b 116me_su(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
117 int parc, const char *parv[])
118{
119 struct Client *target_p;
120
121 if(!(source_p->flags & FLAGS_SERVICE))
ec57fe67
KB
122 {
123 sendto_realops_snomask(SNO_GENERAL, L_ALL,
124 "Non-service server %s attempting to execute services-only command SU", source_p->name);
3c7d6fcc 125 return;
ec57fe67 126 }
212380e3
AC
127
128 if((target_p = find_client(parv[1])) == NULL)
3c7d6fcc 129 return;
212380e3
AC
130
131 if(!target_p->user)
3c7d6fcc 132 return;
212380e3
AC
133
134 if(EmptyString(parv[2]))
135 target_p->user->suser[0] = '\0';
136 else
f427c8b0 137 rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser));
212380e3 138
5a261597 139 sendto_common_channels_local(target_p, CLICAP_ACCOUNT_NOTIFY, NOCAPS, ":%s!%s@%s ACCOUNT %s",
7a7f86d3
AC
140 target_p->name, target_p->username, target_p->host,
141 EmptyString(target_p->user->suser) ? "*" : target_p->user->suser);
142
8e3239be
JP
143 if (MyClient(target_p))
144 {
145 if (EmptyString(target_p->user->suser))
146 sendto_one(target_p, form_str(RPL_LOGGEDOUT), me.name, target_p->name,
147 target_p->name, target_p->username, target_p->host);
148 else
149 sendto_one(target_p, form_str(RPL_LOGGEDIN), me.name, target_p->name,
150 target_p->name, target_p->username, target_p->host, parv[2], parv[2]);
151 }
152
212380e3 153 invalidate_bancache_user(target_p);
212380e3
AC
154}
155
3c7d6fcc 156static void
428ca87b 157me_login(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
158 int parc, const char *parv[])
159{
160 if(!IsPerson(source_p))
3c7d6fcc 161 return;
212380e3 162
f427c8b0 163 rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser));
212380e3
AC
164}
165
c33da0d2
JP
166/* me_rsfnc()
167 * parv[1] = current user nickname
168 * parv[2] = target nickname
169 * parv[3] = new nickts
170 * parv[4] = current nickts
171 * parv[5] = optional; 0 (don't override RESVs) or 1 (override RESVs)
172 */
3c7d6fcc 173static void
428ca87b 174me_rsfnc(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
175 int parc, const char *parv[])
176{
177 struct Client *target_p;
178 struct Client *exist_p;
179 time_t newts, curts;
52f1947f 180 struct nd_entry *nd;
fe5fc851 181 char note[NAMELEN + 10];
212380e3
AC
182
183 if(!(source_p->flags & FLAGS_SERVICE))
ec57fe67
KB
184 {
185 sendto_realops_snomask(SNO_GENERAL, L_ALL,
186 "Non-service server %s attempting to execute services-only command RSFNC", source_p->name);
3c7d6fcc 187 return;
ec57fe67 188 }
212380e3
AC
189
190 if((target_p = find_person(parv[1])) == NULL)
3c7d6fcc 191 return;
212380e3
AC
192
193 if(!MyClient(target_p))
3c7d6fcc 194 return;
212380e3 195
2d28539c 196 if(!clean_nick(parv[2], 0) || IsDigit(parv[2][0]))
3c7d6fcc 197 return;
212380e3
AC
198
199 curts = atol(parv[4]);
200
201 /* if tsinfo is different from what it was when services issued the
202 * RSFNC, then we ignore it. This can happen when a client changes
203 * nicknames before the RSFNC arrives.. --anfl
204 */
205 if(target_p->tsinfo != curts)
3c7d6fcc 206 return;
212380e3 207
c33da0d2
JP
208 /* received a non-forced RSFNC for a nickname that is RESV.
209 * silently ignore it. ~jess
210 */
211 if(parc > 5 && atoi(parv[5]) == 0 && find_nick_resv(parv[2]))
212 return;
213
212380e3
AC
214 if((exist_p = find_named_client(parv[2])))
215 {
216 char buf[BUFSIZE];
217
218 /* this would be one hell of a race condition to trigger
55abcbb2 219 * this one given the tsinfo check above, but its here for
212380e3
AC
220 * safety --anfl
221 */
222 if(target_p == exist_p)
03510227 223 goto doit;
212380e3
AC
224
225 if(MyClient(exist_p))
226 sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)",
227 me.name, exist_p->name);
228
229 exist_p->flags |= FLAGS_KILLED;
230 /* Do not send kills to servers for unknowns -- jilles */
231 if(IsClient(exist_p))
45ed8835 232 {
212380e3
AC
233 kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)",
234 me.name);
45ed8835
JT
235 sendto_realops_snomask(SNO_SKILL, L_ALL,
236 "Nick collision due to services forced nick change on %s",
237 parv[2]);
238 }
212380e3 239
5203cba5 240 snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))",
212380e3
AC
241 me.name);
242 exit_client(NULL, exist_p, &me, buf);
243 }
244
03510227 245doit:
212380e3
AC
246 newts = atol(parv[3]);
247
248 /* timestamp is older than 15mins, ignore it */
e3354945
VY
249 if(newts < (rb_current_time() - 900))
250 newts = rb_current_time() - 900;
212380e3
AC
251
252 target_p->tsinfo = newts;
253
254 monitor_signoff(target_p);
255
256 invalidate_bancache_user(target_p);
257
258 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
259 "Nick change: From %s to %s [%s@%s]",
260 target_p->name, parv[2], target_p->username,
261 target_p->host);
262
583f064f 263 sendto_common_channels_local(target_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s",
212380e3
AC
264 target_p->name, target_p->username,
265 target_p->host, parv[2]);
266
b47f8a4f 267 whowas_add_history(target_p, 1);
212380e3
AC
268 sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld",
269 use_id(target_p), parv[2], (long) target_p->tsinfo);
212380e3
AC
270
271 del_from_client_hash(target_p->name, target_p);
52f1947f
DF
272
273 /* invalidate nick delay because we're forcing this nick to be used */
274 nd = rb_dictionary_retrieve(nd_dict, parv[2]);
275 if (nd != NULL)
276 free_nd_entry(nd);
277
e2606551 278 rb_strlcpy(target_p->name, parv[2], NICKLEN);
212380e3
AC
279 add_to_client_hash(target_p->name, target_p);
280
281 monitor_signon(target_p);
282
c1b01bf5
DF
283 /* Make sure everyone that has this client on its accept list
284 * loses that reference.
285 */
286 del_all_accepts(target_p, false);
212380e3 287
fe5fc851 288 snprintf(note, sizeof(note), "Nick: %s", target_p->name);
75d60088 289 rb_note(target_p->localClient->F, note);
212380e3
AC
290}
291
292/*
293** me_nickdelay
212380e3
AC
294** parv[1] = duration in seconds (0 to remove)
295** parv[2] = nick
296*/
3c7d6fcc 297static void
428ca87b 298me_nickdelay(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
299{
300 int duration;
301 struct nd_entry *nd;
302
303 if(!(source_p->flags & FLAGS_SERVICE))
ec57fe67
KB
304 {
305 sendto_realops_snomask(SNO_GENERAL, L_ALL,
306 "Non-service server %s attempting to execute services-only command NICKDELAY", source_p->name);
3c7d6fcc 307 return;
ec57fe67 308 }
212380e3
AC
309
310 duration = atoi(parv[1]);
311 if (duration <= 0)
312 {
a4bf26dd 313 nd = rb_dictionary_retrieve(nd_dict, parv[2]);
212380e3
AC
314 if (nd != NULL)
315 free_nd_entry(nd);
316 }
317 else
318 {
319 if (duration > 86400)
320 duration = 86400;
321 add_nd_entry(parv[2]);
a4bf26dd 322 nd = rb_dictionary_retrieve(nd_dict, parv[2]);
212380e3 323 if (nd != NULL)
e3354945 324 nd->expire = rb_current_time() + duration;
212380e3 325 }
212380e3
AC
326}
327
328static void
82436efb 329h_svc_server_introduced(void *data)
212380e3 330{
82436efb 331 hook_data_client *hdata = data;
5b96d9a6 332 rb_dlink_node *ptr;
212380e3 333
5b96d9a6 334 RB_DLINK_FOREACH(ptr, service_list.head)
212380e3
AC
335 {
336 if(!irccmp((const char *) ptr->data, hdata->target->name))
337 {
338 hdata->target->flags |= FLAGS_SERVICE;
339 return;
340 }
341 }
342}
343
344static void
82436efb 345h_svc_whois(void *data_)
212380e3 346{
82436efb 347 hook_data_client *data = data_;
212380e3
AC
348 char *p = data->target->user->suser;
349 if(!EmptyString(p))
350 {
351 /* Try to strip off any leading digits as this may be used to
352 * store both an ID number and an account name in one field.
353 * If only digits are present, leave as is.
354 */
355 while(IsDigit(*p))
356 p++;
357 if(*p == '\0')
358 p = data->target->user->suser;
359
5b383ce0
JT
360 sendto_one_numeric(data->client, RPL_WHOISLOGGEDIN,
361 form_str(RPL_WHOISLOGGEDIN),
212380e3
AC
362 data->target->name, p);
363 }
364}
365
366static void
82436efb 367h_svc_stats(void *data_)
212380e3 368{
82436efb 369 hook_data_int *data = data_;
212380e3 370 char statchar = (char) data->arg2;
5b96d9a6 371 rb_dlink_node *ptr;
212380e3 372
d4f7eb4c 373 if (statchar == 'U' && IsOperGeneral(data->client))
212380e3 374 {
5b96d9a6 375 RB_DLINK_FOREACH(ptr, service_list.head)
212380e3
AC
376 {
377 sendto_one_numeric(data->client, RPL_STATSULINE,
378 form_str(RPL_STATSULINE),
0cce01d3 379 (const char *)ptr->data, "*", "*", "s");
212380e3
AC
380 }
381 }
382}
c46a4ecd
KB
383
384static void
385h_svc_conf_read_start(void *dummy)
386{
387 unmark_services();
388}
389
390static void
391unmark_services(void)
392{
393 struct Client *target_p;
394 rb_dlink_node *ptr;
395
396 RB_DLINK_FOREACH(ptr, global_serv_list.head)
397 {
398 target_p = ptr->data;
399
400 target_p->flags &= ~FLAGS_SERVICE;
401 }
402}
403
404static void
405h_svc_conf_read_end(void *dummy)
406{
407 mark_services();
408}
409
410static void
411mark_services(void)
412{
413 struct Client *target_p;
414 rb_dlink_node *ptr;
415
416 RB_DLINK_FOREACH(ptr, service_list.head)
417 {
418 target_p = find_server(NULL, (const char *)ptr->data);
419
420 if (target_p)
421 target_p->flags |= FLAGS_SERVICE;
422 }
423}