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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.time.LocalDateTime;
  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. // :server 311 DMDirc User ~Ident host.dmdirc.com * :Real name
  56. info.put(UserInfoType.ADDRESS,
  57. event.getToken()[3] + '!' + event.getToken()[4] + '@' + event.getToken()[5]);
  58. info.put(UserInfoType.REAL_NAME, event.getToken()[7]);
  59. }
  60. @Handler(condition = "msg.numeric == 318")
  61. void handleEndOfWhois(final NumericEvent event) {
  62. if (client != null) {
  63. sendEvent();
  64. client = null;
  65. }
  66. }
  67. @Handler(condition = "msg.numeric == 301")
  68. void handleAwayMessage(final NumericEvent event) {
  69. // :server 301 DMDirc User :away message
  70. info.put(UserInfoType.AWAY_MESSAGE, event.getToken()[4]);
  71. }
  72. @Handler(condition = "msg.numeric == 312")
  73. void handleServerInfo(final NumericEvent event) {
  74. // :server 312 DMDirc User *.quakenet.org :QuakeNet IRC Server
  75. info.put(UserInfoType.SERVER_NAME, event.getToken()[4]);
  76. info.put(UserInfoType.SERVER_INFO, event.getToken()[5]);
  77. }
  78. @Handler(condition = "msg.numeric == 313")
  79. void handleUserPrivileges(final NumericEvent event) {
  80. // :server 313 DMDirc User :is an IRC Operator
  81. info.put(UserInfoType.SERVER_OPER, event.getToken()[4]);
  82. }
  83. @Handler(condition = "msg.numeric == 378")
  84. void handleConnectingFrom(final NumericEvent event) {
  85. // :server 378 DMDirc User :is connecting from *@hostname.tld xx.xx.xx.xx
  86. if (client.equalsIgnoreCase(event.getToken()[3])) {
  87. info.put(UserInfoType.REAL_ADDRESS, event.getToken()[4]);
  88. }
  89. }
  90. @Handler(condition = "msg.numeric == 671")
  91. void handleSecureConnection(final NumericEvent event) {
  92. // :server 671 DMDirc User :is using a secure connection
  93. info.put(UserInfoType.CONNECTION_SECURITY, event.getToken()[4]);
  94. }
  95. @Handler(condition = "msg.numeric == 319")
  96. void handleChannelList(final NumericEvent event) {
  97. // :server 319 DMDirc User :@#channel1 +#channel2 ...
  98. info.put(UserInfoType.GROUP_CHAT_LIST, event.getToken()[4]);
  99. }
  100. @Handler(condition = "msg.numeric == 317")
  101. void handleIdleTime(final NumericEvent event) {
  102. // :server 317 DMDirc User 305 1422561556 :seconds idle, signon time
  103. info.put(UserInfoType.IDLE_TIME, event.getToken()[4]);
  104. info.put(UserInfoType.CONNECTION_TIME, event.getToken()[5]);
  105. }
  106. @Handler(condition = "msg.numeric == 330")
  107. void handleAccount(final NumericEvent event) {
  108. // :server 330 DMDirc User Account :is authed as
  109. info.put(UserInfoType.ACCOUNT_NAME, event.getToken()[4]);
  110. }
  111. private void sendEvent() {
  112. manager.publish(new UserInfoEvent(parser, LocalDateTime.now(),
  113. parser.getClient(client), info));
  114. }
  115. }