Browse Source

Add a whois on query plugin.

pull/401/head
Greg Holmes 9 years ago
parent
commit
a40652c46f

+ 5
- 1
whoisonquery/plugin.config View File

@@ -7,6 +7,7 @@ keysections:
7 7
   metadata
8 8
   updates
9 9
   version
10
+  defaults
10 11
 
11 12
 metadata:
12 13
   author=Greg <greg@dmdirc.com>
@@ -19,4 +20,7 @@ updates:
19 20
   id=80
20 21
 
21 22
 version:
22
-  friendly=0.1
23
+  friendly=0.1
24
+
25
+defaults:
26
+  whoisonquery=false

+ 104
- 0
whoisonquery/src/com/dmdirc/addons/whoisonquery/WhoisOnQueryManager.java View File

@@ -0,0 +1,104 @@
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.whoisonquery;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
27
+import com.dmdirc.config.prefs.PreferencesCategory;
28
+import com.dmdirc.config.prefs.PreferencesSetting;
29
+import com.dmdirc.config.prefs.PreferencesType;
30
+import com.dmdirc.events.ClientPrefsOpenedEvent;
31
+import com.dmdirc.events.ConnectionPrefsRequestedEvent;
32
+import com.dmdirc.events.QueryOpenedEvent;
33
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
34
+import com.dmdirc.interfaces.config.ConfigProvider;
35
+import com.dmdirc.plugins.PluginDomain;
36
+import com.dmdirc.plugins.PluginInfo;
37
+
38
+import com.google.common.annotations.VisibleForTesting;
39
+
40
+import javax.inject.Inject;
41
+
42
+import net.engio.mbassy.listener.Handler;
43
+
44
+/**
45
+ * Sends a whois when a new query is opened.
46
+ */
47
+public class WhoisOnQueryManager {
48
+
49
+    private final String domain;
50
+    private final PluginInfo pluginInfo;
51
+    private final DMDircMBassador eventBus;
52
+
53
+    @Inject
54
+    public WhoisOnQueryManager(@PluginDomain(WhoisOnQueryPlugin.class) final String domain,
55
+            @PluginDomain(WhoisOnQueryPlugin.class) final PluginInfo pluginInfo,
56
+            final DMDircMBassador eventBus) {
57
+        this.domain = domain;
58
+        this.pluginInfo = pluginInfo;
59
+        this.eventBus = eventBus;
60
+    }
61
+
62
+    public void load() {
63
+        eventBus.subscribe(this);
64
+    }
65
+
66
+    public void unload() {
67
+        eventBus.unsubscribe(this);
68
+    }
69
+
70
+    @VisibleForTesting
71
+    @Handler
72
+    void handleQueryEvent(final QueryOpenedEvent event) {
73
+        event.getQuery().getConnection().ifPresent(c -> {
74
+           final boolean enable = c.getWindowModel().getConfigManager()
75
+                   .getOptionBool(domain, "whoisonquery");
76
+            if (enable) {
77
+                c.requestUserInfo(event.getQuery().getUser());
78
+            }
79
+        });
80
+    }
81
+
82
+    @VisibleForTesting
83
+    @Handler
84
+    void handlePrefsEvent(final ClientPrefsOpenedEvent event) {
85
+        final PreferencesCategory category = new PluginPreferencesCategory(pluginInfo,
86
+                "Whois on Query", "Whois on query");
87
+        category.addSetting(getSetting(event.getModel().getConfigManager(),
88
+                event.getModel().getIdentity()));
89
+        event.getModel().getCategory("Plugins").addSubCategory(category);
90
+    }
91
+
92
+    @VisibleForTesting
93
+    @Handler
94
+    void handleConnectionPrefsEvent(final ConnectionPrefsRequestedEvent event) {
95
+        event.getCategory().addSetting(getSetting(event.getConfig(), event.getIdentity()));
96
+    }
97
+
98
+    private PreferencesSetting getSetting(final AggregateConfigProvider config,
99
+            final ConfigProvider configProvider) {
100
+        return new PreferencesSetting(PreferencesType.BOOLEAN, domain, "whoisonquery",
101
+                "Send whois on query", "Request user information when a query is opened?",
102
+                config, configProvider);
103
+    }
104
+}

whoisonquery/test/com/dmdirc/addons/jpq/WhoisOnQueryPluginTest.java → whoisonquery/src/com/dmdirc/addons/whoisonquery/WhoisOnQueryModule.java View File

@@ -20,15 +20,33 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc.addons.jpq;
23
+package com.dmdirc.addons.whoisonquery;
24 24
 
25
-import org.junit.Test;
26
-import org.junit.runner.RunWith;
27
-import org.mockito.runners.MockitoJUnitRunner;
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28 28
 
29
-@RunWith(MockitoJUnitRunner.class)
30
-public class WhoisOnQueryPluginTest {
29
+import dagger.Module;
30
+import dagger.Provides;
31 31
 
32
-    @Test
33
-    public void testSomething() {}
34
-}
32
+@Module(injects = WhoisOnQueryManager.class, addsTo = ClientModule.class)
33
+public class WhoisOnQueryModule {
34
+
35
+    private final PluginInfo pluginInfo;
36
+
37
+    public WhoisOnQueryModule(final PluginInfo pluginInfo) {
38
+        this.pluginInfo = pluginInfo;
39
+    }
40
+
41
+    @Provides
42
+    @PluginDomain(WhoisOnQueryPlugin.class)
43
+    public String getDomain() {
44
+        return pluginInfo.getDomain();
45
+    }
46
+
47
+    @Provides
48
+    @PluginDomain(WhoisOnQueryPlugin.class)
49
+    public PluginInfo getPluginInfo() {
50
+        return pluginInfo;
51
+    }
52
+}

whoisonquery/src/com/dmdirc/addons/jpq/WhoisOnQueryPlugin.java → whoisonquery/src/com/dmdirc/addons/whoisonquery/WhoisOnQueryPlugin.java View File

@@ -20,10 +20,34 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc.addons.jpq;
23
+package com.dmdirc.addons.whoisonquery;
24 24
 
25
+import com.dmdirc.plugins.PluginInfo;
25 26
 import com.dmdirc.plugins.implementations.BasePlugin;
26 27
 
28
+import dagger.ObjectGraph;
29
+
30
+/**
31
+ * Sends a whois when a new query is opened.
32
+ */
27 33
 public class WhoisOnQueryPlugin extends BasePlugin {
28 34
 
35
+    private WhoisOnQueryManager manager;
36
+
37
+    @Override
38
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
39
+        super.load(pluginInfo, graph);
40
+        setObjectGraph(graph.plus(new WhoisOnQueryModule(pluginInfo)));
41
+        manager = getObjectGraph().get(WhoisOnQueryManager.class);
42
+    }
43
+
44
+    @Override
45
+    public void onLoad() {
46
+        manager.load();
47
+    }
48
+
49
+    @Override
50
+    public void onUnload() {
51
+        manager.unload();
52
+    }
29 53
 }

+ 135
- 0
whoisonquery/test/com/dmdirc/addons/whoisonquery/WhoisOnQueryManagerTest.java View File

@@ -0,0 +1,135 @@
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.whoisonquery;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.Query;
27
+import com.dmdirc.config.prefs.PreferencesCategory;
28
+import com.dmdirc.config.prefs.PreferencesDialogModel;
29
+import com.dmdirc.config.prefs.PreferencesSetting;
30
+import com.dmdirc.events.ClientPrefsOpenedEvent;
31
+import com.dmdirc.events.ConnectionPrefsRequestedEvent;
32
+import com.dmdirc.events.QueryOpenedEvent;
33
+import com.dmdirc.interfaces.Connection;
34
+import com.dmdirc.interfaces.User;
35
+import com.dmdirc.interfaces.WindowModel;
36
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
37
+import com.dmdirc.interfaces.config.ConfigProvider;
38
+import com.dmdirc.plugins.PluginInfo;
39
+import com.dmdirc.plugins.PluginMetaData;
40
+
41
+import java.util.Optional;
42
+
43
+import org.junit.Before;
44
+import org.junit.Test;
45
+import org.junit.runner.RunWith;
46
+import org.mockito.ArgumentCaptor;
47
+import org.mockito.Captor;
48
+import org.mockito.Mock;
49
+import org.mockito.runners.MockitoJUnitRunner;
50
+
51
+import static org.mockito.Matchers.any;
52
+import static org.mockito.Mockito.never;
53
+import static org.mockito.Mockito.verify;
54
+import static org.mockito.Mockito.when;
55
+
56
+@RunWith(MockitoJUnitRunner.class)
57
+public class WhoisOnQueryManagerTest {
58
+
59
+    @Mock private DMDircMBassador eventBus;
60
+    @Mock private PluginInfo pluginInfo;
61
+    @Mock private PluginMetaData pluginMetaData;
62
+    @Mock private QueryOpenedEvent queryOpenedEvent;
63
+    @Mock private AggregateConfigProvider aggregateConfigProvider;
64
+    @Mock private Connection connection;
65
+    @Mock private WindowModel windowModel;
66
+    @Mock private Query query;
67
+    @Mock private User user;
68
+    @Mock private ClientPrefsOpenedEvent clientPrefsOpenedEvent;
69
+    @Mock private ConnectionPrefsRequestedEvent connectionPrefsRequestedEvent;
70
+    @Mock private PreferencesCategory preferencesCategory;
71
+    @Captor private ArgumentCaptor<PreferencesSetting> preferencesSetting;
72
+    @Mock private ConfigProvider configProvider;
73
+    @Mock private PreferencesDialogModel preferencesDialogModel;
74
+    @Captor private ArgumentCaptor<PreferencesCategory> preferencesCategoryArgumentCaptor;
75
+    private WhoisOnQueryManager instance;
76
+
77
+    @Before
78
+    public void setUp() throws Exception {
79
+        when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
80
+        when(pluginMetaData.getFriendlyName()).thenReturn("Plugin");
81
+        when(queryOpenedEvent.getQuery()).thenReturn(query);
82
+        when(query.getUser()).thenReturn(user);
83
+        when(query.getConnection()).thenReturn(Optional.of(connection));
84
+        when(connection.getWindowModel()).thenReturn(windowModel);
85
+        when(windowModel.getConfigManager()).thenReturn(aggregateConfigProvider);
86
+        when(connectionPrefsRequestedEvent.getConfig()).thenReturn(aggregateConfigProvider);
87
+        when(connectionPrefsRequestedEvent.getIdentity()).thenReturn(configProvider);
88
+        when(connectionPrefsRequestedEvent.getCategory()).thenReturn(preferencesCategory);
89
+        when(clientPrefsOpenedEvent.getModel()).thenReturn(preferencesDialogModel);
90
+        when(preferencesDialogModel.getIdentity()).thenReturn(configProvider);
91
+        when(preferencesDialogModel.getConfigManager()).thenReturn(aggregateConfigProvider);
92
+        when(preferencesDialogModel.getCategory("Plugins")).thenReturn(preferencesCategory);
93
+        instance = new WhoisOnQueryManager("domain", pluginInfo, eventBus);
94
+    }
95
+
96
+    @Test
97
+    public void testLoad() throws Exception {
98
+        instance.load();
99
+        verify(eventBus).subscribe(instance);
100
+    }
101
+
102
+    @Test
103
+    public void testUnload() throws Exception {
104
+        instance.unload();
105
+        verify(eventBus).unsubscribe(instance);
106
+    }
107
+
108
+    @Test
109
+    public void testHandleQueryEvent_Set() throws Exception {
110
+        when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
111
+        instance.handleQueryEvent(queryOpenedEvent);
112
+        verify(connection).requestUserInfo(user);
113
+    }
114
+
115
+    @Test
116
+    public void testHandleQueryEvent_Unset() throws Exception {
117
+        when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(false);
118
+        instance.handleQueryEvent(queryOpenedEvent);
119
+        verify(connection, never()).requestUserInfo(user);
120
+    }
121
+
122
+    @Test
123
+    public void testHandlePrefsEvent() throws Exception {
124
+        when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
125
+        instance.handlePrefsEvent(clientPrefsOpenedEvent);
126
+        verify(preferencesCategory).addSubCategory(preferencesCategoryArgumentCaptor.capture());
127
+    }
128
+
129
+    @Test
130
+    public void testHandleConnectionPrefsEvent() throws Exception {
131
+        when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
132
+        instance.handleConnectionPrefsEvent(connectionPrefsRequestedEvent);
133
+        verify(preferencesCategory).addSetting(any());
134
+    }
135
+}

+ 67
- 0
whoisonquery/test/com/dmdirc/addons/whoisonquery/WhoisOnQueryPluginTest.java View File

@@ -0,0 +1,67 @@
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.whoisonquery;
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 WhoisOnQueryPluginTest {
41
+
42
+    @Mock private PluginInfo pluginInfo;
43
+    @Mock private ObjectGraph objectGraph;
44
+    @Mock private WhoisOnQueryManager whoisOnQueryManager;
45
+    private WhoisOnQueryPlugin instance;
46
+
47
+    @Before
48
+    public void setup() {
49
+        when(objectGraph.plus(any())).thenReturn(objectGraph);
50
+        when(objectGraph.get(any())).thenReturn(whoisOnQueryManager);
51
+        instance = new WhoisOnQueryPlugin();
52
+    }
53
+
54
+    @Test
55
+    public void testOnLoad() throws Exception {
56
+        instance.load(pluginInfo, objectGraph);
57
+        instance.onLoad();
58
+        verify(whoisOnQueryManager).load();
59
+    }
60
+
61
+    @Test
62
+    public void testOnUnload() throws Exception {
63
+        instance.load(pluginInfo, objectGraph);
64
+        instance.onUnload();
65
+        verify(whoisOnQueryManager).unload();
66
+    }
67
+}

Loading…
Cancel
Save