Remove cruft left behind by removed stats_p_hook.
[seven-1.x.git] / modules / m_resv.c
blob6c80b872e03999848af216a743b7f51509eb7ff3
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_resv.c: Reserves(jupes) a nickname or channel.
5 * Copyright (C) 2001-2002 Hybrid Development Team
6 * Copyright (C) 2002-2005 ircd-ratbox development team
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.
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.
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
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 "s_log.h"
38 #include "sprintf_irc.h"
40 static int mo_resv(struct Client *, struct Client *, int, const char **);
41 static int ms_resv(struct Client *, struct Client *, int, const char **);
42 static int me_resv(struct Client *, struct Client *, int, const char **);
43 static int mo_unresv(struct Client *, struct Client *, int, const char **);
44 static int ms_unresv(struct Client *, struct Client *, int, const char **);
45 static int me_unresv(struct Client *, struct Client *, int, const char **);
47 struct Message resv_msgtab = {
48 "RESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
49 {mg_ignore, mg_not_oper, {ms_resv, 4}, {ms_resv, 4}, {me_resv, 5}, {mo_resv, 3}}
51 struct Message unresv_msgtab = {
52 "UNRESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
53 {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
56 mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
57 DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision: 125 $");
59 static void parse_resv(struct Client *source_p, const char *name,
60 const char *reason, int temp_time);
61 static void propagate_resv(struct Client *source_p, const char *target,
62 int temp_time, const char *name, const char *reason);
63 static void cluster_resv(struct Client *source_p, int temp_time,
64 const char *name, const char *reason);
66 static void handle_remote_unresv(struct Client *source_p, const char *name);
67 static void remove_resv(struct Client *source_p, const char *name);
68 static int remove_temp_resv(struct Client *source_p, const char *name);
71 * mo_resv()
72 * parv[0] = sender prefix
73 * parv[1] = channel/nick to forbid
74 * parv[2] = reason
76 static int
77 mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
79 const char *name;
80 const char *reason;
81 const char *target_server = NULL;
82 int temp_time;
83 int loc = 1;
85 if(!IsOperResv(source_p))
87 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
88 return 0;
91 /* RESV [time] <name> [ON <server>] :<reason> */
93 if((temp_time = valid_temp_time(parv[loc])) >= 0)
94 loc++;
95 /* we just set temp_time to -1! */
96 else
97 temp_time = 0;
99 name = parv[loc];
100 loc++;
102 if((parc >= loc+2) && (irccmp(parv[loc], "ON") == 0))
104 target_server = parv[loc+1];
105 loc += 2;
108 if(parc <= loc || EmptyString(parv[loc]))
110 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
111 me.name, source_p->name, "RESV");
112 return 0;
115 reason = parv[loc];
117 /* remote resv.. */
118 if(target_server)
120 if(!IsOperRemoteBan(source_p))
122 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
123 "remoteban");
124 return 0;
127 if(temp_time > 0)
128 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
129 "%s is setting a temporary %d-minute RESV for %s on %s",
130 get_oper_name(source_p), temp_time, name, target_server);
131 else
132 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
133 "%s is setting a RESV for %s on %s",
134 get_oper_name(source_p), name, target_server);
137 propagate_resv(source_p, target_server, temp_time, name, reason);
139 if(match(target_server, me.name) == 0)
140 return 0;
142 else if(dlink_list_length(&cluster_conf_list) > 0)
143 cluster_resv(source_p, temp_time, name, reason);
145 parse_resv(source_p, name, reason, temp_time);
147 return 0;
150 /* ms_resv()
151 * parv[0] = sender prefix
152 * parv[1] = target server
153 * parv[2] = channel/nick to forbid
154 * parv[3] = reason
156 static int
157 ms_resv(struct Client *client_p, struct Client *source_p,
158 int parc, const char *parv[])
160 /* parv[0] parv[1] parv[2] parv[3]
161 * oper target server resv reason
163 propagate_resv(source_p, parv[1], 0, parv[2], parv[3]);
165 if(!match(parv[1], me.name))
166 return 0;
168 if(!IsPerson(source_p))
169 return 0;
171 parse_resv(source_p, parv[2], parv[3], 0);
172 return 0;
175 static int
176 me_resv(struct Client *client_p, struct Client *source_p,
177 int parc, const char *parv[])
179 /* time name 0 :reason */
180 if(!IsPerson(source_p))
181 return 0;
183 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]));
184 return 0;
187 /* parse_resv()
189 * inputs - source_p if error messages wanted
190 * - thing to resv
191 * - reason for resv
192 * outputs -
193 * side effects - will parse the resv and create it if valid
195 static void
196 parse_resv(struct Client *source_p, const char *name,
197 const char *reason, int temp_time)
199 struct ConfItem *aconf;
201 if (!MyClient(source_p) &&
202 !find_client_shared_conf(source_p, temp_time > 0)? SHARED_TRESV : SHARED_PRESV)
203 return;
205 if(IsChannelName(name))
207 if(hash_find_resv(name))
209 sendto_one_notice(source_p,
210 ":A RESV has already been placed on channel: %s",
211 name);
212 return;
215 if(strlen(name) > CHANNELLEN)
217 sendto_one_notice(source_p, ":Invalid RESV length: %s",
218 name);
219 return;
222 if(strchr(reason, '"'))
224 sendto_one_notice(source_p,
225 ":Invalid character '\"' in comment");
226 return;
229 aconf = make_conf();
230 aconf->status = CONF_RESV_CHANNEL;
231 aconf->port = 0;
232 DupString(aconf->name, name);
233 DupString(aconf->passwd, reason);
234 add_to_resv_hash(aconf->name, aconf);
236 if(temp_time > 0)
238 aconf->hold = CurrentTime + temp_time;
240 sendto_realops_snomask(SNO_GENERAL, L_ALL,
241 "%s added temporary %d min. RESV for [%s] [%s]",
242 get_oper_name(source_p), temp_time / 60,
243 name, reason);
244 ilog(L_KLINE, "R %s %d %s %s",
245 get_oper_name(source_p), temp_time / 60,
246 name, reason);
247 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
248 temp_time / 60, name);
250 else
251 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
252 aconf->passwd, NULL, NULL, 0);
254 else if(clean_resv_nick(name))
256 if(strlen(name) > NICKLEN*2)
258 sendto_one_notice(source_p, ":Invalid RESV length: %s",
259 name);
260 return;
263 if(strchr(reason, '"'))
265 sendto_one_notice(source_p,
266 ":Invalid character '\"' in comment");
267 return;
270 if(!valid_wild_card_simple(name))
272 sendto_one_notice(source_p,
273 ":Please include at least %d non-wildcard "
274 "characters with the resv",
275 ConfigFileEntry.min_nonwildcard_simple);
276 return;
279 if(find_nick_resv(name))
281 sendto_one_notice(source_p,
282 ":A RESV has already been placed on nick: %s",
283 name);
284 return;
287 aconf = make_conf();
288 aconf->status = CONF_RESV_NICK;
289 aconf->port = 0;
290 DupString(aconf->name, name);
291 DupString(aconf->passwd, reason);
292 dlinkAddAlloc(aconf, &resv_conf_list);
294 if(temp_time > 0)
296 aconf->hold = CurrentTime + temp_time;
298 sendto_realops_snomask(SNO_GENERAL, L_ALL,
299 "%s added temporary %d min. RESV for [%s] [%s]",
300 get_oper_name(source_p), temp_time / 60,
301 name, reason);
302 ilog(L_KLINE, "R %s %d %s %s",
303 get_oper_name(source_p), temp_time / 60,
304 name, reason);
305 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
306 temp_time / 60, name);
308 else
309 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
310 aconf->passwd, NULL, NULL, 0);
312 else
313 sendto_one_notice(source_p,
314 ":You have specified an invalid resv: [%s]",
315 name);
318 static void
319 propagate_resv(struct Client *source_p, const char *target,
320 int temp_time, const char *name, const char *reason)
322 if(!temp_time)
324 sendto_match_servs(source_p, target,
325 CAP_CLUSTER, NOCAPS,
326 "RESV %s %s :%s",
327 target, name, reason);
328 sendto_match_servs(source_p, target,
329 CAP_ENCAP, CAP_CLUSTER,
330 "ENCAP %s RESV %d %s 0 :%s",
331 target, temp_time, name, reason);
333 else
334 sendto_match_servs(source_p, target,
335 CAP_ENCAP, NOCAPS,
336 "ENCAP %s RESV %d %s 0 :%s",
337 target, temp_time, name, reason);
340 static void
341 cluster_resv(struct Client *source_p, int temp_time, const char *name,
342 const char *reason)
344 struct remote_conf *shared_p;
345 dlink_node *ptr;
347 DLINK_FOREACH(ptr, cluster_conf_list.head)
349 shared_p = ptr->data;
351 /* old protocol cant handle temps, and we dont really want
352 * to convert them to perm.. --fl
354 if(!temp_time)
356 if(!(shared_p->flags & SHARED_PRESV))
357 continue;
359 sendto_match_servs(source_p, shared_p->server,
360 CAP_CLUSTER, NOCAPS,
361 "RESV %s %s :%s",
362 shared_p->server, name, reason);
363 sendto_match_servs(source_p, shared_p->server,
364 CAP_ENCAP, CAP_CLUSTER,
365 "ENCAP %s RESV 0 %s 0 :%s",
366 shared_p->server, name, reason);
368 else if(shared_p->flags & SHARED_TRESV)
369 sendto_match_servs(source_p, shared_p->server,
370 CAP_ENCAP, NOCAPS,
371 "ENCAP %s RESV %d %s 0 :%s",
372 shared_p->server, temp_time, name, reason);
378 * mo_unresv()
379 * parv[0] = sender prefix
380 * parv[1] = channel/nick to unforbid
382 static int
383 mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
385 if(!IsOperResv(source_p))
387 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
388 return 0;
391 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
393 if(!IsOperRemoteBan(source_p))
395 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
396 "remoteban");
398 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER,
399 "%s", parv[1]);
401 if(match(parv[3], me.name) == 0)
402 return 0;
405 else if(dlink_list_length(&cluster_conf_list) > 0)
406 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER,
407 "%s", parv[1]);
409 if(remove_temp_resv(source_p, parv[1]))
410 return 0;
412 remove_resv(source_p, parv[1]);
413 return 0;
416 /* ms_unresv()
417 * parv[0] = sender prefix
418 * parv[1] = target server
419 * parv[2] = resv to remove
421 static int
422 ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
424 /* parv[0] parv[1] parv[2]
425 * oper target server resv to remove
427 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER,
428 "%s", parv[2]);
430 if(!match(parv[1], me.name))
431 return 0;
433 if(!IsPerson(source_p))
434 return 0;
436 handle_remote_unresv(source_p, parv[2]);
437 return 0;
440 static int
441 me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
443 /* name */
444 if(!IsPerson(source_p))
445 return 0;
447 handle_remote_unresv(source_p, parv[1]);
448 return 0;
451 static void
452 handle_remote_unresv(struct Client *source_p, const char *name)
454 if (!find_client_shared_conf(source_p, SHARED_UNRESV))
455 return;
456 if(remove_temp_resv(source_p, name))
457 return;
459 remove_resv(source_p, name);
461 return;
464 static int
465 remove_temp_resv(struct Client *source_p, const char *name)
467 struct ConfItem *aconf = NULL;
469 if(IsChannelName(name))
471 if((aconf = hash_find_resv(name)) == NULL)
472 return 0;
474 /* its permanent, let remove_resv do it properly */
475 if(!aconf->hold)
476 return 0;
478 del_from_resv_hash(name, aconf);
479 free_conf(aconf);
481 else
483 dlink_node *ptr;
485 DLINK_FOREACH(ptr, resv_conf_list.head)
487 aconf = ptr->data;
489 if(irccmp(aconf->name, name))
490 aconf = NULL;
491 else
492 break;
495 if(aconf == NULL)
496 return 0;
498 /* permanent, remove_resv() needs to do it properly */
499 if(!aconf->hold)
500 return 0;
502 /* already have ptr from the loop above.. */
503 dlinkDestroy(ptr, &resv_conf_list);
504 free_conf(aconf);
507 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
508 sendto_realops_snomask(SNO_GENERAL, L_ALL,
509 "%s has removed the RESV for: [%s]",
510 get_oper_name(source_p), name);
511 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
513 return 1;
516 /* remove_resv()
518 * inputs - client removing the resv
519 * - resv to remove
520 * outputs -
521 * side effects - resv if found, is removed
523 static void
524 remove_resv(struct Client *source_p, const char *name)
526 FILE *in, *out;
527 char buf[BUFSIZE];
528 char buff[BUFSIZE];
529 char temppath[BUFSIZE];
530 const char *filename;
531 mode_t oldumask;
532 char *p;
533 int error_on_write = 0;
534 int found_resv = 0;
536 ircsprintf(temppath, "%s.tmp", ConfigFileEntry.resvfile);
537 filename = get_conf_name(RESV_TYPE);
539 if((in = fopen(filename, "r")) == NULL)
541 sendto_one_notice(source_p, ":Cannot open %s", filename);
542 return;
545 oldumask = umask(0);
547 if((out = fopen(temppath, "w")) == NULL)
549 sendto_one_notice(source_p, ":Cannot open %s", temppath);
550 fclose(in);
551 umask(oldumask);
552 return;
555 umask(oldumask);
557 while (fgets(buf, sizeof(buf), in))
559 const char *resv_name;
561 if(error_on_write)
563 if(temppath != NULL)
564 (void) unlink(temppath);
566 break;
569 strlcpy(buff, buf, sizeof(buff));
571 if((p = strchr(buff, '\n')) != NULL)
572 *p = '\0';
574 if((*buff == '\0') || (*buff == '#'))
576 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
577 continue;
580 if((resv_name = getfield(buff)) == NULL)
582 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
583 continue;
586 if(irccmp(resv_name, name) == 0)
588 found_resv++;
590 else
592 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
596 fclose(in);
597 fclose(out);
599 if(error_on_write)
601 sendto_one_notice(source_p, ":Couldn't write temp resv file, aborted");
602 return;
604 else if(!found_resv)
606 sendto_one_notice(source_p, ":No RESV for %s", name);
608 if(temppath != NULL)
609 (void) unlink(temppath);
611 return;
614 (void) rename(temppath, filename);
615 rehash_bans(0);
617 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
618 sendto_realops_snomask(SNO_GENERAL, L_ALL,
619 "%s has removed the RESV for: [%s]", get_oper_name(source_p), name);
620 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);