*** VERSION 5.2.0 ***
[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 int getUserCount() {
163     int i, count = 0;
164     struct UserNode *user;
165     for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
166         for(user = userList[i]; user; user = user->next) {
167             count++;
168         }
169     }
170     return count;
171 }
172
173 struct UserNode* addUser(const char *nick) {
174     int userListIndex = get_nicklist_entry(*nick);
175     if(userListIndex == -1 || !is_valid_nick(nick))
176         return NULL;
177     struct UserNode *user = malloc(sizeof(*user));
178     if (!user)
179     {
180         perror("malloc() failed");
181         return NULL;
182     }
183     strcpy(user->nick, nick);
184     user->created = time(0);
185     user->ident[0] = 0;
186     user->host[0] = 0;
187     user->realname[0] = 0;
188     user->flags = 0;
189     user->channel = NULL;
190     user->last_who = 0;
191     user->next = userList[userListIndex];
192     userList[userListIndex] = user;
193     return user;
194 }
195
196 struct UserNode* addUserMask(const char *mask) {
197     char cmask[strlen(mask)+1];
198     strcpy(cmask, mask);
199     int i, ii = 0;
200     struct UserNode *user = NULL;
201     for(i = 0; i < strlen(mask)+1; i++) {
202         if(cmask[i] == '!') {
203             cmask[i] = 0;
204             user = addUser(cmask);
205             if(user == NULL) return NULL;
206             ii = i+1;
207         } else if(cmask[i] == '.' && !user) {
208             //it's a server
209             return NULL;
210         } else if(cmask[i] == '@') {
211             if(user == NULL) return NULL;
212             cmask[i] = 0;
213             strcpy(user->ident, &cmask[ii]);
214             ii = i+1;
215         } else if(cmask[i] == '\0') {
216             if(user == NULL) return NULL;
217             strcpy(user->host, &cmask[ii]);
218         }
219     }
220     return user;
221 }
222
223 struct UserNode* createTempUser(const char *mask) {
224     //note: it could also be a server we have to create a temponary user for...
225     char cmask[strlen(mask)+1];
226     strcpy(cmask, mask);
227     int i, ii = 0;
228     struct UserNode *user = NULL;
229     for(i = 0; i < strlen(mask)+1; i++) {
230         if(cmask[i] == '!') {
231             cmask[i] = 0;
232             user = malloc(sizeof(*user));
233             if (!user)
234             {
235                 perror("malloc() failed");
236                 return NULL;
237             }
238             strcpy(user->nick, cmask);
239             user->created = time(0);
240             user->ident[0] = 0;
241             user->host[0] = 0;
242             user->realname[0] = 0;
243             user->flags = 0;
244             user->channel = NULL;
245             user->last_who = 0;
246             ii = i+1;
247         } else if(cmask[i] == '.' && !user) {
248             //it's a server
249             user = malloc(sizeof(*user));
250             if (!user)
251             {
252                 perror("malloc() failed");
253                 return NULL;
254             }
255             strcpy(user->host, cmask);
256             user->created = time(0);
257             user->ident[0] = 0;
258             user->host[0] = 0;
259             user->realname[0] = 0;
260             user->flags = USERFLAG_ISSERVER;
261             user->channel = NULL;
262             user->last_who = 0;
263             return user;
264         } else if(cmask[i] == '@') {
265             if(user == NULL) return NULL;
266             cmask[i] = 0;
267             strcpy(user->ident, &cmask[ii]);
268             ii = i+1;
269         } else if(cmask[i] == '\0') {
270             if(user == NULL) {
271                 //nick only
272                 user = malloc(sizeof(*user));
273                 if (!user)
274                 {
275                     perror("malloc() failed");
276                     return NULL;
277                 }
278                 strcpy(user->nick, cmask);
279                 user->created = time(0);
280                 user->ident[0] = 0;
281                 user->host[0] = 0;
282                 user->realname[0] = 0;
283                 user->flags = 0;
284                 user->channel = NULL;
285                 user->last_who = 0;
286                 return user;
287             }
288             strcpy(user->host, &cmask[ii]);
289         }
290     }
291     return user;
292 }
293
294 int renameUser(struct UserNode* user, const char *new_nick) {
295     if(!is_valid_nick(new_nick))
296         return 0;
297     if(user->nick[0] == *new_nick) {
298         strcpy(user->nick, new_nick);
299         return 1;
300     }
301     //delUser(user, 0); //EPIC FAIL! This deletes the user from the channel Userlist -.-
302     //manually remove the user from the old userList
303     int userListIndex = get_nicklist_entry(user->nick[0]);
304     if(userListIndex != -1) {
305         struct UserNode *cuser, *last_user = NULL;
306         for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
307             if(cuser == user) {
308                 if(last_user)
309                     last_user->next = user->next;
310                 else
311                     userList[userListIndex] = user->next;
312                 break;
313             } else
314                 last_user = cuser;
315         }
316     }
317     userListIndex = get_nicklist_entry(*new_nick);
318     strcpy(user->nick, new_nick);
319     user->next = userList[userListIndex];
320     userList[userListIndex] = user;
321     return 1;
322 }
323
324 void delUser(struct UserNode* user, int freeUser) {
325     int userListIndex = get_nicklist_entry(user->nick[0]);
326     if(userListIndex == -1) return;
327     struct UserNode *cuser, *last_user = NULL;
328     for(cuser = userList[userListIndex]; cuser; cuser = cuser->next) {
329         if(cuser == user) {
330             if(last_user)
331                 last_user->next = user->next;
332             else
333                 userList[userListIndex] = user->next;
334             break;
335         } else
336             last_user = cuser;
337     }
338     if(user->channel) {
339         struct ChanUser *chanUser, *next;
340         for(chanUser = user->channel; chanUser; chanUser = next) {
341             next = chanUser->next_chan;
342             removeChanUserFromLists(chanUser, 1, 0, freeUser);
343         }
344     }
345     if(freeUser)
346         free(user);
347     else
348         user->next = NULL;
349 }
350
351 void clearTempUsers() {
352     int userListIndex = TEMPUSER_LIST_INDEX;
353     struct UserNode *cuser, *last_user = NULL, *next;
354     time_t now = time(0);
355     for(cuser = userList[userListIndex]; cuser; cuser = next) {
356         next = cuser->next;
357         if(cuser->flags & USERFLAG_FREETMPUSER || now - cuser->created >= 300) {
358             if(last_user)
359                 last_user->next = cuser->next;
360             else
361                 userList[userListIndex] = cuser->next;
362             break;
363         } else
364             last_user = cuser;
365     }
366 }