Procházet zdrojové kódy

Add whois support to the q auth plugin.

pull/405/head
Greg Holmes před 9 roky
rodič
revize
3eabed5b01

+ 35
- 2
qauth/src/com/dmdirc/addons/qauth/QAuthManager.java Zobrazit soubor

@@ -35,6 +35,7 @@ import com.dmdirc.events.QueryMessageEvent;
35 35
 import com.dmdirc.events.ServerConnectedEvent;
36 36
 import com.dmdirc.events.ServerInviteReceivedEvent;
37 37
 import com.dmdirc.events.ServerNoticeEvent;
38
+import com.dmdirc.events.ServerNumericEvent;
38 39
 import com.dmdirc.interfaces.Connection;
39 40
 import com.dmdirc.interfaces.User;
40 41
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
@@ -62,6 +63,8 @@ public class QAuthManager {
62 63
     private boolean whois;
63 64
     private boolean autoInvite;
64 65
     private boolean acceptInvites;
66
+    private boolean waitingWhois;
67
+    private boolean authed;
65 68
 
66 69
     @Inject
67 70
     public QAuthManager(
@@ -106,13 +109,43 @@ public class QAuthManager {
106 109
         connection.sendMessage("Q@Cserve.quakenet.org", "auth " + username + ' ' + password);
107 110
     }
108 111
 
112
+    private void sendWhois(final Connection connection) {
113
+        connection.getLocalUser().ifPresent(u -> {
114
+            connection.requestUserInfo(u);
115
+            waitingWhois = true;
116
+            authed = false;
117
+        });
118
+    }
119
+
109 120
     @Handler
110 121
     void handleConnect(final ServerConnectedEvent event) {
111 122
         if (!isValidConnection(event.getConnection())) {
112 123
             return;
113 124
         }
114
-        // TODO: whois
115
-        auth(event.getConnection());
125
+        if (whois) {
126
+            sendWhois(event.getConnection());
127
+        } else {
128
+            auth(event.getConnection());
129
+        }
130
+    }
131
+
132
+    @Handler
133
+    void handleServerNumericEvent(final ServerNumericEvent event) {
134
+        if (!waitingWhois) {
135
+            return;
136
+        }
137
+        if(event.getNumeric() == 330 && event.getConnection().getLocalUser().map(User::getNickname)
138
+                .orElse("").equalsIgnoreCase(event.getArgs()[3])) {
139
+            // TODO: Check account matches? (param arg 4)
140
+            authed = true;
141
+        }
142
+        if (event.getNumeric() == 318 && event.getConnection().getLocalUser().map(User::getNickname)
143
+                .orElse("").equalsIgnoreCase(event.getArgs()[3])) {
144
+            waitingWhois = false;
145
+            if (!authed) {
146
+                auth(event.getConnection());
147
+            }
148
+        }
116 149
     }
117 150
 
118 151
     @Handler

+ 49
- 0
qauth/test/com/dmdirc/addons/qauth/QAuthManagerTest.java Zobrazit soubor

@@ -26,6 +26,9 @@ import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.Invite;
27 27
 import com.dmdirc.Query;
28 28
 import com.dmdirc.config.ConfigBinder;
29
+import com.dmdirc.config.prefs.PreferencesCategory;
30
+import com.dmdirc.config.prefs.PreferencesDialogModel;
31
+import com.dmdirc.events.ClientPrefsOpenedEvent;
29 32
 import com.dmdirc.events.QueryMessageEvent;
30 33
 import com.dmdirc.events.ServerConnectedEvent;
31 34
 import com.dmdirc.events.ServerInviteReceivedEvent;
@@ -33,16 +36,21 @@ import com.dmdirc.events.ServerNoticeEvent;
33 36
 import com.dmdirc.interfaces.Connection;
34 37
 import com.dmdirc.interfaces.User;
35 38
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
39
+import com.dmdirc.interfaces.config.ConfigProvider;
36 40
 import com.dmdirc.plugins.PluginInfo;
41
+import com.dmdirc.plugins.PluginMetaData;
37 42
 
38 43
 import java.util.Optional;
39 44
 
40 45
 import org.junit.Before;
41 46
 import org.junit.Test;
42 47
 import org.junit.runner.RunWith;
48
+import org.mockito.ArgumentCaptor;
49
+import org.mockito.Captor;
43 50
 import org.mockito.Mock;
44 51
 import org.mockito.runners.MockitoJUnitRunner;
45 52
 
53
+import static org.junit.Assert.assertTrue;
46 54
 import static org.mockito.Matchers.anyString;
47 55
 import static org.mockito.Mockito.never;
48 56
 import static org.mockito.Mockito.verify;
@@ -52,18 +60,26 @@ import static org.mockito.Mockito.when;
52 60
 @RunWith(MockitoJUnitRunner.class)
53 61
 public class QAuthManagerTest {
54 62
 
63
+    @Mock private AggregateConfigProvider aggregateConfigProvider;
64
+    @Mock private ConfigProvider configProvider;
65
+    @Mock private PreferencesDialogModel preferencesDialogModel;
55 66
     @Mock private DMDircMBassador eventBus;
56 67
     @Mock private PluginInfo pluginInfo;
68
+    @Mock private PluginMetaData pluginMetaData;
57 69
     @Mock private AggregateConfigProvider config;
58 70
     @Mock private ConfigBinder configBinder;
59 71
     @Mock private ServerConnectedEvent serverConnectedEvent;
60 72
     @Mock private ServerInviteReceivedEvent serverInviteReceivedEvent;
61 73
     @Mock private ServerNoticeEvent serverNoticeEvent;
62 74
     @Mock private QueryMessageEvent queryMessageEvent;
75
+    @Mock private ClientPrefsOpenedEvent clientPrefsOpenedEvent;
63 76
     @Mock private User user;
77
+    @Mock private User localUser;
64 78
     @Mock private Query query;
65 79
     @Mock private Invite invite;
66 80
     @Mock private Connection connection;
81
+    @Mock private PreferencesCategory preferencesCategory;
82
+    @Captor private ArgumentCaptor<PreferencesCategory> preferencesCategoryArgumentCaptor;
67 83
     private QAuthManager instance;
68 84
 
69 85
     @Before
@@ -82,8 +98,15 @@ public class QAuthManagerTest {
82 98
         when(user.getNickname()).thenReturn("Q");
83 99
         when(query.getConnection()).thenReturn(Optional.of(connection));
84 100
         when(connection.getNetwork()).thenReturn("Quakenet");
101
+        when(connection.getLocalUser()).thenReturn(Optional.of(localUser));
85 102
         when(config.getBinder()).thenReturn(configBinder);
86 103
         when(configBinder.withDefaultDomain("pluginDomain")).thenReturn(configBinder);
104
+        when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
105
+        when(pluginMetaData.getFriendlyName()).thenReturn("Plugin");
106
+        when(clientPrefsOpenedEvent.getModel()).thenReturn(preferencesDialogModel);
107
+        when(preferencesDialogModel.getConfigManager()).thenReturn(aggregateConfigProvider);
108
+        when(preferencesDialogModel.getIdentity()).thenReturn(configProvider);
109
+        when(preferencesDialogModel.getCategory("Plugins")).thenReturn(preferencesCategory);
87 110
         instance = new QAuthManager("pluginDomain", pluginInfo, config, eventBus);
88 111
 
89 112
         instance.handleUsername("username");
@@ -208,4 +231,30 @@ public class QAuthManagerTest {
208 231
         instance.handleNotices(serverNoticeEvent);
209 232
         verify(connection, never()).sendMessage("Q@Cserve.quakenet.org", "invite");
210 233
     }
234
+
235
+    @Test
236
+    public void testAuthWithWhois() {
237
+        instance.handleWhois(true);
238
+        instance.handleConnect(serverConnectedEvent);
239
+        verify(connection).requestUserInfo(localUser);
240
+        verify(connection, never()).sendMessage(anyString(), anyString());
241
+    }
242
+
243
+    @Test
244
+    public void testAuthWithoutWhois() {
245
+        instance.handleWhois(false);
246
+        instance.handleConnect(serverConnectedEvent);
247
+        verify(connection, never()).requestUserInfo(localUser);
248
+        verify(connection).sendMessage(anyString(), anyString());
249
+    }
250
+
251
+    @Test
252
+    public void testHandlePrefsEvent() throws Exception {
253
+        when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
254
+        instance.showConfig(clientPrefsOpenedEvent);
255
+        verify(preferencesCategory).addSubCategory(preferencesCategoryArgumentCaptor.capture());
256
+        assertTrue(preferencesCategoryArgumentCaptor.getValue().getSettings().size() > 1);
257
+    }
258
+
259
+    // TODO: Test ServerNumericEvent method
211 260
 }

Načítá se…
Zrušit
Uložit