Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.addons.urlcatcher;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.Raw;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.actions.CoreActionType;
  27. import com.dmdirc.actions.interfaces.ActionType;
  28. import com.dmdirc.commandparser.CommandManager;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.interfaces.ActionListener;
  31. import com.dmdirc.interfaces.ConfigChangeListener;
  32. import com.dmdirc.plugins.Plugin;
  33. import com.dmdirc.ui.messages.Styliser;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. /**
  37. * A plugin which logs all observed URLs and allows the user to list them
  38. * later.
  39. *
  40. * @author chris
  41. */
  42. public class UrlCatcherPlugin extends Plugin implements ActionListener,
  43. ConfigChangeListener {
  44. private final Map<String, Integer> urls = new HashMap<String, Integer>();
  45. /** Whether to capture URLs from the raw window. */
  46. private boolean captureRaw = false;
  47. private final UrlListCommand command = new UrlListCommand(this);
  48. /** {@inheritDoc} */
  49. @Override
  50. public void onLoad() {
  51. ActionManager.addListener(this, CoreActionType.CLIENT_LINE_ADDED);
  52. CommandManager.registerCommand(command);
  53. IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
  54. updateConfig();
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public void onUnload() {
  59. ActionManager.removeListener(this);
  60. CommandManager.unregisterCommand(command);
  61. IdentityManager.getGlobalConfig().removeListener(this);
  62. }
  63. /**
  64. * Re-reads the configuration of the URL catcher plugin.
  65. */
  66. private void updateConfig() {
  67. captureRaw = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "captureraw");
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void processEvent(final ActionType type, final StringBuffer format,
  72. final Object... arguments) {
  73. if (arguments[0] instanceof Raw && !captureRaw) {
  74. return;
  75. }
  76. final String message = ((FrameContainer<?>) arguments[0]).getStyliser()
  77. .doLinks((String) arguments[1]);
  78. if (message.indexOf(Styliser.CODE_HYPERLINK) > -1) {
  79. final String[] parts = message.split("" + Styliser.CODE_HYPERLINK);
  80. for (int i = 1; i < parts.length; i += 2) {
  81. addURL(parts[i]);
  82. }
  83. }
  84. }
  85. /**
  86. * Adds an URL to the list of tracked URLs, or increases its counter by one.
  87. *
  88. * @param url The URL to be added
  89. */
  90. private void addURL(final String url) {
  91. if (urls.containsKey(url)) {
  92. urls.put(url, urls.get(url) + 1);
  93. } else {
  94. urls.put(url, 1);
  95. }
  96. }
  97. /**
  98. * Retrieves the URLs that have been recorded.
  99. *
  100. * @return A map of URLs to the number of times they've been seen.
  101. */
  102. public Map<String, Integer> getURLS() {
  103. return urls;
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public void configChanged(final String domain, final String key) {
  108. updateConfig();
  109. }
  110. }