Skip to content

Commit

Permalink
Start adding some functionality to Mask
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Nov 7, 2016
1 parent 0758b6d commit f07e49a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.kitteh.irc.client.library.element.mode.ModeStatus;
import org.kitteh.irc.client.library.element.mode.ModeStatusList;
import org.kitteh.irc.client.library.util.CIKeyMap;
import org.kitteh.irc.client.library.util.Mask;
import org.kitteh.irc.client.library.util.Sanity;
import org.kitteh.irc.client.library.util.ToStringer;

Expand Down Expand Up @@ -698,12 +699,6 @@ public String toString() {
}
}

// Valid nick chars: \w\[]^`{}|-_
// Pattern unescaped: ([\w\\\[\]\^`\{\}\|\-_]+)!([~\w]+)@([\w\.\-:]+)
// You know what? Screw it.
// Let's just do it assuming no IRCD can handle following the rules.
// New pattern: ([^!@]+)!([^!@]+)@([^!@]+)
private static final Pattern NICK_PATTERN = Pattern.compile("([^!@]+)!([^!@]+)@([^!@]+)");
private static final Pattern SERVER_PATTERN = Pattern.compile("(?!\\-)(?:[a-zA-Z\\d\\-]{0,62}[a-zA-Z\\d]\\.){1,126}(?!\\d+)[a-zA-Z\\d]{1,63}");

private final InternalClient client;
Expand Down Expand Up @@ -735,7 +730,7 @@ void unTrackChannel(@Nonnull IRCChannel channel) {

@Nonnull
IRCActor getActor(@Nonnull String name) {
Matcher nickMatcher = NICK_PATTERN.matcher(name);
Matcher nickMatcher = Mask.NICK_PATTERN.matcher(name);
if (nickMatcher.matches()) {
String nick = nickMatcher.group(1);
IRCUser user = this.trackedUsers.get(nick);
Expand Down
54 changes: 48 additions & 6 deletions src/main/java/org/kitteh/irc/client/library/util/Mask.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
*/
package org.kitteh.irc.client.library.util;

import org.kitteh.irc.client.library.element.Channel;
import org.kitteh.irc.client.library.element.User;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Represents a mask that can match a {@link User}.
*/
public class Mask {
public class Mask implements Predicate<User> {
/**
* Creates a Mask from a given String.
*
Expand All @@ -40,13 +45,41 @@ public class Mask {
@Nonnull
public static Mask fromString(@Nonnull String string) {
Sanity.nullCheck(string, "String cannot be null");
return new Mask(string);

Matcher matcher = NICK_PATTERN.matcher(name);
if (matcher.matches()) {
return new Mask(matcher.group(1), matcher.group(2), matcher.group(3));
}
}
/**
* Creates a Mask from a given User.
*
* @param user user
* @return mask from user
*/
@Nonnull
public static Mask fromString(@Nonnull User user) {
Sanity.nullCheck(string, "User cannot be null");

return new Mask(user.getHost(), user.getNick(), user.getUserString());
}

private final String string;
// Valid nick chars: \w\[]^`{}|-_
// Pattern unescaped: ([\w\\\[\]\^`\{\}\|\-_]+)!([~\w]+)@([\w\.\-:]+)
// You know what? Screw it.
// Let's just do it assuming no IRCD can handle following the rules.
// New pattern: ([^!@]+)!([^!@]+)@([^!@]+)
public static final Pattern NICK_PATTERN = Pattern.compile("([^!@]+)!([^!@]+)@([^!@]+)");
private String host;
private String nick;
private String user;

private Mask(@Nonnull String string) {
this.string = string;
private Mask(@Nonnull String host, @Nonnull String nick, @Nonnull String user) {
// TODO - null
this.nick = nick;
this.user = user;
this.host = host;
// account?
}

/**
Expand All @@ -56,7 +89,16 @@ private Mask(@Nonnull String string) {
*/
@Nonnull
public String asString() {
return this.string;
return this.nick + '!' + this.user + '@' + this.host;
}

public Collection<User> getMatches(@Nonnull Channel channel) {
return channel.getUsers().stream().filter(this::test)collect(Collectors.toCollection(HashSet::new));
}

@Override
public boolean test(User user) {
return false; // TODO
}

@Override
Expand Down

0 comments on commit f07e49a

Please sign in to comment.