diff --git a/nickkeep/plugin.config b/nickkeep/plugin.config new file mode 100644 index 000000000..afb4edb92 --- /dev/null +++ b/nickkeep/plugin.config @@ -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 + mainclass=com.dmdirc.addons.nickkeep.NickKeepPlugin + description=Keeps your primary nickname + name=nickkeep + nicename=Nick keep plugin + +updates: + id=78 + +version: + friendly=0.1 \ No newline at end of file diff --git a/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepManager.java b/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepManager.java new file mode 100644 index 000000000..0ffd0a5e3 --- /dev/null +++ b/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepManager.java @@ -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 = event.getChannel().getConnection(); + if (!connection.isPresent()) { + //No point carrying on without a connection + return; + } + final Optional 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 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())); + } + } +} diff --git a/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepModule.java b/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepModule.java new file mode 100644 index 000000000..fab232483 --- /dev/null +++ b/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepModule.java @@ -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 {} diff --git a/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepPlugin.java b/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepPlugin.java new file mode 100644 index 000000000..2a6e7ae8f --- /dev/null +++ b/nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepPlugin.java @@ -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(); + } +} diff --git a/nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepManagerTest.java b/nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepManagerTest.java new file mode 100644 index 000000000..14e2083f9 --- /dev/null +++ b/nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepManagerTest.java @@ -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"); + } +} \ No newline at end of file diff --git a/nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepPluginTest.java b/nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepPluginTest.java new file mode 100644 index 000000000..c02c1a227 --- /dev/null +++ b/nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepPluginTest.java @@ -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.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(); + } +} \ No newline at end of file