Skip to content

Commit

Permalink
Expose default CapabilityState
Browse files Browse the repository at this point in the history
  • Loading branch information
mbax committed Jul 19, 2017
1 parent c6300ed commit 4023212
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* * Copyright (C) 2013-2017 Matt Baxter http://kitteh.org
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.kitteh.irc.client.library.element.defaults;

import org.kitteh.irc.client.library.Client;
import org.kitteh.irc.client.library.element.CapabilityState;
import org.kitteh.irc.client.library.util.ToStringer;

import javax.annotation.Nonnull;
import java.util.Optional;

/**
* Default implementation of {@link CapabilityState}.
*/
public class DefaultCapabilityState implements CapabilityState {
private final Client client;
private final long creationTime;
private final boolean disable;
private final String name;
private final Optional<String> value;

/**
* Constructs a capability state.
*
* @param client client
* @param capabilityListItem capability list item including modifiers
*/
public DefaultCapabilityState(@Nonnull Client client, @Nonnull String capabilityListItem) {
this.client = client;
this.creationTime = System.currentTimeMillis();
this.disable = capabilityListItem.charAt(0) == '-';
String remaining = this.disable ? capabilityListItem.substring(1) : capabilityListItem;
int index = remaining.indexOf('=');
if ((index > -1) && (remaining.length() > (index + 1))) {
this.name = remaining.substring(0, index);
this.value = Optional.of(remaining.substring(index + 1));
} else {
this.name = (index > -1) ? remaining.substring(0, index) : remaining;
this.value = Optional.empty();
}
}

@Override
public boolean equals(Object o) {
if (o instanceof DefaultCapabilityState) {
DefaultCapabilityState state = (DefaultCapabilityState) o;
return state.name.equals(this.name) && (state.disable == this.disable);
}
return false;
}

@Override
public boolean isDisabled() {
return this.disable;
}

@Nonnull
@Override
public Client getClient() {
return this.client;
}

@Override
public long getCreationTime() {
return this.creationTime;
}

@Nonnull
@Override
public String getName() {
return this.name;
}

@Nonnull
@Override
public Optional<String> getValue() {
return this.value;
}

@Override
public int hashCode() {
return (2 * this.name.hashCode()) + (this.disable ? 1 : 0);
}

@Nonnull
@Override
public String toString() {
return new ToStringer(this).add("name", this.name).add("disabled", this.disable).add("value", this.value).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.kitteh.irc.client.library.element.ServerMessage;
import org.kitteh.irc.client.library.element.User;
import org.kitteh.irc.client.library.element.WhoisData;
import org.kitteh.irc.client.library.element.defaults.DefaultCapabilityState;
import org.kitteh.irc.client.library.element.defaults.mode.DefaultUserMode;
import org.kitteh.irc.client.library.element.mode.ChannelMode;
import org.kitteh.irc.client.library.element.mode.ChannelUserMode;
Expand Down Expand Up @@ -759,7 +760,7 @@ public void cap(ClientReceiveCommandEvent event) {
} else {
capabilityListIndex = CAPABILITY_LIST_INDEX_DEFAULT;
}
List<CapabilityState> capabilityStateList = Arrays.stream(event.getParameters().get(capabilityListIndex).split(" ")).filter(string -> !string.isEmpty()).map(capability -> new ManagerCapability.IRCCapabilityState(this.client, capability)).collect(Collectors.toCollection(ArrayList::new));
List<CapabilityState> capabilityStateList = Arrays.stream(event.getParameters().get(capabilityListIndex).split(" ")).filter(string -> !string.isEmpty()).map(capability -> new DefaultCapabilityState(this.client, capability)).collect(Collectors.toCollection(ArrayList::new));
switch (event.getParameters().get(1).toLowerCase()) {
case "ack":
this.client.getCapabilityManager().updateCapabilities(capabilityStateList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.kitteh.irc.client.library.implementation;

import org.kitteh.irc.client.library.Client;
import org.kitteh.irc.client.library.element.CapabilityState;
import org.kitteh.irc.client.library.feature.CapabilityManager;
import org.kitteh.irc.client.library.util.ToStringer;
Expand All @@ -32,81 +31,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

final class ManagerCapability implements CapabilityManager, Resettable {
static class IRCCapabilityState implements CapabilityState {
private final Client client;
private final long creationTime;
private final boolean disable;
private final String name;
private final Optional<String> value;

IRCCapabilityState(@Nonnull Client client, @Nonnull String capabilityListItem) {
this.client = client;
this.creationTime = System.currentTimeMillis();
this.disable = capabilityListItem.charAt(0) == '-';
String remaining = this.disable ? capabilityListItem.substring(1) : capabilityListItem;
int index = remaining.indexOf('=');
if ((index > -1) && (remaining.length() > (index + 1))) {
this.name = remaining.substring(0, index);
this.value = Optional.of(remaining.substring(index + 1));
} else {
this.name = (index > -1) ? remaining.substring(0, index) : remaining;
this.value = Optional.empty();
}
}

@Override
public boolean equals(Object o) {
if (o instanceof IRCCapabilityState) {
IRCCapabilityState state = (IRCCapabilityState) o;
return state.name.equals(this.name) && (state.disable == this.disable);
}
return false;
}

@Override
public boolean isDisabled() {
return this.disable;
}

@Nonnull
@Override
public Client getClient() {
return this.client;
}

@Override
public long getCreationTime() {
return this.creationTime;
}

@Nonnull
@Override
public String getName() {
return this.name;
}

@Nonnull
@Override
public Optional<String> getValue() {
return this.value;
}

@Override
public int hashCode() {
return (2 * this.name.hashCode()) + (this.disable ? 1 : 0);
}

@Nonnull
@Override
public String toString() {
return new ToStringer(this).add("name", this.name).add("disabled", this.disable).add("value", this.value).toString();
}
}

private final InternalClient client;
private final Map<String, CapabilityState> capabilities = new ConcurrentHashMap<>();
private List<CapabilityState> supportedCapabilities = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.kitteh.irc.client.library.element.CapabilityState;
import org.kitteh.irc.client.library.element.ServerMessage;
import org.kitteh.irc.client.library.element.defaults.DefaultServerMessage;
import org.kitteh.irc.client.library.element.defaults.DefaultCapabilityState;
import org.kitteh.irc.client.library.event.capabilities.CapabilitiesNewSupportedEvent;
import org.kitteh.irc.client.library.event.capabilities.CapabilitiesSupportedListEvent;
import org.kitteh.irc.client.library.exception.KittehServerMessageException;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void testHandlerWhenInsecure() {

List<CapabilityState> capabilities = new ArrayList<>();
final String policyString = "draft/sts=" + STSPolicy.POLICY_OPTION_KEY_PORT + "=1234," + STSPolicy.POLICY_OPTION_KEY_DURATION + "=300,foobar";
capabilities.add(new ManagerCapability.IRCCapabilityState(client, policyString));
capabilities.add(new DefaultCapabilityState(client, policyString));
List<ServerMessage> messages = new ArrayList<>();
messages.add(new DefaultServerMessage(":test.kitteh CAP ^o^ LS :" + policyString, new ArrayList<>()));
handler.onCapLs(new CapabilitiesSupportedListEvent(client, messages, true, capabilities));
Expand Down Expand Up @@ -68,7 +69,7 @@ public void testHandlerWhenInsecureUsingCapNew() {

List<CapabilityState> capabilities = new ArrayList<>();
final String policyString = "draft/sts=" + STSPolicy.POLICY_OPTION_KEY_PORT + "=1234," + STSPolicy.POLICY_OPTION_KEY_DURATION + "=300,foobar";
capabilities.add(new ManagerCapability.IRCCapabilityState(client, policyString));
capabilities.add(new DefaultCapabilityState(client, policyString));
List<ServerMessage> messages = new ArrayList<>();
messages.add(new DefaultServerMessage(":test.kitteh CAP ^o^ LS :" + policyString, new ArrayList<>()));
handler.onCapNew(new CapabilitiesNewSupportedEvent(client, messages, true, capabilities));
Expand All @@ -95,7 +96,7 @@ public void testHandlerWithInvalidPort() {
STSHandler handler = new STSHandler(machine, client);
List<CapabilityState> capabilities = new ArrayList<>();
final String policyString = "draft/sts=" + STSPolicy.POLICY_OPTION_KEY_PORT + "=cats";
capabilities.add(new ManagerCapability.IRCCapabilityState(client, policyString));
capabilities.add(new DefaultCapabilityState(client, policyString));
List<ServerMessage> messages = new ArrayList<>();
messages.add(new DefaultServerMessage(":test.kitteh CAP ^o^ LS :" + policyString, new ArrayList<>()));
handler.onCapLs(new CapabilitiesSupportedListEvent(client, messages, true, capabilities));
Expand All @@ -112,7 +113,7 @@ public void testHandlerWithMissingPortValue() {
STSHandler handler = new STSHandler(machine, client);
List<CapabilityState> capabilities = new ArrayList<>();
final String policyString = "draft/sts=" + STSPolicy.POLICY_OPTION_KEY_PORT;
capabilities.add(new ManagerCapability.IRCCapabilityState(client, policyString));
capabilities.add(new DefaultCapabilityState(client, policyString));
List<ServerMessage> messages = new ArrayList<>();
messages.add(new DefaultServerMessage(":test.kitteh CAP ^o^ LS :" + policyString, new ArrayList<>()));
handler.onCapLs(new CapabilitiesSupportedListEvent(client, messages, true, capabilities));
Expand Down

0 comments on commit 4023212

Please sign in to comment.