Exemple #1
0
        public static void LoadUsers()
        {
            // This is not a strict INI file, so the IniFile class cannot be used here.
            Bot.commandCallbackNeeded = false;
            Bot.Accounts.Clear();
            if (File.Exists("CBotUsers.ini"))
            {
                try {
                    using (var Reader = new StreamReader("CBotUsers.ini")) {
                        string  Section = null;
                        Account newUser = null;

                        while (!Reader.EndOfStream)
                        {
                            string s = Reader.ReadLine();
                            if (Regex.IsMatch(s, @"^(?>\s*);"))
                            {
                                continue;                                                              // Comment check
                            }
                            Match Match = Regex.Match(s, @"^\s*\[(?<Section>.*?)\]?\s*$");
                            if (Match.Success)
                            {
                                if (Section != null)
                                {
                                    Bot.Accounts.Add(Section, newUser);
                                }
                                Section = Match.Groups["Section"].Value;
                                if (!Bot.Accounts.ContainsKey(Section))
                                {
                                    newUser = new Account {
                                        Permissions = new string[0]
                                    };
                                    if (Section.StartsWith("$a"))
                                    {
                                        Bot.commandCallbackNeeded = true;
                                    }
                                }
                            }
                            else
                            {
                                if (Section != null)
                                {
                                    Match = Regex.Match(s, @"^\s*((?>[^=]*))=(.*)$");
                                    if (Match.Success)
                                    {
                                        string Field = Match.Groups[1].Value;
                                        string Value = Match.Groups[2].Value;
                                        switch (Field.ToUpper())
                                        {
                                        case "PASSWORD":
                                        case "PASS":
                                            newUser.Password = Value;
                                            if (newUser.HashType == HashType.None && newUser.Password != null)
                                            {
                                                // Old format
                                                newUser.HashType = (newUser.Password.Length == 128 ? HashType.SHA256Salted : HashType.PlainText);
                                            }
                                            break;

                                        case "HASHTYPE":
                                            newUser.HashType = (HashType)Enum.Parse(typeof(HashType), Value, true);
                                            break;
                                        }
                                    }
                                    else if (s.Trim() != "")
                                    {
                                        string[] array = newUser.Permissions;
                                        newUser.Permissions = new string[array.Length + 1];
                                        Array.Copy(array, newUser.Permissions, array.Length);
                                        newUser.Permissions[array.Length] = s.Trim();
                                    }
                                }
                            }
                        }

                        Bot.Accounts.Add(Section, newUser);
                    }
                } catch (Exception ex) {
                    ConsoleUtils.WriteLine("%cGRAY[%cREDERROR%cGRAY] %cWHITEI was unable to retrieve user data from the file: $k04" + ex.Message + "%r");
                }
            }
        }
Exemple #2
0
 public static void Write(string text)
 {
     lock (writeLock)
         ConsoleUtils.WriteSub(text, Console.ForegroundColor, Console.BackgroundColor);
 }
Exemple #3
0
 public static void Write(string format, params object[] args)
 {
     lock (writeLock)
         ConsoleUtils.WriteSub(format, args);
 }
Exemple #4
0
        private static void WriteSub(string format, params object[] args)
        {
            int          i = 0; int open = -1; int index = -1; int start = 0;
            ConsoleColor originalForeground = Console.ForegroundColor;
            ConsoleColor originalBackground = Console.BackgroundColor;

            while (i < format.Length)
            {
                char c = format[i];

                if (c == '}')
                {
                    if (i != format.Length - 1 && format[i + 1] == '}')
                    {
                        if (index == -1)
                        {
                            if (i != start)
                            {
                                ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
                            }
                            Console.Write("}");
                            start = i + 2;
                        }
                        ++i;
                    }
                    else if (index < 0)
                    {
                        throw new FormatException();
                    }
                    else
                    {
                        if (open == -1)
                        {
                            Console.Write((args[index] ?? "").ToString());
                        }
                        else
                        {
                            Console.Write("{0" + format.Substring(open, i - open) + "}", args[index]);
                        }
                        open  = -1;
                        index = -1;
                        start = i + 1;
                    }
                }
                else if (open == -1)
                {
                    if (index == -2)
                    {
                        if (c < '0' || c > '9')
                        {
                            throw new FormatException();
                        }
                        index = (int)(c - '0');
                    }
                    else if (index != -1)
                    {
                        if (c != ' ')
                        {
                            if (c == ',' || c == ':')
                            {
                                open = i;
                            }
                            else
                            {
                                if (c < '0' || c > '9')
                                {
                                    throw new FormatException();
                                }
                                index = index * 10 + (int)(c - '0');
                            }
                        }
                    }
                    else if (c == '{')
                    {
                        if (i != format.Length - 1 && format[i + 1] == '{')
                        {
                            if (index == -1)
                            {
                                if (i != start)
                                {
                                    ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
                                }
                                Console.Write("{");
                                start = i + 2;
                            }
                            ++i;
                        }
                        else if (open == -1)
                        {
                            if (i != start)
                            {
                                ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
                            }
                            index = -2;
                        }
                        else
                        {
                            throw new FormatException();
                        }
                    }
                }
                ++i;
            }
            if (index != -1)
            {
                throw new FormatException();
            }
            if (i != start)
            {
                ConsoleUtils.WriteSub(format.Substring(start, i - start), originalForeground, originalBackground);
            }
        }