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"
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: 3045 $");
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_resv_from_file(struct Client
*source_p
, const char *name
);
74 * parv[0] = sender prefix
75 * parv[1] = channel/nick to forbid
79 mo_resv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
83 const char *target_server
= NULL
;
87 if(!IsOperResv(source_p
))
89 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
90 me
.name
, source_p
->name
, "resv");
94 /* RESV [time] <name> [ON <server>] :<reason> */
96 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
98 /* we just set temp_time to -1! */
105 if((parc
>= loc
+2) && (irccmp(parv
[loc
], "ON") == 0))
107 if(!IsOperRemoteBan(source_p
))
109 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
110 me
.name
, source_p
->name
, "remoteban");
114 target_server
= parv
[loc
+1];
118 if(parc
<= loc
|| EmptyString(parv
[loc
]))
120 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
121 me
.name
, source_p
->name
, "RESV");
130 propagate_resv(source_p
, target_server
, temp_time
, name
, reason
);
132 if(match(target_server
, me
.name
) == 0)
135 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
136 cluster_resv(source_p
, temp_time
, name
, reason
);
138 parse_resv(source_p
, name
, reason
, temp_time
);
144 * parv[0] = sender prefix
145 * parv[1] = target server
146 * parv[2] = channel/nick to forbid
150 ms_resv(struct Client
*client_p
, struct Client
*source_p
,
151 int parc
, const char *parv
[])
153 /* parv[0] parv[1] parv[2] parv[3]
154 * oper target server resv reason
156 propagate_resv(source_p
, parv
[1], 0, parv
[2], parv
[3]);
158 if(!match(parv
[1], me
.name
))
161 if(!IsPerson(source_p
))
164 parse_resv(source_p
, parv
[2], parv
[3], 0);
169 me_resv(struct Client
*client_p
, struct Client
*source_p
,
170 int parc
, const char *parv
[])
172 /* time name 0 :reason */
173 if(!IsPerson(source_p
))
176 parse_resv(source_p
, parv
[2], parv
[4], atoi(parv
[1]));
182 * inputs - source_p if error messages wanted
186 * side effects - will parse the resv and create it if valid
189 parse_resv(struct Client
*source_p
, const char *name
,
190 const char *reason
, int temp_time
)
192 struct ConfItem
*aconf
;
194 if(!MyClient(source_p
) &&
195 !find_shared_conf(source_p
->username
, source_p
->host
,
196 source_p
->servptr
->name
,
197 (temp_time
> 0) ? SHARED_TRESV
: SHARED_PRESV
))
200 if(IsChannelName(name
))
202 if(hash_find_resv(name
))
204 sendto_one_notice(source_p
,
205 ":A RESV has already been placed on channel: %s",
210 if(strlen(name
) > CHANNELLEN
)
212 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
217 if(strchr(reason
, '"'))
219 sendto_one_notice(source_p
,
220 ":Invalid character '\"' in comment");
225 aconf
->status
= CONF_RESV_CHANNEL
;
227 aconf
->name
= rb_strdup(name
);
228 aconf
->passwd
= rb_strdup(reason
);
229 add_to_resv_hash(aconf
->name
, aconf
);
233 aconf
->hold
= rb_current_time() + temp_time
;
235 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
236 "%s added temporary %d min. RESV for [%s] [%s]",
237 get_oper_name(source_p
), temp_time
/ 60,
239 ilog(L_KLINE
, "R %s %d %s %s",
240 get_oper_name(source_p
), temp_time
/ 60,
242 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
243 temp_time
/ 60, name
);
246 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
247 aconf
->passwd
, NULL
, NULL
, 0);
249 else if(clean_resv_nick(name
))
251 if(strlen(name
) > NICKLEN
*2)
253 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
258 if(strchr(reason
, '"'))
260 sendto_one_notice(source_p
,
261 ":Invalid character '\"' in comment");
265 if(!valid_wild_card_simple(name
))
267 sendto_one_notice(source_p
,
268 ":Please include at least %d non-wildcard "
269 "characters with the resv",
270 ConfigFileEntry
.min_nonwildcard_simple
);
274 if(find_nick_resv_mask(name
))
276 sendto_one_notice(source_p
,
277 ":A RESV has already been placed on nick: %s",
283 aconf
->status
= CONF_RESV_NICK
;
285 aconf
->name
= rb_strdup(name
);
286 aconf
->passwd
= rb_strdup(reason
);
287 rb_dlinkAddAlloc(aconf
, &resv_conf_list
);
291 aconf
->hold
= rb_current_time() + temp_time
;
293 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
294 "%s added temporary %d min. RESV for [%s] [%s]",
295 get_oper_name(source_p
), temp_time
/ 60,
297 ilog(L_KLINE
, "R %s %d %s %s",
298 get_oper_name(source_p
), temp_time
/ 60,
300 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
301 temp_time
/ 60, name
);
304 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
305 aconf
->passwd
, NULL
, NULL
, 0);
308 sendto_one_notice(source_p
,
309 ":You have specified an invalid resv: [%s]",
314 propagate_resv(struct Client
*source_p
, const char *target
,
315 int temp_time
, const char *name
, const char *reason
)
319 sendto_match_servs(source_p
, target
,
322 target
, name
, reason
);
323 sendto_match_servs(source_p
, target
,
324 CAP_ENCAP
, CAP_CLUSTER
,
325 "ENCAP %s RESV %d %s 0 :%s",
326 target
, temp_time
, name
, reason
);
329 sendto_match_servs(source_p
, target
,
331 "ENCAP %s RESV %d %s 0 :%s",
332 target
, temp_time
, name
, reason
);
336 cluster_resv(struct Client
*source_p
, int temp_time
, const char *name
,
339 struct remote_conf
*shared_p
;
342 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
344 shared_p
= ptr
->data
;
346 /* old protocol cant handle temps, and we dont really want
347 * to convert them to perm.. --fl
351 if(!(shared_p
->flags
& SHARED_PRESV
))
354 sendto_match_servs(source_p
, shared_p
->server
,
357 shared_p
->server
, name
, reason
);
358 sendto_match_servs(source_p
, shared_p
->server
,
359 CAP_ENCAP
, CAP_CLUSTER
,
360 "ENCAP %s RESV 0 %s 0 :%s",
361 shared_p
->server
, name
, reason
);
363 else if(shared_p
->flags
& SHARED_TRESV
)
364 sendto_match_servs(source_p
, shared_p
->server
,
366 "ENCAP %s RESV %d %s 0 :%s",
367 shared_p
->server
, temp_time
, name
, reason
);
374 * parv[0] = sender prefix
375 * parv[1] = channel/nick to unforbid
378 mo_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
380 if(!IsOperResv(source_p
))
382 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
383 me
.name
, source_p
->name
, "resv");
387 if((parc
== 4) && (irccmp(parv
[2], "ON") == 0))
389 if(!IsOperRemoteBan(source_p
))
391 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
392 me
.name
, source_p
->name
, "remoteban");
396 propagate_generic(source_p
, "UNRESV", parv
[3], CAP_CLUSTER
,
399 if(match(parv
[3], me
.name
) == 0)
402 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
403 cluster_generic(source_p
, "UNRESV", SHARED_UNRESV
, CAP_CLUSTER
,
406 remove_resv(source_p
, parv
[1]);
411 * parv[0] = sender prefix
412 * parv[1] = target server
413 * parv[2] = resv to remove
416 ms_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
418 /* parv[0] parv[1] parv[2]
419 * oper target server resv to remove
421 propagate_generic(source_p
, "UNRESV", parv
[1], CAP_CLUSTER
,
424 if(!match(parv
[1], me
.name
))
427 if(!IsPerson(source_p
))
430 handle_remote_unresv(source_p
, parv
[2]);
435 me_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
438 if(!IsPerson(source_p
))
441 handle_remote_unresv(source_p
, parv
[1]);
446 handle_remote_unresv(struct Client
*source_p
, const char *name
)
448 if(!find_shared_conf(source_p
->username
, source_p
->host
,
449 source_p
->servptr
->name
, SHARED_UNRESV
))
452 remove_resv(source_p
, name
);
458 remove_resv(struct Client
*source_p
, const char *name
)
460 struct ConfItem
*aconf
= NULL
;
462 if(IsChannelName(name
))
464 if((aconf
= hash_find_resv(name
)) == NULL
)
466 sendto_one_notice(source_p
, ":No RESV for %s", name
);
472 if (!remove_resv_from_file(source_p
, name
))
477 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
478 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
479 "%s has removed the RESV for: [%s]",
480 get_oper_name(source_p
), name
);
481 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
483 del_from_resv_hash(name
, aconf
);
489 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
493 if(irccmp(aconf
->name
, name
))
501 sendto_one_notice(source_p
, ":No RESV for %s", name
);
507 if (!remove_resv_from_file(source_p
, name
))
512 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
513 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
514 "%s has removed the RESV for: [%s]",
515 get_oper_name(source_p
), name
);
516 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
518 /* already have ptr from the loop above.. */
519 rb_dlinkDestroy(ptr
, &resv_conf_list
);
526 /* remove_resv_from_file()
528 * inputs - client removing the resv
531 * side effects - resv if found, is removed from conf
532 * - does not touch resv hash or resv_conf_list
535 remove_resv_from_file(struct Client
*source_p
, const char *name
)
540 char temppath
[BUFSIZE
];
541 const char *filename
;
544 int error_on_write
= 0;
547 rb_sprintf(temppath
, "%s.tmp", ConfigFileEntry
.resvfile
);
548 filename
= get_conf_name(RESV_TYPE
);
550 if((in
= fopen(filename
, "r")) == NULL
)
552 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
558 if((out
= fopen(temppath
, "w")) == NULL
)
560 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
568 while (fgets(buf
, sizeof(buf
), in
))
570 const char *resv_name
;
575 (void) unlink(temppath
);
580 strlcpy(buff
, buf
, sizeof(buff
));
582 if((p
= strchr(buff
, '\n')) != NULL
)
585 if((*buff
== '\0') || (*buff
== '#'))
587 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
591 if((resv_name
= getfield(buff
)) == NULL
)
593 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
597 if(irccmp(resv_name
, name
) == 0)
603 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
609 error_on_write
= YES
;
613 sendto_one_notice(source_p
, ":Couldn't write temp resv file, aborted");
618 sendto_one_notice(source_p
, ":Cannot find RESV for %s in file", name
);
621 (void) unlink(temppath
);
626 if (rename(temppath
, filename
))
628 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
632 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
633 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
634 "%s has removed the RESV for: [%s]", get_oper_name(source_p
), name
);
635 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);