|
@@ -0,0 +1,84 @@
|
|
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
|
+ @SuppressWarnings("TypeMayBeWeakened")
|
|
38
|
+ public static void install(final IRCParser parser, final String altNickname) {
|
|
39
|
+ install(parser, altNickname, '_');
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ @SuppressWarnings("TypeMayBeWeakened")
|
|
43
|
+ public static void install(final IRCParser parser, final String altNickname, final char prependChar) {
|
|
44
|
+ parser.getCallbackManager()
|
|
45
|
+ .addCallback(NickInUseListener.class, new SimpleNickInUseHandler(altNickname, prependChar));
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ private final String altNickname;
|
|
49
|
+ private final char prependChar;
|
|
50
|
+ private boolean triedAlt;
|
|
51
|
+
|
|
52
|
+ public SimpleNickInUseHandler(final String altNickname, final char prependChar) {
|
|
53
|
+ this.altNickname = altNickname;
|
|
54
|
+ this.prependChar = prependChar;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ @Override
|
|
58
|
+ public void onNickInUse(final Parser parser, final Date date, final String nickname) {
|
|
59
|
+ final IRCParser ircParser = (IRCParser) parser;
|
|
60
|
+ callDebugInfo(parser, IRCParser.DEBUG_INFO, "No Nick in use Handler.");
|
|
61
|
+ if (!ircParser.got001) {
|
|
62
|
+ callDebugInfo(parser, IRCParser.DEBUG_INFO, "Using inbuilt handler");
|
|
63
|
+ // If this is before 001 we will try and get a nickname, else we will leave the
|
|
64
|
+ // nick as-is
|
|
65
|
+ if (triedAlt) {
|
|
66
|
+ final String magicAltNick = prependChar + ircParser.getMyInfo().getNickname();
|
|
67
|
+ if (parser.getStringConverter().equalsIgnoreCase(ircParser.thinkNickname,
|
|
68
|
+ altNickname)
|
|
69
|
+ && !altNickname.equalsIgnoreCase(magicAltNick)) {
|
|
70
|
+ ircParser.thinkNickname = ircParser.getMyInfo().getNickname();
|
|
71
|
+ }
|
|
72
|
+ parser.getLocalClient().setNickname(prependChar + ircParser.thinkNickname);
|
|
73
|
+ } else {
|
|
74
|
+ parser.getLocalClient().setNickname(altNickname);
|
|
75
|
+ triedAlt = true;
|
|
76
|
+ }
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ private void callDebugInfo(final Parser parser, final int level, final String data) {
|
|
81
|
+ parser.getCallbackManager().getCallback(DebugInfoListener.class)
|
|
82
|
+ .onDebugInfo(parser, new Date(), level, data);
|
|
83
|
+ }
|
|
84
|
+}
|