Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions nickkeep/plugin.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is a DMDirc configuration file.

# This section indicates which sections below take key/value
# pairs, rather than a simple list. It should be placed above
# any sections that take key/values.
keysections:
metadata
updates
version

metadata:
author=Greg <greg@dmdirc.com>
mainclass=com.dmdirc.addons.nickkeep.NickKeepPlugin
description=Keeps your primary nickname
name=nickkeep
nicename=Nick keep plugin

updates:
id=78

version:
friendly=0.1
84 changes: 84 additions & 0 deletions nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* 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 com.dmdirc.addons.nickkeep;

import com.dmdirc.DMDircMBassador;
import com.dmdirc.config.profiles.Profile;
import com.dmdirc.events.ChannelNickChangeEvent;
import com.dmdirc.interfaces.Connection;

import java.util.Optional;

import javax.inject.Inject;

import net.engio.mbassy.listener.Handler;

/**
* Provides Nick Keep support in DMDirc.
*/
public class NickKeepManager {

private final DMDircMBassador eventBus;

@Inject
public NickKeepManager(final DMDircMBassador eventBus) {
this.eventBus = eventBus;
}

public void load() {
eventBus.subscribe(this);
}

public void unload() {
eventBus.unsubscribe(this);
}

@Handler
public void handleNickChange(final ChannelNickChangeEvent event) {
final Optional<Connection> connection = event.getChannel().getConnection();
if (!connection.isPresent()) {
//No point carrying on without a connection
return;
}
final Optional<String> currentNickname = connection.map(Connection::getNickname)
.orElse(Optional.empty());
if (!currentNickname.isPresent()) {
//No point carrying on if we don't know our own nickname
return;
}
final Optional<String> desiredNickname = connection.map(Connection::getProfile)
.map(Profile::getNicknames).map(c -> c.stream().findFirst())
.orElse(Optional.empty());
if (!desiredNickname.isPresent()) {
//No point carrying on without a desired nickname
return;
}
if (!event.getOldNick().equals(desiredNickname.get())) {
//This isnt the nickname we're looking for, move along
return;
}
if (!currentNickname.equals(desiredNickname)) {
connection.ifPresent(c -> c.setNickname(desiredNickname.get()));
}
}
}
33 changes: 33 additions & 0 deletions nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* 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 com.dmdirc.addons.nickkeep;

import com.dmdirc.ClientModule;

import dagger.Module;

/**
* Dagger injection module for the Nick Keep plugin
*/
@Module(injects = NickKeepManager.class, addsTo = ClientModule.class)
public class NickKeepModule {}
54 changes: 54 additions & 0 deletions nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* 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 com.dmdirc.addons.nickkeep;

import com.dmdirc.plugins.PluginInfo;
import com.dmdirc.plugins.implementations.BasePlugin;

import dagger.ObjectGraph;

/**
* Plugin to provide Nick Keep support to DMDirc.
*/
public class NickKeepPlugin extends BasePlugin {

private NickKeepManager manager;

@Override
public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
super.load(pluginInfo, graph);

setObjectGraph(graph.plus(new NickKeepModule()));
manager = getObjectGraph().get(NickKeepManager.class);
}

@Override
public void onLoad() {
manager.load();
}

@Override
public void onUnload() {
manager.unload();
}
}
113 changes: 113 additions & 0 deletions nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* 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 com.dmdirc.addons.nickkeep;

import com.dmdirc.Channel;
import com.dmdirc.DMDircMBassador;
import com.dmdirc.config.profiles.Profile;
import com.dmdirc.events.ChannelNickChangeEvent;
import com.dmdirc.interfaces.Connection;

import com.google.common.collect.Lists;

import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class NickKeepManagerTest {

@Mock private DMDircMBassador eventBus;
@Mock private ChannelNickChangeEvent event;
@Mock private Channel channel;
@Mock private Connection connection;
@Mock private Profile profile;
private NickKeepManager instance;

@Before
public void setUp() throws Exception {
instance = new NickKeepManager(eventBus);
when(event.getChannel()).thenReturn(channel);
when(channel.getConnection()).thenReturn(Optional.of(connection));
when(connection.getProfile()).thenReturn(profile);
when(profile.getNicknames()).thenReturn(Lists.newArrayList("one", "two", "three"));
}

@Test
public void testLoad() throws Exception {
instance.load();
verify(eventBus).subscribe(instance);
}

@Test
public void testUnload() throws Exception {
instance.unload();
verify(eventBus).unsubscribe(instance);
}

@Test
public void testHandleNickChange_NoConnection() throws Exception {
when(channel.getConnection()).thenReturn(Optional.empty());
instance.handleNickChange(event);
verify(connection, never()).setNickname("one");
}

@Test
public void testHandleNickChange_NoCurrentNickname() throws Exception {
when(connection.getNickname()).thenReturn(Optional.empty());
instance.handleNickChange(event);
verify(connection, never()).setNickname("one");
}

@Test
public void testHandleNickChange_NoNicknames() throws Exception {
when(profile.getNicknames()).thenReturn(Lists.newArrayList());
instance.handleNickChange(event);
verify(connection, never()).setNickname(anyString());
}

@Test
public void testHandleNickChange_NotDesired() throws Exception {
when(event.getOldNick()).thenReturn("RAR");
when(connection.getNickname()).thenReturn(Optional.of("one"));
instance.handleNickChange(event);
verify(connection, never()).setNickname("one");
}

@Test
public void testHandleNickChange_Desired() throws Exception {
when(event.getOldNick()).thenReturn("one");
when(connection.getNickname()).thenReturn(Optional.of("RAR"));
instance.handleNickChange(event);
verify(connection).setNickname("one");
}
}
66 changes: 66 additions & 0 deletions nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepPluginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* 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 com.dmdirc.addons.nickkeep;

import com.dmdirc.plugins.PluginInfo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import dagger.ObjectGraph;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class NickKeepPluginTest {

@Mock private NickKeepManager nickKeepManager;
@Mock private PluginInfo pluginInfo;
@Mock private ObjectGraph objectGraph;
private NickKeepPlugin instance;

@Before
public void setUp() throws Exception {
when(objectGraph.<NickKeepManager>get(any())).thenReturn(nickKeepManager);
when(objectGraph.plus(any())).thenReturn(objectGraph);
instance = new NickKeepPlugin();
instance.load(pluginInfo, objectGraph);
}

@Test
public void testOnLoad() throws Exception {
instance.onLoad();
verify(nickKeepManager).load();
}

@Test
public void testOnUnload() throws Exception {
instance.onUnload();
verify(nickKeepManager).unload();
}
}