You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleNickInUseHandler.java 3.6KB

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