]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_resv.c
Speed up /unresv. Rehash bans now only happens if explicitly requested.
[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"
39#include "s_log.h"
40#include "sprintf_irc.h"
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};
53struct Message unresv_msgtab = {
54 "UNRESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
55 {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
56};
57
58mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
59DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision: 3045 $");
60
61static void parse_resv(struct Client *source_p, const char *name,
62 const char *reason, int temp_time);
63static void propagate_resv(struct Client *source_p, const char *target,
64 int temp_time, const char *name, const char *reason);
65static void cluster_resv(struct Client *source_p, int temp_time,
66 const char *name, const char *reason);
67
68static void handle_remote_unresv(struct Client *source_p, const char *name);
69static void remove_resv(struct Client *source_p, const char *name);
1328da86 70static int remove_resv_from_file(struct Client *source_p, const char *name);
212380e3 71
72/*
73 * mo_resv()
74 * parv[0] = sender prefix
75 * parv[1] = channel/nick to forbid
76 * parv[2] = reason
77 */
78static int
79mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
80{
81 const char *name;
82 const char *reason;
83 const char *target_server = NULL;
84 int temp_time;
85 int loc = 1;
86
87 /* RESV [time] <name> [ON <server>] :<reason> */
88
89 if((temp_time = valid_temp_time(parv[loc])) >= 0)
90 loc++;
91 /* we just set temp_time to -1! */
92 else
93 temp_time = 0;
94
95 name = parv[loc];
96 loc++;
97
98 if((parc >= loc+2) && (irccmp(parv[loc], "ON") == 0))
99 {
100 if(!IsOperRemoteBan(source_p))
101 {
102 sendto_one(source_p, form_str(ERR_NOPRIVS),
103 me.name, source_p->name, "remoteban");
104 return 0;
105 }
106
107 target_server = parv[loc+1];
108 loc += 2;
109 }
110
111 if(parc <= loc || EmptyString(parv[loc]))
112 {
113 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
114 me.name, source_p->name, "RESV");
115 return 0;
116 }
117
118 reason = parv[loc];
119
120 /* remote resv.. */
121 if(target_server)
122 {
123 propagate_resv(source_p, target_server, temp_time, name, reason);
124
125 if(match(target_server, me.name) == 0)
126 return 0;
127 }
128 else if(dlink_list_length(&cluster_conf_list) > 0)
129 cluster_resv(source_p, temp_time, name, reason);
130
131 parse_resv(source_p, name, reason, temp_time);
132
133 return 0;
134}
135
136/* ms_resv()
137 * parv[0] = sender prefix
138 * parv[1] = target server
139 * parv[2] = channel/nick to forbid
140 * parv[3] = reason
141 */
142static int
143ms_resv(struct Client *client_p, struct Client *source_p,
144 int parc, const char *parv[])
145{
146 /* parv[0] parv[1] parv[2] parv[3]
147 * oper target server resv reason
148 */
149 propagate_resv(source_p, parv[1], 0, parv[2], parv[3]);
150
151 if(!match(parv[1], me.name))
152 return 0;
153
154 if(!IsPerson(source_p))
155 return 0;
156
157 parse_resv(source_p, parv[2], parv[3], 0);
158 return 0;
159}
160
161static int
162me_resv(struct Client *client_p, struct Client *source_p,
163 int parc, const char *parv[])
164{
165 /* time name 0 :reason */
166 if(!IsPerson(source_p))
167 return 0;
168
169 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]));
170 return 0;
171}
172
173/* parse_resv()
174 *
175 * inputs - source_p if error messages wanted
176 * - thing to resv
177 * - reason for resv
178 * outputs -
179 * side effects - will parse the resv and create it if valid
180 */
181static void
182parse_resv(struct Client *source_p, const char *name,
183 const char *reason, int temp_time)
184{
185 struct ConfItem *aconf;
186
187 if(!MyClient(source_p) &&
188 !find_shared_conf(source_p->username, source_p->host,
c88cdb00 189 source_p->servptr->name,
212380e3 190 (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
191 return;
192
193 if(IsChannelName(name))
194 {
195 if(hash_find_resv(name))
196 {
197 sendto_one_notice(source_p,
198 ":A RESV has already been placed on channel: %s",
199 name);
200 return;
201 }
202
203 if(strlen(name) > CHANNELLEN)
204 {
205 sendto_one_notice(source_p, ":Invalid RESV length: %s",
206 name);
207 return;
208 }
209
210 if(strchr(reason, '"'))
211 {
212 sendto_one_notice(source_p,
213 ":Invalid character '\"' in comment");
214 return;
215 }
216
217 aconf = make_conf();
218 aconf->status = CONF_RESV_CHANNEL;
219 aconf->port = 0;
220 DupString(aconf->name, name);
221 DupString(aconf->passwd, reason);
222 add_to_resv_hash(aconf->name, aconf);
223
224 if(temp_time > 0)
225 {
226 aconf->hold = CurrentTime + temp_time;
227
228 sendto_realops_snomask(SNO_GENERAL, L_ALL,
229 "%s added temporary %d min. RESV for [%s] [%s]",
230 get_oper_name(source_p), temp_time / 60,
231 name, reason);
232 ilog(L_KLINE, "R %s %d %s %s",
233 get_oper_name(source_p), temp_time / 60,
234 name, reason);
235 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
236 temp_time / 60, name);
237 }
238 else
239 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
240 aconf->passwd, NULL, NULL, 0);
241 }
242 else if(clean_resv_nick(name))
243 {
244 if(strlen(name) > NICKLEN*2)
245 {
246 sendto_one_notice(source_p, ":Invalid RESV length: %s",
247 name);
248 return;
249 }
250
251 if(strchr(reason, '"'))
252 {
253 sendto_one_notice(source_p,
254 ":Invalid character '\"' in comment");
255 return;
256 }
257
258 if(!valid_wild_card_simple(name))
259 {
260 sendto_one_notice(source_p,
261 ":Please include at least %d non-wildcard "
262 "characters with the resv",
263 ConfigFileEntry.min_nonwildcard_simple);
264 return;
265 }
266
0fdb2570 267 if(find_nick_resv_mask(name))
212380e3 268 {
269 sendto_one_notice(source_p,
270 ":A RESV has already been placed on nick: %s",
271 name);
272 return;
273 }
274
275 aconf = make_conf();
276 aconf->status = CONF_RESV_NICK;
277 aconf->port = 0;
278 DupString(aconf->name, name);
279 DupString(aconf->passwd, reason);
280 dlinkAddAlloc(aconf, &resv_conf_list);
281
282 if(temp_time > 0)
283 {
284 aconf->hold = CurrentTime + temp_time;
285
286 sendto_realops_snomask(SNO_GENERAL, L_ALL,
287 "%s added temporary %d min. RESV for [%s] [%s]",
288 get_oper_name(source_p), temp_time / 60,
289 name, reason);
290 ilog(L_KLINE, "R %s %d %s %s",
291 get_oper_name(source_p), temp_time / 60,
292 name, reason);
293 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
294 temp_time / 60, name);
295 }
296 else
297 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
298 aconf->passwd, NULL, NULL, 0);
299 }
300 else
301 sendto_one_notice(source_p,
302 ":You have specified an invalid resv: [%s]",
303 name);
304}
305
306static void
307propagate_resv(struct Client *source_p, const char *target,
308 int temp_time, const char *name, const char *reason)
309{
310 if(!temp_time)
311 {
312 sendto_match_servs(source_p, target,
313 CAP_CLUSTER, NOCAPS,
314 "RESV %s %s :%s",
315 target, name, reason);
316 sendto_match_servs(source_p, target,
317 CAP_ENCAP, CAP_CLUSTER,
318 "ENCAP %s RESV %d %s 0 :%s",
319 target, temp_time, name, reason);
320 }
321 else
322 sendto_match_servs(source_p, target,
323 CAP_ENCAP, NOCAPS,
324 "ENCAP %s RESV %d %s 0 :%s",
325 target, temp_time, name, reason);
326}
327
328static void
329cluster_resv(struct Client *source_p, int temp_time, const char *name,
330 const char *reason)
331{
332 struct remote_conf *shared_p;
333 dlink_node *ptr;
334
335 DLINK_FOREACH(ptr, cluster_conf_list.head)
336 {
337 shared_p = ptr->data;
338
339 /* old protocol cant handle temps, and we dont really want
340 * to convert them to perm.. --fl
341 */
342 if(!temp_time)
343 {
344 if(!(shared_p->flags & SHARED_PRESV))
345 continue;
346
347 sendto_match_servs(source_p, shared_p->server,
348 CAP_CLUSTER, NOCAPS,
349 "RESV %s %s :%s",
350 shared_p->server, name, reason);
351 sendto_match_servs(source_p, shared_p->server,
352 CAP_ENCAP, CAP_CLUSTER,
353 "ENCAP %s RESV 0 %s 0 :%s",
354 shared_p->server, name, reason);
355 }
356 else if(shared_p->flags & SHARED_TRESV)
357 sendto_match_servs(source_p, shared_p->server,
358 CAP_ENCAP, NOCAPS,
359 "ENCAP %s RESV %d %s 0 :%s",
360 shared_p->server, temp_time, name, reason);
361 }
362}
363
364
365/*
366 * mo_unresv()
367 * parv[0] = sender prefix
368 * parv[1] = channel/nick to unforbid
369 */
370static int
371mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
372{
373 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
374 {
375 if(!IsOperRemoteBan(source_p))
376 {
377 sendto_one(source_p, form_str(ERR_NOPRIVS),
378 me.name, source_p->name, "remoteban");
379 return 0;
380 }
381
382 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER,
383 "%s", parv[1]);
384
385 if(match(parv[3], me.name) == 0)
386 return 0;
387 }
388 else if(dlink_list_length(&cluster_conf_list) > 0)
389 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER,
390 "%s", parv[1]);
391
212380e3 392 remove_resv(source_p, parv[1]);
393 return 0;
394}
395
396/* ms_unresv()
397 * parv[0] = sender prefix
398 * parv[1] = target server
399 * parv[2] = resv to remove
400 */
401static int
402ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
403{
404 /* parv[0] parv[1] parv[2]
405 * oper target server resv to remove
406 */
407 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER,
408 "%s", parv[2]);
409
410 if(!match(parv[1], me.name))
411 return 0;
412
413 if(!IsPerson(source_p))
414 return 0;
415
416 handle_remote_unresv(source_p, parv[2]);
417 return 0;
418}
419
420static int
421me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
422{
423 /* name */
424 if(!IsPerson(source_p))
425 return 0;
426
427 handle_remote_unresv(source_p, parv[1]);
428 return 0;
429}
430
431static void
432handle_remote_unresv(struct Client *source_p, const char *name)
433{
434 if(!find_shared_conf(source_p->username, source_p->host,
c88cdb00 435 source_p->servptr->name, SHARED_UNRESV))
212380e3 436 return;
437
212380e3 438 remove_resv(source_p, name);
439
440 return;
441}
442
1328da86
JT
443static void
444remove_resv(struct Client *source_p, const char *name)
212380e3 445{
446 struct ConfItem *aconf = NULL;
447
448 if(IsChannelName(name))
449 {
450 if((aconf = hash_find_resv(name)) == NULL)
1328da86
JT
451 {
452 sendto_one_notice(source_p, ":No RESV for %s", name);
453 return;
454 }
212380e3 455
212380e3 456 if(!aconf->hold)
1328da86
JT
457 {
458 if (!remove_resv_from_file(source_p, name))
459 return;
460 }
461 else
462 {
463 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
464 sendto_realops_snomask(SNO_GENERAL, L_ALL,
465 "%s has removed the RESV for: [%s]",
466 get_oper_name(source_p), name);
467 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
468 }
212380e3 469 del_from_resv_hash(name, aconf);
212380e3 470 }
471 else
472 {
473 dlink_node *ptr;
474
475 DLINK_FOREACH(ptr, resv_conf_list.head)
476 {
477 aconf = ptr->data;
478
479 if(irccmp(aconf->name, name))
480 aconf = NULL;
481 else
482 break;
483 }
484
485 if(aconf == NULL)
1328da86
JT
486 {
487 sendto_one_notice(source_p, ":No RESV for %s", name);
488 return;
489 }
212380e3 490
212380e3 491 if(!aconf->hold)
1328da86
JT
492 {
493 if (!remove_resv_from_file(source_p, name))
494 return;
495 }
496 else
497 {
498 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
499 sendto_realops_snomask(SNO_GENERAL, L_ALL,
500 "%s has removed the RESV for: [%s]",
501 get_oper_name(source_p), name);
502 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
503 }
212380e3 504 /* already have ptr from the loop above.. */
505 dlinkDestroy(ptr, &resv_conf_list);
212380e3 506 }
1328da86 507 free_conf(aconf);
212380e3 508
1328da86 509 return;
212380e3 510}
511
1328da86 512/* remove_resv_from_file()
212380e3 513 *
514 * inputs - client removing the resv
515 * - resv to remove
516 * outputs -
1328da86
JT
517 * side effects - resv if found, is removed from conf
518 * - does not touch resv hash or resv_conf_list
212380e3 519 */
1328da86
JT
520static int
521remove_resv_from_file(struct Client *source_p, const char *name)
212380e3 522{
523 FILE *in, *out;
524 char buf[BUFSIZE];
525 char buff[BUFSIZE];
526 char temppath[BUFSIZE];
527 const char *filename;
528 mode_t oldumask;
529 char *p;
530 int error_on_write = 0;
531 int found_resv = 0;
532
533 ircsprintf(temppath, "%s.tmp", ConfigFileEntry.resvfile);
534 filename = get_conf_name(RESV_TYPE);
535
536 if((in = fopen(filename, "r")) == NULL)
537 {
538 sendto_one_notice(source_p, ":Cannot open %s", filename);
1328da86 539 return 0;
212380e3 540 }
541
542 oldumask = umask(0);
543
544 if((out = fopen(temppath, "w")) == NULL)
545 {
546 sendto_one_notice(source_p, ":Cannot open %s", temppath);
547 fclose(in);
548 umask(oldumask);
1328da86 549 return 0;
212380e3 550 }
551
552 umask(oldumask);
553
554 while (fgets(buf, sizeof(buf), in))
555 {
556 const char *resv_name;
557
558 if(error_on_write)
559 {
560 if(temppath != NULL)
561 (void) unlink(temppath);
562
563 break;
564 }
565
566 strlcpy(buff, buf, sizeof(buff));
567
568 if((p = strchr(buff, '\n')) != NULL)
569 *p = '\0';
570
571 if((*buff == '\0') || (*buff == '#'))
572 {
573 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
574 continue;
575 }
576
577 if((resv_name = getfield(buff)) == NULL)
578 {
579 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
580 continue;
581 }
582
583 if(irccmp(resv_name, name) == 0)
584 {
585 found_resv++;
586 }
587 else
588 {
589 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
590 }
591 }
592
593 fclose(in);
594 if (fclose(out))
595 error_on_write = YES;
596
597 if(error_on_write)
598 {
599 sendto_one_notice(source_p, ":Couldn't write temp resv file, aborted");
1328da86 600 return 0;
212380e3 601 }
602 else if(!found_resv)
603 {
1328da86 604 sendto_one_notice(source_p, ":Cannot find RESV for %s in file", name);
212380e3 605
606 if(temppath != NULL)
607 (void) unlink(temppath);
608
1328da86 609 return 0;
212380e3 610 }
611
612 if (rename(temppath, filename))
613 {
614 sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
1328da86 615 return 0;
212380e3 616 }
212380e3 617
618 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
619 sendto_realops_snomask(SNO_GENERAL, L_ALL,
620 "%s has removed the RESV for: [%s]", get_oper_name(source_p), name);
621 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
1328da86
JT
622
623 return 1;
212380e3 624}