2017-07-17 19:42:36 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Discord;
|
2016-12-17 04:09:04 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Permissions.Common
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
|
|
|
|
public static class PermissionExtensions
|
|
|
|
|
{
|
2017-03-08 23:58:58 +00:00
|
|
|
|
public static bool CheckPermissions(this IEnumerable<Permissionv2> permsEnumerable, IUserMessage message,
|
|
|
|
|
string commandName, string moduleName, out int permIndex)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
2017-03-08 23:58:58 +00:00
|
|
|
|
var perms = permsEnumerable as List<Permissionv2> ?? permsEnumerable.ToList();
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
2017-03-08 23:58:58 +00:00
|
|
|
|
for (int i = perms.Count - 1; i >= 0; i--)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
|
|
|
|
var perm = perms[i];
|
|
|
|
|
|
2016-09-30 15:16:30 +00:00
|
|
|
|
var result = perm.CheckPermission(message, commandName, moduleName);
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-03-08 23:58:58 +00:00
|
|
|
|
permIndex = i;
|
|
|
|
|
return result.Value;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
permIndex = -1; //defaut behaviour
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//null = not applicable
|
|
|
|
|
//true = applicable, allowed
|
|
|
|
|
//false = applicable, not allowed
|
2017-03-08 23:58:58 +00:00
|
|
|
|
public static bool? CheckPermission(this Permissionv2 perm, IUserMessage message, string commandName, string moduleName)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
|
|
|
|
if (!((perm.SecondaryTarget == SecondaryPermissionType.Command &&
|
2016-09-30 15:16:30 +00:00
|
|
|
|
perm.SecondaryTargetName.ToLowerInvariant() == commandName.ToLowerInvariant()) ||
|
2016-09-30 00:40:33 +00:00
|
|
|
|
(perm.SecondaryTarget == SecondaryPermissionType.Module &&
|
2016-09-30 15:16:30 +00:00
|
|
|
|
perm.SecondaryTargetName.ToLowerInvariant() == moduleName.ToLowerInvariant()) ||
|
2016-09-30 00:40:33 +00:00
|
|
|
|
perm.SecondaryTarget == SecondaryPermissionType.AllModules))
|
2016-09-22 18:53:49 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
2016-09-30 00:40:33 +00:00
|
|
|
|
var guildUser = message.Author as IGuildUser;
|
|
|
|
|
|
2016-09-22 18:53:49 +00:00
|
|
|
|
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;
|
2016-09-30 00:40:33 +00:00
|
|
|
|
case PrimaryPermissionType.Role:
|
2016-09-22 18:53:49 +00:00
|
|
|
|
if (guildUser == null)
|
|
|
|
|
break;
|
2016-12-16 18:43:57 +00:00
|
|
|
|
if (guildUser.RoleIds.Contains(perm.PrimaryTargetId))
|
2016-09-22 18:53:49 +00:00
|
|
|
|
return perm.State;
|
|
|
|
|
break;
|
2016-09-30 00:40:33 +00:00
|
|
|
|
case PrimaryPermissionType.Server:
|
|
|
|
|
if (guildUser == null)
|
|
|
|
|
break;
|
|
|
|
|
return perm.State;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 03:12:00 +00:00
|
|
|
|
public static string GetCommand(this Permissionv2 perm, string prefix, SocketGuild guild = null)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
2016-09-23 08:23:54 +00:00
|
|
|
|
var com = "";
|
2016-09-22 18:53:49 +00:00
|
|
|
|
switch (perm.PrimaryTarget)
|
|
|
|
|
{
|
|
|
|
|
case PrimaryPermissionType.User:
|
|
|
|
|
com += "u";
|
|
|
|
|
break;
|
|
|
|
|
case PrimaryPermissionType.Channel:
|
|
|
|
|
com += "c";
|
|
|
|
|
break;
|
|
|
|
|
case PrimaryPermissionType.Role:
|
|
|
|
|
com += "r";
|
|
|
|
|
break;
|
2016-09-30 00:40:33 +00:00
|
|
|
|
case PrimaryPermissionType.Server:
|
|
|
|
|
com += "s";
|
|
|
|
|
break;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (perm.SecondaryTarget)
|
|
|
|
|
{
|
|
|
|
|
case SecondaryPermissionType.Module:
|
|
|
|
|
com += "m";
|
|
|
|
|
break;
|
|
|
|
|
case SecondaryPermissionType.Command:
|
|
|
|
|
com += "c";
|
|
|
|
|
break;
|
2016-09-23 08:23:54 +00:00
|
|
|
|
case SecondaryPermissionType.AllModules:
|
|
|
|
|
com = "a" + com + "m";
|
|
|
|
|
break;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
2017-06-01 03:12:00 +00:00
|
|
|
|
|
|
|
|
|
var secName = perm.SecondaryTarget == SecondaryPermissionType.Command ?
|
|
|
|
|
prefix + perm.SecondaryTargetName : perm.SecondaryTargetName;
|
|
|
|
|
com += " " + (perm.SecondaryTargetName != "*" ? secName + " " : "") + (perm.State ? "enable" : "disable") + " ";
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
|
|
|
|
switch (perm.PrimaryTarget)
|
|
|
|
|
{
|
|
|
|
|
case PrimaryPermissionType.User:
|
2017-04-04 17:48:33 +00:00
|
|
|
|
com += guild?.GetUser(perm.PrimaryTargetId)?.ToString() ?? $"<@{perm.PrimaryTargetId}>";
|
2016-09-22 18:53:49 +00:00
|
|
|
|
break;
|
|
|
|
|
case PrimaryPermissionType.Channel:
|
|
|
|
|
com += $"<#{perm.PrimaryTargetId}>";
|
|
|
|
|
break;
|
|
|
|
|
case PrimaryPermissionType.Role:
|
2017-03-21 07:46:13 +00:00
|
|
|
|
com += guild?.GetRole(perm.PrimaryTargetId)?.ToString() ?? $"<@&{perm.PrimaryTargetId}>";
|
2016-09-22 18:53:49 +00:00
|
|
|
|
break;
|
2016-09-30 00:40:33 +00:00
|
|
|
|
case PrimaryPermissionType.Server:
|
|
|
|
|
break;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 03:12:00 +00:00
|
|
|
|
return prefix + com;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-27 13:26:37 +00:00
|
|
|
|
public static IEnumerable<Permission> AsEnumerable(this Permission perm)
|
|
|
|
|
{
|
|
|
|
|
do yield return perm;
|
|
|
|
|
while ((perm = perm.Next) != null);
|
|
|
|
|
}
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|