]> jfr.im git - solanum.git/blame_incremental - modules/m_resv.c
Kill Travis
[solanum.git] / modules / m_resv.c
... / ...
CommitLineData
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
24#include "stdinc.h"
25#include "client.h"
26#include "channel.h"
27#include "ircd.h"
28#include "numeric.h"
29#include "s_serv.h"
30#include "send.h"
31#include "msg.h"
32#include "parse.h"
33#include "modules.h"
34#include "s_conf.h"
35#include "s_newconf.h"
36#include "hash.h"
37#include "logger.h"
38#include "bandbi.h"
39#include "operhash.h"
40
41static const char resv_desc[] =
42 "Provides management of reserved nicknames and channels using (UN)RESV";
43
44static void mo_resv(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
45static void ms_resv(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
46static void me_resv(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
47static void mo_unresv(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
48static void ms_unresv(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49static void me_unresv(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50
51struct Message resv_msgtab = {
52 "RESV", 0, 0, 0, 0,
53 {mg_ignore, mg_not_oper, {ms_resv, 4}, {ms_resv, 4}, {me_resv, 5}, {mo_resv, 3}}
54};
55
56struct Message unresv_msgtab = {
57 "UNRESV", 0, 0, 0, 0,
58 {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
59};
60
61mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
62
63DECLARE_MODULE_AV2(resv, NULL, NULL, resv_clist, NULL, NULL, NULL, NULL, resv_desc);
64
65static void parse_resv(struct Client *source_p, const char *name,
66 const char *reason, int temp_time, int propagated);
67static void propagate_resv(struct Client *source_p, const char *target,
68 int temp_time, const char *name, const char *reason);
69static void cluster_resv(struct Client *source_p, int temp_time,
70 const char *name, const char *reason);
71
72static 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 */
79static void
80mo_resv(struct MsgBuf *msgbuf_p, 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;
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;
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;
126 }
127
128 reason = parv[loc];
129
130 /* remote resv.. */
131 if(target_server)
132 {
133 if (temp_time)
134 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is adding a %d min. RESV for [%s] on %s [%s]",
135 get_oper_name(source_p), temp_time / 60, name, target_server, reason);
136 else
137 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is adding a permanent RESV for [%s] on %s [%s]",
138 get_oper_name(source_p), name, target_server, reason);
139
140 propagate_resv(source_p, target_server, temp_time, name, reason);
141
142 if(match(target_server, me.name) == 0)
143 return;
144 }
145 else if(!propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
146 cluster_resv(source_p, temp_time, name, reason);
147
148 if(propagated && temp_time == 0)
149 {
150 sendto_one_notice(source_p, ":Cannot set a permanent global ban");
151 return;
152 }
153
154 parse_resv(source_p, name, reason, temp_time, propagated);
155}
156
157/* ms_resv()
158 * parv[1] = target server
159 * parv[2] = channel/nick to forbid
160 * parv[3] = reason
161 */
162static void
163ms_resv(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
164{
165 /* parv[0] parv[1] parv[2] parv[3]
166 * oper target server resv reason
167 */
168 propagate_resv(source_p, parv[1], 0, parv[2], parv[3]);
169
170 if(!match(parv[1], me.name))
171 return;
172
173 if(!IsPerson(source_p))
174 return;
175
176 parse_resv(source_p, parv[2], parv[3], 0, 0);
177}
178
179static void
180me_resv(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
181{
182 /* time name 0 :reason */
183 if(!IsPerson(source_p))
184 return;
185
186 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]), 0);
187}
188
189/* parse_resv()
190 *
191 * inputs - source_p if error messages wanted
192 * - thing to resv
193 * - reason for resv
194 * outputs -
195 * side effects - will parse the resv and create it if valid
196 */
197static void
198parse_resv(struct Client *source_p, const char *name, const char *reason, int temp_time, int propagated)
199{
200 struct ConfItem *aconf;
201
202 if(IsChannelName(name))
203 {
204 if(hash_find_resv(name))
205 {
206 sendto_one_notice(source_p,
207 ":A RESV has already been placed on channel: %s", name);
208 return;
209 }
210
211 if(strlen(name) > CHANNELLEN)
212 {
213 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
214 return;
215 }
216
217 aconf = make_conf();
218 aconf->status = CONF_RESV_CHANNEL;
219 aconf->port = 0;
220 aconf->created = rb_current_time();
221 aconf->host = rb_strdup(name);
222 aconf->passwd = rb_strdup(reason);
223 aconf->info.oper = operhash_add(get_oper_name(source_p));
224
225 if(propagated)
226 {
227 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
228 aconf->hold = rb_current_time() + temp_time;
229 aconf->lifetime = aconf->hold;
230 replace_old_ban(aconf);
231 rb_dlinkAddAlloc(aconf, &prop_bans);
232
233 sendto_realops_snomask(SNO_GENERAL, L_ALL,
234 "%s added global %d min. RESV for [%s] [%s]",
235 get_oper_name(source_p), temp_time / 60,
236 name, reason);
237 ilog(L_KLINE, "R %s %d %s %s",
238 get_oper_name(source_p), temp_time / 60, name, reason);
239 sendto_one_notice(source_p, ":Added global %d min. RESV [%s]",
240 temp_time / 60, name);
241 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
242 ":%s BAN R * %s %lu %d %d * :%s",
243 source_p->id, aconf->host,
244 (unsigned long)aconf->created,
245 (int)(aconf->hold - aconf->created),
246 (int)(aconf->lifetime - aconf->created),
247 reason);
248 }
249 else if(temp_time > 0)
250 {
251 aconf->hold = rb_current_time() + temp_time;
252
253 sendto_realops_snomask(SNO_GENERAL, L_ALL,
254 "%s added temporary %d min. RESV for [%s] [%s]",
255 get_oper_name(source_p), temp_time / 60,
256 name, reason);
257 ilog(L_KLINE, "R %s %d %s %s",
258 get_oper_name(source_p), temp_time / 60, name, reason);
259 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
260 temp_time / 60, name);
261 }
262 else
263 {
264 sendto_realops_snomask(SNO_GENERAL, L_ALL,
265 "%s added RESV for [%s] [%s]",
266 get_oper_name(source_p), name, reason);
267 ilog(L_KLINE, "R %s 0 %s %s",
268 get_oper_name(source_p), name, reason);
269 sendto_one_notice(source_p, ":Added RESV [%s]", name);
270
271 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
272 }
273
274 add_to_resv_hash(aconf->host, aconf);
275 resv_chan_forcepart(aconf->host, aconf->passwd, temp_time);
276 }
277 else if(clean_resv_nick(name))
278 {
279 if(strlen(name) > NICKLEN * 2)
280 {
281 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
282 return;
283 }
284
285 if(!valid_wild_card_simple(name))
286 {
287 sendto_one_notice(source_p,
288 ":Please include at least %d non-wildcard "
289 "characters with the resv",
290 ConfigFileEntry.min_nonwildcard_simple);
291 return;
292 }
293
294 if(find_nick_resv_mask(name))
295 {
296 sendto_one_notice(source_p,
297 ":A RESV has already been placed on nick: %s", name);
298 return;
299 }
300
301 aconf = make_conf();
302 aconf->status = CONF_RESV_NICK;
303 aconf->port = 0;
304 aconf->created = rb_current_time();
305 aconf->host = rb_strdup(name);
306 aconf->passwd = rb_strdup(reason);
307 aconf->info.oper = operhash_add(get_oper_name(source_p));
308
309 if(propagated)
310 {
311 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
312 aconf->hold = rb_current_time() + temp_time;
313 aconf->lifetime = aconf->hold;
314 replace_old_ban(aconf);
315 rb_dlinkAddAlloc(aconf, &prop_bans);
316
317 sendto_realops_snomask(SNO_GENERAL, L_ALL,
318 "%s added global %d min. RESV for [%s] [%s]",
319 get_oper_name(source_p), temp_time / 60,
320 name, reason);
321 ilog(L_KLINE, "R %s %d %s %s",
322 get_oper_name(source_p), temp_time / 60, name, reason);
323 sendto_one_notice(source_p, ":Added global %d min. RESV [%s]",
324 temp_time / 60, name);
325 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
326 ":%s BAN R * %s %lu %d %d * :%s",
327 source_p->id, aconf->host,
328 (unsigned long)aconf->created,
329 (int)(aconf->hold - aconf->created),
330 (int)(aconf->lifetime - aconf->created),
331 reason);
332 }
333 else if(temp_time > 0)
334 {
335 aconf->hold = rb_current_time() + temp_time;
336
337 sendto_realops_snomask(SNO_GENERAL, L_ALL,
338 "%s added temporary %d min. RESV for [%s] [%s]",
339 get_oper_name(source_p), temp_time / 60,
340 name, reason);
341 ilog(L_KLINE, "R %s %d %s %s",
342 get_oper_name(source_p), temp_time / 60, name, reason);
343 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
344 temp_time / 60, name);
345 }
346 else
347 {
348 sendto_realops_snomask(SNO_GENERAL, L_ALL,
349 "%s added RESV for [%s] [%s]",
350 get_oper_name(source_p), name, reason);
351 ilog(L_KLINE, "R %s 0 %s %s",
352 get_oper_name(source_p), name, reason);
353 sendto_one_notice(source_p, ":Added RESV [%s]", name);
354
355 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
356 }
357
358 rb_dlinkAddAlloc(aconf, &resv_conf_list);
359 resv_nick_fnc(aconf->host, aconf->passwd, temp_time);
360 }
361 else
362 sendto_one_notice(source_p, ":You have specified an invalid resv: [%s]", name);
363}
364
365static void
366propagate_resv(struct Client *source_p, const char *target,
367 int temp_time, const char *name, const char *reason)
368{
369 if(!temp_time)
370 {
371 sendto_match_servs(source_p, target,
372 CAP_CLUSTER, NOCAPS, "RESV %s %s :%s", target, name, reason);
373 sendto_match_servs(source_p, target,
374 CAP_ENCAP, CAP_CLUSTER,
375 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
376 }
377 else
378 sendto_match_servs(source_p, target,
379 CAP_ENCAP, NOCAPS,
380 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
381}
382
383static void
384cluster_resv(struct Client *source_p, int temp_time, const char *name, const char *reason)
385{
386 struct remote_conf *shared_p;
387 rb_dlink_node *ptr;
388
389 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
390 {
391 shared_p = ptr->data;
392
393 /* old protocol cant handle temps, and we dont really want
394 * to convert them to perm.. --fl
395 */
396 if(!temp_time)
397 {
398 if(!(shared_p->flags & SHARED_PRESV))
399 continue;
400
401 sendto_match_servs(source_p, shared_p->server,
402 CAP_CLUSTER, NOCAPS,
403 "RESV %s %s :%s", shared_p->server, name, reason);
404 sendto_match_servs(source_p, shared_p->server,
405 CAP_ENCAP, CAP_CLUSTER,
406 "ENCAP %s RESV 0 %s 0 :%s",
407 shared_p->server, name, reason);
408 }
409 else if(shared_p->flags & SHARED_TRESV)
410 sendto_match_servs(source_p, shared_p->server,
411 CAP_ENCAP, NOCAPS,
412 "ENCAP %s RESV %d %s 0 :%s",
413 shared_p->server, temp_time, name, reason);
414 }
415}
416
417
418/*
419 * mo_unresv()
420 * parv[1] = channel/nick to unforbid
421 */
422static void
423mo_unresv(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
424{
425 int propagated = 1;
426
427 if(!IsOperResv(source_p))
428 {
429 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
430 return;
431 }
432
433 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
434 {
435 if(!IsOperRemoteBan(source_p))
436 {
437 sendto_one(source_p, form_str(ERR_NOPRIVS),
438 me.name, source_p->name, "remoteban");
439 return;
440 }
441
442 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER, "%s", parv[1]);
443
444 if(match(parv[3], me.name) == 0)
445 return;
446
447 propagated = 0;
448 }
449#if 0
450 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
451 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", parv[1]);
452#endif
453 /* cluster{} moved to remove_resv */
454
455 remove_resv(source_p, parv[1], propagated);
456}
457
458/* ms_unresv()
459 * parv[1] = target server
460 * parv[2] = resv to remove
461 */
462static void
463ms_unresv(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
464{
465 /* parv[0] parv[1] parv[2]
466 * oper target server resv to remove
467 */
468 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER, "%s", parv[2]);
469
470 if(!match(parv[1], me.name))
471 return;
472
473 if(!IsPerson(source_p))
474 return;
475
476 remove_resv(source_p, parv[2], 0);
477}
478
479static void
480me_unresv(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
481{
482 /* name */
483 if(!IsPerson(source_p))
484 return;
485
486 remove_resv(source_p, parv[1], 0);
487}
488
489static void
490remove_resv(struct Client *source_p, const char *name, int propagated)
491{
492 struct ConfItem *aconf = NULL;
493 rb_dlink_node *ptr;
494 time_t now;
495
496 if(IsChannelName(name))
497 {
498 if((aconf = hash_find_resv(name)) == NULL)
499 {
500 if(propagated && rb_dlink_list_length(&cluster_conf_list))
501 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
502
503 sendto_one_notice(source_p, ":No RESV for %s", name);
504 return;
505 }
506
507 if(aconf->lifetime)
508 {
509 if(!propagated)
510 {
511 sendto_one_notice(source_p, ":Cannot remove global RESV %s on specific servers", name);
512 return;
513 }
514 ptr = rb_dlinkFind(aconf, &prop_bans);
515 if(ptr == NULL)
516 return;
517 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
518 sendto_realops_snomask(SNO_GENERAL, L_ALL,
519 "%s has removed the global RESV for: [%s]",
520 get_oper_name(source_p), name);
521 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
522 now = rb_current_time();
523 if(aconf->created < now)
524 aconf->created = now;
525 else
526 aconf->created++;
527 aconf->hold = aconf->created;
528 operhash_delete(aconf->info.oper);
529 aconf->info.oper = operhash_add(get_oper_name(source_p));
530 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
531 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
532 ":%s BAN R * %s %lu %d %d * :*",
533 source_p->id, aconf->host,
534 (unsigned long)aconf->created,
535 0,
536 (int)(aconf->lifetime - aconf->created));
537 deactivate_conf(aconf, ptr, now);
538 return;
539 }
540 else if(propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
541 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
542
543 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
544 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
545 if(!aconf->hold)
546 {
547 bandb_del(BANDB_RESV, aconf->host, NULL);
548 sendto_realops_snomask(SNO_GENERAL, L_ALL,
549 "%s has removed the RESV for: [%s]",
550 get_oper_name(source_p), name);
551 }
552 else
553 {
554 sendto_realops_snomask(SNO_GENERAL, L_ALL,
555 "%s has removed the temporary RESV for: [%s]",
556 get_oper_name(source_p), name);
557 }
558 del_from_resv_hash(name, aconf);
559 }
560 else
561 {
562 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
563 {
564 aconf = ptr->data;
565
566 if(irccmp(aconf->host, name))
567 aconf = NULL;
568 else
569 break;
570 }
571
572 if(aconf == NULL)
573 {
574 if(propagated && rb_dlink_list_length(&cluster_conf_list))
575 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
576
577 sendto_one_notice(source_p, ":No RESV for %s", name);
578 return;
579 }
580
581 if(aconf->lifetime)
582 {
583 if(!propagated)
584 {
585 sendto_one_notice(source_p, ":Cannot remove global RESV %s on specific servers", name);
586 return;
587 }
588 ptr = rb_dlinkFind(aconf, &prop_bans);
589 if(ptr == NULL)
590 return;
591 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
592 sendto_realops_snomask(SNO_GENERAL, L_ALL,
593 "%s has removed the global RESV for: [%s]",
594 get_oper_name(source_p), name);
595 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
596 now = rb_current_time();
597 if(aconf->created < now)
598 aconf->created = now;
599 else
600 aconf->created++;
601 aconf->hold = aconf->created;
602 operhash_delete(aconf->info.oper);
603 aconf->info.oper = operhash_add(get_oper_name(source_p));
604 aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
605 sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
606 ":%s BAN R * %s %lu %d %d * :*",
607 source_p->id, aconf->host,
608 (unsigned long)aconf->created,
609 0,
610 (int)(aconf->lifetime - aconf->created));
611 deactivate_conf(aconf, ptr, now);
612 return;
613 }
614 else if(propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
615 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", name);
616
617 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
618 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
619 if(!aconf->hold)
620 {
621 bandb_del(BANDB_RESV, aconf->host, NULL);
622 sendto_realops_snomask(SNO_GENERAL, L_ALL,
623 "%s has removed the RESV for: [%s]",
624 get_oper_name(source_p), name);
625 }
626 else
627 {
628 sendto_realops_snomask(SNO_GENERAL, L_ALL,
629 "%s has removed the temporary RESV for: [%s]",
630 get_oper_name(source_p), name);
631 }
632 /* already have ptr from the loop above.. */
633 rb_dlinkDestroy(ptr, &resv_conf_list);
634 }
635 free_conf(aconf);
636
637 return;
638}