]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/huser.c
Large reorganisation of all Makefiles, including new configure script.
[irc/quakenet/newserv.git] / helpmod2 / huser.c
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <time.h>
6
7 #include "huser.h"
8 #include "hchannel.h"
9 #include "haccount.h"
10 #include "helpmod.h"
11 #include "hban.h"
12 #include "hgen.h"
13 #include "hed.h"
14
15 huser *huser_add(nick *nck)
16 {
17 huser *tmp;
18 int i;
19 assert(huser_get(nck) == NULL);
20
21 tmp = malloc(sizeof(huser));
22 tmp->real_user = nck;
23 tmp->last_activity = time(NULL);
24 tmp->flags = H_USRFLAGS_DEFAULT;
25 tmp->state = helpmod_base;
26 tmp->hchannels = NULL;
27
28 if (IsAccount(nck))
29 tmp->account = haccount_get_by_name(nck->authname);
30 else
31 tmp->account = NULL;
32
33 tmp->hchannels = NULL;
34
35 for (i=0;i<5;i++)
36 tmp->lc[i] = 0;
37
38 tmp->last_line_repeats = 0;
39 tmp->last_line[0] = '\0';
40
41 tmp->flood_val = 0;
42 tmp->spam_val = 0.0;
43
44 tmp->editor = NULL;
45
46 tmp->next = husers;
47 husers = tmp;
48
49 return tmp;
50 }
51
52 void huser_del(huser *husr)
53 {
54 huser **ptr = &husers;
55 if (!husr)
56 return;
57
58 for (;*ptr;ptr = &(*ptr)->next)
59 if (*ptr == husr)
60 {
61 huser *tmp = (*ptr)->next;
62
63 hed_close(husr->editor);
64
65 free(*ptr);
66 *ptr = tmp;
67
68 return;
69 }
70 }
71
72 void huser_del_all(void)
73 {
74 huser **ptr = &husers;
75
76 while (*ptr)
77 huser_del (*ptr);
78 }
79
80 huser *huser_get(nick *nck)
81 {
82 huser *tmp;
83
84 if (nck == NULL)
85 return NULL;
86
87 for (tmp = husers;tmp;tmp = tmp->next)
88 if (tmp->real_user == nck)
89 return tmp;
90 return NULL;
91 }
92
93 void huser_activity(huser *husr, hchannel *hchan)
94 {
95 huser_channel *huserchan;
96
97 husr->last_activity = time(NULL);
98 if (husr->account)
99 husr->account->last_activity = time(NULL);
100 if (hchan != NULL)
101 if ((huserchan = huser_on_channel(husr, hchan)) != NULL)
102 huserchan->last_activity = time(NULL);
103 }
104
105 huser_channel *huser_add_channel(huser *husr, hchannel *hchan)
106 {
107 huser_channel *tmp;
108 assert(huser_on_channel(husr, hchan) == NULL);
109
110 tmp = (huser_channel*)malloc(sizeof(huser_channel));
111
112 tmp->next = husr->hchannels;
113 tmp->hchan = hchan;
114 tmp->responsible_oper = NULL;
115 tmp->last_activity = time(NULL);
116 tmp->flags = 0;
117
118 husr->hchannels = tmp;
119
120 assert(huser_on_channel(husr, hchan) != NULL);
121
122 return tmp;
123 }
124
125 void huser_del_channel(huser *husr, hchannel *hchan)
126 {
127 huser_channel **tmp = &husr->hchannels;
128 assert(huser_on_channel(husr, hchan) != NULL);
129
130 for (;*tmp;tmp = &(*tmp)->next)
131 if ((*tmp)->hchan == hchan)
132 {
133 huser_channel *ptr = (*tmp)->next;
134
135 free(*tmp);
136 *tmp = ptr;
137 break;
138 }
139
140 assert(huser_on_channel(husr, hchan) == NULL);
141 }
142
143 huser_channel *huser_on_channel(huser *husr, hchannel *hchan)
144 {
145 huser_channel *tmp = husr->hchannels;
146
147 for (;tmp;tmp = tmp->next)
148 if (tmp->hchan == hchan)
149 return tmp;
150 return NULL;
151 }
152
153 void huser_clear_inactives(void)
154 {
155 huser **ptr = &husers;
156 while (*ptr)
157 if (!(*ptr)->hchannels && (time(NULL) - (*ptr)->last_activity) > HELPMOD_USER_TIMEOUT)
158 huser_del (*ptr);
159 else
160 ptr = &(*ptr)->next;
161 }
162
163 int huser_count(void)
164 {
165 int count = 0;
166 huser *tmp = husers;
167
168 for (;tmp;tmp = tmp->next)
169 count++;
170
171 return count;
172 }
173
174 void huser_reset_states(void)
175 {
176 huser *tmp = husers;
177
178 for (;tmp;tmp = tmp->next)
179 tmp->state = helpmod_base;
180 }
181
182 hlevel huser_get_level(huser *husr)
183 {
184 if ((strlen(huser_get_nick(husr))) == 1 && isalpha(huser_get_nick(husr)[0]))
185 return H_SERVICE; /* network services, keeps them out of harms way */
186
187 if (husr->account != NULL)
188 {
189 if (IsOper(husr->real_user) && (husr->account->level < H_STAFF))
190 return H_STAFF;
191 else if (!IsOper(husr->real_user) && (husr->account->level >= H_OPER))
192 return H_STAFF;
193 else
194 return husr->account->level;
195 }
196 else
197 {
198 if (IsOper(husr->real_user) || IsXOper(husr->real_user))
199 return H_STAFF;
200 else
201 return H_PEON;
202 }
203 }
204
205 int on_queue(huser *husr, huser_channel *huserchan)
206 {
207 if (huserchan == NULL || huser_get_level(husr) > H_PEON)
208 return 0;
209 if (!(huserchan->hchan->flags & H_QUEUE))
210 return 0;
211 if (huserchan->flags & (HCUMODE_OP | HCUMODE_VOICE))
212 return 0;
213 if (huserchan->flags & HQUEUE_DONE)
214 return 0;
215 return 1;
216 }
217
218 int on_desk(huser* husr, huser_channel *huserchan)
219 {
220 if (huserchan == NULL || huser_get_level(husr) > H_PEON)
221 return 0;
222 if (!(huserchan->hchan->flags & H_QUEUE))
223 return 0;
224 if (!(huserchan->flags & (HCUMODE_OP | HCUMODE_VOICE)))
225 return 0;
226 if (!(huserchan->flags & HQUEUE_DONE))
227 return 0;
228 return 1;
229 }
230
231 int huser_get_account_flags(huser *husr)
232 {
233 if (husr->account != NULL)
234 return husr->account->flags;
235 else
236 return H_ACCFLAGS_DEFAULT;
237 }
238
239 const char *huser_get_nick(huser *husr)
240 {
241 if (husr == NULL)
242 return "Error";
243 else
244 return husr->real_user->nick;
245 }
246
247 const char *huser_get_ident(huser *husr)
248 {
249 if (husr == NULL)
250 return "Error";
251 else
252 return husr->real_user->ident;
253 }
254
255 const char *huser_get_host(huser *husr)
256 {
257 if (husr == NULL)
258 return "Error";
259 else
260 return husr->real_user->host->name->content;
261 }
262
263 const char *huser_get_auth(huser *husr)
264 {
265 if (husr == NULL || !IsAccount(husr->real_user))
266 return "Error";
267 else
268 return husr->real_user->authname;
269 }
270
271 const char *huser_get_realname(huser *husr)
272 {
273 if (husr == NULL)
274 return "Error";
275 else
276 return husr->real_user->realname->name->content;
277
278 }
279
280 int huser_valid(huser* husr)
281 {
282 huser *ptr = husers;
283 for (;ptr;ptr = ptr->next)
284 if (ptr == husr)
285 return !0;
286 return 0;
287 }
288
289 int huser_is_trojan(huser *husr)
290 {
291 /* case 1 */
292 char buffer[32];
293
294 if (IsOper(husr->real_user))
295 return 0;
296
297 if (!IsAccount(husr->real_user))
298 if (strisupper(huser_get_nick(husr)) && (strnumcount( huser_get_nick(husr)) >= 2))
299 {
300 sprintf(buffer, "*%s*", huser_get_nick(husr));
301 if (hword_count(huser_get_realname(husr)) == 1)
302 if (strregexp(huser_get_realname(husr), buffer))
303 return 1;
304 }
305
306 /* case 2 */
307 if (!IsAccount(husr->real_user) && (huser_get_ident(husr)[0] == '~'))
308 if (strislower(huser_get_nick(husr)) && strisalpha( huser_get_nick(husr)))
309 if (strislower(huser_get_ident(husr)) && strisalpha(huser_get_ident(husr)))
310 if (strislower(huser_get_realname(husr)) && strisalpha(huser_get_realname(husr)))
311 if (hword_count(huser_get_realname(husr)) == 1)
312 return 1;
313
314 return 0;
315 }