Exemple #1
0
        public static void EventLog(string message, Types.Log type = Types.Log.Other)
        {
            string appendMessage = "[" + DateTime.Now.ToLongTimeString() + "] <" + type + "> " + message;

            // Output to the console
            Console.WriteLine(appendMessage);

            // Output to the StatusLabel and to the debug RichTextBox
            MainDialog.UpdateStatusTextUI(message);
            MainDialog.UpdateDebugTextUI(appendMessage);
            
            _eventList.AddLast(appendMessage);
        }
Exemple #2
0
        private void AddUsersToList(List<string> nickList, Types.UserLevel level = Types.UserLevel.Viewer, Action progressAction = null)
        {
            //Add the userlist to the database, with level
            Db.NewUsers(nickList, level, progressAction);
            lock (Userlist)
            {
                //Convert the list to a dictionary (list value, param level)
                Dictionary<string, Types.UserLevel> newList = nickList
                                .ToDictionary(item => item, value => level);

                //Merge ^ this dictionary to the userlist.
                Userlist = Userlist.Concat(newList)
                    .GroupBy(kvp => kvp.Key, kvp => kvp.Value)
                    .ToDictionary(g => g.Key, g => g.First());

                Program.MainDialog.SafeInvoke(() =>
                {
                    Program.MainDialog.AddUsersToViewlist(newList);
                    Program.MainDialog.mdTabChat.Enabled = true; //TODO: Remove this!
                });
            }
        }
Exemple #3
0
 // This is a positional argument
 public CommandUserLevel(Types.CommandLevel level)
 {
     Level = level;
 }
Exemple #4
0
        private object[] GetParamObject(MethodInfo methodInfo, Types.UserLevel level, string user, string[] paramStrings)
        {
            if (methodInfo == null || !CommandCheckParamsCount(methodInfo, paramStrings.Length + 2)) return null;
            ParameterInfo[] parameters = methodInfo.GetParameters();
            object[] paramObject = new object[parameters.Length];
            paramObject[0] = level;
            paramObject[1] = user;
            int c = -2;
            foreach (var pi in parameters)
            {
                if (c < 0)
                {
                    c++;
                    continue;
                }
                if (paramStrings.Length != 0 && paramStrings.Length > c)
                {
                    var str = paramStrings[c];
                    int i = 0;
                    double d = 0;
                    bool b = true;
                    object res = null;
                    if (pi.ParameterType == i.GetType())
                    {
                        if (Int32.TryParse(str, NumberStyles.None, Program.CurrentCulture, out i)) res = i;
                    }
                    else if (pi.ParameterType == d.GetType())
                    {
                        if (Double.TryParse(str, NumberStyles.Float, Program.CurrentCulture, out d)) res = d;
                    }
                    else if (pi.ParameterType == b.GetType())
                    {
                        if (Boolean.TryParse(str, out b)) 
                            res = b;
                        else if (Int32.TryParse(str, NumberStyles.None, Program.CurrentCulture, out i) && (i == 0 ||
                                 i == 1))
                            res = i == 1;
                    }
                    else
                    {
                        res = str;
                    }

                    if (res == null) return null;

                    paramObject[2 + c++] = res;
                }
                else
                {
                    if (!pi.HasDefaultValue) return null;
                    paramObject[2 + c++] = pi.DefaultValue;
                }
            }
            return paramObject;
        }
Exemple #5
0
 public bool CommandHasLevel(Types.CommandLevel level, MethodInfo methodInfo)
 {
     if (methodInfo == null) return false; //TODO: This shouldn't happen, ever.
     var attributes = Attribute.GetCustomAttributes(methodInfo);
     return attributes.Any(at => at is CommandUserLevel && ((CommandUserLevel)at).Level <= level);
 }
Exemple #6
0
 public Types.CommandLevel GetCommandLevel(Types.UserLevel level)
 {
     return level == Types.UserLevel.Viewer
         ? Types.CommandLevel.Viewer
         : level == Types.UserLevel.Moderator
             ? Types.CommandLevel.Moderator
             : level == Types.UserLevel.Editor
                 ? Types.CommandLevel.Editor
                 : Types.CommandLevel.Broadcaster;
 }
Exemple #7
0
        public void ExecuteCommand(string user, Types.UserLevel userLevel, string commandName, string[] paramStrings)
        {
            Types.CommandLevel level = GetCommandLevel(userLevel);
            bool isAlias;
            commandName = commandName.ToLower();
            bool isOther = true;
            CommandBase cmd = InternalCommands["other"];
            if (CommandGroupExists(commandName, out isAlias))
            {
                cmd = isAlias ? CommandAliases[commandName] : InternalCommands[commandName];
                isOther = false;
            }
            
            var methods = cmd.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);


            if (!isOther || methods.Any(m => m.Name.ToLower() == commandName))
            {
                string subCommandName = isOther ? commandName : "default";
                bool isDefault = true;
                if (!isOther && paramStrings.Length > 0)
                {
                    isDefault = false;
                    subCommandName = paramStrings[0];
                }

                List<MethodInfo> foundMethods = methods.Where(method => method.Name.ToLower() == subCommandName.ToLower()).ToList();

                if (foundMethods.Count == 0)
                {
                    if (!isDefault)
                    {
                        subCommandName = "default";
                        isDefault = true;
                        foundMethods =
                            methods.Where(method => method.Name.ToLower() == subCommandName.ToLower()).ToList();
                    }
                }
                else
                {
                    if (!isDefault)
                    {
                        string[] newParams = new string[paramStrings.Length - 1];
                        for (int i = 1; i < paramStrings.Length; i++)
                        {
                            newParams[i - 1] = paramStrings[i];
                        }
                        paramStrings = newParams;
                    }
                }

                if (foundMethods.Count != 0)
                {
                    bool succes = false;
                    foreach (var methodInfo in foundMethods)
                    {
                        if (CommandHasLevel(level, methodInfo))
                        {
                            object[] paramObject = GetParamObject(methodInfo, userLevel, user, paramStrings);
                            if (paramObject != null && CommandCheckParams(methodInfo, paramObject))
                            {
                                succes = true;
                                methodInfo.Invoke(cmd, paramObject);
                            }
                        }
                    }

                    if (!succes)
                    {
                        if (isOther || !isDefault)
                        {
                            string usage = null;
                            foreach (var method in foundMethods)
                            {
                                if (CommandHasLevel(level, method))
                                {
                                    usage = GetCommandUsage(method);
                                    break;
                                }
                            }
                            if(usage != null)
                                Program.Irc.SendMessage(String.Format(Resources.CommandUsage, usage, user));
                        }
                    }
                }
            }
            else
            {
                commandName = commandName.ToLower();
                if (CustomCommandExists(commandName))
                {
                    if (CustomCommandHasLevel(level, commandName))
                    {
                        Program.Irc.SendMessage(GetCustomCommandResponse(commandName, paramStrings, user));
                    }
                }
            }
        }
Exemple #8
0
 private bool CustomCommandHasLevel(Types.CommandLevel level, string commandName)
 {
     return CustomCommands[commandName].CommandLevel <= level;
 }
Exemple #9
0
        public static void Chat(string user, string message, Types.Chat type = Types.Chat.Regular, bool isLeave = false)
        {
            string lvlColor = @"\cf" + (int)Rtf.Colors[(Irc.UserInList(user) ? Irc.Userlist[user] : Types.UserLevel.Viewer).ToString()][0];
            string typeColor = @"\cf" + (int)Rtf.Colors[type.ToString()][0];
            string appendText;

            if ((type == Types.Chat.Regular && type == Types.Chat.Status || type == Types.Chat.Subscribe))
            {
                if (!isLeave && !Irc.UserInList(user)) Irc.AddUserToList(user);
                else if(isLeave) Irc.RemoveUserFromList(user);
            }
                

            if (Settings.Default.OptionHideBotMsg && string.Equals(user, Settings.Default.OptionName, StringComparison.OrdinalIgnoreCase)) return;
            
            switch (type)
            {
                case Types.Chat.Regular:
                    // Handle commands.
                    string[] wordArray = message.Split('"')
                     .Select((element, index) => index % 2 == 0  // If even index
                                           ? element.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)  // Split the item
                                           : new[] { element })  // Keep the entire item
                     .SelectMany(element => element).ToArray();
                    if (Irc.UserInList(user) && Regex.IsMatch(wordArray[0], @"^(?=^[!][\d]*[A-Z|a-z])|(?=^[!][A-Z|a-z][\d|A-Z|a-z])"))
                    {
                        string[] paramStrings = new string[wordArray.Length-1];
                        for (int i = 1; i < wordArray.Length; i++)
                            paramStrings[i - 1] = wordArray[i];
                        Commands.ExecuteCommand(user, Irc.Userlist[user], wordArray[0].Substring(1), paramStrings);
                    }

                    // Regular chat messages.
                    string userString = "{" + lvlColor + "<" + user + ">} ";
                    Dictionary<string, string> emoteList = Irc != null && Irc.Emotelist != null ? Irc.Emotelist : new Dictionary<string, string>();
                    message = Rtf.EscapeString(message);
                    appendText = typeColor + userString + (emoteList.Aggregate(message, (current, pair) => Regex.IsMatch(message, "(^| )" + pair.Key + "( |$)") ? Regex.Replace(current, "(^| )" + pair.Key + "( |$)", Rtf.GetEmbedImageString(pair.Value.ToString()) ?? pair.Key.ToString()) : current));
                    break;
                case Types.Chat.Status:
                    // "Joined" and "Left" messages.
                    if (Settings.Default.OptionHideStatusMsg) return;
                    appendText = typeColor + user + " " + message.Trim();
                    break;
                case Types.Chat.Subscribe:
                    // "[USER] just subscribed!" messages.
                    appendText = typeColor + message.Substring(message.IndexOf(":", 1) + 1).Trim();
                    break;
                default:
                    // UNKNOWN, report as ERROR event.
                    appendText = null;
                    EventLog("A chat message of an unknown type (type: " + type + ") has been received", Types.Log.Error);
                    break;
            }

            // Output to the console (disabled by default due to muchness)
            // Console.WriteLine(message.Trim());

            // Output to the chat RichTextBox
            MainDialog.UpdateChatTextUI(appendText);
        }
Exemple #10
0
 public CustomCommand(string displayName, string commandResponse, Types.CommandLevel commandLevel)
 {
     this.CommandResponse = commandResponse;
     this.CommandLevel = commandLevel;
     this.DisplayName = displayName;
 }
Exemple #11
0
 public void AddUserToViewlist(string nick, Types.UserLevel level)
 {
     if (!mdTabChatViewerList.Items.ContainsKey(nick))
     {
         ListViewItem li = new ListViewItem(" " + Types.GetUserLevelLetter(level), level.ToString());
         li.SubItems.Add(nick);
         li.Name = nick;
         mdTabChatViewerList.Items.Add(li);
     }
 }