Skip to content

Commit

Permalink
added matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Jul 14, 2021
1 parent 4ff3ae8 commit 973e195
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BotMatcher implements PermissionMatcher {
LinkedHashMap<String, PermissionMatcher> restricted = new LinkedHashMap<>(5);
Map<Long, GroupMatcher> groupmatchers = new ConcurrentHashMap<>(10);
Map<Long, PermissionResult> friendpermissions = new ConcurrentHashMap<>(10);
Map<WildcardPermission, GroupMatcher> permmatcher = new ConcurrentHashMap<>();
Map<WildcardPermission, GroupMatcher> permmatcher = new LinkedHashMap<>();

@FunctionalInterface
interface PermissionFactory {
Expand All @@ -36,6 +36,7 @@ public PermissionResult match(long id, long group, Bot bot) {
for (PermissionMatcher pm : restricted.values()) {
pr=pr.and(pm.match(id, group, bot));
}
pr=pr.and(friendpermissions.getOrDefault(id, PermissionResult.UNSPECIFIED));
if (group != 0) {
MemberPermission mp = bot.getGroup(group).getBotPermission();
for (Entry<WildcardPermission, GroupMatcher> me : permmatcher.entrySet()) {
Expand All @@ -47,8 +48,6 @@ public PermissionResult match(long id, long group, Bot bot) {
if (pm != null) {
pr=pr.and(pm.match(id, group, bot));
}
} else {
pr=pr.and(friendpermissions.getOrDefault(id, PermissionResult.UNSPECIFIED));
}
return pr;
}
Expand All @@ -59,6 +58,7 @@ public PermissionResult match(Member m) {
for (PermissionMatcher pm : restricted.values()) {
pr=pr.and(pm.match(m));
}
pr=pr.and(friendpermissions.getOrDefault(m.getId(), PermissionResult.UNSPECIFIED));
MemberPermission mp = m.getGroup().getBotPermission();
for (Entry<WildcardPermission, GroupMatcher> me : permmatcher.entrySet()) {
if (me.getKey().isMatch(mp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public enum WildcardPermission {
admin(m->m==MemberPermission.ADMINISTRATOR),
admins(m->m!=MemberPermission.MEMBER),
owner(m->m==MemberPermission.OWNER),
member(m->m==MemberPermission.MEMBER);
member(m->m==MemberPermission.MEMBER),
members(m->true);
private final Predicate<MemberPermission> matcher;

private WildcardPermission(Predicate<MemberPermission> matcher) {
Expand Down

0 comments on commit 973e195

Please sign in to comment.