Browse Source

Merge pull request #313 from greboid/dev4

Add nick keep plugin.
pull/316/head
Chris Smith 9 years ago
parent
commit
00f0f142fa

+ 22
- 0
nickkeep/plugin.config View File

@@ -0,0 +1,22 @@
1
+# This is a DMDirc configuration file.
2
+
3
+# This section indicates which sections below take key/value
4
+# pairs, rather than a simple list. It should be placed above
5
+# any sections that take key/values.
6
+keysections:
7
+  metadata
8
+  updates
9
+  version
10
+
11
+metadata:
12
+  author=Greg <greg@dmdirc.com>
13
+  mainclass=com.dmdirc.addons.nickkeep.NickKeepPlugin
14
+  description=Keeps your primary nickname
15
+  name=nickkeep
16
+  nicename=Nick keep plugin
17
+
18
+updates:
19
+  id=78
20
+
21
+version:
22
+  friendly=0.1

+ 84
- 0
nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepManager.java View File

@@ -0,0 +1,84 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nickkeep;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.profiles.Profile;
27
+import com.dmdirc.events.ChannelNickChangeEvent;
28
+import com.dmdirc.interfaces.Connection;
29
+
30
+import java.util.Optional;
31
+
32
+import javax.inject.Inject;
33
+
34
+import net.engio.mbassy.listener.Handler;
35
+
36
+/**
37
+ * Provides Nick Keep support in DMDirc.
38
+ */
39
+public class NickKeepManager {
40
+
41
+    private final DMDircMBassador eventBus;
42
+
43
+    @Inject
44
+    public NickKeepManager(final DMDircMBassador eventBus) {
45
+        this.eventBus = eventBus;
46
+    }
47
+
48
+    public void load() {
49
+        eventBus.subscribe(this);
50
+    }
51
+
52
+    public void unload() {
53
+        eventBus.unsubscribe(this);
54
+    }
55
+
56
+    @Handler
57
+    public void handleNickChange(final ChannelNickChangeEvent event) {
58
+        final Optional<Connection> connection = event.getChannel().getConnection();
59
+        if (!connection.isPresent()) {
60
+            //No point carrying on without a connection
61
+            return;
62
+        }
63
+        final Optional<String> currentNickname = connection.map(Connection::getNickname)
64
+                .orElse(Optional.empty());
65
+        if (!currentNickname.isPresent()) {
66
+            //No point carrying on if we don't know our own nickname
67
+            return;
68
+        }
69
+        final Optional<String> desiredNickname = connection.map(Connection::getProfile)
70
+                .map(Profile::getNicknames).map(c -> c.stream().findFirst())
71
+                .orElse(Optional.empty());
72
+        if (!desiredNickname.isPresent()) {
73
+            //No point carrying on without a desired nickname
74
+            return;
75
+        }
76
+        if (!event.getOldNick().equals(desiredNickname.get())) {
77
+            //This isnt the nickname we're looking for, move along
78
+            return;
79
+        }
80
+        if (!currentNickname.equals(desiredNickname)) {
81
+            connection.ifPresent(c -> c.setNickname(desiredNickname.get()));
82
+        }
83
+    }
84
+}

+ 33
- 0
nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepModule.java View File

@@ -0,0 +1,33 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nickkeep;
24
+
25
+import com.dmdirc.ClientModule;
26
+
27
+import dagger.Module;
28
+
29
+/**
30
+ * Dagger injection module for the Nick Keep plugin
31
+ */
32
+@Module(injects = NickKeepManager.class, addsTo = ClientModule.class)
33
+public class NickKeepModule {}

+ 54
- 0
nickkeep/src/com/dmdirc/addons/nickkeep/NickKeepPlugin.java View File

@@ -0,0 +1,54 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nickkeep;
24
+
25
+import com.dmdirc.plugins.PluginInfo;
26
+import com.dmdirc.plugins.implementations.BasePlugin;
27
+
28
+import dagger.ObjectGraph;
29
+
30
+/**
31
+ * Plugin to provide Nick Keep support to DMDirc.
32
+ */
33
+public class NickKeepPlugin extends BasePlugin {
34
+
35
+    private NickKeepManager manager;
36
+
37
+    @Override
38
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
39
+        super.load(pluginInfo, graph);
40
+
41
+        setObjectGraph(graph.plus(new NickKeepModule()));
42
+        manager = getObjectGraph().get(NickKeepManager.class);
43
+    }
44
+
45
+    @Override
46
+    public void onLoad() {
47
+        manager.load();
48
+    }
49
+
50
+    @Override
51
+    public void onUnload() {
52
+        manager.unload();
53
+    }
54
+}

+ 113
- 0
nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepManagerTest.java View File

@@ -0,0 +1,113 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nickkeep;
24
+
25
+import com.dmdirc.Channel;
26
+import com.dmdirc.DMDircMBassador;
27
+import com.dmdirc.config.profiles.Profile;
28
+import com.dmdirc.events.ChannelNickChangeEvent;
29
+import com.dmdirc.interfaces.Connection;
30
+
31
+import com.google.common.collect.Lists;
32
+
33
+import java.util.Optional;
34
+
35
+import org.junit.Before;
36
+import org.junit.Test;
37
+import org.junit.runner.RunWith;
38
+import org.mockito.Mock;
39
+import org.mockito.runners.MockitoJUnitRunner;
40
+
41
+import static org.mockito.Matchers.anyString;
42
+import static org.mockito.Mockito.never;
43
+import static org.mockito.Mockito.verify;
44
+import static org.mockito.Mockito.when;
45
+
46
+@RunWith(MockitoJUnitRunner.class)
47
+public class NickKeepManagerTest {
48
+
49
+    @Mock private DMDircMBassador eventBus;
50
+    @Mock private ChannelNickChangeEvent event;
51
+    @Mock private Channel channel;
52
+    @Mock private Connection connection;
53
+    @Mock private Profile profile;
54
+    private NickKeepManager instance;
55
+
56
+    @Before
57
+    public void setUp() throws Exception {
58
+        instance = new NickKeepManager(eventBus);
59
+        when(event.getChannel()).thenReturn(channel);
60
+        when(channel.getConnection()).thenReturn(Optional.of(connection));
61
+        when(connection.getProfile()).thenReturn(profile);
62
+        when(profile.getNicknames()).thenReturn(Lists.newArrayList("one", "two", "three"));
63
+    }
64
+
65
+    @Test
66
+    public void testLoad() throws Exception {
67
+        instance.load();
68
+        verify(eventBus).subscribe(instance);
69
+    }
70
+
71
+    @Test
72
+    public void testUnload() throws Exception {
73
+        instance.unload();
74
+        verify(eventBus).unsubscribe(instance);
75
+    }
76
+
77
+    @Test
78
+    public void testHandleNickChange_NoConnection() throws Exception {
79
+        when(channel.getConnection()).thenReturn(Optional.empty());
80
+        instance.handleNickChange(event);
81
+        verify(connection, never()).setNickname("one");
82
+    }
83
+
84
+    @Test
85
+    public void testHandleNickChange_NoCurrentNickname() throws Exception {
86
+        when(connection.getNickname()).thenReturn(Optional.empty());
87
+        instance.handleNickChange(event);
88
+        verify(connection, never()).setNickname("one");
89
+    }
90
+
91
+    @Test
92
+    public void testHandleNickChange_NoNicknames() throws Exception {
93
+        when(profile.getNicknames()).thenReturn(Lists.newArrayList());
94
+        instance.handleNickChange(event);
95
+        verify(connection, never()).setNickname(anyString());
96
+    }
97
+
98
+    @Test
99
+    public void testHandleNickChange_NotDesired() throws Exception {
100
+        when(event.getOldNick()).thenReturn("RAR");
101
+        when(connection.getNickname()).thenReturn(Optional.of("one"));
102
+        instance.handleNickChange(event);
103
+        verify(connection, never()).setNickname("one");
104
+    }
105
+
106
+    @Test
107
+    public void testHandleNickChange_Desired() throws Exception {
108
+        when(event.getOldNick()).thenReturn("one");
109
+        when(connection.getNickname()).thenReturn(Optional.of("RAR"));
110
+        instance.handleNickChange(event);
111
+        verify(connection).setNickname("one");
112
+    }
113
+}

+ 66
- 0
nickkeep/test/com/dmdirc/addons/nickkeep/NickKeepPluginTest.java View File

@@ -0,0 +1,66 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.nickkeep;
24
+
25
+import com.dmdirc.plugins.PluginInfo;
26
+
27
+import org.junit.Before;
28
+import org.junit.Test;
29
+import org.junit.runner.RunWith;
30
+import org.mockito.Mock;
31
+import org.mockito.runners.MockitoJUnitRunner;
32
+
33
+import dagger.ObjectGraph;
34
+
35
+import static org.mockito.Matchers.any;
36
+import static org.mockito.Mockito.verify;
37
+import static org.mockito.Mockito.when;
38
+
39
+@RunWith(MockitoJUnitRunner.class)
40
+public class NickKeepPluginTest {
41
+
42
+    @Mock private NickKeepManager nickKeepManager;
43
+    @Mock private PluginInfo pluginInfo;
44
+    @Mock private ObjectGraph objectGraph;
45
+    private NickKeepPlugin instance;
46
+
47
+    @Before
48
+    public void setUp() throws Exception {
49
+        when(objectGraph.<NickKeepManager>get(any())).thenReturn(nickKeepManager);
50
+        when(objectGraph.plus(any())).thenReturn(objectGraph);
51
+        instance = new NickKeepPlugin();
52
+        instance.load(pluginInfo, objectGraph);
53
+    }
54
+
55
+    @Test
56
+    public void testOnLoad() throws Exception {
57
+        instance.onLoad();
58
+        verify(nickKeepManager).load();
59
+    }
60
+
61
+    @Test
62
+    public void testOnUnload() throws Exception {
63
+        instance.onUnload();
64
+        verify(nickKeepManager).unload();
65
+    }
66
+}

Loading…
Cancel
Save