added cmd_staff
[NeonServV5.git] / src / UserNode.c
1 /* UserNode.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "UserNode.h"
18 #include "ChanUser.h"
19 #include "tools.h"
20
21 static struct UserNode **userList;
22
23 void init_UserNode() {
24     userList = calloc(VALID_NICK_CHARS_FIRST_LEN+1, sizeof(*userList));
25 }
26
27 void free_UserNode() {
28     //kamikaze free all users
29     //chanusers will be destroyed in free_ChanNode()
30     int i;
31     struct UserNode *user, *next;
32     for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
33         for(user = userList[i]; user; user = next) {
34             next = user->next;
35             free(user);
36         }
37     }
38     free(userList);
39 }
40
41 int is_valid_nick(const char *nick) {
42     unsigned int i;
43     //first char must be one of: a-zA-Z{|}~[\]^_`
44     if (!strchr(VALID_NICK_CHARS_FIRST, *nick))
45         return 0;
46     //all other chars must be one of: a-zA-Z0-9{|}~[\]^_`
47     for (i = 0; nick[i]; ++i)
48         if (!strchr(VALID_NICK_CHARS, nick[i]))
49             return 0;
50     if (strlen(nick) > NICKLEN)
51         return 0;
52     return 1;
53 }
54
55 static int get_nicklist_entry(int nick) {
56     int i;
57     char *valid_chars = VALID_NICK_CHARS_FIRST;
58     for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN; i++) {
59         if(valid_chars[i] == nick)
60             return i;
61     }
62     return -1; //ERROR!
63 }
64
65 struct UserNode* getUserByNick(const char *nick) { //case sensitive
66     int userListIndex = get_nicklist_entry(*nick);
67     if(userListIndex == -1 || userList[userListIndex] == NULL)
68         return NULL;
69     struct UserNode *user;
70     for(user = userList[userListIndex]; user; user = user->next) {
71         if(!stricmp(nick, user->nick))
72             return user;
73     }
74     return NULL;
75 }
76
77 struct UserNode* getUserByMask(const char *mask) { //case sensitive
78     char cmask[strlen(mask)+1];
79     strcpy(cmask, mask);
80     int i;
81     struct UserNode *user = NULL;
82     for(i = 0; i < strlen(mask); i++) {
83         if(cmask[i] == '!') {
84             cmask[i] = 0;
85             user = getUserByNick(&cmask[0]);
86             return user;
87         } else if(cmask[i] == '.') {
88             //it's a server
89             return NULL;
90         }
91     }
92     return NULL;
93 }
94
95 struct UserNode* searchUserByNick(const char *nick) { //case insensitive
96     if(!isalpha(*nick)) 
97         return getUserByNick(nick);
98
99     int userListIndex;
100     struct UserNode *user;
101
102     //search in the lower case "section"
103     userListIndex = get_nicklist_entry(tolower(*nick));
104     if(userListIndex != -1 && userList[userListIndex] != NULL) {
105         for(user = userList[userListIndex]; user; user = user->next) {
106             if(!stricmp(nick, user->nick))
107                 return user;
108         }
109     }
110     //search in the upper case "section"
111     userListIndex = get_nicklist_entry(toupper(*nick));
112     if(userListIndex != -1 && userList[userListIndex] != NULL) {
113         for(user = userList[userListIndex]; user; user = user->next) {
114             if(!stricmp(nick, user->nick))
115                 return user;
116         }
117     }
118     return NULL;
119 }
120
121 int countUsersWithHost(char *host) {
122     int i, count = 0;
123     struct UserNode *user;
124     for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
125         for(user = userList[i]; user; user = user->next) {
126             if(!strcmp(user->host, host)) {
127                 count++;
128             }
129         }
130     }
131     return count;
132 }
133
134 char *getAuthFakehost(char *auth) {
135     int i;
136     struct UserNode *user;
137     for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
138         for(user = userList[i]; user; user = user->next) {
139             if((user->flags & USERFLAG_ISAUTHED) && !strcmp(user->auth, auth) && isFakeHost(user->host)) {
140                 return user->host;
141             }
142         }
143     }
144     return NULL;
145 }
146
147 struct UserNode* getAllUsers(struct UserNode *last) {
148     if(last == NULL || last->next == NULL) {
149         int cindex;
150         if(last == NULL)
151             cindex = 0;
152         else
153             cindex = get_nicklist_entry(last->nick[0]) + 1;
154         while(userList[cindex] == NULL && cindex <= VALID_NICK_CHARS_FIRST_LEN)
155             cindex++;
156         if(cindex > VALID_NICK_CHARS_FIRST_LEN) return NULL;
157         return userList[cindex];
158     } else
159         return last->next;
160 }
161
162 struct UserNode* getUsersWithAuth(const char *auth, struct UserNode *last) {
163     int cindex = (last ? get_nicklist_entry(last->nick[0]) : 0);
164     struct UserNode *cuser = last;
165     while(cindex <= VALID_NICK_CHARS_FIRST_LEN) {
166         for(cuser = (cuser ? cuser->next : userList[cindex]); cuser; cuser = cuser->next) {
167             if((cuser->flags & USERFLAG_ISAUTHED) && !strcmp(cuser->auth, auth))
168                 return cuser;
169         }
170         cindex++;
171         cuser = NULL;
172     }
173     return NULL;
174 }
175
176 int getUserCount() {
177     int i, count = 0;
178     struct UserNode *user;
179     for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
180         for(user = userList[i]; user; user = user->next) {
181             count++;
182         }
183     }
184     return count;
185 }
186
187 struct UserNode* addUser(const char *nick) {
188     int userListIndex = get_nicklist_entry(*nick);
189     if(userListIndex == -1 || !is_valid_nick(nick))
190         return NULL;
191     struct UserNode *user = malloc(sizeof(*user));
192     if (!user)
193     {
194         perror("malloc() failed");
195         return NULL;
196     }
197     strcpy(user->nick, nick);
198     user->created = time(0);
199     user->ident[0] = 0;
200     user->host[0] = 0;
201     user->realname[0] = 0;
202     user->flags = 0;
203     user->channel = NULL;
204     user->last_who = 0;
205     user->next = userList[userListIndex];
206     userList[userListIndex] = user;
207     return user;
208 }
209
210 struct UserNode* addUserMask(const char *mask) {
211     char cmask[strlen(mask)+1];
212     strcpy(cmask, mask);
213     int i, ii = 0;
214     struct UserNode *user = NULL;
215     for(i = 0; i < strlen(mask)+1; i++) {
216         if(cmask[i] == '!') {
217             cmask[i] = 0;
218             user = addUser(cmask);
219             if(user == NULL) return NULL;
220             ii = i+1;
221         } else if(cmask[i] == '.' && !user) {
222             //it's a server
223             return NULL;
224         } else if(cmask[i] == '@') {
225             if(user == NULL) return NULL;
226             cmask[i] = 0;
227             strcpy(user->ident, &cmask[ii]);
228             ii = i+1;
229         } else if(cmask[i] == '\0') {
230             if(user == NULL) return NULL;
231             strcpy(user->host, &cmask[ii]);
232         }
233     }
234     return user;
235 }
236
237 struct UserNode* createTempUser(const char *mask) {
238     //note: it could also be a server we have to create a temponary user for...
239     char cmask[strlen(mask)+1];
240     strcpy(cmask, mask);
241     int i, ii = 0;
242     struct UserNode *user = NULL;
243     for(i = 0; i < strlen(mask)+1; i++) {
244         if(cmask[i] == '!') {
245             cmask[i] = 0;
246             user = malloc(sizeof(*user));
247             if (!user)
248             {
249                 perror("malloc() failed");
250                 return NULL;
251             }
252             strcpy(user->nick, cmask);
253             user->created = time(0);
254             user->ident[0] = 0;
255             user->host[0] = 0;
256             user->realname[0] = 0;
257             user->flags = 0;
258             user->channel = NULL;
259             user->last_who = 0;
260             ii = i+1;
261         } else if(cmask[i] == '.' && !user) {
262             //it's a server
263             user = malloc(sizeof(*user));
264             if (!user)
265             {
266                 perror("malloc() failed");
267                 return NULL;
268             }
269             strcpy(user->host, cmask);
270             user->created = time(0);
271             user->ident[0] = 0;
272             user->host[0] = 0;
273             user->realname[0] = 0;
274             user->flags = USERFLAG_ISSERVER;
275             user->channel = NULL;
276             user->last_who = 0;
277             return user;
278         } else if(cmask[i] == '@') {
279             if(user == NULL) return NULL;
280             cmask[i] = 0;
281             strcpy(user->ident, &cmask[ii]);
282             ii = i+1;
283         } else if(cmask[i] == '\0') {
284             if(user == NULL) {
285                 //nick only
286                 user = malloc(sizeof(*user));
287                 if (!user)
288                 {
289                     perror("malloc() failed");
290                     return NULL;
291                 }
292                 strcpy(user->nick, cmask);
293                 user->created = time(0);
294                 user->ident[0] = 0;
295                 user->host[0] = 0;
296                 user->realname[0] = 0;
297                 user->flags = 0;
298                 user->channel = NULL;
299                 user->last_who = 0;
300                 return user;
301             }
302             strcpy(user->host, &cmask[ii]);
303         }
304     }
305     return user;
306 }
307
308 int renameUser(struct UserNode* user, const char *new_nick) {
309     if(!is_valid_nick(new_nick))
310         return 0;
311     if(user->nick[0] == *new_nick) {
312         strcpy(user->nick, new_nick);
313         return 1;
314     }
315     //delUser(user, 0); //EPIC FAIL! This deletes the user from the channel Userlist -.-
316     //manually remove the user from the old userList
317     int userListIndex = get_nicklist_entry(user->nick[0]);
318     if(userListIndex != -1) {
319         struct UserNode *cuser, *last_user = NULL;
320         for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
321             if(cuser == user) {
322                 if(last_user)
323                     last_user->next = user->next;
324                 else
325                     userList[userListIndex] = user->next;
326                 break;
327             } else
328                 last_user = cuser;
329         }
330     }
331     userListIndex = get_nicklist_entry(*new_nick);
332     strcpy(user->nick, new_nick);
333     user->next = userList[userListIndex];
334     userList[userListIndex] = user;
335     return 1;
336 }
337
338 void delUser(struct UserNode* user, int freeUser) {
339     int userListIndex = get_nicklist_entry(user->nick[0]);
340     if(userListIndex == -1) return;
341     struct UserNode *cuser, *last_user = NULL;
342     for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
343         if(cuser == user) {
344             if(last_user)
345                 last_user->next = user->next;
346             else
347                 userList[userListIndex] = user->next;
348             break;
349         } else
350             last_user = cuser;
351     }
352     if(user->channel) {
353         struct ChanUser *chanUser, *next;
354         for(chanUser = user->channel; chanUser; chanUser = next) {
355             next = chanUser->next_chan;
356             removeChanUserFromLists(chanUser, 1, 0, freeUser);
357         }
358     }
359     if(freeUser)
360         free(user);
361     else
362         user->next = NULL;
363 }
364
365 void clearTempUsers() {
366     int userListIndex = TEMPUSER_LIST_INDEX;
367     struct UserNode *cuser, *last_user = NULL, *next;
368     time_t now = time(0);
369     for(cuser = userList[userListIndex]; cuser; cuser = next) {
370         next = cuser->next;
371         if(cuser->flags & USERFLAG_FREETMPUSER || now - cuser->created >= 300) {
372             if(last_user)
373                 last_user->next = cuser->next;
374             else
375                 userList[userListIndex] = cuser->next;
376             break;
377         } else
378             last_user = cuser;
379     }
380 }