Преглед на файлове

Tidy up the 004/005 processor a bit.

Introduce methods for handling individual bits, instead of having
a pair of giant if statements of doom.

Tidy some comments and formatting up at the same time.

This is functionally the same as it was before.

Change-Id: I83f09cb6c36be203d102c44188f90de2b0587e58
Reviewed-on: http://gerrit.dmdirc.com/2812
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8
Chris Smith преди 10 години
родител
ревизия
dac7f08707
променени са 1 файла, в които са добавени 244 реда и са изтрити 109 реда
  1. 244
    109
      src/com/dmdirc/parser/irc/Process004005.java

+ 244
- 109
src/com/dmdirc/parser/irc/Process004005.java Целия файл

@@ -52,111 +52,19 @@ public class Process004005 extends IRCProcessor {
52 52
      */
53 53
     @Override
54 54
     public void process(final String sParam, final String[] token) {
55
-        if ("002".equals(sParam)) {
56
-            final Pattern pattern = Pattern.compile("running(?: version)? (.*)$");
57
-            final Matcher matcher = pattern.matcher(parser.getLastLine());
58
-            if (matcher.find()) {
59
-                parser.h005Info.put("002IRCD", matcher.group(1));
60
-            }
61
-        } else if ("003".equals(sParam)) {
62
-            parser.h005Info.put("003IRCD", token[token.length - 1]);
63
-        } else if ("004".equals(sParam)) {
64
-            // 004
65
-            final boolean multiParam = token.length > 4;
66
-            int i = multiParam ? 4 : 1;
67
-            final String[] bits = multiParam ? token : token[3].split(" ");
68
-
69
-            parser.h005Info.put("004IRCD", bits[i++]);
70
-
71
-            if (bits[i].matches("^\\d+$")) {
72
-                // some IRCDs put a timestamp where the usermodes should be
73
-                // (issues 4140. 4181 and 4183) so check to see if this is
74
-                // numeric only, and if so, skip it.
75
-                i++;
76
-            }
77
-
78
-            parser.h005Info.put("USERMODES", bits[i++]);
79
-            parser.h005Info.put("USERCHANMODES", bits[i++]);
80
-
81
-            if (bits.length > i) {
82
-                // INSPIRCD includes an extra param
83
-                parser.h005Info.put("USERCHANPARAMMODES", bits[i++]);
84
-            }
85
-
86
-            parser.parseUserModes();
87
-        } else if ("005".equals(sParam)) {
88
-            for (int i = 3; i < token.length; i++) {
89
-                final String[] bits = token[i].split("=", 2);
90
-
91
-                if (bits[0].isEmpty()) {
92
-                    continue;
93
-                }
94
-
95
-                final boolean isNegation = bits[0].charAt(0) == '-';
96
-                final String key = (isNegation ? bits[0].substring(1) : bits[0]).toUpperCase();
97
-                final String value = bits.length == 2 ? bits[1] : "";
98
-
99
-                callDebugInfo(IRCParser.DEBUG_INFO, "%s => %s", key, value);
100
-
101
-                if (isNegation) {
102
-                    parser.h005Info.remove(key);
103
-                } else {
104
-                    parser.h005Info.put(key, value);
105
-                }
106
-
107
-                if ("NETWORK".equals(key) && !isNegation) {
108
-                    parser.networkName = value;
109
-                    callGotNetwork();
110
-                } else if ("CASEMAPPING".equals(key) && !isNegation) {
111
-                    IRCEncoding encoding = IRCEncoding.RFC1459;
112
-
113
-                    try {
114
-                        encoding = IRCEncoding.valueOf(value.toUpperCase().replace('-', '_'));
115
-                    } catch (IllegalArgumentException ex) {
116
-                        parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Unknown casemapping: '" + value + "' - assuming rfc1459", parser.getLastLine()));
117
-                    }
118
-
119
-                    final boolean encodingChanged = parser.getStringConverter().getEncoding() != encoding;
120
-                    parser.setEncoding(encoding);
121
-
122
-                    if (encodingChanged && parser.knownClients() == 1) {
123
-                        // This means that the casemapping is not rfc1459
124
-                        // We have only added ourselves so far (from 001)
125
-                        // We can fix the hashtable easily.
126
-                        parser.removeClient(parser.getLocalClient());
127
-                        parser.addClient(parser.getLocalClient());
128
-                    }
129
-                } else if ("CHANTYPES".equals(key)) {
130
-                    parser.parseChanPrefix();
131
-                } else if ("PREFIX".equals(key)) {
132
-                    parser.parsePrefixModes();
133
-                } else if ("CHANMODES".equals(key)) {
134
-                    parser.parseChanModes();
135
-                } else if ("NAMESX".equals(key) && parser.getCapabilityState("multi-prefix") != CapabilityState.ENABLED) {
136
-                    parser.sendString("PROTOCTL " + key);
137
-                } else if ("UHNAMES".equals(key) && parser.getCapabilityState("userhost-in-names") != CapabilityState.ENABLED) {
138
-                    parser.sendString("PROTOCTL " + key);
139
-                } else if ("LISTMODE".equals(key)) {
140
-                    // Support for potential future decent mode listing in the protocol
141
-                    //
142
-                    // See my proposal: http://shanemcc.co.uk/irc/#listmode
143
-                    // Add listmode handler
144
-                    String[] handles = new String[2];
145
-                    handles[0] = value; // List mode item
146
-                    final String endValue = Integer.toString(Integer.parseInt(value) + 1);
147
-                    parser.h005Info.put("LISTMODEEND", endValue);
148
-                    handles[1] = endValue; // List mode end
149
-                    // Add listmode handlers
150
-                    try {
151
-                        parser.getProcessingManager().addProcessor(handles, parser.getProcessingManager().getProcessor("__LISTMODE__"));
152
-                    } catch (ProcessorNotFoundException e) {
153
-                    }
154
-                } else if ("TIMESTAMPEDIRC".equals(key) && parser.getCapabilityState("dfbnc.com/tsirc") != CapabilityState.ENABLED) {
155
-                    // Let the server know we also understand timestamped irc.
156
-                    // See my other proposal: http://shanemcc.co.uk/irc/#timestamping
157
-                    parser.sendString("TIMESTAMPEDIRC ON", QueuePriority.HIGH);
158
-                }
159
-            }
55
+        switch (sParam) {
56
+            case "002":
57
+                process002();
58
+                break;
59
+            case "003":
60
+                process003(token);
61
+                break;
62
+            case "004":
63
+                process004(token);
64
+                break;
65
+            case "005":
66
+                process005(token);
67
+                break;
160 68
         }
161 69
     }
162 70
 
@@ -174,14 +82,241 @@ public class Process004005 extends IRCProcessor {
174 82
      * Callback to all objects implementing the GotNetwork Callback.
175 83
      * This takes no params of its own, but works them out itself.
176 84
      *
177
-     * @see IGotNetwork
178
-     * @return true if a method was called, false otherwise
85
+     * @see NetworkDetectedListener
179 86
      */
180
-    protected boolean callGotNetwork() {
87
+    private void callGotNetwork() {
181 88
         final String networkName = parser.networkName;
182 89
         final String ircdVersion = parser.getServerSoftware();
183 90
         final String ircdType = parser.getServerSoftwareType();
184 91
 
185
-        return getCallbackManager().getCallbackType(NetworkDetectedListener.class).call(networkName, ircdVersion, ircdType);
92
+        getCallbackManager().getCallbackType(NetworkDetectedListener.class)
93
+                .call(networkName, ircdVersion, ircdType);
94
+    }
95
+
96
+    /**
97
+     * Processes a 002 line.
98
+     */
99
+    private void process002() {
100
+        final Pattern pattern = Pattern.compile("running(?: version)? (.*)$");
101
+        final Matcher matcher = pattern.matcher(parser.getLastLine());
102
+        if (matcher.find()) {
103
+            parser.h005Info.put("002IRCD", matcher.group(1));
104
+        }
105
+    }
106
+
107
+    /**
108
+     * Processes a 003 line.
109
+     *
110
+     * @param token The tokenised line.
111
+     */
112
+    private void process003(final String[] token) {
113
+        parser.h005Info.put("003IRCD", token[token.length - 1]);
114
+    }
115
+
116
+    /**
117
+     * Processes a 004 line.
118
+     *
119
+     * @param token The tokenised line.
120
+     */
121
+    private void process004(final String[] token) {
122
+        final boolean multiParam = token.length > 4;
123
+        int i = multiParam ? 4 : 1;
124
+        final String[] bits = multiParam ? token : token[3].split(" ");
125
+
126
+        parser.h005Info.put("004IRCD", bits[i++]);
127
+
128
+        if (bits[i].matches("^\\d+$")) {
129
+            // some IRCDs put a timestamp where the usermodes should be
130
+            // (issues 4140. 4181 and 4183) so check to see if this is
131
+            // numeric only, and if so, skip it.
132
+            i++;
133
+        }
134
+
135
+        parser.h005Info.put("USERMODES", bits[i++]);
136
+        parser.h005Info.put("USERCHANMODES", bits[i++]);
137
+
138
+        if (bits.length > i) {
139
+            // INSPIRCD includes an extra param
140
+            parser.h005Info.put("USERCHANPARAMMODES", bits[i++]);
141
+        }
142
+
143
+        parser.parseUserModes();
144
+    }
145
+
146
+    /**
147
+     * Processes a 005 line.
148
+     *
149
+     * @param token The tokenised line.
150
+     */
151
+    private void process005(final String[] token) {
152
+        for (int i = 3; i < token.length; i++) {
153
+            final String[] bits = token[i].split("=", 2);
154
+
155
+            if (bits[0].isEmpty()) {
156
+                continue;
157
+            }
158
+
159
+            final boolean isNegation = bits[0].charAt(0) == '-';
160
+            final String key = (isNegation ? bits[0].substring(1) : bits[0]).toUpperCase();
161
+            final String value = bits.length == 2 ? bits[1] : "";
162
+
163
+            callDebugInfo(IRCParser.DEBUG_INFO, "%s => %s", key, value);
164
+
165
+            if (isNegation) {
166
+                parser.h005Info.remove(key);
167
+            } else {
168
+                parser.h005Info.put(key, value);
169
+            }
170
+
171
+            switch (key) {
172
+                case "NETWORK":
173
+                    processNetworkToken(isNegation, value);
174
+                    break;
175
+                case "CASEMAPPING":
176
+                    processCaseMappingToken(isNegation, value);
177
+                    break;
178
+                case "CHANTYPES":
179
+                    processChanTypesToken();
180
+                    break;
181
+                case "PREFIX":
182
+                    processPrefixToken();
183
+                    break;
184
+                case "CHANMODES":
185
+                    processChanModesToken();
186
+                    break;
187
+                case "NAMESX":
188
+                    processNamesxToken(key);
189
+                    break;
190
+                case "UHNAMES":
191
+                    processUhnamesToken(key);
192
+                    break;
193
+                case "LISTMODE":
194
+                    processListmodeToken(value);
195
+                    break;
196
+                case "TIMESTAMPEDIRC":
197
+                    processTimestampedIrcToken();
198
+                    break;
199
+            }
200
+        }
201
+    }
202
+
203
+    /**
204
+     * Processes a 'NETWORK' token received in a 005.
205
+     *
206
+     * @param isNegation Whether the token was negated or not.
207
+     * @param value The value of the token.
208
+     */
209
+    private void processNetworkToken(final boolean isNegation, final String value) {
210
+        if (!isNegation) {
211
+            parser.networkName = value;
212
+            callGotNetwork();
213
+        }
214
+    }
215
+
216
+    /**
217
+     * Processes a 'CASEMAPPING' token received in a 005.
218
+     *
219
+     * @param isNegation Whether the token was negated or not.
220
+     * @param value The value of the token.
221
+     */
222
+    private void processCaseMappingToken(final boolean isNegation, final String value) {
223
+        if (!isNegation) {
224
+            IRCEncoding encoding = IRCEncoding.RFC1459;
225
+
226
+            try {
227
+                encoding = IRCEncoding.valueOf(value.toUpperCase().replace('-', '_'));
228
+            } catch (IllegalArgumentException ex) {
229
+                parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING,
230
+                        "Unknown casemapping: '" + value + "' - assuming rfc1459",
231
+                        parser.getLastLine()));
232
+            }
233
+
234
+            final boolean encodingChanged = parser.getStringConverter().getEncoding() != encoding;
235
+            parser.setEncoding(encoding);
236
+
237
+            if (encodingChanged && parser.knownClients() == 1) {
238
+                // This means that the casemapping is not rfc1459
239
+                // We have only added ourselves so far (from 001)
240
+                // We can fix the hashtable easily.
241
+                parser.removeClient(parser.getLocalClient());
242
+                parser.addClient(parser.getLocalClient());
243
+            }
244
+        }
245
+    }
246
+
247
+    /**
248
+     * Processes a 'CHANTYPES' token received in a 005.
249
+     */
250
+    private void processChanTypesToken() {
251
+        parser.parseChanPrefix();
252
+    }
253
+
254
+    /**
255
+     * Processes a 'PREFIX' token received in a 005.
256
+     */
257
+    private void processPrefixToken() {
258
+        parser.parsePrefixModes();
259
+    }
260
+
261
+    /**
262
+     * Processes a 'CHANMODES' token received in a 005.
263
+     */
264
+    private void processChanModesToken() {
265
+        parser.parseChanModes();
266
+    }
267
+
268
+    /**
269
+     * Processes a 'NAMEX' token received in a 005.
270
+     *
271
+     * @param key The NAMEX token that was received.
272
+     */
273
+    private void processNamesxToken(final String key) {
274
+        if (parser.getCapabilityState("multi-prefix") != CapabilityState.ENABLED) {
275
+            parser.sendString("PROTOCTL " + key);
276
+        }
277
+    }
278
+
279
+    /**
280
+     * Processes a 'UHNAMES' token received in a 005.
281
+     *
282
+     * @param key The UHNAMES token that was received.
283
+     */
284
+    private void processUhnamesToken(final String key) {
285
+        if (parser.getCapabilityState("userhost-in-names") != CapabilityState.ENABLED) {
286
+            parser.sendString("PROTOCTL " + key);
287
+        }
288
+    }
289
+
290
+    /**
291
+     * Processes a 'LISTMODE' token received in a 005. The LISTMODE token
292
+     * indicates support for a new way of describing list modes (such as +b).
293
+     * See the proposal at {@link http://shanemcc.co.uk/irc/#listmode}.
294
+     *
295
+     * @param value The value of the token.
296
+     */
297
+    private void processListmodeToken(final String value) {
298
+        String[] handles = new String[2];
299
+        handles[0] = value; // List mode item
300
+        final String endValue = Integer.toString(Integer.parseInt(value) + 1);
301
+        parser.h005Info.put("LISTMODEEND", endValue);
302
+        handles[1] = endValue; // List mode end
303
+        try {
304
+            parser.getProcessingManager().addProcessor(handles,
305
+                    parser.getProcessingManager().getProcessor("__LISTMODE__"));
306
+        } catch (ProcessorNotFoundException e) {
307
+        }
308
+    }
309
+
310
+    /**
311
+     * Processes a 'TIMESTAMPEDIRC' token received in a 005. The TIMESTAMPEDIRC
312
+     * protocol allows servers (or proxies) to send historical events with a
313
+     * corresponding timestamp, allowing the client to catch up on events that
314
+     * happened prior to them connecting. See the proposal at
315
+     * {@link http://shanemcc.co.uk/irc/#timestamping}.
316
+     */
317
+    private void processTimestampedIrcToken() {
318
+        if (parser.getCapabilityState("dfbnc.com/tsirc") != CapabilityState.ENABLED) {
319
+            parser.sendString("TIMESTAMPEDIRC ON", QueuePriority.HIGH);
320
+        }
186 321
     }
187 322
 }

Loading…
Отказ
Запис