Ver código fonte

Add a whois reponse handler.

Just does ircu stuff for now.
pull/84/head
Chris Smith 9 anos atrás
pai
commit
435e79577a

+ 8
- 0
irc/src/com/dmdirc/parser/irc/IRCParser.java Ver arquivo

@@ -248,6 +248,8 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
248 248
     private final List<String> serverInformationLines = new LinkedList<>();
249 249
     /** Map of capabilities and their state. */
250 250
     private final Map<String, CapabilityState> capabilities = new HashMap<>();
251
+    /** Handler for whois responses. */
252
+    private final WhoisResponseHandler whoisHandler;
251 253
 
252 254
     /**
253 255
      * Default constructor, ServerInfo and MyInfo need to be added separately (using IRC.me and IRC.server).
@@ -296,10 +298,13 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
296 298
             this.me = myDetails;
297 299
         }
298 300
 
301
+        this.whoisHandler = new WhoisResponseHandler(this, getCallbackManager());
302
+
299 303
         setIgnoreList(new IgnoreList());
300 304
         setPingTimerInterval(10000);
301 305
         setPingTimerFraction(6);
302 306
         resetState();
307
+
303 308
     }
304 309
 
305 310
     /**
@@ -648,6 +653,7 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
648 653
             parseChanModes();
649 654
         }
650 655
 
656
+        whoisHandler.start();
651 657
         getCallbackManager().publish(new ServerReadyEvent(this, new Date()));
652 658
     }
653 659
 
@@ -684,6 +690,8 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
684 690
 
685 691
         currentSocketState = SocketState.CLOSED;
686 692
         setEncoding(IRCEncoding.RFC1459);
693
+
694
+        whoisHandler.stop();
687 695
     }
688 696
 
689 697
     /**

+ 119
- 0
irc/src/com/dmdirc/parser/irc/WhoisResponseHandler.java Ver arquivo

@@ -0,0 +1,119 @@
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.parser.irc;
24
+
25
+import com.dmdirc.parser.common.CallbackManager;
26
+import com.dmdirc.parser.events.NumericEvent;
27
+import com.dmdirc.parser.events.UserInfoEvent;
28
+import com.dmdirc.parser.events.UserInfoEvent.UserInfoType;
29
+import com.dmdirc.parser.interfaces.Parser;
30
+
31
+import java.util.Date;
32
+import java.util.EnumMap;
33
+import java.util.Map;
34
+
35
+import javax.annotation.Nullable;
36
+
37
+import net.engio.mbassy.listener.Handler;
38
+
39
+/**
40
+ * Monitors for whois responses and raises a {@link UserInfoEvent} with the results.
41
+ */
42
+public class WhoisResponseHandler {
43
+
44
+    private final Parser parser;
45
+    private final CallbackManager manager;
46
+
47
+    private final Map<UserInfoType, String> info = new EnumMap<>(UserInfoType.class);
48
+
49
+    @Nullable private String client;
50
+
51
+    public WhoisResponseHandler(final Parser parser, final CallbackManager manager) {
52
+        this.parser = parser;
53
+        this.manager = manager;
54
+    }
55
+
56
+    public void start() {
57
+        manager.subscribe(this);
58
+    }
59
+
60
+    public void stop() {
61
+        manager.unsubscribe(this);
62
+    }
63
+
64
+    @Handler
65
+    void handleNumericEvent(final NumericEvent event) {
66
+        if (event.getNumeric() == 311) {
67
+            // RPL_WHOISUSER
68
+            client = event.getToken()[3];
69
+            info.clear();
70
+        }
71
+
72
+        if (event.getNumeric() == 318 && client != null) {
73
+            // RPL_ENDOFWHOIS
74
+            sendEvent();
75
+            client = null;
76
+        }
77
+
78
+        if (client != null && event.getToken().length > 4 && event.getToken()[3].equals(client)) {
79
+            handleWhoisResponse(event.getNumeric(), event.getToken());
80
+        }
81
+    }
82
+
83
+    private void handleWhoisResponse(final int numeric, final String... tokens) {
84
+        switch (numeric) {
85
+            case 311:
86
+                // :server 311 DMDirc User ~Ident host.dmdirc.com * :Real name
87
+                info.put(UserInfoType.ADDRESS, tokens[3] + '!' + tokens[4] + '@' + tokens[5]);
88
+                info.put(UserInfoType.REAL_NAME, tokens[7]);
89
+                break;
90
+
91
+            case 319:
92
+                // :server 319 DMDirc User :@#channel1 +#channel2 ...
93
+                info.put(UserInfoType.GROUP_CHAT_LIST, tokens[4]);
94
+                break;
95
+
96
+            case 312:
97
+                // :server 312 DMDirc User *.quakenet.org :QuakeNet IRC Server
98
+                info.put(UserInfoType.SERVER_NAME, tokens[4]);
99
+                info.put(UserInfoType.SERVER_INFO, tokens[5]);
100
+                break;
101
+
102
+            case 330:
103
+                // :server 330 DMDirc User Account :is authed as
104
+                info.put(UserInfoType.ACCOUNT_NAME, tokens[4]);
105
+                break;
106
+
107
+            case 317:
108
+                // :server 317 DMDirc User 305 1422561556 :seconds idle, signon time
109
+                info.put(UserInfoType.IDLE_TIME, tokens[4]);
110
+                info.put(UserInfoType.CONNECTION_TIME, tokens[5]);
111
+                break;
112
+        }
113
+    }
114
+
115
+    private void sendEvent() {
116
+        manager.publish(new UserInfoEvent(parser, new Date(), parser.getClient(client), info));
117
+    }
118
+
119
+}

Carregando…
Cancelar
Salvar