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

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