Procházet zdrojové kódy

Add standalone handlers for NickInUse and PingFailure.

pull/55/head
Greg Holmes před 9 roky
rodič
revize
e69f72c4c8

+ 1
- 4
irc/src/com/dmdirc/parser/irc/IRCParser.java Zobrazit soubor

@@ -154,8 +154,6 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
154 154
     public String networkName;
155 155
     /** This is what we think the nickname should be. */
156 156
     public String thinkNickname;
157
-    /** When using inbuilt pre-001 NickInUse handler, have we tried our AltNick. */
158
-    public boolean triedAlt;
159 157
     /** Have we received the 001. */
160 158
     public boolean got001;
161 159
     /** Have we fired post005? */
@@ -671,7 +669,6 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
671 669
     /** Reset internal state (use before doConnect). */
672 670
     private void resetState() {
673 671
         // Reset General State info
674
-        triedAlt = false;
675 672
         got001 = false;
676 673
         post005 = false;
677 674
         // Clear the hash tables
@@ -1984,7 +1981,7 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
1984 1981
      *
1985 1982
      * @return value of pingNeeded.
1986 1983
      */
1987
-    private boolean getPingNeeded() {
1984
+    boolean getPingNeeded() {
1988 1985
         return pingNeeded.get();
1989 1986
     }
1990 1987
 

+ 72
- 0
irc/src/com/dmdirc/parser/irc/SimpleNickInUseHandler.java Zobrazit soubor

@@ -0,0 +1,72 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.interfaces.Parser;
26
+import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
27
+import com.dmdirc.parser.interfaces.callbacks.NickInUseListener;
28
+
29
+import java.util.Date;
30
+
31
+/**
32
+ * Simple nick in use handler that tries the alternative nickname, then prepends a character until
33
+ * it gets a nickname.
34
+ */
35
+public class SimpleNickInUseHandler implements NickInUseListener {
36
+
37
+    private final String altNickname;
38
+    private final char prependChar;
39
+    private boolean triedAlt;
40
+
41
+    public SimpleNickInUseHandler(final String altNickname, final char prependChar) {
42
+        this.altNickname = altNickname;
43
+        this.prependChar = prependChar;
44
+    }
45
+
46
+    @Override
47
+    public void onNickInUse(final Parser parser, final Date date, final String nickname) {
48
+        final IRCParser ircParser = (IRCParser) parser;
49
+        callDebugInfo(parser, IRCParser.DEBUG_INFO, "No Nick in use Handler.");
50
+        if (!ircParser.got001) {
51
+            callDebugInfo(parser, IRCParser.DEBUG_INFO, "Using inbuilt handler");
52
+            // If this is before 001 we will try and get a nickname, else we will leave the
53
+            // nick as-is
54
+            if (triedAlt) {
55
+                final String magicAltNick = prependChar + ircParser.getMyInfo().getNickname();
56
+                if (parser.getStringConverter().equalsIgnoreCase(ircParser.thinkNickname, altNickname)
57
+                        && !altNickname.equalsIgnoreCase(magicAltNick)) {
58
+                    ircParser.thinkNickname = ircParser.getMyInfo().getNickname();
59
+                }
60
+                parser.getLocalClient().setNickname(prependChar + ircParser.thinkNickname);
61
+            } else {
62
+                parser.getLocalClient().setNickname(altNickname);
63
+                triedAlt = true;
64
+            }
65
+        }
66
+    }
67
+
68
+    private void callDebugInfo(final Parser parser, final int level, final String data) {
69
+        parser.getCallbackManager().getCallback(DebugInfoListener.class)
70
+                .onDebugInfo(parser, new Date(), level, data);
71
+    }
72
+}

+ 43
- 0
irc/src/com/dmdirc/parser/irc/SimplePingFailureHandler.java Zobrazit soubor

@@ -0,0 +1,43 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.interfaces.Parser;
26
+import com.dmdirc.parser.interfaces.callbacks.PingFailureListener;
27
+
28
+import java.util.Date;
29
+
30
+/**
31
+ * Simple ping failure listener that disconnects on missed pings.
32
+ */
33
+public class SimplePingFailureHandler implements PingFailureListener {
34
+
35
+    @Override
36
+    public void onPingFailed(final Parser parser, final Date date) {
37
+        final IRCParser ircParser = (IRCParser) parser;
38
+        if (ircParser.getPingNeeded()) {
39
+            ircParser.stopPingTimer();
40
+            ircParser.disconnect("Server not responding");
41
+        }
42
+    }
43
+}

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