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