Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

WhoisNumericFormatter.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.interfaces.ActionListener;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.interfaces.actions.ActionType;
  28. import com.dmdirc.interfaces.config.ConfigProvider;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. /**
  32. * Listens for whois-like numeric events and automatically formats them.
  33. *
  34. * @since 0.6.3
  35. */
  36. public class WhoisNumericFormatter implements ActionListener {
  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. /**
  42. * Creates a new whois numeric formatter that will add automatic formats to the specified
  43. * identity. This will normally be a temporary global identity.
  44. *
  45. * @param identity The identity to write formatters to
  46. */
  47. public WhoisNumericFormatter(final ConfigProvider identity) {
  48. this.identity = identity;
  49. }
  50. /**
  51. * Registers this this whois numeric formatter with the global actions manager.
  52. */
  53. public void register() {
  54. ActionManager.getActionManager().registerListener(this,
  55. CoreActionType.SERVER_NUMERIC,
  56. CoreActionType.SERVER_DISCONNECTED);
  57. }
  58. /** {@inheritDoc} */
  59. @Override
  60. public void processEvent(final ActionType type, final StringBuffer format,
  61. final Object... arguments) {
  62. if (CoreActionType.SERVER_DISCONNECTED == type) {
  63. handleServerDisconnected((Connection) arguments[0]);
  64. } else {
  65. handleNumeric((Connection) arguments[0], (Integer) arguments[1],
  66. (String[]) arguments[2], format);
  67. }
  68. }
  69. /**
  70. * Handles a server disconnected event. This clears any entry for that server in the
  71. * {@link #targets} map.
  72. *
  73. * @param server The server that was disconnected
  74. */
  75. private void handleServerDisconnected(final Connection server) {
  76. targets.remove(server);
  77. }
  78. /**
  79. * Handles a received numeric event. This method has special handling for numerics 311 and 318,
  80. * used to signal the start and end of a WHOIS request. It then monitors any other numerics
  81. * without formatters for events which look like WHOIS information, and formats them
  82. * automatically.
  83. *
  84. * @param server The server on which the event was received
  85. * @param numeric The numeric code of the event
  86. * @param arguments The arguments to the numeric event
  87. * @param format The format that should be used to display the event
  88. */
  89. private void handleNumeric(final Connection server, final int numeric,
  90. final String[] arguments, final StringBuffer format) {
  91. switch (numeric) {
  92. case 311: // RPL_WHOISUSER
  93. targets.put(server, arguments[3]);
  94. break;
  95. case 318: // RPL_ENDOFWHOIS
  96. targets.remove(server);
  97. break;
  98. default:
  99. if (arguments.length > 4
  100. && targets.containsKey(server)
  101. && arguments[3].equals(targets.get(server))) {
  102. // This numeric should be automatically formatted.
  103. if (format.length() > 0) {
  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(format.toString(), arguments.length);
  108. } else {
  109. // No custom formatter, switch it to an auto whois
  110. // format and target.
  111. final String target = "numeric_autowhois_" + (arguments.length - 4);
  112. ensureExists(target, arguments.length);
  113. format.replace(0, format.length(), target);
  114. }
  115. }
  116. break;
  117. }
  118. }
  119. /**
  120. * Ensures that the specified formatter exists in our identity.
  121. *
  122. * @param target The target to be checked and added if necessary
  123. * @param arguments The number of arguments for the numeric
  124. */
  125. private void ensureExists(final String target, final int arguments) {
  126. if (!identity.hasOptionString("formatter", target)) {
  127. final StringBuilder builder = new StringBuilder("%4$s %" + arguments + "$s");
  128. for (int i = 5; i < arguments; i++) {
  129. builder.append(" %");
  130. builder.append(i);
  131. builder.append("$s");
  132. }
  133. identity.setOption("formatter", target, builder.toString());
  134. }
  135. if (!identity.hasOptionString("notifications", target)) {
  136. identity.setOption("notifications", target, "group:whois");
  137. }
  138. }
  139. }