|
@@ -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
|
+}
|