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