Initial import
[ratbox-ambernet.git] / modules / m_resv.c
blob2ba608462db29900d953955df70310a4c337205f
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
23 * $Id: m_resv.c 21544 2006-01-03 13:19:41Z leeh $
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"
42 static int mo_resv(struct Client *, struct Client *, int, const char **);
43 static int ms_resv(struct Client *, struct Client *, int, const char **);
44 static int me_resv(struct Client *, struct Client *, int, const char **);
45 static int mo_unresv(struct Client *, struct Client *, int, const char **);
46 static int ms_unresv(struct Client *, struct Client *, int, const char **);
47 static int me_unresv(struct Client *, struct Client *, int, const char **);
49 struct 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}}
53 struct 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}}
58 mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
59 DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision: 21544 $");
61 static void parse_resv(struct Client *source_p, const char *name,
62 const char *reason, int temp_time);
63 static void propagate_resv(struct Client *source_p, const char *target,
64 int temp_time, const char *name, const char *reason);
65 static void cluster_resv(struct Client *source_p, int temp_time,
66 const char *name, const char *reason);
68 static void handle_remote_unresv(struct Client *source_p, const char *name);
69 static void remove_resv(struct Client *source_p, const char *name);
70 static int remove_temp_resv(struct Client *source_p, const char *name);
73 * mo_resv()
74 * parv[0] = sender prefix
75 * parv[1] = channel/nick to forbid
76 * parv[2] = reason
78 static int
79 mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
81 const char *name;
82 const char *reason;
83 const char *target_server = NULL;
84 int temp_time;
85 int loc = 1;
87 /* RESV [time] <name> [ON <server>] :<reason> */
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;
95 name = parv[loc];
96 loc++;
98 if((parc >= loc+2) && (irccmp(parv[loc], "ON") == 0))
100 if(!IsOperRemoteBan(source_p))
102 sendto_one(source_p, form_str(ERR_NOPRIVS),
103 me.name, source_p->name, "remoteban");
104 return 0;
107 target_server = parv[loc+1];
108 loc += 2;
111 if(parc <= loc || EmptyString(parv[loc]))
113 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
114 me.name, source_p->name, "RESV");
115 return 0;
118 reason = parv[loc];
120 /* remote resv.. */
121 if(target_server)
123 propagate_resv(source_p, target_server, temp_time, name, reason);
125 if(match(target_server, me.name) == 0)
126 return 0;
128 else if(dlink_list_length(&cluster_conf_list) > 0)
129 cluster_resv(source_p, temp_time, name, reason);
131 parse_resv(source_p, name, reason, temp_time);
133 return 0;
136 /* ms_resv()
137 * parv[0] = sender prefix
138 * parv[1] = target server
139 * parv[2] = channel/nick to forbid
140 * parv[3] = reason
142 static int
143 ms_resv(struct Client *client_p, struct Client *source_p,
144 int parc, const char *parv[])
146 /* parv[0] parv[1] parv[2] parv[3]
147 * oper target server resv reason
149 propagate_resv(source_p, parv[1], 0, parv[2], parv[3]);
151 if(!match(parv[1], me.name))
152 return 0;
154 if(!IsPerson(source_p))
155 return 0;
157 parse_resv(source_p, parv[2], parv[3], 0);
158 return 0;
161 static int
162 me_resv(struct Client *client_p, struct Client *source_p,
163 int parc, const char *parv[])
165 /* time name 0 :reason */
166 if(!IsPerson(source_p))
167 return 0;
169 parse_resv(source_p, parv[2], parv[4], atoi(parv[1]));
170 return 0;
173 /* parse_resv()
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
181 static void
182 parse_resv(struct Client *source_p, const char *name,
183 const char *reason, int temp_time)
185 struct ConfItem *aconf;
187 if(!MyClient(source_p) &&
188 !find_shared_conf(source_p->username, source_p->host,
189 source_p->servptr->name,
190 (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
191 return;
193 if(IsChannelName(name))
195 const char *p;
197 if(hash_find_resv(name))
199 sendto_one_notice(source_p,
200 ":A RESV has already been placed on channel: %s",
201 name);
202 return;
205 if(strlen(name) > CHANNELLEN)
207 sendto_one_notice(source_p, ":Invalid RESV length: %s",
208 name);
209 return;
212 for(p = name; *p; p++)
214 if(!IsChanChar(*p))
216 sendto_one_notice(source_p,
217 ":Invalid character '%c' in resv",
218 *p);
219 return;
223 if(strchr(reason, '"'))
225 sendto_one_notice(source_p,
226 ":Invalid character '\"' in comment");
227 return;
230 aconf = make_conf();
231 aconf->status = CONF_RESV_CHANNEL;
232 aconf->port = 0;
233 DupString(aconf->name, name);
234 DupString(aconf->passwd, reason);
235 add_to_resv_hash(aconf->name, aconf);
237 if(temp_time > 0)
239 aconf->hold = CurrentTime + temp_time;
241 sendto_realops_flags(UMODE_ALL, 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);
251 else
252 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
253 aconf->passwd, NULL, NULL, 0);
255 else if(clean_resv_nick(name))
257 if(strlen(name) > NICKLEN*2)
259 sendto_one_notice(source_p, ":Invalid RESV length: %s",
260 name);
261 return;
264 if(strchr(reason, '"'))
266 sendto_one_notice(source_p,
267 ":Invalid character '\"' in comment");
268 return;
271 if(!valid_wild_card_simple(name))
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;
280 if(find_nick_resv(name))
282 sendto_one_notice(source_p,
283 ":A RESV has already been placed on nick: %s",
284 name);
285 return;
288 aconf = make_conf();
289 aconf->status = CONF_RESV_NICK;
290 aconf->port = 0;
291 DupString(aconf->name, name);
292 DupString(aconf->passwd, reason);
293 dlinkAddAlloc(aconf, &resv_conf_list);
295 if(temp_time > 0)
297 aconf->hold = CurrentTime + temp_time;
299 sendto_realops_flags(UMODE_ALL, 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);
309 else
310 write_confitem(RESV_TYPE, source_p, NULL, aconf->name,
311 aconf->passwd, NULL, NULL, 0);
313 else
314 sendto_one_notice(source_p,
315 ":You have specified an invalid resv: [%s]",
316 name);
319 static void
320 propagate_resv(struct Client *source_p, const char *target,
321 int temp_time, const char *name, const char *reason)
323 if(!temp_time)
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);
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);
341 static void
342 cluster_resv(struct Client *source_p, int temp_time, const char *name,
343 const char *reason)
345 struct remote_conf *shared_p;
346 dlink_node *ptr;
348 DLINK_FOREACH(ptr, cluster_conf_list.head)
350 shared_p = ptr->data;
352 /* old protocol cant handle temps, and we dont really want
353 * to convert them to perm.. --fl
355 if(!temp_time)
357 if(!(shared_p->flags & SHARED_PRESV))
358 continue;
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);
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);
379 * mo_unresv()
380 * parv[0] = sender prefix
381 * parv[1] = channel/nick to unforbid
383 static int
384 mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
386 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
388 if(!IsOperRemoteBan(source_p))
390 sendto_one(source_p, form_str(ERR_NOPRIVS),
391 me.name, source_p->name, "remoteban");
392 return 0;
395 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER,
396 "%s", parv[1]);
398 if(match(parv[3], me.name) == 0)
399 return 0;
401 else if(dlink_list_length(&cluster_conf_list) > 0)
402 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER,
403 "%s", parv[1]);
405 if(remove_temp_resv(source_p, parv[1]))
406 return 0;
408 remove_resv(source_p, parv[1]);
409 return 0;
412 /* ms_unresv()
413 * parv[0] = sender prefix
414 * parv[1] = target server
415 * parv[2] = resv to remove
417 static int
418 ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
420 /* parv[0] parv[1] parv[2]
421 * oper target server resv to remove
423 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER,
424 "%s", parv[2]);
426 if(!match(parv[1], me.name))
427 return 0;
429 if(!IsPerson(source_p))
430 return 0;
432 handle_remote_unresv(source_p, parv[2]);
433 return 0;
436 static int
437 me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
439 /* name */
440 if(!IsPerson(source_p))
441 return 0;
443 handle_remote_unresv(source_p, parv[1]);
444 return 0;
447 static void
448 handle_remote_unresv(struct Client *source_p, const char *name)
450 if(!find_shared_conf(source_p->username, source_p->host,
451 source_p->servptr->name, SHARED_UNRESV))
452 return;
454 if(remove_temp_resv(source_p, name))
455 return;
457 remove_resv(source_p, name);
459 return;
462 static int
463 remove_temp_resv(struct Client *source_p, const char *name)
465 struct ConfItem *aconf = NULL;
467 if(IsChannelName(name))
469 if((aconf = hash_find_resv(name)) == NULL)
470 return 0;
472 /* its permanent, let remove_resv do it properly */
473 if(!aconf->hold)
474 return 0;
476 del_from_resv_hash(name, aconf);
477 free_conf(aconf);
479 else
481 dlink_node *ptr;
483 DLINK_FOREACH(ptr, resv_conf_list.head)
485 aconf = ptr->data;
487 if(irccmp(aconf->name, name))
488 aconf = NULL;
489 else
490 break;
493 if(aconf == NULL)
494 return 0;
496 /* permanent, remove_resv() needs to do it properly */
497 if(!aconf->hold)
498 return 0;
500 /* already have ptr from the loop above.. */
501 dlinkDestroy(ptr, &resv_conf_list);
502 free_conf(aconf);
505 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
506 sendto_realops_flags(UMODE_ALL, L_ALL,
507 "%s has removed the RESV for: [%s]",
508 get_oper_name(source_p), name);
509 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
511 return 1;
514 /* remove_resv()
516 * inputs - client removing the resv
517 * - resv to remove
518 * outputs -
519 * side effects - resv if found, is removed
521 static void
522 remove_resv(struct Client *source_p, const char *name)
524 FILE *in, *out;
525 char buf[BUFSIZE];
526 char buff[BUFSIZE];
527 char temppath[BUFSIZE];
528 const char *filename;
529 mode_t oldumask;
530 char *p;
531 int error_on_write = 0;
532 int found_resv = 0;
534 ircsprintf(temppath, "%s.tmp", ConfigFileEntry.resvfile);
535 filename = get_conf_name(RESV_TYPE);
537 if((in = fopen(filename, "r")) == NULL)
539 sendto_one_notice(source_p, ":Cannot open %s", filename);
540 return;
543 oldumask = umask(0);
545 if((out = fopen(temppath, "w")) == NULL)
547 sendto_one_notice(source_p, ":Cannot open %s", temppath);
548 fclose(in);
549 umask(oldumask);
550 return;
553 umask(oldumask);
555 while (fgets(buf, sizeof(buf), in))
557 const char *resv_name;
559 if(error_on_write)
561 if(temppath != NULL)
562 (void) unlink(temppath);
564 break;
567 strlcpy(buff, buf, sizeof(buff));
569 if((p = strchr(buff, '\n')) != NULL)
570 *p = '\0';
572 if((*buff == '\0') || (*buff == '#'))
574 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
575 continue;
578 if((resv_name = getfield(buff)) == NULL)
580 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
581 continue;
584 if(irccmp(resv_name, name) == 0)
586 found_resv++;
588 else
590 error_on_write = (fputs(buf, out) < 0) ? YES : NO;
594 fclose(in);
595 fclose(out);
597 if(error_on_write)
599 sendto_one_notice(source_p, ":Couldn't write temp resv file, aborted");
600 return;
602 else if(!found_resv)
604 sendto_one_notice(source_p, ":No RESV for %s", name);
606 if(temppath != NULL)
607 (void) unlink(temppath);
609 return;
612 (void) rename(temppath, filename);
613 rehash_bans(0);
615 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
616 sendto_realops_flags(UMODE_ALL, L_ALL,
617 "%s has removed the RESV for: [%s]", get_oper_name(source_p), name);
618 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);