Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SimpleNickInUseHandler.java 3.0KB

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