fixed last commit
[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
12 struct WHOQueueEntry {
13     char type;
14     struct ClientSocket *client;
15     struct ChanNode *chan;
16     struct UserNode *user;
17     struct WHOQueueEntry *next;
18     void *callback;
19     void *data;
20 };
21
22 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
23
24 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
25     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
26     if (!entry)
27     {
28         perror("malloc() failed");
29         return NULL;
30     }
31     entry->next = NULL;
32     entry->client = client;
33     if(last_entry)
34         last_entry->next = entry;
35     if(!first_entry)
36         first_entry = entry;
37     return entry;
38 }
39
40 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
41     if(!first_entry) return NULL;
42     struct WHOQueueEntry *entry;
43     for(entry = first_entry; entry; entry = entry->next) {
44         if(entry->client == client)
45             break;
46     }
47     if(entry == NULL) return NULL;
48     if(freeEntry) {
49         first_entry = first_entry->next;
50         if(last_entry == first_entry)
51             last_entry = NULL;
52     }
53     return entry;
54 }
55
56 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
57     struct ClientSocket *bot;
58     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
59         if(isUserOnChan(bot->user, chan))
60             break;
61     }
62     if(bot == NULL) return;
63     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
64     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
65     entry->chan = chan;
66     entry->callback = callback;
67     entry->data = data;
68     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
69     putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
70 }
71
72 void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
73     struct ClientSocket *bot;
74     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
75         if(bot->flags & SOCKET_FLAG_PREFERRED)
76             break;
77     }
78     if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
79     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
80     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
81     entry->user = user;
82     entry->callback = callback;
83     entry->data = data;
84     //WHO ".$user->getNick().",".$id." %tuhna,".$id
85     putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
86 }
87
88 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
89     int i;
90     if(argc < 2) return;
91     int type = atoi(argv[1]);
92     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
93     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
94     if(entry == NULL || entry->type != type) return;
95     if(type & WHOQUEUETYPE_USERLIST) {
96         if(argc < 7) return;
97         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
98         struct ChanNode *chan = entry->chan;
99         //add the user toe the channel if he isn't added, yet and update its user data
100         struct UserNode *user = getUserByNick(argv[4]);
101         if(user == NULL) {
102             user = addUser(argv[4]);
103         }
104         //parse flags
105         int userflags = 0;
106         int chanuserflags = 0;
107         for(i = 0; i < strlen(argv[5]); i++) {
108             switch (argv[5][i]) {
109                 case '@':
110                     chanuserflags |= CHANUSERFLAG_OPPED;
111                     break;
112                 case '+':
113                     chanuserflags |= CHANUSERFLAG_VOICED;
114                     break;
115                 case '*':
116                     userflags |= USERFLAG_ISIRCOP;
117                     break;
118                 default:
119                     break;
120             }
121         }
122         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
123         if(!isUserOnChan(user, chan)) {
124             struct ChanUser *chanuser = addChanUser(chan, user);
125             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
126         }
127         if(!*user->ident)
128             strcpy(user->ident, argv[2]);
129         if(!*user->host)
130             strcpy(user->host, argv[3]);
131         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
132             strcpy(user->auth, argv[6]);
133             user->flags |= USERFLAG_ISAUTHED;
134         }
135     } else if(type & WHOQUEUETYPE_USERAUTH) {
136         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
137         if(!strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
138             strcpy(entry->user->auth, argv[5]);
139             entry->user->flags |= USERFLAG_ISAUTHED;
140         }
141         userauth_callback_t *callback = entry->callback;
142         callback(client, entry->user, entry->data);
143     }
144 }
145
146 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
147     if(argc < 2) return;
148     char *typestr = strstr(argv[1], ",") + 1;
149     int type = atoi(typestr);
150     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
151     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
152     if(entry == NULL || entry->type != type) return;
153     if(type & WHOQUEUETYPE_USERLIST) {
154         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
155         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
156         userlist_callback_t *callback = entry->callback;
157         callback(client, entry->chan, entry->data);
158     }
159     free(entry);
160 }
161
162 void free_whoqueue() {
163     struct WHOQueueEntry *entry, *next;
164     for(entry = first_entry; entry; entry = next) {
165         next = entry->next;
166         free(entry);
167     }
168     first_entry = NULL;
169     last_entry = NULL;
170 }