]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_resv.c
Fix up more things with bandb.
[irc/rqf/shadowircd.git] / modules / m_resv.c
CommitLineData
212380e3 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 *
d8a4c5f6 23 * $Id$
212380e3 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"
d3455e2c 39#include "logger.h"
d8a4c5f6 40#include "bandbi.h"
212380e3 41
42static int mo_resv(struct Client *, struct Client *, int, const char **);
43static int ms_resv(struct Client *, struct Client *, int, const char **);
44static int me_resv(struct Client *, struct Client *, int, const char **);
45static int mo_unresv(struct Client *, struct Client *, int, const char **);
46static int ms_unresv(struct Client *, struct Client *, int, const char **);
47static int me_unresv(struct Client *, struct Client *, int, const char **);
48
49struct Message resv_msgtab = {
50 "RESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
51 {mg_ignore, mg_not_oper, {ms_resv, 4}, {ms_resv, 4}, {me_resv, 5}, {mo_resv, 3}}
52};
d8a4c5f6 53
212380e3 54struct Message unresv_msgtab = {
55 "UNRESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
56 {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
57};
58
d8a4c5f6
WP
59mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
60
61DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision$");
212380e3 62
63static void parse_resv(struct Client *source_p, const char *name,
d8a4c5f6 64 const char *reason, int temp_time);
212380e3 65static void propagate_resv(struct Client *source_p, const char *target,
d8a4c5f6
WP
66 int temp_time, const char *name, const char *reason);
67static void cluster_resv(struct Client *source_p, int temp_time,
68 const char *name, const char *reason);
212380e3 69
70static void handle_remote_unresv(struct Client *source_p, const char *name);
71static void remove_resv(struct Client *source_p, const char *name);
212380e3 72
73/*
74 * mo_resv()
d8a4c5f6 75 * parv[0] = sender prefix
212380e3 76 * parv[1] = channel/nick to forbid
77 * parv[2] = reason
78 */
79static int
80mo_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
1ebe6ffc
JT
88 if(!IsOperResv(source_p))
89 {
d8a4c5f6 90 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
1ebe6ffc
JT
91 return 0;
92 }
93
212380e3 94 /* RESV [time] <name> [ON <server>] :<reason> */
95
96 if((temp_time = valid_temp_time(parv[loc])) >= 0)
97 loc++;
98 /* we just set temp_time to -1! */
99 else
100 temp_time = 0;
101
102 name = parv[loc];
103 loc++;
104
d8a4c5f6 105 if((parc >= loc + 2) && (irccmp(parv[loc], "ON") == 0))
212380e3 106 {
107 if(!IsOperRemoteBan(source_p))
108 {
109 sendto_one(source_p, form_str(ERR_NOPRIVS),
d8a4c5f6 110 me.name, source_p->name, "remoteban");
212380e3 111 return 0;
112 }
113
d8a4c5f6 114 target_server = parv[loc + 1];
212380e3 115 loc += 2;
116 }
117
118 if(parc <= loc || EmptyString(parv[loc]))
119 {
d8a4c5f6 120 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "RESV");
212380e3 121 return 0;
122 }
123
124 reason = parv[loc];
125
126 /* remote resv.. */
127 if(target_server)
128 {
129 propagate_resv(source_p, target_server, temp_time, name, reason);
130
131 if(match(target_server, me.name) == 0)
132 return 0;
133 }
08d11e34 134 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
212380e3 135 cluster_resv(source_p, temp_time, name, reason);
136
137 parse_resv(source_p, name, reason, temp_time);
138
139 return 0;
140}
141
142/* ms_resv()
d8a4c5f6 143 * parv[0] = sender prefix
212380e3 144 * parv[1] = target server
145 * parv[2] = channel/nick to forbid
146 * parv[3] = reason
147 */
148static int
d8a4c5f6 149ms_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 150{
d8a4c5f6
WP
151 /* parv[0] parv[1] parv[2] parv[3]
152 * oper target server resv reason
212380e3 153 */
154 propagate_resv(source_p, parv[1], 0, parv[2], parv[3]);
155
156 if(!match(parv[1], me.name))
157 return 0;
158
159 if(!IsPerson(source_p))
160 return 0;
161
162 parse_resv(source_p, parv[2], parv[3], 0);
163 return 0;
164}
165
166static int
d8a4c5f6 167me_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 168{
169 /* time name 0 :reason */
170 if(!IsPerson(source_p))
171 return 0;
172
173 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]));
174 return 0;
175}
176
177/* parse_resv()
178 *
179 * inputs - source_p if error messages wanted
180 * - thing to resv
181 * - reason for resv
182 * outputs -
183 * side effects - will parse the resv and create it if valid
184 */
185static void
d8a4c5f6 186parse_resv(struct Client *source_p, const char *name, const char *reason, int temp_time)
212380e3 187{
188 struct ConfItem *aconf;
189
d8a4c5f6 190 if(!MyClient(source_p) &&
212380e3 191 !find_shared_conf(source_p->username, source_p->host,
d8a4c5f6
WP
192 source_p->servptr->name,
193 (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
212380e3 194 return;
195
196 if(IsChannelName(name))
197 {
198 if(hash_find_resv(name))
199 {
200 sendto_one_notice(source_p,
d8a4c5f6 201 ":A RESV has already been placed on channel: %s", name);
212380e3 202 return;
203 }
204
205 if(strlen(name) > CHANNELLEN)
206 {
d8a4c5f6 207 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
6e5b8a5d
JT
208 return;
209 }
212380e3 210
211 if(strchr(reason, '"'))
212 {
d8a4c5f6 213 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
212380e3 214 return;
215 }
216
217 aconf = make_conf();
218 aconf->status = CONF_RESV_CHANNEL;
219 aconf->port = 0;
ff0482a9 220 aconf->host = rb_strdup(name);
62d28946 221 aconf->passwd = rb_strdup(reason);
ff0482a9 222 add_to_resv_hash(aconf->host, aconf);
212380e3 223
224 if(temp_time > 0)
225 {
9f6bbe3c 226 aconf->hold = rb_current_time() + temp_time;
212380e3 227
228 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
229 "%s added temporary %d min. RESV for [%s] [%s]",
230 get_oper_name(source_p), temp_time / 60,
231 name, reason);
212380e3 232 ilog(L_KLINE, "R %s %d %s %s",
d8a4c5f6 233 get_oper_name(source_p), temp_time / 60, name, reason);
212380e3 234 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
d8a4c5f6 235 temp_time / 60, name);
212380e3 236 }
237 else
ff0482a9 238 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
212380e3 239 }
240 else if(clean_resv_nick(name))
241 {
d8a4c5f6 242 if(strlen(name) > NICKLEN * 2)
212380e3 243 {
d8a4c5f6 244 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
212380e3 245 return;
246 }
247
248 if(strchr(reason, '"'))
249 {
d8a4c5f6 250 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
212380e3 251 return;
252 }
253
254 if(!valid_wild_card_simple(name))
255 {
256 sendto_one_notice(source_p,
d8a4c5f6
WP
257 ":Please include at least %d non-wildcard "
258 "characters with the resv",
259 ConfigFileEntry.min_nonwildcard_simple);
212380e3 260 return;
261 }
262
0fdb2570 263 if(find_nick_resv_mask(name))
212380e3 264 {
265 sendto_one_notice(source_p,
d8a4c5f6 266 ":A RESV has already been placed on nick: %s", name);
212380e3 267 return;
268 }
269
270 aconf = make_conf();
271 aconf->status = CONF_RESV_NICK;
272 aconf->port = 0;
ff0482a9 273 aconf->host = rb_strdup(name);
62d28946 274 aconf->passwd = rb_strdup(reason);
7f4fa195 275 rb_dlinkAddAlloc(aconf, &resv_conf_list);
212380e3 276
277 if(temp_time > 0)
278 {
9f6bbe3c 279 aconf->hold = rb_current_time() + temp_time;
212380e3 280
281 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
282 "%s added temporary %d min. RESV for [%s] [%s]",
283 get_oper_name(source_p), temp_time / 60,
284 name, reason);
212380e3 285 ilog(L_KLINE, "R %s %d %s %s",
d8a4c5f6 286 get_oper_name(source_p), temp_time / 60, name, reason);
212380e3 287 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
d8a4c5f6 288 temp_time / 60, name);
212380e3 289 }
290 else
ff0482a9 291 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
212380e3 292 }
293 else
d8a4c5f6 294 sendto_one_notice(source_p, ":You have specified an invalid resv: [%s]", name);
212380e3 295}
296
d8a4c5f6 297static void
212380e3 298propagate_resv(struct Client *source_p, const char *target,
d8a4c5f6 299 int temp_time, const char *name, const char *reason)
212380e3 300{
301 if(!temp_time)
302 {
303 sendto_match_servs(source_p, target,
d8a4c5f6 304 CAP_CLUSTER, NOCAPS, "RESV %s %s :%s", target, name, reason);
212380e3 305 sendto_match_servs(source_p, target,
d8a4c5f6
WP
306 CAP_ENCAP, CAP_CLUSTER,
307 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
212380e3 308 }
309 else
310 sendto_match_servs(source_p, target,
d8a4c5f6
WP
311 CAP_ENCAP, NOCAPS,
312 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
212380e3 313}
314
315static void
d8a4c5f6 316cluster_resv(struct Client *source_p, int temp_time, const char *name, const char *reason)
212380e3 317{
318 struct remote_conf *shared_p;
08d11e34 319 rb_dlink_node *ptr;
212380e3 320
08d11e34 321 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
212380e3 322 {
323 shared_p = ptr->data;
324
325 /* old protocol cant handle temps, and we dont really want
326 * to convert them to perm.. --fl
327 */
328 if(!temp_time)
329 {
330 if(!(shared_p->flags & SHARED_PRESV))
331 continue;
332
333 sendto_match_servs(source_p, shared_p->server,
d8a4c5f6
WP
334 CAP_CLUSTER, NOCAPS,
335 "RESV %s %s :%s", shared_p->server, name, reason);
212380e3 336 sendto_match_servs(source_p, shared_p->server,
d8a4c5f6
WP
337 CAP_ENCAP, CAP_CLUSTER,
338 "ENCAP %s RESV 0 %s 0 :%s",
339 shared_p->server, name, reason);
212380e3 340 }
341 else if(shared_p->flags & SHARED_TRESV)
342 sendto_match_servs(source_p, shared_p->server,
d8a4c5f6
WP
343 CAP_ENCAP, NOCAPS,
344 "ENCAP %s RESV %d %s 0 :%s",
345 shared_p->server, temp_time, name, reason);
212380e3 346 }
347}
348
349
350/*
351 * mo_unresv()
d8a4c5f6 352 * parv[0] = sender prefix
212380e3 353 * parv[1] = channel/nick to unforbid
354 */
355static int
356mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
357{
1ebe6ffc
JT
358 if(!IsOperResv(source_p))
359 {
d8a4c5f6 360 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
1ebe6ffc
JT
361 return 0;
362 }
363
212380e3 364 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
365 {
366 if(!IsOperRemoteBan(source_p))
367 {
368 sendto_one(source_p, form_str(ERR_NOPRIVS),
d8a4c5f6 369 me.name, source_p->name, "remoteban");
212380e3 370 return 0;
371 }
372
d8a4c5f6 373 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER, "%s", parv[1]);
212380e3 374
375 if(match(parv[3], me.name) == 0)
376 return 0;
377 }
08d11e34 378 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
d8a4c5f6 379 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", parv[1]);
212380e3 380
212380e3 381 remove_resv(source_p, parv[1]);
382 return 0;
383}
384
385/* ms_unresv()
d8a4c5f6 386 * parv[0] = sender prefix
212380e3 387 * parv[1] = target server
388 * parv[2] = resv to remove
389 */
390static int
391ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
392{
d8a4c5f6
WP
393 /* parv[0] parv[1] parv[2]
394 * oper target server resv to remove
212380e3 395 */
d8a4c5f6 396 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER, "%s", parv[2]);
212380e3 397
398 if(!match(parv[1], me.name))
399 return 0;
400
401 if(!IsPerson(source_p))
402 return 0;
403
404 handle_remote_unresv(source_p, parv[2]);
405 return 0;
406}
407
408static int
409me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
410{
411 /* name */
412 if(!IsPerson(source_p))
413 return 0;
414
415 handle_remote_unresv(source_p, parv[1]);
416 return 0;
417}
418
419static void
420handle_remote_unresv(struct Client *source_p, const char *name)
421{
422 if(!find_shared_conf(source_p->username, source_p->host,
d8a4c5f6 423 source_p->servptr->name, SHARED_UNRESV))
212380e3 424 return;
425
212380e3 426 remove_resv(source_p, name);
427
428 return;
429}
430
1328da86
JT
431static void
432remove_resv(struct Client *source_p, const char *name)
212380e3 433{
434 struct ConfItem *aconf = NULL;
435
436 if(IsChannelName(name))
437 {
438 if((aconf = hash_find_resv(name)) == NULL)
1328da86
JT
439 {
440 sendto_one_notice(source_p, ":No RESV for %s", name);
441 return;
442 }
212380e3 443
212380e3 444 if(!aconf->hold)
d8a4c5f6 445 bandb_del(BANDB_RESV, aconf->host, NULL);
1328da86
JT
446 else
447 {
448 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
449 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
450 "%s has removed the RESV for: [%s]",
451 get_oper_name(source_p), name);
1328da86
JT
452 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
453 }
212380e3 454 del_from_resv_hash(name, aconf);
212380e3 455 }
456 else
457 {
08d11e34 458 rb_dlink_node *ptr;
212380e3 459
08d11e34 460 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
212380e3 461 {
462 aconf = ptr->data;
463
ff0482a9 464 if(irccmp(aconf->host, name))
212380e3 465 aconf = NULL;
466 else
467 break;
468 }
469
470 if(aconf == NULL)
1328da86
JT
471 {
472 sendto_one_notice(source_p, ":No RESV for %s", name);
473 return;
474 }
212380e3 475
212380e3 476 if(!aconf->hold)
d8a4c5f6 477 bandb_del(BANDB_RESV, aconf->host, NULL);
1328da86
JT
478 else
479 {
480 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
481 sendto_realops_snomask(SNO_GENERAL, L_ALL,
d8a4c5f6
WP
482 "%s has removed the RESV for: [%s]",
483 get_oper_name(source_p), name);
1328da86
JT
484 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
485 }
212380e3 486 /* already have ptr from the loop above.. */
9f6c3353 487 rb_dlinkDestroy(ptr, &resv_conf_list);
212380e3 488 }
1328da86 489 free_conf(aconf);
212380e3 490
1328da86 491 return;
212380e3 492}