~sircmpwn/ChatSharp

64dbcddf439a6d9db22279dd63bb77f36367988e — Drew DeVault 4 years ago a1329cf + 85e0923
Merge pull request #93 from RockyTV/fixes

Add handler for RPL_WHOREPLY (352)
M ChatSharp/Handlers/MessageHandlers.cs => ChatSharp/Handlers/MessageHandlers.cs +1 -0
@@ 46,6 46,7 @@ namespace ChatSharp.Handlers
            client.SetHandler("318", UserHandlers.HandleWhoIsEnd);
            client.SetHandler("319", UserHandlers.HandleWhoIsChannels);
            client.SetHandler("330", UserHandlers.HandleWhoIsLoggedInAs);
            client.SetHandler("352", UserHandlers.HandleWho);
            client.SetHandler("354", UserHandlers.HandleWhox);
            client.SetHandler("ACCOUNT", UserHandlers.HandleAccount);
            client.SetHandler("CHGHOST", UserHandlers.HandleChangeHost);

M ChatSharp/Handlers/UserHandlers.cs => ChatSharp/Handlers/UserHandlers.cs +56 -8
@@ 72,6 72,38 @@ namespace ChatSharp.Handlers
            client.OnWhoIsReceived(new Events.WhoIsReceivedEventArgs(whois));
        }

        public static void HandleWho(IrcClient client, IrcMessage message)
        {
            // A standard WHO request (numeric 352) is just like a WHOX request, except that it has less fields.
            foreach (var query in client.RequestManager.PendingOperations.Where(kvp => kvp.Key.StartsWith("WHO ")))
            {
                if (query.Key != string.Empty && query.Value != null)
                {
                    var whoList = (List<ExtendedWho>)client.RequestManager.PeekOperation(query.Key).State;
                    var who = new ExtendedWho();

                    who.Channel = message.Parameters[1];
                    who.User.User = message.Parameters[2];
                    who.IP = message.Parameters[3];
                    who.Server = message.Parameters[4];
                    who.User.Nick = message.Parameters[5];
                    who.Flags = message.Parameters[6];


                    var supposedRealName = message.Parameters[7];

                    // Parsing IRC spec craziness: the hops field is included in the real name field
                    var hops = supposedRealName.Substring(0, supposedRealName.IndexOf(" "));
                    who.Hops = int.Parse(hops);

                    var realName = supposedRealName.Substring(supposedRealName.IndexOf(" ") + 1);
                    who.User.RealName = realName;

                    whoList.Add(who);
                }
            }
        }

        public static void HandleWhox(IrcClient client, IrcMessage message)
        {
            int msgQueryType = int.Parse(message.Parameters[1]);


@@ 187,16 219,32 @@ namespace ChatSharp.Handlers

        public static void HandleWhoEnd(IrcClient client, IrcMessage message)
        {
            var query = client.RequestManager.PendingOperations.Where(kvp => kvp.Key.StartsWith("WHO " + message.Parameters[1])).FirstOrDefault();
            var request = client.RequestManager.DequeueOperation(query.Key);
            var whoxList = (List<ExtendedWho>)request.State;
            if (client.ServerInfo.ExtendedWho)
            {
                var query = client.RequestManager.PendingOperations.Where(kvp => kvp.Key.StartsWith("WHO " + message.Parameters[1])).FirstOrDefault();
                var request = client.RequestManager.DequeueOperation(query.Key);
                var whoxList = (List<ExtendedWho>)request.State;

            foreach (var whox in whoxList)
                if (!client.Users.Contains(whox.User.Nick))
                    client.Users.Add(whox.User);
                foreach (var whox in whoxList)
                    if (!client.Users.Contains(whox.User.Nick))
                        client.Users.Add(whox.User);

            request.Callback?.Invoke(request);
            client.OnWhoxReceived(new Events.WhoxReceivedEventArgs(whoxList.ToArray()));
                request.Callback?.Invoke(request);
                client.OnWhoxReceived(new Events.WhoxReceivedEventArgs(whoxList.ToArray()));
            }
            else
            {
                var query = client.RequestManager.PendingOperations.Where(kvp => kvp.Key == "WHO " + message.Parameters[1]).FirstOrDefault();
                var request = client.RequestManager.DequeueOperation(query.Key);
                var whoList = (List<ExtendedWho>)request.State;

                foreach (var who in whoList)
                    if (!client.Users.Contains(who.User.Nick))
                        client.Users.Add(who.User);

                request.Callback?.Invoke(request);
                client.OnWhoxReceived(new Events.WhoxReceivedEventArgs(whoList.ToArray()));
            }
        }

        public static void HandleAccount(IrcClient client, IrcMessage message)

M ChatSharp/IrcClient.Commands.cs => ChatSharp/IrcClient.Commands.cs +12 -0
@@ 194,6 194,18 @@ namespace ChatSharp
                }));
                SendRawMessage(whoQuery);
            }
            else
            {
                var whox = new List<ExtendedWho>();

                string whoQuery = string.Format("WHO {0}", target);

                RequestManager.QueueOperation(whoQuery, new RequestOperation(whox, ro =>
                {
                    callback?.Invoke((List<ExtendedWho>)ro.State);
                }));
                SendRawMessage(whoQuery);
            }
        }

        /// <summary>