Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

WhoisNumericFormatter.java 6.0KB

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