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