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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(condition = "msg.numeric == 311")
  52. void handleStartOfWhois(final NumericEvent event) {
  53. client = event.getToken()[3];
  54. info.clear();
  55. }
  56. @Handler(condition = "msg.numeric == 318")
  57. void handleEndOfWhois(final NumericEvent event) {
  58. if (client != null) {
  59. sendEvent();
  60. client = null;
  61. }
  62. }
  63. @Handler(condition = "msg.numeric == 301")
  64. void handleAwayMessage(final NumericEvent event) {
  65. // :server 301 DMDirc User :away message
  66. info.put(UserInfoType.AWAY_MESSAGE, event.getToken()[4]);
  67. }
  68. @Handler(condition = "msg.numeric == 311")
  69. void handleUserInfo(final NumericEvent event) {
  70. // :server 311 DMDirc User ~Ident host.dmdirc.com * :Real name
  71. info.put(UserInfoType.ADDRESS,
  72. event.getToken()[3] + '!' + event.getToken()[4] + '@' + event.getToken()[5]);
  73. info.put(UserInfoType.REAL_NAME, event.getToken()[7]);
  74. }
  75. @Handler(condition = "msg.numeric == 312")
  76. void handleServerInfo(final NumericEvent event) {
  77. // :server 312 DMDirc User *.quakenet.org :QuakeNet IRC Server
  78. info.put(UserInfoType.SERVER_NAME, event.getToken()[4]);
  79. info.put(UserInfoType.SERVER_INFO, event.getToken()[5]);
  80. }
  81. @Handler(condition = "msg.numeric == 319")
  82. void handleChannelList(final NumericEvent event) {
  83. // :server 319 DMDirc User :@#channel1 +#channel2 ...
  84. info.put(UserInfoType.GROUP_CHAT_LIST, event.getToken()[4]);
  85. }
  86. @Handler(condition = "msg.numeric == 317")
  87. void handleIdleTime(final NumericEvent event) {
  88. // :server 317 DMDirc User 305 1422561556 :seconds idle, signon time
  89. info.put(UserInfoType.IDLE_TIME, event.getToken()[4]);
  90. info.put(UserInfoType.CONNECTION_TIME, event.getToken()[5]);
  91. }
  92. @Handler(condition = "msg.numeric == 330")
  93. void handleAccount(final NumericEvent event) {
  94. // :server 330 DMDirc User Account :is authed as
  95. info.put(UserInfoType.ACCOUNT_NAME, event.getToken()[4]);
  96. }
  97. private void sendEvent() {
  98. manager.publish(new UserInfoEvent(parser, new Date(), parser.getClient(client), info));
  99. }
  100. }