added GPL header to all files and added INSTALL AUTHORS COPYING files.
[NeonServV5.git] / src / WHOHandler.c
1 /* WHOHandler.c - NeonServ v5.0
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
18 #include "WHOHandler.h"
19 #include "ChanNode.h"
20 #include "UserNode.h"
21 #include "ChanUser.h"
22 #include "ClientSocket.h"
23
24 #define WHOQUEUETYPE_ISONQUEUE 0x01
25 #define WHOQUEUETYPE_USERLIST  0x02
26 #define WHOQUEUETYPE_USERAUTH  0x04
27 #define WHOQUEUETYPE_CHECKTYPE 0x07
28 #define WHOQUEUETYPE_FOUND     0x08
29
30 struct WHOQueueEntry {
31     char type;
32     struct ClientSocket *client;
33     struct ChanNode *chan;
34     struct UserNode *user;
35     struct WHOQueueEntry *next;
36     void *callback;
37     void *data;
38 };
39
40 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
41
42 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
43     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
44     if (!entry)
45     {
46         perror("malloc() failed");
47         return NULL;
48     }
49     entry->next = NULL;
50     entry->client = client;
51     if(last_entry) {
52         last_entry->next = entry;
53         last_entry = entry;
54     } else {
55         last_entry = entry;
56         first_entry = entry;
57     }
58     return entry;
59 }
60
61 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
62     if(!first_entry) return NULL;
63     struct WHOQueueEntry *entry;
64     for(entry = first_entry; entry; entry = entry->next) {
65         if(entry->client == client)
66             break;
67     }
68     if(entry == NULL) return NULL;
69     if(freeEntry) {
70         if(entry == first_entry)
71             first_entry = entry->next;
72         if(entry == last_entry) {
73             struct WHOQueueEntry *last;
74             for(last = first_entry; last; last = last->next)
75                 if(last->next == NULL) break;
76             last_entry = last;
77         }
78     }
79     return entry;
80 }
81
82 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
83     struct ClientSocket *bot;
84     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
85         if(isUserOnChan(bot->user, chan))
86             break;
87     }
88     if(bot == NULL) return;
89     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
90     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
91     entry->chan = chan;
92     entry->callback = callback;
93     entry->data = data;
94     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
95     putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
96 }
97
98 void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data) {
99     struct ClientSocket *bot;
100     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
101         if(isUserOnChan(bot->user, chan))
102             break;
103     }
104     if(bot == NULL) return;
105     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
106     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
107     entry->chan = chan;
108     entry->callback = callback;
109     entry->data = data;
110     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
111     putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
112 }
113
114 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
115     struct ClientSocket *bot;
116     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
117         if(bot->flags & SOCKET_FLAG_PREFERRED)
118             break;
119     }
120     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
121     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
122     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
123     entry->user = user;
124     entry->callback = callback;
125     entry->data = data;
126     //WHO ".$user->getNick().",".$id." %tuhna,".$id
127     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
128 }
129
130 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
131     int i;
132     if(argc < 2) return;
133     int type = atoi(argv[1]);
134     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
135     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
136     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
137     if(type & WHOQUEUETYPE_USERLIST) {
138         if(argc < 7) return;
139         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
140         struct ChanNode *chan = entry->chan;
141         //add the user toe the channel if he isn't added, yet and update its user data
142         //parse flags
143         int userflags = 0;
144         int chanuserflags = 0;
145         for(i = 0; i < strlen(argv[5]); i++) {
146             switch (argv[5][i]) {
147                 case '@':
148                     chanuserflags |= CHANUSERFLAG_OPPED;
149                     break;
150                 case '+':
151                     chanuserflags |= CHANUSERFLAG_VOICED;
152                     break;
153                 case '*':
154                     userflags |= USERFLAG_ISIRCOP;
155                     break;
156                 case '<':
157                     chanuserflags |= CHANUSERFLAG_INVISIBLE;
158                     break;
159                 default:
160                     break;
161             }
162         }
163         
164         struct UserNode *user;
165         if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
166             user = createTempUser(argv[4]);
167             user->flags |= USERFLAG_ISTMPUSER;
168             chan->flags |= CHANFLAG_HAVE_INVISIBLES;
169             struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
170             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
171         } else {
172             user = getUserByNick(argv[4]);
173             if(user == NULL) {
174                 user = addUser(argv[4]);
175             }
176             if(!isUserOnChan(user, chan)) {
177                 struct ChanUser *chanuser = addChanUser(chan, user);
178                 chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
179             }
180         }
181         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
182         if(!*user->ident)
183             strcpy(user->ident, argv[2]);
184         if(!*user->host)
185             strcpy(user->host, argv[3]);
186         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
187             strcpy(user->auth, argv[6]);
188             user->flags |= USERFLAG_ISAUTHED;
189         }
190     } else if(type & WHOQUEUETYPE_USERAUTH) {
191         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
192         entry->type |= WHOQUEUETYPE_FOUND;
193         if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
194             strcpy(entry->user->auth, argv[5]);
195             entry->user->flags |= USERFLAG_ISAUTHED;
196         }
197         userauth_callback_t *callback = entry->callback;
198         callback(client, entry->user->nick, entry->user, entry->data);
199     }
200 }
201
202 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
203     if(argc < 2) return;
204     char *typestr = strstr(argv[1], ",") + 1;
205     int type = atoi(typestr);
206     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
207     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
208     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
209     if(type & WHOQUEUETYPE_USERLIST) {
210         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
211         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
212         userlist_callback_t *callback = entry->callback;
213         callback(client, entry->chan, entry->data);
214         if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
215             //remove all invisible users again
216             struct ChanUser *chanuser, *next;
217             for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
218                 next = getChannelUsers(entry->chan, chanuser);
219                 if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
220                     delChanUser(chanuser, 1);
221                 }
222             }
223         }
224     } else if(type & WHOQUEUETYPE_USERAUTH) {
225         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
226             userauth_callback_t *callback = entry->callback;
227             callback(client, entry->user->nick, NULL, entry->data);
228         }
229     }
230     free(entry);
231 }
232
233 void free_whoqueue() {
234     struct WHOQueueEntry *entry, *next;
235     for(entry = first_entry; entry; entry = next) {
236         next = entry->next;
237         free(entry);
238     }
239     first_entry = NULL;
240     last_entry = NULL;
241 }