]> jfr.im git - solanum.git/blob - modules/m_resv.c
Fix erroneous comment
[solanum.git] / modules / m_resv.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_resv.c: Reserves(jupes) a nickname or channel.
4 *
5 * Copyright (C) 2001-2002 Hybrid Development Team
6 * Copyright (C) 2002-2005 ircd-ratbox development team
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 * $Id$
24 */
25
26 #include "stdinc.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "ircd.h"
30 #include "numeric.h"
31 #include "s_serv.h"
32 #include "send.h"
33 #include "msg.h"
34 #include "parse.h"
35 #include "modules.h"
36 #include "s_conf.h"
37 #include "s_newconf.h"
38 #include "hash.h"
39 #include "logger.h"
40 #include "bandbi.h"
41 #include "operhash.h"
42
43 static int mo_resv(struct Client *, struct Client *, int, const char **);
44 static int ms_resv(struct Client *, struct Client *, int, const char **);
45 static int me_resv(struct Client *, struct Client *, int, const char **);
46 static int mo_unresv(struct Client *, struct Client *, int, const char **);
47 static int ms_unresv(struct Client *, struct Client *, int, const char **);
48 static int me_unresv(struct Client *, struct Client *, int, const char **);
49
50 struct Message resv_msgtab = {
51 "RESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
52 {mg_ignore, mg_not_oper, {ms_resv, 4}, {ms_resv, 4}, {me_resv, 5}, {mo_resv, 3}}
53 };
54
55 struct Message unresv_msgtab = {
56 "UNRESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
57 {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
58 };
59
60 mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
61
62 DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision$");
63
64 static void parse_resv(struct Client *source_p, const char *name,
65 const char *reason, int temp_time, int propagated);
66 static void propagate_resv(struct Client *source_p, const char *target,
67 int temp_time, const char *name, const char *reason);
68 static void cluster_resv(struct Client *source_p, int temp_time,
69 const char *name, const char *reason);
70
71 static void handle_remote_unresv(struct Client *source_p, const char *name);
72 static void remove_resv(struct Client *source_p, const char *name, int propagated);
73
74 /*
75 * mo_resv()
76 * parv[1] = channel/nick to forbid
77 * parv[2] = reason
78 */
79 static int
80 mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
81 {
82 const char *name;
83 const char *reason;
84 const char *target_server = NULL;
85 int temp_time;
86 int loc = 1;
87 int propagated = ConfigFileEntry.use_propagated_bans;
88
89 if(!IsOperResv(source_p))
90 {
91 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
92 return 0;
93 }
94
95 /* RESV [time] <name> [ON <server>] :<reason> */
96
97 if((temp_time = valid_temp_time(parv[loc])) >= 0)
98 loc++;
99 /* we just set temp_time to -1! */
100 else
101 temp_time = 0;
102
103 name = parv[loc];
104 loc++;
105
106 if((parc >= loc + 2) && (irccmp(parv[loc], "ON") == 0))
107 {
108 if(!IsOperRemoteBan(source_p))
109 {
110 sendto_one(source_p, form_str(ERR_NOPRIVS),
111 me.name, source_p->name, "remoteban");
112 return 0;
113 }
114
115 target_server = parv[loc + 1];
116 loc += 2;
117
118 /* Set as local-only. */
119 propagated = 0;
120 }
121
122 if(parc <= loc || EmptyString(parv[loc]))
123 {
124 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "RESV");
125 return 0;
126 }
127
128 reason = parv[loc];
129
130 /* remote resv.. */
131 if(target_server)
132 {
133 propagate_resv(source_p, target_server, temp_time, name, reason);
134
135 if(match(target_server, me.name) == 0)
136 return 0;
137 }
138 else if(!propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
139 cluster_resv(source_p, temp_time, name, reason);
140
141 if(propagated && temp_time == 0)
142 {
143 sendto_one_notice(source_p, ":Cannot set a permanent global ban");
144 return 0;
145 }
146
147 parse_resv(source_p, name, reason, temp_time, propagated);
148
149 return 0;
150 }
151
152 /* ms_resv()
153 * parv[1] = target server
154 * parv[2] = channel/nick to forbid
155 * parv[3] = reason
156 */
157 static int
158 ms_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
159 {
160 /* parv[0] parv[1] parv[2] parv[3]
161 * oper target server resv reason
162 */
163 propagate_resv(source_p, parv[1], 0, parv[2], parv[3]);
164
165 if(!match(parv[1], me.name))
166 return 0;
167
168 if(!IsPerson(source_p))
169 return 0;
170
171 parse_resv(source_p, parv[2], parv[3], 0, 0);
172 return 0;
173 }
174
175 static int
176 me_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
177 {
178 /* time name 0 :reason */
179 if(!IsPerson(source_p))
180 return 0;
181
182 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]), 0);
183 return 0;
184 }
185
186 /* parse_resv()
187 *
188 * inputs - source_p if error messages wanted
189 * - thing to resv
190 * - reason for resv
191 * outputs -
192 * side effects - will parse the resv and create it if valid
193 */
194 static void
195 parse_resv(struct Client *source_p, const char *name, const char *reason, int temp_time, int propagated)
196 {
197 struct ConfItem *aconf;
198
199 if(!MyClient(source_p) &&
200 !find_shared_conf(source_p->username, source_p->host,
201 source_p->servptr->name,
202 (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
203 return;
204
205 if(IsChannelName(name))
206 {
207 if(hash_find_resv(name))
208 {
209 sendto_one_notice(source_p,
210 ":A RESV has already been placed on channel: %s", name);
211 return;
212 }
213
214 if(strlen(name) > CHANNELLEN)
215 {
216 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
217 return;
218 }
219
220 aconf = make_conf();
221 aconf->status = CONF_RESV_CHANNEL;
222 aconf->port = 0;
223 aconf->created = rb_current_time();
224 aconf->host = rb_strdup(name);
225 aconf->passwd = rb_strdup(reason);
226 aconf->info.oper = operhash_add(get_oper_name(source_p));
227
228 if(propagated)
229 {
230 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
231 aconf->hold = rb_current_time() + temp_time;
232 aconf->lifetime = aconf->hold;
233 replace_old_ban(aconf);
234 rb_dlinkAddAlloc(aconf, &prop_bans);
235
236 sendto_realops_snomask(SNO_GENERAL, L_ALL,
237 "%s added global %d min. RESV for [%s] [%s]",
238 get_oper_name(source_p), temp_time / 60,
239 name, reason);
240 ilog(L_KLINE, "R %s %d %s %s",
241 get_oper_name(source_p), temp_time / 60, name, reason);
242 sendto_one_notice(source_p, ":Added global %d min. RESV [%s]",
243 temp_time / 60, name);
244 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
245 ":%s BAN R * %s %lu %d %d * :%s",
246 source_p->id, aconf->host,
247 (unsigned long)aconf->created,
248 (int)(aconf->hold - aconf->created),
249 (int)(aconf->lifetime - aconf->created),
250 reason);
251 }
252 else if(temp_time > 0)
253 {
254 aconf->hold = rb_current_time() + temp_time;
255
256 sendto_realops_snomask(SNO_GENERAL, L_ALL,
257 "%s added temporary %d min. RESV for [%s] [%s]",
258 get_oper_name(source_p), temp_time / 60,
259 name, reason);
260 ilog(L_KLINE, "R %s %d %s %s",
261 get_oper_name(source_p), temp_time / 60, name, reason);
262 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
263 temp_time / 60, name);
264 }
265 else
266 {
267 sendto_realops_snomask(SNO_GENERAL, L_ALL,
268 "%s added RESV for [%s] [%s]",
269 get_oper_name(source_p), name, reason);
270 ilog(L_KLINE, "R %s 0 %s %s",
271 get_oper_name(source_p), name, reason);
272 sendto_one_notice(source_p, ":Added RESV [%s]", name);
273
274 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
275 }
276
277 add_to_resv_hash(aconf->host, aconf);
278 resv_chan_forcepart(aconf->host, aconf->passwd, temp_time);
279 }
280 else if(clean_resv_nick(name))
281 {
282 if(strlen(name) > NICKLEN * 2)
283 {
284 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
285 return;
286 }
287
288 if(!valid_wild_card_simple(name))
289 {
290 sendto_one_notice(source_p,
291 ":Please include at least %d non-wildcard "
292 "characters with the resv",
293 ConfigFileEntry.min_nonwildcard_simple);
294 return;
295 }
296
297 if(find_nick_resv_mask(name))
298 {
299 sendto_one_notice(source_p,
300 ":A RESV has already been placed on nick: %s", name);
301 return;
302 }
303
304 aconf = make_conf();
305 aconf->status = CONF_RESV_NICK;
306 aconf->port = 0;
307 aconf->created = rb_current_time();
308 aconf->host = rb_strdup(name);
309 aconf->passwd = rb_strdup(reason);
310 aconf->info.oper = operhash_add(get_oper_name(source_p));
311
312 if(propagated)
313 {
314 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
315 aconf->hold = rb_current_time() + temp_time;
316 aconf->lifetime = aconf->hold;
317 replace_old_ban(aconf);
318 rb_dlinkAddAlloc(aconf, &prop_bans);
319
320 sendto_realops_snomask(SNO_GENERAL, L_ALL,
321 "%s added global %d min. RESV for [%s] [%s]",
322 get_oper_name(source_p), temp_time / 60,
323 name, reason);
324 ilog(L_KLINE, "R %s %d %s %s",
325 get_oper_name(source_p), temp_time / 60, name, reason);
326 sendto_one_notice(source_p, ":Added global %d min. RESV [%s]",
327 temp_time / 60, name);
328 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
329 ":%s BAN R * %s %lu %d %d * :%s",
330 source_p->id, aconf->host,
331 (unsigned long)aconf->created,
332 (int)(aconf->hold - aconf->created),
333 (int)(aconf->lifetime - aconf->created),
334 reason);
335 }
336 else if(temp_time > 0)
337 {
338 aconf->hold = rb_current_time() + temp_time;
339
340 sendto_realops_snomask(SNO_GENERAL, L_ALL,
341 "%s added temporary %d min. RESV for [%s] [%s]",
342 get_oper_name(source_p), temp_time / 60,
343 name, reason);
344 ilog(L_KLINE, "R %s %d %s %s",
345 get_oper_name(source_p), temp_time / 60, name, reason);
346 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
347 temp_time / 60, name);
348 }
349 else
350 {
351 sendto_realops_snomask(SNO_GENERAL, L_ALL,
352 "%s added RESV for [%s] [%s]",
353 get_oper_name(source_p), name, reason);
354 ilog(L_KLINE, "R %s 0 %s %s",
355 get_oper_name(source_p), name, reason);
356 sendto_one_notice(source_p, ":Added RESV [%s]", name);
357
358 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
359 }
360
361 rb_dlinkAddAlloc(aconf, &resv_conf_list);
362 resv_nick_fnc(aconf->host, aconf->passwd, temp_time);
363 }
364 else
365 sendto_one_notice(source_p, ":You have specified an invalid resv: [%s]", name);
366 }
367
368 static void
369 propagate_resv(struct Client *source_p, const char *target,
370 int temp_time, const char *name, const char *reason)
371 {
372 if(!temp_time)
373 {
374 sendto_match_servs(source_p, target,
375 CAP_CLUSTER, NOCAPS, "RESV %s %s :%s", target, name, reason);
376 sendto_match_servs(source_p, target,
377 CAP_ENCAP, CAP_CLUSTER,
378 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
379 }
380 else
381 sendto_match_servs(source_p, target,
382 CAP_ENCAP, NOCAPS,
383 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
384 }
385
386 static void
387 cluster_resv(struct Client *source_p, int temp_time, const char *name, const char *reason)
388 {
389 struct remote_conf *shared_p;
390 rb_dlink_node *ptr;
391
392 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
393 {
394 shared_p = ptr->data;
395
396 /* old protocol cant handle temps, and we dont really want
397 * to convert them to perm.. --fl
398 */
399 if(!temp_time)
400 {
401 if(!(shared_p->flags & SHARED_PRESV))
402 continue;
403
404 sendto_match_servs(source_p, shared_p->server,
405 CAP_CLUSTER, NOCAPS,
406 "RESV %s %s :%s", shared_p->server, name, reason);
407 sendto_match_servs(source_p, shared_p->server,
408 CAP_ENCAP, CAP_CLUSTER,
409 "ENCAP %s RESV 0 %s 0 :%s",
410 shared_p->server, name, reason);
411 }
412 else if(shared_p->flags & SHARED_TRESV)
413 sendto_match_servs(source_p, shared_p->server,
414 CAP_ENCAP, NOCAPS,
415 "ENCAP %s RESV %d %s 0 :%s",
416 shared_p->server, temp_time, name, reason);
417 }
418 }
419
420
421 /*
422 * mo_unresv()
423 * parv[1] = channel/nick to unforbid
424 */
425 static int
426 mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
427 {
428 int propagated = 1;
429
430 if(!IsOperResv(source_p))
431 {
432 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
433 return 0;
434 }
435
436 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
437 {
438 if(!IsOperRemoteBan(source_p))
439 {
440 sendto_one(source_p, form_str(ERR_NOPRIVS),
441 me.name, source_p->name, "remoteban");
442 return 0;
443 }
444
445 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER, "%s", parv[1]);
446
447 if(match(parv[3], me.name) == 0)
448 return 0;
449
450 propagated = 0;
451 }
452 #if 0
453 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
454 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", parv[1]);
455 #endif
456 /* cluster{} moved to remove_resv */
457
458 remove_resv(source_p, parv[1], propagated);
459 return 0;
460 }
461
462 /* ms_unresv()
463 * parv[1] = target server
464 * parv[2] = resv to remove
465 */
466 static int
467 ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
468 {
469 /* parv[0] parv[1] parv[2]
470 * oper target server resv to remove
471 */
472 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER, "%s", parv[2]);
473
474 if(!match(parv[1], me.name))
475 return 0;
476
477 if(!IsPerson(source_p))
478 return 0;
479
480 handle_remote_unresv(source_p, parv[2]);
481 return 0;
482 }
483
484 static int
485 me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
486 {
487 /* name */
488 if(!IsPerson(source_p))
489 return 0;
490
491 handle_remote_unresv(source_p, parv[1]);
492 return 0;
493 }
494
495 static void
496 handle_remote_unresv(struct Client *source_p, const char *name)
497 {
498 if(!find_shared_conf(source_p->username, source_p->host,
499 source_p->servptr->name, SHARED_UNRESV))
500 return;
501
502 remove_resv(source_p, name, 0);
503
504 return;
505 }
506
507 static void
508 remove_resv(struct Client *source_p, const char *name, int propagated)
509 {
510 struct ConfItem *aconf = NULL;
511 rb_dlink_node *ptr;
512 time_t now;
513
514 if(IsChannelName(name))
515 {
516 if((aconf = hash_find_resv(name)) == NULL)
517 {
518 if(propagated && rb_dlink_list_length(&cluster_conf_list))
519 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
520
521 sendto_one_notice(source_p, ":No RESV for %s", name);
522 return;
523 }
524
525 if(aconf->lifetime)
526 {
527 if(!propagated)
528 {
529 sendto_one_notice(source_p, ":Cannot remove global RESV %s on specific servers", name);
530 return;
531 }
532 ptr = rb_dlinkFind(aconf, &prop_bans);
533 if(ptr == NULL)
534 return;
535 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
536 sendto_realops_snomask(SNO_GENERAL, L_ALL,
537 "%s has removed the global RESV for: [%s]",
538 get_oper_name(source_p), name);
539 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
540 now = rb_current_time();
541 if(aconf->created < now)
542 aconf->created = now;
543 else
544 aconf->created++;
545 aconf->hold = aconf->created;
546 operhash_delete(aconf->info.oper);
547 aconf->info.oper = operhash_add(get_oper_name(source_p));
548 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
549 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
550 ":%s BAN R * %s %lu %d %d * :*",
551 source_p->id, aconf->host,
552 (unsigned long)aconf->created,
553 0,
554 (int)(aconf->lifetime - aconf->created));
555 deactivate_conf(aconf, ptr, now);
556 return;
557 }
558 else if(propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
559 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
560
561 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
562 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
563 if(!aconf->hold)
564 {
565 bandb_del(BANDB_RESV, aconf->host, NULL);
566 sendto_realops_snomask(SNO_GENERAL, L_ALL,
567 "%s has removed the RESV for: [%s]",
568 get_oper_name(source_p), name);
569 }
570 else
571 {
572 sendto_realops_snomask(SNO_GENERAL, L_ALL,
573 "%s has removed the temporary RESV for: [%s]",
574 get_oper_name(source_p), name);
575 }
576 del_from_resv_hash(name, aconf);
577 }
578 else
579 {
580 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
581 {
582 aconf = ptr->data;
583
584 if(irccmp(aconf->host, name))
585 aconf = NULL;
586 else
587 break;
588 }
589
590 if(aconf == NULL)
591 {
592 if(propagated && rb_dlink_list_length(&cluster_conf_list))
593 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
594
595 sendto_one_notice(source_p, ":No RESV for %s", name);
596 return;
597 }
598
599 if(aconf->lifetime)
600 {
601 if(!propagated)
602 {
603 sendto_one_notice(source_p, ":Cannot remove global RESV %s on specific servers", name);
604 return;
605 }
606 ptr = rb_dlinkFind(aconf, &prop_bans);
607 if(ptr == NULL)
608 return;
609 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
610 sendto_realops_snomask(SNO_GENERAL, L_ALL,
611 "%s has removed the global RESV for: [%s]",
612 get_oper_name(source_p), name);
613 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
614 now = rb_current_time();
615 if(aconf->created < now)
616 aconf->created = now;
617 else
618 aconf->created++;
619 aconf->hold = aconf->created;
620 operhash_delete(aconf->info.oper);
621 aconf->info.oper = operhash_add(get_oper_name(source_p));
622 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
623 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
624 ":%s BAN R * %s %lu %d %d * :*",
625 source_p->id, aconf->host,
626 (unsigned long)aconf->created,
627 0,
628 (int)(aconf->lifetime - aconf->created));
629 deactivate_conf(aconf, ptr, now);
630 return;
631 }
632 else if(propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
633 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
634
635 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
636 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
637 if(!aconf->hold)
638 {
639 bandb_del(BANDB_RESV, aconf->host, NULL);
640 sendto_realops_snomask(SNO_GENERAL, L_ALL,
641 "%s has removed the RESV for: [%s]",
642 get_oper_name(source_p), name);
643 }
644 else
645 {
646 sendto_realops_snomask(SNO_GENERAL, L_ALL,
647 "%s has removed the temporary RESV for: [%s]",
648 get_oper_name(source_p), name);
649 }
650 /* already have ptr from the loop above.. */
651 rb_dlinkDestroy(ptr, &resv_conf_list);
652 }
653 free_conf(aconf);
654
655 return;
656 }