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