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.

WhoisResponseHandler.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2015 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.common.CallbackManager;
  24. import com.dmdirc.parser.events.NumericEvent;
  25. import com.dmdirc.parser.events.UserInfoEvent;
  26. import com.dmdirc.parser.events.UserInfoEvent.UserInfoType;
  27. import com.dmdirc.parser.interfaces.Parser;
  28. import java.util.Date;
  29. import java.util.EnumMap;
  30. import java.util.Map;
  31. import javax.annotation.Nullable;
  32. import net.engio.mbassy.listener.Handler;
  33. /**
  34. * Monitors for whois responses and raises a {@link UserInfoEvent} with the results.
  35. */
  36. public class WhoisResponseHandler {
  37. private final Parser parser;
  38. private final CallbackManager manager;
  39. private final Map<UserInfoType, String> info = new EnumMap<>(UserInfoType.class);
  40. @Nullable private String client;
  41. public WhoisResponseHandler(final Parser parser, final CallbackManager manager) {
  42. this.parser = parser;
  43. this.manager = manager;
  44. }
  45. public void start() {
  46. manager.subscribe(this);
  47. }
  48. public void stop() {
  49. manager.unsubscribe(this);
  50. }
  51. @Handler
  52. void handleNumericEvent(final NumericEvent event) {
  53. if (event.getNumeric() == 311) {
  54. // RPL_WHOISUSER
  55. client = event.getToken()[3];
  56. info.clear();
  57. }
  58. if (event.getNumeric() == 318 && client != null) {
  59. // RPL_ENDOFWHOIS
  60. sendEvent();
  61. client = null;
  62. }
  63. if (client != null && event.getToken().length > 4 && event.getToken()[3].equals(client)) {
  64. handleWhoisResponse(event.getNumeric(), event.getToken());
  65. }
  66. }
  67. private void handleWhoisResponse(final int numeric, final String... tokens) {
  68. switch (numeric) {
  69. case 301:
  70. // :server 301 DMDirc User :away message
  71. info.put(UserInfoType.AWAY_MESSAGE, tokens[4]);
  72. break;
  73. case 311:
  74. // :server 311 DMDirc User ~Ident host.dmdirc.com * :Real name
  75. info.put(UserInfoType.ADDRESS, tokens[3] + '!' + tokens[4] + '@' + tokens[5]);
  76. info.put(UserInfoType.REAL_NAME, tokens[7]);
  77. break;
  78. case 312:
  79. // :server 312 DMDirc User *.quakenet.org :QuakeNet IRC Server
  80. info.put(UserInfoType.SERVER_NAME, tokens[4]);
  81. info.put(UserInfoType.SERVER_INFO, tokens[5]);
  82. break;
  83. case 319:
  84. // :server 319 DMDirc User :@#channel1 +#channel2 ...
  85. info.put(UserInfoType.GROUP_CHAT_LIST, tokens[4]);
  86. break;
  87. case 317:
  88. // :server 317 DMDirc User 305 1422561556 :seconds idle, signon time
  89. info.put(UserInfoType.IDLE_TIME, tokens[4]);
  90. info.put(UserInfoType.CONNECTION_TIME, tokens[5]);
  91. break;
  92. case 330:
  93. // :server 330 DMDirc User Account :is authed as
  94. info.put(UserInfoType.ACCOUNT_NAME, tokens[4]);
  95. break;
  96. }
  97. }
  98. private void sendEvent() {
  99. manager.publish(new UserInfoEvent(parser, new Date(), parser.getClient(client), info));
  100. }
  101. }