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
23 * $Id: m_resv.c 3045 2006-12-26 23:16:57Z jilles $
37 #include "s_newconf.h"
41 static int mo_resv(struct Client
*, struct Client
*, int, const char **);
42 static int ms_resv(struct Client
*, struct Client
*, int, const char **);
43 static int me_resv(struct Client
*, struct Client
*, int, const char **);
44 static int mo_unresv(struct Client
*, struct Client
*, int, const char **);
45 static int ms_unresv(struct Client
*, struct Client
*, int, const char **);
46 static int me_unresv(struct Client
*, struct Client
*, int, const char **);
48 struct Message resv_msgtab
= {
49 "RESV", 0, 0, 0, MFLG_SLOW
| MFLG_UNREG
,
50 {mg_ignore
, mg_not_oper
, {ms_resv
, 4}, {ms_resv
, 4}, {me_resv
, 5}, {mo_resv
, 3}}
52 struct Message unresv_msgtab
= {
53 "UNRESV", 0, 0, 0, MFLG_SLOW
| MFLG_UNREG
,
54 {mg_ignore
, mg_not_oper
, {ms_unresv
, 3}, {ms_unresv
, 3}, {me_unresv
, 2}, {mo_unresv
, 2}}
57 mapi_clist_av1 resv_clist
[] = { &resv_msgtab
, &unresv_msgtab
, NULL
};
58 DECLARE_MODULE_AV1(resv
, NULL
, NULL
, resv_clist
, NULL
, NULL
, "$Revision: 3045 $");
60 static void parse_resv(struct Client
*source_p
, const char *name
,
61 const char *reason
, int temp_time
);
62 static void propagate_resv(struct Client
*source_p
, const char *target
,
63 int temp_time
, const char *name
, const char *reason
);
64 static void cluster_resv(struct Client
*source_p
, int temp_time
,
65 const char *name
, const char *reason
);
67 static void handle_remote_unresv(struct Client
*source_p
, const char *name
);
68 static void remove_resv(struct Client
*source_p
, const char *name
);
69 static int remove_resv_from_file(struct Client
*source_p
, const char *name
);
73 * parv[1] = channel/nick to forbid
77 mo_resv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
81 const char *target_server
= NULL
;
85 if(!IsOperResv(source_p
))
87 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
88 me
.name
, source_p
->name
, "resv");
92 /* RESV [time] <name> [ON <server>] :<reason> */
94 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
96 /* we just set temp_time to -1! */
103 if((parc
>= loc
+2) && (irccmp(parv
[loc
], "ON") == 0))
105 if(!IsOperRemoteBan(source_p
))
107 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
108 me
.name
, source_p
->name
, "remoteban");
112 target_server
= parv
[loc
+1];
116 if(parc
<= loc
|| EmptyString(parv
[loc
]))
118 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
119 me
.name
, source_p
->name
, "RESV");
128 propagate_resv(source_p
, target_server
, temp_time
, name
, reason
);
130 if(match(target_server
, me
.name
) == 0)
133 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
134 cluster_resv(source_p
, temp_time
, name
, reason
);
136 parse_resv(source_p
, name
, reason
, temp_time
);
142 * parv[1] = target server
143 * parv[2] = channel/nick to forbid
147 ms_resv(struct Client
*client_p
, struct Client
*source_p
,
148 int parc
, const char *parv
[])
150 /* source_p parv[1] parv[2] parv[3]
151 * oper target server resv reason
153 propagate_resv(source_p
, parv
[1], 0, parv
[2], parv
[3]);
155 if(!match(parv
[1], me
.name
))
158 if(!IsPerson(source_p
))
161 parse_resv(source_p
, parv
[2], parv
[3], 0);
166 me_resv(struct Client
*client_p
, struct Client
*source_p
,
167 int parc
, const char *parv
[])
169 /* time name 0 :reason */
170 if(!IsPerson(source_p
))
173 parse_resv(source_p
, parv
[2], parv
[4], atoi(parv
[1]));
179 * inputs - source_p if error messages wanted
183 * side effects - will parse the resv and create it if valid
186 parse_resv(struct Client
*source_p
, const char *name
,
187 const char *reason
, int temp_time
)
189 struct ConfItem
*aconf
;
191 if(!MyClient(source_p
) &&
192 !find_shared_conf(source_p
->username
, source_p
->host
,
193 source_p
->servptr
->name
,
194 (temp_time
> 0) ? SHARED_TRESV
: SHARED_PRESV
))
197 if(IsChannelName(name
))
199 if(hash_find_resv(name
))
201 sendto_one_notice(source_p
,
202 ":A RESV has already been placed on channel: %s",
207 if(strlen(name
) > CHANNELLEN
)
209 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
214 if(strchr(name
, ','))
216 sendto_one_notice(source_p
,
217 ":Invalid character ',' in channel RESV");
221 if(strchr(reason
, '"'))
223 sendto_one_notice(source_p
,
224 ":Invalid character '\"' in comment");
229 aconf
->status
= CONF_RESV_CHANNEL
;
231 aconf
->name
= rb_strdup(name
);
232 aconf
->passwd
= rb_strdup(reason
);
233 add_to_resv_hash(aconf
->name
, aconf
);
237 aconf
->hold
= rb_current_time() + temp_time
;
239 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
240 "%s added temporary %d min. RESV for [%s] [%s]",
241 get_oper_name(source_p
), temp_time
/ 60,
243 ilog(L_KLINE
, "R %s %d %s %s",
244 get_oper_name(source_p
), temp_time
/ 60,
246 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
247 temp_time
/ 60, name
);
250 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
251 aconf
->passwd
, NULL
, NULL
, 0);
253 else if(clean_resv_nick(name
))
255 if(strlen(name
) > NICKLEN
*2)
257 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
262 if(strchr(reason
, '"'))
264 sendto_one_notice(source_p
,
265 ":Invalid character '\"' in comment");
269 if(!valid_wild_card_simple(name
))
271 sendto_one_notice(source_p
,
272 ":Please include at least %d non-wildcard "
273 "characters with the resv",
274 ConfigFileEntry
.min_nonwildcard_simple
);
278 if(find_nick_resv_mask(name
))
280 sendto_one_notice(source_p
,
281 ":A RESV has already been placed on nick: %s",
287 aconf
->status
= CONF_RESV_NICK
;
289 aconf
->name
= rb_strdup(name
);
290 aconf
->passwd
= rb_strdup(reason
);
291 rb_dlinkAddAlloc(aconf
, &resv_conf_list
);
295 aconf
->hold
= rb_current_time() + temp_time
;
297 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
298 "%s added temporary %d min. RESV for [%s] [%s]",
299 get_oper_name(source_p
), temp_time
/ 60,
301 ilog(L_KLINE
, "R %s %d %s %s",
302 get_oper_name(source_p
), temp_time
/ 60,
304 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
305 temp_time
/ 60, name
);
308 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
309 aconf
->passwd
, NULL
, NULL
, 0);
312 sendto_one_notice(source_p
,
313 ":You have specified an invalid resv: [%s]",
318 propagate_resv(struct Client
*source_p
, const char *target
,
319 int temp_time
, const char *name
, const char *reason
)
323 sendto_match_servs(source_p
, target
,
326 target
, name
, reason
);
327 sendto_match_servs(source_p
, target
,
328 CAP_ENCAP
, CAP_CLUSTER
,
329 "ENCAP %s RESV %d %s 0 :%s",
330 target
, temp_time
, name
, reason
);
333 sendto_match_servs(source_p
, target
,
335 "ENCAP %s RESV %d %s 0 :%s",
336 target
, temp_time
, name
, reason
);
340 cluster_resv(struct Client
*source_p
, int temp_time
, const char *name
,
343 struct remote_conf
*shared_p
;
346 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
348 shared_p
= ptr
->data
;
350 /* old protocol cant handle temps, and we dont really want
351 * to convert them to perm.. --fl
355 if(!(shared_p
->flags
& SHARED_PRESV
))
358 sendto_match_servs(source_p
, shared_p
->server
,
361 shared_p
->server
, name
, reason
);
362 sendto_match_servs(source_p
, shared_p
->server
,
363 CAP_ENCAP
, CAP_CLUSTER
,
364 "ENCAP %s RESV 0 %s 0 :%s",
365 shared_p
->server
, name
, reason
);
367 else if(shared_p
->flags
& SHARED_TRESV
)
368 sendto_match_servs(source_p
, shared_p
->server
,
370 "ENCAP %s RESV %d %s 0 :%s",
371 shared_p
->server
, temp_time
, name
, reason
);
378 * parv[1] = channel/nick to unforbid
381 mo_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
383 if(!IsOperResv(source_p
))
385 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
386 me
.name
, source_p
->name
, "resv");
390 if((parc
== 4) && (irccmp(parv
[2], "ON") == 0))
392 if(!IsOperRemoteBan(source_p
))
394 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
395 me
.name
, source_p
->name
, "remoteban");
399 propagate_generic(source_p
, "UNRESV", parv
[3], CAP_CLUSTER
,
402 if(match(parv
[3], me
.name
) == 0)
405 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
406 cluster_generic(source_p
, "UNRESV", SHARED_UNRESV
, CAP_CLUSTER
,
409 remove_resv(source_p
, parv
[1]);
414 * parv[1] = target server
415 * parv[2] = resv to remove
418 ms_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
420 /* source_p parv[1] parv[2]
421 * oper target server resv to remove
423 propagate_generic(source_p
, "UNRESV", parv
[1], CAP_CLUSTER
,
426 if(!match(parv
[1], me
.name
))
429 if(!IsPerson(source_p
))
432 handle_remote_unresv(source_p
, parv
[2]);
437 me_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
440 if(!IsPerson(source_p
))
443 handle_remote_unresv(source_p
, parv
[1]);
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
))
454 remove_resv(source_p
, name
);
460 remove_resv(struct Client
*source_p
, const char *name
)
462 struct ConfItem
*aconf
= NULL
;
464 if(IsChannelName(name
))
466 if((aconf
= hash_find_resv(name
)) == NULL
)
468 sendto_one_notice(source_p
, ":No RESV for %s", name
);
474 if (!remove_resv_from_file(source_p
, name
))
479 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
480 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
481 "%s has removed the RESV for: [%s]",
482 get_oper_name(source_p
), name
);
483 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
485 del_from_resv_hash(name
, aconf
);
491 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
495 if(irccmp(aconf
->name
, name
))
503 sendto_one_notice(source_p
, ":No RESV for %s", name
);
509 if (!remove_resv_from_file(source_p
, name
))
514 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
515 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
516 "%s has removed the RESV for: [%s]",
517 get_oper_name(source_p
), name
);
518 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
520 /* already have ptr from the loop above.. */
521 rb_dlinkDestroy(ptr
, &resv_conf_list
);
528 /* remove_resv_from_file()
530 * inputs - client removing the resv
533 * side effects - resv if found, is removed from conf
534 * - does not touch resv hash or resv_conf_list
537 remove_resv_from_file(struct Client
*source_p
, const char *name
)
542 char temppath
[BUFSIZE
];
543 const char *filename
;
546 int error_on_write
= 0;
549 rb_sprintf(temppath
, "%s.tmp", ConfigFileEntry
.resvfile
);
550 filename
= get_conf_name(RESV_TYPE
);
552 if((in
= fopen(filename
, "r")) == NULL
)
554 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
560 if((out
= fopen(temppath
, "w")) == NULL
)
562 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
570 while (fgets(buf
, sizeof(buf
), in
))
572 const char *resv_name
;
577 (void) unlink(temppath
);
582 rb_strlcpy(buff
, buf
, sizeof(buff
));
584 if((p
= strchr(buff
, '\n')) != NULL
)
587 if((*buff
== '\0') || (*buff
== '#'))
589 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
593 if((resv_name
= getfield(buff
)) == NULL
)
595 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
599 if(irccmp(resv_name
, name
) == 0)
605 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
611 error_on_write
= YES
;
615 sendto_one_notice(source_p
, ":Couldn't write temp resv file, aborted");
620 sendto_one_notice(source_p
, ":Cannot find RESV for %s in file", name
);
623 (void) unlink(temppath
);
628 if (rename(temppath
, filename
))
630 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
634 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
635 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
636 "%s has removed the RESV for: [%s]", get_oper_name(source_p
), name
);
637 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);