]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_resv.c
Add bandb IRCd APIs.
[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 *
23 * $Id: m_resv.c 3045 2006-12-26 23:16:57Z jilles $
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"
212380e3 40
41static int mo_resv(struct Client *, struct Client *, int, const char **);
42static int ms_resv(struct Client *, struct Client *, int, const char **);
43static int me_resv(struct Client *, struct Client *, int, const char **);
44static int mo_unresv(struct Client *, struct Client *, int, const char **);
45static int ms_unresv(struct Client *, struct Client *, int, const char **);
46static int me_unresv(struct Client *, struct Client *, int, const char **);
47
48struct Message resv_msgtab = {
49 "RESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
50 {mg_ignore, mg_not_oper, {ms_resv, 4}, {ms_resv, 4}, {me_resv, 5}, {mo_resv, 3}}
51};
52struct Message unresv_msgtab = {
53 "UNRESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
54 {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
55};
56
57mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
58DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision: 3045 $");
59
60static void parse_resv(struct Client *source_p, const char *name,
61 const char *reason, int temp_time);
62static void propagate_resv(struct Client *source_p, const char *target,
63 int temp_time, const char *name, const char *reason);
64static void cluster_resv(struct Client *source_p, int temp_time,
65 const char *name, const char *reason);
66
67static void handle_remote_unresv(struct Client *source_p, const char *name);
68static void remove_resv(struct Client *source_p, const char *name);
1328da86 69static int remove_resv_from_file(struct Client *source_p, const char *name);
100563e8 70static void resv_chan_forcepart(const char *name, const char *reason, int temp_time);
212380e3 71
72/*
73 * mo_resv()
212380e3 74 * parv[1] = channel/nick to forbid
75 * parv[2] = reason
76 */
77static int
78mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
79{
80 const char *name;
81 const char *reason;
82 const char *target_server = NULL;
83 int temp_time;
84 int loc = 1;
85
1ebe6ffc
JT
86 if(!IsOperResv(source_p))
87 {
88 sendto_one(source_p, form_str(ERR_NOPRIVS),
89 me.name, source_p->name, "resv");
90 return 0;
91 }
92
212380e3 93 /* RESV [time] <name> [ON <server>] :<reason> */
94
95 if((temp_time = valid_temp_time(parv[loc])) >= 0)
96 loc++;
97 /* we just set temp_time to -1! */
98 else
99 temp_time = 0;
100
101 name = parv[loc];
102 loc++;
103
104 if((parc >= loc+2) && (irccmp(parv[loc], "ON") == 0))
105 {
106 if(!IsOperRemoteBan(source_p))
107 {
108 sendto_one(source_p, form_str(ERR_NOPRIVS),
109 me.name, source_p->name, "remoteban");
110 return 0;
111 }
112
113 target_server = parv[loc+1];
114 loc += 2;
115 }
116
117 if(parc <= loc || EmptyString(parv[loc]))
118 {
119 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
120 me.name, source_p->name, "RESV");
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()
212380e3 143 * parv[1] = target server
144 * parv[2] = channel/nick to forbid
145 * parv[3] = reason
146 */
147static int
148ms_resv(struct Client *client_p, struct Client *source_p,
149 int parc, const char *parv[])
150{
77f3c1f4
JT
151 /* source_p 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
167me_resv(struct Client *client_p, struct Client *source_p,
168 int parc, const char *parv[])
169{
170 /* time name 0 :reason */
171 if(!IsPerson(source_p))
172 return 0;
173
174 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]));
175 return 0;
176}
177
178/* parse_resv()
179 *
180 * inputs - source_p if error messages wanted
181 * - thing to resv
182 * - reason for resv
183 * outputs -
184 * side effects - will parse the resv and create it if valid
185 */
186static void
187parse_resv(struct Client *source_p, const char *name,
188 const char *reason, int temp_time)
189{
190 struct ConfItem *aconf;
191
192 if(!MyClient(source_p) &&
193 !find_shared_conf(source_p->username, source_p->host,
c88cdb00 194 source_p->servptr->name,
212380e3 195 (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
196 return;
197
198 if(IsChannelName(name))
199 {
200 if(hash_find_resv(name))
201 {
202 sendto_one_notice(source_p,
203 ":A RESV has already been placed on channel: %s",
204 name);
205 return;
206 }
207
208 if(strlen(name) > CHANNELLEN)
209 {
210 sendto_one_notice(source_p, ":Invalid RESV length: %s",
211 name);
212 return;
213 }
6e5b8a5d
JT
214
215 if(strchr(name, ','))
216 {
217 sendto_one_notice(source_p,
218 ":Invalid character ',' in channel RESV");
219 return;
220 }
212380e3 221
222 if(strchr(reason, '"'))
223 {
224 sendto_one_notice(source_p,
225 ":Invalid character '\"' in comment");
226 return;
227 }
228
229 aconf = make_conf();
230 aconf->status = CONF_RESV_CHANNEL;
231 aconf->port = 0;
bf019bb0 232 aconf->name = rb_strdup(name);
62d28946 233 aconf->passwd = rb_strdup(reason);
bf019bb0 234 add_to_resv_hash(aconf->name, aconf);
100563e8 235 resv_chan_forcepart(aconf->name, aconf->passwd, temp_time);
212380e3 236
237 if(temp_time > 0)
238 {
9f6bbe3c 239 aconf->hold = rb_current_time() + temp_time;
212380e3 240
241 sendto_realops_snomask(SNO_GENERAL, L_ALL,
242 "%s added temporary %d min. RESV for [%s] [%s]",
243 get_oper_name(source_p), temp_time / 60,
244 name, reason);
245 ilog(L_KLINE, "R %s %d %s %s",
246 get_oper_name(source_p), temp_time / 60,
247 name, reason);
248 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
249 temp_time / 60, name);
250 }
251 else
bf019bb0 252 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
212380e3 253 aconf->passwd, NULL, NULL, 0);
254 }
255 else if(clean_resv_nick(name))
256 {
257 if(strlen(name) > NICKLEN*2)
258 {
259 sendto_one_notice(source_p, ":Invalid RESV length: %s",
260 name);
261 return;
262 }
263
264 if(strchr(reason, '"'))
265 {
266 sendto_one_notice(source_p,
267 ":Invalid character '\"' in comment");
268 return;
269 }
270
271 if(!valid_wild_card_simple(name))
272 {
273 sendto_one_notice(source_p,
274 ":Please include at least %d non-wildcard "
275 "characters with the resv",
276 ConfigFileEntry.min_nonwildcard_simple);
277 return;
278 }
279
0fdb2570 280 if(find_nick_resv_mask(name))
212380e3 281 {
282 sendto_one_notice(source_p,
283 ":A RESV has already been placed on nick: %s",
284 name);
285 return;
286 }
287
288 aconf = make_conf();
289 aconf->status = CONF_RESV_NICK;
290 aconf->port = 0;
bf019bb0 291 aconf->name = rb_strdup(name);
62d28946 292 aconf->passwd = rb_strdup(reason);
7f4fa195 293 rb_dlinkAddAlloc(aconf, &resv_conf_list);
212380e3 294
295 if(temp_time > 0)
296 {
9f6bbe3c 297 aconf->hold = rb_current_time() + temp_time;
212380e3 298
299 sendto_realops_snomask(SNO_GENERAL, L_ALL,
300 "%s added temporary %d min. RESV for [%s] [%s]",
301 get_oper_name(source_p), temp_time / 60,
302 name, reason);
303 ilog(L_KLINE, "R %s %d %s %s",
304 get_oper_name(source_p), temp_time / 60,
305 name, reason);
306 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
307 temp_time / 60, name);
308 }
309 else
bf019bb0 310 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
212380e3 311 aconf->passwd, NULL, NULL, 0);
312 }
313 else
314 sendto_one_notice(source_p,
315 ":You have specified an invalid resv: [%s]",
316 name);
317}
318
319static void
320propagate_resv(struct Client *source_p, const char *target,
321 int temp_time, const char *name, const char *reason)
322{
323 if(!temp_time)
324 {
325 sendto_match_servs(source_p, target,
326 CAP_CLUSTER, NOCAPS,
327 "RESV %s %s :%s",
328 target, name, reason);
329 sendto_match_servs(source_p, target,
330 CAP_ENCAP, CAP_CLUSTER,
331 "ENCAP %s RESV %d %s 0 :%s",
332 target, temp_time, name, reason);
333 }
334 else
335 sendto_match_servs(source_p, target,
336 CAP_ENCAP, NOCAPS,
337 "ENCAP %s RESV %d %s 0 :%s",
338 target, temp_time, name, reason);
339}
340
341static void
342cluster_resv(struct Client *source_p, int temp_time, const char *name,
343 const char *reason)
344{
345 struct remote_conf *shared_p;
08d11e34 346 rb_dlink_node *ptr;
212380e3 347
08d11e34 348 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
212380e3 349 {
350 shared_p = ptr->data;
351
352 /* old protocol cant handle temps, and we dont really want
353 * to convert them to perm.. --fl
354 */
355 if(!temp_time)
356 {
357 if(!(shared_p->flags & SHARED_PRESV))
358 continue;
359
360 sendto_match_servs(source_p, shared_p->server,
361 CAP_CLUSTER, NOCAPS,
362 "RESV %s %s :%s",
363 shared_p->server, name, reason);
364 sendto_match_servs(source_p, shared_p->server,
365 CAP_ENCAP, CAP_CLUSTER,
366 "ENCAP %s RESV 0 %s 0 :%s",
367 shared_p->server, name, reason);
368 }
369 else if(shared_p->flags & SHARED_TRESV)
370 sendto_match_servs(source_p, shared_p->server,
371 CAP_ENCAP, NOCAPS,
372 "ENCAP %s RESV %d %s 0 :%s",
373 shared_p->server, temp_time, name, reason);
374 }
375}
376
377
378/*
379 * mo_unresv()
212380e3 380 * parv[1] = channel/nick to unforbid
381 */
382static int
383mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
384{
1ebe6ffc
JT
385 if(!IsOperResv(source_p))
386 {
387 sendto_one(source_p, form_str(ERR_NOPRIVS),
388 me.name, source_p->name, "resv");
389 return 0;
390 }
391
212380e3 392 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
393 {
394 if(!IsOperRemoteBan(source_p))
395 {
396 sendto_one(source_p, form_str(ERR_NOPRIVS),
397 me.name, source_p->name, "remoteban");
398 return 0;
399 }
400
401 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER,
402 "%s", parv[1]);
403
404 if(match(parv[3], me.name) == 0)
405 return 0;
406 }
08d11e34 407 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
212380e3 408 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER,
409 "%s", parv[1]);
410
212380e3 411 remove_resv(source_p, parv[1]);
412 return 0;
413}
414
415/* ms_unresv()
212380e3 416 * parv[1] = target server
417 * parv[2] = resv to remove
418 */
419static int
420ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
421{
77f3c1f4
JT
422 /* source_p parv[1] parv[2]
423 * oper target server resv to remove
212380e3 424 */
425 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER,
426 "%s", parv[2]);
427
428 if(!match(parv[1], me.name))
429 return 0;
430
431 if(!IsPerson(source_p))
432 return 0;
433
434 handle_remote_unresv(source_p, parv[2]);
435 return 0;
436}
437
438static int
439me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
440{
441 /* name */
442 if(!IsPerson(source_p))
443 return 0;
444
445 handle_remote_unresv(source_p, parv[1]);
446 return 0;
447}
448
449static void
450handle_remote_unresv(struct Client *source_p, const char *name)
451{
452 if(!find_shared_conf(source_p->username, source_p->host,
c88cdb00 453 source_p->servptr->name, SHARED_UNRESV))
212380e3 454 return;
455
212380e3 456 remove_resv(source_p, name);
457
458 return;
459}
460
1328da86
JT
461static void
462remove_resv(struct Client *source_p, const char *name)
212380e3 463{
464 struct ConfItem *aconf = NULL;
465
466 if(IsChannelName(name))
467 {
468 if((aconf = hash_find_resv(name)) == NULL)
1328da86
JT
469 {
470 sendto_one_notice(source_p, ":No RESV for %s", name);
471 return;
472 }
212380e3 473
212380e3 474 if(!aconf->hold)
1328da86
JT
475 {
476 if (!remove_resv_from_file(source_p, name))
477 return;
478 }
479 else
480 {
481 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
482 sendto_realops_snomask(SNO_GENERAL, L_ALL,
483 "%s has removed the RESV for: [%s]",
484 get_oper_name(source_p), name);
485 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
486 }
212380e3 487 del_from_resv_hash(name, aconf);
212380e3 488 }
489 else
490 {
08d11e34 491 rb_dlink_node *ptr;
212380e3 492
08d11e34 493 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
212380e3 494 {
495 aconf = ptr->data;
496
bf019bb0 497 if(irccmp(aconf->name, name))
212380e3 498 aconf = NULL;
499 else
500 break;
501 }
502
503 if(aconf == NULL)
1328da86
JT
504 {
505 sendto_one_notice(source_p, ":No RESV for %s", name);
506 return;
507 }
212380e3 508
212380e3 509 if(!aconf->hold)
1328da86
JT
510 {
511 if (!remove_resv_from_file(source_p, name))
512 return;
513 }
514 else
515 {
516 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
517 sendto_realops_snomask(SNO_GENERAL, L_ALL,
518 "%s has removed the RESV for: [%s]",
519 get_oper_name(source_p), name);
520 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
521 }
212380e3 522 /* already have ptr from the loop above.. */
9f6c3353 523 rb_dlinkDestroy(ptr, &resv_conf_list);
212380e3 524 }
1328da86 525 free_conf(aconf);
212380e3 526
1328da86 527 return;
212380e3 528}
529
1328da86 530/* remove_resv_from_file()
212380e3 531 *
532 * inputs - client removing the resv
533 * - resv to remove
534 * outputs -
1328da86
JT
535 * side effects - resv if found, is removed from conf
536 * - does not touch resv hash or resv_conf_list
212380e3 537 */
1328da86
JT
538static int
539remove_resv_from_file(struct Client *source_p, const char *name)
212380e3 540{
541 FILE *in, *out;
542 char buf[BUFSIZE];
543 char buff[BUFSIZE];
544 char temppath[BUFSIZE];
545 const char *filename;
546 mode_t oldumask;
547 char *p;
548 int error_on_write = 0;
549 int found_resv = 0;
550
581fa5c4 551 rb_sprintf(temppath, "%s.tmp", ConfigFileEntry.resvfile);
212380e3 552 filename = get_conf_name(RESV_TYPE);
553
554 if((in = fopen(filename, "r")) == NULL)
555 {
556 sendto_one_notice(source_p, ":Cannot open %s", filename);
1328da86 557 return 0;
212380e3 558 }
559
560 oldumask = umask(0);
561
562 if((out = fopen(temppath, "w")) == NULL)
563 {
564 sendto_one_notice(source_p, ":Cannot open %s", temppath);
565 fclose(in);
566 umask(oldumask);
1328da86 567 return 0;
212380e3 568 }
569
570 umask(oldumask);
571
572 while (fgets(buf, sizeof(buf), in))
573 {
574 const char *resv_name;
575
576 if(error_on_write)
577 {
578 if(temppath != NULL)
579 (void) unlink(temppath);
580
581 break;
582 }
583
907468c4 584 rb_strlcpy(buff, buf, sizeof(buff));
212380e3 585
586 if((p = strchr(buff, '\n')) != NULL)
587 *p = '\0';
588
589 if((*buff == '\0') || (*buff == '#'))
590 {
591 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
592 continue;
593 }
594
595 if((resv_name = getfield(buff)) == NULL)
596 {
597 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
598 continue;
599 }
600
601 if(irccmp(resv_name, name) == 0)
602 {
603 found_resv++;
604 }
605 else
606 {
607 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
608 }
609 }
610
611 fclose(in);
612 if (fclose(out))
613 error_on_write = YES;
614
615 if(error_on_write)
616 {
617 sendto_one_notice(source_p, ":Couldn't write temp resv file, aborted");
1328da86 618 return 0;
212380e3 619 }
620 else if(!found_resv)
621 {
1328da86 622 sendto_one_notice(source_p, ":Cannot find RESV for %s in file", name);
212380e3 623
624 if(temppath != NULL)
625 (void) unlink(temppath);
626
1328da86 627 return 0;
212380e3 628 }
629
630 if (rename(temppath, filename))
631 {
632 sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
1328da86 633 return 0;
212380e3 634 }
212380e3 635
636 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
637 sendto_realops_snomask(SNO_GENERAL, L_ALL,
638 "%s has removed the RESV for: [%s]", get_oper_name(source_p), name);
639 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
1328da86
JT
640
641 return 1;
212380e3 642}
100563e8
JT
643
644static void
645resv_chan_forcepart(const char *name, const char *reason, int temp_time)
646{
647 rb_dlink_node *ptr;
648 rb_dlink_node *next_ptr;
649 struct Channel *chptr;
650 struct membership *msptr;
651 struct Client *target_p;
652
653 if(!ConfigChannel.resv_forcepart)
654 return;
655
656 /* for each user on our server in the channel list
657 * send them a PART, and notify opers.
658 */
659 chptr = find_channel(name);
660 if(chptr != NULL)
661 {
662 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
663 {
664 msptr = ptr->data;
665 target_p = msptr->client_p;
666
667 if(IsExemptResv(target_p))
668 continue;
669
670 sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
671 ":%s PART %s", target_p->id, chptr->chname);
672
673 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
674 target_p->name, target_p->username,
675 target_p->host, chptr->chname, target_p->name);
676
677 remove_user_from_channel(msptr);
678
679 /* notify opers & user they were removed from the channel */
680 sendto_realops_snomask(SNO_GENERAL, L_ALL,
681 "Forced PART for %s!%s@%s from %s (%s)",
682 target_p->name, target_p->username,
683 target_p->host, name, reason);
684
685 if(temp_time > 0)
686 sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
687 name);
688 else
689 sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
690 name);
691 }
692 }
693}