changed Makefile; build all commands as an own file
[NeonServV5.git] / WHOHandler.c
index 1aeca95375cbad8f19e1a8e3edfcf28e8e5fe0cd..f924e463f7bf1c44eb826a7133f9669e932751bd 100644 (file)
 
 #include "WHOHandler.h"
+#include "ChanNode.h"
+#include "UserNode.h"
+#include "ChanUser.h"
+#include "ClientSocket.h"
 
-void get_userlist(struct ChanNode *chan, userlist_callback_t callback) {
-    
+#define WHOQUEUETYPE_ISONQUEUE 0x01
+#define WHOQUEUETYPE_USERLIST  0x02
+#define WHOQUEUETYPE_USERAUTH  0x04
+#define WHOQUEUETYPE_CHECKTYPE 0x07
+#define WHOQUEUETYPE_FOUND     0x08
+
+struct WHOQueueEntry {
+    char type;
+    struct ClientSocket *client;
+    struct ChanNode *chan;
+    struct UserNode *user;
+    struct WHOQueueEntry *next;
+    void *callback;
+    void *data;
+};
+
+static struct WHOQueueEntry *first_entry = NULL, *last_entry = NULL;
+
+static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
+    struct WHOQueueEntry *entry = malloc(sizeof(*entry));
+    if (!entry)
+    {
+        perror("malloc() failed");
+        return NULL;
+    }
+    entry->next = NULL;
+    entry->client = client;
+    if(last_entry) {
+        last_entry->next = entry;
+        last_entry = entry;
+    } else {
+        last_entry = entry;
+        first_entry = entry;
+    }
+    return entry;
+}
+
+static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
+    if(!first_entry) return NULL;
+    struct WHOQueueEntry *entry;
+    for(entry = first_entry; entry; entry = entry->next) {
+        if(entry->client == client)
+            break;
+    }
+    if(entry == NULL) return NULL;
+    if(freeEntry) {
+        if(entry == first_entry)
+            first_entry = entry->next;
+        if(entry == last_entry) {
+            struct WHOQueueEntry *last;
+            for(last = first_entry; last; last = last->next)
+                if(last->next == NULL) break;
+            last_entry = last;
+        }
+    }
+    return entry;
+}
+
+void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
+    struct ClientSocket *bot;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        if(isUserOnChan(bot->user, chan))
+            break;
+    }
+    if(bot == NULL) return;
+    struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
+    entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
+    entry->chan = chan;
+    entry->callback = callback;
+    entry->data = data;
+    //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
+    putsock(bot, "WHO %s,%d %%tuhnaf,%d", chan->name, entry->type, entry->type);
+}
+
+void get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t callback, void *data) {
+    struct ClientSocket *bot;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        if(isUserOnChan(bot->user, chan))
+            break;
+    }
+    if(bot == NULL) return;
+    struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
+    entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERLIST;
+    entry->chan = chan;
+    entry->callback = callback;
+    entry->data = data;
+    //WHO ".$channel->getName().",".$id." d%tuhnaf,".$id
+    putsock(bot, "WHO %s,%d d%%tuhnaf,%d", chan->name, entry->type, entry->type);
+}
+
+void get_userauth(struct UserNode *user, userauth_callback_t callback, void *data) {
+    struct ClientSocket *bot;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        if(bot->flags & SOCKET_FLAG_PREFERRED)
+            break;
+    }
+    if(bot == NULL) bot = getBots(SOCKET_FLAG_READY, NULL);
+    struct WHOQueueEntry* entry = addWHOQueueEntry(bot);
+    entry->type = WHOQUEUETYPE_ISONQUEUE | WHOQUEUETYPE_USERAUTH;
+    entry->user = user;
+    entry->callback = callback;
+    entry->data = data;
+    //WHO ".$user->getNick().",".$id." %tuhna,".$id
+    putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
 }
 
 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
-    
+    int i;
+    if(argc < 2) return;
+    int type = atoi(argv[1]);
+    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
+    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
+    if(type & WHOQUEUETYPE_USERLIST) {
+        if(argc < 7) return;
+        //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
+        struct ChanNode *chan = entry->chan;
+        //add the user toe the channel if he isn't added, yet and update its user data
+        //parse flags
+        int userflags = 0;
+        int chanuserflags = 0;
+        for(i = 0; i < strlen(argv[5]); i++) {
+            switch (argv[5][i]) {
+                case '@':
+                    chanuserflags |= CHANUSERFLAG_OPPED;
+                    break;
+                case '+':
+                    chanuserflags |= CHANUSERFLAG_VOICED;
+                    break;
+                case '*':
+                    userflags |= USERFLAG_ISIRCOP;
+                    break;
+                case '<':
+                    chanuserflags |= CHANUSERFLAG_INVISIBLE;
+                    break;
+                default:
+                    break;
+            }
+        }
+        
+        struct UserNode *user;
+        if(chanuserflags & CHANUSERFLAG_INVISIBLE) {
+            user = createTempUser(argv[4]);
+            user->flags |= USERFLAG_ISTMPUSER;
+            chan->flags |= CHANFLAG_HAVE_INVISIBLES;
+            struct ChanUser *chanuser = addInvisibleChanUser(chan, user);
+            chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
+        } else {
+            user = getUserByNick(argv[4]);
+            if(user == NULL) {
+                user = addUser(argv[4]);
+            }
+            if(!isUserOnChan(user, chan)) {
+                struct ChanUser *chanuser = addChanUser(chan, user);
+                chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
+            }
+        }
+        user->flags = (user->flags & ~USERFLAG_ISIRCOP) | userflags;
+        if(!*user->ident)
+            strcpy(user->ident, argv[2]);
+        if(!*user->host)
+            strcpy(user->host, argv[3]);
+        if(!(user->flags & USERFLAG_ISAUTHED) && strcmp(argv[6], "0")) {
+            strcpy(user->auth, argv[6]);
+            user->flags |= USERFLAG_ISAUTHED;
+        }
+    } else if(type & WHOQUEUETYPE_USERAUTH) {
+        //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
+        entry->type |= WHOQUEUETYPE_FOUND;
+        if(strcmp(argv[5], "0") && !(entry->user->flags & USERFLAG_ISAUTHED)) {
+            strcpy(entry->user->auth, argv[5]);
+            entry->user->flags |= USERFLAG_ISAUTHED;
+        }
+        userauth_callback_t *callback = entry->callback;
+        callback(client, entry->user->nick, entry->user, entry->data);
+    }
 }
 
 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
-    
+    if(argc < 2) return;
+    char *typestr = strstr(argv[1], ",") + 1;
+    int type = atoi(typestr);
+    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
+    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
+    if(type & WHOQUEUETYPE_USERLIST) {
+        //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
+        entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
+        userlist_callback_t *callback = entry->callback;
+        callback(client, entry->chan, entry->data);
+        if(entry->chan->flags & CHANFLAG_HAVE_INVISIBLES) {
+            //remove all invisible users again
+            struct ChanUser *chanuser, *next;
+            for(chanuser = getChannelUsers(entry->chan, NULL); chanuser; chanuser = next) {
+                next = getChannelUsers(entry->chan, chanuser);
+                if(chanuser->flags & CHANUSERFLAG_INVISIBLE) {
+                    delChanUser(chanuser, 1);
+                }
+            }
+        }
+    } else if(type & WHOQUEUETYPE_USERAUTH) {
+        if(!(entry->type & WHOQUEUETYPE_FOUND)) {
+            userauth_callback_t *callback = entry->callback;
+            callback(client, entry->user->nick, NULL, entry->data);
+        }
+    }
+    free(entry);
+}
+
+void free_whoqueue() {
+    struct WHOQueueEntry *entry, *next;
+    for(entry = first_entry; entry; entry = next) {
+        next = entry->next;
+        free(entry);
+    }
+    first_entry = NULL;
+    last_entry = NULL;
 }