added some code & compiler information to cmd_netinfo
[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_userauth(struct UserNode *user, userauth_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(bot->flags & SOCKET_FLAG_PREFERRED)
85             break;
86     }
87     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
88     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
89     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
90     entry->user = user;
91     entry->callback = callback;
92     entry->data = data;
93     //WHO ".$user->getNick().",".$id." %tuhna,".$id
94     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
95 }
96
97 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
98     int i;
99     if(argc < 2) return;
100     int type = atoi(argv[1]);
101     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
102     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
103     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
104     if(type & WHOQUEUETYPE_USERLIST) {
105         if(argc < 7) return;
106         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
107         struct ChanNode *chan = entry->chan;
108         //add the user toe the channel if he isn't added, yet and update its user data
109         struct UserNode *user = getUserByNick(argv[4]);
110         if(user == NULL) {
111             user = addUser(argv[4]);
112         }
113         //parse flags
114         int userflags = 0;
115         int chanuserflags = 0;
116         for(i = 0; i < strlen(argv[5]); i++) {
117             switch (argv[5][i]) {
118                 case '@':
119                     chanuserflags |= CHANUSERFLAG_OPPED;
120                     break;
121                 case '+':
122                     chanuserflags |= CHANUSERFLAG_VOICED;
123                     break;
124                 case '*':
125                     userflags |= USERFLAG_ISIRCOP;
126                     break;
127                 default:
128                     break;
129             }
130         }
131         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
132         if(!isUserOnChan(user, chan)) {
133             struct ChanUser *chanuser = addChanUser(chan, user);
134             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
135         }
136         if(!*user->ident)
137             strcpy(user->ident, argv[2]);
138         if(!*user->host)
139             strcpy(user->host, argv[3]);
140         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
141             strcpy(user->auth, argv[6]);
142             user->flags |= USERFLAG_ISAUTHED;
143         }
144     } else if(type & WHOQUEUETYPE_USERAUTH) {
145         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
146         entry->type |= WHOQUEUETYPE_FOUND;
147         if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
148             strcpy(entry->user->auth, argv[5]);
149             entry->user->flags |= USERFLAG_ISAUTHED;
150         }
151         userauth_callback_t *callback = entry->callback;
152         callback(client, entry->user, entry->data);
153     }
154 }
155
156 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
157     if(argc < 2) return;
158     char *typestr = strstr(argv[1], ",") + 1;
159     int type = atoi(typestr);
160     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
161     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
162     if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
163     if(type & WHOQUEUETYPE_USERLIST) {
164         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
165         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
166         userlist_callback_t *callback = entry->callback;
167         callback(client, entry->chan, entry->data);
168     } else if(type & WHOQUEUETYPE_USERAUTH) {
169         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
170             userauth_callback_t *callback = entry->callback;
171             callback(client, NULL, entry->data);
172         }
173     }
174     free(entry);
175 }
176
177 void free_whoqueue() {
178     struct WHOQueueEntry *entry, *next;
179     for(entry = first_entry; entry; entry = next) {
180         next = entry->next;
181         free(entry);
182     }
183     first_entry = NULL;
184     last_entry = NULL;
185 }