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

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