Initial split of the modules

This commit is contained in:
Master Kwoth
2017-09-30 00:46:33 +02:00
parent cdc2c43913
commit 599245b1ca
499 changed files with 469 additions and 256 deletions

View File

@ -0,0 +1,18 @@
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Permissions.Common
{
public class OldPermissionCache
{
public string PermRole { get; set; }
public bool Verbose { get; set; } = true;
public Permission RootPermission { get; set; }
}
public class PermissionCache
{
public string PermRole { get; set; }
public bool Verbose { get; set; } = true;
public PermissionsCollection<Permissionv2> Permissions { get; set; }
}
}

View File

@ -0,0 +1,131 @@
using System.Collections.Generic;
using System.Linq;
using Discord;
using Discord.WebSocket;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Permissions.Common
{
public static class PermissionExtensions
{
public static bool CheckPermissions(this IEnumerable<Permissionv2> permsEnumerable, IUserMessage message,
string commandName, string moduleName, out int permIndex)
{
var perms = permsEnumerable as List<Permissionv2> ?? permsEnumerable.ToList();
for (int i = perms.Count - 1; i >= 0; i--)
{
var perm = perms[i];
var result = perm.CheckPermission(message, commandName, moduleName);
if (result == null)
{
continue;
}
permIndex = i;
return result.Value;
}
permIndex = -1; //defaut behaviour
return true;
}
//null = not applicable
//true = applicable, allowed
//false = applicable, not allowed
public static bool? CheckPermission(this Permissionv2 perm, IUserMessage message, string commandName, string moduleName)
{
if (!((perm.SecondaryTarget == SecondaryPermissionType.Command &&
perm.SecondaryTargetName.ToLowerInvariant() == commandName.ToLowerInvariant()) ||
(perm.SecondaryTarget == SecondaryPermissionType.Module &&
perm.SecondaryTargetName.ToLowerInvariant() == moduleName.ToLowerInvariant()) ||
perm.SecondaryTarget == SecondaryPermissionType.AllModules))
return null;
var guildUser = message.Author as IGuildUser;
switch (perm.PrimaryTarget)
{
case PrimaryPermissionType.User:
if (perm.PrimaryTargetId == message.Author.Id)
return perm.State;
break;
case PrimaryPermissionType.Channel:
if (perm.PrimaryTargetId == message.Channel.Id)
return perm.State;
break;
case PrimaryPermissionType.Role:
if (guildUser == null)
break;
if (guildUser.RoleIds.Contains(perm.PrimaryTargetId))
return perm.State;
break;
case PrimaryPermissionType.Server:
if (guildUser == null)
break;
return perm.State;
}
return null;
}
public static string GetCommand(this Permissionv2 perm, string prefix, SocketGuild guild = null)
{
var com = "";
switch (perm.PrimaryTarget)
{
case PrimaryPermissionType.User:
com += "u";
break;
case PrimaryPermissionType.Channel:
com += "c";
break;
case PrimaryPermissionType.Role:
com += "r";
break;
case PrimaryPermissionType.Server:
com += "s";
break;
}
switch (perm.SecondaryTarget)
{
case SecondaryPermissionType.Module:
com += "m";
break;
case SecondaryPermissionType.Command:
com += "c";
break;
case SecondaryPermissionType.AllModules:
com = "a" + com + "m";
break;
}
var secName = perm.SecondaryTarget == SecondaryPermissionType.Command ?
prefix + perm.SecondaryTargetName : perm.SecondaryTargetName;
com += " " + (perm.SecondaryTargetName != "*" ? secName + " " : "") + (perm.State ? "enable" : "disable") + " ";
switch (perm.PrimaryTarget)
{
case PrimaryPermissionType.User:
com += guild?.GetUser(perm.PrimaryTargetId)?.ToString() ?? $"<@{perm.PrimaryTargetId}>";
break;
case PrimaryPermissionType.Channel:
com += $"<#{perm.PrimaryTargetId}>";
break;
case PrimaryPermissionType.Role:
com += guild?.GetRole(perm.PrimaryTargetId)?.ToString() ?? $"<@&{perm.PrimaryTargetId}>";
break;
case PrimaryPermissionType.Server:
break;
}
return prefix + com;
}
public static IEnumerable<Permission> AsEnumerable(this Permission perm)
{
do yield return perm;
while ((perm = perm.Next) != null);
}
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using NadekoBot.Common.Collections;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Permissions.Common
{
public class PermissionsCollection<T> : IndexedCollection<T> where T : class, IIndexed
{
private readonly object _localLocker = new object();
public PermissionsCollection(IEnumerable<T> source) : base(source)
{
}
public static implicit operator List<T>(PermissionsCollection<T> x) =>
x.Source;
public override void Clear()
{
lock (_localLocker)
{
var first = Source[0];
base.Clear();
Source[0] = first;
}
}
public override bool Remove(T item)
{
bool removed;
lock (_localLocker)
{
if(Source.IndexOf(item) == 0)
throw new ArgumentException("You can't remove first permsission (allow all)");
removed = base.Remove(item);
}
return removed;
}
public override void Insert(int index, T item)
{
lock (_localLocker)
{
if(index == 0) // can't insert on first place. Last item is always allow all.
throw new IndexOutOfRangeException(nameof(index));
base.Insert(index, item);
}
}
public override void RemoveAt(int index)
{
lock (_localLocker)
{
if(index == 0) // you can't remove first permission (allow all)
throw new IndexOutOfRangeException(nameof(index));
base.RemoveAt(index);
}
}
public override T this[int index] {
get => Source[index];
set {
lock (_localLocker)
{
if(index == 0) // can't set first element. It's always allow all
throw new IndexOutOfRangeException(nameof(index));
base[index] = value;
}
}
}
}
}