]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_resv.c
Fix up more things with bandb.
[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 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
239 }
240 else if(clean_resv_nick(name))
241 {
242 if(strlen(name) > NICKLEN * 2)
243 {
244 sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
245 return;
246 }
247
248 if(strchr(reason, '"'))
249 {
250 sendto_one_notice(source_p, ":Invalid character '\"' in comment");
251 return;
252 }
253
254 if(!valid_wild_card_simple(name))
255 {
256 sendto_one_notice(source_p,
257 ":Please include at least %d non-wildcard "
258 "characters with the resv",
259 ConfigFileEntry.min_nonwildcard_simple);
260 return;
261 }
262
263 if(find_nick_resv_mask(name))
264 {
265 sendto_one_notice(source_p,
266 ":A RESV has already been placed on nick: %s", name);
267 return;
268 }
269
270 aconf = make_conf();
271 aconf->status = CONF_RESV_NICK;
272 aconf->port = 0;
273 aconf->host = rb_strdup(name);
274 aconf->passwd = rb_strdup(reason);
275 rb_dlinkAddAlloc(aconf, &resv_conf_list);
276
277 if(temp_time > 0)
278 {
279 aconf->hold = rb_current_time() + temp_time;
280
281 sendto_realops_snomask(SNO_GENERAL, L_ALL,
282 "%s added temporary %d min. RESV for [%s] [%s]",
283 get_oper_name(source_p), temp_time / 60,
284 name, reason);
285 ilog(L_KLINE, "R %s %d %s %s",
286 get_oper_name(source_p), temp_time / 60, name, reason);
287 sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
288 temp_time / 60, name);
289 }
290 else
291 bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
292 }
293 else
294 sendto_one_notice(source_p, ":You have specified an invalid resv: [%s]", name);
295 }
296
297 static void
298 propagate_resv(struct Client *source_p, const char *target,
299 int temp_time, const char *name, const char *reason)
300 {
301 if(!temp_time)
302 {
303 sendto_match_servs(source_p, target,
304 CAP_CLUSTER, NOCAPS, "RESV %s %s :%s", target, name, reason);
305 sendto_match_servs(source_p, target,
306 CAP_ENCAP, CAP_CLUSTER,
307 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
308 }
309 else
310 sendto_match_servs(source_p, target,
311 CAP_ENCAP, NOCAPS,
312 "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
313 }
314
315 static void
316 cluster_resv(struct Client *source_p, int temp_time, const char *name, const char *reason)
317 {
318 struct remote_conf *shared_p;
319 rb_dlink_node *ptr;
320
321 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
322 {
323 shared_p = ptr->data;
324
325 /* old protocol cant handle temps, and we dont really want
326 * to convert them to perm.. --fl
327 */
328 if(!temp_time)
329 {
330 if(!(shared_p->flags & SHARED_PRESV))
331 continue;
332
333 sendto_match_servs(source_p, shared_p->server,
334 CAP_CLUSTER, NOCAPS,
335 "RESV %s %s :%s", shared_p->server, name, reason);
336 sendto_match_servs(source_p, shared_p->server,
337 CAP_ENCAP, CAP_CLUSTER,
338 "ENCAP %s RESV 0 %s 0 :%s",
339 shared_p->server, name, reason);
340 }
341 else if(shared_p->flags & SHARED_TRESV)
342 sendto_match_servs(source_p, shared_p->server,
343 CAP_ENCAP, NOCAPS,
344 "ENCAP %s RESV %d %s 0 :%s",
345 shared_p->server, temp_time, name, reason);
346 }
347 }
348
349
350 /*
351 * mo_unresv()
352 * parv[0] = sender prefix
353 * parv[1] = channel/nick to unforbid
354 */
355 static int
356 mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
357 {
358 if(!IsOperResv(source_p))
359 {
360 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
361 return 0;
362 }
363
364 if((parc == 4) && (irccmp(parv[2], "ON") == 0))
365 {
366 if(!IsOperRemoteBan(source_p))
367 {
368 sendto_one(source_p, form_str(ERR_NOPRIVS),
369 me.name, source_p->name, "remoteban");
370 return 0;
371 }
372
373 propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER, "%s", parv[1]);
374
375 if(match(parv[3], me.name) == 0)
376 return 0;
377 }
378 else if(rb_dlink_list_length(&cluster_conf_list) > 0)
379 cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", parv[1]);
380
381 remove_resv(source_p, parv[1]);
382 return 0;
383 }
384
385 /* ms_unresv()
386 * parv[0] = sender prefix
387 * parv[1] = target server
388 * parv[2] = resv to remove
389 */
390 static int
391 ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
392 {
393 /* parv[0] parv[1] parv[2]
394 * oper target server resv to remove
395 */
396 propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER, "%s", parv[2]);
397
398 if(!match(parv[1], me.name))
399 return 0;
400
401 if(!IsPerson(source_p))
402 return 0;
403
404 handle_remote_unresv(source_p, parv[2]);
405 return 0;
406 }
407
408 static int
409 me_unresv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
410 {
411 /* name */
412 if(!IsPerson(source_p))
413 return 0;
414
415 handle_remote_unresv(source_p, parv[1]);
416 return 0;
417 }
418
419 static void
420 handle_remote_unresv(struct Client *source_p, const char *name)
421 {
422 if(!find_shared_conf(source_p->username, source_p->host,
423 source_p->servptr->name, SHARED_UNRESV))
424 return;
425
426 remove_resv(source_p, name);
427
428 return;
429 }
430
431 static void
432 remove_resv(struct Client *source_p, const char *name)
433 {
434 struct ConfItem *aconf = NULL;
435
436 if(IsChannelName(name))
437 {
438 if((aconf = hash_find_resv(name)) == NULL)
439 {
440 sendto_one_notice(source_p, ":No RESV for %s", name);
441 return;
442 }
443
444 if(!aconf->hold)
445 bandb_del(BANDB_RESV, aconf->host, NULL);
446 else
447 {
448 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
449 sendto_realops_snomask(SNO_GENERAL, L_ALL,
450 "%s has removed the RESV for: [%s]",
451 get_oper_name(source_p), name);
452 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
453 }
454 del_from_resv_hash(name, aconf);
455 }
456 else
457 {
458 rb_dlink_node *ptr;
459
460 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
461 {
462 aconf = ptr->data;
463
464 if(irccmp(aconf->host, name))
465 aconf = NULL;
466 else
467 break;
468 }
469
470 if(aconf == NULL)
471 {
472 sendto_one_notice(source_p, ":No RESV for %s", name);
473 return;
474 }
475
476 if(!aconf->hold)
477 bandb_del(BANDB_RESV, aconf->host, NULL);
478 else
479 {
480 sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
481 sendto_realops_snomask(SNO_GENERAL, L_ALL,
482 "%s has removed the RESV for: [%s]",
483 get_oper_name(source_p), name);
484 ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
485 }
486 /* already have ptr from the loop above.. */
487 rb_dlinkDestroy(ptr, &resv_conf_list);
488 }
489 free_conf(aconf);
490
491 return;
492 }