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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2017 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.events.NickInUseEvent;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import java.time.LocalDateTime;
  27. import net.engio.mbassy.listener.Handler;
  28. import net.engio.mbassy.listener.Listener;
  29. import net.engio.mbassy.listener.References;
  30. /**
  31. * Simple nick in use handler that tries the alternative nickname, then prepends a character until
  32. * it gets a nickname.
  33. */
  34. @Listener(references = References.Strong)
  35. public class SimpleNickInUseHandler {
  36. private final String altNickname;
  37. private final char prependChar;
  38. private boolean triedAlt;
  39. SimpleNickInUseHandler(final String altNickname, final char prependChar) {
  40. this.altNickname = altNickname;
  41. this.prependChar = prependChar;
  42. }
  43. @Handler
  44. public void onNickInUse(final NickInUseEvent event) {
  45. final IRCParser parser = (IRCParser) event.getParser();
  46. callDebugInfo(parser, IRCParser.DEBUG_INFO, "No Nick in use Handler.");
  47. if (!parser.got001) {
  48. callDebugInfo(parser, IRCParser.DEBUG_INFO, "Using inbuilt handler");
  49. // If this is before 001 we will try and get a nickname, else we will leave the
  50. // nick as-is
  51. if (triedAlt) {
  52. final String magicAltNick = prependChar + parser.getMyInfo().getNickname();
  53. if (parser.getStringConverter().equalsIgnoreCase(parser.thinkNickname,
  54. altNickname)
  55. && !altNickname.equalsIgnoreCase(magicAltNick)) {
  56. parser.thinkNickname = parser.getMyInfo().getNickname();
  57. }
  58. parser.getLocalClient().setNickname(prependChar + parser.thinkNickname);
  59. } else {
  60. parser.getLocalClient().setNickname(altNickname);
  61. triedAlt = true;
  62. }
  63. }
  64. }
  65. private void callDebugInfo(final Parser parser, final int level, final String data) {
  66. parser.getCallbackManager().publish(
  67. new DebugInfoEvent(parser, LocalDateTime.now(), level, data));
  68. }
  69. /**
  70. * Installs a new {@link SimpleNickInUseHandler} on the given parser.
  71. *
  72. * @param parser The parser to install the handler on.
  73. * @param altNickname The alternate nickname to try using.
  74. * @param prependChar The character to prepend if the alt nickname is in use.
  75. */
  76. public static void install(
  77. final Parser parser, final String altNickname, final char prependChar) {
  78. parser.getCallbackManager().subscribe(new SimpleNickInUseHandler(altNickname, prependChar));
  79. }
  80. }