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_temp_resv(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 /* RESV [time] <name> [ON <server>] :<reason> */
89 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
91 /* we just set temp_time to -1! */
98 if((parc
>= loc
+2) && (irccmp(parv
[loc
], "ON") == 0))
100 if(!IsOperRemoteBan(source_p
))
102 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
103 me
.name
, source_p
->name
, "remoteban");
107 target_server
= parv
[loc
+1];
111 if(parc
<= loc
|| EmptyString(parv
[loc
]))
113 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
114 me
.name
, source_p
->name
, "RESV");
123 propagate_resv(source_p
, target_server
, temp_time
, name
, reason
);
125 if(match(target_server
, me
.name
) == 0)
128 else if(dlink_list_length(&cluster_conf_list
) > 0)
129 cluster_resv(source_p
, temp_time
, name
, reason
);
131 parse_resv(source_p
, name
, reason
, temp_time
);
137 * parv[0] = sender prefix
138 * parv[1] = target server
139 * parv[2] = channel/nick to forbid
143 ms_resv(struct Client
*client_p
, struct Client
*source_p
,
144 int parc
, const char *parv
[])
146 /* parv[0] parv[1] parv[2] parv[3]
147 * oper target server resv reason
149 propagate_resv(source_p
, parv
[1], 0, parv
[2], parv
[3]);
151 if(!match(parv
[1], me
.name
))
154 if(!IsPerson(source_p
))
157 parse_resv(source_p
, parv
[2], parv
[3], 0);
162 me_resv(struct Client
*client_p
, struct Client
*source_p
,
163 int parc
, const char *parv
[])
165 /* time name 0 :reason */
166 if(!IsPerson(source_p
))
169 parse_resv(source_p
, parv
[2], parv
[4], atoi(parv
[1]));
175 * inputs - source_p if error messages wanted
179 * side effects - will parse the resv and create it if valid
182 parse_resv(struct Client
*source_p
, const char *name
,
183 const char *reason
, int temp_time
)
185 struct ConfItem
*aconf
;
187 if(!MyClient(source_p
) &&
188 !find_shared_conf(source_p
->username
, source_p
->host
,
189 source_p
->user
->server
,
190 (temp_time
> 0) ? SHARED_TRESV
: SHARED_PRESV
))
193 if(IsChannelName(name
))
195 if(hash_find_resv(name
))
197 sendto_one_notice(source_p
,
198 ":A RESV has already been placed on channel: %s",
203 if(strlen(name
) > CHANNELLEN
)
205 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
210 if(strchr(reason
, '"'))
212 sendto_one_notice(source_p
,
213 ":Invalid character '\"' in comment");
218 aconf
->status
= CONF_RESV_CHANNEL
;
220 DupString(aconf
->name
, name
);
221 DupString(aconf
->passwd
, reason
);
222 add_to_resv_hash(aconf
->name
, aconf
);
226 aconf
->hold
= CurrentTime
+ temp_time
;
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,
232 ilog(L_KLINE
, "R %s %d %s %s",
233 get_oper_name(source_p
), temp_time
/ 60,
235 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
236 temp_time
/ 60, name
);
239 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
240 aconf
->passwd
, NULL
, NULL
, 0);
242 else if(clean_resv_nick(name
))
244 if(strlen(name
) > NICKLEN
*2)
246 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
251 if(strchr(reason
, '"'))
253 sendto_one_notice(source_p
,
254 ":Invalid character '\"' in comment");
258 if(!valid_wild_card_simple(name
))
260 sendto_one_notice(source_p
,
261 ":Please include at least %d non-wildcard "
262 "characters with the resv",
263 ConfigFileEntry
.min_nonwildcard_simple
);
267 if(find_nick_resv(name
))
269 sendto_one_notice(source_p
,
270 ":A RESV has already been placed on nick: %s",
276 aconf
->status
= CONF_RESV_NICK
;
278 DupString(aconf
->name
, name
);
279 DupString(aconf
->passwd
, reason
);
280 dlinkAddAlloc(aconf
, &resv_conf_list
);
284 aconf
->hold
= CurrentTime
+ temp_time
;
286 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
287 "%s added temporary %d min. RESV for [%s] [%s]",
288 get_oper_name(source_p
), temp_time
/ 60,
290 ilog(L_KLINE
, "R %s %d %s %s",
291 get_oper_name(source_p
), temp_time
/ 60,
293 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
294 temp_time
/ 60, name
);
297 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
298 aconf
->passwd
, NULL
, NULL
, 0);
301 sendto_one_notice(source_p
,
302 ":You have specified an invalid resv: [%s]",
307 propagate_resv(struct Client
*source_p
, const char *target
,
308 int temp_time
, const char *name
, const char *reason
)
312 sendto_match_servs(source_p
, target
,
315 target
, name
, reason
);
316 sendto_match_servs(source_p
, target
,
317 CAP_ENCAP
, CAP_CLUSTER
,
318 "ENCAP %s RESV %d %s 0 :%s",
319 target
, temp_time
, name
, reason
);
322 sendto_match_servs(source_p
, target
,
324 "ENCAP %s RESV %d %s 0 :%s",
325 target
, temp_time
, name
, reason
);
329 cluster_resv(struct Client
*source_p
, int temp_time
, const char *name
,
332 struct remote_conf
*shared_p
;
335 DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
337 shared_p
= ptr
->data
;
339 /* old protocol cant handle temps, and we dont really want
340 * to convert them to perm.. --fl
344 if(!(shared_p
->flags
& SHARED_PRESV
))
347 sendto_match_servs(source_p
, shared_p
->server
,
350 shared_p
->server
, name
, reason
);
351 sendto_match_servs(source_p
, shared_p
->server
,
352 CAP_ENCAP
, CAP_CLUSTER
,
353 "ENCAP %s RESV 0 %s 0 :%s",
354 shared_p
->server
, name
, reason
);
356 else if(shared_p
->flags
& SHARED_TRESV
)
357 sendto_match_servs(source_p
, shared_p
->server
,
359 "ENCAP %s RESV %d %s 0 :%s",
360 shared_p
->server
, temp_time
, name
, reason
);
367 * parv[0] = sender prefix
368 * parv[1] = channel/nick to unforbid
371 mo_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
373 if((parc
== 4) && (irccmp(parv
[2], "ON") == 0))
375 if(!IsOperRemoteBan(source_p
))
377 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
378 me
.name
, source_p
->name
, "remoteban");
382 propagate_generic(source_p
, "UNRESV", parv
[3], CAP_CLUSTER
,
385 if(match(parv
[3], me
.name
) == 0)
388 else if(dlink_list_length(&cluster_conf_list
) > 0)
389 cluster_generic(source_p
, "UNRESV", SHARED_UNRESV
, CAP_CLUSTER
,
392 if(remove_temp_resv(source_p
, parv
[1]))
395 remove_resv(source_p
, parv
[1]);
400 * parv[0] = sender prefix
401 * parv[1] = target server
402 * parv[2] = resv to remove
405 ms_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
407 /* parv[0] parv[1] parv[2]
408 * oper target server resv to remove
410 propagate_generic(source_p
, "UNRESV", parv
[1], CAP_CLUSTER
,
413 if(!match(parv
[1], me
.name
))
416 if(!IsPerson(source_p
))
419 handle_remote_unresv(source_p
, parv
[2]);
424 me_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
427 if(!IsPerson(source_p
))
430 handle_remote_unresv(source_p
, parv
[1]);
435 handle_remote_unresv(struct Client
*source_p
, const char *name
)
437 if(!find_shared_conf(source_p
->username
, source_p
->host
,
438 source_p
->user
->server
, SHARED_UNRESV
))
441 if(remove_temp_resv(source_p
, name
))
444 remove_resv(source_p
, name
);
450 remove_temp_resv(struct Client
*source_p
, const char *name
)
452 struct ConfItem
*aconf
= NULL
;
454 if(IsChannelName(name
))
456 if((aconf
= hash_find_resv(name
)) == NULL
)
459 /* its permanent, let remove_resv do it properly */
463 del_from_resv_hash(name
, aconf
);
470 DLINK_FOREACH(ptr
, resv_conf_list
.head
)
474 if(irccmp(aconf
->name
, name
))
483 /* permanent, remove_resv() needs to do it properly */
487 /* already have ptr from the loop above.. */
488 dlinkDestroy(ptr
, &resv_conf_list
);
492 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
493 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
494 "%s has removed the RESV for: [%s]",
495 get_oper_name(source_p
), name
);
496 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
503 * inputs - client removing the resv
506 * side effects - resv if found, is removed
509 remove_resv(struct Client
*source_p
, const char *name
)
514 char temppath
[BUFSIZE
];
515 const char *filename
;
518 int error_on_write
= 0;
521 ircsprintf(temppath
, "%s.tmp", ConfigFileEntry
.resvfile
);
522 filename
= get_conf_name(RESV_TYPE
);
524 if((in
= fopen(filename
, "r")) == NULL
)
526 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
532 if((out
= fopen(temppath
, "w")) == NULL
)
534 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
542 while (fgets(buf
, sizeof(buf
), in
))
544 const char *resv_name
;
549 (void) unlink(temppath
);
554 strlcpy(buff
, buf
, sizeof(buff
));
556 if((p
= strchr(buff
, '\n')) != NULL
)
559 if((*buff
== '\0') || (*buff
== '#'))
561 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
565 if((resv_name
= getfield(buff
)) == NULL
)
567 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
571 if(irccmp(resv_name
, name
) == 0)
577 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
583 error_on_write
= YES
;
587 sendto_one_notice(source_p
, ":Couldn't write temp resv file, aborted");
592 sendto_one_notice(source_p
, ":No RESV for %s", name
);
595 (void) unlink(temppath
);
600 if (rename(temppath
, filename
))
602 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
607 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
608 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
609 "%s has removed the RESV for: [%s]", get_oper_name(source_p
), name
);
610 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);