ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/flags.C
Revision: 1.4
Committed: Tue Aug 28 17:08:12 2007 UTC (16 years, 9 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +17 -14 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# Content
1 /*
2 * flags.C: Functions to convert a flags table into a bitmask.
3 * Rights to this code are documented in doc/pod/license.pod.
4 *
5 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
6 */
7
8 static char const rcsid[] = "$Id: flags.C,v 1.3 2007-07-21 13:23:21 pippijn Exp $";
9
10 #include "atheme.h"
11 #include <account/chanacs.h>
12
13 #define FLAGS_ADD 0x1
14 #define FLAGS_DEL 0x2
15
16 unsigned int ca_all = CA_ALL_ALL;
17
18 static char flags_buf[128];
19
20 struct flags_table chanacs_flags[] = {
21 {'v', CA_VOICE},
22 {'V', CA_AUTOVOICE},
23 {'o', CA_OP},
24 {'O', CA_AUTOOP},
25 {'t', CA_TOPIC},
26 {'s', CA_SET},
27 {'r', CA_REMOVE},
28 {'i', CA_INVITE},
29 {'R', CA_RECOVER},
30 {'f', CA_FLAGS},
31 {'h', CA_HALFOP},
32 {'H', CA_AUTOHALFOP},
33 {'A', CA_ACLVIEW},
34 {'F', CA_FOUNDER},
35 {'b', CA_AKICK},
36 {0, 0}
37 };
38
39 /* Construct bitmasks to be added and removed
40 * Postcondition *addflags & *removeflags == 0
41 * -- jilles */
42 void
43 flags_make_bitmasks (char const * const string, struct flags_table table[], unsigned int *addflags, unsigned int *removeflags)
44 {
45 int status = FLAGS_ADD;
46 char const *s = string;
47 short i = 0;
48
49 *addflags = *removeflags = 0;
50 while (*s)
51 {
52 switch (*s)
53 {
54 case '+':
55 status = FLAGS_ADD;
56 break;
57
58 case '-':
59 status = FLAGS_DEL;
60 break;
61
62 case '=':
63 *addflags = 0;
64 *removeflags = 0xFFFFFFFF;
65 status = FLAGS_ADD;
66 break;
67
68 case '*':
69 if (status == FLAGS_ADD)
70 {
71 *addflags = 0xFFFFFFFF;
72 *removeflags = 0;
73
74 /* If this is chanacs_flags[], remove the ban flag. */
75 if (table == chanacs_flags)
76 {
77 *addflags &= ~CA_FOUNDER;
78 *removeflags |= CA_AKICK;
79 }
80 }
81 else if (status == FLAGS_DEL)
82 {
83 *addflags = 0;
84 *removeflags = 0xFFFFFFFF;
85 }
86 break;
87
88 default:
89 for (i = 0; table[i].flag; i++)
90 if (table[i].flag == *s)
91 {
92 if (status == FLAGS_ADD)
93 {
94 *addflags |= table[i].value;
95 *removeflags &= ~table[i].value;
96 }
97 else if (status == FLAGS_DEL)
98 {
99 *addflags &= ~table[i].value;
100 *removeflags |= table[i].value;
101 }
102 }
103 }
104
105 (void) *s++;
106 }
107
108 return;
109 }
110
111 unsigned int
112 flags_to_bitmask (char const * const string, struct flags_table table[], unsigned int flags)
113 {
114 int bitmask = (flags ? flags : 0x0);
115 int status = FLAGS_ADD;
116 char const *s = string;
117 short i = 0;
118
119 while (*s)
120 {
121 switch (*s)
122 {
123 case '+':
124 status = FLAGS_ADD;
125 break;
126
127 case '-':
128 status = FLAGS_DEL;
129 break;
130
131 case '=':
132 bitmask = 0;
133 status = FLAGS_ADD;
134 break;
135
136 case '*':
137 if (status == FLAGS_ADD)
138 {
139 bitmask = 0xFFFFFFFF;
140
141 /* If this is chanacs_flags[], do privs only */
142 if (table == chanacs_flags)
143 bitmask &= CA_ALLPRIVS & ca_all & ~CA_FOUNDER;
144 }
145 else if (status == FLAGS_DEL)
146 bitmask = 0;
147 break;
148
149 default:
150 for (i = 0; table[i].flag; i++)
151 if (table[i].flag == *s)
152 {
153 if (status == FLAGS_ADD)
154 bitmask |= table[i].value;
155 else if (status == FLAGS_DEL)
156 bitmask &= ~table[i].value;
157 }
158 }
159
160 (void) *s++;
161 }
162
163 return bitmask;
164 }
165
166 char *
167 bitmask_to_flags (unsigned int flags, struct flags_table table[])
168 {
169 char *bptr;
170 short i = 0;
171
172 bptr = flags_buf;
173
174 *bptr++ = '+';
175
176 for (i = 0; table[i].flag; i++)
177 if (table[i].value & flags)
178 *bptr++ = table[i].flag;
179
180 *bptr++ = '\0';
181
182 return flags_buf;
183 }
184
185 char *
186 bitmask_to_flags2 (unsigned int addflags, unsigned int removeflags, struct flags_table table[])
187 {
188 char *bptr;
189 short i = 0;
190
191 bptr = flags_buf;
192
193 if (removeflags)
194 {
195 *bptr++ = '-';
196 for (i = 0; table[i].flag; i++)
197 if (table[i].value & removeflags)
198 *bptr++ = table[i].flag;
199 }
200 if (addflags)
201 {
202 *bptr++ = '+';
203 for (i = 0; table[i].flag; i++)
204 if (table[i].value & addflags)
205 *bptr++ = table[i].flag;
206 }
207
208 *bptr++ = '\0';
209
210 return flags_buf;
211 }
212
213 /* flags a non-founder with +f and these flags is allowed to set -- jilles */
214 unsigned int
215 allow_flags (unsigned int flags)
216 {
217 flags &= ~CA_AKICK;
218 if (flags & CA_REMOVE)
219 flags |= CA_AKICK;
220 if (flags & CA_OP)
221 flags |= CA_AUTOOP;
222 if (flags & CA_HALFOP)
223 flags |= CA_AUTOHALFOP;
224 if (flags & CA_VOICE)
225 flags |= CA_AUTOVOICE;
226 return flags;
227 }
228
229 void
230 update_chanacs_flags (void)
231 {
232 ca_all = CA_ALL_ALL;
233 if (!ircd->uses_halfops)
234 ca_all &= ~(CA_HALFOP | CA_AUTOHALFOP);
235 }
236
237 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
238 * vim:ts=8
239 * vim:sw=8
240 * vim:noexpandtab
241 */