|
| 1 | +// TS3AudioBot - An advanced Musicbot for Teamspeak 3 |
| 2 | +// Copyright (C) 2016 TS3AudioBot contributors |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace TS3AudioBot.Rights |
| 18 | +{ |
| 19 | + using Nett; |
| 20 | + using System.Collections.Generic; |
| 21 | + using System.Linq; |
| 22 | + |
| 23 | + internal abstract class RightsDecl |
| 24 | + { |
| 25 | + public int Id { get; private set; } |
| 26 | + public int Level { get; set; } |
| 27 | + |
| 28 | + public RightsRule Parent { get; set; } |
| 29 | + private string[] includeNames; |
| 30 | + public RightsGroup[] Includes { get; set; } |
| 31 | + |
| 32 | + public string[] DeclAdd { get; set; } |
| 33 | + public string[] DeclDeny { get; set; } |
| 34 | + |
| 35 | + public RightsDecl() { } |
| 36 | + |
| 37 | + public virtual void FillNull() |
| 38 | + { |
| 39 | + if (includeNames == null) includeNames = new string[0]; |
| 40 | + if (DeclAdd == null) DeclAdd = new string[0]; |
| 41 | + if (DeclDeny == null) DeclDeny = new string[0]; |
| 42 | + } |
| 43 | + |
| 44 | + public virtual bool ParseKey(string key, TomlObject tomlObj, List<RightsDecl> rules) |
| 45 | + { |
| 46 | + switch (key) |
| 47 | + { |
| 48 | + case "+": |
| 49 | + DeclAdd = TomlTools.GetValues<string>(tomlObj); |
| 50 | + if (DeclAdd == null) |
| 51 | + Log.Write(Log.Level.Error, "<+> Field has invalid data."); |
| 52 | + break; |
| 53 | + case "-": |
| 54 | + DeclDeny = TomlTools.GetValues<string>(tomlObj); |
| 55 | + if (DeclDeny == null) |
| 56 | + Log.Write(Log.Level.Error, "<-> Field has invalid data."); |
| 57 | + break; |
| 58 | + case "include": |
| 59 | + includeNames = TomlTools.GetValues<string>(tomlObj); |
| 60 | + if (includeNames == null) |
| 61 | + Log.Write(Log.Level.Error, "<include> Field has invalid data."); |
| 62 | + break; |
| 63 | + default: return false; |
| 64 | + } |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + public void ParseChilden(TomlTable tomlObj, List<RightsDecl> rules) |
| 69 | + { |
| 70 | + Id = rules.Count; |
| 71 | + rules.Add(this); |
| 72 | + |
| 73 | + foreach (var item in tomlObj) |
| 74 | + { |
| 75 | + if (!ParseKey(item.Key, item.Value, rules)) |
| 76 | + { |
| 77 | + Log.Write(Log.Level.Error, "Unrecognized key <{0}>.", item.Key); |
| 78 | + } |
| 79 | + } |
| 80 | + FillNull(); |
| 81 | + } |
| 82 | + |
| 83 | + public abstract RightsGroup ResolveGroup(string groupName); |
| 84 | + |
| 85 | + public bool ResolveIncludes() |
| 86 | + { |
| 87 | + bool hasErrors = false; |
| 88 | + if (includeNames != null) |
| 89 | + { |
| 90 | + Includes = includeNames.Select(ResolveGroup).ToArray(); |
| 91 | + for (int i = 0; i < includeNames.Length; i++) |
| 92 | + if (Includes[i] == null) |
| 93 | + { |
| 94 | + Log.Write(Log.Level.Error, "Could not find group \"{0}\" to include.", includeNames[i]); |
| 95 | + hasErrors = true; |
| 96 | + } |
| 97 | + includeNames = null; |
| 98 | + } |
| 99 | + return !hasErrors; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments