Example #1
0
        /// <summary>
        /// Sends an extended WHO query asking for specific information about a single user
        /// or the users in a channel, and runs a callback when we have received the response.
        /// </summary>
        public void Who(string target, WhoxFlag flags, WhoxField fields, Action <List <ExtendedWho> > callback)
        {
            if (ServerInfo.ExtendedWho)
            {
                var whox = new List <ExtendedWho>();

                // Generate random querytype for WHO query
                int queryType = RandomNumber.Next(0, 999);

                // Add the querytype field if it wasn't defined
                var _fields = fields;
                if ((fields & WhoxField.QueryType) == 0)
                {
                    _fields |= WhoxField.QueryType;
                }

                string whoQuery = string.Format("WHO {0} {1}%{2},{3}", target, flags.AsString(), _fields.AsString(), queryType);
                string queryKey = string.Format("WHO {0} {1} {2:D}", target, queryType, _fields);

                RequestManager.QueueOperation(queryKey, new RequestOperation(whox, ro =>
                {
                    callback?.Invoke((List <ExtendedWho>)ro.State);
                }));
                SendRawMessage(whoQuery);
            }
        }
Example #2
0
        public static string AsString(this WhoxField field)
        {
            // cdfhilnrstuao
            var result = string.Empty;

            if ((field & WhoxField.Channel) != 0)
            {
                result += 'c';
            }
            if ((field & WhoxField.Hops) != 0)
            {
                result += 'd';
            }
            if ((field & WhoxField.Flags) != 0)
            {
                result += 'f';
            }
            if ((field & WhoxField.Hostname) != 0)
            {
                result += 'h';
            }
            if ((field & WhoxField.UserIp) != 0)
            {
                result += 'i';
            }
            if ((field & WhoxField.TimeIdle) != 0)
            {
                result += 'l';
            }
            if ((field & WhoxField.Nick) != 0)
            {
                result += 'n';
            }
            if ((field & WhoxField.RealName) != 0)
            {
                result += 'r';
            }
            if ((field & WhoxField.ServerName) != 0)
            {
                result += 's';
            }
            if ((field & WhoxField.QueryType) != 0)
            {
                result += 't';
            }
            if ((field & WhoxField.Username) != 0)
            {
                result += 'u';
            }
            if ((field & WhoxField.AccountName) != 0)
            {
                result += 'a';
            }
            if ((field & WhoxField.OpLevel) != 0)
            {
                result += 'o';
            }

            if (field == WhoxField.None)
            {
                result = string.Empty;
            }

            return(result);
        }
        public static void HandleWhox(IrcClient client, IrcMessage message)
        {
            int msgQueryType = int.Parse(message.Parameters[1]);
            var whoxQuery    = new KeyValuePair <string, RequestOperation>();

            foreach (var query in client.RequestManager.PendingOperations.Where(kvp => kvp.Key.StartsWith("WHO ")))
            {
                // Parse query to retrieve querytype
                string   key        = query.Key;
                string[] queryParts = key.Split(new[] { ' ' });

                int queryType = int.Parse(queryParts[2]);

                // Check querytype against message querytype
                if (queryType == msgQueryType)
                {
                    whoxQuery = query;
                }
            }

            if (whoxQuery.Key != string.Empty && whoxQuery.Value != null)
            {
                var whoxList = (List <ExtendedWho>)client.RequestManager.PeekOperation(whoxQuery.Key).State;
                var whox     = new ExtendedWho();

                string   key        = whoxQuery.Key;
                string[] queryParts = key.Split(new[] { ' ' });

                // Handle what fields were included in the WHOX request
                WhoxField fields   = (WhoxField)int.Parse(queryParts[3]);
                int       fieldIdx = 1;
                do
                {
                    if ((fields & WhoxField.QueryType) != 0)
                    {
                        whox.QueryType = msgQueryType;
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.Channel) != 0)
                    {
                        whox.Channel = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.Username) != 0)
                    {
                        whox.User.User = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.UserIp) != 0)
                    {
                        whox.IP = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.Hostname) != 0)
                    {
                        whox.User.Hostname = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.ServerName) != 0)
                    {
                        whox.Server = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.Nick) != 0)
                    {
                        whox.User.Nick = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.Flags) != 0)
                    {
                        whox.Flags = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.Hops) != 0)
                    {
                        whox.Hops = int.Parse(message.Parameters[fieldIdx]);
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.TimeIdle) != 0)
                    {
                        whox.TimeIdle = int.Parse(message.Parameters[fieldIdx]);
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.AccountName) != 0)
                    {
                        whox.User.Account = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.OpLevel) != 0)
                    {
                        whox.OpLevel = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }

                    if ((fields & WhoxField.RealName) != 0)
                    {
                        whox.User.RealName = message.Parameters[fieldIdx];
                        fieldIdx++;
                    }
                }while (fieldIdx < message.Parameters.Length - 1);
                whoxList.Add(whox);
            }
        }