Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / snomask.c
blobfeb7cb4a216d4823bad8506afa6cdde4e9414b58
1 /*
2 * charybdis: An advanced ircd.
3 * snomask.c: Management for user server-notice masks.
5 * Copyright (c) 2006 William Pitcock <nenolod@nenolod.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include "stdinc.h"
35 #include "client.h"
36 #include "snomask.h"
38 /* *INDENT-OFF* */
39 int snomask_modes[256] = {
40 /* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0F */
41 /* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x1F */
42 /* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x2F */
43 /* 0x30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x3F */
44 0, /* @ */
45 0, /* A */
46 0, /* B */
47 SNO_CCONNEXT, /* C */
48 0, /* D */
49 0, /* E */
50 0, /* F */
51 0, /* G */
52 0, /* H */
53 0, /* I */
54 0, /* J */
55 0, /* K */
56 0, /* L */
57 0, /* M */
58 SNO_NCHANGE_GLOBAL, /* N */
59 0, /* O */
60 0, /* P */
61 0, /* Q */
62 0, /* R */
63 0, /* S */
64 0, /* T */
65 0, /* U */
66 0, /* V */
67 0, /* W */
68 0, /* X */
69 0, /* Y */
70 0, /* Z */
71 /* 0x5B */ 0, 0, 0, 0, 0, 0, /* 0x60 */
72 0, /* a */
73 SNO_BOTS, /* b */
74 SNO_CCONN, /* c */
75 SNO_DEBUG, /* d */
76 0, /* e */
77 SNO_FULL, /* f */
78 0, /* g */
79 0, /* h */
80 0, /* i */
81 0, /* j */
82 SNO_SKILL, /* k */
83 0, /* l */
84 0, /* m */
85 SNO_NCHANGE, /* n */
86 0, /* o */
87 0, /* p */
88 0, /* q */
89 SNO_REJ, /* r */
90 SNO_GENERAL, /* s */
91 0, /* t */
92 SNO_UNAUTH, /* u */
93 0, /* v */
94 0, /* w */
95 SNO_EXTERNAL, /* x */
96 SNO_SPY, /* y */
97 0, /* z */
98 /* 0x7B */ 0, 0, 0, 0, 0, /* 0x7F */
99 /* 0x80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
100 /* 0x90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
101 /* 0xA0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xAF */
102 /* 0xB0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xBF */
103 /* 0xC0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xCF */
104 /* 0xD0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xDF */
105 /* 0xE0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xEF */
106 /* 0xF0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xFF */
108 /* *INDENT-ON* */
110 static char snobuf[BUFSIZE];
113 * construct_snobuf
115 * inputs - client to generate snomask string for
116 * outputs - snomask string of client
117 * side effects - NONE
119 char *
120 construct_snobuf(unsigned int val)
122 int i;
123 char *ptr = snobuf;
125 *ptr = '\0';
126 *ptr++ = '+';
128 for (i = 0; i < 128; i++)
129 if (snomask_modes[i] && (val & snomask_modes[i]))
130 *ptr++ = (char) i;
132 *ptr++ = '\0';
134 return snobuf;
138 * construct_snobuf_changes
140 * inputs - snomask before changes, snomask after changes
141 * outputs - snomask string of changes from before to after
142 * side effects - NONE
144 char *
145 construct_snobuf_changes(unsigned int addflags, unsigned int removeflags)
147 int i;
148 char *ptr = snobuf;
150 *ptr = '\0';
151 *ptr++ = '+';
153 if (addflags)
155 for (i = 0; i < 128; i++)
156 if (snomask_modes[i] && (addflags & snomask_modes[i]))
157 *ptr++ = (char) i;
160 if (removeflags)
162 *ptr++ = '-';
163 for (i = 0; i < 128; i++)
164 if (snomask_modes[i] && (removeflags & snomask_modes[i]))
165 *ptr++ = (char) i;
168 *ptr++ = '\0';
170 return snobuf;
175 * parse_snobuf_to_mask
177 * inputs - value to alter bitmask for, snomask itself
178 * outputs - replacement bitmask to set
179 * side effects - NONE
181 unsigned int
182 parse_snobuf_to_mask(unsigned int val, const char *sno)
184 const char *p;
185 int what = SNO_ADD;
187 if (sno == NULL)
188 return val;
190 for (p = sno; *p != '\0'; p++)
192 switch(*p)
194 case '+':
195 what = SNO_ADD;
196 break;
197 case '-':
198 what = SNO_DEL;
199 break;
200 default:
201 if (what == SNO_ADD)
202 val |= snomask_modes[(unsigned char) *p];
203 else if (what == SNO_DEL)
204 val &= ~snomask_modes[(unsigned char) *p];
206 break;
210 return val;
214 * find_snomask_slot
216 * inputs - NONE
217 * outputs - an available umode bitmask or
218 * 0 if no umodes are available
219 * side effects - NONE
221 unsigned int
222 find_snomask_slot(void)
224 unsigned int all_umodes = 0, my_umode = 0, i;
226 for (i = 0; i < 128; i++)
227 all_umodes |= snomask_modes[i];
229 for (my_umode = 1; my_umode && (all_umodes & my_umode);
230 my_umode <<= 1);
232 return my_umode;