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