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.

WhoisNumericFormatter.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.ui.messages;
  23. import com.dmdirc.ClientModule;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.events.ServerDisconnectedEvent;
  26. import com.dmdirc.events.ServerNumericEvent;
  27. import com.dmdirc.interfaces.Connection;
  28. import com.dmdirc.interfaces.SystemLifecycleComponent;
  29. import com.dmdirc.interfaces.config.ConfigProvider;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import javax.inject.Inject;
  33. import javax.inject.Singleton;
  34. import net.engio.mbassy.listener.Handler;
  35. /**
  36. * Listens for whois-like numeric events and automatically formats them.
  37. *
  38. * @since 0.6.3
  39. */
  40. @Singleton
  41. public class WhoisNumericFormatter implements SystemLifecycleComponent {
  42. /** The name of the target of any current whois requests. */
  43. private final Map<Connection, String> targets = new HashMap<>();
  44. /** The identity to add formatters to. */
  45. private final ConfigProvider identity;
  46. /** Event bus to subscribe to events on. */
  47. private final DMDircMBassador eventBus;
  48. /**
  49. * Creates a new whois numeric formatter that will add automatic formats to the specified
  50. * identity. This will normally be a temporary global identity.
  51. *
  52. * @param identity The identity to write formatters to
  53. * @param eventBus The event bus to subscribe to events on
  54. */
  55. @Inject
  56. public WhoisNumericFormatter(
  57. @ClientModule.AddonConfig final ConfigProvider identity,
  58. final DMDircMBassador eventBus) {
  59. this.identity = identity;
  60. this.eventBus = eventBus;
  61. }
  62. /**
  63. * Handles a server disconnected event. This clears any entry for that server in the
  64. * {@link #targets} map.
  65. *
  66. * @param event The server disconnected event to process
  67. */
  68. @Handler
  69. public void handleServerDisconnected(final ServerDisconnectedEvent event) {
  70. targets.remove(event.getConnection());
  71. }
  72. /**
  73. * Handles a received numeric event. This method has special handling for numerics 311 and 318,
  74. * used to signal the start and end of a WHOIS request. It then monitors any other numerics
  75. * without formatters for events which look like WHOIS information, and formats them
  76. * automatically.
  77. *
  78. * @param event The server numeric event to process
  79. */
  80. @Handler
  81. public void handleNumeric(final ServerNumericEvent event) {
  82. final Connection server = event.getConnection();
  83. final int numeric = event.getNumeric();
  84. final String[] arguments = event.getArgs();
  85. switch (numeric) {
  86. case 311: // RPL_WHOISUSER
  87. targets.put(server, arguments[3]);
  88. break;
  89. case 318: // RPL_ENDOFWHOIS
  90. targets.remove(server);
  91. break;
  92. default:
  93. if (arguments.length > 4
  94. && targets.containsKey(server)
  95. && arguments[3].equals(targets.get(server))) {
  96. // This numeric should be automatically formatted.
  97. if (event.getDisplayFormat().isEmpty()) {
  98. // No custom formatter, switch it to an auto whois
  99. // format and target.
  100. final String target = "numeric_autowhois_" + (arguments.length - 4);
  101. ensureExists(target, arguments.length);
  102. event.setDisplayFormat(target);
  103. } else {
  104. // There's a custom format. We'll see if we need to
  105. // add a formatter or notification settings for it
  106. // anyway.
  107. ensureExists(event.getDisplayFormat(), arguments.length);
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. /**
  114. * Ensures that the specified formatter exists in our identity.
  115. *
  116. * @param target The target to be checked and added if necessary
  117. * @param arguments The number of arguments for the numeric
  118. */
  119. private void ensureExists(final String target, final int arguments) {
  120. if (!identity.hasOptionString("formatter", target)) {
  121. final StringBuilder builder = new StringBuilder("%4$s %" + arguments + "$s");
  122. for (int i = 5; i < arguments; i++) {
  123. builder.append(" %");
  124. builder.append(i);
  125. builder.append("$s");
  126. }
  127. identity.setOption("formatter", target, builder.toString());
  128. }
  129. if (!identity.hasOptionString("notifications", target)) {
  130. identity.setOption("notifications", target, "group:whois");
  131. }
  132. }
  133. @Override
  134. public void startUp() {
  135. eventBus.subscribe(this);
  136. }
  137. @Override
  138. public void shutDown() {
  139. eventBus.unsubscribe(this);
  140. }
  141. }