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[0] = sender prefix
74 * parv[1] = channel/nick to forbid
78 mo_resv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
82 const char *target_server
= NULL
;
86 if(!IsOperResv(source_p
))
88 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
89 me
.name
, source_p
->name
, "resv");
93 /* RESV [time] <name> [ON <server>] :<reason> */
95 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
97 /* we just set temp_time to -1! */
104 if((parc
>= loc
+2) && (irccmp(parv
[loc
], "ON") == 0))
106 if(!IsOperRemoteBan(source_p
))
108 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
109 me
.name
, source_p
->name
, "remoteban");
113 target_server
= parv
[loc
+1];
117 if(parc
<= loc
|| EmptyString(parv
[loc
]))
119 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
120 me
.name
, source_p
->name
, "RESV");
129 propagate_resv(source_p
, target_server
, temp_time
, name
, reason
);
131 if(match(target_server
, me
.name
) == 0)
134 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
135 cluster_resv(source_p
, temp_time
, name
, reason
);
137 parse_resv(source_p
, name
, reason
, temp_time
);
143 * parv[0] = sender prefix
144 * parv[1] = target server
145 * parv[2] = channel/nick to forbid
149 ms_resv(struct Client
*client_p
, struct Client
*source_p
,
150 int parc
, const char *parv
[])
152 /* parv[0] parv[1] parv[2] parv[3]
153 * oper target server resv reason
155 propagate_resv(source_p
, parv
[1], 0, parv
[2], parv
[3]);
157 if(!match(parv
[1], me
.name
))
160 if(!IsPerson(source_p
))
163 parse_resv(source_p
, parv
[2], parv
[3], 0);
168 me_resv(struct Client
*client_p
, struct Client
*source_p
,
169 int parc
, const char *parv
[])
171 /* time name 0 :reason */
172 if(!IsPerson(source_p
))
175 parse_resv(source_p
, parv
[2], parv
[4], atoi(parv
[1]));
181 * inputs - source_p if error messages wanted
185 * side effects - will parse the resv and create it if valid
188 parse_resv(struct Client
*source_p
, const char *name
,
189 const char *reason
, int temp_time
)
191 struct ConfItem
*aconf
;
193 if(!MyClient(source_p
) &&
194 !find_shared_conf(source_p
->username
, source_p
->host
,
195 source_p
->servptr
->name
,
196 (temp_time
> 0) ? SHARED_TRESV
: SHARED_PRESV
))
199 if(IsChannelName(name
))
201 if(hash_find_resv(name
))
203 sendto_one_notice(source_p
,
204 ":A RESV has already been placed on channel: %s",
209 if(strlen(name
) > CHANNELLEN
)
211 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
216 if(strchr(reason
, '"'))
218 sendto_one_notice(source_p
,
219 ":Invalid character '\"' in comment");
224 aconf
->status
= CONF_RESV_CHANNEL
;
226 aconf
->name
= rb_strdup(name
);
227 aconf
->passwd
= rb_strdup(reason
);
228 add_to_resv_hash(aconf
->name
, aconf
);
232 aconf
->hold
= rb_current_time() + temp_time
;
234 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
235 "%s added temporary %d min. RESV for [%s] [%s]",
236 get_oper_name(source_p
), temp_time
/ 60,
238 ilog(L_KLINE
, "R %s %d %s %s",
239 get_oper_name(source_p
), temp_time
/ 60,
241 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
242 temp_time
/ 60, name
);
245 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
246 aconf
->passwd
, NULL
, NULL
, 0);
248 else if(clean_resv_nick(name
))
250 if(strlen(name
) > NICKLEN
*2)
252 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
257 if(strchr(reason
, '"'))
259 sendto_one_notice(source_p
,
260 ":Invalid character '\"' in comment");
264 if(!valid_wild_card_simple(name
))
266 sendto_one_notice(source_p
,
267 ":Please include at least %d non-wildcard "
268 "characters with the resv",
269 ConfigFileEntry
.min_nonwildcard_simple
);
273 if(find_nick_resv_mask(name
))
275 sendto_one_notice(source_p
,
276 ":A RESV has already been placed on nick: %s",
282 aconf
->status
= CONF_RESV_NICK
;
284 aconf
->name
= rb_strdup(name
);
285 aconf
->passwd
= rb_strdup(reason
);
286 rb_dlinkAddAlloc(aconf
, &resv_conf_list
);
290 aconf
->hold
= rb_current_time() + temp_time
;
292 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
293 "%s added temporary %d min. RESV for [%s] [%s]",
294 get_oper_name(source_p
), temp_time
/ 60,
296 ilog(L_KLINE
, "R %s %d %s %s",
297 get_oper_name(source_p
), temp_time
/ 60,
299 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
300 temp_time
/ 60, name
);
303 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
304 aconf
->passwd
, NULL
, NULL
, 0);
307 sendto_one_notice(source_p
,
308 ":You have specified an invalid resv: [%s]",
313 propagate_resv(struct Client
*source_p
, const char *target
,
314 int temp_time
, const char *name
, const char *reason
)
318 sendto_match_servs(source_p
, target
,
321 target
, name
, reason
);
322 sendto_match_servs(source_p
, target
,
323 CAP_ENCAP
, CAP_CLUSTER
,
324 "ENCAP %s RESV %d %s 0 :%s",
325 target
, temp_time
, name
, reason
);
328 sendto_match_servs(source_p
, target
,
330 "ENCAP %s RESV %d %s 0 :%s",
331 target
, temp_time
, name
, reason
);
335 cluster_resv(struct Client
*source_p
, int temp_time
, const char *name
,
338 struct remote_conf
*shared_p
;
341 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
343 shared_p
= ptr
->data
;
345 /* old protocol cant handle temps, and we dont really want
346 * to convert them to perm.. --fl
350 if(!(shared_p
->flags
& SHARED_PRESV
))
353 sendto_match_servs(source_p
, shared_p
->server
,
356 shared_p
->server
, name
, reason
);
357 sendto_match_servs(source_p
, shared_p
->server
,
358 CAP_ENCAP
, CAP_CLUSTER
,
359 "ENCAP %s RESV 0 %s 0 :%s",
360 shared_p
->server
, name
, reason
);
362 else if(shared_p
->flags
& SHARED_TRESV
)
363 sendto_match_servs(source_p
, shared_p
->server
,
365 "ENCAP %s RESV %d %s 0 :%s",
366 shared_p
->server
, temp_time
, name
, reason
);
373 * parv[0] = sender prefix
374 * parv[1] = channel/nick to unforbid
377 mo_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
379 if(!IsOperResv(source_p
))
381 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
382 me
.name
, source_p
->name
, "resv");
386 if((parc
== 4) && (irccmp(parv
[2], "ON") == 0))
388 if(!IsOperRemoteBan(source_p
))
390 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
391 me
.name
, source_p
->name
, "remoteban");
395 propagate_generic(source_p
, "UNRESV", parv
[3], CAP_CLUSTER
,
398 if(match(parv
[3], me
.name
) == 0)
401 else if(rb_dlink_list_length(&cluster_conf_list
) > 0)
402 cluster_generic(source_p
, "UNRESV", SHARED_UNRESV
, CAP_CLUSTER
,
405 remove_resv(source_p
, parv
[1]);
410 * parv[0] = sender prefix
411 * parv[1] = target server
412 * parv[2] = resv to remove
415 ms_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
417 /* parv[0] parv[1] parv[2]
418 * oper target server resv to remove
420 propagate_generic(source_p
, "UNRESV", parv
[1], CAP_CLUSTER
,
423 if(!match(parv
[1], me
.name
))
426 if(!IsPerson(source_p
))
429 handle_remote_unresv(source_p
, parv
[2]);
434 me_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
437 if(!IsPerson(source_p
))
440 handle_remote_unresv(source_p
, parv
[1]);
445 handle_remote_unresv(struct Client
*source_p
, const char *name
)
447 if(!find_shared_conf(source_p
->username
, source_p
->host
,
448 source_p
->servptr
->name
, SHARED_UNRESV
))
451 remove_resv(source_p
, name
);
457 remove_resv(struct Client
*source_p
, const char *name
)
459 struct ConfItem
*aconf
= NULL
;
461 if(IsChannelName(name
))
463 if((aconf
= hash_find_resv(name
)) == NULL
)
465 sendto_one_notice(source_p
, ":No RESV for %s", name
);
471 if (!remove_resv_from_file(source_p
, name
))
476 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
477 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
478 "%s has removed the RESV for: [%s]",
479 get_oper_name(source_p
), name
);
480 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
482 del_from_resv_hash(name
, aconf
);
488 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
492 if(irccmp(aconf
->name
, name
))
500 sendto_one_notice(source_p
, ":No RESV for %s", name
);
506 if (!remove_resv_from_file(source_p
, name
))
511 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
512 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
513 "%s has removed the RESV for: [%s]",
514 get_oper_name(source_p
), name
);
515 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);
517 /* already have ptr from the loop above.. */
518 rb_dlinkDestroy(ptr
, &resv_conf_list
);
525 /* remove_resv_from_file()
527 * inputs - client removing the resv
530 * side effects - resv if found, is removed from conf
531 * - does not touch resv hash or resv_conf_list
534 remove_resv_from_file(struct Client
*source_p
, const char *name
)
539 char temppath
[BUFSIZE
];
540 const char *filename
;
543 int error_on_write
= 0;
546 rb_sprintf(temppath
, "%s.tmp", ConfigFileEntry
.resvfile
);
547 filename
= get_conf_name(RESV_TYPE
);
549 if((in
= fopen(filename
, "r")) == NULL
)
551 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
557 if((out
= fopen(temppath
, "w")) == NULL
)
559 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
567 while (fgets(buf
, sizeof(buf
), in
))
569 const char *resv_name
;
574 (void) unlink(temppath
);
579 rb_strlcpy(buff
, buf
, sizeof(buff
));
581 if((p
= strchr(buff
, '\n')) != NULL
)
584 if((*buff
== '\0') || (*buff
== '#'))
586 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
590 if((resv_name
= getfield(buff
)) == NULL
)
592 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
596 if(irccmp(resv_name
, name
) == 0)
602 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
608 error_on_write
= YES
;
612 sendto_one_notice(source_p
, ":Couldn't write temp resv file, aborted");
617 sendto_one_notice(source_p
, ":Cannot find RESV for %s in file", name
);
620 (void) unlink(temppath
);
625 if (rename(temppath
, filename
))
627 sendto_one_notice(source_p
, ":Couldn't rename temp file, aborted");
631 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
632 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
633 "%s has removed the RESV for: [%s]", get_oper_name(source_p
), name
);
634 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);