fixed missing return
[NeonServV5.git] / WHOHandler.c
1
2 #include "WHOHandler.h"
3 #include "ChanNode.h"
4 #include "UserNode.h"
5
6 #define WHOQUEUETYPE_ISONQUEUE 0x01
7 #define WHOQUEUETYPE_USERLIST  0x02
8
9 struct WHOQueueEntry {
10     char type;
11     struct ClientSocket *client;
12     struct ChanNode *chan;
13     struct UserNode *user;
14     struct WHOQueueEntry *next;
15     userlist_callback_t *callback;
16 };
17
18 static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
19
20 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
21     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
22     if (!entry)
23     {
24         perror("malloc() failed");
25         return NULL;
26     }
27     entry->next = NULL;
28     entry->client = client;
29     if(last_entry)
30         last_entry->next = entry;
31     if(!first_entry)
32         first_entry = entry;
33     return entry;
34 }
35
36 static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
37     if(!first_entry) return NULL;
38     struct WHOQueueEntry *entry;
39     for(entry = first_entry; entry; entry = entry->next) {
40         if(entry->client == client)
41             break;
42     }
43     if(entry == NULL) return NULL;
44     if(freeEntry) {
45         first_entry = first_entry->next;
46         if(last_entry == first_entry)
47             last_entry = NULL;
48     }
49     return entry;
50 }
51
52 void get_userlist(struct ChanNode *chan, userlist_callback_t callback) {
53     struct ClientSocket *bot;
54     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
55         if(isUserOnChan(bot->user, chan))
56             break;
57     }
58     if(bot == NULL) return;
59     struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
60     entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
61     entry->chan = chan;
62     entry->callback = callback;
63     //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
64     putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
65 }
66
67 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
68     int i;
69     if(argc < 2) return;
70     int type = atoi(argv[1]);
71     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
72     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
73     if(entry == NULL || entry->type != type) return;
74     if(type & WHOQUEUETYPE_USERLIST) {
75         if(argc < 7) return;
76         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
77         struct ChanNode *chan = entry->chan;
78         //add the user toe the channel if he isn't added, yet and update its user data
79         struct UserNode *user = getUserByNick(argv[4]);
80         if(user == NULL) {
81             user = addUser(argv[4]);
82         }
83         //parse flags
84         int userflags = 0;
85         int chanuserflags = 0;
86         for(i = 0; i < strlen(argv[5]); i++) {
87             switch (argv[5][i]) {
88                 case '@':
89                     chanuserflags |= CHANUSERFLAG_OPPED;
90                     break;
91                 case '+':
92                     chanuserflags |= CHANUSERFLAG_VOICED;
93                     break;
94                 case '*':
95                     userflags |= USERFLAG_ISIRCOP;
96                     break;
97                 default:
98                     break;
99             }
100         }
101         user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
102         if(!isUserOnChan(user, chan)) {
103             struct ChanUser *chanuser = addChanUser(chan, user);
104             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
105         }
106         if(!*user->ident)
107             strcpy(user->ident, argv[2]);
108         if(!*user->host)
109             strcpy(user->host, argv[3]);
110         if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
111             strcpy(user->auth, argv[6]);
112             user->flags |= USERFLAG_ISAUTHED;
113         }
114     }
115 }
116
117 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
118     if(argc < 2) return;
119     char *typestr = strstr(argv[1], ",") + 1;
120     int type = atoi(typestr);
121     if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
122     struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
123     if(entry == NULL || entry->type != type) return;
124     if(type & WHOQUEUETYPE_USERLIST) {
125         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
126         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
127         entry->callback(client, entry->chan);
128     }
129     free(entry);
130 }