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
35 #include "s_newconf.h"
38 #include "sprintf_irc.h"
40 static int mo_resv(struct Client
*, struct Client
*, int, const char **);
41 static int ms_resv(struct Client
*, struct Client
*, int, const char **);
42 static int me_resv(struct Client
*, struct Client
*, int, const char **);
43 static int mo_unresv(struct Client
*, struct Client
*, int, const char **);
44 static int ms_unresv(struct Client
*, struct Client
*, int, const char **);
45 static int me_unresv(struct Client
*, struct Client
*, int, const char **);
47 struct Message resv_msgtab
= {
48 "RESV", 0, 0, 0, MFLG_SLOW
| MFLG_UNREG
,
49 {mg_ignore
, mg_not_oper
, {ms_resv
, 4}, {ms_resv
, 4}, {me_resv
, 5}, {mo_resv
, 3}}
51 struct Message unresv_msgtab
= {
52 "UNRESV", 0, 0, 0, MFLG_SLOW
| MFLG_UNREG
,
53 {mg_ignore
, mg_not_oper
, {ms_unresv
, 3}, {ms_unresv
, 3}, {me_unresv
, 2}, {mo_unresv
, 2}}
56 mapi_clist_av1 resv_clist
[] = { &resv_msgtab
, &unresv_msgtab
, NULL
};
57 DECLARE_MODULE_AV1(resv
, NULL
, NULL
, resv_clist
, NULL
, NULL
, "$Revision: 125 $");
59 static void parse_resv(struct Client
*source_p
, const char *name
,
60 const char *reason
, int temp_time
);
61 static void propagate_resv(struct Client
*source_p
, const char *target
,
62 int temp_time
, const char *name
, const char *reason
);
63 static void cluster_resv(struct Client
*source_p
, int temp_time
,
64 const char *name
, const char *reason
);
66 static void handle_remote_unresv(struct Client
*source_p
, const char *name
);
67 static void remove_resv(struct Client
*source_p
, const char *name
);
68 static int remove_temp_resv(struct Client
*source_p
, const char *name
);
72 * parv[0] = sender prefix
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
), me
.name
, source_p
->name
, "resv");
91 /* RESV [time] <name> [ON <server>] :<reason> */
93 if((temp_time
= valid_temp_time(parv
[loc
])) >= 0)
95 /* we just set temp_time to -1! */
102 if((parc
>= loc
+2) && (irccmp(parv
[loc
], "ON") == 0))
104 target_server
= parv
[loc
+1];
108 if(parc
<= loc
|| EmptyString(parv
[loc
]))
110 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
),
111 me
.name
, source_p
->name
, "RESV");
120 if(!IsOperRemoteBan(source_p
))
122 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
,
128 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
129 "%s is setting a temporary %d-minute RESV for %s on %s",
130 get_oper_name(source_p
), temp_time
, name
, target_server
);
132 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
133 "%s is setting a RESV for %s on %s",
134 get_oper_name(source_p
), name
, target_server
);
137 propagate_resv(source_p
, target_server
, temp_time
, name
, reason
);
139 if(match(target_server
, me
.name
) == 0)
142 else if(dlink_list_length(&cluster_conf_list
) > 0)
143 cluster_resv(source_p
, temp_time
, name
, reason
);
145 parse_resv(source_p
, name
, reason
, temp_time
);
151 * parv[0] = sender prefix
152 * parv[1] = target server
153 * parv[2] = channel/nick to forbid
157 ms_resv(struct Client
*client_p
, struct Client
*source_p
,
158 int parc
, const char *parv
[])
160 /* parv[0] parv[1] parv[2] parv[3]
161 * oper target server resv reason
163 propagate_resv(source_p
, parv
[1], 0, parv
[2], parv
[3]);
165 if(!match(parv
[1], me
.name
))
168 if(!IsPerson(source_p
))
171 parse_resv(source_p
, parv
[2], parv
[3], 0);
176 me_resv(struct Client
*client_p
, struct Client
*source_p
,
177 int parc
, const char *parv
[])
179 /* time name 0 :reason */
180 if(!IsPerson(source_p
))
183 parse_resv(source_p
, parv
[2], parv
[4], atoi(parv
[1]));
189 * inputs - source_p if error messages wanted
193 * side effects - will parse the resv and create it if valid
196 parse_resv(struct Client
*source_p
, const char *name
,
197 const char *reason
, int temp_time
)
199 struct ConfItem
*aconf
;
201 if (!MyClient(source_p
) &&
202 !find_client_shared_conf(source_p
, temp_time
> 0)? SHARED_TRESV
: SHARED_PRESV
)
205 if(IsChannelName(name
))
207 if(hash_find_resv(name
))
209 sendto_one_notice(source_p
,
210 ":A RESV has already been placed on channel: %s",
215 if(strlen(name
) > CHANNELLEN
)
217 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
222 if(strchr(reason
, '"'))
224 sendto_one_notice(source_p
,
225 ":Invalid character '\"' in comment");
230 aconf
->status
= CONF_RESV_CHANNEL
;
232 DupString(aconf
->name
, name
);
233 DupString(aconf
->passwd
, reason
);
234 add_to_resv_hash(aconf
->name
, aconf
);
238 aconf
->hold
= CurrentTime
+ temp_time
;
240 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
241 "%s added temporary %d min. RESV for [%s] [%s]",
242 get_oper_name(source_p
), temp_time
/ 60,
244 ilog(L_KLINE
, "R %s %d %s %s",
245 get_oper_name(source_p
), temp_time
/ 60,
247 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
248 temp_time
/ 60, name
);
251 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
252 aconf
->passwd
, NULL
, NULL
, 0);
254 else if(clean_resv_nick(name
))
256 if(strlen(name
) > NICKLEN
*2)
258 sendto_one_notice(source_p
, ":Invalid RESV length: %s",
263 if(strchr(reason
, '"'))
265 sendto_one_notice(source_p
,
266 ":Invalid character '\"' in comment");
270 if(!valid_wild_card_simple(name
))
272 sendto_one_notice(source_p
,
273 ":Please include at least %d non-wildcard "
274 "characters with the resv",
275 ConfigFileEntry
.min_nonwildcard_simple
);
279 if(find_nick_resv(name
))
281 sendto_one_notice(source_p
,
282 ":A RESV has already been placed on nick: %s",
288 aconf
->status
= CONF_RESV_NICK
;
290 DupString(aconf
->name
, name
);
291 DupString(aconf
->passwd
, reason
);
292 dlinkAddAlloc(aconf
, &resv_conf_list
);
296 aconf
->hold
= CurrentTime
+ temp_time
;
298 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
299 "%s added temporary %d min. RESV for [%s] [%s]",
300 get_oper_name(source_p
), temp_time
/ 60,
302 ilog(L_KLINE
, "R %s %d %s %s",
303 get_oper_name(source_p
), temp_time
/ 60,
305 sendto_one_notice(source_p
, ":Added temporary %d min. RESV [%s]",
306 temp_time
/ 60, name
);
309 write_confitem(RESV_TYPE
, source_p
, NULL
, aconf
->name
,
310 aconf
->passwd
, NULL
, NULL
, 0);
313 sendto_one_notice(source_p
,
314 ":You have specified an invalid resv: [%s]",
319 propagate_resv(struct Client
*source_p
, const char *target
,
320 int temp_time
, const char *name
, const char *reason
)
324 sendto_match_servs(source_p
, target
,
327 target
, name
, reason
);
328 sendto_match_servs(source_p
, target
,
329 CAP_ENCAP
, CAP_CLUSTER
,
330 "ENCAP %s RESV %d %s 0 :%s",
331 target
, temp_time
, name
, reason
);
334 sendto_match_servs(source_p
, target
,
336 "ENCAP %s RESV %d %s 0 :%s",
337 target
, temp_time
, name
, reason
);
341 cluster_resv(struct Client
*source_p
, int temp_time
, const char *name
,
344 struct remote_conf
*shared_p
;
347 DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
349 shared_p
= ptr
->data
;
351 /* old protocol cant handle temps, and we dont really want
352 * to convert them to perm.. --fl
356 if(!(shared_p
->flags
& SHARED_PRESV
))
359 sendto_match_servs(source_p
, shared_p
->server
,
362 shared_p
->server
, name
, reason
);
363 sendto_match_servs(source_p
, shared_p
->server
,
364 CAP_ENCAP
, CAP_CLUSTER
,
365 "ENCAP %s RESV 0 %s 0 :%s",
366 shared_p
->server
, name
, reason
);
368 else if(shared_p
->flags
& SHARED_TRESV
)
369 sendto_match_servs(source_p
, shared_p
->server
,
371 "ENCAP %s RESV %d %s 0 :%s",
372 shared_p
->server
, temp_time
, name
, reason
);
379 * parv[0] = sender prefix
380 * parv[1] = channel/nick to unforbid
383 mo_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
385 if(!IsOperResv(source_p
))
387 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
, "resv");
391 if((parc
== 4) && (irccmp(parv
[2], "ON") == 0))
393 if(!IsOperRemoteBan(source_p
))
395 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
, source_p
->name
,
398 propagate_generic(source_p
, "UNRESV", parv
[3], CAP_CLUSTER
,
401 if(match(parv
[3], me
.name
) == 0)
405 else if(dlink_list_length(&cluster_conf_list
) > 0)
406 cluster_generic(source_p
, "UNRESV", SHARED_UNRESV
, CAP_CLUSTER
,
409 if(remove_temp_resv(source_p
, parv
[1]))
412 remove_resv(source_p
, parv
[1]);
417 * parv[0] = sender prefix
418 * parv[1] = target server
419 * parv[2] = resv to remove
422 ms_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
424 /* parv[0] parv[1] parv[2]
425 * oper target server resv to remove
427 propagate_generic(source_p
, "UNRESV", parv
[1], CAP_CLUSTER
,
430 if(!match(parv
[1], me
.name
))
433 if(!IsPerson(source_p
))
436 handle_remote_unresv(source_p
, parv
[2]);
441 me_unresv(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
444 if(!IsPerson(source_p
))
447 handle_remote_unresv(source_p
, parv
[1]);
452 handle_remote_unresv(struct Client
*source_p
, const char *name
)
454 if (!find_client_shared_conf(source_p
, SHARED_UNRESV
))
456 if(remove_temp_resv(source_p
, name
))
459 remove_resv(source_p
, name
);
465 remove_temp_resv(struct Client
*source_p
, const char *name
)
467 struct ConfItem
*aconf
= NULL
;
469 if(IsChannelName(name
))
471 if((aconf
= hash_find_resv(name
)) == NULL
)
474 /* its permanent, let remove_resv do it properly */
478 del_from_resv_hash(name
, aconf
);
485 DLINK_FOREACH(ptr
, resv_conf_list
.head
)
489 if(irccmp(aconf
->name
, name
))
498 /* permanent, remove_resv() needs to do it properly */
502 /* already have ptr from the loop above.. */
503 dlinkDestroy(ptr
, &resv_conf_list
);
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
);
518 * inputs - client removing the resv
521 * side effects - resv if found, is removed
524 remove_resv(struct Client
*source_p
, const char *name
)
529 char temppath
[BUFSIZE
];
530 const char *filename
;
533 int error_on_write
= 0;
536 ircsprintf(temppath
, "%s.tmp", ConfigFileEntry
.resvfile
);
537 filename
= get_conf_name(RESV_TYPE
);
539 if((in
= fopen(filename
, "r")) == NULL
)
541 sendto_one_notice(source_p
, ":Cannot open %s", filename
);
547 if((out
= fopen(temppath
, "w")) == NULL
)
549 sendto_one_notice(source_p
, ":Cannot open %s", temppath
);
557 while (fgets(buf
, sizeof(buf
), in
))
559 const char *resv_name
;
564 (void) unlink(temppath
);
569 strlcpy(buff
, buf
, sizeof(buff
));
571 if((p
= strchr(buff
, '\n')) != NULL
)
574 if((*buff
== '\0') || (*buff
== '#'))
576 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
580 if((resv_name
= getfield(buff
)) == NULL
)
582 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
586 if(irccmp(resv_name
, name
) == 0)
592 error_on_write
= (fputs(buf
, out
) < 0) ? YES
: NO
;
601 sendto_one_notice(source_p
, ":Couldn't write temp resv file, aborted");
606 sendto_one_notice(source_p
, ":No RESV for %s", name
);
609 (void) unlink(temppath
);
614 (void) rename(temppath
, filename
);
617 sendto_one_notice(source_p
, ":RESV for [%s] is removed", name
);
618 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
619 "%s has removed the RESV for: [%s]", get_oper_name(source_p
), name
);
620 ilog(L_KLINE
, "UR %s %s", get_oper_name(source_p
), name
);