1 /*
2  * levels.h: Unified levels system
3  *
4  * Copyright 1990 Michael Sandrof
5  * Copyright 1997, 2003 EPIC Software Labs
6  * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
7  */
8 
9 #ifndef __levels_h__
10 #define __levels_h__
11 
12 extern int	LEVEL_NONE;
13 extern int	LEVEL_OTHER;
14 extern int	LEVEL_PUBLIC;
15 extern int	LEVEL_MSG;
16 extern int	LEVEL_NOTICE;
17 extern int	LEVEL_WALL;
18 extern int	LEVEL_WALLOP;
19 extern int	LEVEL_OPNOTE;
20 extern int	LEVEL_SNOTE;
21 extern int	LEVEL_ACTION;
22 extern int	LEVEL_DCC;
23 extern int	LEVEL_CTCP;
24 extern int	LEVEL_INVITE;
25 extern int	LEVEL_JOIN;
26 extern int	LEVEL_NICK;
27 extern int	LEVEL_TOPIC;
28 extern int	LEVEL_PART;
29 extern int	LEVEL_QUIT;
30 extern int	LEVEL_KICK;
31 extern int	LEVEL_MODE;
32 extern int	LEVEL_OPERWALL;
33 extern int	LEVEL_SYSERR;
34 extern int	LEVEL_USER1;
35 extern int	LEVEL_USER2;
36 extern int	LEVEL_USER3;
37 extern int	LEVEL_USER4;
38 extern int	LEVEL_USER5;
39 extern int	LEVEL_USER6;
40 extern int	LEVEL_USER7;
41 extern int	LEVEL_USER8;
42 extern int	LEVEL_USER9;
43 extern int	LEVEL_USER10;
44 extern int	LEVEL_ALL;
45 
46 /*-
47  * Copyright (c) 1982, 1986, 1989, 1991, 1993
48  *	The Regents of the University of California.  All rights reserved.
49  *
50  * Redistribution and use in source and binary forms, with or without
51  * modification, are permitted provided that the following conditions
52  * are met:
53  * 1. Redistributions of source code must retain the above copyright
54  *    notice, this list of conditions and the following disclaimer.
55  * 2. Redistributions in binary form must reproduce the above copyright
56  *    notice, this list of conditions and the following disclaimer in the
57  *    documentation and/or other materials provided with the distribution.
58  * 3. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  */
74 
75 #define BIT_WORDS      2
76 #define BIT_MAXBIT     64
77 #define BIT_IDX(bit)   ((bit) - 1)
78 #define BIT_WORD(bit)  (BIT_IDX(bit) >> 5)
79 #define BIT_BIT(bit)   (1 << (BIT_IDX(bit) & 31))
80 #define BIT_VALID(bit) ((bit) < BIT_MAXBIT && (bit) > 0)
81 
82 typedef struct Mask {
83 	unsigned int	__bits[BIT_WORDS];
84 } Mask;
85 
mask_setall(Mask * set)86 __inline static int	mask_setall (Mask *set)
87 {
88 	int i;
89 
90 	for (i = 0; i < BIT_WORDS; i++)
91 		set->__bits[i] = ~0U;
92 	return 0;
93 }
94 
mask_unsetall(Mask * set)95 __inline static int	mask_unsetall (Mask *set)
96 {
97 	int i;
98 
99 	for (i = 0; i < BIT_WORDS; i++)
100 		set->__bits[i] = 0;
101 	return 0;
102 }
103 
mask_set(Mask * set,int bit)104 __inline static int	mask_set (Mask *set, int bit)
105 {
106 	if (bit == LEVEL_NONE)
107 		return mask_unsetall(set);
108 	if (bit == LEVEL_ALL)
109 		return mask_setall(set);
110 
111 	if (!BIT_VALID(bit))
112 		return -1;
113 	set->__bits[BIT_WORD(bit)] |= BIT_BIT(bit);
114 	return 0;
115 }
116 
mask_unset(Mask * set,int bit)117 __inline static int	mask_unset (Mask *set, int bit)
118 {
119 	if (bit == LEVEL_NONE)
120 		return mask_setall(set);
121 	if (bit == LEVEL_ALL)
122 		return mask_unsetall(set);
123 
124 	if (!BIT_VALID(bit))
125 		return -1;
126 	set->__bits[BIT_WORD(bit)] &= ~BIT_BIT(bit);
127 	return 0;
128 }
129 
mask_isall(const Mask * set)130 __inline static int	mask_isall (const Mask *set)
131 {
132 	int	i;
133 
134 	for (i = 0; i < BIT_WORDS; i++)
135 		if (set->__bits[i] != ~0U)
136 			return 0;
137 	return 1;
138 }
139 
mask_isnone(const Mask * set)140 __inline static int	mask_isnone (const Mask *set)
141 {
142 	int	i;
143 
144 	for (i = 0; i < BIT_WORDS; i++)
145 		if (set->__bits[i] != 0U)
146 			return 0;
147 	return 1;
148 }
149 
mask_isset(const Mask * set,int bit)150 __inline static int	mask_isset (const Mask *set, int bit)
151 {
152 	if (bit == LEVEL_NONE)
153 		return mask_isnone(set);
154 	if (bit == LEVEL_ALL)
155 		return mask_isall(set);
156 
157 	if (!BIT_VALID(bit))
158 		return -1;
159 	return ((set->__bits[BIT_WORD(bit)] & BIT_BIT(bit)) ? 1 : 0);
160 }
161 
162 /*---------------- end of bsd stuff ------------------*/
163 
164 	void		init_levels	(void);
165 	int		add_new_level	(const char *);
166 	int		add_new_level_alias (int, const char *);
167 	char *		get_all_levels	(void);
168 	const char *	mask_to_str	(const Mask *);
169 	int		str_to_mask	(Mask *, const char *, char **);
170 	void    	standard_level_warning (const char *, char **);
171 	const char *	level_to_str	(int);
172 	int		str_to_level	(const char *);
173 	char *		levelctl	(char *);
174 
175 #endif
176