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(reason
, '"'))
216 sendto_one_notice(source_p
,
217 ":Invalid character '\"' in comment");
222 aconf
->status
= CONF_RESV_CHANNEL
;
224 aconf
->name
= rb_strdup(name
);
225 aconf
->passwd
= rb_strdup(reason
);
226 add_to_resv_hash(aconf
->name
, aconf
);
230 aconf
->hold
= rb_current_time() + temp_time
;
232 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
233 "%s added temporary %d min. RESV for [%s] [%s]",
234 get_oper_name(source_p
), temp_time
/ 60,
236 ilog(L_KLINE
, "R %s %d %s %s",
237 get_oper_name(source_p
), temp_time
/ 60,
239 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
240 temp_time
/ 60, name
);
243 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
244 aconf
->passwd
, NULL
, NULL
, 0);
246 else if(clean_resv_nick(name
))
248 if(strlen(name
) > NICKLEN
*2)
250 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
255 if(strchr(reason
, '"'))
257 sendto_one_notice(source_p
,
258 ":Invalid character '\"' in comment");
262 if(!valid_wild_card_simple(name
))
264 sendto_one_notice(source_p
,
265 ":Please include at least %d non-wildcard "
266 "characters with the resv",
267 ConfigFileEntry
.min_nonwildcard_simple
);
271 if(find_nick_resv_mask(name
))
273 sendto_one_notice(source_p
,
274 ":A RESV has already been placed on nick: %s",
280 aconf
->status
= CONF_RESV_NICK
;
282 aconf
->name
= rb_strdup(name
);
283 aconf
->passwd
= rb_strdup(reason
);
284 rb_dlinkAddAlloc(aconf
, &resv_conf_list
);
288 aconf
->hold
= rb_current_time() + temp_time
;
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,
294 ilog(L_KLINE
, "R %s %d %s %s",
295 get_oper_name(source_p
), temp_time
/ 60,
297 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
298 temp_time
/ 60, name
);
301 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
302 aconf
->passwd
, NULL
, NULL
, 0);
305 sendto_one_notice(source_p
,
306 ":You have specified an invalid resv: [%s]",
311 propagate_resv(struct Client
*source_p
, const char *target
,
312 int temp_time
, const char *name
, const char *reason
)
316 sendto_match_servs(source_p
, target
,
319 target
, name
, reason
);
320 sendto_match_servs(source_p
, target
,
321 CAP_ENCAP
, CAP_CLUSTER
,
322 "ENCAP %s RESV %d %s 0 :%s",
323 target
, temp_time
, name
, reason
);
326 sendto_match_servs(source_p
, target
,
328 "ENCAP %s RESV %d %s 0 :%s",
329 target
, temp_time
, name
, reason
);
333 cluster_resv(struct Client
*source_p
, int temp_time
, const char *name
,
336 struct remote_conf
*shared_p
;
339 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
341 shared_p
= ptr
->data
;
343 /* old protocol cant handle temps, and we dont really want
344 * to convert them to perm.. --fl
348 if(!(shared_p
->flags
& SHARED_PRESV
))
351 sendto_match_servs(source_p
, shared_p
->server
,
354 shared_p
->server
, name
, reason
);
355 sendto_match_servs(source_p
, shared_p
->server
,
356 CAP_ENCAP
, CAP_CLUSTER
,
357 "ENCAP %s RESV 0 %s 0 :%s",
358 shared_p
->server
, name
, reason
);
360 else if(shared_p
->flags
& SHARED_TRESV
)
361 sendto_match_servs(source_p
, shared_p
->server
,
363 "ENCAP %s RESV %d %s 0 :%s",
364 shared_p
->server
, temp_time
, name
, reason
);
371 * parv[1] = channel/nick to unforbid
374 mo_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
376 if(!IsOperResv(source_p
))
378 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
379 me
.name
, source_p
->name
, "resv");
383 if((parc
== 4) && (irccmp(parv
[2], "ON") == 0))
385 if(!IsOperRemoteBan(source_p
))
387 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
388 me
.name
, source_p
->name
, "remoteban");
392 propagate_generic(source_p
, "UNRESV", parv
[3], CAP_CLUSTER
,
395 if(match(parv
[3], me
.name
) == 0)
398 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
399 cluster_generic(source_p
, "UNRESV", SHARED_UNRESV
, CAP_CLUSTER
,
402 remove_resv(source_p
, parv
[1]);
407 * parv[1] = target server
408 * parv[2] = resv to remove
411 ms_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
413 /* source_p parv[1] parv[2]
414 * oper target server resv to remove
416 propagate_generic(source_p
, "UNRESV", parv
[1], CAP_CLUSTER
,
419 if(!match(parv
[1], me
.name
))
422 if(!IsPerson(source_p
))
425 handle_remote_unresv(source_p
, parv
[2]);
430 me_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
433 if(!IsPerson(source_p
))
436 handle_remote_unresv(source_p
, parv
[1]);
441 handle_remote_unresv(struct Client
*source_p
, const char *name
)
443 if(!find_shared_conf(source_p
->username
, source_p
->host
,
444 source_p
->servptr
->name
, SHARED_UNRESV
))
447 remove_resv(source_p
, name
);
453 remove_resv(struct Client
*source_p
, const char *name
)
455 struct ConfItem
*aconf
= NULL
;
457 if(IsChannelName(name
))
459 if((aconf
= hash_find_resv(name
)) == NULL
)
461 sendto_one_notice(source_p
, ":No RESV for %s", name
);
467 if (!remove_resv_from_file(source_p
, name
))
472 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
473 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
474 "%s has removed the RESV for: [%s]",
475 get_oper_name(source_p
), name
);
476 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
478 del_from_resv_hash(name
, aconf
);
484 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
488 if(irccmp(aconf
->name
, name
))
496 sendto_one_notice(source_p
, ":No RESV for %s", name
);
502 if (!remove_resv_from_file(source_p
, name
))
507 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
508 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
509 "%s has removed the RESV for: [%s]",
510 get_oper_name(source_p
), name
);
511 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
513 /* already have ptr from the loop above.. */
514 rb_dlinkDestroy(ptr
, &resv_conf_list
);
521 /* remove_resv_from_file()
523 * inputs - client removing the resv
526 * side effects - resv if found, is removed from conf
527 * - does not touch resv hash or resv_conf_list
530 remove_resv_from_file(struct Client
*source_p
, const char *name
)
535 char temppath
[BUFSIZE
];
536 const char *filename
;
539 int error_on_write
= 0;
542 rb_sprintf(temppath
, "%s.tmp", ConfigFileEntry
.resvfile
);
543 filename
= get_conf_name(RESV_TYPE
);
545 if((in
= fopen(filename
, "r")) == NULL
)
547 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
553 if((out
= fopen(temppath
, "w")) == NULL
)
555 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
563 while (fgets(buf
, sizeof(buf
), in
))
565 const char *resv_name
;
570 (void) unlink(temppath
);
575 rb_strlcpy(buff
, buf
, sizeof(buff
));
577 if((p
= strchr(buff
, '\n')) != NULL
)
580 if((*buff
== '\0') || (*buff
== '#'))
582 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
586 if((resv_name
= getfield(buff
)) == NULL
)
588 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
592 if(irccmp(resv_name
, name
) == 0)
598 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
604 error_on_write
= YES
;
608 sendto_one_notice(source_p
, ":Couldn't write temp resv file, aborted");
613 sendto_one_notice(source_p
, ":Cannot find RESV for %s in file", name
);
616 (void) unlink(temppath
);
621 if (rename(temppath
, filename
))
623 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
627 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
628 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
629 "%s has removed the RESV for: [%s]", get_oper_name(source_p
), name
);
630 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);