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